-
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.
* implement set_selection_cost * format test * add test set_cost_forward * refactor with get_content * implement set_insertion_cost * refactor with set_cost * fix build for gcc * implement set_opt_method * refactor with libft * refactor set_opt * add min_cost * add is_opt * fix build for gcc * fix test for gcc * fix test_sort for gcc * implement greedy_operation * implement is_sorted_stack * implement sort * refactor greedy_operation * rename get_index * refactor with initialization * update sort * update Makefile
- Loading branch information
Showing
33 changed files
with
1,196 additions
and
261 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/01/10 12:37:54 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/07 15:04:00 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/10 21:18:02 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -20,22 +20,40 @@ | |
# include <unistd.h> | ||
|
||
typedef t_list t_stack; | ||
|
||
typedef enum e_method | ||
{ | ||
INIT, | ||
FF, | ||
FR, | ||
RF, | ||
RR, | ||
} t_method; | ||
|
||
typedef struct s_content | ||
{ | ||
int index; | ||
int delta_a; | ||
int delta_b; | ||
int target; | ||
int sf_cost; | ||
int sr_cost; | ||
int if_cost; | ||
int ir_cost; | ||
int min_cost; | ||
t_method opt_method; | ||
bool is_opt; | ||
} t_content; | ||
|
||
int check_args(int argc, char **argv); | ||
|
||
t_stack *generate_stack(int argc, char **argv); | ||
void exit_with_error(void); | ||
|
||
void sort(t_stack **p_a, t_stack **p_b, int num_a); | ||
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 sort(t_stack **p_a, t_stack **p_b); | ||
void nano_sort(t_stack **p_a); | ||
void micro_sort(t_stack **p_a, t_stack **p_b); | ||
void large_sort(t_stack **p_a, t_stack **p_b); | ||
void set_cost(t_stack **p_a, t_stack **p_b); | ||
void set_opt(t_stack **p_b); | ||
void greedy_operation(t_stack **p_a, t_stack **p_b); | ||
bool is_sorted_stack(t_stack *st); | ||
|
||
void operate_sa(t_stack **p_a); | ||
void operate_sb(t_stack **p_b); | ||
|
@@ -48,18 +66,21 @@ void operate_rrb(t_stack **p_a); | |
void operate_rrr(t_stack **p_a, t_stack **p_b); | ||
void operate_pa(t_stack **p_b, t_stack **p_a); | ||
void operate_pb(t_stack **p_a, t_stack **p_b); | ||
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 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); | ||
t_list *ft_lst_before(t_list *lst, t_list *trg); | ||
|
||
int first_content(t_stack **p_stack); | ||
int second_content(t_stack **p_stack); | ||
int third_content(t_stack **p_stack); | ||
void exit_with_error(void); | ||
void clear_stack(t_stack **p_stack, void (*del)(void *)); | ||
t_content *get_content(t_stack *st); | ||
int get_first_index(t_stack **p_stack); | ||
int get_second_index(t_stack **p_stack); | ||
int get_third_index(t_stack **p_stack); | ||
int stack_size(t_stack *stack); | ||
|
||
#endif |
Submodule libft
updated
5 files
+2 −0 | Makefile | |
+20 −0 | integer/ft_abs.c | |
+80 −76 | libft.h | |
+27 −0 | list/ft_lstbefore.c | |
+86 −40 | test/gtest.cpp |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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/01/10 12:37:36 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/07 15:12:35 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/10 21:19:08 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -41,7 +41,7 @@ int main(int argc, char **argv) | |
check_args(argc, argv); | ||
a = generate_stack(argc, argv); | ||
b = NULL; | ||
sort(&a, &b, argc - 1); | ||
sort(&a, &b); | ||
clear_stack(&a, free); | ||
return (0); | ||
} |
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,26 +1,28 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* large_sort.c :+: :+: :+: */ | ||
/* is_sorted_stack.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/06 17:45:36 by reasuke ### ########.fr */ | ||
/* Created: 2024/02/10 18:47:11 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/10 19:12:48 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
// naive implement: insertion sort | ||
// find smallest number of a | ||
// rotate | ||
// push to b | ||
// push to a | ||
|
||
void large_sort(t_stack **p_a, t_stack **p_b, int num_a) | ||
bool is_sorted_stack(t_stack *st) | ||
{ | ||
(void)p_a; | ||
(void)p_b; | ||
(void)num_a; | ||
int current; | ||
|
||
current = 1; | ||
while (st) | ||
{ | ||
if (get_content(st)->index != current) | ||
return (false); | ||
current++; | ||
st = st->next; | ||
} | ||
return (true); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* greedy_operation.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/10 20:34:46 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
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) | ||
st_b = st_b->next; | ||
return (st_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; | ||
|
||
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) | ||
{ | ||
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) | ||
{ | ||
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); | ||
} | ||
} | ||
|
||
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; | ||
|
||
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) | ||
{ | ||
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) | ||
{ | ||
do_single_n_operations(p_b, sr_abs, operate_rrb); | ||
do_single_n_operations(p_a, if_abs, operate_ra); | ||
} | ||
} | ||
|
||
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) | ||
_do_alined_operation(p_a, p_b, opt_st_b); | ||
else if (opt_method == FR || opt_method == RF) | ||
_do_mixed_operation(p_a, p_b, opt_st_b); | ||
operate_pa(p_b, p_a); | ||
} | ||
|
||
void greedy_operation(t_stack **p_a, t_stack **p_b) | ||
{ | ||
t_stack *opt_st_b; | ||
|
||
opt_st_b = _find_opt_st_b(p_b); | ||
_do_opt_operation(p_a, p_b, opt_st_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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* large_sort.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/10 21:24:58 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
static void _push_b_n_times(t_stack **p_a, t_stack **p_b, int n) | ||
{ | ||
while (n--) | ||
operate_pb(p_a, p_b); | ||
} | ||
|
||
static void _sort_stack_a(t_stack **p_a, int num_a) | ||
{ | ||
if (get_content(*p_a)->index > num_a / 2) | ||
{ | ||
while (!is_sorted_stack(*p_a)) | ||
operate_ra(p_a); | ||
} | ||
else | ||
{ | ||
while (!is_sorted_stack(*p_a)) | ||
operate_rra(p_a); | ||
} | ||
} | ||
|
||
void large_sort(t_stack **p_a, t_stack **p_b) | ||
{ | ||
int size_a; | ||
int size_b; | ||
|
||
size_a = stack_size(*p_a); | ||
size_b = size_a - 3; | ||
_push_b_n_times(p_a, p_b, size_b); | ||
nano_sort(p_a); | ||
while (size_b--) | ||
{ | ||
set_cost(p_a, p_b); | ||
set_opt(p_b); | ||
greedy_operation(p_a, p_b); | ||
} | ||
_sort_stack_a(p_a, size_a); | ||
} |
Oops, something went wrong.