Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/rename stack operation #53

Merged
merged 4 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/stack_operations/swap.c \
$(SRC_DIR)/stack_operations/rotate.c \
$(SRC_DIR)/stack_operations/reverse_rotate.c \
$(SRC_DIR)/stack_operations/do_single_n_operations.c \
$(SRC_DIR)/stack_operations/do_double_n_operations.c \
$(SRC_DIR)/stack_operations/repeat_stack_operations.c \
$(SRC_DIR)/stack_operations/repeat_dual_stack_operations.c \
$(SRC_DIR)/utils/get_first_index.c \
$(SRC_DIR)/utils/get_second_index.c \
$(SRC_DIR)/utils/get_third_index.c \
Expand Down
12 changes: 7 additions & 5 deletions include/stack_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:41:01 by reasuke #+# #+# */
/* Updated: 2024/02/20 14:16:47 by reasuke ### ########.fr */
/* Updated: 2024/04/20 15:40:33 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,15 +15,17 @@

# include "push_swap.h"

typedef void t_operation(t_stack **);
typedef void t_dual_operation(t_stack **, t_stack **);

void push_stack(t_stack **p_s1, t_stack **p_s2);
void swap_stack(t_stack **p_stack);
void rotate_stack(t_stack **p_stack);
void reverse_rotate_stack(t_stack **p_stack);

void do_single_n_operations(t_stack **p_st, int n,
void (*operation)(t_stack **));
void do_double_n_operations(t_stack **p_a, t_stack **p_b, int n,
void (*operation)(t_stack **, t_stack **));
void repeat_stack_operations(t_stack **p_st, int n, t_operation *op);
void repeat_dual_stack_operations(t_stack **p_a, t_stack **p_b, int n,
t_dual_operation *op);

void operate_sa(t_stack **p_a);
void operate_sb(t_stack **p_b);
Expand Down
58 changes: 27 additions & 31 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 <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */
/* Updated: 2024/02/20 14:17:27 by reasuke ### ########.fr */
/* Updated: 2024/04/20 16:01:25 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,53 +26,49 @@ static t_stack *_find_opt_st_b(t_stack **p_b)
static void _do_alined_operation(t_stack **p_a, t_stack **p_b,
t_stack *opt_st_b)
{
int sf_abs;
int sr_abs;
int if_abs;
int ir_abs;
int n_a;
int n_b;

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);
n_b = ft_abs(get_content(opt_st_b)->rb_cost);
n_a = ft_abs(get_content(opt_st_b)->ra_cost);
repeat_dual_stack_operations(p_a, p_b, ft_min(n_b, n_a), operate_rr);
if (n_b > n_a)
repeat_stack_operations(p_b, n_b - n_a, operate_rb);
else if (n_b < n_a)
repeat_stack_operations(p_a, n_a - n_b, operate_ra);
}
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)
do_single_n_operations(p_b, sr_abs - ir_abs, operate_rrb);
else if (sr_abs < ir_abs)
do_single_n_operations(p_a, ir_abs - sr_abs, operate_rra);
n_b = ft_abs(get_content(opt_st_b)->rrb_cost);
n_a = ft_abs(get_content(opt_st_b)->rra_cost);
repeat_dual_stack_operations(p_a, p_b, ft_min(n_b, n_a), operate_rrr);
if (n_b > n_a)
repeat_stack_operations(p_b, n_b - n_a, operate_rrb);
else if (n_b < n_a)
repeat_stack_operations(p_a, n_a - n_b, operate_rra);
}
}

static void _do_mixed_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b)
{
int sf_abs;
int sr_abs;
int if_abs;
int ir_abs;
int n_a;
int n_b;

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);
n_a = ft_abs(get_content(opt_st_b)->rra_cost);
n_b = ft_abs(get_content(opt_st_b)->rb_cost);
repeat_stack_operations(p_b, n_b, operate_rb);
repeat_stack_operations(p_a, n_a, operate_rra);
}
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);
n_a = ft_abs(get_content(opt_st_b)->ra_cost);
n_b = ft_abs(get_content(opt_st_b)->rrb_cost);
repeat_stack_operations(p_b, n_b, operate_rrb);
repeat_stack_operations(p_a, n_a, operate_ra);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* do_double_n_operations.c :+: :+: :+: */
/* repeat_dual_stack_operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 19:22:50 by reasuke #+# #+# */
/* Updated: 2024/02/20 14:15:32 by reasuke ### ########.fr */
/* Updated: 2024/04/20 15:40:46 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "stack_operations.h"

void do_double_n_operations(t_stack **p_a, t_stack **p_b, int n,
void (*operation)(t_stack **, t_stack **))
void repeat_dual_stack_operations(t_stack **p_a, t_stack **p_b, int n,
t_dual_operation *op)
{
while (n--)
operation(p_a, p_b);
op(p_a, p_b);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* do_single_n_operations.c :+: :+: :+: */
/* repeat_stack_operations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 19:22:26 by reasuke #+# #+# */
/* Updated: 2024/02/20 14:15:35 by reasuke ### ########.fr */
/* Updated: 2024/04/20 15:39:53 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "stack_operations.h"

void do_single_n_operations(t_stack **p_st, int n,
void (*operation)(t_stack **))
void repeat_stack_operations(t_stack **p_st, int n, t_operation *op)
{
while (n--)
operation(p_st);
op(p_st);
}