mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
replacing piles by stacks
This commit is contained in:
@@ -14,48 +14,48 @@
|
|||||||
#include "medium_headers.h"
|
#include "medium_headers.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static int path_to_end(t_stacks *piles, int max_range, int range, char c)
|
static int path_to_end(t_stacks *stacks, int max_range, int range, char c)
|
||||||
{
|
{
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
int i;
|
int i;
|
||||||
int first_pass;
|
int first_pass;
|
||||||
|
|
||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
tmp = piles->a;
|
tmp = stacks->a;
|
||||||
else
|
else
|
||||||
tmp = piles->b;
|
tmp = stacks->b;
|
||||||
tmp = tmp->previous;
|
tmp = tmp->previous;
|
||||||
i = 0;
|
i = 0;
|
||||||
first_pass = 1;
|
first_pass = 1;
|
||||||
while (first_pass || tmp != piles->b->previous)
|
while (first_pass || tmp != stacks->b->previous)
|
||||||
{
|
{
|
||||||
first_pass = 0;
|
first_pass = 0;
|
||||||
if (in_range(tmp->value, max_range, range))
|
if (in_range(tmp->value, max_range, range))
|
||||||
tmp = piles->b;
|
tmp = stacks->b;
|
||||||
tmp = tmp->previous;
|
tmp = tmp->previous;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int path_to_start(t_stacks *piles, int max_range, int range, char c)
|
static int path_to_start(t_stacks *stacks, int max_range, int range, char c)
|
||||||
{
|
{
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
int i;
|
int i;
|
||||||
int first_pass;
|
int first_pass;
|
||||||
|
|
||||||
if (c == 'a')
|
if (c == 'a')
|
||||||
tmp = piles->a;
|
tmp = stacks->a;
|
||||||
else
|
else
|
||||||
tmp = piles->b;
|
tmp = stacks->b;
|
||||||
i = 0;
|
i = 0;
|
||||||
first_pass = 1;
|
first_pass = 1;
|
||||||
while (first_pass || tmp != piles->b)
|
while (first_pass || tmp != stacks->b)
|
||||||
{
|
{
|
||||||
first_pass = 0;
|
first_pass = 0;
|
||||||
if (in_range(tmp->value, max_range, range))
|
if (in_range(tmp->value, max_range, range))
|
||||||
{
|
{
|
||||||
tmp = piles->b->previous;
|
tmp = stacks->b->previous;
|
||||||
}
|
}
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
i++;
|
i++;
|
||||||
@@ -63,13 +63,13 @@ static int path_to_start(t_stacks *piles, int max_range, int range, char c)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int wich_path(t_stacks *piles, int max_range, int range, char c)
|
int wich_path(t_stacks *stacks, int max_range, int range, char c)
|
||||||
{
|
{
|
||||||
int path_start;
|
int path_start;
|
||||||
int path_end;
|
int path_end;
|
||||||
|
|
||||||
path_start = path_to_start(piles, max_range, range, c);
|
path_start = path_to_start(stacks, max_range, range, c);
|
||||||
path_end = path_to_end(piles, max_range, range, c);
|
path_end = path_to_end(stacks, max_range, range, c);
|
||||||
if (path_start < path_end)
|
if (path_start < path_end)
|
||||||
return (1);
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
@@ -90,16 +90,16 @@ int stack_len(t_stack *stack)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void bucket_algo(t_stacks *piles, t_tab *preset, int range)
|
void bucket_algo(t_stacks *stacks, t_tab *preset, int range)
|
||||||
{
|
{
|
||||||
t_tab *tmp;
|
t_tab *tmp;
|
||||||
|
|
||||||
tmp = preset;
|
tmp = preset;
|
||||||
while (piles->a)
|
while (stacks->a)
|
||||||
pb(piles);
|
pb(stacks);
|
||||||
while (preset)
|
while (preset)
|
||||||
{
|
{
|
||||||
push_range_to_b(piles, preset, range);
|
push_range_to_b(stacks, preset, range);
|
||||||
if (preset->next)
|
if (preset->next)
|
||||||
tmp = preset->next;
|
tmp = preset->next;
|
||||||
else
|
else
|
||||||
@@ -108,7 +108,7 @@ void bucket_algo(t_stacks *piles, t_tab *preset, int range)
|
|||||||
free(preset);
|
free(preset);
|
||||||
preset = tmp;
|
preset = tmp;
|
||||||
}
|
}
|
||||||
while (piles->b)
|
while (stacks->b)
|
||||||
pa(piles);
|
pa(stacks);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,13 @@
|
|||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
#include "medium_headers.h"
|
#include "medium_headers.h"
|
||||||
|
|
||||||
static int number_move_reverse(t_stacks *piles, int value, char start_end)
|
static int number_move_reverse(t_stacks *stacks, int value, char start_end)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
tmp = piles->a;
|
tmp = stacks->a;
|
||||||
if (start_end == 's')
|
if (start_end == 's')
|
||||||
{
|
{
|
||||||
while (tmp->value < value)
|
while (tmp->value < value)
|
||||||
@@ -40,56 +40,56 @@ static int number_move_reverse(t_stacks *piles, int value, char start_end)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int sort_path(t_stacks *piles, int value)
|
static int sort_path(t_stacks *stacks, int value)
|
||||||
{
|
{
|
||||||
int start_path;
|
int start_path;
|
||||||
int end_path;
|
int end_path;
|
||||||
|
|
||||||
start_path = number_move_reverse(piles, value, 's');
|
start_path = number_move_reverse(stacks, value, 's');
|
||||||
if (start_path == 0)
|
if (start_path == 0)
|
||||||
return (1);
|
return (1);
|
||||||
end_path = number_move_reverse(piles, value, 'e');
|
end_path = number_move_reverse(stacks, value, 'e');
|
||||||
if (start_path < end_path)
|
if (start_path < end_path)
|
||||||
return (1);
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sort_little_pile(t_stacks *piles)
|
static void sort_little_pile(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
if (!piles->a)
|
if (!stacks->a)
|
||||||
{
|
{
|
||||||
pa(piles);
|
pa(stacks);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
if (piles->a->previous->value < piles->b->value)
|
if (stacks->a->previous->value < stacks->b->value)
|
||||||
{
|
{
|
||||||
pa(piles);
|
pa(stacks);
|
||||||
ra(piles);
|
ra(stacks);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
if (sort_path(piles, piles->b->value))
|
if (sort_path(stacks, stacks->b->value))
|
||||||
sort_from_left(piles);
|
sort_from_left(stacks);
|
||||||
else
|
else
|
||||||
sort_from_right(piles);
|
sort_from_right(stacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range)
|
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range)
|
||||||
{
|
{
|
||||||
while (one_preset->nb_in > 0)
|
while (one_preset->nb_in > 0)
|
||||||
{
|
{
|
||||||
if (wich_path(piles, one_preset->max_range, range, 'b'))
|
if (wich_path(stacks, one_preset->max_range, range, 'b'))
|
||||||
{
|
{
|
||||||
while (!in_range(piles->b->value, one_preset->max_range, range))
|
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||||
rb(piles);
|
rb(stacks);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (!in_range(piles->b->value, one_preset->max_range, range))
|
while (!in_range(stacks->b->value, one_preset->max_range, range))
|
||||||
{
|
{
|
||||||
rrb(piles);
|
rrb(stacks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort_little_pile(piles);
|
sort_little_pile(stacks);
|
||||||
one_preset->nb_in--;
|
one_preset->nb_in--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,38 +12,38 @@
|
|||||||
|
|
||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
void sort_from_left(t_stacks *piles)
|
void sort_from_left(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (piles->b->value > piles->a->value)
|
while (stacks->b->value > stacks->a->value)
|
||||||
{
|
{
|
||||||
ra(piles);
|
ra(stacks);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
pa(piles);
|
pa(stacks);
|
||||||
while (i > 0)
|
while (i > 0)
|
||||||
{
|
{
|
||||||
rra(piles);
|
rra(stacks);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sort_from_right(t_stacks *piles)
|
void sort_from_right(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (piles->b->value < piles->a->previous->value)
|
while (stacks->b->value < stacks->a->previous->value)
|
||||||
{
|
{
|
||||||
rra(piles);
|
rra(stacks);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
pa(piles);
|
pa(stacks);
|
||||||
while (i >= 0)
|
while (i >= 0)
|
||||||
{
|
{
|
||||||
ra(piles);
|
ra(stacks);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,41 +13,41 @@
|
|||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
#include "medium_headers.h"
|
#include "medium_headers.h"
|
||||||
|
|
||||||
void simple(t_stacks *piles)
|
void simple(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
len = stack_a_len(piles);
|
len = stack_a_len(stacks);
|
||||||
piles->algo = 1;
|
stacks->algo = 1;
|
||||||
insertion(piles, len);
|
insertion(stacks, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void medium(t_stacks *piles)
|
void medium(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
t_tab *buckets;
|
t_tab *buckets;
|
||||||
int range;
|
int range;
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
piles->algo = 2;
|
stacks->algo = 2;
|
||||||
len = stack_a_len(piles);
|
len = stack_a_len(stacks);
|
||||||
if (len == 2)
|
if (len == 2)
|
||||||
sort_two(piles);
|
sort_two(stacks);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
range = range_bucket(piles->a);
|
range = range_bucket(stacks->a);
|
||||||
buckets = get_tabs(piles->a, range);
|
buckets = get_tabs(stacks->a, range);
|
||||||
bucket_algo(piles, buckets, range);
|
bucket_algo(stacks, buckets, range);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void complex(t_stacks *piles)
|
void complex(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
piles->algo = 3;
|
stacks->algo = 3;
|
||||||
radix(piles);
|
radix(stacks);
|
||||||
}
|
}
|
||||||
|
|
||||||
void adaptive(t_stacks *piles)
|
void adaptive(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
(void)piles;
|
(void)stacks;
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|||||||
14
flags/flag.c
14
flags/flag.c
@@ -93,18 +93,18 @@ int pos_bench(char **argv, int mod)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void flags(int pos, int pos_b, char **argv, t_stacks *piles)
|
void flags(int pos, int pos_b, char **argv, t_stacks *stacks)
|
||||||
{
|
{
|
||||||
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
|
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
|
||||||
simple(piles);
|
simple(stacks);
|
||||||
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
|
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
|
||||||
medium(piles);
|
medium(stacks);
|
||||||
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
|
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
|
||||||
complex(piles);
|
complex(stacks);
|
||||||
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
|
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
|
||||||
adaptive(piles);
|
adaptive(stacks);
|
||||||
else
|
else
|
||||||
adaptive(piles);
|
adaptive(stacks);
|
||||||
if (pos_b > 0)
|
if (pos_b > 0)
|
||||||
piles->bench = 1;
|
stacks->bench = 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,11 @@
|
|||||||
#ifndef FLAGS_H
|
#ifndef FLAGS_H
|
||||||
# define FLAGS_H
|
# define FLAGS_H
|
||||||
|
|
||||||
void simple(t_stacks *piles);
|
void simple(t_stacks *stacks);
|
||||||
void medium(t_stacks *piles);
|
void medium(t_stacks *stacks);
|
||||||
void complex(t_stacks *piles);
|
void complex(t_stacks *stacks);
|
||||||
void adaptive(t_stacks *piles);
|
void adaptive(t_stacks *stacks);
|
||||||
void flags(int pos, int pos_b, char **argv, t_stacks *piles);
|
void flags(int pos, int pos_b, char **argv, t_stacks *stacks);
|
||||||
int pos_bench(char **argv, int mod);
|
int pos_bench(char **argv, int mod);
|
||||||
int pos_flag(char **argv, int mod);
|
int pos_flag(char **argv, int mod);
|
||||||
int calcul_mod(int argc, char **argv);
|
int calcul_mod(int argc, char **argv);
|
||||||
|
|||||||
@@ -21,13 +21,13 @@ typedef struct s_tab
|
|||||||
} t_tab;
|
} t_tab;
|
||||||
|
|
||||||
/* MEDIUM ALGO FILE */
|
/* MEDIUM ALGO FILE */
|
||||||
int wich_path(t_stacks *piles, int max_range, int range, char c);
|
int wich_path(t_stacks *stacks, int max_range, int range, char c);
|
||||||
int stack_len(t_stack *stack);
|
int stack_len(t_stack *stack);
|
||||||
void bucket_algo(t_stacks *piles, t_tab *preset, int range);
|
void bucket_algo(t_stacks *stacks, t_tab *preset, int range);
|
||||||
/* SORT UTILS FILES */
|
/* SORT UTILS FILES */
|
||||||
void sort_from_left(t_stacks *piles);
|
void sort_from_left(t_stacks *stacks);
|
||||||
void sort_from_right(t_stacks *piles);
|
void sort_from_right(t_stacks *stacks);
|
||||||
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range);
|
void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range);
|
||||||
/* MEDIUM UTILS FILES */
|
/* MEDIUM UTILS FILES */
|
||||||
int range_bucket(t_stack *first);
|
int range_bucket(t_stack *first);
|
||||||
int get_first_lower(t_stack *first);
|
int get_first_lower(t_stack *first);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
# include "push_swap.h"
|
# include "push_swap.h"
|
||||||
|
|
||||||
int ft_atoi(const char *nptr);
|
int ft_atoi(const char *nptr);
|
||||||
t_stacks *init_piles(int argc, char **argv, int mod);
|
t_stacks *init_stacks(int argc, char **argv, int mod);
|
||||||
int ft_strncmp(const char *s1, const char *s2, int n);
|
int ft_strncmp(const char *s1, const char *s2, int n);
|
||||||
size_t ft_strlen(const char *s);
|
size_t ft_strlen(const char *s);
|
||||||
char *ft_substr(char const *s, unsigned int start, size_t len);
|
char *ft_substr(char const *s, unsigned int start, size_t len);
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ t_stack *new_stack(int value);
|
|||||||
void stack_add_back(t_stack **stack, t_stack *new);
|
void stack_add_back(t_stack **stack, t_stack *new);
|
||||||
void stack_add_front(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 stack_clear_all(t_stack *stack, t_stack *first);
|
||||||
void free_all(t_stacks *piles);
|
void free_all(t_stacks *stacks);
|
||||||
/* STACKS LEN FILES */
|
/* STACKS LEN FILES */
|
||||||
int stack_a_len(t_stacks *stacks);
|
int stack_a_len(t_stacks *stacks);
|
||||||
int stack_b_len(t_stacks *stacks);
|
int stack_b_len(t_stacks *stacks);
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ static void set_t_stacks(t_stacks *stacks)
|
|||||||
stacks->rrr = 0;
|
stacks->rrr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
t_stacks *init_piles(int argc, char **argv, int mod)
|
t_stacks *init_stacks(int argc, char **argv, int mod)
|
||||||
{
|
{
|
||||||
t_stacks *stacks;
|
t_stacks *stacks;
|
||||||
t_stack *a;
|
t_stack *a;
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ void stack_clear_all(t_stack *stack, t_stack *first)
|
|||||||
free(stack);
|
free(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_all(t_stacks *piles)
|
void free_all(t_stacks *stacks)
|
||||||
{
|
{
|
||||||
if (piles->a)
|
if (stacks->a)
|
||||||
stack_clear_all(piles->a, piles->a);
|
stack_clear_all(stacks->a, stacks->a);
|
||||||
if (piles->b)
|
if (stacks->b)
|
||||||
stack_clear_all(piles->b, piles->b);
|
stack_clear_all(stacks->b, stacks->b);
|
||||||
free(piles);
|
free(stacks);
|
||||||
}
|
}
|
||||||
|
|||||||
18
test_one.c
18
test_one.c
@@ -19,21 +19,21 @@
|
|||||||
|
|
||||||
int test1(int argc, char **argv)
|
int test1(int argc, char **argv)
|
||||||
{
|
{
|
||||||
t_stacks *piles;
|
t_stacks *stacks;
|
||||||
int mod;
|
int mod;
|
||||||
|
|
||||||
piles = NULL;
|
stacks = NULL;
|
||||||
mod = calcul_mod(argc, argv);
|
mod = calcul_mod(argc, argv);
|
||||||
if (mod == -1)
|
if (mod == -1)
|
||||||
return (0);
|
return (0);
|
||||||
piles = init_piles(argc, argv, mod);
|
stacks = init_stacks(argc, argv, mod);
|
||||||
if (!piles)
|
if (!stacks)
|
||||||
return (0);
|
return (0);
|
||||||
if (check_order(piles->a))
|
if (check_order(stacks->a))
|
||||||
return (0);
|
return (0);
|
||||||
flags(pos_flag(argv, mod), pos_bench(argv, mod), argv, piles);
|
flags(pos_flag(argv, mod), pos_bench(argv, mod), argv, stacks);
|
||||||
if (piles->bench == 1)
|
if (stacks->bench == 1)
|
||||||
print_bench(piles);
|
print_bench(stacks);
|
||||||
free_all(piles);
|
free_all(stacks);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user