aboutsummaryrefslogtreecommitdiff
path: root/src/algo
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
commit5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde (patch)
tree80911dc3c32e9f230750e7e1042d413dfb6efab2 /src/algo
parentee32953ea79616e72f5428cdf40c834714a891c9 (diff)
parentb96b82194ccad2cddbb46b77aa1962a57c47ff44 (diff)
downloadlibft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.gz
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.bz2
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.zip
Merge branch 'master' into ft_ssl
Diffstat (limited to 'src/algo')
-rw-r--r--src/algo/ft_compar_int.c9
-rw-r--r--src/algo/ft_compar_str.c25
-rw-r--r--src/algo/ft_is_set.c13
-rw-r--r--src/algo/ft_mergesort.c24
-rw-r--r--src/algo/ft_qsort.c35
-rw-r--r--src/algo/ft_reverse.c9
6 files changed, 108 insertions, 7 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;