From cc9492586f41d238e9a741da94f807b4d7b3a5cf Mon Sep 17 00:00:00 2001 From: rask24 Date: Thu, 8 Feb 2024 21:42:35 +0900 Subject: [PATCH] refactor with set_cost --- Makefile | 3 +- include/push_swap.h | 5 +- src/sort/large_sort.c | 5 +- src/sort/{set_insertion_cost.c => set_cost.c} | 51 ++++++++++--------- src/sort/set_selection_cost.c | 31 ----------- test/test_set_cost.cpp | 8 +-- 6 files changed, 38 insertions(+), 65 deletions(-) rename src/sort/{set_insertion_cost.c => set_cost.c} (66%) delete mode 100644 src/sort/set_selection_cost.c diff --git a/Makefile b/Makefile index e8ed3dc..37a98cf 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,7 @@ SRC = $(SRC_DIR)/main.c \ $(SRC_DIR)/sort/micro_sort.c \ $(SRC_DIR)/sort/nano_sort.c \ $(SRC_DIR)/sort/large_sort.c \ - $(SRC_DIR)/sort/set_selection_cost.c \ - $(SRC_DIR)/sort/set_insertion_cost.c \ + $(SRC_DIR)/sort/set_cost.c \ $(SRC_DIR)/stack_operations/push.c \ $(SRC_DIR)/stack_operations/swap.c \ $(SRC_DIR)/stack_operations/rotate.c \ diff --git a/include/push_swap.h b/include/push_swap.h index 60a022c..2ef62db 100644 --- a/include/push_swap.h +++ b/include/push_swap.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ -/* Updated: 2024/02/08 07:44:33 by reasuke ### ########.fr */ +/* Updated: 2024/02/08 13:35:51 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,8 +38,7 @@ void nano_sort(t_stack **p_a, int num_a); void micro_sort(t_stack **p_a, t_stack **p_b, int num_a); void large_sort(t_stack **p_a, t_stack **p_b, int num_a); -void set_selection_cost(t_stack **p_b); -void set_insertion_cost(t_stack **p_a, t_stack **p_b); +void set_cost(t_stack **p_a, t_stack **p_b); void operate_sa(t_stack **p_a); void operate_sb(t_stack **p_b); diff --git a/src/sort/large_sort.c b/src/sort/large_sort.c index 3e9ad9a..cc97c1d 100644 --- a/src/sort/large_sort.c +++ b/src/sort/large_sort.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */ -/* Updated: 2024/02/08 07:33:42 by reasuke ### ########.fr */ +/* Updated: 2024/02/08 13:34:33 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,5 @@ void large_sort(t_stack **p_a, t_stack **p_b, int num_a) { _init_stack(p_a, p_b, num_a); nano_sort(p_a, 3); - set_selection_cost(p_b); - set_insertion_cost(p_a, p_b); + set_cost(p_a, p_b); } diff --git a/src/sort/set_insertion_cost.c b/src/sort/set_cost.c similarity index 66% rename from src/sort/set_insertion_cost.c rename to src/sort/set_cost.c index 735a47b..0f09ae0 100644 --- a/src/sort/set_insertion_cost.c +++ b/src/sort/set_cost.c @@ -1,25 +1,23 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* set_insertion_cost.c :+: :+: :+: */ +/* set_cost.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/08 07:32:05 by reasuke #+# #+# */ -/* Updated: 2024/02/08 10:09:34 by reasuke ### ########.fr */ +/* Created: 2024/02/08 12:17:33 by reasuke #+# #+# */ +/* Updated: 2024/02/08 21:38:17 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -static void _set_insertion_forward_cost(t_stack **p_a, t_stack *st_b) +static int _calc_insertion_threshold(t_stack **p_a, t_stack *st_b) { t_stack *st_a; int size_a; int i; int threshold; - int max_a; - int max_index; int min; st_a = *p_a; @@ -27,54 +25,61 @@ static void _set_insertion_forward_cost(t_stack **p_a, t_stack *st_b) i = 0; threshold = INT_MAX; min = INT_MAX; - max_a = INT_MIN; while (i < size_a) { if (get_content(st_a)->index > get_content(st_b)->index && ft_chmin(&min, get_content(st_a)->index)) threshold = i; - if (ft_chmax(&max_a, get_content(st_a)->index)) - max_index = i; st_a = st_a->next; i++; } - if (threshold == INT_MAX) - threshold = (max_index + 1) % size_a; - get_content(st_b)->if_cost = threshold % size_a; + return (threshold); } -static void _set_insertion_reverse_cost(t_stack **p_a, t_stack *st_b) +static int _calc_max_index(t_stack **p_a) { t_stack *st_a; int size_a; int i; - int threshold; int max_a; int max_index; - int min; st_a = *p_a; size_a = stack_size(*p_a); i = 0; - threshold = INT_MAX; - min = INT_MAX; max_a = INT_MIN; while (i < size_a) { - if (get_content(st_a)->index > get_content(st_b)->index - && ft_chmin(&min, get_content(st_a)->index)) - threshold = i; if (ft_chmax(&max_a, get_content(st_a)->index)) max_index = i; st_a = st_a->next; i++; } + return (max_index); +} + +static void _set_insertion_cost(t_stack **p_a, t_stack *st_b) +{ + int max_index; + int size_a; + int threshold; + + size_a = stack_size(*p_a); + threshold = _calc_insertion_threshold(p_a, st_b); + max_index = _calc_max_index(p_a); if (threshold == INT_MAX) threshold = (max_index + 1) % size_a; + get_content(st_b)->if_cost = threshold % size_a; get_content(st_b)->ir_cost = -((size_a - threshold) % size_a); } -void set_insertion_cost(t_stack **p_a, t_stack **p_b) +static void _set_selection_cost(t_stack *st_b, int i, int size_b) +{ + get_content(st_b)->sf_cost = i; + get_content(st_b)->sr_cost = -((size_b - i) % size_b); +} + +void set_cost(t_stack **p_a, t_stack **p_b) { t_stack *st_b; int size_b; @@ -85,8 +90,8 @@ void set_insertion_cost(t_stack **p_a, t_stack **p_b) i = 0; while (i < size_b) { - _set_insertion_forward_cost(p_a, st_b); - _set_insertion_reverse_cost(p_a, st_b); + _set_selection_cost(st_b, i, size_b); + _set_insertion_cost(p_a, st_b); st_b = st_b->next; i++; } diff --git a/src/sort/set_selection_cost.c b/src/sort/set_selection_cost.c deleted file mode 100644 index 1f342b1..0000000 --- a/src/sort/set_selection_cost.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* set_selection_cost.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: reasuke +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/07 16:37:17 by reasuke #+# #+# */ -/* Updated: 2024/02/08 07:49:59 by reasuke ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "push_swap.h" - -void set_selection_cost(t_stack **p_b) -{ - t_stack *st; - int size_b; - int i; - - st = *p_b; - size_b = stack_size(*p_b); - i = 0; - while (i < size_b) - { - get_content(st)->sf_cost = i; - get_content(st)->sr_cost = -((size_b - i) % size_b); - st = st->next; - i++; - } -} diff --git a/test/test_set_cost.cpp b/test/test_set_cost.cpp index 3235645..5ef1510 100644 --- a/test/test_set_cost.cpp +++ b/test/test_set_cost.cpp @@ -18,13 +18,15 @@ extern "C" { TEST(set_selection_cost, selection) { int N = 5; + t_stack *stack_a = NULL; t_stack *stack_b = NULL; for (int i = 0; i < N; ++i) { t_content *c = new t_content({i, 0, 0, 0, 0}); + ft_lstadd_back(&stack_a, ft_lstnew(c)); ft_lstadd_back(&stack_b, ft_lstnew(c)); } - set_selection_cost(&stack_b); + set_cost(&stack_a, &stack_b); // index: 2 t_stack *target = stack_b->next->next; @@ -53,7 +55,7 @@ TEST(set_insertion_cost, insertion1) { ft_lstadd_back(&stack_b, ft_lstnew(c)); } - set_insertion_cost(&stack_a, &stack_b); + set_cost(&stack_a, &stack_b); t_stack *target; int if_cost; @@ -99,7 +101,7 @@ TEST(set_insertion_cost, insertion2) { ft_lstadd_back(&stack_b, ft_lstnew(c)); } - set_insertion_cost(&stack_a, &stack_b); + set_cost(&stack_a, &stack_b); t_stack *target; int if_cost;