Skip to content

Commit

Permalink
update push b segmented (#34)
Browse files Browse the repository at this point in the history
* rename opt

* add member segment to t_content

* set segment

* refactor test

* implement segment based optimization

* rename is_opt -> is_target
  • Loading branch information
rask24 committed Feb 14, 2024
1 parent 62dd0c6 commit 2fc4ec9
Show file tree
Hide file tree
Showing 18 changed files with 531 additions and 295 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/sort/large_sort/large_sort.c \
$(SRC_DIR)/sort/large_sort/push_b_segmented.c \
$(SRC_DIR)/sort/large_sort/set_cost.c \
$(SRC_DIR)/sort/large_sort/set_opt.c \
$(SRC_DIR)/sort/large_sort/set_min_cost_opt_method.c \
$(SRC_DIR)/sort/large_sort/set_is_target.c \
$(SRC_DIR)/sort/large_sort/greedy_operation.c \
$(SRC_DIR)/stack_operations/push.c \
$(SRC_DIR)/stack_operations/swap.c \
Expand Down Expand Up @@ -65,7 +66,8 @@ TEST_SRC = $(TEST_DIR)/test_check_args.cpp \
$(TEST_DIR)/test_swap_stack.cpp \
$(TEST_DIR)/test_push_b_segmented.cpp \
$(TEST_DIR)/test_set_cost.cpp \
$(TEST_DIR)/test_set_opt_method.cpp \
$(TEST_DIR)/test_set_min_cost_opt_method.cpp \
$(TEST_DIR)/test_set_is_target.cpp \
$(TEST_DIR)/test_greedy_operation.cpp \
$(TEST_DIR)/test_is_sorted_stack.cpp \
$(TEST_DIR)/test_sort.cpp
Expand Down
5 changes: 3 additions & 2 deletions include/large_sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:36:38 by reasuke #+# #+# */
/* Updated: 2024/02/12 18:32:55 by reasuke ### ########.fr */
/* Updated: 2024/02/14 16:41:29 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,7 +18,8 @@
void large_sort(t_stack **p_a, t_stack **p_b);
void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int num_seg);
void set_cost(t_stack **p_a, t_stack **p_b);
void set_opt(t_stack **p_b);
void set_min_cost_opt_method(t_stack **p_b);
void set_is_target(t_stack **p_b);
void greedy_operation(t_stack **p_a, t_stack **p_b);

#endif
21 changes: 11 additions & 10 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:41 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:58:04 by reasuke ### ########.fr */
/* Updated: 2024/02/14 16:40:21 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,21 +18,22 @@ typedef t_list t_stack;
typedef enum e_method
{
INIT,
FF,
FR,
RF,
RR,
RA_RB,
RRA_RB,
RA_RRB,
RRA_RRB,
} t_method;

typedef struct s_content
{
int index;
int sf_cost;
int sr_cost;
int if_cost;
int ir_cost;
int segment;
int rb_cost;
int rrb_cost;
int ra_cost;
int rra_cost;
int min_cost;
t_method opt_method;
bool is_opt;
bool is_target;
} t_content;
#endif
3 changes: 2 additions & 1 deletion 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 14:01:23 by reasuke ### ########.fr */
/* Updated: 2024/02/14 14:28:39 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -103,6 +103,7 @@ t_stack *generate_stack(int argc, char **argv)
if (!ptr)
exit_with_error();
ptr->index = comp[i];
ptr->segment = -1;
ft_lstadd_back(&st, ft_lstnew(ptr));
i++;
}
Expand Down
32 changes: 16 additions & 16 deletions src/sort/large_sort/greedy_operation.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 10:43:16 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:53:46 by reasuke ### ########.fr */
/* Updated: 2024/02/14 16:40:21 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,7 +19,7 @@ static t_stack *_find_opt_st_b(t_stack **p_b)
t_stack *st_b;

st_b = *p_b;
while (!get_content(st_b)->is_opt)
while (!get_content(st_b)->is_target)
st_b = st_b->next;
return (st_b);
}
Expand All @@ -32,19 +32,19 @@ static void _do_alined_operation(t_stack **p_a, t_stack **p_b,
int if_abs;
int ir_abs;

sf_abs = ft_abs(get_content(opt_st_b)->sf_cost);
sr_abs = ft_abs(get_content(opt_st_b)->sr_cost);
if_abs = ft_abs(get_content(opt_st_b)->if_cost);
ir_abs = ft_abs(get_content(opt_st_b)->ir_cost);
if (get_content(opt_st_b)->opt_method == FF)
sf_abs = ft_abs(get_content(opt_st_b)->rb_cost);
sr_abs = ft_abs(get_content(opt_st_b)->rrb_cost);
if_abs = ft_abs(get_content(opt_st_b)->ra_cost);
ir_abs = ft_abs(get_content(opt_st_b)->rra_cost);
if (get_content(opt_st_b)->opt_method == RA_RB)
{
do_double_n_operations(p_a, p_b, ft_min(sf_abs, if_abs), operate_rr);
if (sf_abs > if_abs)
do_single_n_operations(p_b, sf_abs - if_abs, operate_rb);
else if (sf_abs < if_abs)
do_single_n_operations(p_a, if_abs - sf_abs, operate_ra);
}
if (get_content(opt_st_b)->opt_method == RR)
if (get_content(opt_st_b)->opt_method == RRA_RRB)
{
do_double_n_operations(p_a, p_b, ft_min(sr_abs, ir_abs), operate_rrr);
if (sr_abs > ir_abs)
Expand All @@ -61,16 +61,16 @@ static void _do_mixed_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b)
int if_abs;
int ir_abs;

sf_abs = ft_abs(get_content(opt_st_b)->sf_cost);
sr_abs = ft_abs(get_content(opt_st_b)->sr_cost);
if_abs = ft_abs(get_content(opt_st_b)->if_cost);
ir_abs = ft_abs(get_content(opt_st_b)->ir_cost);
if (get_content(opt_st_b)->opt_method == FR)
sf_abs = ft_abs(get_content(opt_st_b)->rb_cost);
sr_abs = ft_abs(get_content(opt_st_b)->rrb_cost);
if_abs = ft_abs(get_content(opt_st_b)->ra_cost);
ir_abs = ft_abs(get_content(opt_st_b)->rra_cost);
if (get_content(opt_st_b)->opt_method == RRA_RB)
{
do_single_n_operations(p_b, sf_abs, operate_rb);
do_single_n_operations(p_a, ir_abs, operate_rra);
}
if (get_content(opt_st_b)->opt_method == RF)
if (get_content(opt_st_b)->opt_method == RA_RRB)
{
do_single_n_operations(p_b, sr_abs, operate_rrb);
do_single_n_operations(p_a, if_abs, operate_ra);
Expand All @@ -82,9 +82,9 @@ static void _do_opt_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b)
t_method opt_method;

opt_method = get_content(opt_st_b)->opt_method;
if (opt_method == FF || opt_method == RR)
if (opt_method == RA_RB || opt_method == RRA_RRB)
_do_alined_operation(p_a, p_b, opt_st_b);
else if (opt_method == FR || opt_method == RF)
else if (opt_method == RRA_RB || opt_method == RA_RRB)
_do_mixed_operation(p_a, p_b, opt_st_b);
operate_pa(p_b, p_a);
}
Expand Down
5 changes: 3 additions & 2 deletions src/sort/large_sort/large_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/22 16:55:10 by reasuke #+# #+# */
/* Updated: 2024/02/13 13:28:59 by reasuke ### ########.fr */
/* Updated: 2024/02/14 16:41:29 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -58,7 +58,8 @@ void large_sort(t_stack **p_a, t_stack **p_b)
while (size_b--)
{
set_cost(p_a, p_b);
set_opt(p_b);
set_min_cost_opt_method(p_b);
set_is_target(p_b);
greedy_operation(p_a, p_b);
}
_sort_stack_a(p_a, size_a);
Expand Down
7 changes: 5 additions & 2 deletions src/sort/large_sort/push_b_segmented.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 22:57:28 by reasuke #+# #+# */
/* Updated: 2024/02/13 15:01:23 by reasuke ### ########.fr */
/* Updated: 2024/02/14 14:31:29 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -62,15 +62,18 @@ void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs)
{
int pushed;
int index;
int segment;

pushed = 0;
while (pushed < n)
{
index = get_content(*p_a)->index;
if (_should_push_b(index, pushed, n, segs))
{
segment = _calc_segment_id(index, n, segs);
get_content(*p_a)->segment = segment;
operate_pb(p_a, p_b);
if (_calc_segment_id(index, n, segs) % 2 == 1)
if (segment % 2 == 1)
operate_rb(p_b);
pushed++;
}
Expand Down
10 changes: 5 additions & 5 deletions src/sort/large_sort/set_cost.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/08 12:17:33 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:53:50 by reasuke ### ########.fr */
/* Updated: 2024/02/14 14:15:57 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -71,14 +71,14 @@ static void _set_insertion_cost(t_stack **p_a, t_stack *st_b)
max_index = _calc_max_index(p_a);
if (threshold == INT_MAX)
threshold = (max_index + 1) % size_a;
get_content(st_b)->if_cost = threshold % size_a;
get_content(st_b)->ir_cost = -((size_a - threshold) % size_a);
get_content(st_b)->ra_cost = threshold % size_a;
get_content(st_b)->rra_cost = -((size_a - threshold) % size_a);
}

static void _set_selection_cost(t_stack *st_b, int i, int size_b)
{
get_content(st_b)->sf_cost = i;
get_content(st_b)->sr_cost = -((size_b - i) % size_b);
get_content(st_b)->rb_cost = i;
get_content(st_b)->rrb_cost = -((size_b - i) % size_b);
}

void set_cost(t_stack **p_a, t_stack **p_b)
Expand Down
63 changes: 63 additions & 0 deletions src/sort/large_sort/set_is_target.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_is_target.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/14 14:44:38 by reasuke #+# #+# */
/* Updated: 2024/02/14 16:42:11 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

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

static int _calc_max_segment(t_stack *st_b)
{
int max_seg;

max_seg = INT_MIN;
while (st_b)
{
ft_chmax(&max_seg, get_content(st_b)->segment);
st_b = st_b->next;
}
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_target(t_stack **p_b)
{
int i;
t_stack *st_b;
int max_seg;
int target_index;

st_b = *p_b;
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_target = true;
}
68 changes: 68 additions & 0 deletions src/sort/large_sort/set_min_cost_opt_method.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_min_cost_opt_method.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 10:23:28 by reasuke #+# #+# */
/* Updated: 2024/02/14 15:44:15 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

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

static int _calc_cost(t_stack *st_b, t_method method)
{
int rb_cost;
int rrb_cost;
int ra_cost;
int rra_cost;

rb_cost = get_content(st_b)->rb_cost;
rrb_cost = get_content(st_b)->rrb_cost;
ra_cost = get_content(st_b)->ra_cost;
rra_cost = get_content(st_b)->rra_cost;
if (method == RA_RB)
return (ft_max(rb_cost, ra_cost));
else if (method == RRA_RB)
return (rb_cost - rra_cost);
else if (method == RA_RRB)
return (ra_cost - rrb_cost);
else if (method == RRA_RRB)
return (ft_max(-rrb_cost, -rra_cost));
return (-1);
}

static void _set_opt_method(t_stack *st_b, int *cost)
{
if (ft_chmin(cost, _calc_cost(st_b, RA_RB)))
get_content(st_b)->opt_method = RA_RB;
if (ft_chmin(cost, _calc_cost(st_b, RRA_RB)))
get_content(st_b)->opt_method = RRA_RB;
if (ft_chmin(cost, _calc_cost(st_b, RA_RRB)))
get_content(st_b)->opt_method = RA_RRB;
if (ft_chmin(cost, _calc_cost(st_b, RRA_RRB)))
get_content(st_b)->opt_method = RRA_RRB;
}

void set_min_cost_opt_method(t_stack **p_b)
{
int i;
int size_b;
int cost;
t_stack *st_b;

i = 0;
size_b = stack_size(*p_b);
st_b = *p_b;
while (i < size_b)
{
cost = INT_MAX;
_set_opt_method(st_b, &cost);
get_content(st_b)->min_cost = cost;
st_b = st_b->next;
i++;
}
}
Loading

0 comments on commit 2fc4ec9

Please sign in to comment.