From fe48b78005cec914f77e4029b0e949f1ba29eb39 Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 19 Nov 2024 14:43:14 +0100 Subject: [PATCH] ready to refactor --- inc/push_swap.h | 1 + src/sort/sort3.c | 6 +++--- src/sort/sort5.c | 1 + src/sort/sortmore.c | 25 +++++++++++++++---------- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/inc/push_swap.h b/inc/push_swap.h index dfaab6e..87e6856 100644 --- a/inc/push_swap.h +++ b/inc/push_swap.h @@ -24,6 +24,7 @@ typedef struct s_state int merge_to_a(t_state *state); void sortmore(t_state *state); +void common_rotations(int *shortest_a, int *shortest_b, t_state *state); void push_all_but_3_b(t_state *state); int ft_abs(int a); int ft_max(int a, int b); diff --git a/src/sort/sort3.c b/src/sort/sort3.c index ab9b58b..71752df 100644 --- a/src/sort/sort3.c +++ b/src/sort/sort3.c @@ -15,10 +15,10 @@ // abc // acb rra sa -// bca rra +// bca (rra) // bac sa -// cab ra -// cba sa rra +// cab (ra) +// cba sa (rra) int sort3(t_state *state) { diff --git a/src/sort/sort5.c b/src/sort/sort5.c index 0902aa3..b4ec2cf 100644 --- a/src/sort/sort5.c +++ b/src/sort/sort5.c @@ -19,6 +19,7 @@ int rotate_a_to_top(t_state *state) return (1); } +//sort5 int merge_to_a(t_state *state) { const int b = *(int *) state->b->content; diff --git a/src/sort/sortmore.c b/src/sort/sortmore.c index d3c0d3a..6772054 100644 --- a/src/sort/sortmore.c +++ b/src/sort/sortmore.c @@ -58,15 +58,23 @@ void b_merge_a(int idx_b, t_state *state) shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a)); shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b)); - if (shortest_a * shortest_b > 0) + common_rotations(&shortest_a, &shortest_b, state); + rotate_a(shortest_a, state); + rotate_b(shortest_b, state); + pa(state); +} + +void common_rotations(int *shortest_a, int *shortest_b, t_state *state) +{ + if (*shortest_a * *shortest_b > 0) { - if (shortest_a > 0) + if (*shortest_a > 0) { - while (shortest_a > 0 && shortest_b > 0) + while (*shortest_a > 0 && *shortest_b > 0) { rr(state); - shortest_a--; - shortest_b--; + (*shortest_a)--; + (*shortest_b)--; } } else @@ -74,14 +82,11 @@ void b_merge_a(int idx_b, t_state *state) while (shortest_a < 0 && shortest_b < 0) { rrr(state); - shortest_a++; - shortest_b++; + (*shortest_a)++; + (*shortest_b)++; } } } - rotate_a(shortest_a, state); - rotate_b(shortest_b, state); - pa(state); } void sortmore(t_state *state)