-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_lstmap.c
More file actions
91 lines (77 loc) · 2.83 KB
/
ft_lstmap.c
File metadata and controls
91 lines (77 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgasc <lgasc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:59:30 by lgasc #+# #+# */
/* Updated: 2024/02/28 16:47:12 by lgasc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_list map_skeleton(const size_t size);
static void hydrate_map(const t_list map, const t_list source,
void *(*const mapper)(const void *const datum));
///Iterate the `list` and call the `mapper` on the `datum` of each node,
/// and create a new list from the results of the calls to the `mapper`.
///The `deleter` function is used to delete the datum of a node if needed.
///@param list The head pointer of the original list
///@param mapper The address of a function used to map
///@param deleter The address of a function which can free a `node.datum`
///@return A new list, or `NULL` if the allocation fails.
///@remark External functions: `malloc`, `free`.
///@remark This function assumes that `NULL` being returned by the `mapper`
/// function is just a normal (valid) value, and not some kind of error code.
///`deleter`?
///I barely know her!
///DEL-EAT DEEZ
t_list ft_lstmap(const t_list list, void *(*mapper)(const void *const datum),
void (*const deleter)(const void *const datum))
{
const t_list map = map_skeleton(ft_lstsize(list));
(void) deleter;
if (map == NULL)
return (NULL);
hydrate_map(map, list, mapper);
return (map);
}
static void noop(const void *const x);
static t_list map_skeleton(const size_t size)
{
size_t i;
t_list skeleton;
t_node *node;
i = 0;
skeleton = NULL;
while (i++ < size)
{
node = ft_lstnew(NULL);
if (node == NULL)
return (ft_lstclear(&skeleton, noop), NULL);
ft_lstadd_back(&skeleton, node);
}
return (skeleton);
}
static void noop(const void *const x)
{
if (x)
return ;
return ;
}
static void hydrate_map(const t_list map, const t_list source,
void *(*const mapper)(const void *const datum))
{
t_node *map_node;
const t_node *source_node;
if ((! source) || (! mapper) || (! map))
return ;
map_node = map;
source_node = source;
while (map_node != NULL)
{
map_node->datum = mapper(source_node->datum);
map_node = map_node->next;
source_node = source_node->next;
}
}