diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-02-10 02:22:33 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-02-10 02:22:33 +0100 |
| commit | 48f43e34e9beafbc76a85c6f6874aae051981c72 (patch) | |
| tree | 6bc73d0537019932fd0478fc074de2d235134002 | |
| parent | 0acdc4fec5cae4e619d0f4f8bd67e171bffa110e (diff) | |
| parent | a5b068767440327f910dfa815be12ff4c0d94309 (diff) | |
| download | libft-48f43e34e9beafbc76a85c6f6874aae051981c72.tar.gz libft-48f43e34e9beafbc76a85c6f6874aae051981c72.tar.bz2 libft-48f43e34e9beafbc76a85c6f6874aae051981c72.zip | |
Merge branch 'push_swap'
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | include/libft.h | 2 | ||||
| -rw-r--r-- | include/libft_algo.h | 32 | ||||
| -rw-r--r-- | src/algo/ft_compar_int.c | 18 | ||||
| -rw-r--r-- | src/algo/ft_is_set.c | 31 | ||||
| -rw-r--r-- | src/algo/ft_qsort.c | 64 | ||||
| -rw-r--r-- | src/io/ft_getchar.c | 22 | ||||
| -rw-r--r-- | src/lst/ft_lstreverse_ret.c | 2 | ||||
| -rw-r--r-- | src/mem/ft_memswap.c | 29 | ||||
| -rw-r--r-- | src/str/ft_atoi_strict.c | 2 | ||||
| -rw-r--r-- | src/str/ft_strtol.c | 16 |
11 files changed, 211 insertions, 11 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/10/08 15:45:53 by cacharle #+# #+# # -# Updated: 2020/01/31 09:20:36 by cacharle ### ########.fr # +# Updated: 2020/02/10 02:18:58 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -22,6 +22,8 @@ OBJ_DIR = obj SCRIPT_DIR = script TEST_DIR = test +INCLUDE_DIR = include + CC = gcc CCFLAGS = -I$(INCLUDE_DIR) -Wall -Wextra -Werror diff --git a/include/libft.h b/include/libft.h index 898c7d9..74f074b 100644 --- a/include/libft.h +++ b/include/libft.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:45:02 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:51:40 by cacharle ### ########.fr */ +/* Updated: 2020/02/10 02:19:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/include/libft_algo.h b/include/libft_algo.h new file mode 100644 index 0000000..4dc3353 --- /dev/null +++ b/include/libft_algo.h @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_algo.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/19 07:22:57 by cacharle #+# #+# */ +/* Updated: 2020/02/10 02:21:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_ALGO_H +# define LIBFT_ALGO_H + +typedef int t_bool; + +typedef struct +{ + int lo; + int hi; +} t_ftrange; + +typedef int (*t_ftcompar_func)(const void*, const void*); + +t_bool ft_is_set(void *base, size_t nel, size_t width, + t_ftcompar_func compar); +void ft_qsort(void *base, size_t nel, size_t width, + t_ftcompar_func compar); +int ft_compar_int(const void *a, const void *b); + +#endif 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; |
