From: Lukas Jiriste Date: Sat, 24 Feb 2024 13:13:56 +0000 (+0100) Subject: Refactor push.c so as to conform with the Norm X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=121f339202d15ce904990200102e5845074c91c1;p=42%2Fpush_swap.git Refactor push.c so as to conform with the Norm --- diff --git a/Makefile b/Makefile index 3044f30..037c673 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ CHECKSOURCES := checker.c \ stack_manipulation.c \ arg_parsing.c \ stacks_mem.c \ - is_sorted.c \ + helpers.c \ CHECKSOURCES := $(addprefix $(CHECKSRCDIR)/, $(CHECKSOURCES)) @@ -31,8 +31,9 @@ PUSHSOURCES := push.c \ stacks_mem.c \ actions.c \ prints.c \ - is_sorted.c \ + helpers.c \ prepare.c \ + find.c \ PUSHSOURCES := $(addprefix $(PUSHSRCDIR)/, $(PUSHSOURCES)) diff --git a/src/checker.h b/src/checker.h index 2b7f3d7..52e74bf 100644 --- a/src/checker.h +++ b/src/checker.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/24 12:13:51 by ljiriste #+# #+# */ -/* Updated: 2024/02/23 09:48:21 by ljiriste ### ########.fr */ +/* Updated: 2024/02/24 14:06:52 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,9 +67,11 @@ void stack_rotate(t_stack *s, int amount); void stack_pop(t_stack *s); int stack_top(t_stack *s); -int is_sorted(t_stack *s); -int parse(int argc, char **argv, t_stack *s); - +int is_sorted(t_stack *s); +int parse(int argc, char **argv, t_stack *s); void prepare_stacks(t_stacks *s, size_t ind_a, size_t ind_b); size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus); +size_t find_best(t_stacks *s, t_vec *target_a_indeces); +size_t find_target_ind(t_stack *stack, int val); +size_t find_minind(t_stack *s); #endif //CHECKER_H diff --git a/src/find.c b/src/find.c new file mode 100644 index 0000000..d64cf98 --- /dev/null +++ b/src/find.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/24 11:37:22 by ljiriste #+# #+# */ +/* Updated: 2024/02/24 14:10:54 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "checker.h" +#include "libft.h" +#include +#include + +size_t find_minind(t_stack *s) +{ + size_t minind; + size_t i; + int min; + + min = INT_MAX; + i = 0; + while (i < s->stack.size) + { + if (min >= stack_top(s)) + { + minind = i; + min = stack_top(s); + } + stack_rotate(s, 1); + ++i; + } + return (minind); +} + +static int max_or_min(t_stack *stack, int val) +{ + int max; + int min; + size_t i; + + i = 0; + max = INT_MIN; + min = INT_MAX; + while (i < stack->stack.size) + { + if (stack_top(stack) > max) + { + max = stack_top(stack); + } + if (stack_top(stack) < min) + { + min = stack_top(stack); + } + stack_rotate(stack, 1); + ++i; + } + return (val < min || val > max); +} + +size_t find_target_ind(t_stack *stack, int val) +{ + size_t i; + size_t res; + const int minind = find_minind(stack); + + i = 0; + if (max_or_min(stack, val)) + return (minind); + while (i < stack->stack.size && val < stack_top(stack)) + { + stack_rotate(stack, 1); + ++i; + } + while (i < stack->stack.size && val > stack_top(stack)) + { + stack_rotate(stack, 1); + ++i; + } + res = i; + while (i < stack->stack.size) + { + stack_rotate(stack, 1); + ++i; + } + return (res); +} + +size_t find_best(t_stacks *s, t_vec *target_a_indeces) +{ + size_t res_ind; + size_t cur; + size_t best; + size_t i; + + best = SIZE_MAX; + i = 0; + while (i < s->b.stack.size) + { + cur = cost(s, i, *(size_t *)ft_vec_access(target_a_indeces, i)); + if (cur < best) + { + res_ind = i; + best = cur; + } + ++i; + } + return (res_ind); +} diff --git a/src/helpers.c b/src/helpers.c new file mode 100644 index 0000000..ef0a1c3 --- /dev/null +++ b/src/helpers.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/22 14:22:08 by ljiriste #+# #+# */ +/* Updated: 2024/02/24 14:09:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "checker.h" +#include "libft.h" +#include + +int is_sorted(t_stack *s) +{ + const size_t start_ind = s->ind; + int prev; + int cur; + + prev = stack_top(s); + stack_rotate(s, 1); + while (s->ind != start_ind) + { + cur = stack_top(s); + if (cur <= prev) + { + s->ind = start_ind; + return (0); + } + prev = cur; + stack_rotate(s, 1); + } + return (1); +} + +// Let's split the stacks by the index of the current value +// +// a b +// | | +// | a+ | +// | | +// | | b+ +// --- | +// | | +// | | +// | | +// | a- --- +// | | +// | | b- +// | | +// +// Either we have to stack_rotate both in the same direction +// in which case the cost is the higher of those +// - max(a+, b+) or max(a-, b-) +// Or we have to stack_rotate them in the opposite direction +// in which case we have to add the moves +// - a+ + b- or a- + b+ +// +// The minimum cost is the lowest of these four values +size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus) +{ + size_t res; + const size_t a_minus = s->a.stack.size - a_plus; + const size_t b_minus = s->b.stack.size - b_plus; + + res = ft_maxs(a_plus, b_plus); + res = ft_mins(res, ft_maxs(a_minus, b_minus)); + res = ft_mins(res, a_plus + b_minus); + res = ft_mins(res, a_minus + b_plus); + return (res); +} diff --git a/src/is_sorted.c b/src/is_sorted.c deleted file mode 100644 index 15e2a88..0000000 --- a/src/is_sorted.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* is_sorted.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: ljiriste +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/22 14:22:08 by ljiriste #+# #+# */ -/* Updated: 2024/02/23 10:58:13 by ljiriste ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "checker.h" -#include - -int is_sorted(t_stack *s) -{ - const size_t start_ind = s->ind; - int prev; - int cur; - - prev = stack_top(s); - stack_rotate(s, 1); - while (s->ind != start_ind) - { - cur = stack_top(s); - if (cur <= prev) - { - s->ind = start_ind; - return (0); - } - prev = cur; - stack_rotate(s, 1); - } - return (1); -} diff --git a/src/prepare.c b/src/prepare.c index 51e4c1f..f67b960 100644 --- a/src/prepare.c +++ b/src/prepare.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/23 09:35:45 by ljiriste #+# #+# */ -/* Updated: 2024/02/23 09:54:39 by ljiriste ### ########.fr */ +/* Updated: 2024/02/24 11:29:15 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ static void prepare_plus_together(t_stacks *s, size_t rot_a, size_t rot_b) i = 0; while (i < ft_mins(rot_a, rot_b)) { - print_rotate(s, both); + print_rotate(s, both); ++i; } if (rot_a > rot_b) @@ -50,7 +50,7 @@ static void prepare_minus_together(t_stacks *s, size_t rot_a, size_t rot_b) i = 0; while (i < ft_mins(rot_a, rot_b)) { - print_reverse_rotate(s, both); + print_reverse_rotate(s, both); ++i; } if (rot_a > rot_b) @@ -117,7 +117,8 @@ void prepare_stacks(t_stacks *s, size_t ind_a, size_t ind_b) if (c == ft_maxs(ind_a, ind_b)) prepare_plus_together(s, ind_a, ind_b); else if (c == ft_maxs(s->a.stack.size - ind_a, s->b.stack.size - ind_b)) - prepare_minus_together(s, s->a.stack.size - ind_a, s->b.stack.size - ind_b); + prepare_minus_together(s, s->a.stack.size - ind_a, + s->b.stack.size - ind_b); else if (c == ind_a + (s->b.stack.size - ind_b)) prepare_a_plus_against(s, ind_a, s->b.stack.size - ind_b); else diff --git a/src/push.c b/src/push.c index c3b67a9..fcf5b9d 100644 --- a/src/push.c +++ b/src/push.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/24 13:17:32 by ljiriste #+# #+# */ -/* Updated: 2024/02/24 11:15:29 by ljiriste ### ########.fr */ +/* Updated: 2024/02/24 14:10:54 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ #include "checker.h" #include "libft.h" #include +#include void a_sort_three_or_less(t_stacks *s) { @@ -30,7 +31,7 @@ void a_sort_three_or_less(t_stacks *s) last = stack_top(&s->a); max = INT_MIN; steps_to_smaller = 0; - while (i < s->a.stack.size) + while (i < s->a.stack.size) { stack_rotate(&s->a, 1); steps_to_smaller += (stack_top(&s->a) < last); @@ -44,139 +45,6 @@ void a_sort_three_or_less(t_stacks *s) return ; } -size_t find_minind(t_stack *s) -{ - size_t minind; - size_t i; - int min; - - min = INT_MAX; - i = 0; - while (i < s->stack.size) - { - if (min >= stack_top(s)) - { - minind = i; - min = stack_top(s); - } - stack_rotate(s, 1); - ++i; - } - return (minind); -} - -int max_or_min(t_stack *stack, int val) -{ - int max; - int min; - size_t i; - - i = 0; - max = INT_MIN; - min = INT_MAX; - while (i < stack->stack.size) - { - if (stack_top(stack) > max) - { - max = stack_top(stack); - } - if (stack_top(stack) < min) - { - min = stack_top(stack); - } - stack_rotate(stack, 1); - ++i; - } - return (val < min || val > max); -} - -size_t find_target_ind(t_stack *stack, int val) -{ - size_t i; - size_t res; - const int minind = find_minind(stack); - - i = 0; - if (max_or_min(stack, val)) - return (minind); - while (i < stack->stack.size && val < stack_top(stack)) - { - stack_rotate(stack, 1); - ++i; - } - while (i < stack->stack.size && val > stack_top(stack)) - { - stack_rotate(stack, 1); - ++i; - } - res = i; - while (i < stack->stack.size) - { - stack_rotate(stack, 1); - ++i; - } - return (res); -} - -// Let's split the stacks by the index of the current value -// -// a b -// | | -// | a+ | -// | | -// | | b+ -// --- | -// | | -// | | -// | | -// | a- --- -// | | -// | | b- -// | | -// -// Either we have to stack_rotate both in the same direction -// in which case the cost is the higher of those -// - max(a+, b+) or max(a-, b-) -// Or we have to stack_rotate them in the opposite direction -// in which case we have to add the moves -// - a+ + b- or a- + b+ -// -// The minimum cost is the lowest of these four values -size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus) -{ - size_t res; - const size_t a_minus = s->a.stack.size - a_plus; - const size_t b_minus = s->b.stack.size - b_plus; - - res = ft_maxs(a_plus, b_plus); - res = ft_mins(res, ft_maxs(a_minus, b_minus)); - res = ft_mins(res, a_plus + b_minus); - res = ft_mins(res, a_minus + b_plus); - return (res); -} - -size_t find_best(t_stacks *s, t_vec *target_a_indeces) -{ - size_t res_ind; - size_t cur; - size_t best; - size_t i; - - best = SIZE_MAX; - i = 0; - while (i < s->b.stack.size) - { - cur = cost(s, i, *(size_t *)ft_vec_access(target_a_indeces, i)); - if (cur < best) - { - res_ind = i; - best = cur; - } - ++i; - } - return (res_ind); -} - void get_push_indeces(t_stacks *s, int *ind_a, int *ind_b) { t_vec target_a_indeces;