From 8131a5d26441c5152ab151b4bb49b561e5ca6e81 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 31 Jan 2020 10:44:30 +0100 Subject: hash table unit testing, norming --- src/ht/ft_htdelone.c | 2 +- src/ht/ft_htdestroy.c | 4 ++-- src/ht/ft_htget.c | 2 +- src/ht/ft_htnew.c | 2 +- src/ht/ft_htset.c | 2 +- src/ht/ft_inter_htkey_cmp.c | 2 +- src/io/ft_next_line.c | 2 +- src/lst/ft_lstbsearch.c | 2 +- src/lst/ft_lstpop_front.c | 4 ++-- src/lst/ft_lstremove_if.c | 2 +- 10 files changed, 12 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 2c54721..77eae58 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:35:06 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:10:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index e0442c6..900f76e 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/30 08:19:06 by cacharle #+# #+# */ -/* Updated: 2020/01/30 08:33:09 by cacharle ### ########.fr */ +/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:10:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index 76e4fb2..1562eb0 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/02/19 01:44:41 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:21:09 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index d98a724..f77398f 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:16:20 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:10:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index c7068d5..81aba97 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:44:10 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:11:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/ht/ft_inter_htkey_cmp.c b/src/ht/ft_inter_htkey_cmp.c index 6f04ecc..be2e52f 100644 --- a/src/ht/ft_inter_htkey_cmp.c +++ b/src/ht/ft_inter_htkey_cmp.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:03:14 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:20:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/io/ft_next_line.c b/src/io/ft_next_line.c index 0f4cc2c..74afa71 100644 --- a/src/io/ft_next_line.c +++ b/src/io/ft_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:38:01 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:11:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lst/ft_lstbsearch.c b/src/lst/ft_lstbsearch.c index d694209..d3dc7e4 100644 --- a/src/lst/ft_lstbsearch.c +++ b/src/lst/ft_lstbsearch.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */ -/* Updated: 2020/02/17 03:03:21 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:12:12 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lst/ft_lstpop_front.c b/src/lst/ft_lstpop_front.c index ff386f9..5bf2c60 100644 --- a/src/lst/ft_lstpop_front.c +++ b/src/lst/ft_lstpop_front.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/25 03:32:51 by cacharle #+# #+# */ -/* Updated: 2020/01/15 12:46:28 by cacharle ### ########.fr */ +/* Created: 2020/01/30 08:29:58 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:12:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c index 5221ae4..03643fa 100644 --- a/src/lst/ft_lstremove_if.c +++ b/src/lst/ft_lstremove_if.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:06:22 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:20:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit From 915f1b888cf9c05e4b61321f84ac045eacd8ddd1 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 27 Feb 2020 18:07:57 +0100 Subject: Added ft_lstlfind,ft_lstlsearch and ft_split_destroy --- src/ht/ft_htnew.c | 2 +- src/lst/ft_lstlfind.c | 4 ++-- src/lst/ft_lstlsearch.c | 4 ++-- src/util/ft_split_destroy.c | 24 ++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/util/ft_split_destroy.c (limited to 'src') diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index f77398f..6827c36 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:10:51 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:23:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lst/ft_lstlfind.c b/src/lst/ft_lstlfind.c index 92d37d8..c399b6e 100644 --- a/src/lst/ft_lstlfind.c +++ b/src/lst/ft_lstlfind.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/17 03:04:52 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:03:11 by cacharle ### ########.fr */ +/* Created: 2020/02/27 18:00:37 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lst/ft_lstlsearch.c b/src/lst/ft_lstlsearch.c index 8d2acc8..f59b5de 100644 --- a/src/lst/ft_lstlsearch.c +++ b/src/lst/ft_lstlsearch.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/17 02:57:12 by cacharle #+# #+# */ -/* Updated: 2020/02/17 03:35:45 by cacharle ### ########.fr */ +/* Created: 2020/02/27 16:18:33 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/util/ft_split_destroy.c b/src/util/ft_split_destroy.c new file mode 100644 index 0000000..bc3f4e6 --- /dev/null +++ b/src/util/ft_split_destroy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 16:30:55 by cacharle #+# #+# */ +/* Updated: 2020/02/27 17:52:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_util.h" + +void *ft_split_destroy(char **strs) +{ + int i; + + i = -1; + while (strs[++i] != NULL) + free(strs[i]); + free(strs); + return (NULL); +} -- cgit From 2a9133a87109f9430e4827a858ff86596c5f98d5 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 4 Mar 2020 12:49:31 +0100 Subject: Added hash table documentation --- src/ht/ft_htcontent_new.c | 6 ++++++ src/ht/ft_htdelone.c | 4 ++++ src/ht/ft_htdestroy.c | 5 +++++ src/ht/ft_htget.c | 7 ++++++- src/ht/ft_hthash.c | 5 +++++ src/ht/ft_htnew.c | 6 ++++++ src/ht/ft_htset.c | 12 +++++++++--- src/ht/ft_inter_htkey_cmp.c | 4 ++++ 8 files changed, 45 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ht/ft_htcontent_new.c b/src/ht/ft_htcontent_new.c index 214e125..7dd4f20 100644 --- a/src/ht/ft_htcontent_new.c +++ b/src/ht/ft_htcontent_new.c @@ -13,6 +13,12 @@ #include "libft.h" #include "libft_ht.h" +/* +** Create a new hash table key/value pair. +** `key` is always duplicated. +** Return a pointer to the created content or NULL is an allocation failed. +*/ + t_ftht_content *ft_htcontent_new(char *key, void *value) { t_ftht_content *content; diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 77eae58..e3bb355 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -13,6 +13,10 @@ #include "libft.h" #include "libft_ht.h" +/* +** Delete one hash table entry at `key`. +*/ + void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)) { ft_lstremove_if(ht->entries + ft_hthash(ht, key), diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index 900f76e..a788a78 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -13,6 +13,11 @@ #include "libft.h" #include "libft_ht.h" +/* +** Destroy an hash table. +** The `del` function is used to destroy each key/value pair +*/ + void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)) { if (ht == NULL) diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index 1562eb0..94a4164 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -13,9 +13,14 @@ #include "libft.h" #include "libft_ht.h" +/* +** Retrieve a value with associated key. +** Returns NULL if there is no value at `key`. +*/ + void *ft_htget(t_ftht *ht, char *key) { - + t_ftht_digest digest; t_ftlst *found; diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index e7e696c..ba922e7 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -12,6 +12,11 @@ #include "libft_ht.h" +/* +** Hash a string according to the size of the hash table. +*/ + +// maybe use a less efficient but understandable function t_ftht_digest ft_hthash(t_ftht *ht, char *key) { t_ftht_digest digest; diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index 6827c36..465eabf 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -13,6 +13,12 @@ #include "libft.h" #include "libft_ht.h" +/* +** Create a new hash table. +** `size` is the size of the underlying array of linked list (buckets) +** Return NULL is an allocation failed. +*/ + t_ftht *ft_htnew(t_ftsize size) { t_ftht *ht; diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 81aba97..0755889 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -13,6 +13,13 @@ #include "libft.h" #include "libft_ht.h" +/* +** Create/Update a entry in an hash table. +** If `key` already exist in `ht`, updates only the list element content. +** else create a new list node in addition the list content. +** Return a pointer to the created entry, NULL if an allocation failed. +*/ + t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, void (*del)(t_ftht_content*)) { @@ -30,14 +37,13 @@ t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, if (tmp != NULL) { if (del != NULL) - (*del)(tmp->content); + del(tmp->content); tmp->content = content; return ((t_ftht_content*)tmp->content); } - if ((entry = ft_lstnew(content)) == NULL) { - free(content); + del(content); return (NULL); } ft_lstadd_front(ht->entries + digest, entry); diff --git a/src/ht/ft_inter_htkey_cmp.c b/src/ht/ft_inter_htkey_cmp.c index be2e52f..397177e 100644 --- a/src/ht/ft_inter_htkey_cmp.c +++ b/src/ht/ft_inter_htkey_cmp.c @@ -13,6 +13,10 @@ #include "libft.h" #include "libft_ht.h" +/* +** Hash table internal function to compare key string in linked list. +*/ + int ft_inter_htkey_cmp(const void *ref_key, const void *content) { if (ref_key == NULL || content == NULL) -- cgit From e792d0a3ff1c1da456c241530571263df0b887b5 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 4 Mar 2020 16:19:05 +0100 Subject: Added ft_htiter.c --- src/ht/ft_htiter.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/ht/ft_htiter.c (limited to 'src') diff --git a/src/ht/ft_htiter.c b/src/ht/ft_htiter.c new file mode 100644 index 0000000..5473412 --- /dev/null +++ b/src/ht/ft_htiter.c @@ -0,0 +1,17 @@ +#include "libft_ht.h" + +/* +** Iterate function `f` over every pair in `ht`. +*/ + +void ft_htiter(t_ftht *ht, void (*f)(t_ftht_content*)) +{ + size_t i; + + i = 0; + while (i < ht->size) + { + ft_lstiter(ht->entries[i], (void (*)(void*))f); + i++; + } +} -- cgit From c128213daa677d548bfc2905496257fe4a4faf79 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 11 Mar 2020 21:07:32 +0100 Subject: ft_mem* and ft_strlen optimization --- src/mem/ft_memccpy.c | 56 +++++++++++++++++++++++++++------ src/mem/ft_memchr.c | 44 +++++++++++++++++++++----- src/mem/ft_memcmp.c | 46 ++++++++++++++++++++------- src/mem/ft_memcpy.c | 7 +++-- src/mem/ft_memmove.c | 13 ++++---- src/mem/ft_memset.c | 32 ++++++++++++------- src/str/ft_strcat.c | 1 + src/str/ft_strcmp.c | 7 +---- src/str/ft_strdup.c | 7 +++-- src/str/ft_strlen.c | 88 ++++++++++++++++++++++++++++++++++++++++------------ src/str/ft_strncat.c | 18 +++++------ src/str/ft_strncmp.c | 12 +++---- src/str/ft_strstr.c | 9 ++---- src/str/ft_substr.c | 1 + 14 files changed, 238 insertions(+), 103 deletions(-) (limited to 'src') diff --git a/src/mem/ft_memccpy.c b/src/mem/ft_memccpy.c index 8ce656a..0f52242 100644 --- a/src/mem/ft_memccpy.c +++ b/src/mem/ft_memccpy.c @@ -12,20 +12,56 @@ #include "libft.h" +#define HIMAGIC 0x8080808080808080L +#define LOMAGIC 0x0101010101010101L + void *ft_memccpy(void *dest, const void *src, int c, size_t n) { - size_t i; - t_ftbyte *cast_dest; - t_ftbyte *cast_src; + uint64_t buf; + uint64_t lw; - cast_dest = (t_ftbyte*)dest; - cast_src = (t_ftbyte*)src; - i = -1; - while (++i < n) + if (dest == src) + return (dest); + c = (uint8_t)c; + while ((n & 0b111) != 0) + { + *(uint8_t*)dest = *(uint8_t*)src; + if (*(uint8_t*)dest == c) + return ((uint8_t*)dest + 1); + src++; + dest++; + n--; + } + buf = (uint64_t)c | (uint64_t)c << 8 | (uint64_t)c << 16 + | (uint64_t)c << 24 | (uint64_t)c << 32 | (uint64_t)c << 40 + | (uint64_t)c << 48 | (uint64_t)c << 56; + while (n > 0) { - cast_dest[i] = cast_src[i]; - if (cast_dest[i] == (unsigned char)c) - return (cast_dest + i + 1); + lw = *(uint64_t*)src ^ buf; + if ((lw - LOMAGIC) & ~lw & HIMAGIC) + { + if ( (((uint8_t*)dest)[0] = ((uint8_t*)src)[0]) == (uint8_t)c) + return ((uint8_t*)dest + 1); + if ( (((uint8_t*)dest)[1] = ((uint8_t*)src)[1]) == (uint8_t)c) + return ((uint8_t*)dest + 2); + if ( (((uint8_t*)dest)[2] = ((uint8_t*)src)[2]) == (uint8_t)c) + return ((uint8_t*)dest + 3); + if ( (((uint8_t*)dest)[3] = ((uint8_t*)src)[3]) == (uint8_t)c) + return ((uint8_t*)dest + 4); + if ( (((uint8_t*)dest)[4] = ((uint8_t*)src)[4]) == (uint8_t)c) + return ((uint8_t*)dest + 5); + if ( (((uint8_t*)dest)[5] = ((uint8_t*)src)[5]) == (uint8_t)c) + return ((uint8_t*)dest + 6); + if ( (((uint8_t*)dest)[6] = ((uint8_t*)src)[6]) == (uint8_t)c) + return ((uint8_t*)dest + 7); + if ( (((uint8_t*)dest)[7] = ((uint8_t*)src)[7]) == (uint8_t)c) + return ((uint8_t*)dest + 8); + } + else + *(uint64_t*)dest = *(uint64_t*)src; + n -= 8; + dest += 8; + src += 8; } return (NULL); } diff --git a/src/mem/ft_memchr.c b/src/mem/ft_memchr.c index 4fd8689..54780fe 100644 --- a/src/mem/ft_memchr.c +++ b/src/mem/ft_memchr.c @@ -12,15 +12,45 @@ #include "libft.h" +/* +** Determining if a long word contain byte n +** +** xor all bytes with n, then check for zero byte like in ft_strlen. +*/ + +#define HIMAGIC 0x8080808080808080L +#define LOMAGIC 0x0101010101010101L + void *ft_memchr(const void *s, int c, size_t n) { - size_t i; - t_ftbyte *cast_s; + uint64_t buf; + uint64_t lw; - cast_s = (t_ftbyte*)s; - i = -1; - while (++i < n) - if (cast_s[i] == (unsigned char)c) - return (cast_s + i); + c = (uint8_t)c; + while (((uint64_t)s & 0b111) != 0) + { + n--; + if (*(uint8_t*)s == (uint8_t)c) + return ((uint8_t*)s); + s++; + } + buf = (uint64_t)c | (uint64_t)c << 8 | (uint64_t)c << 16 + | (uint64_t)c << 24 | (uint64_t)c << 32 | (uint64_t)c << 40 + | (uint64_t)c << 48 | (uint64_t)c << 56; + while (n >= 8) + { + lw = *(uint64_t*)s ^ buf; + if ((lw - LOMAGIC) & ~lw & HIMAGIC) + break; + n -= 8; + s += 8; + } + while (n > 0) + { + if (*(uint8_t*)s == (uint8_t)c) + return ((uint8_t*)s); + n--; + s++; + } return (NULL); } diff --git a/src/mem/ft_memcmp.c b/src/mem/ft_memcmp.c index 233d796..c61ca9a 100644 --- a/src/mem/ft_memcmp.c +++ b/src/mem/ft_memcmp.c @@ -11,20 +11,42 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_mem.h" int ft_memcmp(const void *s1, const void *s2, size_t n) { - size_t i; - t_ftbyte *cast_s1; - t_ftbyte *cast_s2; - - cast_s1 = (t_ftbyte*)s1; - cast_s2 = (t_ftbyte*)s2; - if (n == 0) - return (0); - i = -1; - while (++i < n) - if (cast_s1[i] != cast_s2[i]) - return (cast_s1[i] - cast_s2[i]); + while ((n & 0b111) != 0) + { + n--; + if (*(uint8_t*)s1 != *(uint8_t*)s2) + return (*(uint8_t*)s1 - *(uint8_t*)s2); + s1++; + s2++; + } + while (n > 0) + { + if (*(uint64_t*)s1 != *(uint64_t*)s2) + { + if (((uint8_t*)s1)[0] != ((uint8_t*)s2)[0]) + return (((uint8_t*)s1)[0] - ((uint8_t*)s2)[0]); + if (((uint8_t*)s1)[1] != ((uint8_t*)s2)[1]) + return (((uint8_t*)s1)[1] - ((uint8_t*)s2)[1]); + if (((uint8_t*)s1)[2] != ((uint8_t*)s2)[2]) + return (((uint8_t*)s1)[2] - ((uint8_t*)s2)[2]); + if (((uint8_t*)s1)[3] != ((uint8_t*)s2)[3]) + return (((uint8_t*)s1)[3] - ((uint8_t*)s2)[3]); + if (((uint8_t*)s1)[4] != ((uint8_t*)s2)[4]) + return (((uint8_t*)s1)[4] - ((uint8_t*)s2)[4]); + if (((uint8_t*)s1)[5] != ((uint8_t*)s2)[5]) + return (((uint8_t*)s1)[5] - ((uint8_t*)s2)[5]); + if (((uint8_t*)s1)[6] != ((uint8_t*)s2)[6]) + return (((uint8_t*)s1)[6] - ((uint8_t*)s2)[6]); + if (((uint8_t*)s1)[7] != ((uint8_t*)s2)[7]) + return (((uint8_t*)s1)[7] - ((uint8_t*)s2)[7]); + } + n -= 8; + s1 += 8; + s2 += 8; + } return (0); } diff --git a/src/mem/ft_memcpy.c b/src/mem/ft_memcpy.c index d0ef008..1f84bfd 100644 --- a/src/mem/ft_memcpy.c +++ b/src/mem/ft_memcpy.c @@ -11,18 +11,19 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_mem.h" void *ft_memcpy(void *dest, const void *src, size_t n) { - long int *long_dest; - const long int *long_src; + uint64_t *long_dest; + const uint64_t *long_src; if (dest == src) return (dest); while (n % 8 > 0) { n--; - ((t_ftbyte*)dest)[n] = ((t_ftbyte*)src)[n]; + ((uint8_t*)dest)[n] = ((uint8_t*)src)[n]; } long_dest = dest; long_src = src; diff --git a/src/mem/ft_memmove.c b/src/mem/ft_memmove.c index 2f794fd..142b761 100644 --- a/src/mem/ft_memmove.c +++ b/src/mem/ft_memmove.c @@ -11,11 +11,12 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_mem.h" void *ft_memmove(void *dst, const void *src, size_t len) { - long int *long_dst; - const long int *long_src; + uint64_t *dst64; + const uint64_t *src64; void *dst_copy; if (dst >= src) @@ -24,12 +25,12 @@ void *ft_memmove(void *dst, const void *src, size_t len) while (len % 8 > 0) { len--; - *(t_ftbyte*)dst++ = *(t_ftbyte*)src++; + *(uint8_t*)dst++ = *(uint8_t*)src++; } - long_dst = dst; - long_src = src; + dst64 = dst; + src64 = src; len /= 8; while (len-- > 0) - *long_dst++ = *long_src++; + *dst64++ = *src64++; return (dst_copy); } diff --git a/src/mem/ft_memset.c b/src/mem/ft_memset.c index 89f53ff..ce471a5 100644 --- a/src/mem/ft_memset.c +++ b/src/mem/ft_memset.c @@ -11,21 +11,29 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_mem.h" void *ft_memset(void *s, int c, size_t n) { - long int buf; - long int *long_s; + uint64_t buf; + void *cpy; - c = (unsigned char)c; - while (n % 8 > 0) - *((t_ftbyte*)s + --n) = c; - buf = (long int)c | (long int)c << 8 | (long int)c << 16 - | (long int)c << 24 | (long int)c << 32 | (long int)c << 40 - | (long int)c << 48 | (long int)c << 56; - n /= 8; - long_s = s; + cpy = s; + c = (uint8_t)c; + buf = (uint64_t)c | (uint64_t)c << 8 | (uint64_t)c << 16 + | (uint64_t)c << 24 | (uint64_t)c << 32 | (uint64_t)c << 40 + | (uint64_t)c << 48 | (uint64_t)c << 56; + while (n > 8) + { + *(uint64_t*)s = buf; + n -= 8; + s += 8; + } while (n > 0) - long_s[--n] = buf; - return (s); + { + *(uint8_t*)s = c; + s++; + n--; + } + return (cpy); } diff --git a/src/str/ft_strcat.c b/src/str/ft_strcat.c index d5bc7e0..faed515 100644 --- a/src/str/ft_strcat.c +++ b/src/str/ft_strcat.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_mem.h" char *ft_strcat(char *dest, const char *src) { diff --git a/src/str/ft_strcmp.c b/src/str/ft_strcmp.c index aced711..25d2972 100644 --- a/src/str/ft_strcmp.c +++ b/src/str/ft_strcmp.c @@ -14,10 +14,5 @@ int ft_strcmp(const char *s1, const char *s2) { - while (*s1 && *s2 && *s1 == *s2) - { - s1++; - s2++; - } - return (*s1 - *s2); + return (ft_memcmp(s1, s2, ft_strlen(s1) + 1)); } diff --git a/src/str/ft_strdup.c b/src/str/ft_strdup.c index b248272..9493d82 100644 --- a/src/str/ft_strdup.c +++ b/src/str/ft_strdup.c @@ -11,12 +11,15 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_str.h" char *ft_strdup(const char *s) { char *clone; + size_t size; - if ((clone = (char*)malloc(sizeof(char) * (ft_strlen(s) + 1))) == NULL) + size = ft_strlen(s) + 1; + if ((clone = (char*)malloc(sizeof(char) * size)) == NULL) return (NULL); - return (ft_strcpy(clone, s)); + return (ft_memcpy(clone, s, size)); } diff --git a/src/str/ft_strlen.c b/src/str/ft_strlen.c index 0d593e1..72405c4 100644 --- a/src/str/ft_strlen.c +++ b/src/str/ft_strlen.c @@ -11,31 +11,79 @@ /* ************************************************************************** */ #include "libft.h" +#include + +/* +** Determining if one byte of a long word is 0 +** +** ~((((lw & 0x7F7F7F7F) + 0x7F7F7F7F) | lw) | 0x7F7F7F7F) +** +** where `lw` is a long word +** +** 0x7F -> 0b 0111 1111 +** +** null_high = lw & 0x7F7F7F7F // will set the high bit of each byte to 0 +** overflow = null_high + 0x7F7F7F7F // addition will overflow the high bit is one of the other bits was 1. +** +** oring = overflow | lw // the high bit of a byte is set iff any bit in the byte was set +** ones = oring | 0x7F7F7F7F // the high bits and ones everywhere else +** has_no_zero_byte = ~ones // the ones become zeros, if no high bit was set, there was no zero +** +** +** (lw - 0x01010101) & ~lw & 0x80808080 +** +** overflow = lw - 0x01010101 // overflow the high bit if one was 0 or > 0x80 (0b 1000 0000) 0 || >0x80 +** no_high_bit = ~lw & 0x80808080 // high bit set if the high bit was 0 (i.e < 0x80) 0 || <0x80 +** has_zero = overflow & no_high_bit // (0 || >0x80) && (0 || <0x80) -> 0 && 0 +** +** +** libc's strlen only filter out < 0x80 bytes by omitting the ~lw & 0x80808080 +** part because most string only contain ascii characters. +** +** sources: +** - https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord +** - https://stackoverflow.com/questions/20021066 +*/ + +#define HIMAGIC 0x8080808080808080L +#define LOMAGIC 0x0101010101010101L size_t ft_strlen(const char *s) { - unsigned long int *ptr; - const char *cpy; + uint64_t *ptr; + const char *cpy; + uint64_t lw; - ptr = (unsigned long int*)s; - while (TRUE) + cpy = s; + while (((uint64_t)cpy & 0b111) != 0) { - cpy = (const char*)ptr++; - if (cpy[0] == '\0') + if (*cpy == 0) return (cpy - s); - if (cpy[1] == '\0') - return (cpy + 1 - s); - if (cpy[2] == '\0') - return (cpy + 2 - s); - if (cpy[3] == '\0') - return (cpy + 3 - s); - if (cpy[4] == '\0') - return (cpy + 4 - s); - if (cpy[5] == '\0') - return (cpy + 5 - s); - if (cpy[6] == '\0') - return (cpy + 6 - s); - if (cpy[7] == '\0') - return (cpy + 7 - s); + cpy++; + } + ptr = (uint64_t*)cpy; + while (TRUE) + { + lw = *ptr++; + if (((lw - LOMAGIC) & HIMAGIC) != 0) + { + cpy = (const char*)(ptr - 1); + if (cpy[0] == '\0') + return (cpy - s); + if (cpy[1] == '\0') + return (cpy - s + 1); + if (cpy[2] == '\0') + return (cpy - s + 2); + if (cpy[3] == '\0') + return (cpy - s + 3); + if (cpy[4] == '\0') + return (cpy - s + 4); + if (cpy[5] == '\0') + return (cpy - s + 5); + if (cpy[6] == '\0') + return (cpy - s + 6); + if (cpy[7] == '\0') + return (cpy - s + 7); + } } } diff --git a/src/str/ft_strncat.c b/src/str/ft_strncat.c index d68db0a..4686d59 100644 --- a/src/str/ft_strncat.c +++ b/src/str/ft_strncat.c @@ -14,16 +14,14 @@ char *ft_strncat(char *dest, const char *src, size_t n) { - size_t i; - size_t j; + size_t dest_len; + size_t src_len; - i = ft_strlen(dest); - j = 0; - while (j < n && src[j]) - { - dest[i + j] = src[j]; - j++; - } - dest[i + j] = '\0'; + dest_len = ft_strlen(dest); + src_len = ft_strlen(src); + if (n < src_len) + src_len = n; + ft_memcpy(dest + dest_len, src, src_len); + dest[dest_len + src_len] = '\0'; return (dest); } diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c index caa052b..a0371e4 100644 --- a/src/str/ft_strncmp.c +++ b/src/str/ft_strncmp.c @@ -11,16 +11,12 @@ /* ************************************************************************** */ #include "libft.h" -#include "libft_types.h" +#include "libft_str.h" int ft_strncmp(const char *s1, const char *s2, size_t n) { - size_t i; + size_t len; - if (n == 0) - return (0); - i = 0; - while (i + 1 < n && s1[i] == s2[i] && s1[i]) - i++; - return ((t_ftuchar)s1[i] - (t_ftuchar)s2[i]); + len = ft_strlen(s1); + return (ft_memcmp(s1, s2, n < len ? n : len + 1)); } diff --git a/src/str/ft_strstr.c b/src/str/ft_strstr.c index 4d4d403..893ae1e 100644 --- a/src/str/ft_strstr.c +++ b/src/str/ft_strstr.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_str.h" char *ft_strstr(const char *haystack, const char *needle) { @@ -19,11 +20,5 @@ char *ft_strstr(const char *haystack, const char *needle) needle_len = ft_strlen(needle); if (needle_len == 0) return ((char*)haystack); - while (*haystack) - { - if (ft_strnequ(haystack, needle, needle_len)) - return ((char*)haystack); - haystack++; - } - return (NULL); + return (ft_memmem(haystack, ft_strlen(haystack), needle, needle_len)); } diff --git a/src/str/ft_substr.c b/src/str/ft_substr.c index ad9c706..59fe3f2 100644 --- a/src/str/ft_substr.c +++ b/src/str/ft_substr.c @@ -20,6 +20,7 @@ char *ft_substr(char const *s, unsigned int start, size_t len) return (NULL); if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL) return (NULL); + sub[len] = '\0'; if (start > ft_strlen(s)) return (sub); return (ft_strncpy(sub, s + start, len)); -- cgit From 8bba48a8326d9e7a703c3f6f6ab70b8a8a393473 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 28 Mar 2020 12:07:44 +0100 Subject: Added ft_strjoin3 --- src/str/ft_strjoin3.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/str/ft_strjoin3.c (limited to 'src') diff --git a/src/str/ft_strjoin3.c b/src/str/ft_strjoin3.c new file mode 100644 index 0000000..69de060 --- /dev/null +++ b/src/str/ft_strjoin3.c @@ -0,0 +1,16 @@ +#include "libft.h" + +char *ft_strjoin3(char const *s1, char const *s2, char const *s3) +{ + char *joined; + + if (s1 == NULL || s2 == NULL || s3 == NULL) + return (NULL); + if ((joined = (char*)malloc(sizeof(char) + * (ft_strlen(s1) + ft_strlen(s2) + ft_strlen(s3) + 1))) == NULL) + return (NULL); + ft_strcpy(joined, s1); + ft_strcat(joined, s2); + ft_strcat(joined, s3); + return (joined); +} -- cgit From 40ed37c023627726a5c9c6928284e9f042dc0fa4 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 30 Mar 2020 16:38:30 +0200 Subject: Added Doxygen config file --- src/str/ft_atoi.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c index fa31407..1b9bfc4 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -12,6 +12,10 @@ #include "libft.h" +/** +** Convert a string to an int +*/ + int ft_atoi(const char *str) { return ((int)ft_strtol(str, (char**)NULL, 10)); -- cgit From 901402c99018422c994bdb297e3ba404969c88ea Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 30 Mar 2020 22:26:36 +0200 Subject: Added documentation for ht and lst --- src/ht/ft_htcontent_new.c | 37 ------------------------------------- src/ht/ft_htdelone.c | 12 ++++++++---- src/ht/ft_htdestroy.c | 13 +++++++------ src/ht/ft_htentry_new.c | 37 +++++++++++++++++++++++++++++++++++++ src/ht/ft_htget.c | 12 +++++++----- src/ht/ft_hthash.c | 7 +++++-- src/ht/ft_htiter.c | 10 ++++++---- src/ht/ft_htnew.c | 12 ++++++------ src/ht/ft_htset.c | 36 ++++++++++++++++++++---------------- src/ht/ft_inter_htkey_cmp.c | 2 +- src/io/ft_printf/internals/list.c | 4 ++-- src/io/ft_printf/internals/parse.c | 2 +- src/lst/ft_lstadd_back.c | 26 -------------------------- src/lst/ft_lstadd_front.c | 22 ---------------------- src/lst/ft_lstbsearch.c | 4 ++-- src/lst/ft_lstclear.c | 25 ------------------------- src/lst/ft_lstdelone.c | 10 +++++++--- src/lst/ft_lstdestroy.c | 29 +++++++++++++++++++++++++++++ src/lst/ft_lstiter.c | 10 +++++++--- src/lst/ft_lstlast.c | 8 ++++++-- src/lst/ft_lstlfind.c | 2 +- src/lst/ft_lstlsearch.c | 2 +- src/lst/ft_lstmap.c | 21 ++++++++++++++------- src/lst/ft_lstnew.c | 12 ++++++++---- src/lst/ft_lstpop_front.c | 6 +++++- src/lst/ft_lstpush_back.c | 30 ++++++++++++++++++++++++++++++ src/lst/ft_lstpush_front.c | 26 ++++++++++++++++++++++++++ src/lst/ft_lstremove_if.c | 10 ++++++++-- src/lst/ft_lstreverse.c | 5 ++++- src/lst/ft_lstreverse_ret.c | 6 +++++- src/lst/ft_lstsize.c | 8 ++++++-- src/lst/ft_lstsort.c | 6 ++++++ src/lst/ft_lstsorted_merge.c | 10 +++++++++- 33 files changed, 274 insertions(+), 188 deletions(-) delete mode 100644 src/ht/ft_htcontent_new.c create mode 100644 src/ht/ft_htentry_new.c delete mode 100644 src/lst/ft_lstadd_back.c delete mode 100644 src/lst/ft_lstadd_front.c delete mode 100644 src/lst/ft_lstclear.c create mode 100644 src/lst/ft_lstdestroy.c create mode 100644 src/lst/ft_lstpush_back.c create mode 100644 src/lst/ft_lstpush_front.c (limited to 'src') diff --git a/src/ht/ft_htcontent_new.c b/src/ht/ft_htcontent_new.c deleted file mode 100644 index 7dd4f20..0000000 --- a/src/ht/ft_htcontent_new.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_htcontent_new.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/30 08:45:36 by cacharle #+# #+# */ -/* Updated: 2020/02/17 04:09:50 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "libft_ht.h" - -/* -** Create a new hash table key/value pair. -** `key` is always duplicated. -** Return a pointer to the created content or NULL is an allocation failed. -*/ - -t_ftht_content *ft_htcontent_new(char *key, void *value) -{ - t_ftht_content *content; - - if (key == NULL) - return (NULL); - if ((content = (t_ftht_content*)malloc(sizeof(t_ftht_content))) == NULL) - return (NULL); - if ((content->key = ft_strdup(key)) == NULL) - { - free(content); - return (NULL); - } - content->value = value; - return (content); -} diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index e3bb355..3672b23 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -13,13 +13,17 @@ #include "libft.h" #include "libft_ht.h" -/* -** Delete one hash table entry at `key`. +/** +** \brief Delete one hash table entry +** \param key Key of entry to delete +** \param del Function to destroy the entry +** \warning The del function HAS to free the key +** \note Do nothing if their is to entry which correspond to key */ -void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)) +void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_entry*)) { - ft_lstremove_if(ht->entries + ft_hthash(ht, key), + ft_lstremove_if(ht->buckets + ft_hthash(ht, key), ft_inter_htkey_cmp, key, (void (*)(void*))del); } diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index a788a78..ef4b257 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -13,17 +13,18 @@ #include "libft.h" #include "libft_ht.h" -/* -** Destroy an hash table. -** The `del` function is used to destroy each key/value pair +/** +** \brief Destroy an hash table. +** \param del Function to delete each entry +** \warning The del function HAS to free the key */ -void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)) +void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_entry*)) { if (ht == NULL) return ; while (ht->size-- > 0) - ft_lstclear(ht->entries + ht->size, (void (*)(void*))del); - free(ht->entries); + ft_lstdestroy(ht->buckets + ht->size, (void (*)(void*))del); + free(ht->buckets); free(ht); } diff --git a/src/ht/ft_htentry_new.c b/src/ht/ft_htentry_new.c new file mode 100644 index 0000000..03c0980 --- /dev/null +++ b/src/ht/ft_htentry_new.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htentry_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:45:36 by cacharle #+# #+# */ +/* Updated: 2020/02/17 04:09:50 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +/** +** \brief Create a new hash table key/value pair. +** \param key Hash entry string key (always duplicated) +** \return Content or NULL if an allocation failed. +*/ + +t_ftht_entry *ft_htentry_new(char *key, void *value) +{ + t_ftht_entry *content; + + if (key == NULL) + return (NULL); + if ((content = (t_ftht_entry*)malloc(sizeof(t_ftht_entry))) == NULL) + return (NULL); + if ((content->key = ft_strdup(key)) == NULL) + { + free(content); + return (NULL); + } + content->value = value; + return (content); +} diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index 94a4164..6b5df48 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -13,9 +13,11 @@ #include "libft.h" #include "libft_ht.h" -/* -** Retrieve a value with associated key. -** Returns NULL if there is no value at `key`. +/** +** \brief Retrieve a value with a key +** \param ht Hash table where key is searched +** \param key Searched key +** \return Value void pointer at key or NULL if not found */ void *ft_htget(t_ftht *ht, char *key) @@ -27,8 +29,8 @@ void *ft_htget(t_ftht *ht, char *key) if (ht == NULL || key == NULL) return (NULL); digest = ft_hthash(ht, key); - found = ft_lstlfind(ht->entries[digest], ft_inter_htkey_cmp, key); + found = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); if (found == NULL) return (NULL); - return (((t_ftht_content*)found->content)->value); + return (((t_ftht_entry*)found->data)->value); } diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index ba922e7..2670e31 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -12,8 +12,11 @@ #include "libft_ht.h" -/* -** Hash a string according to the size of the hash table. +/** +** \brief Hash a string +** \param ht So that the index is in the hash table bound +** \param key String to hash +** \return Hash */ // maybe use a less efficient but understandable function diff --git a/src/ht/ft_htiter.c b/src/ht/ft_htiter.c index 5473412..e5ab2eb 100644 --- a/src/ht/ft_htiter.c +++ b/src/ht/ft_htiter.c @@ -1,17 +1,19 @@ #include "libft_ht.h" -/* -** Iterate function `f` over every pair in `ht`. +/** +** \brief Iterate over entry of hash table +** \param ht Iterated hash table +** \param f Function applied to each entry */ -void ft_htiter(t_ftht *ht, void (*f)(t_ftht_content*)) +void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*)) { size_t i; i = 0; while (i < ht->size) { - ft_lstiter(ht->entries[i], (void (*)(void*))f); + ft_lstiter(ht->buckets[i], (void (*)(void*))f); i++; } } diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index 465eabf..e28b544 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -13,10 +13,10 @@ #include "libft.h" #include "libft_ht.h" -/* -** Create a new hash table. -** `size` is the size of the underlying array of linked list (buckets) -** Return NULL is an allocation failed. +/** +** \brief Create a new hash table. +** \param size Size of the underlying array of linked list (buckets) +** \return Created hash table or NULL is an allocation failed */ t_ftht *ft_htnew(t_ftsize size) @@ -27,8 +27,8 @@ t_ftht *ft_htnew(t_ftsize size) return (NULL); if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) return (NULL); - ht->entries = (t_ftht_entry*)ft_calloc(size, sizeof(t_ftht_entry)); - if (ht->entries == NULL) + ht->buckets = (t_ftht_bucket*)ft_calloc(size, sizeof(t_ftht_entry)); + if (ht->buckets == NULL) { free(ht); return (NULL); diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 0755889..9738c14 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -13,39 +13,43 @@ #include "libft.h" #include "libft_ht.h" -/* -** Create/Update a entry in an hash table. -** If `key` already exist in `ht`, updates only the list element content. -** else create a new list node in addition the list content. -** Return a pointer to the created entry, NULL if an allocation failed. +/** +** \brief Create/Update a entry in hash table. +** \note If `key` already exist in `ht`, only updates the list node content. +** Else create a new list node in addition the list content. +** \param ht Hash table where the entry is modified +** \param key Key of the new entry +** \param value Value of the new entry +** \param del Destroy function in case the entry is modified. +** \return Pointer to the created entry, NULL if an allocation failed. */ -t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, - void (*del)(t_ftht_content*)) +t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value, + void (*del)(t_ftht_entry*)) { t_ftht_digest digest; - t_ftht_content *content; - t_ftht_entry entry; + t_ftht_entry *content; + t_ftht_bucket bucket; t_ftlst *tmp; if (ht == NULL || key == NULL) return (NULL); - if ((content = ft_htcontent_new(key, value)) == NULL) + if ((content = ft_htentry_new(key, value)) == NULL) return (NULL); digest = ft_hthash(ht, key); - tmp = ft_lstlfind(ht->entries[digest], ft_inter_htkey_cmp, key); + tmp = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); if (tmp != NULL) { if (del != NULL) - del(tmp->content); - tmp->content = content; - return ((t_ftht_content*)tmp->content); + del(tmp->data); + tmp->data = content; + return ((t_ftht_entry*)tmp->data); } - if ((entry = ft_lstnew(content)) == NULL) + if ((bucket = ft_lstnew(content)) == NULL) { del(content); return (NULL); } - ft_lstadd_front(ht->entries + digest, entry); + ft_lstpush_front(ht->buckets + digest, bucket); return (content); } diff --git a/src/ht/ft_inter_htkey_cmp.c b/src/ht/ft_inter_htkey_cmp.c index 397177e..e8a0375 100644 --- a/src/ht/ft_inter_htkey_cmp.c +++ b/src/ht/ft_inter_htkey_cmp.c @@ -21,5 +21,5 @@ int ft_inter_htkey_cmp(const void *ref_key, const void *content) { if (ref_key == NULL || content == NULL) return (-1); - return (ft_strcmp((char*)ref_key, ((t_ftht_content*)content)->key)); + return (ft_strcmp((char*)ref_key, ((t_ftht_entry*)content)->key)); } diff --git a/src/io/ft_printf/internals/list.c b/src/io/ft_printf/internals/list.c index 99491f4..37f8013 100644 --- a/src/io/ft_printf/internals/list.c +++ b/src/io/ft_printf/internals/list.c @@ -18,7 +18,7 @@ t_flist *list_new(t_pformat *content) if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL) return (NULL); - lst->content = content; + lst->data = content; lst->next = NULL; return (lst); } @@ -47,7 +47,7 @@ void list_pop_front(t_flist **lst) if (lst == NULL || *lst == NULL) return ; tmp = (*lst)->next; - free((*lst)->content); + free((*lst)->data); free(*lst); *lst = tmp; } diff --git a/src/io/ft_printf/internals/parse.c b/src/io/ft_printf/internals/parse.c index 33928a0..4650481 100644 --- a/src/io/ft_printf/internals/parse.c +++ b/src/io/ft_printf/internals/parse.c @@ -28,7 +28,7 @@ int parse(const char *format, t_flist **flist) if ((tmp = list_new(parsed)) == NULL) return ((int)list_destroy(flist)); list_push_front(flist, tmp); - format += (*flist)->content->fmt_len; + format += (*flist)->data->fmt_len; } *flist = list_reverse(*flist); return (1); diff --git a/src/lst/ft_lstadd_back.c b/src/lst/ft_lstadd_back.c deleted file mode 100644 index 8f39a75..0000000 --- a/src/lst/ft_lstadd_back.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstadd_back_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/09 09:02:03 by cacharle #+# #+# */ -/* Updated: 2019/11/20 04:01:26 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "libft_lst.h" - -void ft_lstadd_back(t_ftlst **alst, t_ftlst *new) -{ - if (alst == NULL) - return ; - if (*alst == NULL) - { - *alst = new; - return ; - } - ft_lstlast(*alst)->next = new; -} diff --git a/src/lst/ft_lstadd_front.c b/src/lst/ft_lstadd_front.c deleted file mode 100644 index bcd5ad9..0000000 --- a/src/lst/ft_lstadd_front.c +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstadd_front_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/09 09:02:25 by cacharle #+# #+# */ -/* Updated: 2019/10/18 12:16:06 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "libft_lst.h" - -void ft_lstadd_front(t_ftlst **alst, t_ftlst *new) -{ - if (alst == NULL || new == NULL) - return ; - new->next = *alst; - *alst = new; -} 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_lstclear.c b/src/lst/ft_lstclear.c deleted file mode 100644 index 0bacb4f..0000000 --- a/src/lst/ft_lstclear.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lstclear_bonus.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/09 09:02:39 by cacharle #+# #+# */ -/* Updated: 2019/11/20 04:02:37 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include "libft_lst.h" - -void ft_lstclear(t_ftlst **lst, void (*del)(void *)) -{ - if (lst == NULL) - return ; - if (*lst == NULL) - return ; - ft_lstclear(&((*lst)->next), del); - ft_lstdelone(*lst, del); - *lst = NULL; -} 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -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_lstdestroy.c b/src/lst/ft_lstdestroy.c new file mode 100644 index 0000000..5e55f04 --- /dev/null +++ b/src/lst/ft_lstdestroy.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:39 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:37 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/** +** \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_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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -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_lstpush_back.c b/src/lst/ft_lstpush_back.c new file mode 100644 index 0000000..372c18c --- /dev/null +++ b/src/lst/ft_lstpush_back.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpush_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:03 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/** +** \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 ; + if (*alst == NULL) + { + *alst = new; + return ; + } + ft_lstlast(*alst)->next = new; +} diff --git a/src/lst/ft_lstpush_front.c b/src/lst/ft_lstpush_front.c new file mode 100644 index 0000000..c17a586 --- /dev/null +++ b/src/lst/ft_lstpush_front.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpush_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:25 by cacharle #+# #+# */ +/* Updated: 2019/10/18 12:16:06 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/** +** \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 ; + new->next = *alst; + *alst = new; +} 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -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); -- cgit From 9316f2063255bd4a0abd5c38d4c065969a8980bb Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 1 Apr 2020 18:10:36 +0200 Subject: Norm compliant comment format, dirty script for doxygen comments --- src/ht/ft_htdelone.c | 2 +- src/ht/ft_htdestroy.c | 2 +- src/ht/ft_htentry_new.c | 2 +- src/ht/ft_htget.c | 5 ++--- src/ht/ft_hthash.c | 2 +- src/ht/ft_htiter.c | 14 +++++++++++++- src/ht/ft_htnew.c | 2 +- src/ht/ft_htset.c | 7 ++++--- src/lst/ft_lstdelone.c | 2 +- src/lst/ft_lstdestroy.c | 2 +- src/lst/ft_lstiter.c | 2 +- src/lst/ft_lstlast.c | 2 +- src/lst/ft_lstmap.c | 2 +- src/lst/ft_lstnew.c | 2 +- src/lst/ft_lstpop_front.c | 2 +- src/lst/ft_lstpush_back.c | 2 +- src/lst/ft_lstpush_front.c | 2 +- src/lst/ft_lstremove_if.c | 2 +- src/lst/ft_lstreverse.c | 2 +- src/lst/ft_lstreverse_ret.c | 2 +- src/lst/ft_lstsize.c | 2 +- src/lst/ft_lstsort.c | 2 +- src/lst/ft_lstsorted_merge.c | 2 +- src/str/ft_atoi.c | 2 +- src/str/ft_strjoin3.c | 20 ++++++++++++++++++++ 25 files changed, 60 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 3672b23..7374a44 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -13,7 +13,7 @@ #include "libft.h" #include "libft_ht.h" -/** +/* ** \brief Delete one hash table entry ** \param key Key of entry to delete ** \param del Function to destroy the entry diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index ef4b257..ff362d2 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -13,7 +13,7 @@ #include "libft.h" #include "libft_ht.h" -/** +/* ** \brief Destroy an hash table. ** \param del Function to delete each entry ** \warning The del function HAS to free the key diff --git a/src/ht/ft_htentry_new.c b/src/ht/ft_htentry_new.c index 03c0980..12a1159 100644 --- a/src/ht/ft_htentry_new.c +++ b/src/ht/ft_htentry_new.c @@ -13,7 +13,7 @@ #include "libft.h" #include "libft_ht.h" -/** +/* ** \brief Create a new hash table key/value pair. ** \param key Hash entry string key (always duplicated) ** \return Content or NULL if an allocation failed. diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index 6b5df48..a6383fe 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,14 +6,14 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:21:09 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 18:02:57 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" -/** +/* ** \brief Retrieve a value with a key ** \param ht Hash table where key is searched ** \param key Searched key @@ -22,7 +22,6 @@ void *ft_htget(t_ftht *ht, char *key) { - t_ftht_digest digest; t_ftlst *found; diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index 2670e31..3369d24 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -12,7 +12,7 @@ #include "libft_ht.h" -/** +/* ** \brief Hash a string ** \param ht So that the index is in the hash table bound ** \param key String to hash diff --git a/src/ht/ft_htiter.c b/src/ht/ft_htiter.c index e5ab2eb..b854993 100644 --- a/src/ht/ft_htiter.c +++ b/src/ht/ft_htiter.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:02:24 by charles #+# #+# */ +/* Updated: 2020/04/01 18:02:32 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "libft_ht.h" -/** +/* ** \brief Iterate over entry of hash table ** \param ht Iterated hash table ** \param f Function applied to each entry diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index e28b544..e5335d2 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -13,7 +13,7 @@ #include "libft.h" #include "libft_ht.h" -/** +/* ** \brief Create a new hash table. ** \param size Size of the underlying array of linked list (buckets) ** \return Created hash table or NULL is an allocation failed diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 9738c14..68d3752 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,16 +6,17 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:11:00 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 18:02:12 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" -/** +/* ** \brief Create/Update a entry in hash table. -** \note If `key` already exist in `ht`, only updates the list node content. +** \note If `key` already exist in `ht` +** only updates the list node content. ** Else create a new list node in addition the list content. ** \param ht Hash table where the entry is modified ** \param key Key of the new entry diff --git a/src/lst/ft_lstdelone.c b/src/lst/ft_lstdelone.c index 40e25cd..3dfbbbb 100644 --- a/src/lst/ft_lstdelone.c +++ b/src/lst/ft_lstdelone.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Delete list node ** \param del Delete function for node's data */ diff --git a/src/lst/ft_lstdestroy.c b/src/lst/ft_lstdestroy.c index 5e55f04..35da2a5 100644 --- a/src/lst/ft_lstdestroy.c +++ b/src/lst/ft_lstdestroy.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Destroy a list and set his pointer to NULL ** \param del Delete Function for data of each node */ diff --git a/src/lst/ft_lstiter.c b/src/lst/ft_lstiter.c index 39d870b..e46b507 100644 --- a/src/lst/ft_lstiter.c +++ b/src/lst/ft_lstiter.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Iterate of list ** \param f Funtion applied to data of each node */ diff --git a/src/lst/ft_lstlast.c b/src/lst/ft_lstlast.c index 12fdeee..97b853d 100644 --- a/src/lst/ft_lstlast.c +++ b/src/lst/ft_lstlast.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Last node ** \return List's last node */ diff --git a/src/lst/ft_lstmap.c b/src/lst/ft_lstmap.c index bf96892..3182bb0 100644 --- a/src/lst/ft_lstmap.c +++ b/src/lst/ft_lstmap.c @@ -12,7 +12,7 @@ #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 diff --git a/src/lst/ft_lstnew.c b/src/lst/ft_lstnew.c index 41c8153..1616b71 100644 --- a/src/lst/ft_lstnew.c +++ b/src/lst/ft_lstnew.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Create a list node ** \param data Pointer to data of node */ diff --git a/src/lst/ft_lstpop_front.c b/src/lst/ft_lstpop_front.c index 798bf83..a61350a 100644 --- a/src/lst/ft_lstpop_front.c +++ b/src/lst/ft_lstpop_front.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Delete head node and replace it with next node ** \param del Delete function for node data */ diff --git a/src/lst/ft_lstpush_back.c b/src/lst/ft_lstpush_back.c index 372c18c..1dca078 100644 --- a/src/lst/ft_lstpush_back.c +++ b/src/lst/ft_lstpush_back.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Push new node to the list end ** \param new Pushed node */ diff --git a/src/lst/ft_lstpush_front.c b/src/lst/ft_lstpush_front.c index c17a586..85df649 100644 --- a/src/lst/ft_lstpush_front.c +++ b/src/lst/ft_lstpush_front.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Push node to list front ** \param new Pushed node */ diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c index fdac710..4070355 100644 --- a/src/lst/ft_lstremove_if.c +++ b/src/lst/ft_lstremove_if.c @@ -12,7 +12,7 @@ #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` diff --git a/src/lst/ft_lstreverse.c b/src/lst/ft_lstreverse.c index fd5259b..7c2778d 100644 --- a/src/lst/ft_lstreverse.c +++ b/src/lst/ft_lstreverse.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Reverse a list */ diff --git a/src/lst/ft_lstreverse_ret.c b/src/lst/ft_lstreverse_ret.c index 259af9e..36c0c5c 100644 --- a/src/lst/ft_lstreverse_ret.c +++ b/src/lst/ft_lstreverse_ret.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Reverse list ** \return Pointer to reversed list */ diff --git a/src/lst/ft_lstsize.c b/src/lst/ft_lstsize.c index 6a92b99..3c6956b 100644 --- a/src/lst/ft_lstsize.c +++ b/src/lst/ft_lstsize.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief List size ** \return Number of node in list */ diff --git a/src/lst/ft_lstsort.c b/src/lst/ft_lstsort.c index fbd046a..9945a0f 100644 --- a/src/lst/ft_lstsort.c +++ b/src/lst/ft_lstsort.c @@ -12,7 +12,7 @@ #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 diff --git a/src/lst/ft_lstsorted_merge.c b/src/lst/ft_lstsorted_merge.c index 0cd5721..995785f 100644 --- a/src/lst/ft_lstsorted_merge.c +++ b/src/lst/ft_lstsorted_merge.c @@ -12,7 +12,7 @@ #include "libft_lst.h" -/** +/* ** \brief Merge sorted lists, the new list is also sorted ** \param l1 First list ** \param l2 Second list diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c index 1b9bfc4..d6fa5bb 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -12,7 +12,7 @@ #include "libft.h" -/** +/* ** Convert a string to an int */ diff --git a/src/str/ft_strjoin3.c b/src/str/ft_strjoin3.c index 69de060..e5e5530 100644 --- a/src/str/ft_strjoin3.c +++ b/src/str/ft_strjoin3.c @@ -1,5 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:00:49 by charles #+# #+# */ +/* Updated: 2020/04/01 18:01:43 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "libft.h" +/* +** \brief Join 3 strings in a new malloc'd one +** \param s1 String 1 +** \param s2 String 2 +** \param s3 String 3 +** \return The joined string +*/ + char *ft_strjoin3(char const *s1, char const *s2, char const *s3) { char *joined; -- cgit From a1675f56b35f5521a91851bae8ca650706374ae6 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 1 Apr 2020 21:21:10 +0200 Subject: Added vector --- src/vec/ft_vecdestroy.c | 29 +++++++++++++++++++++++++++++ src/vec/ft_vecgrow.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/vec/ft_veciter.c | 31 +++++++++++++++++++++++++++++++ src/vec/ft_vecnew.c | 38 ++++++++++++++++++++++++++++++++++++++ src/vec/ft_vecpop.c | 28 ++++++++++++++++++++++++++++ src/vec/ft_vecpush.c | 30 ++++++++++++++++++++++++++++++ 6 files changed, 199 insertions(+) create mode 100644 src/vec/ft_vecdestroy.c create mode 100644 src/vec/ft_vecgrow.c create mode 100644 src/vec/ft_veciter.c create mode 100644 src/vec/ft_vecnew.c create mode 100644 src/vec/ft_vecpop.c create mode 100644 src/vec/ft_vecpush.c (limited to 'src') diff --git a/src/vec/ft_vecdestroy.c b/src/vec/ft_vecdestroy.c new file mode 100644 index 0000000..781e02e --- /dev/null +++ b/src/vec/ft_vecdestroy.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:06:22 by charles #+# #+# */ +/* Updated: 2020/04/01 19:09:09 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Destroy a vector +** \param vec Vector to destroy +** \param del Delete function applied to each element of the vector +*/ + +void ft_vecdestroy(t_ftvec *vec, void (*del)(void *elem)) +{ + if (vec == NULL) + return ; + if (del != NULL) + ft_veciter(vec, del); + free(vec->data); + free(vec); +} diff --git a/src/vec/ft_vecgrow.c b/src/vec/ft_vecgrow.c new file mode 100644 index 0000000..bb8b8c7 --- /dev/null +++ b/src/vec/ft_vecgrow.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:13:07 by charles #+# #+# */ +/* Updated: 2020/04/01 21:15:20 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Vector Growth factor +*/ + +#define FT_VEC_GROWTH_FACTOR 1.5 + +/* +** \brief Grow the vector capacity by a constant factor +** \param vec Vector to grow +** \return Passed vector of NULL on error +*/ + +t_ftvec *ft_vecgrow(t_ftvec *vec) +{ + size_t new_capacity; + void **new_data; + + if (vec->capacity <= 1) + new_capacity = 2; + else + new_capacity = vec->capacity * FT_VEC_GROWTH_FACTOR; + if ((new_data = (void**)malloc(sizeof(void*) * new_capacity)) == NULL) + return (NULL); + ft_memcpy(new_data, vec->data, vec->size); + free(vec->data); + vec->data = new_data; + vec->capacity = new_capacity; + return (vec); +} diff --git a/src/vec/ft_veciter.c b/src/vec/ft_veciter.c new file mode 100644 index 0000000..ec4a917 --- /dev/null +++ b/src/vec/ft_veciter.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_veciter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:09:51 by charles #+# #+# */ +/* Updated: 2020/04/01 20:15:13 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Iterate a function over elements of a vector +** \param vec Iterated vector +** \param f Function applied to each elements +*/ + +void ft_veciter(t_ftvec *vec, void (*f)(void *elem)) +{ + size_t i; + + i = 0; + while (i < vec->size) + { + f(vec->data[i]); + i++; + } +} diff --git a/src/vec/ft_vecnew.c b/src/vec/ft_vecnew.c new file mode 100644 index 0000000..8a8736a --- /dev/null +++ b/src/vec/ft_vecnew.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:03:49 by charles #+# #+# */ +/* Updated: 2020/04/01 20:00:00 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Create a new vector +** \param capacity Initial capacity of the underlying array +** Can't be lower than 1 +** \return Created vector or NULL on malloc error +*/ + +t_ftvec *ft_vecnew(size_t capacity) +{ + t_ftvec *vec; + + if ((vec = (t_ftvec*)malloc(sizeof(t_ftvec))) == NULL) + return (NULL); + if (capacity == 0) + capacity = 1; + if ((vec->data = (void**)malloc(sizeof(void*) * capacity)) == NULL) + { + free(vec); + return (NULL); + } + vec->capacity = capacity; + vec->size = 0; + return (vec); +} diff --git a/src/vec/ft_vecpop.c b/src/vec/ft_vecpop.c new file mode 100644 index 0000000..5b77b46 --- /dev/null +++ b/src/vec/ft_vecpop.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:26:27 by charles #+# #+# */ +/* Updated: 2020/04/01 20:26:34 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Pop last element of a vector +** \param vec Vector poped +** \param del Delete function applied to last element +*/ + +void ft_vecpop(t_ftvec *vec, void (*del)(void *elem)) +{ + if (vec->size == 0) + return ; + if (del != NULL) + del(vec->data[vec->size - 1]); + vec->size--; +} diff --git a/src/vec/ft_vecpush.c b/src/vec/ft_vecpush.c new file mode 100644 index 0000000..fc903ef --- /dev/null +++ b/src/vec/ft_vecpush.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpush.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:22:20 by charles #+# #+# */ +/* Updated: 2020/04/01 20:20:06 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Push element at the end of vector +** \param vec Vector where element will be pushed +** \param pushed Element to push +** \return Passed vector or NULL if couldnt grow vector +*/ + +t_ftvec *ft_vecpush(t_ftvec *vec, void *pushed) +{ + if (vec->capacity <= vec->size) + if (ft_vecgrow(vec) == NULL) + return (NULL); + vec->data[vec->size] = pushed; + vec->size++; + return (vec); +} -- cgit From 1925805cc760061d5742f9d215998561fcd45211 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 1 Apr 2020 23:02:59 +0200 Subject: Added ft_vecremove, Updated README.md --- src/vec/ft_vecremove.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/vec/ft_vecremove.c (limited to 'src') diff --git a/src/vec/ft_vecremove.c b/src/vec/ft_vecremove.c new file mode 100644 index 0000000..d24ba29 --- /dev/null +++ b/src/vec/ft_vecremove.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecremove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 22:45:07 by charles #+# #+# */ +/* Updated: 2020/04/01 22:58:21 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Remove element from vector +** \param vec Vector to remove from +** \param i Index of the element to remove +** \param del Delete function applied to ith element +*/ + +void ft_vecremove(t_ftvec *vec, size_t i, void (*del)(void *elem)) +{ + if (vec->size == 0 || i > vec->size - 1) + return ; + if (del != NULL) + del(vec->data[i]); + ft_memmove(vec->data + i, vec->data + i + 1, + (vec->size - i - 1) * sizeof(void*)); + vec->size--; +} -- cgit From a2452f88fffffe4e2a278578de5ac961858a5b72 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 2 Apr 2020 11:20:44 +0200 Subject: Fixed ft_vecgrow, Added ft_vecinsert --- src/vec/ft_vecgrow.c | 4 ++-- src/vec/ft_vecinsert.c | 36 ++++++++++++++++++++++++++++++++++++ src/vec/ft_vecpush.c | 2 +- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/vec/ft_vecinsert.c (limited to 'src') diff --git a/src/vec/ft_vecgrow.c b/src/vec/ft_vecgrow.c index bb8b8c7..2213c88 100644 --- a/src/vec/ft_vecgrow.c +++ b/src/vec/ft_vecgrow.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:13:07 by charles #+# #+# */ -/* Updated: 2020/04/01 21:15:20 by charles ### ########.fr */ +/* Updated: 2020/04/02 10:43:01 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ t_ftvec *ft_vecgrow(t_ftvec *vec) new_capacity = vec->capacity * FT_VEC_GROWTH_FACTOR; if ((new_data = (void**)malloc(sizeof(void*) * new_capacity)) == NULL) return (NULL); - ft_memcpy(new_data, vec->data, vec->size); + ft_memcpy(new_data, vec->data, vec->size * sizeof(void*)); free(vec->data); vec->data = new_data; vec->capacity = new_capacity; diff --git a/src/vec/ft_vecinsert.c b/src/vec/ft_vecinsert.c new file mode 100644 index 0000000..1682daa --- /dev/null +++ b/src/vec/ft_vecinsert.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/02 10:46:59 by charles #+# #+# */ +/* Updated: 2020/04/02 11:04:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Insert element in vector +** \param vec Vector where element is inserted +** \param i Index where element should be inserted, +** bound checking is performed +** \param elem Element to insert +** \return Passed vector or NULL on error +*/ + +t_ftvec *ft_vecinsert(t_ftvec *vec, size_t i, void *elem) +{ + if (i > vec->size) + return (NULL); + if (vec->capacity <= vec->size) + if (ft_vecgrow(vec) == NULL) + return (NULL); + ft_memmove(vec->data + i + 1, vec->data + i, + (vec->size - i) * sizeof(void*)); + vec->data[i] = elem; + vec->size++; + return (vec); +} diff --git a/src/vec/ft_vecpush.c b/src/vec/ft_vecpush.c index fc903ef..026ae3d 100644 --- a/src/vec/ft_vecpush.c +++ b/src/vec/ft_vecpush.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:22:20 by charles #+# #+# */ -/* Updated: 2020/04/01 20:20:06 by charles ### ########.fr */ +/* Updated: 2020/04/02 10:51:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit From 948c0953527fe3bef28904b38a16a9e4342e7e98 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 3 Apr 2020 00:29:26 +0200 Subject: Added ft_fnmatch function --- src/str/ft_fnmatch.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/str/ft_fnmatch.c (limited to 'src') diff --git a/src/str/ft_fnmatch.c b/src/str/ft_fnmatch.c new file mode 100644 index 0000000..5fc35d8 --- /dev/null +++ b/src/str/ft_fnmatch.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_fnmatch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/02 23:24:16 by charles #+# #+# */ +/* Updated: 2020/04/03 00:28:46 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Search a glob pattern in a string +** \param pattern Glob pattern '*' are interpreted as zero or more character +** \param string String to search in +** \return True if pattern was found, false otherwise +*/ + +bool ft_fnmatch(const char *pattern, const char *string) +{ + if (*pattern == '\0') + return (*string == '\0'); + if (*string == '\0') + return (*pattern == '\0' || (*pattern == '*' && pattern[1] == '\0')); + if (*pattern == '*') + { + if (ft_fnmatch(pattern + 1, string)) + return (true); + return (ft_fnmatch(pattern, string + 1)); + } + if (*pattern != *string) + return (false); + return (ft_fnmatch(pattern + 1, string + 1)); +} -- cgit From d2feec1f97e9f8f201e56ad33662bb663c328a0a Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 3 Apr 2020 07:19:25 +0200 Subject: Changing hash table del function to regular one with a first order internal function, removing a few typedef to instead use standard types --- src/algo/ft_is_set.c | 5 ++--- src/ht/ft_htdelone.c | 11 ++++++----- src/ht/ft_htdestroy.c | 10 ++++++---- src/ht/ft_htget.c | 6 +++--- src/ht/ft_hthash.c | 4 ++-- src/ht/ft_htnew.c | 8 ++++---- src/ht/ft_htset.c | 37 ++++++++++++++++++++----------------- src/ht/ft_inter_htdel_first_order.c | 33 +++++++++++++++++++++++++++++++++ src/str/ft_strcasecmp.c | 2 +- src/str/ft_strncasecmp.c | 2 +- src/str/ft_strncmp.c | 2 +- 11 files changed, 79 insertions(+), 41 deletions(-) create mode 100644 src/ht/ft_inter_htdel_first_order.c (limited to 'src') diff --git a/src/algo/ft_is_set.c b/src/algo/ft_is_set.c index 3e7ae31..79a5e7c 100644 --- a/src/algo/ft_is_set.c +++ b/src/algo/ft_is_set.c @@ -6,14 +6,13 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:51:41 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:05:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_algo.h" -t_ftbool ft_is_set(void *base, size_t nel, size_t width, +bool ft_is_set(void *base, size_t nel, size_t width, t_ftcompar_func compar) { size_t i; diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 7374a44..bc2e047 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:10:16 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:14:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,15 @@ /* ** \brief Delete one hash table entry ** \param key Key of entry to delete -** \param del Function to destroy the entry -** \warning The del function HAS to free the key +** \param del Function to destroy the entry value ** \note Do nothing if their is to entry which correspond to key */ -void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_entry*)) +void ft_htdelone(t_ftht *ht, char *key, void (*del)(void*)) { + ft_inter_htdel_first_order_setup(del); ft_lstremove_if(ht->buckets + ft_hthash(ht, key), ft_inter_htkey_cmp, key, - (void (*)(void*))del); + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); } diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index ff362d2..c43754e 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -6,11 +6,10 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:10:31 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:01:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" /* @@ -19,12 +18,15 @@ ** \warning The del function HAS to free the key */ -void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_entry*)) +void ft_htdestroy(t_ftht *ht, t_ftdel_func del) { if (ht == NULL) return ; + ft_inter_htdel_first_order_setup(del); while (ht->size-- > 0) - ft_lstdestroy(ht->buckets + ht->size, (void (*)(void*))del); + ft_lstdestroy(ht->buckets + ht->size, + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); free(ht->buckets); free(ht); } diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index a6383fe..75da785 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/04/01 18:02:57 by charles ### ########.fr */ +/* Updated: 2020/04/03 07:12:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,8 @@ void *ft_htget(t_ftht *ht, char *key) { - t_ftht_digest digest; - t_ftlst *found; + size_t digest; + t_ftlst *found; if (ht == NULL || key == NULL) return (NULL); diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index 3369d24..c5d42ea 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -20,9 +20,9 @@ */ // maybe use a less efficient but understandable function -t_ftht_digest ft_hthash(t_ftht *ht, char *key) +size_t ft_hthash(t_ftht *ht, char *key) { - t_ftht_digest digest; + size_t digest; if (*key == '\0') return (0); diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index e5335d2..b6d1cc3 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,12 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:23:43 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 06:53:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" +#include "libft_mem.h" /* ** \brief Create a new hash table. @@ -19,7 +19,7 @@ ** \return Created hash table or NULL is an allocation failed */ -t_ftht *ft_htnew(t_ftsize size) +t_ftht *ft_htnew(size_t size) { t_ftht *ht; @@ -27,7 +27,7 @@ t_ftht *ft_htnew(t_ftsize size) return (NULL); if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) return (NULL); - ht->buckets = (t_ftht_bucket*)ft_calloc(size, sizeof(t_ftht_entry)); + ht->buckets = (t_ftlst**)ft_calloc(size, sizeof(t_ftlst*)); if (ht->buckets == NULL) { free(ht); diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 68d3752..b3a44ee 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,18 +6,17 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/04/01 18:02:12 by charles ### ########.fr */ +/* Updated: 2020/04/03 07:13:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" /* ** \brief Create/Update a entry in hash table. ** \note If `key` already exist in `ht` -** only updates the list node content. -** Else create a new list node in addition the list content. +** only updates the list node entry. +** Else create a new list node in addition the list entry. ** \param ht Hash table where the entry is modified ** \param key Key of the new entry ** \param value Value of the new entry @@ -25,32 +24,36 @@ ** \return Pointer to the created entry, NULL if an allocation failed. */ -t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value, - void (*del)(t_ftht_entry*)) +t_ftht_entry *ft_htset( + t_ftht *ht, + char *key, + void *value, + void (*del)(void*) +) { - t_ftht_digest digest; - t_ftht_entry *content; - t_ftht_bucket bucket; + size_t digest; + t_ftht_entry *entry; t_ftlst *tmp; if (ht == NULL || key == NULL) return (NULL); - if ((content = ft_htentry_new(key, value)) == NULL) - return (NULL); digest = ft_hthash(ht, key); tmp = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); if (tmp != NULL) { if (del != NULL) - del(tmp->data); - tmp->data = content; + del(((t_ftht_entry*)tmp->data)->value); + ((t_ftht_entry*)tmp->data)->value = value; return ((t_ftht_entry*)tmp->data); } - if ((bucket = ft_lstnew(content)) == NULL) + if ((entry = ft_htentry_new(key, value)) == NULL) + return (NULL); + if ((tmp = ft_lstnew(entry)) == NULL) { - del(content); + free(entry->key); + free(entry); return (NULL); } - ft_lstpush_front(ht->buckets + digest, bucket); - return (content); + ft_lstpush_front(ht->buckets + digest, tmp); + return (entry); } diff --git a/src/ht/ft_inter_htdel_first_order.c b/src/ht/ft_inter_htdel_first_order.c new file mode 100644 index 0000000..b6fd770 --- /dev/null +++ b/src/ht/ft_inter_htdel_first_order.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_inter_htdel_first_order.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 06:56:54 by charles #+# #+# */ +/* Updated: 2020/04/03 06:58:36 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +static t_ftdel_func g_htdelone_value_del_func = NULL; + +void ft_inter_htdel_first_order(t_ftht_entry *entry) +{ + if (g_htdelone_value_del_func != NULL) + g_htdelone_value_del_func(entry->value); + free(entry->key); + free(entry); +} + +void ft_inter_htdel_first_order_setup(t_ftdel_func del) +{ + g_htdelone_value_del_func = del; +} + +void ft_inter_htdel_first_order_teardown(void) +{ + g_htdelone_value_del_func = NULL; +} diff --git a/src/str/ft_strcasecmp.c b/src/str/ft_strcasecmp.c index 044e6de..6dd86eb 100644 --- a/src/str/ft_strcasecmp.c +++ b/src/str/ft_strcasecmp.c @@ -11,7 +11,7 @@ /* ************************************************************************** */ #include "libft_str.h" -#include "libft_types.h" +#include "libft_def.h" int ft_strcasecmp(const char *s1, const char *s2) { diff --git a/src/str/ft_strncasecmp.c b/src/str/ft_strncasecmp.c index aafdc8c..7153237 100644 --- a/src/str/ft_strncasecmp.c +++ b/src/str/ft_strncasecmp.c @@ -11,7 +11,7 @@ /* ************************************************************************** */ #include "libft.h" -#include "libft_types.h" +#include "libft_def.h" int ft_strncasecmp(const char *s1, const char *s2, size_t n) { diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c index caa052b..d810e8c 100644 --- a/src/str/ft_strncmp.c +++ b/src/str/ft_strncmp.c @@ -11,7 +11,7 @@ /* ************************************************************************** */ #include "libft.h" -#include "libft_types.h" +#include "libft_def.h" int ft_strncmp(const char *s1, const char *s2, size_t n) { -- cgit From bf263810b0a47a68fae0681e6e6d2229a488c069 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 3 Apr 2020 15:11:33 +0200 Subject: Added Dynamic string struct (no test, no doc) --- src/dstr/ft_dstrdestroy.c | 21 +++++++++++++++++++++ src/dstr/ft_dstrgrow.c | 31 +++++++++++++++++++++++++++++++ src/dstr/ft_dstrinsert.c | 31 +++++++++++++++++++++++++++++++ src/dstr/ft_dstrnew.c | 25 +++++++++++++++++++++++++ src/dstr/ft_dstrunwrap.c | 22 ++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 src/dstr/ft_dstrdestroy.c create mode 100644 src/dstr/ft_dstrgrow.c create mode 100644 src/dstr/ft_dstrinsert.c create mode 100644 src/dstr/ft_dstrnew.c create mode 100644 src/dstr/ft_dstrunwrap.c (limited to 'src') diff --git a/src/dstr/ft_dstrdestroy.c b/src/dstr/ft_dstrdestroy.c new file mode 100644 index 0000000..6f1a819 --- /dev/null +++ b/src/dstr/ft_dstrdestroy.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:58:46 by charles #+# #+# */ +/* Updated: 2020/04/03 13:59:25 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +void ft_dstrdestroy(t_ftdstr *dstr) +{ + if (dstr == NULL) + return ; + free(dstr->str); + free(dstr); +} diff --git a/src/dstr/ft_dstrgrow.c b/src/dstr/ft_dstrgrow.c new file mode 100644 index 0000000..9ad51ea --- /dev/null +++ b/src/dstr/ft_dstrgrow.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 14:17:09 by charles #+# #+# */ +/* Updated: 2020/04/03 15:09:21 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +#define FT_DSTR_GROWTH_FACTOR 1.5 + +t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least) +{ + size_t new_capacity; + char *new_str; + + new_capacity = dstr->capacity <= 1 ? 2 : dstr->capacity; + while (new_capacity < at_least) + new_capacity *= FT_DSTR_GROWTH_FACTOR; + if ((new_str = (char*)malloc(sizeof(char) * new_capacity)) == NULL) + return (NULL); + ft_memcpy(new_str, dstr->str, dstr->capacity); + dstr->capacity = new_capacity; + dstr->str = new_str; + return (dstr); +} diff --git a/src/dstr/ft_dstrinsert.c b/src/dstr/ft_dstrinsert.c new file mode 100644 index 0000000..6a1486c --- /dev/null +++ b/src/dstr/ft_dstrinsert.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 14:00:44 by charles #+# #+# */ +/* Updated: 2020/04/03 15:09:52 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i) +{ + size_t inserted_len; + + if (i > dstr->capacity) + return (NULL); + inserted_len = ft_strlen(inserted); + if (dstr->capacity - dstr->length - 1 < inserted_len) + if (ft_dstrgrow(dstr, dstr->capacity + inserted_len + 1) == NULL) + return (NULL); + ft_memmove(dstr->str + i + inserted_len, + dstr->str + i, + dstr->length - i + 1); + ft_memcpy(dstr->str + i, inserted, inserted_len); + dstr->length += inserted_len; + return (dstr); +} diff --git a/src/dstr/ft_dstrnew.c b/src/dstr/ft_dstrnew.c new file mode 100644 index 0000000..189fc78 --- /dev/null +++ b/src/dstr/ft_dstrnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:54:52 by charles #+# #+# */ +/* Updated: 2020/04/03 13:58:17 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +t_ftdstr *ft_dstrnew(char *from) +{ + t_ftdstr *dstr; + + if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL || + (dstr->str = ft_strdup(from)) == NULL) + return (NULL); + dstr->length = ft_strlen(from); + dstr->capacity = dstr->length + 1; + return (dstr); +} diff --git a/src/dstr/ft_dstrunwrap.c b/src/dstr/ft_dstrunwrap.c new file mode 100644 index 0000000..ba62a23 --- /dev/null +++ b/src/dstr/ft_dstrunwrap.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrunwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:59:35 by charles #+# #+# */ +/* Updated: 2020/04/03 14:00:36 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +char *ft_dstrunwrap(t_ftdstr *dstr) +{ + char *tmp; + + tmp = dstr->str; + free(dstr); + return (tmp); +} -- cgit From 636c3ff6b600c291a40877ac52d8b0a1a58b9b79 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 3 Apr 2020 17:06:38 +0200 Subject: Added doubly linked list struct and basic functions on it --- src/dlst/ft_dlstdelone.c | 26 ++++++++++++++++++++++++++ src/dlst/ft_dlstdestroy.c | 32 ++++++++++++++++++++++++++++++++ src/dlst/ft_dlstnew.c | 25 +++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 src/dlst/ft_dlstdelone.c create mode 100644 src/dlst/ft_dlstdestroy.c create mode 100644 src/dlst/ft_dlstnew.c (limited to 'src') diff --git a/src/dlst/ft_dlstdelone.c b/src/dlst/ft_dlstdelone.c new file mode 100644 index 0000000..7f826f5 --- /dev/null +++ b/src/dlst/ft_dlstdelone.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 15:28:47 by charles #+# #+# */ +/* Updated: 2020/04/03 15:40:32 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dlst.h" + +void ft_dlstdelone(t_ftdlst *dlst, t_ftdel_func del) +{ + if (dlst == NULL) + return ; + if (dlst->prev != NULL) + dlst->prev->next = dlst->next; + if (dlst->next != NULL) + dlst->next->prev = dlst->prev; + if (del != NULL) + del(dlst->data); + free(dlst); +} diff --git a/src/dlst/ft_dlstdestroy.c b/src/dlst/ft_dlstdestroy.c new file mode 100644 index 0000000..6ce0d98 --- /dev/null +++ b/src/dlst/ft_dlstdestroy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 15:22:51 by charles #+# #+# */ +/* Updated: 2020/04/03 15:44:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dlst.h" + +void ft_dlstdestroy(t_ftdlst *dlst, t_ftdel_func del) +{ + if (dlst == NULL) + return ; + if (dlst->prev != NULL) + { + dlst->prev->next = NULL; + ft_dlstdestroy(dlst->prev, del); + } + if (dlst->next != NULL) + { + dlst->next->prev = NULL; + ft_dlstdestroy(dlst->next, del); + } + if (del != NULL) + del(dlst->data); + free(dlst); +} diff --git a/src/dlst/ft_dlstnew.c b/src/dlst/ft_dlstnew.c new file mode 100644 index 0000000..6eb9346 --- /dev/null +++ b/src/dlst/ft_dlstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dlstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 15:21:38 by charles #+# #+# */ +/* Updated: 2020/04/03 15:22:45 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dlst.h" + +t_ftdlst *ft_dlstnew(void *data) +{ + t_ftdlst *dlst; + + if ((dlst = (t_ftdlst*)malloc(sizeof(t_ftdlst))) == NULL) + return (NULL); + dlst->prev = NULL; + dlst->next = NULL; + dlst->data = data; + return (dlst); +} -- cgit From 51b845a6a202b50966f50e166cfb11bcbdccbe33 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 4 Apr 2020 15:58:24 +0200 Subject: Added ft_strsjoin, ft_strsjoinf, ft_compar_str, ft_vecsort, ft_vecpush_safe --- src/algo/ft_compar_str.c | 18 ++++++++++++++++++ src/str/ft_strsep.c | 15 ++++++++++----- src/str/ft_strsjoin.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/str/ft_strsjoinf.c | 22 ++++++++++++++++++++++ src/vec/ft_vecpush_safe.c | 27 +++++++++++++++++++++++++++ src/vec/ft_vecsort.c | 18 ++++++++++++++++++ 6 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 src/algo/ft_compar_str.c create mode 100644 src/str/ft_strsjoin.c create mode 100644 src/str/ft_strsjoinf.c create mode 100644 src/vec/ft_vecpush_safe.c create mode 100644 src/vec/ft_vecsort.c (limited to 'src') diff --git a/src/algo/ft_compar_str.c b/src/algo/ft_compar_str.c new file mode 100644 index 0000000..0d3e6ff --- /dev/null +++ b/src/algo/ft_compar_str.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_compar_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 15:44:24 by charles #+# #+# */ +/* Updated: 2020/04/04 15:46:28 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_algo.h" + +int ft_compar_str(const void *s1_p, const void *s2_p) +{ + return (ft_strcmp(*(const char**)s1_p, *(const char**)s2_p)); +} diff --git a/src/str/ft_strsep.c b/src/str/ft_strsep.c index 2000706..50197fb 100644 --- a/src/str/ft_strsep.c +++ b/src/str/ft_strsep.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 04:44:11 by cacharle #+# #+# */ -/* Updated: 2020/02/10 04:51:15 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 12:48:24 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,13 +15,18 @@ char *ft_strsep(char **stringp, const char *delim) { char *tmp; + char *origin; - if (stringp == NULL || *stringp == NULL || delim == NULL) + if (*stringp == NULL) return (NULL); + origin = *stringp; tmp = ft_strpbrk(*stringp, delim); if (tmp == NULL) - return (NULL); + { + *stringp = NULL; + return (origin); + } *tmp = '\0'; - *stringp = tmp; - return (tmp); + *stringp = tmp + 1; + return (origin); } diff --git a/src/str/ft_strsjoin.c b/src/str/ft_strsjoin.c new file mode 100644 index 0000000..507903b --- /dev/null +++ b/src/str/ft_strsjoin.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 14:30:08 by charles #+# #+# */ +/* Updated: 2020/04/04 14:45:58 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +char *ft_strsjoin(char **strs, char *delim) +{ + int i; + size_t join_len; + size_t delim_len; + char *join; + + delim_len = ft_strlen(delim); + join_len = 0; + i = -1; + while (strs[++i] != NULL) + { + join_len += ft_strlen(strs[i]); + if (strs[i + 1] != NULL) + join_len += delim_len; + } + if ((join = (char*)malloc(sizeof(char) * (join_len + 1))) == NULL) + return (NULL); + join[0] = '\0'; + i = -1; + while (strs[++i] != NULL) + { + ft_strcat(join, strs[i]); + if (strs[i + 1] != NULL) + ft_strcat(join, delim); + } + return (join); +} diff --git a/src/str/ft_strsjoinf.c b/src/str/ft_strsjoinf.c new file mode 100644 index 0000000..f5077c1 --- /dev/null +++ b/src/str/ft_strsjoinf.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsjoinf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 14:27:33 by charles #+# #+# */ +/* Updated: 2020/04/04 14:29:49 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +char *ft_strsjoinf(char **strs, char *delim) +{ + char *ret; + + ret = ft_strsjoin(strs, delim); + ft_split_destroy(strs); + return (ret); +} diff --git a/src/vec/ft_vecpush_safe.c b/src/vec/ft_vecpush_safe.c new file mode 100644 index 0000000..8e5a8d4 --- /dev/null +++ b/src/vec/ft_vecpush_safe.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpush_safe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 13:29:48 by charles #+# #+# */ +/* Updated: 2020/04/04 14:24:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Wrapper around ft_vecpush which reject null element +** \param vec Pushed vector +** \param pushed Element pushed which can't be NULL +** \return NULL if pushed is NULL, whatever ft_vecpush returns otherwise +*/ + +t_ftvec *ft_vecpush_safe(t_ftvec *vec, void *pushed) +{ + if (pushed == NULL) + return (NULL); + return (ft_vecpush(vec, pushed)); +} diff --git a/src/vec/ft_vecsort.c b/src/vec/ft_vecsort.c new file mode 100644 index 0000000..99b1a5f --- /dev/null +++ b/src/vec/ft_vecsort.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 15:53:23 by charles #+# #+# */ +/* Updated: 2020/04/04 15:55:21 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +void ft_vecsort(t_ftvec *vec, t_ftcompar_func cmp) +{ + ft_qsort(vec->data, vec->size, sizeof(void*), cmp); +} -- cgit From 42316d8393d32bd88fb8e0cba6825185f78dacd0 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 4 Apr 2020 21:28:21 +0200 Subject: Added test and doc for dynamic string --- src/dstr/ft_dstrdestroy.c | 7 ++++++- src/dstr/ft_dstrgrow.c | 14 ++++++++++++-- src/dstr/ft_dstrinsert.c | 17 ++++++++++++----- src/dstr/ft_dstrnew.c | 9 ++++++++- src/dstr/ft_dstrunwrap.c | 8 +++++++- src/vec/ft_vecnew.c | 2 +- src/vec/ft_vecsort.c | 8 +++++++- 7 files changed, 53 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/dstr/ft_dstrdestroy.c b/src/dstr/ft_dstrdestroy.c index 6f1a819..c76b692 100644 --- a/src/dstr/ft_dstrdestroy.c +++ b/src/dstr/ft_dstrdestroy.c @@ -6,12 +6,17 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:58:46 by charles #+# #+# */ -/* Updated: 2020/04/03 13:59:25 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:51:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Destroy a dynamic string +** \param dstr Dynamic string to destroy +*/ + void ft_dstrdestroy(t_ftdstr *dstr) { if (dstr == NULL) diff --git a/src/dstr/ft_dstrgrow.c b/src/dstr/ft_dstrgrow.c index 9ad51ea..40cad86 100644 --- a/src/dstr/ft_dstrgrow.c +++ b/src/dstr/ft_dstrgrow.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 14:17:09 by charles #+# #+# */ -/* Updated: 2020/04/03 15:09:21 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:56:26 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,18 +14,28 @@ #define FT_DSTR_GROWTH_FACTOR 1.5 +/* +** \brief Grow the capacity of a dynamic string +** \param dstr Dynamic string to grow +** \param at_least Minimum capacity - 1 required +** \return Passed dynamic string or NULL on error +*/ + t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least) { size_t new_capacity; char *new_str; + if (at_least < dstr->capacity - 1) + return (dstr); new_capacity = dstr->capacity <= 1 ? 2 : dstr->capacity; - while (new_capacity < at_least) + while (at_least > new_capacity - 1) new_capacity *= FT_DSTR_GROWTH_FACTOR; if ((new_str = (char*)malloc(sizeof(char) * new_capacity)) == NULL) return (NULL); ft_memcpy(new_str, dstr->str, dstr->capacity); dstr->capacity = new_capacity; + free(dstr->str); dstr->str = new_str; return (dstr); } diff --git a/src/dstr/ft_dstrinsert.c b/src/dstr/ft_dstrinsert.c index 6a1486c..931f5d8 100644 --- a/src/dstr/ft_dstrinsert.c +++ b/src/dstr/ft_dstrinsert.c @@ -6,22 +6,29 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 14:00:44 by charles #+# #+# */ -/* Updated: 2020/04/03 15:09:52 by charles ### ########.fr */ +/* Updated: 2020/04/04 21:21:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Insert string in dynamic string +** \param dstr Dynamic string where the string will be inserted +** \param inserted Static string to insert +** \param i Index where it should be inserted +** \return Passed dynamic string or NULL on error +*/ + t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i) { size_t inserted_len; - if (i > dstr->capacity) + if (i > dstr->length) return (NULL); inserted_len = ft_strlen(inserted); - if (dstr->capacity - dstr->length - 1 < inserted_len) - if (ft_dstrgrow(dstr, dstr->capacity + inserted_len + 1) == NULL) - return (NULL); + if (ft_dstrgrow(dstr, dstr->capacity + inserted_len) == NULL) + return (NULL); ft_memmove(dstr->str + i + inserted_len, dstr->str + i, dstr->length - i + 1); diff --git a/src/dstr/ft_dstrnew.c b/src/dstr/ft_dstrnew.c index 189fc78..8ae4a64 100644 --- a/src/dstr/ft_dstrnew.c +++ b/src/dstr/ft_dstrnew.c @@ -6,12 +6,19 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:54:52 by charles #+# #+# */ -/* Updated: 2020/04/03 13:58:17 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:50:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Create a new dynamic string +** \param from Static string to create the dynamic one from +** (will be duplicated) +** \return Created dynamic string or NULL on malloc error +*/ + t_ftdstr *ft_dstrnew(char *from) { t_ftdstr *dstr; diff --git a/src/dstr/ft_dstrunwrap.c b/src/dstr/ft_dstrunwrap.c index ba62a23..5b29b5b 100644 --- a/src/dstr/ft_dstrunwrap.c +++ b/src/dstr/ft_dstrunwrap.c @@ -6,12 +6,18 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:59:35 by charles #+# #+# */ -/* Updated: 2020/04/03 14:00:36 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:58:41 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_dstr.h" +/* +** \brief Destroy dynamic string but keep the underlying static string +** \param dstr Dynamic string to unwrap +** \return Underlying string of the dynamic one +*/ + char *ft_dstrunwrap(t_ftdstr *dstr) { char *tmp; diff --git a/src/vec/ft_vecnew.c b/src/vec/ft_vecnew.c index 8a8736a..0def9c9 100644 --- a/src/vec/ft_vecnew.c +++ b/src/vec/ft_vecnew.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:03:49 by charles #+# #+# */ -/* Updated: 2020/04/01 20:00:00 by charles ### ########.fr */ +/* Updated: 2020/04/04 21:24:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/ft_vecsort.c b/src/vec/ft_vecsort.c index 99b1a5f..8aa5c2a 100644 --- a/src/vec/ft_vecsort.c +++ b/src/vec/ft_vecsort.c @@ -6,12 +6,18 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 15:53:23 by charles #+# #+# */ -/* Updated: 2020/04/04 15:55:21 by charles ### ########.fr */ +/* Updated: 2020/04/04 19:30:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_vec.h" +/* +** \brief Wrapper around ft_qsort +** \param vec Vector to sort +** \param cmp Function to compare each vector element +*/ + void ft_vecsort(t_ftvec *vec, t_ftcompar_func cmp) { ft_qsort(vec->data, vec->size, sizeof(void*), cmp); -- cgit From 65cf641e9533b190db870d0cc46f2f852239ebf6 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 4 Apr 2020 23:36:19 +0200 Subject: Added test for ft_strsjoin, ft_strsjoinf, ft_vecpush_safe, Added doc for algo functions, tested functions --- src/algo/ft_compar_int.c | 9 ++++++++- src/algo/ft_compar_str.c | 9 ++++++++- src/algo/ft_is_set.c | 10 +++++++++- src/algo/ft_mergesort.c | 24 +++++++++++++++++++++++- src/algo/ft_qsort.c | 35 ++++++++++++++++++++++++++++++++++- src/algo/ft_reverse.c | 9 ++++++++- src/ht/ft_htnew.c | 3 +-- src/str/ft_atoi.c | 7 +++++-- src/str/ft_strsjoin.c | 12 ++++++++++-- src/str/ft_strsjoinf.c | 9 ++++++++- 10 files changed, 114 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/algo/ft_compar_int.c b/src/algo/ft_compar_int.c index 848dc71..ab057bf 100644 --- a/src/algo/ft_compar_int.c +++ b/src/algo/ft_compar_int.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 08:24:43 by cacharle #+# #+# */ -/* Updated: 2020/01/19 08:27:38 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:30:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/* +** \brief Comparison function for 2 ints +** \param a Pointer to first int +** \param b Pointer to first int +** \return The difference between a and b +*/ + int ft_compar_int(const void *a, const void *b) { return (*(int*)a - *(int*)b); diff --git a/src/algo/ft_compar_str.c b/src/algo/ft_compar_str.c index 0d3e6ff..6749ab0 100644 --- a/src/algo/ft_compar_str.c +++ b/src/algo/ft_compar_str.c @@ -6,12 +6,19 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 15:44:24 by charles #+# #+# */ -/* Updated: 2020/04/04 15:46:28 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:31:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief String comparison function +** \param s1_p Pointer to first string +** \param s2_p Pointer to second string +** \return `strcmp` of both strings +*/ + int ft_compar_str(const void *s1_p, const void *s2_p) { return (ft_strcmp(*(const char**)s1_p, *(const char**)s2_p)); diff --git a/src/algo/ft_is_set.c b/src/algo/ft_is_set.c index 79a5e7c..fd26a88 100644 --- a/src/algo/ft_is_set.c +++ b/src/algo/ft_is_set.c @@ -6,12 +6,20 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */ -/* Updated: 2020/04/03 07:05:19 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:33:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Test whether array only contains unique elements +** \param base Array to test +** \param nel Number of element in the array +** \param width Size of an element +** \param compar Comparison function to test if 2 elements are equal +*/ + bool ft_is_set(void *base, size_t nel, size_t width, t_ftcompar_func compar) { diff --git a/src/algo/ft_mergesort.c b/src/algo/ft_mergesort.c index 25b4255..8556145 100644 --- a/src/algo/ft_mergesort.c +++ b/src/algo/ft_mergesort.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 02:26:41 by cacharle #+# #+# */ -/* Updated: 2020/02/13 23:14:21 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:40:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Helper function to return in case of error +** \param left Left subarray +** \param right Right subarray +** \return -1 to indicate error +*/ + static int st_mergesort_error(void *left, void *right) { free(left); @@ -19,6 +26,13 @@ static int st_mergesort_error(void *left, void *right) return (-1); } +/* +** \brief Merge 2 sorted arrays +** \param arrays Struct containing the arrays (base, left, right) +** \param nel Number of element in base +** \param compar Comparison function +*/ + static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel, size_t width, int (*compar)(const void *, const void *)) { @@ -49,6 +63,14 @@ static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel, arrays->right + ri++ * width, width); } +/* +** \brief Sort an array using the merge sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \return 0 on success, -1 on error +*/ + int ft_mergesort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)) { diff --git a/src/algo/ft_qsort.c b/src/algo/ft_qsort.c index 9bcfcdf..0c1c0f7 100644 --- a/src/algo/ft_qsort.c +++ b/src/algo/ft_qsort.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:25:51 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:55:14 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:10:29 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Helper to create a new range +** \param lo Lower bound +** \param hi Higher bound +** \return Range struct (not allocated) +*/ + static t_ftrange ft_range_new(int lo, int hi) { t_ftrange range; @@ -21,6 +28,16 @@ static t_ftrange ft_range_new(int lo, int hi) return (range); } +/* +** \brief Array partitionning, +** takes a pivot and place every element lower than it before +** and every element greater after +** \param base Partitioned array +** \param range Lower and upper bound +** \param width Single element size +** \param compar Comparison function +*/ + static int ft_qsort_partition(void *base, t_ftrange range, size_t width, t_ftcompar_func compar) { @@ -43,6 +60,14 @@ static int ft_qsort_partition(void *base, t_ftrange range, return (p); } +/* +** \brief Qsort recursion function +** \param base Array to sort +** \param range Lower and upper bound which define the area to sort +** \param width Single element size +** \param compar Comparision function +*/ + static void ft_qsort_rec(void *base, t_ftrange range, size_t width, t_ftcompar_func compar) { @@ -55,6 +80,14 @@ static void ft_qsort_rec(void *base, t_ftrange range, ft_qsort_rec(base, ft_range_new(pivot + 1, range.hi), width, compar); } +/* +** \brief Sort an array using the Quick sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \param compar Comparision function +*/ + void ft_qsort(void *base, size_t nel, size_t width, t_ftcompar_func compar) { diff --git a/src/algo/ft_reverse.c b/src/algo/ft_reverse.c index 0bc447f..c617338 100644 --- a/src/algo/ft_reverse.c +++ b/src/algo/ft_reverse.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 05:07:13 by cacharle #+# #+# */ -/* Updated: 2020/02/10 05:19:22 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:36:23 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Reverse an array +** \param base Array to reverse +** \param nel Number of element in the array +** \param width Size of each elements +*/ + void ft_reverse(void *base, size_t nel, size_t width) { size_t i; diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index b6d1cc3..585c211 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,12 +6,11 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/04/03 06:53:51 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:34:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_ht.h" -#include "libft_mem.h" /* ** \brief Create a new hash table. diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c index d6fa5bb..b8f979d 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -6,14 +6,17 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */ -/* Updated: 2020/01/15 10:56:06 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:34:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" /* -** Convert a string to an int +** \brief Extract first int in a string +** (takes as much digits has possible) +** \param str String to convert +** \return Extracted int */ int ft_atoi(const char *str) diff --git a/src/str/ft_strsjoin.c b/src/str/ft_strsjoin.c index 507903b..0923bde 100644 --- a/src/str/ft_strsjoin.c +++ b/src/str/ft_strsjoin.c @@ -6,12 +6,20 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 14:30:08 by charles #+# #+# */ -/* Updated: 2020/04/04 14:45:58 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:34:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_str.h" +/* +** \brief Join null-terminated array of strings +** \param strs Array of strings +** \param delim String iterspersed between strings +** \return Joined string or NULL on error +** \note Empty strings are ignored +*/ + char *ft_strsjoin(char **strs, char *delim) { int i; @@ -35,7 +43,7 @@ char *ft_strsjoin(char **strs, char *delim) while (strs[++i] != NULL) { ft_strcat(join, strs[i]); - if (strs[i + 1] != NULL) + if (*strs[i] != '\0' && strs[i + 1] != NULL) ft_strcat(join, delim); } return (join); diff --git a/src/str/ft_strsjoinf.c b/src/str/ft_strsjoinf.c index f5077c1..36a2892 100644 --- a/src/str/ft_strsjoinf.c +++ b/src/str/ft_strsjoinf.c @@ -6,12 +6,19 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 14:27:33 by charles #+# #+# */ -/* Updated: 2020/04/04 14:29:49 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:24:24 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_str.h" +/* +** \brief Join null-terminated array of strings and free the array +** \param strs Array of strings +** \param delim String which will be put between each string +** \return Joined string or NULL on error +*/ + char *ft_strsjoinf(char **strs, char *delim) { char *ret; -- cgit From 944a236443bacf531085b346d06907b24e3739b1 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 5 Apr 2020 01:11:50 +0200 Subject: Added ft_dstrerase, ft_dstrsubstitute --- src/dstr/ft_dstrerase.c | 35 +++++++++++++++++++++++++++++++++++ src/dstr/ft_dstrsubstitute.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/dstr/ft_dstrerase.c create mode 100644 src/dstr/ft_dstrsubstitute.c (limited to 'src') diff --git a/src/dstr/ft_dstrerase.c b/src/dstr/ft_dstrerase.c new file mode 100644 index 0000000..3d4732b --- /dev/null +++ b/src/dstr/ft_dstrerase.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrerase.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 00:39:12 by charles #+# #+# */ +/* Updated: 2020/04/05 01:11:07 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Erase part of a dynamic string +** \param dstr Dynamic string to erase from +** \param start Erase starting index +** \param len Number of character to erase +*/ + +void ft_dstrerase(t_ftdstr *dstr, size_t start, size_t len) +{ + if (start > dstr->length) + return ; + if (start + len > dstr->length) + len = dstr->length - start; + ft_memmove( + dstr->str + start, + dstr->str + start + len, + dstr->length - start - len + ); + dstr->length -= len; + dstr->str[dstr->length] = '\0'; +} diff --git a/src/dstr/ft_dstrsubstitute.c b/src/dstr/ft_dstrsubstitute.c new file mode 100644 index 0000000..84adc29 --- /dev/null +++ b/src/dstr/ft_dstrsubstitute.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrsubstitute.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 00:22:55 by charles #+# #+# */ +/* Updated: 2020/04/05 00:38:40 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Substitute part of a dynamic string for an other string +** \param dstr Dynamic string to substitute in +** \param replacement Replacement text +** \param start Substitution start index +** \param len Substitution length +*/ + +t_ftdstr *ft_dstrsubstitute( + t_ftdstr *dstr, + char *replacement, + size_t start, + size_t len +) +{ + ft_dstrerase(dstr, start, len); + return (ft_dstrinsert(dstr, replacement, start)); +} -- cgit From 3a2d19df9e509d0b015c786eb02f8315ff0ad91c Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 5 Apr 2020 14:05:43 +0200 Subject: Renamed ft_substr to ft_strsub, Added ft_strsubf, ft_strcat3 --- src/str/ft_strcat3.c | 26 ++++++++++++++++++++++++++ src/str/ft_strsub.c | 39 +++++++++++++++++++++++++++++++++++++++ src/str/ft_strsubf.c | 30 ++++++++++++++++++++++++++++++ src/str/ft_strtrim.c | 4 ++-- src/str/ft_substr.c | 26 -------------------------- 5 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 src/str/ft_strcat3.c create mode 100644 src/str/ft_strsub.c create mode 100644 src/str/ft_strsubf.c delete mode 100644 src/str/ft_substr.c (limited to 'src') diff --git a/src/str/ft_strcat3.c b/src/str/ft_strcat3.c new file mode 100644 index 0000000..1f7c5df --- /dev/null +++ b/src/str/ft_strcat3.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcat3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 12:53:05 by charles #+# #+# */ +/* Updated: 2020/04/05 12:55:49 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Wrapper around ft_strcat to concatenate 3 strings +** \param dest Destination of the concatenation +** \param src1 First concatenation +** \param src2 Second concatenation +** \return Pointer to destination +*/ + +char *ft_strcat3(char *dest, const char *src1, const char *src2) +{ + return (ft_strcat(ft_strcat(dest, src1), src2)); +} diff --git a/src/str/ft_strsub.c b/src/str/ft_strsub.c new file mode 100644 index 0000000..77bffb2 --- /dev/null +++ b/src/str/ft_strsub.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsub.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ +/* Updated: 2020/04/05 13:47:55 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Extract a substring from a string +** \param s String to extract from +** \param start Starting index of the substring +** \param len Substring length +** \return The created substring or NULL on error +*/ + +char *ft_strsub(char const *s, size_t start, size_t len) +{ + char *sub; + size_t s_len; + + if (s == NULL) + return (NULL); + s_len = ft_strlen(s); + if (start > s_len) + return (NULL); + if (start + len > s_len) + len = s_len - start; + if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + sub[len] = '\0'; + return (ft_strncpy(sub, s + start, len)); +} diff --git a/src/str/ft_strsubf.c b/src/str/ft_strsubf.c new file mode 100644 index 0000000..dc49ba5 --- /dev/null +++ b/src/str/ft_strsubf.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsubf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/05 13:48:13 by charles #+# #+# */ +/* Updated: 2020/04/05 13:51:47 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Wrapper around ft_substr which free the original string +** \param s String to extract from (will be free) +** \param start Starting index of the substring +** \param len Substring length +** \return The created substring or NULL on error +*/ + +char *ft_strsubf(char const *s, size_t start, size_t len) +{ + char *ret; + + ret = ft_strsub(s, start, len); + free((void*)s); + return (ret); +} diff --git a/src/str/ft_strtrim.c b/src/str/ft_strtrim.c index aa48826..fa9b192 100644 --- a/src/str/ft_strtrim.c +++ b/src/str/ft_strtrim.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:24:16 by cacharle #+# #+# */ -/* Updated: 2019/11/20 03:52:58 by cacharle ### ########.fr */ +/* Updated: 2020/04/05 13:50:43 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,5 +27,5 @@ char *ft_strtrim(char const *s1, char const *set) while (s1[start + len - 1] && ft_strchr(set, s1[start + len - 1]) != NULL) len--; - return (ft_substr(s1, start, len)); + return (ft_strsub(s1, start, len)); } diff --git a/src/str/ft_substr.c b/src/str/ft_substr.c deleted file mode 100644 index ad9c706..0000000 --- a/src/str/ft_substr.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_substr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:44:42 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -char *ft_substr(char const *s, unsigned int start, size_t len) -{ - char *sub; - - if (s == NULL) - return (NULL); - if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL) - return (NULL); - if (start > ft_strlen(s)) - return (sub); - return (ft_strncpy(sub, s + start, len)); -} -- cgit From a3c962abbcdae671b886c4c76ddb9bb8ac27c958 Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 26 Apr 2020 21:04:43 +0200 Subject: Added ft_btsorted_insert, ft_btsorted_search, Red-black tree struct (not tested) --- src/bt/ft_btdestroy.c | 13 +++++-- src/bt/ft_btnew.c | 16 ++++++--- src/bt/ft_btsorted_insert.c | 38 ++++++++++++++++++++ src/bt/ft_btsorted_search.c | 35 +++++++++++++++++++ src/rbt/ft_rbtinsert.c | 83 ++++++++++++++++++++++++++++++++++++++++++++ src/rbt/ft_rbtnew.c | 35 +++++++++++++++++++ src/rbt/ft_rbtrotate_left.c | 38 ++++++++++++++++++++ src/rbt/ft_rbtrotate_right.c | 38 ++++++++++++++++++++ 8 files changed, 288 insertions(+), 8 deletions(-) create mode 100644 src/bt/ft_btsorted_insert.c create mode 100644 src/bt/ft_btsorted_search.c create mode 100644 src/rbt/ft_rbtinsert.c create mode 100644 src/rbt/ft_rbtnew.c create mode 100644 src/rbt/ft_rbtrotate_left.c create mode 100644 src/rbt/ft_rbtrotate_right.c (limited to 'src') diff --git a/src/bt/ft_btdestroy.c b/src/bt/ft_btdestroy.c index c802db0..d8746ef 100644 --- a/src/bt/ft_btdestroy.c +++ b/src/bt/ft_btdestroy.c @@ -6,18 +6,25 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/07 21:30:53 by cacharle #+# #+# */ -/* Updated: 2020/02/07 21:35:19 by cacharle ### ########.fr */ +/* Updated: 2020/04/26 19:47:45 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_bt.h" -void ft_btdestroy(t_ftbtree *tree, void (*del)(void *data)) +/* +** \brief Destroy a binary tree +** \param tree Binary tree to destroy +** \param del Delete function applied to each node data +*/ + +void ft_btdestroy(t_ftbt *tree, void (*del)(void *data)) { if (tree == NULL) return ; ft_btdestroy(tree->left, del); ft_btdestroy(tree->right, del); - (*del)(tree->data); + if (del != NULL) + del(tree->data); free(tree); } diff --git a/src/bt/ft_btnew.c b/src/bt/ft_btnew.c index 973e1a4..09cfa12 100644 --- a/src/bt/ft_btnew.c +++ b/src/bt/ft_btnew.c @@ -6,20 +6,26 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/07 21:33:16 by cacharle #+# #+# */ -/* Updated: 2020/02/07 21:34:35 by cacharle ### ########.fr */ +/* Updated: 2020/04/26 19:46:57 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_bt.h" -t_ftbtree *ft_btnew(void *data) +/* +** \brief Create a new binary tree +** \param data Node's data +** \return Allocated node with left and right set to NULL, NULL on error +*/ + +t_ftbt *ft_btnew(void *data) { - t_ftbtree *tree; + t_ftbt *tree; - if ((tree = (t_ftbtree*)malloc(sizeof(t_ftbtree))) == NULL) + if ((tree = (t_ftbt*)malloc(sizeof(t_ftbt))) == NULL) return (NULL); - tree->data = data; tree->left = NULL; tree->right = NULL; + tree->data = data; return (tree); } diff --git a/src/bt/ft_btsorted_insert.c b/src/bt/ft_btsorted_insert.c new file mode 100644 index 0000000..12c0cde --- /dev/null +++ b/src/bt/ft_btsorted_insert.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_btsorted_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/26 19:10:36 by charles #+# #+# */ +/* Updated: 2020/04/26 19:19:24 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_bt.h" + +/* +** \brief Insert a data in a binary search tree so that it stays sorted +** \param tree Tree where data is inserted +** \param data Data to insert +** \param cmp Comparison function +** \return The new root or NULL if a new node couldn't be allocated +*/ + +t_ftbt *ft_btsorted_insert(t_ftbt *tree, void *data, int (*cmp)(void*, void*)) +{ + if (tree == NULL) + return (ft_btnew(data)); + if (cmp(data, tree->data) < 0) + { + if (ft_btsorted_insert(tree->left, data, cmp) == NULL) + return (NULL); + } + else + { + if (ft_btsorted_insert(tree->right, data, cmp) == NULL) + return (NULL); + } + return (tree); +} diff --git a/src/bt/ft_btsorted_search.c b/src/bt/ft_btsorted_search.c new file mode 100644 index 0000000..00629d0 --- /dev/null +++ b/src/bt/ft_btsorted_search.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_btsorted_search.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/26 19:31:42 by charles #+# #+# */ +/* Updated: 2020/04/26 19:35:53 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_bt.h" + +/* +** \brief Search a element in a sorted binary tree +** \param tree Searched tree +** \param ref First argument of comparison function +** \param cmp Comparison function +** \return Node data if found, NULL otherwise +*/ + +void *ft_btsorted_search(t_ftbt *tree, void *ref, int (*cmp)(void*, void*)) +{ + int res; + + if (tree == NULL) + return (NULL); + if ((res = cmp(ref, tree->data)) == 0) + return (tree->data); + if (res < 0) + return (ft_btsorted_search(tree->left, ref, cmp)); + else + return (ft_btsorted_search(tree->right, ref, cmp)); +} diff --git a/src/rbt/ft_rbtinsert.c b/src/rbt/ft_rbtinsert.c new file mode 100644 index 0000000..ce86777 --- /dev/null +++ b/src/rbt/ft_rbtinsert.c @@ -0,0 +1,83 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_rbtinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/26 19:10:36 by charles #+# #+# */ +/* Updated: 2020/04/26 21:02:52 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_rbt.h" + +static void st_insert_node( + t_ftrbt **root, + t_ftrbt *node, + int (*cmp)(void*, void*)) +{ + if (*root == NULL) + { + *root = node; + return ; + } + if (cmp(node->data, (*root)->data) < 0) + st_insert_node(&(*root)->left, node->data, cmp); + else + st_insert_node(&(*root)->left, node->data, cmp); +} + +static void st_repare_tree(t_ftrbt *node) +{ + (void)node; + /* t_ftrbt *parent; */ + /* */ + /* parent = node->parent; */ + /* if (parent == NULL) */ + /* node->color = FTRBT_COLOR_BLACK; */ + /* else if (parent->color == FTRBT_COLOR_BLACK) */ + /* return ; */ + /* else if (parent->color == FTRBT_COLOR_RED && uncle(node)->color == FTRBT_COLOR_RED) */ + /* { */ + /* parent->color == FTRBT_COLOR_BLACK; */ + /* uncle->color = FTRBT_COLOR_BLACK; */ + /* parent->parent->color = FTRBT_COLOR_RED; */ + /* st_repare_tree(parent->parent); */ + /* } */ + /* else if (parent->color == FTRBT_COLOR_RED && uncle(node)->color == FTRBT_COLOR_BLACK) */ + /* { */ + /* if (node == parent->right && parent == parent->parent->left) */ + /* { */ + /* ft_rbtrotate_left(parent); */ + /* node = node->left; */ + /* } */ + /* else if (node == parent->left && parent == parent->parent->right) */ + /* { */ + /* ft_rbtrotate_right(parent); */ + /* node = node->right; */ + /* } */ + /* if (node == parent->left) */ + /* ft_rbtrotate_right(parent); */ + /* else */ + /* ft_rbtrotate_left(parent); */ + /* parent->color = FTRBT_COLOR_BLACK; */ + /* parent->parent->color = FTRBT_COLOR_RED; */ + /* } */ +} + +t_ftrbt *ft_rbtinsert( + t_ftrbt *tree, + void *data, + int (*cmp)(void*, void*)) +{ + t_ftrbt *node; + + if ((node = ft_rbtnew(data, FTRBT_COLOR_RED)) == NULL) + return (NULL); + st_insert_node(&tree, node, cmp); + st_repare_tree(node); + while (node->parent != NULL) + node = node->parent; + return node; +} diff --git a/src/rbt/ft_rbtnew.c b/src/rbt/ft_rbtnew.c new file mode 100644 index 0000000..bda7d55 --- /dev/null +++ b/src/rbt/ft_rbtnew.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_rbtnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/26 20:22:07 by charles #+# #+# */ +/* Updated: 2020/04/26 20:24:44 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_rbt.h" + +/* +** \brief Create a new red-black tree +** \param data Node's data +** \param color Node's color +** \return The created node with left, right and parent pointer to NULL +** or NULL on error +*/ + +t_ftrbt *ft_rbtnew(void *data, enum e_ftrbt_color color) +{ + t_ftrbt *tree; + + if ((tree = (t_ftrbt*)malloc(sizeof(t_ftrbt))) == NULL) + return (NULL); + tree->left = NULL; + tree->right = NULL; + tree->data = data; + tree->parent = NULL; + tree->color = color; + return (tree); +} diff --git a/src/rbt/ft_rbtrotate_left.c b/src/rbt/ft_rbtrotate_left.c new file mode 100644 index 0000000..7cff65d --- /dev/null +++ b/src/rbt/ft_rbtrotate_left.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_rbtrotate_left.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/26 18:03:35 by charles #+# #+# */ +/* Updated: 2020/04/26 20:27:44 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_rbt.h" + +/* +** \brief Rotate a red-black tree to the left +** \param tree Pointer to Pointer to a red-black tree +** +** 5 10 +** / \ / \ +** 4 10 --> 5 11 +** / \ / \ +** 6 11 4 6 +*/ + +void ft_rbtrotate_left(t_ftrbt **tree) +{ + t_ftrbt *new_root; + t_ftrbt *tmp; + + if (tree == NULL || *tree == NULL || (*tree)->right == NULL) + return ; + new_root = (*tree)->right; + tmp = new_root->left; + new_root->left = *tree; + (*tree)->right = tmp; + *tree = new_root; +} diff --git a/src/rbt/ft_rbtrotate_right.c b/src/rbt/ft_rbtrotate_right.c new file mode 100644 index 0000000..46d72e2 --- /dev/null +++ b/src/rbt/ft_rbtrotate_right.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_rbtrotate_right.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/26 18:10:57 by charles #+# #+# */ +/* Updated: 2020/04/26 20:27:53 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_rbt.h" + +/* +** \brief Rotate a red-black tree to the right +** \param tree Pointer to Pointer to a red-black tree +** +** 10 5 +** / \ / \ +** 5 11 --> 4 10 +** / \ / \ +** 4 6 6 11 +*/ + +void ft_rbtrotate_right(t_ftrbt **tree) +{ + t_ftrbt *new_root; + t_ftrbt *tmp; + + if (tree == NULL || *tree == NULL || (*tree)->left == NULL) + return ; + new_root = (*tree)->left; + tmp = new_root->right; + new_root->right = *tree; + (*tree)->left = tmp; + *tree = new_root; +} -- cgit From b5124347359833fcde33452978c62133879c6c9e Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 8 May 2020 13:40:45 +0200 Subject: Fixed bug in ft_split --- src/str/ft_split.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/str/ft_split.c b/src/str/ft_split.c index 6fb5964..0cb08e4 100644 --- a/src/str/ft_split.c +++ b/src/str/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ -/* Updated: 2019/11/20 04:08:27 by cacharle ### ########.fr */ +/* Updated: 2020/05/08 13:39:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,16 +33,6 @@ static size_t count_segment(char const *s, char c) return (counter); } -static void *destroy_strs(char **strs) -{ - if (strs == NULL) - return (NULL); - while (*strs != NULL) - free(*strs++); - free(strs); - return (NULL); -} - char **ft_split(char const *s, char c) { char **strs; @@ -65,7 +55,7 @@ char **ft_split(char const *s, char c) while (s[j + i] && s[j + i] != c) i++; if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL) - return (destroy_strs(strs)); + return (ft_split_destroy(strs)); j += i - 1; } strs[tab_counter] = NULL; -- cgit From d3fb362c2e0b83cc9754a05ae5bc4a68a5f9269d Mon Sep 17 00:00:00 2001 From: Charles Date: Sun, 10 May 2020 22:01:15 +0200 Subject: Added ft_strtof, ft_atof, ft_vectobuf32, ft_split_len (not tested) --- src/dstr/ft_dstrnew.c | 6 ++- src/io/ft_getline.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ src/io/ft_next_line.c | 113 ------------------------------------------------ src/str/ft_atof.c | 18 ++++++++ src/str/ft_strtof.c | 44 +++++++++++++++++++ src/util/ft_split_len.c | 23 ++++++++++ src/vec/ft_vectobuf32.c | 29 +++++++++++++ 7 files changed, 232 insertions(+), 114 deletions(-) create mode 100644 src/io/ft_getline.c delete mode 100644 src/io/ft_next_line.c create mode 100644 src/str/ft_atof.c create mode 100644 src/str/ft_strtof.c create mode 100644 src/util/ft_split_len.c create mode 100644 src/vec/ft_vectobuf32.c (limited to 'src') diff --git a/src/dstr/ft_dstrnew.c b/src/dstr/ft_dstrnew.c index 8ae4a64..c280dd1 100644 --- a/src/dstr/ft_dstrnew.c +++ b/src/dstr/ft_dstrnew.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:54:52 by charles #+# #+# */ -/* Updated: 2020/04/04 19:50:38 by charles ### ########.fr */ +/* Updated: 2020/05/09 12:57:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,9 +23,13 @@ t_ftdstr *ft_dstrnew(char *from) { t_ftdstr *dstr; + dstr = NULL; if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL || (dstr->str = ft_strdup(from)) == NULL) + { + free(dstr); return (NULL); + } dstr->length = ft_strlen(from); dstr->capacity = dstr->length + 1; return (dstr); diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c new file mode 100644 index 0000000..4cf3962 --- /dev/null +++ b/src/io/ft_getline.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ +/* Updated: 2020/05/09 11:19:12 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int st_find_newline(char *str) +{ + int i; + + i = -1; + while (str[++i]) + if (str[i] == '\n') + return (i); + return (-1); +} + +static int st_free_return(char **ptr, char **ptr2, int ret) +{ + if (ptr != NULL) + { + free(*ptr); + *ptr = NULL; + } + if (ptr2 != NULL) + { + free(*ptr2); + *ptr2 = NULL; + } + return (ret); +} + +static int st_read_line(int fd, char **line, char *rest) +{ + int ret; + int split_at; + char *buf; + + if ((buf = malloc(sizeof(char) * (FT_GETLINE_BUFFER_SIZE + 1))) == NULL) + return (st_free_return(line, NULL, FT_ERROR)); + while ((ret = read(fd, buf, FT_GETLINE_BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((split_at = st_find_newline(buf)) != -1) + { + ft_strcpy(rest, buf + split_at + 1); + buf[split_at] = '\0'; + if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) + return (st_free_return(&buf, NULL, FT_ERROR)); + return (st_free_return(&buf, NULL, FT_LINE)); + } + if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) + return (st_free_return(&buf, NULL, FT_ERROR)); + } + if (ret == -1) + return (st_free_return(&buf, line, FT_ERROR)); + return (st_free_return(&buf, NULL, ret)); +} + +/* +** if has rest: +** if rest has newline: +** push rest until newline in line, shift rest +** return LINE_READ +** else: +** push rest in line +** +** while can read fd in buf +** if buf has newline: +** push buf until newline in line +** push buf after newline in rest +** return LINE_READ +** push buf in line +** +** return FTNL_EOF +*/ + +int ft_getline(int fd, char **line) +{ + int split_at; + static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; + + if (fd < 0 || fd > OPEN_MAX || line == NULL || FT_GETLINE_BUFFER_SIZE <= 0) + return (FT_ERROR); + if ((*line = ft_strdup("")) == NULL) + return (FT_ERROR); + if (rest[fd][0] == '\0') + return (st_read_line(fd, line, rest[fd])); + if ((split_at = st_find_newline(rest[fd])) != -1) + { + free(*line); + if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) + return (FT_ERROR); + ft_strncpy(*line, rest[fd], split_at); + (*line)[split_at] = '\0'; + ft_strcpy(rest[fd], rest[fd] + split_at + 1); + return (FT_LINE); + } + free(*line); + if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) + return (FT_ERROR); + ft_strcpy(*line, rest[fd]); + rest[fd][0] = '\0'; + return (st_read_line(fd, line, rest[fd])); +} diff --git a/src/io/ft_next_line.c b/src/io/ft_next_line.c deleted file mode 100644 index 74afa71..0000000 --- a/src/io/ft_next_line.c +++ /dev/null @@ -1,113 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:11:35 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -static int st_find_newline(char *str) -{ - int i; - - i = -1; - while (str[++i]) - if (str[i] == '\n') - return (i); - return (-1); -} - -static int st_free_return(char **ptr, char **ptr2, int ret) -{ - if (ptr != NULL) - { - free(*ptr); - *ptr = NULL; - } - if (ptr2 != NULL) - { - free(*ptr2); - *ptr2 = NULL; - } - return (ret); -} - -static int st_read_line(int fd, char **line, char *rest) -{ - int ret; - int split_at; - char *buf; - - if ((buf = malloc(sizeof(char) * (FTNL_BUFFER_SIZE + 1))) == NULL) - return (st_free_return(line, NULL, FTNL_STATUS_ERROR)); - while ((ret = read(fd, buf, FTNL_BUFFER_SIZE)) > 0) - { - buf[ret] = '\0'; - if ((split_at = st_find_newline(buf)) != -1) - { - ft_strcpy(rest, buf + split_at + 1); - buf[split_at] = '\0'; - if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FTNL_STATUS_ERROR)); - return (st_free_return(&buf, NULL, FTNL_STATUS_LINE)); - } - if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FTNL_STATUS_ERROR)); - } - if (ret == -1) - return (st_free_return(&buf, line, FTNL_STATUS_ERROR)); - return (st_free_return(&buf, NULL, ret)); -} - -/* -** if has rest: -** if rest has newline: -** push rest until newline in line, shift rest -** return LINE_READ -** else: -** push rest in line -** -** while can read fd in buf -** if buf has newline: -** push buf until newline in line -** push buf after newline in rest -** return LINE_READ -** push buf in line -** -** return FTNL_EOF -*/ - -int ft_next_line(int fd, char **line) -{ - int split_at; - static char rest[OPEN_MAX][FTNL_BUFFER_SIZE + 1] = {{0}}; - - if (fd < 0 || fd > OPEN_MAX || line == NULL || FTNL_BUFFER_SIZE <= 0) - return (FTNL_STATUS_ERROR); - if ((*line = ft_strdup("")) == NULL) - return (FTNL_STATUS_ERROR); - if (rest[fd][0] == '\0') - return (st_read_line(fd, line, rest[fd])); - if ((split_at = st_find_newline(rest[fd])) != -1) - { - free(*line); - if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) - return (FTNL_STATUS_ERROR); - ft_strncpy(*line, rest[fd], split_at); - (*line)[split_at] = '\0'; - ft_strcpy(rest[fd], rest[fd] + split_at + 1); - return (FTNL_STATUS_LINE); - } - free(*line); - if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) - return (FTNL_STATUS_ERROR); - ft_strcpy(*line, rest[fd]); - rest[fd][0] = '\0'; - return (st_read_line(fd, line, rest[fd])); -} diff --git a/src/str/ft_atof.c b/src/str/ft_atof.c new file mode 100644 index 0000000..74d5a42 --- /dev/null +++ b/src/str/ft_atof.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 21:03:50 by charles #+# #+# */ +/* Updated: 2020/05/10 21:04:54 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +float ft_atof(const char *nptr) +{ + return (ft_strtof(nptr, NULL)); +} diff --git a/src/str/ft_strtof.c b/src/str/ft_strtof.c new file mode 100644 index 0000000..f147394 --- /dev/null +++ b/src/str/ft_strtof.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtof.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/09 12:09:13 by charles #+# #+# */ +/* Updated: 2020/05/09 12:23:03 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Extract a float from a string +** \param nptr String to extract from +** \param endptr If not NULL pointer to last character +** of the extraction is placed in *endptr +** \return Extracted float value +** \note This function doesn't try mimic the original perfectly +** (no hexadecimal, exponent, NaN, inf handling) +*/ + +float ft_strtof(const char *nptr, char **endptr) +{ + float n; + bool is_neg; + const char *tmp; + + while (ft_isspace(*nptr)) + nptr++; + is_neg = *nptr == '-'; + if (*nptr == '-' || *nptr == '+') + nptr++; + n = (float)ft_strtol(nptr, (char**)&nptr, 10); + if (*nptr == '.') + nptr++; + tmp = nptr; + n += (float)ft_strtol(nptr, (char**)&nptr, 10) / (10 * (nptr - tmp)); + if (endptr != NULL) + *endptr = (char*)nptr; + return (is_neg ? -n : n); +} diff --git a/src/util/ft_split_len.c b/src/util/ft_split_len.c new file mode 100644 index 0000000..090a0c9 --- /dev/null +++ b/src/util/ft_split_len.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 20:58:46 by charles #+# #+# */ +/* Updated: 2020/05/10 21:02:08 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_util.h" + +size_t ft_split_len(char **split) +{ + size_t count; + + count = 0; + while (split[count] != NULL) + count++; + return (count); +} diff --git a/src/vec/ft_vectobuf32.c b/src/vec/ft_vectobuf32.c new file mode 100644 index 0000000..d152d37 --- /dev/null +++ b/src/vec/ft_vectobuf32.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vectobuf32.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 21:09:35 by charles #+# #+# */ +/* Updated: 2020/05/10 21:14:31 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +void *ft_vectobuf32(t_ftvec *vec) +{ + uint32_t *buf; + size_t i; + + if ((buf = malloc(sizeof(uint32_t) * vec->size)) == NULL) + return (NULL); + i = 0; + while (i < vec->size) + { + buf[i] = *(uint32_t*)&vec->data[i]; + i++; + } + return (buf); +} -- cgit From a4b9cda7d6733f2b077f8586e3b3e69351e7dfba Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 11 May 2020 10:54:58 +0200 Subject: Added ft_getfile --- src/io/ft_getfile.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/io/ft_getfile.c (limited to 'src') diff --git a/src/io/ft_getfile.c b/src/io/ft_getfile.c new file mode 100644 index 0000000..891ea51 --- /dev/null +++ b/src/io/ft_getfile.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getfile.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 09:52:32 by charles #+# #+# */ +/* Updated: 2020/05/11 10:15:12 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_io.h" + +char *ft_getfile(int fd) +{ + char *s; + char buf[FT_GETFILE_BUFFER_SIZE + 1]; + int ret; + + if (fd < 0 || fd > OPEN_MAX) + return (NULL); + if ((s = ft_strdup("")) == NULL) + return (NULL); + while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((s = ft_strjoinf(s, buf, FT_STRJOINF_FST)) == NULL) + return (NULL); + } + if (ret == -1) + { + free(s); + return (NULL); + } + return (s); +} -- cgit From b9f000a80cbba38b8f21c9737a42f07573ec7b91 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 11 May 2020 16:14:38 +0200 Subject: Moved util/ft_split* in str, Added ft_memjoin and ft_memjoinf1, Modified ft_getfile so that it can read non-ascii file --- src/io/ft_getfile.c | 35 +++++++++++++++++++---------------- src/mem/ft_memjoin.c | 24 ++++++++++++++++++++++++ src/mem/ft_memjoinf1.c | 22 ++++++++++++++++++++++ src/str/ft_split.c | 4 ++-- src/str/ft_strjoinf.c | 4 +--- src/str/ft_strnew.c | 19 ++++++++++++++++--- src/str/ft_strsdestroy.c | 30 ++++++++++++++++++++++++++++++ src/str/ft_strsjoinf.c | 4 ++-- src/str/ft_strslen.c | 23 +++++++++++++++++++++++ src/util/ft_split_destroy.c | 24 ------------------------ src/util/ft_split_len.c | 23 ----------------------- 11 files changed, 139 insertions(+), 73 deletions(-) create mode 100644 src/mem/ft_memjoin.c create mode 100644 src/mem/ft_memjoinf1.c create mode 100644 src/str/ft_strsdestroy.c create mode 100644 src/str/ft_strslen.c delete mode 100644 src/util/ft_split_destroy.c delete mode 100644 src/util/ft_split_len.c (limited to 'src') diff --git a/src/io/ft_getfile.c b/src/io/ft_getfile.c index 891ea51..d3f697c 100644 --- a/src/io/ft_getfile.c +++ b/src/io/ft_getfile.c @@ -6,32 +6,35 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/05/11 09:52:32 by charles #+# #+# */ -/* Updated: 2020/05/11 10:15:12 by charles ### ########.fr */ +/* Updated: 2020/05/11 16:09:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_io.h" -char *ft_getfile(int fd) +/* +** \brief Read a file in a memory buffer +** \param fd File descriptor to read from +** \param mem Pointer to mem struct (buffer and buffer size) +** \return -1 on error, 0 otherwise +*/ + +int ft_getfile(int fd, t_ftmem *mem) { - char *s; - char buf[FT_GETFILE_BUFFER_SIZE + 1]; + char buf[FT_GETFILE_BUFFER_SIZE]; int ret; - if (fd < 0 || fd > OPEN_MAX) - return (NULL); - if ((s = ft_strdup("")) == NULL) - return (NULL); + if (fd < 0 || fd > OPEN_MAX || mem == NULL + || (mem->data = malloc(1)) == NULL) + return (-1); + mem->size = 0; while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0) { - buf[ret] = '\0'; - if ((s = ft_strjoinf(s, buf, FT_STRJOINF_FST)) == NULL) - return (NULL); + if ((mem->data = ft_memjoinf1(mem->data, mem->size, buf, ret)) == NULL) + return (-1); + mem->size += ret; } if (ret == -1) - { - free(s); - return (NULL); - } - return (s); + free(mem->data); + return (ret); } diff --git a/src/mem/ft_memjoin.c b/src/mem/ft_memjoin.c new file mode 100644 index 0000000..a31a812 --- /dev/null +++ b/src/mem/ft_memjoin.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 15:09:47 by charles #+# #+# */ +/* Updated: 2020/05/11 15:14:08 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_mem.h" + +void *ft_memjoin(void *m1, size_t m1_size, void *m2, size_t m2_size) +{ + void *joined; + + if ((joined = malloc(m1_size + m2_size)) == NULL) + return (NULL); + ft_memcpy(joined, m1, m1_size); + ft_memcpy(joined + m1_size, m2, m2_size); + return (joined); +} diff --git a/src/mem/ft_memjoinf1.c b/src/mem/ft_memjoinf1.c new file mode 100644 index 0000000..8b4db92 --- /dev/null +++ b/src/mem/ft_memjoinf1.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memjoinf1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 15:18:57 by charles #+# #+# */ +/* Updated: 2020/05/11 15:20:06 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_mem.h" + +void *ft_memjoinf1(void *m1, size_t m1_size, void *m2, size_t m2_size) +{ + void *joined; + + joined = ft_memjoin(m1, m1_size, m2, m2_size); + free(m1); + return (joined); +} diff --git a/src/str/ft_split.c b/src/str/ft_split.c index 0cb08e4..5d164d4 100644 --- a/src/str/ft_split.c +++ b/src/str/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ -/* Updated: 2020/05/08 13:39:31 by charles ### ########.fr */ +/* Updated: 2020/05/11 15:54:10 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ char **ft_split(char const *s, char c) while (s[j + i] && s[j + i] != c) i++; if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL) - return (ft_split_destroy(strs)); + return (ft_strsdestroy(strs)); j += i - 1; } strs[tab_counter] = NULL; diff --git a/src/str/ft_strjoinf.c b/src/str/ft_strjoinf.c index 228a963..adf9825 100644 --- a/src/str/ft_strjoinf.c +++ b/src/str/ft_strjoinf.c @@ -6,12 +6,10 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 03:41:07 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:41:25 by cacharle ### ########.fr */ +/* Updated: 2020/05/11 15:20:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include "libft.h" #include "libft_str.h" char *ft_strjoinf(char const *s1, char const *s2, t_ftstrjoinf_tag tag) diff --git a/src/str/ft_strnew.c b/src/str/ft_strnew.c index 1bca6d5..f0d2221 100644 --- a/src/str/ft_strnew.c +++ b/src/str/ft_strnew.c @@ -6,13 +6,26 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:17:34 by cacharle #+# #+# */ -/* Updated: 2019/11/20 03:16:14 by cacharle ### ########.fr */ +/* Updated: 2020/05/11 15:28:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -char *ft_strnew(size_t size) +/* +** \brief Create a new null-terminated string +** \param len String length +** \return Allocated string or NULL is allocation failed +** \note This implementation doesn't follow the subject +** because zeroing every byte is too inefficient +*/ + +char *ft_strnew(size_t len) { - return ((char*)ft_calloc(size + 1, sizeof(char))); + char *s; + + if ((s = (char*)malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + s[len] = '\0'; + return (s); } diff --git a/src/str/ft_strsdestroy.c b/src/str/ft_strsdestroy.c new file mode 100644 index 0000000..bb2204c --- /dev/null +++ b/src/str/ft_strsdestroy.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 16:30:55 by cacharle #+# #+# */ +/* Updated: 2020/05/11 15:53:49 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Destroy a NULL-terminated array of malloc'd string +** \param strs Strings to destroy +** \return NULL (so that it can be used in return statement) +*/ + +void *ft_strsdestroy(char **strs) +{ + int i; + + i = -1; + while (strs[++i] != NULL) + free(strs[i]); + free(strs); + return (NULL); +} diff --git a/src/str/ft_strsjoinf.c b/src/str/ft_strsjoinf.c index 36a2892..c1a0623 100644 --- a/src/str/ft_strsjoinf.c +++ b/src/str/ft_strsjoinf.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 14:27:33 by charles #+# #+# */ -/* Updated: 2020/04/04 23:24:24 by charles ### ########.fr */ +/* Updated: 2020/05/11 15:54:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,6 @@ char *ft_strsjoinf(char **strs, char *delim) char *ret; ret = ft_strsjoin(strs, delim); - ft_split_destroy(strs); + ft_strsdestroy(strs); return (ret); } diff --git a/src/str/ft_strslen.c b/src/str/ft_strslen.c new file mode 100644 index 0000000..0268033 --- /dev/null +++ b/src/str/ft_strslen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strslen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/10 20:58:46 by charles #+# #+# */ +/* Updated: 2020/05/11 15:49:40 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +size_t ft_strslen(char **strs) +{ + size_t count; + + count = 0; + while (strs[count] != NULL) + count++; + return (count); +} diff --git a/src/util/ft_split_destroy.c b/src/util/ft_split_destroy.c deleted file mode 100644 index bc3f4e6..0000000 --- a/src/util/ft_split_destroy.c +++ /dev/null @@ -1,24 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_split_destroy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/27 16:30:55 by cacharle #+# #+# */ -/* Updated: 2020/02/27 17:52:31 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft_util.h" - -void *ft_split_destroy(char **strs) -{ - int i; - - i = -1; - while (strs[++i] != NULL) - free(strs[i]); - free(strs); - return (NULL); -} diff --git a/src/util/ft_split_len.c b/src/util/ft_split_len.c deleted file mode 100644 index 090a0c9..0000000 --- a/src/util/ft_split_len.c +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_split_len.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/05/10 20:58:46 by charles #+# #+# */ -/* Updated: 2020/05/10 21:02:08 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft_util.h" - -size_t ft_split_len(char **split) -{ - size_t count; - - count = 0; - while (split[count] != NULL) - count++; - return (count); -} -- cgit From 966eb29634a84496e0851ef2b5a7d64f413d33ed Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 12 May 2020 19:10:14 +0200 Subject: Added ft_veciter_ret --- src/io/ft_getline.c | 4 ++-- src/vec/ft_veciter_ret.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/vec/ft_veciter_ret.c (limited to 'src') diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c index 4cf3962..d59aa63 100644 --- a/src/io/ft_getline.c +++ b/src/io/ft_getline.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/05/09 11:19:12 by charles ### ########.fr */ +/* Updated: 2020/05/12 17:56:59 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,7 +88,7 @@ int ft_getline(int fd, char **line) int split_at; static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; - if (fd < 0 || fd > OPEN_MAX || line == NULL || FT_GETLINE_BUFFER_SIZE <= 0) + if (fd < 0 || fd > OPEN_MAX || line == NULL) return (FT_ERROR); if ((*line = ft_strdup("")) == NULL) return (FT_ERROR); diff --git a/src/vec/ft_veciter_ret.c b/src/vec/ft_veciter_ret.c new file mode 100644 index 0000000..ddeedcf --- /dev/null +++ b/src/vec/ft_veciter_ret.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_veciter_ret.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/12 18:33:34 by charles #+# #+# */ +/* Updated: 2020/05/12 18:35:48 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Iterate a function over elements of a vector, +** pointer can be changed +** \param vec Iterated vector +** \param f Function applied to each elements, +** returned value is the new pointer value +*/ + +void ft_veciter_ret(t_ftvec *vec, void *(*f)(void *elem)) +{ + size_t i; + + i = 0; + while (i < vec->size) + { + vec->data[i] = f(vec->data[i]); + i++; + } +} -- cgit From d4aeabeac0a8bd1665b14aacf487df17198c1ab9 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 12 May 2020 21:40:41 +0200 Subject: Fixing/refactoring ft_getline, fixed/normed ft_memchr, added ft_strmove --- src/io/ft_getline.c | 77 ++++++++++++++++------------------------------------ src/mem/ft_memchr.c | 23 ++++++---------- src/mem/ft_memcpy.c | 3 +- src/str/ft_strmove.c | 25 +++++++++++++++++ 4 files changed, 58 insertions(+), 70 deletions(-) create mode 100644 src/str/ft_strmove.c (limited to 'src') diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c index d59aa63..067e66c 100644 --- a/src/io/ft_getline.c +++ b/src/io/ft_getline.c @@ -6,63 +6,34 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/05/12 17:56:59 by charles ### ########.fr */ +/* Updated: 2020/05/12 21:32:50 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -static int st_find_newline(char *str) -{ - int i; - - i = -1; - while (str[++i]) - if (str[i] == '\n') - return (i); - return (-1); -} - -static int st_free_return(char **ptr, char **ptr2, int ret) -{ - if (ptr != NULL) - { - free(*ptr); - *ptr = NULL; - } - if (ptr2 != NULL) - { - free(*ptr2); - *ptr2 = NULL; - } - return (ret); -} - static int st_read_line(int fd, char **line, char *rest) { - int ret; - int split_at; - char *buf; + int ret; + char *cut; + static char buf[FT_GETLINE_BUFFER_SIZE + 1] = {'\0'}; - if ((buf = malloc(sizeof(char) * (FT_GETLINE_BUFFER_SIZE + 1))) == NULL) - return (st_free_return(line, NULL, FT_ERROR)); while ((ret = read(fd, buf, FT_GETLINE_BUFFER_SIZE)) > 0) { buf[ret] = '\0'; - if ((split_at = st_find_newline(buf)) != -1) + if ((cut = ft_strchr(buf, '\n')) != NULL) { - ft_strcpy(rest, buf + split_at + 1); - buf[split_at] = '\0'; - if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FT_ERROR)); - return (st_free_return(&buf, NULL, FT_LINE)); + ft_strcpy(rest, cut + 1); + *cut = '\0'; } if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) - return (st_free_return(&buf, NULL, FT_ERROR)); + return (FT_ERROR); + if (cut != NULL) + return (FT_LINE); } if (ret == -1) - return (st_free_return(&buf, line, FT_ERROR)); - return (st_free_return(&buf, NULL, ret)); + free(line); + return (ret); } /* @@ -85,29 +56,27 @@ static int st_read_line(int fd, char **line, char *rest) int ft_getline(int fd, char **line) { - int split_at; - static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{0}}; + char *cut; + static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{'\0'}}; if (fd < 0 || fd > OPEN_MAX || line == NULL) return (FT_ERROR); - if ((*line = ft_strdup("")) == NULL) - return (FT_ERROR); if (rest[fd][0] == '\0') + { + if ((*line = ft_strdup("")) == NULL) + return (FT_ERROR); return (st_read_line(fd, line, rest[fd])); - if ((split_at = st_find_newline(rest[fd])) != -1) + } + if ((cut = ft_strchr(rest[fd], '\n')) != NULL) { - free(*line); - if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL) + *cut = '\0'; + if ((*line = ft_strdup(rest[fd])) == NULL) return (FT_ERROR); - ft_strncpy(*line, rest[fd], split_at); - (*line)[split_at] = '\0'; - ft_strcpy(rest[fd], rest[fd] + split_at + 1); + ft_strmove(rest[fd], cut + 1); return (FT_LINE); } - free(*line); - if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1)))) + if ((*line = ft_strdup(rest[fd])) == NULL) return (FT_ERROR); - ft_strcpy(*line, rest[fd]); rest[fd][0] = '\0'; return (st_read_line(fd, line, rest[fd])); } diff --git a/src/mem/ft_memchr.c b/src/mem/ft_memchr.c index e0266af..848b436 100644 --- a/src/mem/ft_memchr.c +++ b/src/mem/ft_memchr.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:55:31 by cacharle #+# #+# */ -/* Updated: 2020/04/01 21:50:49 by charles ### ########.fr */ +/* Updated: 2020/05/12 21:39:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,16 +27,15 @@ void *ft_memchr(const void *s, int c, size_t n) uint64_t lw; c = (uint8_t)c; - while (((uint64_t)s & 0b111) != 0) + while (((uint64_t)s & 0b111) != 0 && n > 0) { - n--; if (*(uint8_t*)s == (uint8_t)c) - return ((uint8_t*)s); + return ((void*)s); + n--; s++; } - buf = (uint64_t)c | (uint64_t)c << 8 | (uint64_t)c << 16 - | (uint64_t)c << 24 | (uint64_t)c << 32 | (uint64_t)c << 40 - | (uint64_t)c << 48 | (uint64_t)c << 56; + buf = c | c << 8 | c << 16 | c << 24; + buf |= buf << 32; while (n >= 8) { lw = *(uint64_t*)s ^ buf; @@ -45,12 +44,8 @@ void *ft_memchr(const void *s, int c, size_t n) n -= 8; s += 8; } - while (n > 0) - { - if (*(uint8_t*)s == (uint8_t)c) - return ((uint8_t*)s); - n--; - s++; - } + while (n-- > 0) + if (*(uint8_t*)s++ == (uint8_t)c) + return ((void*)(s - 1)); return (NULL); } diff --git a/src/mem/ft_memcpy.c b/src/mem/ft_memcpy.c index 1f84bfd..699111f 100644 --- a/src/mem/ft_memcpy.c +++ b/src/mem/ft_memcpy.c @@ -6,11 +6,10 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:00:07 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:39:04 by cacharle ### ########.fr */ +/* Updated: 2020/05/12 20:49:35 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_mem.h" void *ft_memcpy(void *dest, const void *src, size_t n) diff --git a/src/str/ft_strmove.c b/src/str/ft_strmove.c new file mode 100644 index 0000000..1e297ea --- /dev/null +++ b/src/str/ft_strmove.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/12 20:46:30 by charles #+# #+# */ +/* Updated: 2020/05/12 20:49:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Same as ft_strcpy but the strings can overlap +** \param dest Where src will be copied +** \param src Source string +** \return Returns dest +*/ + +char *ft_strmove(char *dest, const char *src) +{ + return (ft_memmove(dest, src, ft_strlen(src) + 1)); +} -- cgit