-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea41c26
commit b4d8ad3
Showing
5 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_lstmap.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/03/20 19:10:32 by ivanderw #+# #+# */ | ||
/* Updated: 2023/03/20 20:49:38 by ivanderw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
/* | ||
PROTOTYPE | ||
t_list *ft_lstmap(t_list *lst, void * (*f)(void *), void (*del)(void *)); | ||
PARAMETERS | ||
lst: The address of a pointer to a node. | ||
f: The address of the function used to iterate on | ||
the list. | ||
del: The address of the function used to delete | ||
the content of a node if needed. | ||
RETURN | ||
The new list. | ||
NULL if the allocation fails. | ||
EXTERNAL FUNCTIONS | ||
malloc, free | ||
DESCRIPTION | ||
Iterates the list ’lst’ and applies the function | ||
’f’ on the content of each node. Creates a new | ||
list resulting of the successive applications of | ||
the function ’f’. The ’del’ function is used to | ||
delete the content of a node if needed. | ||
*/ | ||
#include "libft.h" | ||
|
||
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) | ||
{ | ||
t_list *map_list; | ||
|
||
if (!lst || !f || !del) | ||
return (NULL); | ||
map_list = ft_lstnew(f(lst->content)); | ||
lst = lst->next; | ||
while (lst) | ||
{ | ||
ft_lstadd_back(&map_list, ft_lstnew(f(lst->content))); | ||
lst = lst->next; | ||
} | ||
return (&map_list[0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_lstnew.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/03/20 16:24:52 by ivanderw #+# #+# */ | ||
/* Updated: 2023/03/20 16:48:20 by ivanderw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
/* | ||
PROTOTYPE | ||
t_list *ft_lstnew(void *content); | ||
PARAMETERS | ||
content: The content to create the node with. | ||
RETURN | ||
The new node. | ||
EXTERNAL FUNCTIONS | ||
malloc | ||
DESCRIPTION | ||
Allocates (with (malloc(3)) and returns a new node. | ||
The member variable 'content' is initialized with the value | ||
of the parameter 'content'. The variable 'next' is initialized to | ||
NULL. | ||
*/ | ||
#include "libft.h" | ||
|
||
t_list *ft_lstnew(void *content) | ||
{ | ||
t_list *my_list; | ||
|
||
my_list = (t_list *)malloc(sizeof(t_list)); | ||
if (!my_list) | ||
return (NULL); | ||
my_list->content = content; | ||
my_list->next = NULL; | ||
return (my_list); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_lstsize.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/03/20 17:07:04 by ivanderw #+# #+# */ | ||
/* Updated: 2023/03/20 17:30:06 by ivanderw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
/* | ||
PROTOTYPE | ||
int ft_lstsize(t_list *lst); | ||
PARAMETERS | ||
lst: The beginning of the list. | ||
RETURN VALUE | ||
The length of the list. | ||
DESCRIPTION | ||
Counts the number of nodes in a list. | ||
*/ | ||
#include "libft.h" | ||
|
||
int ft_lstsize(t_list *lst) | ||
{ | ||
int length; | ||
|
||
length = 0; | ||
while (lst) | ||
{ | ||
lst = lst->next; | ||
length++; | ||
} | ||
return (length); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_memchr.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/03/10 13:59:45 by ivanderw #+# #+# */ | ||
/* Updated: 2023/03/21 18:56:32 by ivanderw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
#include "libft.h" | ||
/* | ||
NAME | ||
memchr -- locate byte in byte string | ||
SYNOPSIS | ||
#include <string.h> | ||
void * | ||
memchr(const void *s, int c, size_t n); | ||
DESCRIPTION | ||
The memchr() function locates the first occurrence of c | ||
(converted to an: unsigned char) in string s. | ||
RETURN VALUES | ||
The memchr() function returns a pointer to the byte located, or NULL | ||
if no such byte exists within n bytes. | ||
*/ | ||
|
||
void *ft_memchr(const void *s, int c, size_t n) | ||
{ | ||
size_t i; | ||
unsigned char *ptr; | ||
|
||
ptr = (unsigned char *)s; | ||
i = 0; | ||
if (n == 0) | ||
return (NULL); | ||
while (i < n) | ||
{ | ||
if (ptr[i] == (unsigned char)c) | ||
return (ptr + i); | ||
i++; | ||
} | ||
return (NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_memcmp.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/03/14 18:02:12 by ivanderw #+# #+# */ | ||
/* Updated: 2023/03/14 18:14:23 by ivanderw ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
/* | ||
* NAME | ||
memcmp -- compare byte string | ||
LIBRARY | ||
Standard C Library (libc, -lc) | ||
SYNOPSIS | ||
#include <string.h> | ||
int | ||
memcmp(const void *s1, const void *s2, size_t n); | ||
DESCRIPTION | ||
The memcmp() function compares byte string s1 against byte string s2. | ||
Both strings are assumed to be n bytes long. | ||
RETURN VALUES | ||
The memcmp() function returns zero if the two strings are identical, | ||
otherwise returns the difference between the first two differing bytes | ||
(treated as unsigned char values, so that `\200' is greater than | ||
`\0', for example). | ||
Zero-length strings are always identical. | ||
This behavior is not required by C and portable code should only depend | ||
on the sign of the returned value. | ||
*/ | ||
#include <string.h> | ||
|
||
int ft_memcmp(const void *s1, const void *s2, size_t n) | ||
{ | ||
size_t i; | ||
unsigned char *my_s1; | ||
const unsigned char *my_s2; | ||
|
||
i = 0; | ||
my_s1 = (unsigned char *)s1; | ||
my_s2 = (const unsigned char *)s2; | ||
while (i < n) | ||
{ | ||
if (my_s1[i] != my_s2[i]) | ||
return (my_s1[i] - my_s2[i]); | ||
i++; | ||
} | ||
return (0); | ||
} |