From: Lukas Jiriste Date: Fri, 1 Mar 2024 09:01:33 +0000 (+0100) Subject: Bring source code to compliance with the 42 Norm X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=90339fec8e79db72de3fc7865994c0228106ca87;p=42%2Fpush_swap.git Bring source code to compliance with the 42 Norm --- diff --git a/Makefile b/Makefile index a1c984c..644f05d 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,7 @@ PUSHSOURCES := push.c \ prepare.c \ find.c \ circular_lis.c \ + special_a.c \ PUSHSOURCES := $(addprefix $(PUSHSRCDIR)/, $(PUSHSOURCES)) diff --git a/src/checker.h b/src/checker.h index d9aeec0..2d09590 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/29 16:27:28 by ljiriste ### ########.fr */ +/* Updated: 2024/03/01 09:49:02 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,4 +77,7 @@ size_t binary_search(t_vec *sorted, t_stack *stack, size_t val_ind); 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); +void rotate_a_to_sort(t_stacks *s); +void isolate_lis_in_a(t_stacks *s, t_stack *lis); +void a_sort_three_or_less(t_stacks *s); #endif //CHECKER_H diff --git a/src/circular_lis.c b/src/circular_lis.c index b1ccd9b..aaef1cc 100644 --- a/src/circular_lis.c +++ b/src/circular_lis.c @@ -6,14 +6,15 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/27 15:18:21 by ljiriste #+# #+# */ -/* Updated: 2024/02/29 16:59:55 by ljiriste ### ########.fr */ +/* Updated: 2024/03/01 09:59:03 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "checker.h" #include "libft.h" -static t_arr_stat reconstruct_lis(t_stack *lis, t_stack *stack, t_vec *lowest, t_vec *predecessors) +static t_arr_stat reconstruct_lis(t_stack *lis, t_stack *stack, + t_vec *lowest, t_vec *predecessors) { size_t i; size_t j; @@ -36,7 +37,8 @@ static t_arr_stat reconstruct_lis(t_stack *lis, t_stack *stack, t_vec *lowest, t return (success); } -static t_arr_stat get_lis_loop(t_stack *stack, t_vec *inds_of_lowest, t_vec *inds_of_predecessor) +static t_arr_stat get_lis_loop(t_stack *stack, t_vec *inds_of_lowest, + t_vec *inds_of_predecessor) { size_t i; size_t new_ind; @@ -52,13 +54,14 @@ static t_arr_stat get_lis_loop(t_stack *stack, t_vec *inds_of_lowest, t_vec *ind else *(size_t *)ft_vec_access(inds_of_lowest, new_ind) = i; if (res != success) - break; + break ; if (new_ind == 0) res = ft_vec_append(inds_of_predecessor, &invalid); else - res = ft_vec_append(inds_of_predecessor, ft_vec_access(inds_of_lowest, new_ind - 1)); + res = ft_vec_append(inds_of_predecessor, + ft_vec_access(inds_of_lowest, new_ind - 1)); if (res != success) - break; + break ; ++i; } return (res); @@ -66,7 +69,7 @@ static t_arr_stat get_lis_loop(t_stack *stack, t_vec *inds_of_lowest, t_vec *ind /* This is the algorithm for finding the longest increasing subsequence found on wikipedia adjusted for circular sequences (stack).*/ -static t_arr_stat get_lis(t_stack *lis, t_stack *stack, size_t ind) +static t_arr_stat get_lis(t_stack *lis, t_stack *stack, size_t ind) { t_vec inds_of_lowest; t_vec inds_of_predecessor; diff --git a/src/helpers.c b/src/helpers.c index ee17362..b68558d 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/22 14:22:08 by ljiriste #+# #+# */ -/* Updated: 2024/02/29 16:20:32 by ljiriste ### ########.fr */ +/* Updated: 2024/03/01 09:59:03 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,7 +97,8 @@ size_t binary_search(t_vec *sorted, t_stack *stack, size_t val_ind) mid = lo + (hi - lo) / 2; while (lo != hi) { - if (val > unrolled_stack_access(stack, *(size_t *)ft_vec_access(sorted, mid))) + if (val > unrolled_stack_access(stack, + *(size_t *)ft_vec_access(sorted, mid))) { lo = mid + 1; } @@ -109,4 +110,3 @@ size_t binary_search(t_vec *sorted, t_stack *stack, size_t val_ind) } return (mid); } - diff --git a/src/push.c b/src/push.c index 8bc0fb5..ae09a0f 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/29 14:41:41 by ljiriste ### ########.fr */ +/* Updated: 2024/03/01 09:59:04 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,34 +17,8 @@ #include "checker.h" #include "libft.h" -#include #include -void a_sort_three_or_less(t_stacks *s) -{ - int last; - int max; - size_t steps_to_smaller; - size_t i; - - i = 0; - last = stack_top(&s->a); - max = INT_MIN; - steps_to_smaller = 0; - while (i < s->a.stack.size) - { - stack_rotate(&s->a, 1); - steps_to_smaller += (stack_top(&s->a) < last); - last = stack_top(&s->a); - if (max < stack_top(&s->a)) - max = stack_top(&s->a); - ++i; - } - if (steps_to_smaller == 2) - print_swap(s, a); - return ; -} - void get_push_indeces(t_stacks *s, int *ind_a, int *ind_b) { t_vec target_a_indeces; @@ -68,27 +42,6 @@ void get_push_indeces(t_stacks *s, int *ind_a, int *ind_b) return ; } -void rotate_a_to_sort(t_stacks *s) -{ - size_t minind; - t_action *print_action; - - minind = find_minind(&s->a); - if (minind <= s->a.stack.size / 2) - print_action = print_rotate; - else - { - minind -= s->a.stack.size / 2; - print_action = print_reverse_rotate; - } - while (minind > 0) - { - print_action(s, a); - --minind; - } - return ; -} - void sort(t_stacks *s) { int ind_a; @@ -97,16 +50,7 @@ void sort(t_stacks *s) init_stack(&lis); get_circular_lis(&lis, &s->a); - while (s->a.stack.size > lis.stack.size && s->a.stack.size > 3) - { - if (stack_top(&lis) == stack_top(&s->a)) - { - stack_rotate(&lis, 1); - print_rotate(s, a); - } - else - print_push(s, b); - } + isolate_lis_in_a(s, &lis); free_stack(&lis); if (lis.stack.size < 3) a_sort_three_or_less(s); diff --git a/src/special_a.c b/src/special_a.c new file mode 100644 index 0000000..919a73b --- /dev/null +++ b/src/special_a.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* special_a.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/01 09:45:01 by ljiriste #+# #+# */ +/* Updated: 2024/03/01 09:59:04 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "checker.h" +#include "libft.h" +#include +#include + +void rotate_a_to_sort(t_stacks *s) +{ + size_t minind; + t_action *print_action; + + minind = find_minind(&s->a); + if (minind <= s->a.stack.size / 2) + print_action = print_rotate; + else + { + minind -= s->a.stack.size / 2; + print_action = print_reverse_rotate; + } + while (minind > 0) + { + print_action(s, a); + --minind; + } + return ; +} + +void isolate_lis_in_a(t_stacks *s, t_stack *lis) +{ + while (s->a.stack.size > lis->stack.size && s->a.stack.size > 3) + { + if (stack_top(lis) == stack_top(&s->a)) + { + stack_rotate(lis, 1); + print_rotate(s, a); + } + else + print_push(s, b); + } + return ; +} + +void a_sort_three_or_less(t_stacks *s) +{ + int last; + int max; + size_t steps_to_smaller; + size_t i; + + i = 0; + last = stack_top(&s->a); + max = INT_MIN; + steps_to_smaller = 0; + while (i < s->a.stack.size) + { + stack_rotate(&s->a, 1); + steps_to_smaller += (stack_top(&s->a) < last); + last = stack_top(&s->a); + if (max < stack_top(&s->a)) + max = stack_top(&s->a); + ++i; + } + if (steps_to_smaller == 2) + print_swap(s, a); + return ; +}