From 89a53687119f18fa760f173cb5da9b1a665b7279 Mon Sep 17 00:00:00 2001 From: rask24 Date: Tue, 13 Feb 2024 15:41:48 +0900 Subject: [PATCH] fix 0 indexed --- Makefile | 2 +- src/initialization/generate_stack.c | 4 +-- src/main.c | 10 ++++-- src/sort/is_sorted_stack.c | 4 +-- src/sort/large_sort/push_b_segmented.c | 24 +++---------- src/sort/micro_sort.c | 16 ++++----- test/test_is_sorted_stack.cpp | 4 +-- test/test_push_b_segmented.cpp | 50 +++++++++++++------------- test/test_sort.cpp | 8 ++--- 9 files changed, 56 insertions(+), 66 deletions(-) diff --git a/Makefile b/Makefile index 729c542..5aa7bb5 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ NAME = push_swap CFLAGS = -Werror -Wextra -Wall CXXFLAGS = -std=c++17 -Wall -Wextra -Werror PROD_FLAGS = -O3 -DEV_FLAGS = -g -fsanitize=address -O0 -D DEV +DEV_FLAGS = -g -fsanitize=address,integer,undefined -O0 -D DEV LEAK_FLAGS = -O0 -D DEV -D LEAK DEPFLAGS = -MMD -MP INCLUDE = -I $(INC_DIR) diff --git a/src/initialization/generate_stack.c b/src/initialization/generate_stack.c index ba01532..3e4ae48 100644 --- a/src/initialization/generate_stack.c +++ b/src/initialization/generate_stack.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */ -/* Updated: 2024/02/13 11:46:14 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 14:01:23 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,7 +76,7 @@ int *_coordinate_compression(int size_a, char **argv) while (j < size_a) { if (ft_atoi(argv[i + 1]) == sorted[j]) - comp[i] = j + 1; + comp[i] = j; j++; } i++; diff --git a/src/main.c b/src/main.c index ab6d137..78a4385 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */ -/* Updated: 2024/02/13 12:45:59 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 15:00:02 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #ifdef LEAK # ifdef __APPLE__ -void leak_chek(void) __attribute__((destructor)); +void leak_chek(void) __attribute__((destructor)); void leak_chek(void) { @@ -33,6 +33,11 @@ void put_void(void *content) #endif +static void _put_void(void *p_content) +{ + ft_printf("%d\n", ((t_content *)p_content)->index); +} + int main(int argc, char **argv) { t_stack *st_a; @@ -41,6 +46,7 @@ int main(int argc, char **argv) check_args(argc, argv); st_a = generate_stack(argc, argv); st_b = NULL; + ft_lstiter(st_a, _put_void); sort(&st_a, &st_b); clear_stack(&st_a, free); return (0); diff --git a/src/sort/is_sorted_stack.c b/src/sort/is_sorted_stack.c index 1fe7b89..d130a36 100644 --- a/src/sort/is_sorted_stack.c +++ b/src/sort/is_sorted_stack.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/10 18:47:11 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:54:04 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 14:01:58 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ bool is_sorted_stack(t_stack *st) { int current; - current = 1; + current = 0; while (st) { if (get_content(st)->index != current) diff --git a/src/sort/large_sort/push_b_segmented.c b/src/sort/large_sort/push_b_segmented.c index 808af83..c383255 100644 --- a/src/sort/large_sort/push_b_segmented.c +++ b/src/sort/large_sort/push_b_segmented.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/11 22:57:28 by reasuke #+# #+# */ -/* Updated: 2024/02/12 23:28:29 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 15:01:23 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,20 +14,6 @@ #include "push_swap.h" #include "stack_operations.h" -// 1 ~ 100 -// segment1: 1 ~ 20 -// segment1: 21 ~ 40 -// segment1: 41 ~ 60 -// segment1: 61 ~ 80 -// segment1: 81 ~ 100 - -// 123: 24 -// [0, 24) -> [0, seg_size) -// [24, 48) -> [seg_size, seg_size * 2) -// [48, 72) -> [seg_size * 2, seg_size * 3) -// [72, 96) -> [seg_size * 3, seg_size * 4) -// [96, 123) -> [seg_size * 4, N) - static int _calc_segment_id(int target, int n, int segs) { int seg_size; @@ -48,8 +34,6 @@ static int _calc_segment_id(int target, int n, int segs) return (-1); } -// ft_printf("ns: %d, index: %d, pushed: %d, n: %d\n", num_segment, index, -// pushed, n); static bool _should_push_b(int index, int pushed, int n, int segs) { int seg_size; @@ -70,8 +54,8 @@ static bool _should_push_b(int index, int pushed, int n, int segs) sup = seg_size * (cur_id + 1); } if (cur_id == segs - 1) - return (inf < index && index <= n); - return (inf < index && index <= sup); + return (inf <= index); + return (inf <= index && index < sup); } void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs) @@ -86,7 +70,7 @@ void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs) if (_should_push_b(index, pushed, n, segs)) { operate_pb(p_a, p_b); - if (_calc_segment_id(index - 1, n, segs) % 2 == 1) + if (_calc_segment_id(index, n, segs) % 2 == 1) operate_rb(p_b); pushed++; } diff --git a/src/sort/micro_sort.c b/src/sort/micro_sort.c index 8f70f63..60d0283 100644 --- a/src/sort/micro_sort.c +++ b/src/sort/micro_sort.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/15 16:32:27 by reasuke #+# #+# */ -/* Updated: 2024/02/11 12:54:02 by reasuke ### ########.fr */ +/* Updated: 2024/02/13 15:38:56 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,21 +18,21 @@ static void _handle_4(t_stack **p_a, t_stack **p_b) { operate_pb(p_a, p_b); nano_sort(p_a); - if (get_first_index(p_b) == 1) + if (get_first_index(p_b) == 0) operate_pa(p_b, p_a); - else if (get_first_index(p_b) == 2) + else if (get_first_index(p_b) == 1) { operate_pa(p_b, p_a); operate_sa(p_a); } - else if (get_first_index(p_b) == 3) + else if (get_first_index(p_b) == 2) { operate_rra(p_a); operate_pa(p_b, p_a); operate_ra(p_a); operate_ra(p_a); } - else if (get_first_index(p_b) == 4) + else if (get_first_index(p_b) == 3) { operate_pa(p_b, p_a); operate_ra(p_a); @@ -49,7 +49,7 @@ static void _edge_flow_5(t_stack **p_a, t_stack **p_b) static void _normal_flow_5(t_stack **p_a, t_stack **p_b) { - if (get_first_index(p_b) == 5) + if (get_first_index(p_b) == 4) { operate_pa(p_b, p_a); operate_rra(p_a); @@ -63,7 +63,7 @@ static void _normal_flow_5(t_stack **p_a, t_stack **p_b) while (get_first_index(p_a) != get_first_index(p_b) + 1) operate_rra(p_a); operate_pa(p_b, p_a); - while (get_first_index(p_a) != 1) + while (get_first_index(p_a) != 0) operate_rra(p_a); } @@ -74,7 +74,7 @@ static void _handle_5(t_stack **p_a, t_stack **p_b) nano_sort(p_a); if (get_first_index(p_b) < get_second_index(p_b)) operate_sb(p_b); - if (get_first_index(p_b) == 5 && get_second_index(p_b) == 4) + if (get_first_index(p_b) == 4 && get_second_index(p_b) == 3) _edge_flow_5(p_a, p_b); else _normal_flow_5(p_a, p_b); diff --git a/test/test_is_sorted_stack.cpp b/test/test_is_sorted_stack.cpp index 340caeb..f8f583b 100644 --- a/test/test_is_sorted_stack.cpp +++ b/test/test_is_sorted_stack.cpp @@ -28,7 +28,7 @@ TEST(is_sorted_stack, sorted) { st = NULL; for (int i = 0; i < N; ++i) { - t_content *c = new t_content({i + 1, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&st, ft_lstnew(c)); } @@ -53,7 +53,7 @@ TEST(is_sorted_stack, not_sorted) { st = NULL; for (int &i : v) { - t_content *c = new t_content({i + 1, 0, 0, 0, 0, 0, INIT, false}); + t_content *c = new t_content({i, 0, 0, 0, 0, 0, INIT, false}); ft_lstadd_back(&st, ft_lstnew(c)); } diff --git a/test/test_push_b_segmented.cpp b/test/test_push_b_segmented.cpp index bcee2f7..b5321f4 100644 --- a/test/test_push_b_segmented.cpp +++ b/test/test_push_b_segmented.cpp @@ -18,8 +18,8 @@ __attribute__((unused)) static void put_void(void *p_content) { } static void segmented_test_main1() { - std::vector v = {9, 14, 18, 13, 12, 19, 2, 8, 16, 6, - 10, 17, 7, 1, 20, 15, 3, 11, 4, 5}; + std::vector v = {2, 7, 4, 9, 10, 0, 17, 19, 13, 8, + 1, 15, 11, 3, 5, 18, 16, 12, 14, 6}; int size_a = v.size(); t_stack *stack_a = NULL; @@ -34,20 +34,20 @@ static void segmented_test_main1() { t_stack *st = stack_b; for (int i = 0; i < size_a; ++i) { if (i < 4) { - EXPECT_GE(get_content(st)->index, 17); - EXPECT_LE(get_content(st)->index, 20); + EXPECT_GE(get_content(st)->index, 16); + EXPECT_LE(get_content(st)->index, 19); } else if (4 <= i && i < 8) { - EXPECT_GE(get_content(st)->index, 9); - EXPECT_LE(get_content(st)->index, 12); + EXPECT_GE(get_content(st)->index, 8); + EXPECT_LE(get_content(st)->index, 11); } else if (8 <= i && i < 12) { - EXPECT_GE(get_content(st)->index, 1); - EXPECT_LE(get_content(st)->index, 4); + EXPECT_GE(get_content(st)->index, 0); + EXPECT_LE(get_content(st)->index, 3); } else if (12 <= i && i < 16) { - EXPECT_GE(get_content(st)->index, 5); - EXPECT_LE(get_content(st)->index, 8); + EXPECT_GE(get_content(st)->index, 4); + EXPECT_LE(get_content(st)->index, 7); } else { - EXPECT_GE(get_content(st)->index, 13); - EXPECT_LE(get_content(st)->index, 16); + EXPECT_GE(get_content(st)->index, 12); + EXPECT_LE(get_content(st)->index, 15); } st = st->next; } @@ -73,7 +73,7 @@ static void segmented_test_main2() { int N = 20; std::vector v(N); for (int i = 0; i < N; ++i) { - v[i] = i + 1; + v[i] = i; } // shuffle vector std::random_device seed_gen; @@ -92,28 +92,28 @@ static void segmented_test_main2() { t_stack *st = stack_b; for (int i = 0; i < size_a - 3; ++i) { if (i < 5) { - EXPECT_GE(get_content(st)->index, 13); - EXPECT_LE(get_content(st)->index, 17); + EXPECT_GE(get_content(st)->index, 12); + EXPECT_LE(get_content(st)->index, 19); } else if (5 <= i && i < 8) { - EXPECT_GE(get_content(st)->index, 7); - EXPECT_LE(get_content(st)->index, 9); + EXPECT_GE(get_content(st)->index, 6); + EXPECT_LE(get_content(st)->index, 8); } else if (8 <= i && i < 11) { - EXPECT_GE(get_content(st)->index, 1); - EXPECT_LE(get_content(st)->index, 3); + EXPECT_GE(get_content(st)->index, 0); + EXPECT_LE(get_content(st)->index, 2); } else if (11 <= i && i < 14) { - EXPECT_GE(get_content(st)->index, 4); - EXPECT_LE(get_content(st)->index, 6); + EXPECT_GE(get_content(st)->index, 3); + EXPECT_LE(get_content(st)->index, 5); } else { - EXPECT_GE(get_content(st)->index, 10); - EXPECT_LE(get_content(st)->index, 12); + EXPECT_GE(get_content(st)->index, 9); + EXPECT_LE(get_content(st)->index, 11); } st = st->next; } st = stack_a; while (st) { - EXPECT_GE(get_content(st)->index, 18); - EXPECT_LE(get_content(st)->index, 20); + EXPECT_GE(get_content(st)->index, 12); + EXPECT_LE(get_content(st)->index, 19); st = st->next; } } diff --git a/test/test_sort.cpp b/test/test_sort.cpp index aad5f26..f489cc8 100644 --- a/test/test_sort.cpp +++ b/test/test_sort.cpp @@ -16,7 +16,7 @@ static void sort_test_main(int N) { // e.g. N = 3 -> v = {1, 2, 3} std::vector v(N); for (int i = 0; i < N; ++i) { - v[i] = i + 1; + v[i] = i; } // generate permutation of v (N! patterns) @@ -38,7 +38,7 @@ static void sort_test_main(int N) { // sort sort(&stack_a, &stack_b); // check if the order is appropreate - for (int i = 1; stack_a; ++i, stack_a = stack_a->next) { + for (int i = 0; stack_a; ++i, stack_a = stack_a->next) { EXPECT_EQ(get_first_index(&stack_a), i); } } while (std::next_permutation(v.begin(), v.end())); @@ -63,7 +63,7 @@ static void sort_test(int N) { static void random_sort_test_main(int N) { std::vector v(N); for (int i = 0; i < N; ++i) { - v[i] = i + 1; + v[i] = i; } // shuffle vector std::random_device seed_gen; @@ -79,7 +79,7 @@ static void random_sort_test_main(int N) { // sort sort(&stack_a, &stack_b); // check if the order is appropreate - for (int i = 1; stack_a; ++i, stack_a = stack_a->next) { + for (int i = 0; stack_a; ++i, stack_a = stack_a->next) { EXPECT_EQ(*(int *)stack_a->content, i); } }