Skip to content

Commit

Permalink
implement segment based optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Feb 14, 2024
1 parent 21db686 commit 314606a
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 23 deletions.
56 changes: 39 additions & 17 deletions src/sort/large_sort/set_is_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,58 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/14 14:44:38 by reasuke #+# #+# */
/* Updated: 2024/02/14 14:45:03 by reasuke ### ########.fr */
/* Updated: 2024/02/14 16:22:59 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "large_sort.h"
#include "push_swap.h"

void set_is_opt(t_stack **p_b)
static int _calc_max_segment(t_stack *st_b)
{
int i;
t_stack *st_b;
int size_b;
int opt_cost;
int min_index;
int max_seg;

size_b = stack_size(*p_b);
i = 0;
st_b = *p_b;
opt_cost = INT_MAX;
min_index = -1;
while (i < size_b)
max_seg = INT_MIN;
while (st_b)
{
if (ft_chmin(&opt_cost, get_content(st_b)->min_cost))
min_index = i;
ft_chmax(&max_seg, get_content(st_b)->segment);
st_b = st_b->next;
i++;
}
return (max_seg);
}

static int _calc_target_index(t_stack *st_b, int max_seg)
{
int target_index;
int cost;
int i;

target_index = -1;
cost = INT_MAX;
i = 0;
while (st_b)
{
if (get_content(st_b)->segment == max_seg && ft_chmin(&cost,
get_content(st_b)->min_cost))
target_index = i;
i++;
st_b = st_b->next;
}
return (target_index);
}

void set_is_opt(t_stack **p_b)
{
int i;
t_stack *st_b;
int max_seg;
int target_index;

st_b = *p_b;
while (i++ < min_index)
max_seg = _calc_max_segment(st_b);
target_index = _calc_target_index(st_b, max_seg);
i = 0;
while (i++ < target_index)
st_b = st_b->next;
get_content(st_b)->is_opt = true;
}
121 changes: 119 additions & 2 deletions test/test_set_is_opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
// (-1, 0)
// 4 4 1 1: 1

TEST(set_opt, _set_is_opt1) {
TEST(set_is_opt, case1_segment_not_set) {
int N;
t_stack *stack_a;
t_stack *stack_b;
Expand Down Expand Up @@ -58,6 +58,61 @@ TEST(set_opt, _set_is_opt1) {
}
}

// stack_a | stack_b
// 0 | 1 <- RA_RB: (0, 1), RRA_RB: (0, -4), RA_RRB: (0, 1), RRA_RRB:
// (0, -4)
// 1 4 1 4: 1
// ==== segment -1 ====
// 2 | 3 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2),
// RRA_RRB: (-4, -3)
// 2 4 6 4: 2
// ==== segment 1 ====
// 4 | 5 <- RA_RB: (2, 3), RRA_RB: (2, -2), RA_RRB: (-3, 3),
// RRA_RRB: (-3, -2)
// 3 4 6 3: 3
// ==== segment -1 ====
// 6 | 7 <- RA_RB: (3, 4), RRA_RB: (3, -1), RA_RRB: (-2, 4),
// RRA_RRB: (-2, -1)
// 4 4 6 2: 2
// ==== segment -1 ====
// 8 | 9 <- RA_RB: (4, 0), RRA_RB: (4, 0), RA_RRB: (-1, 0), RRA_RRB:
// (-1, 0)
// 4 4 1 1: 1
// ==== segment 0 ====> target

TEST(set_is_opt, case1_segment_set) {
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, -1, 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));
}
// segment set
get_content(ft_lstlast(stack_b))->segment = 0;
set_cost(&stack_a, &stack_b);
set_min_cost_opt_method(&stack_b);
set_is_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;
}
}

// stack_a | stack_b
// 6 | 8 <- RA_RB: (0, 2), RRA_RB: (0, -3), RA_RRB: (0, 2),
// RRA_RRB: (0, -3)
Expand All @@ -75,7 +130,7 @@ TEST(set_opt, _set_is_opt1) {
// RRA_RRB: (-1, 0)
// 4 1 4 1: 1

TEST(set_opt, _set_is_opt2) {
TEST(set_is_opt, case2_segment_not_set) {
int N = 5;
t_stack *stack_a;
t_stack *stack_b;
Expand Down Expand Up @@ -108,3 +163,65 @@ TEST(set_opt, _set_is_opt2) {
st = st->next;
}
}

// stack_a | stack_b
// 6 | 8 <- RA_RB: (0, 2), RRA_RB: (0, -3), RA_RRB: (0, 2),
// RRA_RRB: (0, -3)
// 2 3 2 3: 2
// ==== segment 0 ====
// 7 | 9 <- RA_RB: (1, 2), RRA_RB: (1, -3), RA_RRB: (-4, 2),
// RRA_RRB: (-4, -3)
// 2 4 6 4: 2
// ==== segment 1 ====
// 1 | 0 <- RA_RB: (2, 2), RRA_RB: (2, -3), RA_RRB: (2, 2),
// RRA_RRB: (2, -3)
// 2 5 2 5: 2
// ==== segment 2 ====
// 3 | 2 <- RA_RB: (3, 3), RRA_RB: (3, -2), RA_RRB: (-2, 3),
// RRA_RRB: (-2, -2)
// 3 5 5 2: 2
// ==== segment 3 ====
// 4 | 5 <- RA_RB: (4, 0), RRA_RB: (-1, 0), RA_RRB: (4, 0),
// RRA_RRB: (-1, 0)
// 4 1 4 1: 1
// ==== segment -1 ====

TEST(set_is_opt, case2_segment_set) {
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, -1, 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, -1, 0, 0, 0, 0, 0, INIT, false});
ft_lstadd_back(&stack_b, ft_lstnew(c));
}

// segment set
get_content(stack_b)->segment = 0;
get_content(stack_b->next)->segment = 1;
get_content(stack_b->next->next)->segment = 2;
get_content(stack_b->next->next->next)->segment = 3;

set_cost(&stack_a, &stack_b);
set_min_cost_opt_method(&stack_b);
set_is_opt(&stack_b);

t_stack *st = stack_b;

for (int i = 0; i < N; ++i) {
if (i == 3)
EXPECT_EQ(get_content(st)->is_opt, true);
else
EXPECT_EQ(get_content(st)->is_opt, false);
st = st->next;
}
}
8 changes: 4 additions & 4 deletions test/test_set_min_cost_opt_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
// (-1, 0)
// 4 4 1 1: 1

TEST(set_opt, _set_opt_method1) {
TEST(set_min_cost_opt_method, _set_opt_method1) {
int N;
t_stack *stack_a;
t_stack *stack_b;
Expand Down Expand Up @@ -60,7 +60,7 @@ TEST(set_opt, _set_opt_method1) {
EXPECT_EQ(get_content(target)->opt_method, RA_RRB);
}

TEST(set_opt, _set_min_cost1) {
TEST(set_min_cost_opt_method, _set_min_cost1) {
int N;
t_stack *stack_a;
t_stack *stack_b;
Expand Down Expand Up @@ -110,7 +110,7 @@ TEST(set_opt, _set_min_cost1) {
// RRA_RRB: (-1, 0)
// 4 1 4 1: 1

TEST(set_opt, _set_opt_method2) {
TEST(set_min_cost_opt_method, _set_opt_method2) {
t_stack *stack_a;
t_stack *stack_b;
t_content *c;
Expand Down Expand Up @@ -145,7 +145,7 @@ TEST(set_opt, _set_opt_method2) {
EXPECT_EQ(get_content(target)->opt_method, RA_RRB);
}

TEST(set_opt, _set_min_cost2) {
TEST(set_min_cost_opt_method, _set_min_cost2) {
t_stack *stack_a;
t_stack *stack_b;
t_content *c;
Expand Down

0 comments on commit 314606a

Please sign in to comment.