diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-05-09 12:31:50 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-05-09 12:31:50 +0200 |
| commit | 02abc030a68cb2fdd2f21c96db830ec8cb9176ad (patch) | |
| tree | 0c2d67c94a3618639fc2cd29d8bc78820e41c254 /src | |
| parent | b5124347359833fcde33452978c62133879c6c9e (diff) | |
| parent | 3a2d19df9e509d0b015c786eb02f8315ff0ad91c (diff) | |
| download | libft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.tar.gz libft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.tar.bz2 libft-02abc030a68cb2fdd2f21c96db830ec8cb9176ad.zip | |
Merge remote-tracking branch 'origin/minishell'
Diffstat (limited to 'src')
42 files changed, 829 insertions, 64 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/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..8ae4a64 --- /dev/null +++ b/src/dstr/ft_dstrnew.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 13:54:52 by charles #+# #+# */ +/* Updated: 2020/04/04 19:50:38 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +/* +** \brief Create a new dynamic string +** \param from Static string to create the dynamic one from +** (will be duplicated) +** \return Created dynamic string or NULL on malloc error +*/ + +t_ftdstr *ft_dstrnew(char *from) +{ + t_ftdstr *dstr; + + if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL || + (dstr->str = ft_strdup(from)) == NULL) + return (NULL); + dstr->length = ft_strlen(from); + dstr->capacity = dstr->length + 1; + return (dstr); +} diff --git a/src/dstr/ft_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 7374a44..bc2e047 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:10:16 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:14:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,15 @@ /* ** \brief Delete one hash table entry ** \param key Key of entry to delete -** \param del Function to destroy the entry -** \warning The del function HAS to free the key +** \param del Function to destroy the entry value ** \note Do nothing if their is to entry which correspond to key */ -void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_entry*)) +void ft_htdelone(t_ftht *ht, char *key, void (*del)(void*)) { + ft_inter_htdel_first_order_setup(del); ft_lstremove_if(ht->buckets + ft_hthash(ht, key), ft_inter_htkey_cmp, key, - (void (*)(void*))del); + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); } diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index ff362d2..c43754e 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:10:31 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:01:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" /* @@ -19,12 +18,15 @@ ** \warning The del function HAS to free the key */ -void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_entry*)) +void ft_htdestroy(t_ftht *ht, t_ftdel_func del) { if (ht == NULL) return ; + ft_inter_htdel_first_order_setup(del); while (ht->size-- > 0) - ft_lstdestroy(ht->buckets + ht->size, (void (*)(void*))del); + ft_lstdestroy(ht->buckets + ht->size, + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); free(ht->buckets); free(ht); } diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index a6383fe..75da785 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/04/01 18:02:57 by charles ### ########.fr */ +/* Updated: 2020/04/03 07:12:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,8 @@ void *ft_htget(t_ftht *ht, char *key) { - t_ftht_digest digest; - t_ftlst *found; + size_t digest; + t_ftlst *found; if (ht == NULL || key == NULL) return (NULL); diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index 3369d24..c5d42ea 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -20,9 +20,9 @@ */ // maybe use a less efficient but understandable function -t_ftht_digest ft_hthash(t_ftht *ht, char *key) +size_t ft_hthash(t_ftht *ht, char *key) { - t_ftht_digest digest; + size_t digest; if (*key == '\0') return (0); diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index e5335d2..585c211 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:23:43 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:34:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" /* @@ -19,7 +18,7 @@ ** \return Created hash table or NULL is an allocation failed */ -t_ftht *ft_htnew(t_ftsize size) +t_ftht *ft_htnew(size_t size) { t_ftht *ht; @@ -27,7 +26,7 @@ t_ftht *ft_htnew(t_ftsize size) return (NULL); if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) return (NULL); - ht->buckets = (t_ftht_bucket*)ft_calloc(size, sizeof(t_ftht_entry)); + ht->buckets = (t_ftlst**)ft_calloc(size, sizeof(t_ftlst*)); if (ht->buckets == NULL) { free(ht); diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 68d3752..b3a44ee 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,18 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/04/01 18:02:12 by charles ### ########.fr */ +/* Updated: 2020/04/03 07:13:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" /* ** \brief Create/Update a entry in hash table. ** \note If `key` already exist in `ht` -** only updates the list node content. -** Else create a new list node in addition the list content. +** only updates the list node entry. +** Else create a new list node in addition the list entry. ** \param ht Hash table where the entry is modified ** \param key Key of the new entry ** \param value Value of the new entry @@ -25,32 +24,36 @@ ** \return Pointer to the created entry, NULL if an allocation failed. */ -t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value, - void (*del)(t_ftht_entry*)) +t_ftht_entry *ft_htset( + t_ftht *ht, + char *key, + void *value, + void (*del)(void*) +) { - t_ftht_digest digest; - t_ftht_entry *content; - t_ftht_bucket bucket; + size_t digest; + t_ftht_entry *entry; t_ftlst *tmp; if (ht == NULL || key == NULL) return (NULL); - if ((content = ft_htentry_new(key, value)) == NULL) - return (NULL); digest = ft_hthash(ht, key); tmp = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); if (tmp != NULL) { if (del != NULL) - del(tmp->data); - tmp->data = content; + del(((t_ftht_entry*)tmp->data)->value); + ((t_ftht_entry*)tmp->data)->value = value; return ((t_ftht_entry*)tmp->data); } - if ((bucket = ft_lstnew(content)) == NULL) + if ((entry = ft_htentry_new(key, value)) == NULL) + return (NULL); + if ((tmp = ft_lstnew(entry)) == NULL) { - del(content); + free(entry->key); + free(entry); return (NULL); } - ft_lstpush_front(ht->buckets + digest, bucket); - return (content); + ft_lstpush_front(ht->buckets + digest, tmp); + return (entry); } diff --git a/src/ht/ft_inter_htdel_first_order.c b/src/ht/ft_inter_htdel_first_order.c new file mode 100644 index 0000000..b6fd770 --- /dev/null +++ b/src/ht/ft_inter_htdel_first_order.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_inter_htdel_first_order.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <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/str/ft_atoi.c b/src/str/ft_atoi.c index d6fa5bb..b8f979d 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -6,14 +6,17 @@ /* By: cacharle <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" /* -** Convert a string to an int +** \brief Extract first int in a string +** (takes as much digits has possible) +** \param str String to convert +** \return Extracted int */ int ft_atoi(const char *str) diff --git a/src/str/ft_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_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_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_strncasecmp.c b/src/str/ft_strncasecmp.c index aafdc8c..7153237 100644 --- a/src/str/ft_strncasecmp.c +++ b/src/str/ft_strncasecmp.c @@ -11,7 +11,7 @@ /* ************************************************************************** */ #include "libft.h" -#include "libft_types.h" +#include "libft_def.h" int ft_strncasecmp(const char *s1, const char *s2, size_t n) { diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c index a0371e4..3e01708 100644 --- a/src/str/ft_strncmp.c +++ b/src/str/ft_strncmp.c @@ -6,11 +6,10 @@ /* 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_str.h" int ft_strncmp(const char *s1, const char *s2, size_t n) 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..36a2892 --- /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/04/04 23:24:24 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +/* +** \brief Join null-terminated array of strings and free the array +** \param strs Array of strings +** \param delim String which will be put between each string +** \return Joined string or NULL on error +*/ + +char *ft_strsjoinf(char **strs, char *delim) +{ + char *ret; + + ret = ft_strsjoin(strs, delim); + ft_split_destroy(strs); + return (ret); +} diff --git a/src/str/ft_substr.c b/src/str/ft_strsub.c index 59fe3f2..c7121bd 100644 --- a/src/str/ft_substr.c +++ b/src/str/ft_strsub.c @@ -1,23 +1,37 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* 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'; 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_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_vecgrow.c b/src/vec/ft_vecgrow.c index bb8b8c7..2213c88 100644 --- a/src/vec/ft_vecgrow.c +++ b/src/vec/ft_vecgrow.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:13:07 by charles #+# #+# */ -/* Updated: 2020/04/01 21:15:20 by charles ### ########.fr */ +/* Updated: 2020/04/02 10:43:01 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ t_ftvec *ft_vecgrow(t_ftvec *vec) new_capacity = vec->capacity * FT_VEC_GROWTH_FACTOR; if ((new_data = (void**)malloc(sizeof(void*) * new_capacity)) == NULL) return (NULL); - ft_memcpy(new_data, vec->data, vec->size); + ft_memcpy(new_data, vec->data, vec->size * sizeof(void*)); free(vec->data); vec->data = new_data; vec->capacity = new_capacity; diff --git a/src/vec/ft_vecinsert.c b/src/vec/ft_vecinsert.c new file mode 100644 index 0000000..1682daa --- /dev/null +++ b/src/vec/ft_vecinsert.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <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_vecnew.c b/src/vec/ft_vecnew.c index 8a8736a..0def9c9 100644 --- a/src/vec/ft_vecnew.c +++ b/src/vec/ft_vecnew.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:03:49 by charles #+# #+# */ -/* Updated: 2020/04/01 20:00:00 by charles ### ########.fr */ +/* Updated: 2020/04/04 21:24:36 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/ft_vecpush.c b/src/vec/ft_vecpush.c index fc903ef..026ae3d 100644 --- a/src/vec/ft_vecpush.c +++ b/src/vec/ft_vecpush.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 19:22:20 by charles #+# #+# */ -/* Updated: 2020/04/01 20:20:06 by charles ### ########.fr */ +/* Updated: 2020/04/02 10:51:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/vec/ft_vecpush_safe.c b/src/vec/ft_vecpush_safe.c new file mode 100644 index 0000000..8e5a8d4 --- /dev/null +++ b/src/vec/ft_vecpush_safe.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpush_safe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/04 13:29:48 by charles #+# #+# */ +/* Updated: 2020/04/04 14:24:19 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Wrapper around ft_vecpush which reject null element +** \param vec Pushed vector +** \param pushed Element pushed which can't be NULL +** \return NULL if pushed is NULL, whatever ft_vecpush returns otherwise +*/ + +t_ftvec *ft_vecpush_safe(t_ftvec *vec, void *pushed) +{ + if (pushed == NULL) + return (NULL); + return (ft_vecpush(vec, pushed)); +} diff --git a/src/vec/ft_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); +} |
