new lst functions and extras
This commit is contained in:
parent
a8da79f3c1
commit
258a00cfeb
22 changed files with 353 additions and 28 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
6
libft/ft_diff.c
Normal file
6
libft/ft_diff.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include "libft.h"
|
||||
|
||||
int ft_diff(void *a, void *b)
|
||||
{
|
||||
return (*(int *)a - *(int *)b);
|
||||
}
|
||||
6
libft/ft_id.c
Normal file
6
libft/ft_id.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#include "libft.h"
|
||||
|
||||
void *ft_id(void *a)
|
||||
{
|
||||
return (a);
|
||||
}
|
||||
18
libft/ft_lst_find.c
Normal file
18
libft/ft_lst_find.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
13
libft/ft_lst_print.c
Normal file
13
libft/ft_lst_print.c
Normal file
|
|
@ -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");
|
||||
}
|
||||
25
libft/ft_lst_print2.c
Normal file
25
libft/ft_lst_print2.c
Normal file
|
|
@ -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");
|
||||
}
|
||||
35
libft/ft_lst_remove_if.c
Normal file
35
libft/ft_lst_remove_if.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
libft/ft_lst_remove_sub.c
Normal file
31
libft/ft_lst_remove_sub.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
libft/ft_lst_size.c
Normal file
18
libft/ft_lst_size.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
26
libft/ft_lst_sorted_insert.c
Normal file
26
libft/ft_lst_sorted_insert.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
32
libft/ft_lst_sorted_merge.c
Normal file
32
libft/ft_lst_sorted_merge.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -1,7 +1,10 @@
|
|||
#include "libft.h"
|
||||
|
||||
void ft_lstadd(t_list **alst, t_list *new)
|
||||
{
|
||||
if (new)
|
||||
{
|
||||
new->next = *alst;
|
||||
*alst = new;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
8
libft/ft_lstdelone.c
Normal file
8
libft/ft_lstdelone.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
10
libft/ft_lstiter.c
Normal file
10
libft/ft_lstiter.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
t_list *ft_lstlast(t_list *lst)
|
||||
{
|
||||
if (lst)
|
||||
while (lst->next)
|
||||
lst = lst->next;
|
||||
return (lst);
|
||||
|
|
|
|||
24
libft/ft_lstmap.c
Normal file
24
libft/ft_lstmap.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
16
libft/ft_lstnew_range.c
Normal file
16
libft/ft_lstnew_range.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
37
libft/ft_lstsort.c
Normal file
37
libft/ft_lstsort.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define LIBFT_H
|
||||
# include <string.h>
|
||||
# include <unistd.h>
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# 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
|
||||
|
|
|
|||
Loading…
Reference in a new issue