diff options
Diffstat (limited to 'src/lst')
| -rw-r--r-- | src/lst/ft_lstadd_back.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstadd_front.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstbsearch.c | 57 | ||||
| -rw-r--r-- | src/lst/ft_lstclear.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstdelone.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstiter.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstlast.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstmap.c | 9 | ||||
| -rw-r--r-- | src/lst/ft_lstnew.c | 7 | ||||
| -rw-r--r-- | src/lst/ft_lstpop_front.c | 6 | ||||
| -rw-r--r-- | src/lst/ft_lstremove_if.c | 33 | ||||
| -rw-r--r-- | src/lst/ft_lstreverse.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstreverse_ret.c | 7 | ||||
| -rw-r--r-- | src/lst/ft_lstsize.c | 3 | ||||
| -rw-r--r-- | src/lst/ft_lstsort.c | 40 | ||||
| -rw-r--r-- | src/lst/ft_lstsorted_merge.c | 36 |
16 files changed, 198 insertions, 21 deletions
diff --git a/src/lst/ft_lstadd_back.c b/src/lst/ft_lstadd_back.c index 01eb00c..8f39a75 100644 --- a/src/lst/ft_lstadd_back.c +++ b/src/lst/ft_lstadd_back.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -void ft_lstadd_back(t_list **alst, t_list *new) +void ft_lstadd_back(t_ftlst **alst, t_ftlst *new) { if (alst == NULL) return ; diff --git a/src/lst/ft_lstadd_front.c b/src/lst/ft_lstadd_front.c index 282b0b4..bcd5ad9 100644 --- a/src/lst/ft_lstadd_front.c +++ b/src/lst/ft_lstadd_front.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -void ft_lstadd_front(t_list **alst, t_list *new) +void ft_lstadd_front(t_ftlst **alst, t_ftlst *new) { if (alst == NULL || new == NULL) return ; diff --git a/src/lst/ft_lstbsearch.c b/src/lst/ft_lstbsearch.c new file mode 100644 index 0000000..25a7b4c --- /dev/null +++ b/src/lst/ft_lstbsearch.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstbsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */ +/* Updated: 2020/01/31 10:42:22 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_lst.h" + +static t_ftlst *st_lstmiddle(t_ftlst *lst, t_ftlst *last) +{ + t_ftlst *slow; + t_ftlst *fast; + + if (lst == NULL) + return (NULL); + slow = lst; + fast = lst; + while (fast != last) + { + fast = fast->next; + if (fast == NULL) + break ; + slow = slow->next; + fast = fast->next; + } + return (slow); +} + +static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last, + t_ftbool (*equal)(void *ref, void *content), void *ref) +{ + t_ftlst *mid; + t_ftlst *left; + + if (lst == NULL) + return (NULL); + mid = st_lstmiddle(lst, last); + if ((*equal)(ref, mid->content)) + return (mid); + left = st_lstbsearch_rec(lst, mid, equal, ref); + if (left != NULL) + return (left); + return (st_lstbsearch_rec(mid, NULL, equal, ref)); +} + +t_ftlst *ft_lstbsearch(t_ftlst *lst, + t_ftbool (*equal)(void *ref, void *content), void *ref) +{ + return (st_lstbsearch_rec(lst, NULL, equal, ref)); +} diff --git a/src/lst/ft_lstclear.c b/src/lst/ft_lstclear.c index ee1d9e5..0bacb4f 100644 --- a/src/lst/ft_lstclear.c +++ b/src/lst/ft_lstclear.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -void ft_lstclear(t_list **lst, void (*del)(void *)) +void ft_lstclear(t_ftlst **lst, void (*del)(void *)) { if (lst == NULL) return ; diff --git a/src/lst/ft_lstdelone.c b/src/lst/ft_lstdelone.c index 30cec69..63dcc35 100644 --- a/src/lst/ft_lstdelone.c +++ b/src/lst/ft_lstdelone.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -void ft_lstdelone(t_list *lst, void (*del)(void *)) +void ft_lstdelone(t_ftlst *lst, void (*del)(void *)) { if (lst == NULL) return ; diff --git a/src/lst/ft_lstiter.c b/src/lst/ft_lstiter.c index 282e0fa..9b2895b 100644 --- a/src/lst/ft_lstiter.c +++ b/src/lst/ft_lstiter.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -void ft_lstiter(t_list *lst, void (*f)(void *)) +void ft_lstiter(t_ftlst *lst, void (*f)(void *)) { if (f == NULL) return ; diff --git a/src/lst/ft_lstlast.c b/src/lst/ft_lstlast.c index 247f4da..728cbf2 100644 --- a/src/lst/ft_lstlast.c +++ b/src/lst/ft_lstlast.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -t_list *ft_lstlast(t_list *lst) +t_ftlst *ft_lstlast(t_ftlst *lst) { if (lst == NULL) return (NULL); diff --git a/src/lst/ft_lstmap.c b/src/lst/ft_lstmap.c index c623d6f..9039d71 100644 --- a/src/lst/ft_lstmap.c +++ b/src/lst/ft_lstmap.c @@ -11,11 +11,12 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) { - t_list *mapped; - t_list *tmp; + t_ftlst *mapped; + t_ftlst *tmp; if (lst == NULL || f == NULL) return (NULL); @@ -36,7 +37,7 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) /* ** Rest in peace, my beautiful recursion. ** -** t_list *tmp; +** t_ftlst *tmp; ** ** if (lst == NULL) ** return (NULL); diff --git a/src/lst/ft_lstnew.c b/src/lst/ft_lstnew.c index ea10e4d..11cf223 100644 --- a/src/lst/ft_lstnew.c +++ b/src/lst/ft_lstnew.c @@ -11,12 +11,13 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -t_list *ft_lstnew(void const *content) +t_ftlst *ft_lstnew(void const *content) { - t_list *elem; + t_ftlst *elem; - if ((elem = (t_list*)malloc(sizeof(t_list))) == NULL) + if ((elem = (t_ftlst*)malloc(sizeof(t_ftlst))) == NULL) return (NULL); elem->content = (void*)content; elem->next = NULL; diff --git a/src/lst/ft_lstpop_front.c b/src/lst/ft_lstpop_front.c index f59c9b8..ff386f9 100644 --- a/src/lst/ft_lstpop_front.c +++ b/src/lst/ft_lstpop_front.c @@ -10,12 +10,12 @@ /* */ /* ************************************************************************** */ -#include <stdlib.h> #include "libft.h" +#include "libft_lst.h" -void ft_lstpop_front(t_list **lst, void (*del)(void *)) +void ft_lstpop_front(t_ftlst **lst, void (*del)(void *)) { - t_list *tmp; + t_ftlst *tmp; if (lst == NULL || *lst == NULL) return ; diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c new file mode 100644 index 0000000..a597c2e --- /dev/null +++ b/src/lst/ft_lstremove_if.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstremove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:55:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_lst.h" + +void ft_lstremove_if(t_ftlst **lst, + t_ftbool (*equal)(void *ref, void *content), void *ref, + void (*del)(void *content)) +{ + t_ftlst *saved_next; + + if (lst == NULL || *lst == NULL) + return ; + if (!equal(ref, &(*lst)->content)) + { + ft_lstremove_if(&(*lst)->next, equal, ref, del); + return ; + } + saved_next = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = saved_next; + ft_lstremove_if(lst, equal, ref, del); +} diff --git a/src/lst/ft_lstreverse.c b/src/lst/ft_lstreverse.c index c45a912..3d3fc7b 100644 --- a/src/lst/ft_lstreverse.c +++ b/src/lst/ft_lstreverse.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -void ft_lstreverse(t_list **lst) +void ft_lstreverse(t_ftlst **lst) { *lst = ft_lstreverse_ret(*lst); } diff --git a/src/lst/ft_lstreverse_ret.c b/src/lst/ft_lstreverse_ret.c index caf6ec1..c115ac5 100644 --- a/src/lst/ft_lstreverse_ret.c +++ b/src/lst/ft_lstreverse_ret.c @@ -6,15 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 12:51:15 by cacharle #+# #+# */ -/* Updated: 2020/01/18 11:52:50 by cacharle ### ########.fr */ +/* Updated: 2020/02/10 02:20:21 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -t_list *ft_lstreverse_ret(t_list *lst) +t_ftlst *ft_lstreverse_ret(t_ftlst *lst) { - t_list *tmp; + t_ftlst *tmp; if (lst == NULL) return (NULL); diff --git a/src/lst/ft_lstsize.c b/src/lst/ft_lstsize.c index b9d65d2..922b581 100644 --- a/src/lst/ft_lstsize.c +++ b/src/lst/ft_lstsize.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_lst.h" -int ft_lstsize(t_list *lst) +int ft_lstsize(t_ftlst *lst) { int counter; diff --git a/src/lst/ft_lstsort.c b/src/lst/ft_lstsort.c new file mode 100644 index 0000000..883604f --- /dev/null +++ b/src/lst/ft_lstsort.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 01:53:55 by cacharle #+# #+# */ +/* Updated: 2020/02/10 02:09:28 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +void ft_lstsort(t_ftlst **begin_list, int (*cmp)(void *, void*)) +{ + t_ftlst *fast; + t_ftlst *slow; + t_ftlst *middle; + + if (begin_list == NULL || *begin_list == NULL + || (*begin_list)->next == NULL) + return ; + fast = (*begin_list)->next; + slow = *begin_list; + while (fast != NULL) + { + fast = fast->next; + if (fast != NULL) + { + fast = fast->next; + slow = slow->next; + } + } + middle = slow->next; + slow->next = NULL; + ft_lstsort(begin_list, cmp); + ft_lstsort(&middle, cmp); + *begin_list = ft_lstsorted_merge(*begin_list, middle, cmp); +} diff --git a/src/lst/ft_lstsorted_merge.c b/src/lst/ft_lstsorted_merge.c new file mode 100644 index 0000000..c3d0391 --- /dev/null +++ b/src/lst/ft_lstsorted_merge.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsorted_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 01:58:52 by cacharle #+# #+# */ +/* Updated: 2020/02/10 02:13:37 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, + int (*cmp)(void *, void *)) +{ + t_ftlst *merged; + + merged = NULL; + if (l1 == NULL) + return (l2); + if (l2 == NULL) + return (l1); + if (cmp(l1->content, l2->content) < 0) + { + merged = l1; + merged->next = ft_lstsorted_merge(l1->next, l2, cmp); + } + else + { + merged = l2; + merged->next = ft_lstsorted_merge(l1, l2->next, cmp); + } + return (merged); +} |
