Skip to content

Commit

Permalink
fix 0 indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Feb 13, 2024
1 parent 699594c commit 89a5368
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/initialization/generate_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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++;
Expand Down
10 changes: 8 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -15,7 +15,7 @@
#ifdef LEAK
# ifdef __APPLE__

void leak_chek(void) __attribute__((destructor));
void leak_chek(void) __attribute__((destructor));

void leak_chek(void)
{
Expand All @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/sort/is_sorted_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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)
Expand Down
24 changes: 4 additions & 20 deletions src/sort/large_sort/push_b_segmented.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,14 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

#include "large_sort.h"
#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;
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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++;
}
Expand Down
16 changes: 8 additions & 8 deletions src/sort/micro_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/test_is_sorted_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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));
}

Expand Down
50 changes: 25 additions & 25 deletions test/test_push_b_segmented.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ __attribute__((unused)) static void put_void(void *p_content) {
}

static void segmented_test_main1() {
std::vector<int> v = {9, 14, 18, 13, 12, 19, 2, 8, 16, 6,
10, 17, 7, 1, 20, 15, 3, 11, 4, 5};
std::vector<int> 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;
Expand All @@ -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;
}
Expand All @@ -73,7 +73,7 @@ static void segmented_test_main2() {
int N = 20;
std::vector<int> v(N);
for (int i = 0; i < N; ++i) {
v[i] = i + 1;
v[i] = i;
}
// shuffle vector
std::random_device seed_gen;
Expand All @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/test_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static void sort_test_main(int N) {
// e.g. N = 3 -> v = {1, 2, 3}
std::vector<int> v(N);
for (int i = 0; i < N; ++i) {
v[i] = i + 1;
v[i] = i;
}

// generate permutation of v (N! patterns)
Expand All @@ -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()));
Expand All @@ -63,7 +63,7 @@ static void sort_test(int N) {
static void random_sort_test_main(int N) {
std::vector<int> v(N);
for (int i = 0; i < N; ++i) {
v[i] = i + 1;
v[i] = i;
}
// shuffle vector
std::random_device seed_gen;
Expand All @@ -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);
}
}
Expand Down

0 comments on commit 89a5368

Please sign in to comment.