Skip to content

Commit

Permalink
add min_cost
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Feb 9, 2024
1 parent bbbba12 commit 0004ba8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 17 deletions.
3 changes: 2 additions & 1 deletion include/push_swap.h
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:54 by reasuke #+# #+# */
/* Updated: 2024/02/09 23:10:54 by reasuke ### ########.fr */
/* Updated: 2024/02/09 23:13:42 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,6 +37,7 @@ typedef struct s_content
int sr_cost;
int if_cost;
int ir_cost;
int min_cost;
t_method opt_method;
bool is_opt;
} t_content;
Expand Down
28 changes: 17 additions & 11 deletions 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:11:44 by reasuke ### ########.fr */
/* Updated: 2024/02/09 23:20:32 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,7 +34,19 @@ static int _calc_cost(t_stack *st_b, t_method method)
return (-1);
}

static void _set_opt_method(t_stack **p_b)
static void _set_opt_method(t_stack *st_b, int *cost)
{
if (ft_chmin(cost, _calc_cost(st_b, FF)))
get_content(st_b)->opt_method = FF;
if (ft_chmin(cost, _calc_cost(st_b, FR)))
get_content(st_b)->opt_method = FR;
if (ft_chmin(cost, _calc_cost(st_b, RF)))
get_content(st_b)->opt_method = RF;
if (ft_chmin(cost, _calc_cost(st_b, RR)))
get_content(st_b)->opt_method = RR;
}

static void _set_min_cost_opt_method(t_stack **p_b)
{
int i;
int size_b;
Expand All @@ -47,20 +59,14 @@ static void _set_opt_method(t_stack **p_b)
while (i < size_b)
{
cost = INT_MAX;
if (ft_chmin(&cost, _calc_cost(st_b, FF)))
get_content(st_b)->opt_method = FF;
if (ft_chmin(&cost, _calc_cost(st_b, FR)))
get_content(st_b)->opt_method = FR;
if (ft_chmin(&cost, _calc_cost(st_b, RF)))
get_content(st_b)->opt_method = RF;
if (ft_chmin(&cost, _calc_cost(st_b, RR)))
get_content(st_b)->opt_method = RR;
_set_opt_method(st_b, &cost);
get_content(st_b)->min_cost = cost;
st_b = st_b->next;
i++;
}
}

void set_opt(t_stack **p_b)
{
_set_opt_method(p_b);
_set_min_cost_opt_method(p_b);
}
70 changes: 65 additions & 5 deletions test/test_set_opt_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern "C" {
// 8 | 9 <- FF: (4, 0), FR: (4, 0), RF: (-1, 0), RR: (-1, 0)
// . 4 4 1 1

TEST(set_opt, set_opt_method1) {
TEST(set_opt, _set_opt_method1) {
int N;
t_stack *stack_a;
t_stack *stack_b;
Expand All @@ -29,7 +29,7 @@ TEST(set_opt, set_opt_method1) {
stack_a = NULL;
stack_b = NULL;
for (int i = 0; i < 2 * N; ++i) {
c = new t_content({i, 0, 0, 0, 0, INIT, false});
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
Expand All @@ -48,6 +48,35 @@ TEST(set_opt, set_opt_method1) {
EXPECT_EQ(get_content(target)->opt_method, RF);
}

TEST(set_opt, _set_min_cost1) {
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 *target;

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

// stack_a | stack_b
// 6 | 8 <- FF: (0, 2), FR: (0, 3), RF: (0, 2), RR: (0, 3)
// 2 3 2 3
Expand All @@ -58,7 +87,7 @@ TEST(set_opt, set_opt_method1) {
// 4 | 5 <- FF: (4, 0), FR: (-1, 0), RF: (4, 0), RR: (-1, 0)
// 4 1 4 1

TEST(set_opt, opt_method2) {
TEST(set_opt, _set_opt_method2) {
t_stack *stack_a;
t_stack *stack_b;
t_content *c;
Expand All @@ -68,11 +97,11 @@ TEST(set_opt, opt_method2) {
stack_a = NULL;
stack_b = NULL;
for (int &i : v_a) {
c = new t_content({i, 0, 0, 0, 0, INIT, false});
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, INIT, false});
c = new t_content({i, 0, 0, 0, 0, 0, INIT, false});
ft_lstadd_back(&stack_b, ft_lstnew(c));
}

Expand All @@ -88,3 +117,34 @@ TEST(set_opt, opt_method2) {
target = stack_b->next->next->next->next;
EXPECT_EQ(get_content(target)->opt_method, RF);
}

TEST(set_opt, _set_min_cost2) {
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 *target;

target = stack_b;
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->next;
EXPECT_EQ(get_content(target)->min_cost, 1);
}

0 comments on commit 0004ba8

Please sign in to comment.