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

refactor #17

Merged
merged 4 commits into from
Jan 23, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ gtest/

# Visualizer
push_swap_visualizer/
push-swap-pain/
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/stack_operations/ft_lst_before.c \
$(SRC_DIR)/utils/first_content.c \
$(SRC_DIR)/utils/second_content.c \
$(SRC_DIR)/utils/exit_with_error.c
$(SRC_DIR)/utils/exit_with_error.c \
$(SRC_DIR)/utils/clear_stack.c
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
DEP = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.d, $(SRC))
OBJ_FILTER_MAIN = $(filter-out $(BUILD_DIR)/main.o, $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC)))
Expand Down
41 changes: 22 additions & 19 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/01/17 18:29:53 by reasuke ### ########.fr */
/* Updated: 2024/01/23 15:51:40 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,28 +23,31 @@

# include "../libft/libft.h"

typedef t_list t_stack;

int check_args(int argc, char **argv);

t_list *generate_stack(int argc, char **argv);

void sort(t_list **stack_a, t_list **stack_b, int num_a);
void nano_sort(t_list **stack, int num_a);
void micro_sort(t_list **stack_a, t_list **stack_b, int num_a);

void operate_sa(t_list **stack_a);
void operate_sb(t_list **stack_b);
void operate_ra(t_list **stack_a);
void operate_rra(t_list **stack_a);
void operate_pa(t_list **stack_b, t_list **stack_a);
void operate_pb(t_list **stack_a, t_list **stack_b);
void push_stack(t_list **stack_1, t_list **stack_2);
void swap_stack(t_list **stack);
void rotate_stack(t_list **stack);
void reverse_rotate_stack(t_list **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 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_list **stack);
int second_content(t_list **stack);
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 *));

#endif
6 changes: 3 additions & 3 deletions src/generate_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */
/* Updated: 2024/01/15 12:56:26 by reasuke ### ########.fr */
/* Updated: 2024/01/23 15:10:51 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -88,9 +88,9 @@ int *_coordinate_compression(int argc, char **argv)
return (compressred_array);
}

t_list *generate_stack(int argc, char **argv)
t_stack *generate_stack(int argc, char **argv)
{
t_list *stack;
t_stack *stack;
int *compressed_array;
int i;
int *ptr;
Expand Down
14 changes: 7 additions & 7 deletions src/main.c
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:36 by reasuke #+# #+# */
/* Updated: 2024/01/22 15:36:13 by reasuke ### ########.fr */
/* Updated: 2024/01/23 15:48:43 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,13 +34,13 @@ void put_void(void *content)

int main(int argc, char **argv)
{
t_list *stack_a;
t_list *stack_b;
t_stack *a;
t_stack *b;

check_args(argc, argv);
stack_a = generate_stack(argc, argv);
stack_b = NULL;
sort(&stack_a, &stack_b, argc - 1);
ft_lstclear(&stack_a, free);
a = generate_stack(argc, argv);
b = NULL;
sort(&a, &b, argc - 1);
clear_stack(&a, free);
return (0);
}
92 changes: 46 additions & 46 deletions src/sort/micro_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,82 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 16:32:27 by reasuke #+# #+# */
/* Updated: 2024/01/17 18:33:18 by reasuke ### ########.fr */
/* Updated: 2024/01/23 15:29:19 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

static void _handle_4(t_list **stack_a, t_list **stack_b)
static void _handle_4(t_stack **p_a, t_stack **p_b)
{
operate_pb(stack_a, stack_b);
nano_sort(stack_a, 3);
if (first_content(stack_b) == 1)
operate_pa(stack_b, stack_a);
else if (first_content(stack_b) == 2)
operate_pb(p_a, p_b);
nano_sort(p_a, 3);
if (first_content(p_b) == 1)
operate_pa(p_b, p_a);
else if (first_content(p_b) == 2)
{
operate_pa(stack_b, stack_a);
operate_sa(stack_a);
operate_pa(p_b, p_a);
operate_sa(p_a);
}
else if (first_content(stack_b) == 3)
else if (first_content(p_b) == 3)
{
operate_rra(stack_a);
operate_pa(stack_b, stack_a);
operate_ra(stack_a);
operate_ra(stack_a);
operate_rra(p_a);
operate_pa(p_b, p_a);
operate_ra(p_a);
operate_ra(p_a);
}
else if (first_content(stack_b) == 4)
else if (first_content(p_b) == 4)
{
operate_pa(stack_b, stack_a);
operate_ra(stack_a);
operate_pa(p_b, p_a);
operate_ra(p_a);
}
}

static void _edge_flow_5(t_list **stack_a, t_list **stack_b)
static void _edge_flow_5(t_stack **p_a, t_stack **p_b)
{
operate_pa(stack_b, stack_a);
operate_pa(stack_b, stack_a);
operate_ra(stack_a);
operate_ra(stack_a);
operate_pa(p_b, p_a);
operate_pa(p_b, p_a);
operate_ra(p_a);
operate_ra(p_a);
}

static void _normal_flow_5(t_list **stack_a, t_list **stack_b)
static void _normal_flow_5(t_stack **p_a, t_stack **p_b)
{
if (first_content(stack_b) == 5)
if (first_content(p_b) == 5)
{
operate_pa(stack_b, stack_a);
operate_rra(stack_a);
operate_pa(p_b, p_a);
operate_rra(p_a);
}
else
{
while (first_content(stack_a) != first_content(stack_b) + 1)
operate_ra(stack_a);
operate_pa(stack_b, stack_a);
while (first_content(p_a) != first_content(p_b) + 1)
operate_ra(p_a);
operate_pa(p_b, p_a);
}
while (first_content(stack_a) != first_content(stack_b) + 1)
operate_rra(stack_a);
operate_pa(stack_b, stack_a);
while (first_content(stack_a) != 1)
operate_rra(stack_a);
while (first_content(p_a) != first_content(p_b) + 1)
operate_rra(p_a);
operate_pa(p_b, p_a);
while (first_content(p_a) != 1)
operate_rra(p_a);
}

static void _handle_5(t_list **stack_a, t_list **stack_b)
static void _handle_5(t_stack **p_a, t_stack **p_b)
{
operate_pb(stack_a, stack_b);
operate_pb(stack_a, stack_b);
nano_sort(stack_a, 3);
if (first_content(stack_b) < second_content(stack_b))
operate_sb(stack_b);
if (first_content(stack_b) == 5 && second_content(stack_b) == 4)
_edge_flow_5(stack_a, stack_b);
operate_pb(p_a, p_b);
operate_pb(p_a, p_b);
nano_sort(p_a, 3);
if (first_content(p_b) < second_content(p_b))
operate_sb(p_b);
if (first_content(p_b) == 5 && second_content(p_b) == 4)
_edge_flow_5(p_a, p_b);
else
_normal_flow_5(stack_a, stack_b);
_normal_flow_5(p_a, p_b);
}

void micro_sort(t_list **stack_a, t_list **stack_b, int num_a)
void micro_sort(t_stack **p_a, t_stack **p_b, int num_a)
{
if (num_a == 4)
_handle_4(stack_a, stack_b);
_handle_4(p_a, p_b);
else if (num_a == 5)
_handle_5(stack_a, stack_b);
_handle_5(p_a, p_b);
}
38 changes: 19 additions & 19 deletions src/sort/nano_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 15:58:22 by reasuke #+# #+# */
/* Updated: 2024/01/17 16:04:38 by reasuke ### ########.fr */
/* Updated: 2024/01/23 15:27:24 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

static void _handle_2(t_list **stack)
static void _handle_2(t_stack **p_a)
{
int first;
int second;

first = *(int *)(*stack)->content;
second = *(int *)(*stack)->next->content;
first = *(int *)(*p_a)->content;
second = *(int *)(*p_a)->next->content;
if (first > second)
{
swap_stack(stack);
swap_stack(p_a);
ft_putendl_fd("sa", STDOUT_FILENO);
}
}

static void _handle_3(t_list **stack)
static void _handle_3(t_stack **p_a)
{
int first;
int second;
int third;

first = *(int *)(*stack)->content;
second = *(int *)(*stack)->next->content;
third = *(int *)(*stack)->next->next->content;
first = *(int *)(*p_a)->content;
second = *(int *)(*p_a)->next->content;
third = *(int *)(*p_a)->next->next->content;
if (second < first && first < third)
operate_sa(stack);
operate_sa(p_a);
else if (third < second && second < first)
{
operate_sa(stack);
operate_rra(stack);
operate_sa(p_a);
operate_rra(p_a);
}
else if (second < third && third < first)
operate_ra(stack);
operate_ra(p_a);
else if (first < third && third < second)
{
operate_sa(stack);
operate_ra(stack);
operate_sa(p_a);
operate_ra(p_a);
}
else if (third < first && first < second)
operate_rra(stack);
operate_rra(p_a);
}

void nano_sort(t_list **stack, int num_a)
void nano_sort(t_stack **p_a, int num_a)
{
if (num_a == 2)
_handle_2(stack);
_handle_2(p_a);
else if (num_a == 3)
_handle_3(stack);
_handle_3(p_a);
}
8 changes: 4 additions & 4 deletions src/sort/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 15:10:35 by reasuke #+# #+# */
/* Updated: 2024/01/17 16:08:05 by reasuke ### ########.fr */
/* Updated: 2024/01/23 15:26:11 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void sort(t_list **stack_a, t_list **stack_b, int num_a)
void sort(t_stack **p_a, t_stack **p_b, int num_a)
{
if (num_a <= 3)
nano_sort(stack_a, num_a);
nano_sort(p_a, num_a);
else if (num_a <= 5)
micro_sort(stack_a, stack_b, num_a);
micro_sort(p_a, p_b, num_a);
}
24 changes: 12 additions & 12 deletions src/stack_operations/push.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 15:56:53 by reasuke #+# #+# */
/* Updated: 2024/01/15 17:02:11 by reasuke ### ########.fr */
/* Updated: 2024/01/23 15:36:08 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void push_stack(t_list **stack_1, t_list **stack_2)
void push_stack(t_stack **p_s1, t_stack **p_s2)
{
t_list *stack_1_first;
t_stack *s1_first;

if (!*stack_1)
if (!*p_s1)
return ;
stack_1_first = *stack_1;
*stack_1 = (*stack_1)->next;
stack_1_first->next = NULL;
ft_lstadd_front(stack_2, stack_1_first);
s1_first = *p_s1;
*p_s1 = (*p_s1)->next;
s1_first->next = NULL;
ft_lstadd_front(p_s2, s1_first);
}

void operate_pa(t_list **stack_b, t_list **stack_a)
void operate_pa(t_stack **p_b, t_stack **p_a)
{
push_stack(stack_b, stack_a);
push_stack(p_b, p_a);
ft_putendl_fd("pa", STDOUT_FILENO);
}

void operate_pb(t_list **stack_a, t_list **stack_b)
void operate_pb(t_stack **p_a, t_stack **p_b)
{
push_stack(stack_a, stack_b);
push_stack(p_a, p_b);
ft_putendl_fd("pb", STDOUT_FILENO);
}
Loading
Loading