diff --git a/libftasm/Makefile b/libftasm/Makefile index 43ddd412..e4897b34 100644 --- a/libftasm/Makefile +++ b/libftasm/Makefile @@ -22,11 +22,12 @@ all: $(NAME) $(D_OBJ)/%.o: $(D_SRC)/%.c @$(MKDIR) $(D_OBJ) @$(CC) $(W_FLAGS) -c $< -o $@ $(DEBUG) - @echo "Compiling "$<"..." + @echo "(libft) Compiling "$<"..." $(NAME): $(F_OBJ) - $(AR) $(NAME) $(F_OBJ) - ranlib $(NAME) + @$(AR) $(NAME) $(F_OBJ) + @echo "(libft) Linking "$@"..." + @ranlib $(NAME) clean: $(RM) $(D_OBJ) diff --git a/libftasm/ft_debug.c b/libftasm/ft_debug.c index c5ee3616..4e9b18ee 100644 --- a/libftasm/ft_debug.c +++ b/libftasm/ft_debug.c @@ -6,7 +6,6 @@ void ft_debug(void) static int n = 0; n++; - ft_putendl("----------"); - printf(" check %02i\n", n); - ft_putendl("----------"); + printf("----------\n check %02i\n----------\n", n); + fflush(stdout); } diff --git a/libftasm/ft_diff.c b/libftasm/ft_diff.c new file mode 100644 index 00000000..137ade0c --- /dev/null +++ b/libftasm/ft_diff.c @@ -0,0 +1,6 @@ +#include "libft.h" + +int ft_diff(void *a, void *b) +{ + return (*(int *)a - *(int *)b); +} diff --git a/libftasm/ft_id.c b/libftasm/ft_id.c new file mode 100644 index 00000000..ab2bb70f --- /dev/null +++ b/libftasm/ft_id.c @@ -0,0 +1,6 @@ +#include "libft.h" + +void *ft_id(void *a) +{ + return (a); +} diff --git a/libftasm/ft_lst_find.c b/libftasm/ft_lst_find.c new file mode 100644 index 00000000..716ec65e --- /dev/null +++ b/libftasm/ft_lst_find.c @@ -0,0 +1,18 @@ +#include "libft.h" + +t_list *ft_list_find( + t_list *begin_list, + void *data_ref, + int (*cmp)()) +{ + t_list *list_ptr; + + list_ptr = begin_list; + while (list_ptr) + { + if ((*cmp)(list_ptr->content, data_ref) == 0) + return (list_ptr); + list_ptr = list_ptr->next; + } + return (list_ptr); +} diff --git a/libftasm/ft_lst_print.c b/libftasm/ft_lst_print.c new file mode 100644 index 00000000..f681f06d --- /dev/null +++ b/libftasm/ft_lst_print.c @@ -0,0 +1,13 @@ +#include "libft.h" + +void ft_lst_print(t_list *list, void (*printer)()) +{ + while (list) + { + ft_putstr("["); + (*printer)(*(int *)list->content); + ft_putstr("]->"); + list = list->next; + } + ft_putendl("X\n"); +} diff --git a/libftasm/ft_lst_print2.c b/libftasm/ft_lst_print2.c new file mode 100644 index 00000000..9721f722 --- /dev/null +++ b/libftasm/ft_lst_print2.c @@ -0,0 +1,25 @@ +#include "libft.h" + +void ft_lst_print2(t_list *list, void (*printer)()) +{ + t_list *list2; + + while (list) + { + ft_putendl("---"); + list2 = *(t_list**)list->content; + while (list2) + { + ft_putstr("["); + (*printer)(*(int *)list2->content); + ft_putstr("]->"); + list2 = list2->next; + } + ft_putendl("X"); + ft_putendl("---"); + ft_putendl(" |"); + ft_putendl(" V"); + list = list->next; + } + ft_putendl(" X\n"); +} diff --git a/libftasm/ft_lst_remove_if.c b/libftasm/ft_lst_remove_if.c new file mode 100644 index 00000000..2e96a970 --- /dev/null +++ b/libftasm/ft_lst_remove_if.c @@ -0,0 +1,35 @@ +#include "libft.h" + +void ft_lst_remove_if( + t_list **begin_list, + void *data_ref, + int (*cmp)()) +{ + t_list *last; + t_list *current; + t_list *tmp; + + last = NULL; + current = *begin_list; + tmp = NULL; + fflush(stdout); + while (current) + { + if ((*cmp)(current->content, &data_ref) == 0) + { + if (current == *begin_list) + *begin_list = current->next; + else + last->next = current->next; + tmp = current; + current = current->next; + free(tmp); + } + else + { + last = current; + current = current->next; + } + } +} + diff --git a/libftasm/ft_lst_remove_sub.c b/libftasm/ft_lst_remove_sub.c new file mode 100644 index 00000000..e419b285 --- /dev/null +++ b/libftasm/ft_lst_remove_sub.c @@ -0,0 +1,31 @@ +#include "libft.h" + +void ft_lstdelsub(t_list **alst, t_list *sub, int (*cmp)(), void (*del)(void *, size_t)) +{ + t_list *last; + t_list *current; + t_list *tmp; + + last = NULL; + current = *alst; + tmp = NULL; + while (current && sub) + { + if ((*cmp)(current->content, sub->content) == 0) + { + if (current == *alst) + *alst = current->next; + else + last->next = current->next; + tmp = current; + current = current->next; + sub = sub->next; + ft_lstdelone(&tmp, del); + } + else + { + last = current; + current = current->next; + } + } +} diff --git a/libftasm/ft_lst_size.c b/libftasm/ft_lst_size.c new file mode 100644 index 00000000..bdb1895e --- /dev/null +++ b/libftasm/ft_lst_size.c @@ -0,0 +1,18 @@ +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + if (lst) + { + i = 1; + while (lst->next) + { + lst = lst->next; + i++; + } + } + return (i); +} diff --git a/libftasm/ft_lst_sorted_insert.c b/libftasm/ft_lst_sorted_insert.c new file mode 100644 index 00000000..40fcd2fd --- /dev/null +++ b/libftasm/ft_lst_sorted_insert.c @@ -0,0 +1,26 @@ +#include "libft.h" + +void ft_lst_sorted_insert(t_list **begin_list, t_list *insert, int (*cmp)()) +{ + t_list *link; + + link = *begin_list; + if (!link || (*cmp)(insert->content, link->content) < 0) + { + *begin_list = insert; + insert->next = link ? link : NULL; + return ; + } + while (link->next) + { + if ((*cmp)(insert->content, link->content) > 0 + && (*cmp)(insert->content, link->next->content) <= 0) + { + insert->next = link->next; + link->next = insert; + return ; + } + link = link->next; + } + link->next = insert; +} diff --git a/libftasm/ft_lst_sorted_merge.c b/libftasm/ft_lst_sorted_merge.c new file mode 100644 index 00000000..eb5e0d5d --- /dev/null +++ b/libftasm/ft_lst_sorted_merge.c @@ -0,0 +1,32 @@ +#include "libft.h" + +void ft_lst_sorted_merge( + t_list **begin_list1, + t_list *begin_list2, + int (*cmp)()) +{ + t_list *tail; + t_list *head1; + t_list *head2; + int comp; + + if (!*begin_list1 || !begin_list2) + { + *begin_list1 = begin_list2 ? begin_list2 : *begin_list1; + return ; + } + comp = (*cmp)(begin_list2->content, (*begin_list1)->content); + head1 = (comp < 0) ? *begin_list1 : (*begin_list1)->next; + head2 = (comp < 0) ? begin_list2->next : begin_list2; + *begin_list1 = (comp < 0) ? begin_list2 : *begin_list1; + tail = *begin_list1; + while (head1 && head2) + { + comp = (*cmp)(head2->content, head1->content); + tail->next = (comp < 0 ? head2 : head1); + head1 = comp < 0 ? head1 : head1->next; + head2 = comp < 0 ? head2->next : head2; + tail = tail->next; + } + tail->next = head2 ? head2 : head1; +} diff --git a/libftasm/ft_lstadd.c b/libftasm/ft_lstadd.c index e9042877..068fdaf3 100644 --- a/libftasm/ft_lstadd.c +++ b/libftasm/ft_lstadd.c @@ -2,6 +2,9 @@ void ft_lstadd(t_list **alst, t_list *new) { - new->next = *alst; - *alst = new; + if (new) + { + new->next = *alst; + *alst = new; + } } diff --git a/libftasm/ft_lstdelone.c b/libftasm/ft_lstdelone.c new file mode 100644 index 00000000..e2de5a66 --- /dev/null +++ b/libftasm/ft_lstdelone.c @@ -0,0 +1,8 @@ +#include "libft.h" + +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)) +{ + (*del)((*alst)->content, (*alst)->content_size); + free(*alst); + *alst = NULL; +} diff --git a/libftasm/ft_lstiter.c b/libftasm/ft_lstiter.c new file mode 100644 index 00000000..0756d531 --- /dev/null +++ b/libftasm/ft_lstiter.c @@ -0,0 +1,10 @@ +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)) +{ + while (lst) + { + (*f)(lst); + lst = lst->next; + } +} diff --git a/libftasm/ft_lstlast.c b/libftasm/ft_lstlast.c index 5115a1fe..daaa94f4 100644 --- a/libftasm/ft_lstlast.c +++ b/libftasm/ft_lstlast.c @@ -2,7 +2,8 @@ t_list *ft_lstlast(t_list *lst) { - while (lst->next) - lst = lst->next; + if (lst) + while (lst->next) + lst = lst->next; return (lst); } diff --git a/libftasm/ft_lstmap.c b/libftasm/ft_lstmap.c new file mode 100644 index 00000000..9c8a3edd --- /dev/null +++ b/libftasm/ft_lstmap.c @@ -0,0 +1,24 @@ +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)) +{ + t_list *out; + t_list *elem; + + out = NULL; + while (lst) + { + elem = (*f)(lst); + /* printf("size=%zu\n", elem->content_size); */ + /* printf("content=%i\n", *(int*)elem->content); */ + /* fflush(stdout); */ + elem = ft_lstnew(elem->content, elem->content_size); + /* printf("size=%zu\n", elem->content_size); */ + /* printf("content=%i\n", *(int*)elem->content); */ + /* fflush(stdout); */ + ft_lsteadd(&out, elem); + lst = lst->next; + } + /* ft_lst_print(out, &ft_putnbr); */ + return (out); +} diff --git a/libftasm/ft_lstnew.c b/libftasm/ft_lstnew.c index 76a1ba39..fee8bc78 100644 --- a/libftasm/ft_lstnew.c +++ b/libftasm/ft_lstnew.c @@ -2,26 +2,25 @@ t_list *ft_lstnew(void const *content, size_t content_size) { - t_list *link; + t_list *new; if (!content) { - link = malloc(1); - if (!link) + new = malloc(sizeof(*new)); + if (!new) return (NULL); - link->next = NULL; - link->content_size = 0; - link->content = NULL; + new->content_size = 0; + new->content = NULL; } else { - link = (t_list *)malloc(sizeof(link)); - if (!link) + new = (t_list *)malloc(sizeof(*new)); + if (!new) return (NULL); - link->content_size = content_size; - link->next = NULL; - link->content = ft_memalloc(content_size); - ft_memcpy(link->content, content, content_size); + new->content_size = content_size; + new->content = ft_memalloc(content_size + 1); + ft_memcpy(new->content, content, content_size); } - return (link); + new->next = NULL; + return (new); } diff --git a/libftasm/ft_lstnew_range.c b/libftasm/ft_lstnew_range.c new file mode 100644 index 00000000..985a5182 --- /dev/null +++ b/libftasm/ft_lstnew_range.c @@ -0,0 +1,16 @@ +#include "libft.h" + +t_list *ft_lstnew_range(int a, int b) +{ + t_list *lst; + + if (a >= b) + return (NULL); + lst = NULL; + while (a < b) + { + b--; + ft_lstadd(&lst, ft_lstnew(&b, sizeof(int))); + } + return (lst); +} diff --git a/libftasm/ft_lstsort.c b/libftasm/ft_lstsort.c new file mode 100644 index 00000000..1b7eb7d0 --- /dev/null +++ b/libftasm/ft_lstsort.c @@ -0,0 +1,37 @@ +#include "libft.h" + +static void ft_list_swap(t_list **current) +{ + t_list *tmp; + + tmp = (*current)->next->next; + (*current)->next->next = (*current); + (*current)->next = tmp; +} + +void ft_list_sort(t_list **begin_list, int (*cmp)()) +{ + t_list *current; + t_list *last; + + current = *begin_list; + if (!*begin_list) + return ; + while (current->next) + { + if ((*cmp)(current->content, current->next->content) > 0) + { + if (current != *begin_list) + last->next = current->next; + else + *begin_list = current->next; + ft_list_swap(¤t); + current = *begin_list; + } + else + { + last = current; + current = current->next ? current->next : current; + } + } +} diff --git a/libftasm/ft_memcpy.c b/libftasm/ft_memcpy.c index 5e6f7428..c4ee7141 100644 --- a/libftasm/ft_memcpy.c +++ b/libftasm/ft_memcpy.c @@ -2,10 +2,15 @@ void *ft_memcpy(void *dst, const void *src, size_t n) { - size_t i; + char *c1; + char *c2; - i = -1; - while (++i < n) - ((char *)dst)[i] = *(char *)src++; + if (n == 0 || dst == src) + return (dst); + c1 = (char *)dst; + c2 = (char *)src; + while (--n) + *c1++ = *c2++; + *c1 = *c2; return (dst); } diff --git a/libftasm/libft.h b/libftasm/libft.h index c2e95ece..c03f0d44 100644 --- a/libftasm/libft.h +++ b/libftasm/libft.h @@ -2,6 +2,7 @@ #define LIBFT_H # include # include +# include # include # define SEP(x) (x == ' ' || x == '\t' || x == '\n') # define ABS(x) (((x) < 0) ? -(x) : (x)) @@ -75,9 +76,20 @@ void ft_putendl_fd(char const *s, int fd); void ft_putnbr_fd(int n, int fd); t_list *ft_lstnew(void const *content, size_t content_size); +void ft_lstdelone(t_list **alst, void (*del)(void *, size_t)); void ft_lstadd(t_list **alst, t_list *new); +void ft_lstiter(t_list *lst, void (*f)(t_list *elem)); +t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem)); + +void ft_lst_print(t_list *list, void (*printer)()); +int ft_lstsize(t_list *lst); void ft_lsteadd(t_list **alst, t_list *new); void ft_lstnadd(t_list **alst, t_list *new, int n); -t_list *ft_lstlast(t_list *lst) +t_list *ft_lstlast(t_list *lst); +void ft_lst_sorted_merge(t_list **begin_list1, t_list *begin_list2, int (*cmp)()); +void ft_lst_sorted_insert(t_list **begin_list, t_list *insert, int (*cmp)()); +t_list *ft_lstnew_range(int a, int b); +int ft_diff(void *a, void *b); +void *ft_id(void *a); #endif