Skip to content

Commit

Permalink
add is_opt
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Feb 9, 2024
1 parent 0004ba8 commit 30afe94
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 12 deletions.
32 changes: 31 additions & 1 deletion src/sort/set_opt_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 10:23:28 by reasuke #+# #+# */
/* Updated: 2024/02/09 23:20:32 by reasuke ### ########.fr */
/* Updated: 2024/02/09 23:40:17 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -66,7 +66,37 @@ static void _set_min_cost_opt_method(t_stack **p_b)
}
}

static void _set_is_opt(t_stack **p_b)
{
int i;
t_stack *st_b;
int size_b;
int opt_cost;
int min_index;

size_b = stack_size(*p_b);
i = 0;
st_b = *p_b;
opt_cost = INT_MAX;
while (i < size_b)
{
if (ft_chmin(&opt_cost, get_content(st_b)->min_cost))
min_index = i;
st_b = st_b->next;
i++;
}
i = 0;
st_b = *p_b;
while (i < min_index)
{
st_b = st_b->next;
i++;
}
get_content(st_b)->is_opt = true;
}

void set_opt(t_stack **p_b)
{
_set_min_cost_opt_method(p_b);
_set_is_opt(p_b);
}
105 changes: 94 additions & 11 deletions test/test_set_opt_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ extern "C" {

// stack_a | stack_b
// 0 | 1 <- FF: (0, 1), FR: (0, -4), RF: (0, 1), RR: (0, -4)
// 1 4 1 4
// 2 | 3
// 1 4 1 4: 1
// 2 | 3 <- FF: (1, 2), FR: (1, -3), RF: (-4, 2), RR: (-4, -3)
// 2 4 6 4: 2
// 4 | 5 <- FF: (2, 3), FR: (2, -2), RF: (-3, 3), RR: (-3, -2)
// 3 4 6 3
// 6 | 7
// 3 4 6 3: 3
// 6 | 7 <- FF: (3, 4), FR: (3, -1), RF: (-2, 4), RR: (-2, -1)
// 4 4 6 2: 2
// 8 | 9 <- FF: (4, 0), FR: (4, 0), RF: (-1, 0), RR: (-1, 0)
// . 4 4 1 1
// 4 4 1 1: 1

TEST(set_opt, _set_opt_method1) {
int N;
Expand All @@ -42,8 +44,12 @@ TEST(set_opt, _set_opt_method1) {

target = stack_b;
EXPECT_EQ(get_content(target)->opt_method, FF);
target = stack_b->next;
EXPECT_EQ(get_content(target)->opt_method, FF);
target = stack_b->next->next;
EXPECT_EQ(get_content(target)->opt_method, FF);
target = stack_b->next->next->next;
EXPECT_EQ(get_content(target)->opt_method, RR);
target = stack_b->next->next->next->next;
EXPECT_EQ(get_content(target)->opt_method, RF);
}
Expand Down Expand Up @@ -71,21 +77,57 @@ TEST(set_opt, _set_min_cost1) {

target = stack_b;
EXPECT_EQ(get_content(target)->min_cost, 1);
target = stack_b->next;
EXPECT_EQ(get_content(target)->min_cost, 2);
target = stack_b->next->next;
EXPECT_EQ(get_content(target)->min_cost, 3);
target = stack_b->next->next->next;
EXPECT_EQ(get_content(target)->min_cost, 2);
target = stack_b->next->next->next->next;
EXPECT_EQ(get_content(target)->min_cost, 1);
}

TEST(set_opt, _set_is_opt1) {
int N;
t_stack *stack_a;
t_stack *stack_b;
t_content *c;

N = 5;
stack_a = NULL;
stack_b = NULL;
for (int i = 0; i < 2 * N; ++i) {
c = new t_content({i, 0, 0, 0, 0, 0, INIT, false});
if (i % 2 == 0)
ft_lstadd_back(&stack_a, ft_lstnew(c));
else
ft_lstadd_back(&stack_b, ft_lstnew(c));
}
set_cost(&stack_a, &stack_b);
set_opt(&stack_b);

t_stack *st = stack_b;

for (int i = 0; i < N; ++i) {
if (i == 0)
EXPECT_EQ(get_content(st)->is_opt, true);
else
EXPECT_EQ(get_content(st)->is_opt, false);
st = st->next;
}
}

// stack_a | stack_b
// 6 | 8 <- FF: (0, 2), FR: (0, 3), RF: (0, 2), RR: (0, 3)
// 2 3 2 3
// 7 | 9
// 6 | 8 <- FF: (0, 2), FR: (0, -3), RF: (0, 2), RR: (0, -3)
// 2 3 2 3: 2
// 7 | 9 <- FF: (1, 2), FR: (1, -3), RF: (-4, 2), RR: (-4, -3)
// 2 4 6 4: 2
// 1 | 0 <- FF: (2, 2), FR: (2, -3), RF: (2, 2), RR: (2, -3)
// 2 5 2 5
// 3 | 2
// 2 5 2 5: 2
// 3 | 2 <- FF: (3, 3), FR: (3, -2), RF: (-2, 3), RR: (-2, -2)
// 3 5 5 2: 2
// 4 | 5 <- FF: (4, 0), FR: (-1, 0), RF: (4, 0), RR: (-1, 0)
// 4 1 4 1
// 4 1 4 1: 1

TEST(set_opt, _set_opt_method2) {
t_stack *stack_a;
Expand All @@ -112,8 +154,12 @@ TEST(set_opt, _set_opt_method2) {

target = stack_b;
EXPECT_EQ(get_content(target)->opt_method, FF);
target = stack_b->next;
EXPECT_EQ(get_content(target)->opt_method, FF);
target = stack_b->next->next;
EXPECT_EQ(get_content(target)->opt_method, FF);
target = stack_b->next->next->next;
EXPECT_EQ(get_content(target)->opt_method, RR);
target = stack_b->next->next->next->next;
EXPECT_EQ(get_content(target)->opt_method, RF);
}
Expand Down Expand Up @@ -143,8 +189,45 @@ TEST(set_opt, _set_min_cost2) {

target = stack_b;
EXPECT_EQ(get_content(target)->min_cost, 2);
target = stack_b->next;
EXPECT_EQ(get_content(target)->min_cost, 2);
target = stack_b->next->next;
EXPECT_EQ(get_content(target)->min_cost, 2);
target = stack_b->next->next->next;
EXPECT_EQ(get_content(target)->min_cost, 2);
target = stack_b->next->next->next->next;
EXPECT_EQ(get_content(target)->min_cost, 1);
}

TEST(set_opt, _set_is_opt2) {
int N = 5;
t_stack *stack_a;
t_stack *stack_b;
t_content *c;

std::vector<int> v_a = {6, 7, 1, 3, 4};
std::vector<int> v_b = {8, 9, 0, 2, 5};
stack_a = NULL;
stack_b = NULL;
for (int &i : v_a) {
c = new t_content({i, 0, 0, 0, 0, 0, INIT, false});
ft_lstadd_back(&stack_a, ft_lstnew(c));
}
for (int &i : v_b) {
c = new t_content({i, 0, 0, 0, 0, 0, INIT, false});
ft_lstadd_back(&stack_b, ft_lstnew(c));
}

set_cost(&stack_a, &stack_b);
set_opt(&stack_b);

t_stack *st = stack_b;

for (int i = 0; i < N; ++i) {
if (i == 4)
EXPECT_EQ(get_content(st)->is_opt, true);
else
EXPECT_EQ(get_content(st)->is_opt, false);
st = st->next;
}
}

0 comments on commit 30afe94

Please sign in to comment.