From 0d9b46c748f9454bf8e78a24a84a06c223c307b3 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 7 Jan 2026 12:03:57 +0100 Subject: [PATCH 1/4] fix calcul_range function cause i didnt manage to scan the last value on the pile --- .gitignore | 2 +- algorithms/medium/utils_medium.c | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index df7cd48..3a271bd 100644 --- a/.gitignore +++ b/.gitignore @@ -58,7 +58,7 @@ dkms.conf *.swp # Executable -pushswap +push_swap # File obj obj/ diff --git a/algorithms/medium/utils_medium.c b/algorithms/medium/utils_medium.c index 0e09104..37e8c86 100644 --- a/algorithms/medium/utils_medium.c +++ b/algorithms/medium/utils_medium.c @@ -32,11 +32,14 @@ int get_next_lower(t_stack *first, int old_lower) { t_stack *tmp; int next_lower; + int skip_first; tmp = first; + skip_first = 1; next_lower = 2147483647; - while (tmp->next != first) + while (tmp != first || skip_first) { + skip_first = 0; if (old_lower < tmp->value && tmp->value <= next_lower) { next_lower = tmp->value; @@ -50,6 +53,7 @@ int calcul_range(int value, int range) { int max_range; + max_range = 0; if (value < 0) while (max_range > value) From 93e8c10b27c9703626a44bc8026f72a6d893a55f Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 7 Jan 2026 13:13:30 +0100 Subject: [PATCH 2/4] adding the flags --- Makefile | 6 +++++- flags/algorithms_sort.c | 42 ++++++++++++++++++++++++++++++++++++++ flags/flag.c | 29 ++++++++++++++++++++++++++ headers/flags.h | 22 ++++++++++++++++++++ headers/parsing.h | 1 + headers/push_swap.h | 1 + parsing/ft_strncmp.c | 25 +++++++++++++++++++++++ stack_utils/stack_remove.c | 9 ++++++++ 8 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 flags/algorithms_sort.c create mode 100644 flags/flag.c create mode 100644 headers/flags.h create mode 100644 parsing/ft_strncmp.c diff --git a/Makefile b/Makefile index 4d233cf..9d20870 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,8 @@ PARS_DIR = parsing MEDIUM_DIR = medium +FLAGS_DIR = flags + INCLUDES = headers #============================ @@ -24,7 +26,9 @@ SRC = main.c test_one.c INSERTION = insertion.c -PARSING = ft_atoi.c parsing.c parsing_2.c +FLAGS_FILES = algorithms_sort.c flag.c + +PARSING = ft_atoi.c parsing.c parsing_2.c ft_strncmp.c STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c diff --git a/flags/algorithms_sort.c b/flags/algorithms_sort.c new file mode 100644 index 0000000..a0f9b28 --- /dev/null +++ b/flags/algorithms_sort.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* algorithms_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/07 12:15:02 by mteriier #+# #+# */ +/* Updated: 2026/01/07 12:15:05 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include "medium_algo.h" + +void simple(t_stacks *piles) +{ + int len; + + len = stack_a_len(piles); + insertion(piles, len); +} + +void medium(t_stacks *piles) +{ + t_tab *buckets; + int range; + + range = range_bucket(piles->a); + buckets = get_tabs(piles->a, range); + bucket_algo(piles, buckets, range); +} + +void complex(t_stacks *piles) +{ + return ; +} + +void adaptive(t_stacks *piles) +{ + return ; +} diff --git a/flags/flag.c b/flags/flag.c new file mode 100644 index 0000000..55c20eb --- /dev/null +++ b/flags/flag.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* flag.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/07 12:39:29 by mteriier #+# #+# */ +/* Updated: 2026/01/07 12:39:31 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include "parsing.h" + +void flags(int pos, char **argv, t_stacks *piles) +{ + if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0) + simple(piles); + else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0) + medium(piles); + else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0) + complex(piles); + else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0) + adaptive(piles); + else + adaptive(piles); + +} diff --git a/headers/flags.h b/headers/flags.h new file mode 100644 index 0000000..b7b82a7 --- /dev/null +++ b/headers/flags.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* flags.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/07 13:05:52 by mteriier #+# #+# */ +/* Updated: 2026/01/07 13:05:53 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FLAGS_H +# define FLAGS_H + +void simple(t_stacks *piles); +void medium(t_stacks *piles); +void complex(t_stacks *piles); +void adaptive(t_stacks *piles); +void flags(int pos, char **argv, t_stacks *piles); + +#endif diff --git a/headers/parsing.h b/headers/parsing.h index a42d78e..d5f4358 100644 --- a/headers/parsing.h +++ b/headers/parsing.h @@ -16,5 +16,6 @@ int ft_atoi(const char *nptr); t_stacks *init_big_stacks2(int *tab, int len); t_stacks *init_big_stacks(int argc, char **argv); +int ft_strncmp(const char *s1, const char *s2, int n); #endif diff --git a/headers/push_swap.h b/headers/push_swap.h index f38f1b3..b358062 100644 --- a/headers/push_swap.h +++ b/headers/push_swap.h @@ -45,6 +45,7 @@ t_stack *new_stack(int value); void stack_add_back(t_stack **stack, t_stack *new); void stack_add_front(t_stack **stack, t_stack *new); void stack_clear_all(t_stack *stack, t_stack *first); +void free_all(t_stacks *piles); /* STACKS LEN FILES */ int stack_a_len(t_stacks *stacks); int stack_b_len(t_stacks *stacks); diff --git a/parsing/ft_strncmp.c b/parsing/ft_strncmp.c new file mode 100644 index 0000000..67f36b5 --- /dev/null +++ b/parsing/ft_strncmp.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mteriier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/07 12:36:38 by mteriier #+# #+# */ +/* Updated: 2026/01/07 12:36:40 by mteriier ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +int ft_strncmp(const char *s1, const char *s2, int n) +{ + int i; + + i = 0; + if (n == 0) + return (0); + while (i < n - 1 && s1[i] && s1[i] == s2[i]) + { + i++; + } + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} diff --git a/stack_utils/stack_remove.c b/stack_utils/stack_remove.c index cf23ef8..d53c3a8 100644 --- a/stack_utils/stack_remove.c +++ b/stack_utils/stack_remove.c @@ -21,3 +21,12 @@ void stack_clear_all(t_stack *stack, t_stack *first) stack_clear_all(stack->next, first); free(stack); } + +void free_all(t_stacks *piles) +{ + if (piles->a) + stack_clear_all(piles->a, piles->a); + if (piles->b) + stack_clear_all(piles->b, piles->b); + free(piles); +} From e1351b4d2fcc4b87852b43cf5c3c9bfc831e4dd1 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 7 Jan 2026 13:23:42 +0100 Subject: [PATCH 3/4] doing the norm --- algorithms/insertion/insertion.c | 3 ++- algorithms/medium/sort_utils.c | 2 +- algorithms/medium/utils_medium.c | 1 - algorithms/medium/utils_medium_two.c | 2 +- flags/flag.c | 1 - headers/flags.h | 2 +- headers/parsing.h | 4 ++-- stack_utils/stack_remove.c | 2 +- test_one.c | 5 +---- 9 files changed, 9 insertions(+), 13 deletions(-) diff --git a/algorithms/insertion/insertion.c b/algorithms/insertion/insertion.c index f5fe9ca..2b403f4 100644 --- a/algorithms/insertion/insertion.c +++ b/algorithms/insertion/insertion.c @@ -24,7 +24,8 @@ static int to_insert(t_stacks *stacks, int sorted) a = stacks->a; while (i < sorted) { - if (stacks->b->value > a->previous->value && stacks->b->value <= a->value) + if (stacks->b->value > a->previous->value + && stacks->b->value <= a->value) return (i); a = a->previous; i++; diff --git a/algorithms/medium/sort_utils.c b/algorithms/medium/sort_utils.c index afea4b4..66424fa 100644 --- a/algorithms/medium/sort_utils.c +++ b/algorithms/medium/sort_utils.c @@ -92,4 +92,4 @@ void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range) sort_little_pile(piles); one_preset->nb_in--; } -} \ No newline at end of file +} diff --git a/algorithms/medium/utils_medium.c b/algorithms/medium/utils_medium.c index 37e8c86..5b78da6 100644 --- a/algorithms/medium/utils_medium.c +++ b/algorithms/medium/utils_medium.c @@ -53,7 +53,6 @@ int calcul_range(int value, int range) { int max_range; - max_range = 0; if (value < 0) while (max_range > value) diff --git a/algorithms/medium/utils_medium_two.c b/algorithms/medium/utils_medium_two.c index 8ff6ad6..70d91ed 100644 --- a/algorithms/medium/utils_medium_two.c +++ b/algorithms/medium/utils_medium_two.c @@ -64,7 +64,7 @@ int range_bucket(t_stack *first) int sqrt; len = stack_len(first); - diff = (get_max_number(first) - get_min_number(first)) ; + diff = get_max_number(first) - get_min_number(first); sqrt = my_sqrt(len); if (diff / sqrt < 2) { diff --git a/flags/flag.c b/flags/flag.c index 55c20eb..31e0811 100644 --- a/flags/flag.c +++ b/flags/flag.c @@ -25,5 +25,4 @@ void flags(int pos, char **argv, t_stacks *piles) adaptive(piles); else adaptive(piles); - } diff --git a/headers/flags.h b/headers/flags.h index b7b82a7..90130e2 100644 --- a/headers/flags.h +++ b/headers/flags.h @@ -13,7 +13,7 @@ #ifndef FLAGS_H # define FLAGS_H -void simple(t_stacks *piles); +void simple(t_stacks *piles); void medium(t_stacks *piles); void complex(t_stacks *piles); void adaptive(t_stacks *piles); diff --git a/headers/parsing.h b/headers/parsing.h index d5f4358..5a59a8d 100644 --- a/headers/parsing.h +++ b/headers/parsing.h @@ -13,9 +13,9 @@ #ifndef PARSING_H # define PARSING_H -int ft_atoi(const char *nptr); +int ft_atoi(const char *nptr); t_stacks *init_big_stacks2(int *tab, int len); t_stacks *init_big_stacks(int argc, char **argv); -int ft_strncmp(const char *s1, const char *s2, int n); +int ft_strncmp(const char *s1, const char *s2, int n); #endif diff --git a/stack_utils/stack_remove.c b/stack_utils/stack_remove.c index d53c3a8..a8561d3 100644 --- a/stack_utils/stack_remove.c +++ b/stack_utils/stack_remove.c @@ -28,5 +28,5 @@ void free_all(t_stacks *piles) stack_clear_all(piles->a, piles->a); if (piles->b) stack_clear_all(piles->b, piles->b); - free(piles); + free(piles); } diff --git a/test_one.c b/test_one.c index c710be1..7ea1979 100644 --- a/test_one.c +++ b/test_one.c @@ -28,10 +28,7 @@ int test1(int argc, char **argv) preset = get_tabs(piles->a, range_bucket(piles->a)); bucket_algo(piles, preset, range_bucket(piles->a)); } - if (piles->a) - stack_clear_all(piles->a, piles->a); - if (piles->b) - stack_clear_all(piles->b, piles->b); + free_all(piles); free(piles); return (0); } From 17f9a5affdf095618e2a240ed01bc8a31d7172b5 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 7 Jan 2026 13:27:06 +0100 Subject: [PATCH 4/4] delete the parsing2.c cause no use for now --- Makefile | 2 +- headers/parsing.h | 1 - parsing/parsing_2.c | 55 --------------------------------------------- 3 files changed, 1 insertion(+), 57 deletions(-) delete mode 100644 parsing/parsing_2.c diff --git a/Makefile b/Makefile index 9d20870..9911a0a 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ INSERTION = insertion.c FLAGS_FILES = algorithms_sort.c flag.c -PARSING = ft_atoi.c parsing.c parsing_2.c ft_strncmp.c +PARSING = ft_atoi.c parsing.c ft_strncmp.c STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c diff --git a/headers/parsing.h b/headers/parsing.h index 5a59a8d..72ac070 100644 --- a/headers/parsing.h +++ b/headers/parsing.h @@ -14,7 +14,6 @@ # define PARSING_H int ft_atoi(const char *nptr); -t_stacks *init_big_stacks2(int *tab, int len); t_stacks *init_big_stacks(int argc, char **argv); int ft_strncmp(const char *s1, const char *s2, int n); diff --git a/parsing/parsing_2.c b/parsing/parsing_2.c deleted file mode 100644 index b1f653f..0000000 --- a/parsing/parsing_2.c +++ /dev/null @@ -1,55 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parsing_2.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: mteriier +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/12/22 13:10:58 by mteriier #+# #+# */ -/* Updated: 2025/12/22 13:11:21 by mteriier ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" -#include - -static t_stack *parsing2(int *tab, int len) -{ - int i; - int stock; - t_stack *first; - t_stack *new; - - i = 0; - first = NULL; - while (i < len) - { - stock = tab[i]; - new = new_stack(stock); - if (!new && !first) - return (NULL); - else if (!new) - { - stack_clear_all(first, first); - return (NULL); - } - stack_add_back(&first, new); - i++; - } - return (first); -} - -t_stacks *init_big_stacks2(int *tab, int len) -{ - t_stacks *stacks; - t_stack *a; - - stacks = malloc(sizeof(t_stacks)); - stacks->a = NULL; - stacks->b = NULL; - if (!stacks) - return (NULL); - a = parsing2(tab, len); - stacks->a = a; - return (stacks); -}