-
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 large sort * add visualizer push-swap-pain * implement operate_rb * implement stack_size * implement all oparations * fix filename * refactor witch t_content * update test * format
- Loading branch information
Showing
21 changed files
with
282 additions
and
85 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,48 +6,60 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:51:40 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:04:00 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef PUSH_SWAP_H | ||
# define PUSH_SWAP_H | ||
|
||
# include "../libft/libft.h" | ||
# include <limits.h> | ||
# include <stdbool.h> | ||
# include <stdlib.h> | ||
# include <unistd.h> | ||
|
||
// TODO: delete | ||
# include <stdio.h> | ||
typedef t_list t_stack; | ||
typedef struct s_content | ||
{ | ||
int index; | ||
int delta_a; | ||
int delta_b; | ||
int target; | ||
} t_content; | ||
|
||
# include "../libft/libft.h" | ||
int check_args(int argc, char **argv); | ||
|
||
typedef t_list t_stack; | ||
t_stack *generate_stack(int argc, char **argv); | ||
|
||
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 operate_sa(t_stack **p_a); | ||
void operate_sb(t_stack **p_b); | ||
void operate_ss(t_stack **p_a, t_stack **p_b); | ||
void operate_ra(t_stack **p_a); | ||
void operate_rb(t_stack **p_b); | ||
void operate_rr(t_stack **p_a, t_stack **p_b); | ||
void operate_rra(t_stack **p_a); | ||
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 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 check_args(int argc, char **argv); | ||
|
||
t_stack *generate_stack(int argc, char **argv); | ||
|
||
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 operate_sa(t_stack **p_a); | ||
void operate_sb(t_stack **p_b); | ||
void operate_ra(t_stack **p_a); | ||
void operate_rra(t_stack **p_a); | ||
void operate_pa(t_stack **p_b, t_stack **p_a); | ||
void operate_pb(t_stack **p_a, t_stack **p_b); | ||
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); | ||
void exit_with_error(void); | ||
void clear_stack(t_stack **p_stack, void (*del)(void *)); | ||
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 *)); | ||
int stack_size(t_stack *stack); | ||
|
||
#endif |
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/11 14:44:55 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 13:18:39 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:06:08 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -66,8 +66,7 @@ static bool _has_duplicate(int argc, char **argv) | |
|
||
int check_args(int argc, char **argv) | ||
{ | ||
if (_has_not_digit(argc, argv) | ||
|| _has_overflow(argc, argv) | ||
if (_has_not_digit(argc, argv) || _has_overflow(argc, argv) | ||
|| _has_duplicate(argc, argv)) | ||
exit_with_error(); | ||
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:10:51 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:06:20 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -90,20 +90,20 @@ int *_coordinate_compression(int argc, char **argv) | |
|
||
t_stack *generate_stack(int argc, char **argv) | ||
{ | ||
t_stack *stack; | ||
int *compressed_array; | ||
int i; | ||
int *ptr; | ||
t_stack *stack; | ||
int *compressed_array; | ||
int i; | ||
t_content *ptr; | ||
|
||
compressed_array = _coordinate_compression(argc, argv); | ||
i = 0; | ||
stack = NULL; | ||
while (i < argc - 1) | ||
{ | ||
ptr = ft_calloc(1, sizeof(int)); | ||
ptr = ft_calloc(1, sizeof(t_content)); | ||
if (!ptr) | ||
exit_with_error(); | ||
*ptr = compressed_array[i]; | ||
ptr->index = compressed_array[i]; | ||
ft_lstadd_back(&stack, ft_lstnew(ptr)); | ||
i++; | ||
} | ||
|
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/01/23 15:48:43 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:12:35 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -15,8 +15,9 @@ | |
#ifdef LEAK | ||
# ifdef __APPLE__ | ||
|
||
__attribute__((destructor)) | ||
void destructor(void) | ||
void leak_chek(void) __attribute__((destructor)); | ||
|
||
void leak_chek(void) | ||
{ | ||
system("leaks -q push_swap"); | ||
} | ||
|
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,26 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* large_sort.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/06 17:45:36 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) | ||
{ | ||
(void)p_a; | ||
(void)p_b; | ||
(void)num_a; | ||
} |
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/15 16:32:27 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:29:19 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:04:27 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
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,20 +6,15 @@ | |
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/12 15:58:22 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:27:24 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:04:35 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
static void _handle_2(t_stack **p_a) | ||
{ | ||
int first; | ||
int second; | ||
|
||
first = *(int *)(*p_a)->content; | ||
second = *(int *)(*p_a)->next->content; | ||
if (first > second) | ||
if (first_content(p_a) > second_content(p_a)) | ||
{ | ||
swap_stack(p_a); | ||
ft_putendl_fd("sa", STDOUT_FILENO); | ||
|
@@ -32,9 +27,9 @@ static void _handle_3(t_stack **p_a) | |
int second; | ||
int third; | ||
|
||
first = *(int *)(*p_a)->content; | ||
second = *(int *)(*p_a)->next->content; | ||
third = *(int *)(*p_a)->next->next->content; | ||
first = first_content(p_a); | ||
second = second_content(p_a); | ||
third = third_content(p_a); | ||
if (second < first && first < third) | ||
operate_sa(p_a); | ||
else if (third < second && second < first) | ||
|
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/15 15:10:35 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:26:11 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:04:45 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
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/15 16:54:29 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/15 16:54:44 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:04:54 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
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/15 15:56:53 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:36:08 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:05:10 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
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,46 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* reverse_rotate.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: reasuke <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/01/23 17:18:15 by reasuke #+# #+# */ | ||
/* Updated: 2024/02/07 15:04:58 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "push_swap.h" | ||
|
||
void reverse_rotate_stack(t_stack **p_stack) | ||
{ | ||
t_stack *before_last; | ||
t_stack *last; | ||
|
||
if (ft_lstsize(*p_stack) < 2) | ||
return ; | ||
last = ft_lstlast(*p_stack); | ||
before_last = ft_lst_before(*p_stack, last); | ||
before_last->next = NULL; | ||
last->next = *p_stack; | ||
*p_stack = last; | ||
} | ||
|
||
void operate_rra(t_stack **p_a) | ||
{ | ||
reverse_rotate_stack(p_a); | ||
ft_putendl_fd("rra", STDOUT_FILENO); | ||
} | ||
|
||
void operate_rrb(t_stack **p_b) | ||
{ | ||
reverse_rotate_stack(p_b); | ||
ft_putendl_fd("rrb", STDOUT_FILENO); | ||
} | ||
|
||
void operate_rrr(t_stack **p_a, t_stack **p_b) | ||
{ | ||
reverse_rotate_stack(p_a); | ||
reverse_rotate_stack(p_b); | ||
ft_putendl_fd("rrr", STDOUT_FILENO); | ||
} |
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/15 14:05:46 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:31:46 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:05:17 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -26,28 +26,21 @@ void rotate_stack(t_stack **p_stack) | |
*p_stack = second; | ||
} | ||
|
||
void reverse_rotate_stack(t_stack **p_stack) | ||
{ | ||
t_stack *before_last; | ||
t_stack *last; | ||
|
||
if (ft_lstsize(*p_stack) < 2) | ||
return ; | ||
last = ft_lstlast(*p_stack); | ||
before_last = ft_lst_before(*p_stack, last); | ||
before_last->next = NULL; | ||
last->next = *p_stack; | ||
*p_stack = last; | ||
} | ||
|
||
void operate_ra(t_stack **p_a) | ||
{ | ||
rotate_stack(p_a); | ||
ft_putendl_fd("ra", STDOUT_FILENO); | ||
} | ||
|
||
void operate_rra(t_stack **p_a) | ||
void operate_rb(t_stack **p_b) | ||
{ | ||
rotate_stack(p_b); | ||
ft_putendl_fd("rb", STDOUT_FILENO); | ||
} | ||
|
||
void operate_rr(t_stack **p_a, t_stack **p_b) | ||
{ | ||
reverse_rotate_stack(p_a); | ||
ft_putendl_fd("rra", STDOUT_FILENO); | ||
rotate_stack(p_a); | ||
rotate_stack(p_b); | ||
ft_putendl_fd("rr", STDOUT_FILENO); | ||
} |
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/15 13:28:54 by reasuke #+# #+# */ | ||
/* Updated: 2024/01/23 15:32:30 by reasuke ### ########.fr */ | ||
/* Updated: 2024/02/07 15:05:21 by reasuke ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -35,3 +35,10 @@ void operate_sb(t_stack **p_b) | |
swap_stack(p_b); | ||
ft_putendl_fd("sb", STDOUT_FILENO); | ||
} | ||
|
||
void operate_ss(t_stack **p_a, t_stack **p_b) | ||
{ | ||
swap_stack(p_a); | ||
swap_stack(p_b); | ||
ft_putendl_fd("ss", STDOUT_FILENO); | ||
} |
Oops, something went wrong.