aboutsummaryrefslogtreecommitdiff
path: root/src/lst
diff options
context:
space:
mode:
Diffstat (limited to 'src/lst')
-rw-r--r--src/lst/ft_lstbsearch.c4
-rw-r--r--src/lst/ft_lstdelone.c10
-rw-r--r--src/lst/ft_lstdestroy.c (renamed from src/lst/ft_lstclear.c)12
-rw-r--r--src/lst/ft_lstiter.c10
-rw-r--r--src/lst/ft_lstlast.c8
-rw-r--r--src/lst/ft_lstlfind.c2
-rw-r--r--src/lst/ft_lstlsearch.c2
-rw-r--r--src/lst/ft_lstmap.c21
-rw-r--r--src/lst/ft_lstnew.c12
-rw-r--r--src/lst/ft_lstpop_front.c6
-rw-r--r--src/lst/ft_lstpush_back.c (renamed from src/lst/ft_lstadd_back.c)10
-rw-r--r--src/lst/ft_lstpush_front.c (renamed from src/lst/ft_lstadd_front.c)10
-rw-r--r--src/lst/ft_lstremove_if.c10
-rw-r--r--src/lst/ft_lstreverse.c5
-rw-r--r--src/lst/ft_lstreverse_ret.c6
-rw-r--r--src/lst/ft_lstsize.c8
-rw-r--r--src/lst/ft_lstsort.c6
-rw-r--r--src/lst/ft_lstsorted_merge.c10
18 files changed, 111 insertions, 41 deletions
diff --git a/src/lst/ft_lstbsearch.c b/src/lst/ft_lstbsearch.c
index d3dc7e4..0c48eb0 100644
--- a/src/lst/ft_lstbsearch.c
+++ b/src/lst/ft_lstbsearch.c
@@ -46,11 +46,11 @@ static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last,
return (NULL);
if (mid->next == NULL)
{
- if (cmp(ref, mid->content) == 0)
+ if (cmp(ref, mid->data) == 0)
return (mid);
return (NULL);
}
- res = cmp(ref, mid->next->content);
+ res = cmp(ref, mid->next->data);
if (res < 0)
return (st_lstbsearch_rec(lst, mid, cmp, ref));
else if (res > 0)
diff --git a/src/lst/ft_lstdelone.c b/src/lst/ft_lstdelone.c
index 63dcc35..40e25cd 100644
--- a/src/lst/ft_lstdelone.c
+++ b/src/lst/ft_lstdelone.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstdelone_bonus.c :+: :+: :+: */
+/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,14 +10,18 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Delete list node
+** \param del Delete function for node's data
+*/
+
void ft_lstdelone(t_ftlst *lst, void (*del)(void *))
{
if (lst == NULL)
return ;
if (del != NULL)
- (*del)(lst->content);
+ (*del)(lst->data);
free(lst);
}
diff --git a/src/lst/ft_lstclear.c b/src/lst/ft_lstdestroy.c
index 0bacb4f..5e55f04 100644
--- a/src/lst/ft_lstclear.c
+++ b/src/lst/ft_lstdestroy.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstclear_bonus.c :+: :+: :+: */
+/* ft_lstdestroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,16 +10,20 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
-void ft_lstclear(t_ftlst **lst, void (*del)(void *))
+/**
+** \brief Destroy a list and set his pointer to NULL
+** \param del Delete Function for data of each node
+*/
+
+void ft_lstdestroy(t_ftlst **lst, void (*del)(void *))
{
if (lst == NULL)
return ;
if (*lst == NULL)
return ;
- ft_lstclear(&((*lst)->next), del);
+ ft_lstdestroy(&((*lst)->next), del);
ft_lstdelone(*lst, del);
*lst = NULL;
}
diff --git a/src/lst/ft_lstiter.c b/src/lst/ft_lstiter.c
index 9b2895b..39d870b 100644
--- a/src/lst/ft_lstiter.c
+++ b/src/lst/ft_lstiter.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstiter_bonus.c :+: :+: :+: */
+/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,16 +10,20 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Iterate of list
+** \param f Funtion applied to data of each node
+*/
+
void ft_lstiter(t_ftlst *lst, void (*f)(void *))
{
if (f == NULL)
return ;
while (lst != NULL)
{
- (*f)(lst->content);
+ (*f)(lst->data);
lst = lst->next;
}
}
diff --git a/src/lst/ft_lstlast.c b/src/lst/ft_lstlast.c
index 728cbf2..12fdeee 100644
--- a/src/lst/ft_lstlast.c
+++ b/src/lst/ft_lstlast.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstlast_bonus.c :+: :+: :+: */
+/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,9 +10,13 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Last node
+** \return List's last node
+*/
+
t_ftlst *ft_lstlast(t_ftlst *lst)
{
if (lst == NULL)
diff --git a/src/lst/ft_lstlfind.c b/src/lst/ft_lstlfind.c
index c399b6e..fd7e688 100644
--- a/src/lst/ft_lstlfind.c
+++ b/src/lst/ft_lstlfind.c
@@ -16,7 +16,7 @@ t_ftlst *ft_lstlfind(t_ftlst *lst, t_ftcompar_func cmp, const void *ref)
{
if (lst == NULL)
return (NULL);
- if (cmp(ref, lst->content) == 0)
+ if (cmp(ref, lst->data) == 0)
return (lst);
return (ft_lstlfind(lst->next, cmp, ref));
}
diff --git a/src/lst/ft_lstlsearch.c b/src/lst/ft_lstlsearch.c
index f59b5de..11c528c 100644
--- a/src/lst/ft_lstlsearch.c
+++ b/src/lst/ft_lstlsearch.c
@@ -16,7 +16,7 @@ t_ftlst *ft_lstlsearch(t_ftlst *lst, t_ftcompar_func cmp, const void *ref)
{
if (lst == NULL)
return (ft_lstnew(ref));
- if (cmp(ref, lst->content) == 0)
+ if (cmp(ref, lst->data) == 0)
return (lst);
if (lst->next == NULL)
{
diff --git a/src/lst/ft_lstmap.c b/src/lst/ft_lstmap.c
index dda15de..bf96892 100644
--- a/src/lst/ft_lstmap.c
+++ b/src/lst/ft_lstmap.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstmap_bonus.c :+: :+: :+: */
+/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,9 +10,16 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Clone a list and map a function to each node data
+** \param lst Origin list
+** \param f Function applied to each node's data
+** \param del Delete function for cleanning up in case of failed allocation
+** \return Mapped clone list
+*/
+
t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *))
{
t_ftlst *mapped;
@@ -23,12 +30,12 @@ t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *))
mapped = NULL;
while (lst != NULL)
{
- if ((tmp = ft_lstnew((*f)(lst->content))) == NULL)
+ if ((tmp = ft_lstnew((*f)(lst->data))) == NULL)
{
- ft_lstclear(&mapped, del);
+ ft_lstdestroy(&mapped, del);
return (NULL);
}
- ft_lstadd_back(&mapped, tmp);
+ ft_lstpush_back(&mapped, tmp);
lst = lst->next;
}
return (mapped);
@@ -41,9 +48,9 @@ t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *))
**
** if (lst == NULL)
** return (NULL);
-** if ((tmp = ft_lstnew(lst->content)) == NULL)
+** if ((tmp = ft_lstnew(lst->data)) == NULL)
** return (NULL);
-** tmp->content = (*f)(tmp->content);
+** tmp->data = (*f)(tmp->data);
** tmp->next = ft_lstmap(lst->next, f);
** return (tmp);
*/
diff --git a/src/lst/ft_lstnew.c b/src/lst/ft_lstnew.c
index 11cf223..41c8153 100644
--- a/src/lst/ft_lstnew.c
+++ b/src/lst/ft_lstnew.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstnew_bonus.c :+: :+: :+: */
+/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,16 +10,20 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
-t_ftlst *ft_lstnew(void const *content)
+/**
+** \brief Create a list node
+** \param data Pointer to data of node
+*/
+
+t_ftlst *ft_lstnew(void const *data)
{
t_ftlst *elem;
if ((elem = (t_ftlst*)malloc(sizeof(t_ftlst))) == NULL)
return (NULL);
- elem->content = (void*)content;
+ elem->data = (void*)data;
elem->next = NULL;
return (elem);
}
diff --git a/src/lst/ft_lstpop_front.c b/src/lst/ft_lstpop_front.c
index 5bf2c60..798bf83 100644
--- a/src/lst/ft_lstpop_front.c
+++ b/src/lst/ft_lstpop_front.c
@@ -10,9 +10,13 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Delete head node and replace it with next node
+** \param del Delete function for node data
+*/
+
void ft_lstpop_front(t_ftlst **lst, void (*del)(void *))
{
t_ftlst *tmp;
diff --git a/src/lst/ft_lstadd_back.c b/src/lst/ft_lstpush_back.c
index 8f39a75..372c18c 100644
--- a/src/lst/ft_lstadd_back.c
+++ b/src/lst/ft_lstpush_back.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstadd_back_bonus.c :+: :+: :+: */
+/* ft_lstpush_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,10 +10,14 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
-void ft_lstadd_back(t_ftlst **alst, t_ftlst *new)
+/**
+** \brief Push new node to the list end
+** \param new Pushed node
+*/
+
+void ft_lstpush_back(t_ftlst **alst, t_ftlst *new)
{
if (alst == NULL)
return ;
diff --git a/src/lst/ft_lstadd_front.c b/src/lst/ft_lstpush_front.c
index bcd5ad9..c17a586 100644
--- a/src/lst/ft_lstadd_front.c
+++ b/src/lst/ft_lstpush_front.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstadd_front_bonus.c :+: :+: :+: */
+/* ft_lstpush_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,10 +10,14 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
-void ft_lstadd_front(t_ftlst **alst, t_ftlst *new)
+/**
+** \brief Push node to list front
+** \param new Pushed node
+*/
+
+void ft_lstpush_front(t_ftlst **alst, t_ftlst *new)
{
if (alst == NULL || new == NULL)
return ;
diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c
index 03643fa..fdac710 100644
--- a/src/lst/ft_lstremove_if.c
+++ b/src/lst/ft_lstremove_if.c
@@ -10,9 +10,15 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Remove node on some condition
+** \param cmp Comparison function, return 0 if equal
+** \param ref Reference data passed has the first arg of `cmp`
+** \param del Delete function to free removed node data
+*/
+
void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp,
const void *ref, t_ftdel_func del)
{
@@ -20,7 +26,7 @@ void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp,
if (lst == NULL || *lst == NULL)
return ;
- if (cmp(ref, (*lst)->content) == 0)
+ if (cmp(ref, (*lst)->data) == 0)
{
saved_next = (*lst)->next;
ft_lstdelone(*lst, del);
diff --git a/src/lst/ft_lstreverse.c b/src/lst/ft_lstreverse.c
index 61c9daf..fd5259b 100644
--- a/src/lst/ft_lstreverse.c
+++ b/src/lst/ft_lstreverse.c
@@ -10,9 +10,12 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Reverse a list
+*/
+
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 c115ac5..259af9e 100644
--- a/src/lst/ft_lstreverse_ret.c
+++ b/src/lst/ft_lstreverse_ret.c
@@ -10,9 +10,13 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief Reverse list
+** \return Pointer to reversed list
+*/
+
t_ftlst *ft_lstreverse_ret(t_ftlst *lst)
{
t_ftlst *tmp;
diff --git a/src/lst/ft_lstsize.c b/src/lst/ft_lstsize.c
index 922b581..6a92b99 100644
--- a/src/lst/ft_lstsize.c
+++ b/src/lst/ft_lstsize.c
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_lstsize_bonus.c :+: :+: :+: */
+/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -10,9 +10,13 @@
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_lst.h"
+/**
+** \brief List size
+** \return Number of node in list
+*/
+
int ft_lstsize(t_ftlst *lst)
{
int counter;
diff --git a/src/lst/ft_lstsort.c b/src/lst/ft_lstsort.c
index e1c9913..fbd046a 100644
--- a/src/lst/ft_lstsort.c
+++ b/src/lst/ft_lstsort.c
@@ -12,6 +12,12 @@
#include "libft_lst.h"
+/**
+** \brief Sort list
+** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater
+** \note Use merge sort algorithm
+*/
+
void ft_lstsort(t_ftlst **begin_list, t_ftcompar_func cmp)
{
t_ftlst *fast;
diff --git a/src/lst/ft_lstsorted_merge.c b/src/lst/ft_lstsorted_merge.c
index 4f5332c..0cd5721 100644
--- a/src/lst/ft_lstsorted_merge.c
+++ b/src/lst/ft_lstsorted_merge.c
@@ -12,6 +12,14 @@
#include "libft_lst.h"
+/**
+** \brief Merge sorted lists, the new list is also sorted
+** \param l1 First list
+** \param l2 Second list
+** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater
+** \return Pointer to first node of merged list
+*/
+
t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, t_ftcompar_func cmp)
{
t_ftlst *merged;
@@ -21,7 +29,7 @@ t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, t_ftcompar_func cmp)
return (l2);
if (l2 == NULL)
return (l1);
- if (cmp(l1->content, l2->content) < 0)
+ if (cmp(l1->data, l2->data) < 0)
{
merged = l1;
merged->next = ft_lstsorted_merge(l1->next, l2, cmp);