From 65cf641e9533b190db870d0cc46f2f852239ebf6 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 4 Apr 2020 23:36:19 +0200 Subject: Added test for ft_strsjoin, ft_strsjoinf, ft_vecpush_safe, Added doc for algo functions, tested functions --- src/algo/ft_compar_int.c | 9 ++++++++- src/algo/ft_compar_str.c | 9 ++++++++- src/algo/ft_is_set.c | 10 +++++++++- src/algo/ft_mergesort.c | 24 +++++++++++++++++++++++- src/algo/ft_qsort.c | 35 ++++++++++++++++++++++++++++++++++- src/algo/ft_reverse.c | 9 ++++++++- src/ht/ft_htnew.c | 3 +-- src/str/ft_atoi.c | 7 +++++-- src/str/ft_strsjoin.c | 12 ++++++++++-- src/str/ft_strsjoinf.c | 9 ++++++++- 10 files changed, 114 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/algo/ft_compar_int.c b/src/algo/ft_compar_int.c index 848dc71..ab057bf 100644 --- a/src/algo/ft_compar_int.c +++ b/src/algo/ft_compar_int.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 08:24:43 by cacharle #+# #+# */ -/* Updated: 2020/01/19 08:27:38 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:30:09 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +/* +** \brief Comparison function for 2 ints +** \param a Pointer to first int +** \param b Pointer to first int +** \return The difference between a and b +*/ + int ft_compar_int(const void *a, const void *b) { return (*(int*)a - *(int*)b); diff --git a/src/algo/ft_compar_str.c b/src/algo/ft_compar_str.c index 0d3e6ff..6749ab0 100644 --- a/src/algo/ft_compar_str.c +++ b/src/algo/ft_compar_str.c @@ -6,12 +6,19 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 15:44:24 by charles #+# #+# */ -/* Updated: 2020/04/04 15:46:28 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:31:15 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief String comparison function +** \param s1_p Pointer to first string +** \param s2_p Pointer to second string +** \return `strcmp` of both strings +*/ + int ft_compar_str(const void *s1_p, const void *s2_p) { return (ft_strcmp(*(const char**)s1_p, *(const char**)s2_p)); diff --git a/src/algo/ft_is_set.c b/src/algo/ft_is_set.c index 79a5e7c..fd26a88 100644 --- a/src/algo/ft_is_set.c +++ b/src/algo/ft_is_set.c @@ -6,12 +6,20 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */ -/* Updated: 2020/04/03 07:05:19 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:33:03 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Test whether array only contains unique elements +** \param base Array to test +** \param nel Number of element in the array +** \param width Size of an element +** \param compar Comparison function to test if 2 elements are equal +*/ + bool ft_is_set(void *base, size_t nel, size_t width, t_ftcompar_func compar) { diff --git a/src/algo/ft_mergesort.c b/src/algo/ft_mergesort.c index 25b4255..8556145 100644 --- a/src/algo/ft_mergesort.c +++ b/src/algo/ft_mergesort.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 02:26:41 by cacharle #+# #+# */ -/* Updated: 2020/02/13 23:14:21 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:40:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Helper function to return in case of error +** \param left Left subarray +** \param right Right subarray +** \return -1 to indicate error +*/ + static int st_mergesort_error(void *left, void *right) { free(left); @@ -19,6 +26,13 @@ static int st_mergesort_error(void *left, void *right) return (-1); } +/* +** \brief Merge 2 sorted arrays +** \param arrays Struct containing the arrays (base, left, right) +** \param nel Number of element in base +** \param compar Comparison function +*/ + static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel, size_t width, int (*compar)(const void *, const void *)) { @@ -49,6 +63,14 @@ static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel, arrays->right + ri++ * width, width); } +/* +** \brief Sort an array using the merge sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \return 0 on success, -1 on error +*/ + int ft_mergesort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)) { diff --git a/src/algo/ft_qsort.c b/src/algo/ft_qsort.c index 9bcfcdf..0c1c0f7 100644 --- a/src/algo/ft_qsort.c +++ b/src/algo/ft_qsort.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 07:25:51 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:55:14 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:10:29 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Helper to create a new range +** \param lo Lower bound +** \param hi Higher bound +** \return Range struct (not allocated) +*/ + static t_ftrange ft_range_new(int lo, int hi) { t_ftrange range; @@ -21,6 +28,16 @@ static t_ftrange ft_range_new(int lo, int hi) return (range); } +/* +** \brief Array partitionning, +** takes a pivot and place every element lower than it before +** and every element greater after +** \param base Partitioned array +** \param range Lower and upper bound +** \param width Single element size +** \param compar Comparison function +*/ + static int ft_qsort_partition(void *base, t_ftrange range, size_t width, t_ftcompar_func compar) { @@ -43,6 +60,14 @@ static int ft_qsort_partition(void *base, t_ftrange range, return (p); } +/* +** \brief Qsort recursion function +** \param base Array to sort +** \param range Lower and upper bound which define the area to sort +** \param width Single element size +** \param compar Comparision function +*/ + static void ft_qsort_rec(void *base, t_ftrange range, size_t width, t_ftcompar_func compar) { @@ -55,6 +80,14 @@ static void ft_qsort_rec(void *base, t_ftrange range, ft_qsort_rec(base, ft_range_new(pivot + 1, range.hi), width, compar); } +/* +** \brief Sort an array using the Quick sort algorithm +** \param base Array to sort +** \param nel Number of element in the array +** \param width Size of each element +** \param compar Comparision function +*/ + void ft_qsort(void *base, size_t nel, size_t width, t_ftcompar_func compar) { diff --git a/src/algo/ft_reverse.c b/src/algo/ft_reverse.c index 0bc447f..c617338 100644 --- a/src/algo/ft_reverse.c +++ b/src/algo/ft_reverse.c @@ -6,12 +6,19 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/10 05:07:13 by cacharle #+# #+# */ -/* Updated: 2020/02/10 05:19:22 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:36:23 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_algo.h" +/* +** \brief Reverse an array +** \param base Array to reverse +** \param nel Number of element in the array +** \param width Size of each elements +*/ + void ft_reverse(void *base, size_t nel, size_t width) { size_t i; diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index b6d1cc3..585c211 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,12 +6,11 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/04/03 06:53:51 by charles ### ########.fr */ +/* Updated: 2020/04/04 22:34:55 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_ht.h" -#include "libft_mem.h" /* ** \brief Create a new hash table. diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c index d6fa5bb..b8f979d 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -6,14 +6,17 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:46:16 by cacharle #+# #+# */ -/* Updated: 2020/01/15 10:56:06 by cacharle ### ########.fr */ +/* Updated: 2020/04/04 22:34:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" /* -** Convert a string to an int +** \brief Extract first int in a string +** (takes as much digits has possible) +** \param str String to convert +** \return Extracted int */ int ft_atoi(const char *str) diff --git a/src/str/ft_strsjoin.c b/src/str/ft_strsjoin.c index 507903b..0923bde 100644 --- a/src/str/ft_strsjoin.c +++ b/src/str/ft_strsjoin.c @@ -6,12 +6,20 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 14:30:08 by charles #+# #+# */ -/* Updated: 2020/04/04 14:45:58 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:34:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_str.h" +/* +** \brief Join null-terminated array of strings +** \param strs Array of strings +** \param delim String iterspersed between strings +** \return Joined string or NULL on error +** \note Empty strings are ignored +*/ + char *ft_strsjoin(char **strs, char *delim) { int i; @@ -35,7 +43,7 @@ char *ft_strsjoin(char **strs, char *delim) while (strs[++i] != NULL) { ft_strcat(join, strs[i]); - if (strs[i + 1] != NULL) + if (*strs[i] != '\0' && strs[i + 1] != NULL) ft_strcat(join, delim); } return (join); diff --git a/src/str/ft_strsjoinf.c b/src/str/ft_strsjoinf.c index f5077c1..36a2892 100644 --- a/src/str/ft_strsjoinf.c +++ b/src/str/ft_strsjoinf.c @@ -6,12 +6,19 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/04 14:27:33 by charles #+# #+# */ -/* Updated: 2020/04/04 14:29:49 by charles ### ########.fr */ +/* Updated: 2020/04/04 23:24:24 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft_str.h" +/* +** \brief Join null-terminated array of strings and free the array +** \param strs Array of strings +** \param delim String which will be put between each string +** \return Joined string or NULL on error +*/ + char *ft_strsjoinf(char **strs, char *delim) { char *ret; -- cgit