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/Makefile b/Makefile index b7f2ab5..37e2663 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,8 @@ MEDIUM_DIR = medium COMPLEX_DIR = radix +FLAGS_DIR = flags + INCLUDES = headers #============================ @@ -26,7 +28,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 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/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 0e09104..5b78da6 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; 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/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..31e0811 --- /dev/null +++ b/flags/flag.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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..90130e2 --- /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..72ac070 100644 --- a/headers/parsing.h +++ b/headers/parsing.h @@ -13,8 +13,8 @@ #ifndef PARSING_H # define PARSING_H -int ft_atoi(const char *nptr); -t_stacks *init_big_stacks2(int *tab, int len); +int ft_atoi(const char *nptr); 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 f47f445..516b731 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/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); -} diff --git a/stack_utils/stack_remove.c b/stack_utils/stack_remove.c index cf23ef8..a8561d3 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); +} diff --git a/test_one.c b/test_one.c index 21ffbeb..7c47995 100644 --- a/test_one.c +++ b/test_one.c @@ -29,10 +29,7 @@ int test1(int argc, char **argv) //bucket_algo(piles, preset, range_bucket(piles->a)); radix(piles); } - 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); }