aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/algo/ft_compar_int.c18
-rw-r--r--src/algo/ft_is_set.c31
-rw-r--r--src/algo/ft_qsort.c64
-rw-r--r--src/io/ft_getchar.c22
-rw-r--r--src/lst/ft_lstreverse_ret.c2
-rw-r--r--src/mem/ft_memswap.c29
-rw-r--r--src/str/ft_atoi_strict.c2
-rw-r--r--src/str/ft_strtol.c16
8 files changed, 175 insertions, 9 deletions
diff --git a/src/algo/ft_compar_int.c b/src/algo/ft_compar_int.c
new file mode 100644
index 0000000..848dc71
--- /dev/null
+++ b/src/algo/ft_compar_int.c
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_compar_int.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 08:24:43 by cacharle #+# #+# */
+/* Updated: 2020/01/19 08:27:38 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+int ft_compar_int(const void *a, const void *b)
+{
+ return (*(int*)a - *(int*)b);
+}
diff --git a/src/algo/ft_is_set.c b/src/algo/ft_is_set.c
new file mode 100644
index 0000000..8e45b49
--- /dev/null
+++ b/src/algo/ft_is_set.c
@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_is_set.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */
+/* Updated: 2020/01/19 08:21:12 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+t_bool ft_is_set(void *base, size_t nel, size_t width,
+ t_ftcompar_func compar)
+{
+ size_t i;
+
+ if (nel < 2)
+ return (TRUE);
+ ft_qsort(base, nel, width, compar);
+ i = 0;
+ while (i < nel - 1)
+ {
+ if (compar(base + (i * width), base + ((i + 1) * width)) == 0)
+ return (FALSE);
+ i++;
+ }
+ return (TRUE);
+}
diff --git a/src/algo/ft_qsort.c b/src/algo/ft_qsort.c
new file mode 100644
index 0000000..8c125a1
--- /dev/null
+++ b/src/algo/ft_qsort.c
@@ -0,0 +1,64 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_qsort.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 07:25:51 by cacharle #+# #+# */
+/* Updated: 2020/01/19 08:28:32 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static t_ftrange ft_range_new(int lo, int hi)
+{
+ t_ftrange range;
+
+ range.lo = lo;
+ range.hi = hi;
+ return (range);
+}
+
+static int ft_qsort_partition(void *base, t_ftrange range,
+ size_t width, t_ftcompar_func compar)
+{
+ void *pivot;
+ int p;
+ int i;
+
+ pivot = base + (range.hi * width);
+ p = range.lo;
+ i = range.lo - 1;
+ while (++i < range.hi)
+ {
+ if (compar(base + (i * width), pivot) < 0)
+ {
+ ft_memswap(base + (i * width), base + (p * width), width);
+ p++;
+ }
+ }
+ ft_memswap(pivot, base + (p * width), width);
+ return (p);
+}
+
+static void ft_qsort_rec(void *base, t_ftrange range,
+ size_t width, t_ftcompar_func compar)
+{
+ int pivot;
+
+ if (range.lo >= range.hi)
+ return ;
+ pivot = ft_qsort_partition(base, range, width, compar);
+ ft_qsort_rec(base, ft_range_new(range.lo, pivot - 1), width, compar);
+ ft_qsort_rec(base, ft_range_new(pivot + 1, range.hi), width, compar);
+}
+
+void ft_qsort(void *base, size_t nel, size_t width,
+ t_ftcompar_func compar)
+{
+ if (width == 0 || nel < 2)
+ return ;
+ ft_qsort_rec(base, ft_range_new(0, nel - 1), width, compar);
+}
diff --git a/src/io/ft_getchar.c b/src/io/ft_getchar.c
new file mode 100644
index 0000000..54b44d4
--- /dev/null
+++ b/src/io/ft_getchar.c
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_getchar.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/18 10:29:54 by cacharle #+# #+# */
+/* Updated: 2020/01/18 10:46:56 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char ft_getchar(void)
+{
+ char c;
+
+ if (read(STDIN_FILENO, &c, 1) < 0)
+ return (-1);
+ return (c);
+}
diff --git a/src/lst/ft_lstreverse_ret.c b/src/lst/ft_lstreverse_ret.c
index a3edb99..c115ac5 100644
--- a/src/lst/ft_lstreverse_ret.c
+++ b/src/lst/ft_lstreverse_ret.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 12:51:15 by cacharle #+# #+# */
-/* Updated: 2020/01/17 10:12:42 by cacharle ### ########.fr */
+/* Updated: 2020/02/10 02:20:21 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/mem/ft_memswap.c b/src/mem/ft_memswap.c
new file mode 100644
index 0000000..4abaa83
--- /dev/null
+++ b/src/mem/ft_memswap.c
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_memswap.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 07:56:43 by cacharle #+# #+# */
+/* Updated: 2020/01/19 08:22:17 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_memswap(void *a, void *b, size_t size)
+{
+ t_byte tmp;
+ t_byte *cast_a;
+ t_byte *cast_b;
+
+ cast_a = (t_byte*)a;
+ cast_b = (t_byte*)b;
+ while (size-- > 0)
+ {
+ tmp = cast_a[size];
+ cast_a[size] = cast_b[size];
+ cast_b[size] = tmp;
+ }
+}
diff --git a/src/str/ft_atoi_strict.c b/src/str/ft_atoi_strict.c
index 0079807..f573593 100644
--- a/src/str/ft_atoi_strict.c
+++ b/src/str/ft_atoi_strict.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 10:06:29 by cacharle #+# #+# */
-/* Updated: 2020/01/17 10:12:54 by cacharle ### ########.fr */
+/* Updated: 2020/02/10 02:20:40 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/str/ft_strtol.c b/src/str/ft_strtol.c
index e5ce552..82276d8 100644
--- a/src/str/ft_strtol.c
+++ b/src/str/ft_strtol.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */
-/* Updated: 2020/01/17 10:54:41 by cacharle ### ########.fr */
+/* Updated: 2020/02/10 02:21:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,8 +14,10 @@
#define STRTOL_STD_BASE "0123456789abcdefghijklmnopqrstuvwxyz"
-static int strtol_handle_base(int base, const char **str)
+static int st_strtol_handle_base(int base, const char **str)
{
+ if (base > 36)
+ return (-1);
if (base != 16 && base != 0)
return (base);
if (base == 16 && **str == '0' && (*str)[1] == 'x')
@@ -37,7 +39,7 @@ static int strtol_handle_base(int base, const char **str)
return (10);
}
-static long errno_return(int err)
+static long st_errno_return(int err)
{
errno = err;
return (0);
@@ -55,17 +57,17 @@ long ft_strtol(const char *str, char **endptr, int base)
long long nb;
char base_str[37];
- if (base > 36)
- return (errno_return(EINVAL));
while (ft_isspace(*str))
str++;
is_negative = *str == '-' ? TRUE : FALSE;
if (*str == '-' || *str == '+')
str++;
- base = strtol_handle_base(base, &str);
+ if ((base = st_strtol_handle_base(base, &str)) == -1)
+ return (st_errno_return(EINVAL));
ft_strncpy(base_str, STRTOL_STD_BASE, base);
+ base_str[base] = '\0';
nb = 0;
- while (ft_strchr(base_str, *str) != base_str + base)
+ while (*str != '\0' && ft_strchr(base_str, *str) != NULL)
{
nb *= base;
nb += ft_strchr(base_str, ft_tolower(*str++)) - base_str;