diff options
Diffstat (limited to 'src')
105 files changed, 2184 insertions, 411 deletions
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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 new file mode 100644 index 0000000..6749ab0 --- /dev/null +++ b/src/algo/ft_compar_str.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_compar_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 15:44:24 by charles #+# #+# */ +/* 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 3e7ae31..fd26a88 100644 --- a/src/algo/ft_is_set.c +++ b/src/algo/ft_is_set.c @@ -6,14 +6,21 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:51:41 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:33:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_algo.h" -t_ftbool ft_is_set(void *base, size_t nel, size_t width, +/* +** \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) { size_t i; 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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/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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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/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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/src/dstr/ft_dstrdestroy.c b/src/dstr/ft_dstrdestroy.c new file mode 100644 index 0000000..c76b692 --- /dev/null +++ b/src/dstr/ft_dstrdestroy.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:58:46 by charles #+# #+# */ +/* 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) + return ; + free(dstr->str); + free(dstr); +} 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_dstrgrow.c b/src/dstr/ft_dstrgrow.c new file mode 100644 index 0000000..40cad86 --- /dev/null +++ b/src/dstr/ft_dstrgrow.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 14:17:09 by charles #+# #+# */ +/* Updated: 2020/04/04 19:56:26 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +#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 (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 new file mode 100644 index 0000000..931f5d8 --- /dev/null +++ b/src/dstr/ft_dstrinsert.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 14:00:44 by charles #+# #+# */ +/* 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->length) + return (NULL); + inserted_len = ft_strlen(inserted); + 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); + 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..c280dd1 --- /dev/null +++ b/src/dstr/ft_dstrnew.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:54:52 by charles #+# #+# */ +/* Updated: 2020/05/09 12:57:17 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; + + 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/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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/src/dstr/ft_dstrunwrap.c b/src/dstr/ft_dstrunwrap.c new file mode 100644 index 0000000..5b29b5b --- /dev/null +++ b/src/dstr/ft_dstrunwrap.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrunwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:59:35 by charles #+# #+# */ +/* 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; + + tmp = dstr->str; + free(dstr); + return (tmp); +} diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 2c54721..bc2e047 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -6,16 +6,25 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:35:06 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:14:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" -void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)) +/* +** \brief Delete one hash table entry +** \param key Key of entry to delete +** \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)(void*)) { - ft_lstremove_if(ht->entries + ft_hthash(ht, key), + 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 e0442c6..c43754e 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -5,20 +5,28 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* 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/04/03 07:01:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" -void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)) +/* +** \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, t_ftdel_func del) { if (ht == NULL) return ; + ft_inter_htdel_first_order_setup(del); while (ht->size-- > 0) - ft_lstclear(ht->entries + ht->size, (void (*)(void*))del); - free(ht->entries); + 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_htcontent_new.c b/src/ht/ft_htentry_new.c index 214e125..12a1159 100644 --- a/src/ht/ft_htcontent_new.c +++ b/src/ht/ft_htentry_new.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_htcontent_new.c :+: :+: :+: */ +/* ft_htentry_new.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -13,13 +13,19 @@ #include "libft.h" #include "libft_ht.h" -t_ftht_content *ft_htcontent_new(char *key, void *value) +/* +** \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_content *content; + t_ftht_entry *content; if (key == NULL) return (NULL); - if ((content = (t_ftht_content*)malloc(sizeof(t_ftht_content))) == NULL) + if ((content = (t_ftht_entry*)malloc(sizeof(t_ftht_entry))) == NULL) return (NULL); if ((content->key = ft_strdup(key)) == NULL) { diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index 76e4fb2..75da785 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,24 +6,30 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/02/19 01:44:41 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:12:58 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 +** \return Value void pointer at key or NULL if not found +*/ + 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); 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 e7e696c..c5d42ea 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -12,9 +12,17 @@ #include "libft_ht.h" -t_ftht_digest ft_hthash(t_ftht *ht, char *key) +/* +** \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 +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_htiter.c b/src/ht/ft_htiter.c new file mode 100644 index 0000000..b854993 --- /dev/null +++ b/src/ht/ft_htiter.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 +*/ + +void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*)) +{ + size_t i; + + i = 0; + while (i < ht->size) + { + ft_lstiter(ht->buckets[i], (void (*)(void*))f); + i++; + } +} diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index d98a724..585c211 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,14 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:16:20 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:34:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" -t_ftht *ft_htnew(t_ftsize size) +/* +** \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(size_t size) { t_ftht *ht; @@ -21,8 +26,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_ftlst**)ft_calloc(size, sizeof(t_ftlst*)); + if (ht->buckets == NULL) { free(ht); return (NULL); diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index c7068d5..b3a44ee 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,40 +6,54 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:44:10 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:13:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" -t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, - void (*del)(t_ftht_content*)) +/* +** \brief Create/Update a entry in hash table. +** \note If `key` already exist in `ht` +** 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 +** \param del Destroy function in case the entry is modified. +** \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)(void*) +) { - t_ftht_digest digest; - t_ftht_content *content; - t_ftht_entry entry; + size_t digest; + t_ftht_entry *entry; t_ftlst *tmp; if (ht == NULL || key == NULL) return (NULL); - if ((content = ft_htcontent_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(((t_ftht_entry*)tmp->data)->value); + ((t_ftht_entry*)tmp->data)->value = value; + return ((t_ftht_entry*)tmp->data); } - - if ((entry = ft_lstnew(content)) == NULL) + if ((entry = ft_htentry_new(key, value)) == NULL) + return (NULL); + if ((tmp = ft_lstnew(entry)) == NULL) { - free(content); + free(entry->key); + free(entry); return (NULL); } - ft_lstadd_front(ht->entries + digest, entry); - 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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/ht/ft_inter_htkey_cmp.c b/src/ht/ft_inter_htkey_cmp.c index 6f04ecc..e8a0375 100644 --- a/src/ht/ft_inter_htkey_cmp.c +++ b/src/ht/ft_inter_htkey_cmp.c @@ -6,16 +6,20 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ #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) 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_read_file.c b/src/io/ft_getfile.c index 90ee38d..6ae4833 100644 --- a/src/io/ft_read_file.c +++ b/src/io/ft_getfile.c @@ -1,33 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_read_file.c :+: :+: :+: */ +/* ft_getfile.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/22 10:32:47 by cacharle #+# #+# */ -/* Updated: 2020/02/22 10:35:07 by cacharle ### ########.fr */ +/* Updated: 2020/08/02 11:01:06 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_io.h" -char *ft_read_file(char *filename) +int ft_getfile(char *filename, t_ftmem *mem) { int fd; - char *file; - if ((fd = open(filename, O_RDONLY)) < 0 || fd > OPEN_MAX) - return (NULL); - if ((file = ft_read_fd(fd)) == NULL) + if ((fd = open(filename, O_RDONLY)) < 0) + return (-1); + if (ft_getfile_fd(fd, mem) < 0) { - free(file); - return (NULL); + close(fd); + return (-1); } - if (close(fd) < 0) - { - free(file); - return (NULL); - } - return (file); + return (close(fd)); } diff --git a/src/io/ft_getfile_fd.c b/src/io/ft_getfile_fd.c new file mode 100644 index 0000000..91ebf7f --- /dev/null +++ b/src/io/ft_getfile_fd.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getfile_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/05/11 09:52:32 by charles #+# #+# */ +/* Updated: 2020/08/02 10:55:46 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_io.h" + +/* +** \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_fd(int fd, t_ftmem *mem) +{ + char buf[FT_GETFILE_BUFFER_SIZE]; + int ret; + + 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) + { + if ((mem->data = ft_memjoinf1(mem->data, mem->size, buf, ret)) == NULL) + return (-1); + mem->size += ret; + } + if (ret == -1) + free(mem->data); + return (ret); +} diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c new file mode 100644 index 0000000..067e66c --- /dev/null +++ b/src/io/ft_getline.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_getline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ +/* Updated: 2020/05/12 21:32:50 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int st_read_line(int fd, char **line, char *rest) +{ + int ret; + char *cut; + static char buf[FT_GETLINE_BUFFER_SIZE + 1] = {'\0'}; + + while ((ret = read(fd, buf, FT_GETLINE_BUFFER_SIZE)) > 0) + { + buf[ret] = '\0'; + if ((cut = ft_strchr(buf, '\n')) != NULL) + { + ft_strcpy(rest, cut + 1); + *cut = '\0'; + } + if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL) + return (FT_ERROR); + if (cut != NULL) + return (FT_LINE); + } + if (ret == -1) + free(line); + return (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) +{ + 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 (rest[fd][0] == '\0') + { + if ((*line = ft_strdup("")) == NULL) + return (FT_ERROR); + return (st_read_line(fd, line, rest[fd])); + } + if ((cut = ft_strchr(rest[fd], '\n')) != NULL) + { + *cut = '\0'; + if ((*line = ft_strdup(rest[fd])) == NULL) + return (FT_ERROR); + ft_strmove(rest[fd], cut + 1); + return (FT_LINE); + } + if ((*line = ft_strdup(rest[fd])) == NULL) + return (FT_ERROR); + 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 0f4cc2c..0000000 --- a/src/io/ft_next_line.c +++ /dev/null @@ -1,113 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:38:01 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/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_lstbsearch.c b/src/lst/ft_lstbsearch.c index d694209..0c48eb0 100644 --- a/src/lst/ft_lstbsearch.c +++ b/src/lst/ft_lstbsearch.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -46,11 +46,11 @@ static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last, return (NULL); if (mid->next == NULL) { - if (cmp(ref, mid->content) == 0) + if (cmp(ref, mid->data) == 0) return (mid); return (NULL); } - res = cmp(ref, mid->next->content); + res = cmp(ref, mid->next->data); if (res < 0) return (st_lstbsearch_rec(lst, mid, cmp, ref)); else if (res > 0) diff --git a/src/lst/ft_lstdelone.c b/src/lst/ft_lstdelone.c index 63dcc35..3dfbbbb 100644 --- a/src/lst/ft_lstdelone.c +++ b/src/lst/ft_lstdelone.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstdelone_bonus.c :+: :+: :+: */ +/* ft_lstdelone.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,14 +10,18 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Delete list node +** \param del Delete function for node's data +*/ + void ft_lstdelone(t_ftlst *lst, void (*del)(void *)) { if (lst == NULL) return ; if (del != NULL) - (*del)(lst->content); + (*del)(lst->data); free(lst); } diff --git a/src/lst/ft_lstclear.c b/src/lst/ft_lstdestroy.c index 0bacb4f..35da2a5 100644 --- a/src/lst/ft_lstclear.c +++ b/src/lst/ft_lstdestroy.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstclear_bonus.c :+: :+: :+: */ +/* ft_lstdestroy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,16 +10,20 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -void ft_lstclear(t_ftlst **lst, void (*del)(void *)) +/* +** \brief Destroy a list and set his pointer to NULL +** \param del Delete Function for data of each node +*/ + +void ft_lstdestroy(t_ftlst **lst, void (*del)(void *)) { if (lst == NULL) return ; if (*lst == NULL) return ; - ft_lstclear(&((*lst)->next), del); + ft_lstdestroy(&((*lst)->next), del); ft_lstdelone(*lst, del); *lst = NULL; } diff --git a/src/lst/ft_lstiter.c b/src/lst/ft_lstiter.c index 9b2895b..e46b507 100644 --- a/src/lst/ft_lstiter.c +++ b/src/lst/ft_lstiter.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstiter_bonus.c :+: :+: :+: */ +/* ft_lstiter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,16 +10,20 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Iterate of list +** \param f Funtion applied to data of each node +*/ + void ft_lstiter(t_ftlst *lst, void (*f)(void *)) { if (f == NULL) return ; while (lst != NULL) { - (*f)(lst->content); + (*f)(lst->data); lst = lst->next; } } diff --git a/src/lst/ft_lstlast.c b/src/lst/ft_lstlast.c index 728cbf2..97b853d 100644 --- a/src/lst/ft_lstlast.c +++ b/src/lst/ft_lstlast.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstlast_bonus.c :+: :+: :+: */ +/* ft_lstlast.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,9 +10,13 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Last node +** \return List's last node +*/ + t_ftlst *ft_lstlast(t_ftlst *lst) { if (lst == NULL) diff --git a/src/lst/ft_lstlfind.c b/src/lst/ft_lstlfind.c index 92d37d8..fd7e688 100644 --- a/src/lst/ft_lstlfind.c +++ b/src/lst/ft_lstlfind.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* 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 */ /* */ /* ************************************************************************** */ @@ -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 8d2acc8..11c528c 100644 --- a/src/lst/ft_lstlsearch.c +++ b/src/lst/ft_lstlsearch.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* 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 */ /* */ /* ************************************************************************** */ @@ -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..3182bb0 100644 --- a/src/lst/ft_lstmap.c +++ b/src/lst/ft_lstmap.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstmap_bonus.c :+: :+: :+: */ +/* ft_lstmap.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,9 +10,16 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Clone a list and map a function to each node data +** \param lst Origin list +** \param f Function applied to each node's data +** \param del Delete function for cleanning up in case of failed allocation +** \return Mapped clone list +*/ + t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) { t_ftlst *mapped; @@ -23,12 +30,12 @@ t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) mapped = NULL; while (lst != NULL) { - if ((tmp = ft_lstnew((*f)(lst->content))) == NULL) + if ((tmp = ft_lstnew((*f)(lst->data))) == NULL) { - ft_lstclear(&mapped, del); + ft_lstdestroy(&mapped, del); return (NULL); } - ft_lstadd_back(&mapped, tmp); + ft_lstpush_back(&mapped, tmp); lst = lst->next; } return (mapped); @@ -41,9 +48,9 @@ t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) ** ** if (lst == NULL) ** return (NULL); -** if ((tmp = ft_lstnew(lst->content)) == NULL) +** if ((tmp = ft_lstnew(lst->data)) == NULL) ** return (NULL); -** tmp->content = (*f)(tmp->content); +** tmp->data = (*f)(tmp->data); ** tmp->next = ft_lstmap(lst->next, f); ** return (tmp); */ diff --git a/src/lst/ft_lstnew.c b/src/lst/ft_lstnew.c index 11cf223..1616b71 100644 --- a/src/lst/ft_lstnew.c +++ b/src/lst/ft_lstnew.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstnew_bonus.c :+: :+: :+: */ +/* ft_lstnew.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,16 +10,20 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -t_ftlst *ft_lstnew(void const *content) +/* +** \brief Create a list node +** \param data Pointer to data of node +*/ + +t_ftlst *ft_lstnew(void const *data) { t_ftlst *elem; if ((elem = (t_ftlst*)malloc(sizeof(t_ftlst))) == NULL) return (NULL); - elem->content = (void*)content; + elem->data = (void*)data; elem->next = NULL; return (elem); } diff --git a/src/lst/ft_lstpop_front.c b/src/lst/ft_lstpop_front.c index ff386f9..a61350a 100644 --- a/src/lst/ft_lstpop_front.c +++ b/src/lst/ft_lstpop_front.c @@ -5,14 +5,18 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* 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 */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Delete head node and replace it with next node +** \param del Delete function for node data +*/ + void ft_lstpop_front(t_ftlst **lst, void (*del)(void *)) { t_ftlst *tmp; diff --git a/src/lst/ft_lstadd_back.c b/src/lst/ft_lstpush_back.c index 8f39a75..1dca078 100644 --- a/src/lst/ft_lstadd_back.c +++ b/src/lst/ft_lstpush_back.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstadd_back_bonus.c :+: :+: :+: */ +/* ft_lstpush_back.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,10 +10,14 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -void ft_lstadd_back(t_ftlst **alst, t_ftlst *new) +/* +** \brief Push new node to the list end +** \param new Pushed node +*/ + +void ft_lstpush_back(t_ftlst **alst, t_ftlst *new) { if (alst == NULL) return ; diff --git a/src/lst/ft_lstadd_front.c b/src/lst/ft_lstpush_front.c index bcd5ad9..85df649 100644 --- a/src/lst/ft_lstadd_front.c +++ b/src/lst/ft_lstpush_front.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstadd_front_bonus.c :+: :+: :+: */ +/* ft_lstpush_front.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,10 +10,14 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -void ft_lstadd_front(t_ftlst **alst, t_ftlst *new) +/* +** \brief Push node to list front +** \param new Pushed node +*/ + +void ft_lstpush_front(t_ftlst **alst, t_ftlst *new) { if (alst == NULL || new == NULL) return ; diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c index 5221ae4..4070355 100644 --- a/src/lst/ft_lstremove_if.c +++ b/src/lst/ft_lstremove_if.c @@ -6,13 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ -#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..7c2778d 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..36c0c5c 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..3c6956b 100644 --- a/src/lst/ft_lstsize.c +++ b/src/lst/ft_lstsize.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstsize_bonus.c :+: :+: :+: */ +/* ft_lstsize.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,9 +10,13 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief List size +** \return Number of node in list +*/ + int ft_lstsize(t_ftlst *lst) { int counter; diff --git a/src/lst/ft_lstsort.c b/src/lst/ft_lstsort.c index e1c9913..9945a0f 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..995785f 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); diff --git a/src/mem/ft_memccpy.c b/src/mem/ft_memccpy.c index 8ce656a..2f6525a 100644 --- a/src/mem/ft_memccpy.c +++ b/src/mem/ft_memccpy.c @@ -6,26 +6,62 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:01:53 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:54:03 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:50:29 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #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..848b436 100644 --- a/src/mem/ft_memchr.c +++ b/src/mem/ft_memchr.c @@ -6,21 +6,46 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:55:31 by cacharle #+# #+# */ -/* Updated: 2020/02/13 04:28:00 by cacharle ### ########.fr */ +/* Updated: 2020/05/12 21:39:04 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #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 > 0) + { + if (*(uint8_t*)s == (uint8_t)c) + return ((void*)s); + n--; + s++; + } + buf = c | c << 8 | c << 16 | c << 24; + buf |= buf << 32; + 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 ((void*)(s - 1)); 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..699111f 100644 --- a/src/mem/ft_memcpy.c +++ b/src/mem/ft_memcpy.c @@ -6,23 +6,23 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { - 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_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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/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..9005947 100644 --- a/src/mem/ft_memset.c +++ b/src/mem/ft_memset.c @@ -6,26 +6,34 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:01:23 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:39:10 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:49:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #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/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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; +} 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_atoi.c b/src/str/ft_atoi.c index fa31407..b8f979d 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -6,12 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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" +/* +** \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) { return ((int)ft_strtol(str, (char**)NULL, 10)); 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/src/str/ft_split.c b/src/str/ft_split.c index 6fb5964..5d164d4 100644 --- a/src/str/ft_split.c +++ b/src/str/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ -/* Updated: 2019/11/20 04:08:27 by cacharle ### ########.fr */ +/* Updated: 2020/05/11 15:54:10 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_strsdestroy(strs)); j += i - 1; } strs[tab_counter] = 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_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_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_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_strjoin3.c b/src/str/ft_strjoin3.c new file mode 100644 index 0000000..e5e5530 --- /dev/null +++ b/src/str/ft_strjoin3.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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; + + 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); +} 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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <stdlib.h> -#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_strlen.c b/src/str/ft_strlen.c index 0d593e1..999763e 100644 --- a/src/str/ft_strlen.c +++ b/src/str/ft_strlen.c @@ -6,36 +6,68 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */ -/* Updated: 2020/01/17 11:13:43 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:45:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +#include <stdint.h> + +/* +** 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; + const char *found; - ptr = (unsigned long int*)s; + cpy = s; + while (((uint64_t)cpy & 0b111) != 0) + if (*cpy++ == '\0') + return (cpy - s - 1); + ptr = (uint64_t*)cpy; while (TRUE) - { - cpy = (const char*)ptr++; - if (cpy[0] == '\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); - } + if (((*ptr++ - LOMAGIC) & HIMAGIC) != 0) + { + cpy = (const char*)(ptr - 1); + if ((found = ft_memchr(cpy, '\0', sizeof(uint64_t))) != NULL) + return (found - s); + } } 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} 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_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..3e01708 100644 --- a/src/str/ft_strncmp.c +++ b/src/str/ft_strncmp.c @@ -6,21 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:27:34 by cacharle #+# #+# */ -/* Updated: 2020/02/10 04:17:50 by cacharle ### ########.fr */ +/* Updated: 2020/05/09 12:29:41 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#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_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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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..0923bde --- /dev/null +++ b/src/str/ft_strsjoin.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 14:30:08 by charles #+# #+# */ +/* 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; + 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] != '\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 new file mode 100644 index 0000000..c1a0623 --- /dev/null +++ b/src/str/ft_strsjoinf.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsjoinf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 14:27:33 by charles #+# #+# */ +/* Updated: 2020/05/11 15:54:33 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; + + ret = ft_strsjoin(strs, delim); + 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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/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_strsub.c index ad9c706..c7121bd 100644 --- a/src/str/ft_substr.c +++ b/src/str/ft_strsub.c @@ -1,25 +1,40 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_substr.c :+: :+: :+: */ +/* ft_strsub.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:44:42 by cacharle ### ########.fr */ +/* Updated: 2020/05/09 12:30:39 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" +#include "libft_str.h" -char *ft_substr(char const *s, unsigned int start, size_t len) +/* +** \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; + 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'; if (start > ft_strlen(s)) return (sub); 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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/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 <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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/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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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..2213c88 --- /dev/null +++ b/src/vec/ft_vecgrow.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:13:07 by charles #+# #+# */ +/* Updated: 2020/04/02 10:43:01 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 * sizeof(void*)); + free(vec->data); + vec->data = new_data; + vec->capacity = new_capacity; + return (vec); +} 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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_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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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++; + } +} diff --git a/src/vec/ft_vecnew.c b/src/vec/ft_vecnew.c new file mode 100644 index 0000000..0def9c9 --- /dev/null +++ b/src/vec/ft_vecnew.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:03:49 by charles #+# #+# */ +/* Updated: 2020/04/04 21:24:36 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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..026ae3d --- /dev/null +++ b/src/vec/ft_vecpush.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpush.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:22:20 by charles #+# #+# */ +/* Updated: 2020/04/02 10:51:38 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); +} diff --git a/src/io/ft_read_fd.c b/src/vec/ft_vecpush_safe.c index 758216a..8e5a8d4 100644 --- a/src/io/ft_read_fd.c +++ b/src/vec/ft_vecpush_safe.c @@ -1,36 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_read_fd.c :+: :+: :+: */ +/* ft_vecpush_safe.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/22 10:26:44 by cacharle #+# #+# */ -/* Updated: 2020/02/22 10:36:26 by cacharle ### ########.fr */ +/* Created: 2020/04/04 13:29:48 by charles #+# #+# */ +/* Updated: 2020/04/04 14:24:19 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" +#include "libft_vec.h" -#define FT_READ_FD_BUF_SIZE 2048 +/* +** \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 +*/ -char *ft_read_fd(int fd) +t_ftvec *ft_vecpush_safe(t_ftvec *vec, void *pushed) { - int ret; - char *file; - char buf[FT_READ_FD_BUF_SIZE + 1]; - - if ((file = ft_strdup("")) == NULL) - return (NULL); - while ((ret = read(fd, buf, FT_READ_FD_BUF_SIZE)) > 0) - { - buf[ret] = '\0'; - ft_strjoinf(file, buf, FT_STRJOINF_FST); - } - if (ret == -1) - { - free(file); + if (pushed == NULL) return (NULL); - } - return (file); + return (ft_vecpush(vec, pushed)); } 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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--; +} diff --git a/src/vec/ft_vecsort.c b/src/vec/ft_vecsort.c new file mode 100644 index 0000000..8aa5c2a --- /dev/null +++ b/src/vec/ft_vecsort.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 15:53:23 by charles #+# #+# */ +/* 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); +} 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} |
