From 8c6d01ed8d7c68577171c5507ed72f76aa7d099a Mon Sep 17 00:00:00 2001 From: rask24 Date: Sat, 20 Apr 2024 15:24:48 +0900 Subject: [PATCH 1/4] rename repeat_stack_operations --- Makefile | 2 +- include/stack_operations.h | 4 ++-- src/sort/large_sort/greedy_operation.c | 18 +++++++++--------- src/stack_operations/do_double_n_operations.c | 2 +- ..._operations.c => repeat_stack_operations.c} | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) rename src/stack_operations/{do_single_n_operations.c => repeat_stack_operations.c} (82%) diff --git a/Makefile b/Makefile index b66bbde..175b450 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,7 @@ SRC = $(SRC_DIR)/main.c \ $(SRC_DIR)/stack_operations/swap.c \ $(SRC_DIR)/stack_operations/rotate.c \ $(SRC_DIR)/stack_operations/reverse_rotate.c \ - $(SRC_DIR)/stack_operations/do_single_n_operations.c \ + $(SRC_DIR)/stack_operations/repeat_stack_operations.c \ $(SRC_DIR)/stack_operations/do_double_n_operations.c \ $(SRC_DIR)/utils/get_first_index.c \ $(SRC_DIR)/utils/get_second_index.c \ diff --git a/include/stack_operations.h b/include/stack_operations.h index 8c75758..bed12f0 100644 --- a/include/stack_operations.h +++ b/include/stack_operations.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:41:01 by reasuke #+# #+# */ -/* Updated: 2024/02/20 14:16:47 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:24:23 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void swap_stack(t_stack **p_stack); void rotate_stack(t_stack **p_stack); void reverse_rotate_stack(t_stack **p_stack); -void do_single_n_operations(t_stack **p_st, int n, +void repeat_stack_operations(t_stack **p_st, int n, void (*operation)(t_stack **)); void do_double_n_operations(t_stack **p_a, t_stack **p_b, int n, void (*operation)(t_stack **, t_stack **)); diff --git a/src/sort/large_sort/greedy_operation.c b/src/sort/large_sort/greedy_operation.c index 90a3537..0de80b7 100644 --- a/src/sort/large_sort/greedy_operation.c +++ b/src/sort/large_sort/greedy_operation.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */ -/* Updated: 2024/02/20 14:17:27 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:21:20 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,17 +39,17 @@ static void _do_alined_operation(t_stack **p_a, t_stack **p_b, { do_double_n_operations(p_a, p_b, ft_min(sf_abs, if_abs), operate_rr); if (sf_abs > if_abs) - do_single_n_operations(p_b, sf_abs - if_abs, operate_rb); + repeat_stack_operations(p_b, sf_abs - if_abs, operate_rb); else if (sf_abs < if_abs) - do_single_n_operations(p_a, if_abs - sf_abs, operate_ra); + repeat_stack_operations(p_a, if_abs - sf_abs, operate_ra); } if (get_content(opt_st_b)->opt_method == RRA_RRB) { do_double_n_operations(p_a, p_b, ft_min(sr_abs, ir_abs), operate_rrr); if (sr_abs > ir_abs) - do_single_n_operations(p_b, sr_abs - ir_abs, operate_rrb); + repeat_stack_operations(p_b, sr_abs - ir_abs, operate_rrb); else if (sr_abs < ir_abs) - do_single_n_operations(p_a, ir_abs - sr_abs, operate_rra); + repeat_stack_operations(p_a, ir_abs - sr_abs, operate_rra); } } @@ -66,13 +66,13 @@ static void _do_mixed_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b) ir_abs = ft_abs(get_content(opt_st_b)->rra_cost); if (get_content(opt_st_b)->opt_method == RRA_RB) { - do_single_n_operations(p_b, sf_abs, operate_rb); - do_single_n_operations(p_a, ir_abs, operate_rra); + repeat_stack_operations(p_b, sf_abs, operate_rb); + repeat_stack_operations(p_a, ir_abs, operate_rra); } if (get_content(opt_st_b)->opt_method == RA_RRB) { - do_single_n_operations(p_b, sr_abs, operate_rrb); - do_single_n_operations(p_a, if_abs, operate_ra); + repeat_stack_operations(p_b, sr_abs, operate_rrb); + repeat_stack_operations(p_a, if_abs, operate_ra); } } diff --git a/src/stack_operations/do_double_n_operations.c b/src/stack_operations/do_double_n_operations.c index 2956ba0..1de0c77 100644 --- a/src/stack_operations/do_double_n_operations.c +++ b/src/stack_operations/do_double_n_operations.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 19:22:50 by reasuke #+# #+# */ -/* Updated: 2024/02/20 14:15:32 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:15:59 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/stack_operations/do_single_n_operations.c b/src/stack_operations/repeat_stack_operations.c similarity index 82% rename from src/stack_operations/do_single_n_operations.c rename to src/stack_operations/repeat_stack_operations.c index 009266a..92e4758 100644 --- a/src/stack_operations/do_single_n_operations.c +++ b/src/stack_operations/repeat_stack_operations.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* do_single_n_operations.c :+: :+: :+: */ +/* repeat_stack_operations.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 19:22:26 by reasuke #+# #+# */ -/* Updated: 2024/02/20 14:15:35 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:25:50 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "stack_operations.h" -void do_single_n_operations(t_stack **p_st, int n, +void repeat_stack_operations(t_stack **p_st, int n, void (*operation)(t_stack **)) { while (n--) From a8e47684b7209cade5822bc6b77cf41df9b22de5 Mon Sep 17 00:00:00 2001 From: rask24 Date: Sat, 20 Apr 2024 15:34:28 +0900 Subject: [PATCH 2/4] rename repeat_dual_stack_operations --- Makefile | 2 +- include/stack_operations.h | 4 ++-- src/sort/large_sort/greedy_operation.c | 6 +++--- ...double_n_operations.c => repeat_dual_stack_operations.c} | 6 +++--- src/stack_operations/repeat_stack_operations.c | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) rename src/stack_operations/{do_double_n_operations.c => repeat_dual_stack_operations.c} (81%) diff --git a/Makefile b/Makefile index 175b450..7aaf03f 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ SRC = $(SRC_DIR)/main.c \ $(SRC_DIR)/stack_operations/rotate.c \ $(SRC_DIR)/stack_operations/reverse_rotate.c \ $(SRC_DIR)/stack_operations/repeat_stack_operations.c \ - $(SRC_DIR)/stack_operations/do_double_n_operations.c \ + $(SRC_DIR)/stack_operations/repeat_dual_stack_operations.c \ $(SRC_DIR)/utils/get_first_index.c \ $(SRC_DIR)/utils/get_second_index.c \ $(SRC_DIR)/utils/get_third_index.c \ diff --git a/include/stack_operations.h b/include/stack_operations.h index bed12f0..a259d85 100644 --- a/include/stack_operations.h +++ b/include/stack_operations.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:41:01 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:24:23 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:25:05 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ void reverse_rotate_stack(t_stack **p_stack); void repeat_stack_operations(t_stack **p_st, int n, void (*operation)(t_stack **)); -void do_double_n_operations(t_stack **p_a, t_stack **p_b, int n, +void repeat_dual_stack_operations(t_stack **p_a, t_stack **p_b, int n, void (*operation)(t_stack **, t_stack **)); void operate_sa(t_stack **p_a); diff --git a/src/sort/large_sort/greedy_operation.c b/src/sort/large_sort/greedy_operation.c index 0de80b7..78f1692 100644 --- a/src/sort/large_sort/greedy_operation.c +++ b/src/sort/large_sort/greedy_operation.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:21:20 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:30:34 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,7 @@ static void _do_alined_operation(t_stack **p_a, t_stack **p_b, ir_abs = ft_abs(get_content(opt_st_b)->rra_cost); if (get_content(opt_st_b)->opt_method == RA_RB) { - do_double_n_operations(p_a, p_b, ft_min(sf_abs, if_abs), operate_rr); + repeat_dual_stack_operations(p_a, p_b, ft_min(sf_abs, if_abs), operate_rr); if (sf_abs > if_abs) repeat_stack_operations(p_b, sf_abs - if_abs, operate_rb); else if (sf_abs < if_abs) @@ -45,7 +45,7 @@ static void _do_alined_operation(t_stack **p_a, t_stack **p_b, } if (get_content(opt_st_b)->opt_method == RRA_RRB) { - do_double_n_operations(p_a, p_b, ft_min(sr_abs, ir_abs), operate_rrr); + repeat_dual_stack_operations(p_a, p_b, ft_min(sr_abs, ir_abs), operate_rrr); if (sr_abs > ir_abs) repeat_stack_operations(p_b, sr_abs - ir_abs, operate_rrb); else if (sr_abs < ir_abs) diff --git a/src/stack_operations/do_double_n_operations.c b/src/stack_operations/repeat_dual_stack_operations.c similarity index 81% rename from src/stack_operations/do_double_n_operations.c rename to src/stack_operations/repeat_dual_stack_operations.c index 1de0c77..eac74a8 100644 --- a/src/stack_operations/do_double_n_operations.c +++ b/src/stack_operations/repeat_dual_stack_operations.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* do_double_n_operations.c :+: :+: :+: */ +/* repeat_dual_stack_operations.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 19:22:50 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:15:59 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:25:42 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "stack_operations.h" -void do_double_n_operations(t_stack **p_a, t_stack **p_b, int n, +void repeat_dual_stack_operations(t_stack **p_a, t_stack **p_b, int n, void (*operation)(t_stack **, t_stack **)) { while (n--) diff --git a/src/stack_operations/repeat_stack_operations.c b/src/stack_operations/repeat_stack_operations.c index 92e4758..943c4a5 100644 --- a/src/stack_operations/repeat_stack_operations.c +++ b/src/stack_operations/repeat_stack_operations.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 19:22:26 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:25:50 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:32:16 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ From 6e5af0c6ba439b5ff75d5d0d8a528c4f186181e8 Mon Sep 17 00:00:00 2001 From: rask24 Date: Sat, 20 Apr 2024 15:41:25 +0900 Subject: [PATCH 3/4] define t_operation --- include/stack_operations.h | 10 ++++++---- src/stack_operations/repeat_dual_stack_operations.c | 6 +++--- src/stack_operations/repeat_stack_operations.c | 7 +++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/include/stack_operations.h b/include/stack_operations.h index a259d85..835868d 100644 --- a/include/stack_operations.h +++ b/include/stack_operations.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 12:41:01 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:25:05 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:40:33 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,15 +15,17 @@ # include "push_swap.h" +typedef void t_operation(t_stack **); +typedef void t_dual_operation(t_stack **, t_stack **); + void push_stack(t_stack **p_s1, t_stack **p_s2); void swap_stack(t_stack **p_stack); void rotate_stack(t_stack **p_stack); void reverse_rotate_stack(t_stack **p_stack); -void repeat_stack_operations(t_stack **p_st, int n, - void (*operation)(t_stack **)); +void repeat_stack_operations(t_stack **p_st, int n, t_operation *op); void repeat_dual_stack_operations(t_stack **p_a, t_stack **p_b, int n, - void (*operation)(t_stack **, t_stack **)); + t_dual_operation *op); void operate_sa(t_stack **p_a); void operate_sb(t_stack **p_b); diff --git a/src/stack_operations/repeat_dual_stack_operations.c b/src/stack_operations/repeat_dual_stack_operations.c index eac74a8..3d959bf 100644 --- a/src/stack_operations/repeat_dual_stack_operations.c +++ b/src/stack_operations/repeat_dual_stack_operations.c @@ -6,15 +6,15 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 19:22:50 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:25:42 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:40:46 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "stack_operations.h" void repeat_dual_stack_operations(t_stack **p_a, t_stack **p_b, int n, - void (*operation)(t_stack **, t_stack **)) + t_dual_operation *op) { while (n--) - operation(p_a, p_b); + op(p_a, p_b); } diff --git a/src/stack_operations/repeat_stack_operations.c b/src/stack_operations/repeat_stack_operations.c index 943c4a5..ec782f7 100644 --- a/src/stack_operations/repeat_stack_operations.c +++ b/src/stack_operations/repeat_stack_operations.c @@ -6,15 +6,14 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 19:22:26 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:32:16 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 15:39:53 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "stack_operations.h" -void repeat_stack_operations(t_stack **p_st, int n, - void (*operation)(t_stack **)) +void repeat_stack_operations(t_stack **p_st, int n, t_operation *op) { while (n--) - operation(p_st); + op(p_st); } From 050a145533709af9d6294ca7b17249b2dd91fcf4 Mon Sep 17 00:00:00 2001 From: rask24 Date: Sat, 20 Apr 2024 16:01:52 +0900 Subject: [PATCH 4/4] refactor --- src/sort/large_sort/greedy_operation.c | 58 ++++++++++++-------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/src/sort/large_sort/greedy_operation.c b/src/sort/large_sort/greedy_operation.c index 78f1692..9e76f6b 100644 --- a/src/sort/large_sort/greedy_operation.c +++ b/src/sort/large_sort/greedy_operation.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */ -/* Updated: 2024/04/20 15:30:34 by reasuke ### ########.fr */ +/* Updated: 2024/04/20 16:01:25 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,53 +26,49 @@ static t_stack *_find_opt_st_b(t_stack **p_b) static void _do_alined_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b) { - int sf_abs; - int sr_abs; - int if_abs; - int ir_abs; + int n_a; + int n_b; - sf_abs = ft_abs(get_content(opt_st_b)->rb_cost); - sr_abs = ft_abs(get_content(opt_st_b)->rrb_cost); - if_abs = ft_abs(get_content(opt_st_b)->ra_cost); - ir_abs = ft_abs(get_content(opt_st_b)->rra_cost); if (get_content(opt_st_b)->opt_method == RA_RB) { - repeat_dual_stack_operations(p_a, p_b, ft_min(sf_abs, if_abs), operate_rr); - if (sf_abs > if_abs) - repeat_stack_operations(p_b, sf_abs - if_abs, operate_rb); - else if (sf_abs < if_abs) - repeat_stack_operations(p_a, if_abs - sf_abs, operate_ra); + n_b = ft_abs(get_content(opt_st_b)->rb_cost); + n_a = ft_abs(get_content(opt_st_b)->ra_cost); + repeat_dual_stack_operations(p_a, p_b, ft_min(n_b, n_a), operate_rr); + if (n_b > n_a) + repeat_stack_operations(p_b, n_b - n_a, operate_rb); + else if (n_b < n_a) + repeat_stack_operations(p_a, n_a - n_b, operate_ra); } if (get_content(opt_st_b)->opt_method == RRA_RRB) { - repeat_dual_stack_operations(p_a, p_b, ft_min(sr_abs, ir_abs), operate_rrr); - if (sr_abs > ir_abs) - repeat_stack_operations(p_b, sr_abs - ir_abs, operate_rrb); - else if (sr_abs < ir_abs) - repeat_stack_operations(p_a, ir_abs - sr_abs, operate_rra); + n_b = ft_abs(get_content(opt_st_b)->rrb_cost); + n_a = ft_abs(get_content(opt_st_b)->rra_cost); + repeat_dual_stack_operations(p_a, p_b, ft_min(n_b, n_a), operate_rrr); + if (n_b > n_a) + repeat_stack_operations(p_b, n_b - n_a, operate_rrb); + else if (n_b < n_a) + repeat_stack_operations(p_a, n_a - n_b, operate_rra); } } static void _do_mixed_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b) { - int sf_abs; - int sr_abs; - int if_abs; - int ir_abs; + int n_a; + int n_b; - sf_abs = ft_abs(get_content(opt_st_b)->rb_cost); - sr_abs = ft_abs(get_content(opt_st_b)->rrb_cost); - if_abs = ft_abs(get_content(opt_st_b)->ra_cost); - ir_abs = ft_abs(get_content(opt_st_b)->rra_cost); if (get_content(opt_st_b)->opt_method == RRA_RB) { - repeat_stack_operations(p_b, sf_abs, operate_rb); - repeat_stack_operations(p_a, ir_abs, operate_rra); + n_a = ft_abs(get_content(opt_st_b)->rra_cost); + n_b = ft_abs(get_content(opt_st_b)->rb_cost); + repeat_stack_operations(p_b, n_b, operate_rb); + repeat_stack_operations(p_a, n_a, operate_rra); } if (get_content(opt_st_b)->opt_method == RA_RRB) { - repeat_stack_operations(p_b, sr_abs, operate_rrb); - repeat_stack_operations(p_a, if_abs, operate_ra); + n_a = ft_abs(get_content(opt_st_b)->ra_cost); + n_b = ft_abs(get_content(opt_st_b)->rrb_cost); + repeat_stack_operations(p_b, n_b, operate_rrb); + repeat_stack_operations(p_a, n_a, operate_ra); } }