-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rename repeat_stack_operations * rename repeat_dual_stack_operations * define t_operation * refactor
- Loading branch information
Showing
5 changed files
with
45 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
} | ||
} | ||
|
||
|
10 changes: 5 additions & 5 deletions
10
...stack_operations/do_double_n_operations.c → ...operations/repeat_dual_stack_operations.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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); | ||
} |
9 changes: 4 additions & 5 deletions
9
...stack_operations/do_single_n_operations.c → ...tack_operations/repeat_stack_operations.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* 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); | ||
} |