Skip to content

Commit

Permalink
refactor with set_cost
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Feb 8, 2024
1 parent 4acf5ae commit cc94925
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 65 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/sort/micro_sort.c \
$(SRC_DIR)/sort/nano_sort.c \
$(SRC_DIR)/sort/large_sort.c \
$(SRC_DIR)/sort/set_selection_cost.c \
$(SRC_DIR)/sort/set_insertion_cost.c \
$(SRC_DIR)/sort/set_cost.c \
$(SRC_DIR)/stack_operations/push.c \
$(SRC_DIR)/stack_operations/swap.c \
$(SRC_DIR)/stack_operations/rotate.c \
Expand Down
5 changes: 2 additions & 3 deletions 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/08 07:44:33 by reasuke ### ########.fr */
/* Updated: 2024/02/08 13:35:51 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,8 +38,7 @@ void nano_sort(t_stack **p_a, int num_a);
void micro_sort(t_stack **p_a, t_stack **p_b, int num_a);
void large_sort(t_stack **p_a, t_stack **p_b, int num_a);

void set_selection_cost(t_stack **p_b);
void set_insertion_cost(t_stack **p_a, t_stack **p_b);
void set_cost(t_stack **p_a, t_stack **p_b);

void operate_sa(t_stack **p_a);
void operate_sb(t_stack **p_b);
Expand Down
5 changes: 2 additions & 3 deletions src/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/08 07:33:42 by reasuke ### ########.fr */
/* Updated: 2024/02/08 13:34:33 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,6 +28,5 @@ void large_sort(t_stack **p_a, t_stack **p_b, int num_a)
{
_init_stack(p_a, p_b, num_a);
nano_sort(p_a, 3);
set_selection_cost(p_b);
set_insertion_cost(p_a, p_b);
set_cost(p_a, p_b);
}
51 changes: 28 additions & 23 deletions src/sort/set_insertion_cost.c → src/sort/set_cost.c
Original file line number Diff line number Diff line change
@@ -1,80 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_insertion_cost.c :+: :+: :+: */
/* set_cost.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/08 07:32:05 by reasuke #+# #+# */
/* Updated: 2024/02/08 10:09:34 by reasuke ### ########.fr */
/* Created: 2024/02/08 12:17:33 by reasuke #+# #+# */
/* Updated: 2024/02/08 21:38:17 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

static void _set_insertion_forward_cost(t_stack **p_a, t_stack *st_b)
static int _calc_insertion_threshold(t_stack **p_a, t_stack *st_b)
{
t_stack *st_a;
int size_a;
int i;
int threshold;
int max_a;
int max_index;
int min;

st_a = *p_a;
size_a = stack_size(*p_a);
i = 0;
threshold = INT_MAX;
min = INT_MAX;
max_a = INT_MIN;
while (i < size_a)
{
if (get_content(st_a)->index > get_content(st_b)->index
&& ft_chmin(&min, get_content(st_a)->index))
threshold = i;
if (ft_chmax(&max_a, get_content(st_a)->index))
max_index = i;
st_a = st_a->next;
i++;
}
if (threshold == INT_MAX)
threshold = (max_index + 1) % size_a;
get_content(st_b)->if_cost = threshold % size_a;
return (threshold);
}

static void _set_insertion_reverse_cost(t_stack **p_a, t_stack *st_b)
static int _calc_max_index(t_stack **p_a)
{
t_stack *st_a;
int size_a;
int i;
int threshold;
int max_a;
int max_index;
int min;

st_a = *p_a;
size_a = stack_size(*p_a);
i = 0;
threshold = INT_MAX;
min = INT_MAX;
max_a = INT_MIN;
while (i < size_a)
{
if (get_content(st_a)->index > get_content(st_b)->index
&& ft_chmin(&min, get_content(st_a)->index))
threshold = i;
if (ft_chmax(&max_a, get_content(st_a)->index))
max_index = i;
st_a = st_a->next;
i++;
}
return (max_index);
}

static void _set_insertion_cost(t_stack **p_a, t_stack *st_b)
{
int max_index;
int size_a;
int threshold;

size_a = stack_size(*p_a);
threshold = _calc_insertion_threshold(p_a, 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);
}

void set_insertion_cost(t_stack **p_a, t_stack **p_b)
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);
}

void set_cost(t_stack **p_a, t_stack **p_b)
{
t_stack *st_b;
int size_b;
Expand All @@ -85,8 +90,8 @@ void set_insertion_cost(t_stack **p_a, t_stack **p_b)
i = 0;
while (i < size_b)
{
_set_insertion_forward_cost(p_a, st_b);
_set_insertion_reverse_cost(p_a, st_b);
_set_selection_cost(st_b, i, size_b);
_set_insertion_cost(p_a, st_b);
st_b = st_b->next;
i++;
}
Expand Down
31 changes: 0 additions & 31 deletions src/sort/set_selection_cost.c

This file was deleted.

8 changes: 5 additions & 3 deletions test/test_set_cost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ extern "C" {

TEST(set_selection_cost, selection) {
int N = 5;
t_stack *stack_a = NULL;
t_stack *stack_b = NULL;
for (int i = 0; i < N; ++i) {
t_content *c = new t_content({i, 0, 0, 0, 0});
ft_lstadd_back(&stack_a, ft_lstnew(c));
ft_lstadd_back(&stack_b, ft_lstnew(c));
}

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

// index: 2
t_stack *target = stack_b->next->next;
Expand Down Expand Up @@ -53,7 +55,7 @@ TEST(set_insertion_cost, insertion1) {
ft_lstadd_back(&stack_b, ft_lstnew(c));
}

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

t_stack *target;
int if_cost;
Expand Down Expand Up @@ -99,7 +101,7 @@ TEST(set_insertion_cost, insertion2) {
ft_lstadd_back(&stack_b, ft_lstnew(c));
}

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

t_stack *target;
int if_cost;
Expand Down

0 comments on commit cc94925

Please sign in to comment.