From 69b65b653abbb4f61d9a65787aefdca414e561e4 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 10 Mar 2024 20:05:57 +0100 Subject: [PATCH] feat(list_remove_if) --- Makefile | 1 + ft_list_remove_if.s | 61 +++++++++++++++++++++++++++++++++++++++++++++ libasm.h | 2 ++ main.c | 19 ++++++++++++-- 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 ft_list_remove_if.s diff --git a/Makefile b/Makefile index 8b0ce74..97134bb 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ SRC = \ ft_list_size.s \ ft_list_push_front.s \ ft_list_sort.s \ + ft_list_remove_if.s \ OBJ = $(SRC:.s=.o) diff --git a/ft_list_remove_if.s b/ft_list_remove_if.s new file mode 100644 index 0000000..79dbf2a --- /dev/null +++ b/ft_list_remove_if.s @@ -0,0 +1,61 @@ +global ft_list_remove_if +extern free + +ft_list_remove_if: + push rsp ; push callee saved registers + push rbx + push r12 + push r13 + push r14 + push r15 + push rsi + mov r14, rdx ; save cmp func + mov r15, rcx ; save free func + mov r13, rdi ; save begin_list + mov rbx, 0 ; prev = null + mov r12, [r13] ; curr = *begin_list + +.loop: + cmp r12, 0 + je .done + mov rdi, [r12] + pop rsi + push rsi + call r14 + cmp rax, 0 + jne .next + cmp rbx, 0 + je .first_elem + mov rdi, [r12] + call r15 + mov rax, [r12 + 8] + mov [rbx + 8], rax + mov rdi, r12 + call free WRT ..plt + mov r12, [rbx + 8] + jmp .loop + +.first_elem: + mov rdi, [r12] + call r15 + mov rax, [r12 + 8] + mov [r13], rax + mov rdi, r12 + call free WRT ..plt + mov r12, [r13] + jmp .loop + +.next: + mov rbx, r12 ; prev = curr + mov r12, [r12 + 8] ; curr = curr->next + jmp .loop + +.done: + pop rsi + pop r15 + pop r14 + pop r13 + pop r12 + pop rbx + pop rsp + ret diff --git a/libasm.h b/libasm.h index 5709d6b..2ee108b 100644 --- a/libasm.h +++ b/libasm.h @@ -20,7 +20,9 @@ char *ft_strdup(const char *); void ft_list_push_front(t_list **, void *); int ft_list_size(t_list *); void ft_list_sort(t_list **, int (*)()); +void ft_list_remove_if(t_list **, void *, int (*)(), void (*)(void *)); void ft_list_print(t_list *begin_list); #endif + diff --git a/main.c b/main.c index 82ee520..e9dc583 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,10 @@ #include #include "libasm.h" +void do_nothing(void *data) { + (void)data; +} + int ft_strcmp(const char *s1, const char *s2) { printf("s1: %ss2: %s", s1, s2); while (*s1 && *s2 && *s1 == *s2) { @@ -13,6 +17,7 @@ int ft_strcmp(const char *s1, const char *s2) { } int main(void) { + /* t_list *list; list = NULL; @@ -86,13 +91,23 @@ int main(void) { printf("\n"); ft_list_print(begin_ok); printf("\n"); - +*/ t_list *c = NULL; ft_list_push_front(&c, "b\n"); ft_list_push_front(&c, "c\n"); ft_list_push_front(&c, "a\n"); + ft_list_push_front(&c, "c\n"); + ft_list_push_front(&c, "a\n"); + ft_list_push_front(&c, "b\n"); + ft_list_push_front(&c, "c\n"); + ft_list_push_front(&c, "c\n"); + ft_list_push_front(&c, "b\n"); + ft_list_push_front(&c, "a\n"); + ft_list_push_front(&c, "b\n"); ft_list_print(c); - ft_list_sort(&c, *ft_strcmp); +// ft_list_sort(&c, *ft_strcmp); + ft_list_print(c); + ft_list_remove_if(&c, "b\n", &ft_strcmp, &do_nothing); ft_list_print(c); return 0;