aboutsummaryrefslogtreecommitdiff
path: root/src/lst/ft_lstmap.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
commit5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde (patch)
tree80911dc3c32e9f230750e7e1042d413dfb6efab2 /src/lst/ft_lstmap.c
parentee32953ea79616e72f5428cdf40c834714a891c9 (diff)
parentb96b82194ccad2cddbb46b77aa1962a57c47ff44 (diff)
downloadlibft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.gz
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.bz2
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.zip
Merge branch 'master' into ft_ssl
Diffstat (limited to 'src/lst/ft_lstmap.c')
-rw-r--r--src/lst/ft_lstmap.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/lst/ft_lstmap.c b/src/lst/ft_lstmap.c
index dda15de..3182bb0 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);
*/