Skip to content

Commit

Permalink
sort prep (#18)
Browse files Browse the repository at this point in the history
* 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
rask24 authored Feb 7, 2024
1 parent e42f8be commit 3cff634
Show file tree
Hide file tree
Showing 21 changed files with 282 additions and 85 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/sort/sort.c \
$(SRC_DIR)/sort/micro_sort.c \
$(SRC_DIR)/sort/nano_sort.c \
$(SRC_DIR)/sort/large_sort.c \
$(SRC_DIR)/stack_operations/push.c \
$(SRC_DIR)/stack_operations/swap.c \
$(SRC_DIR)/stack_operations/rotate.c \
$(SRC_DIR)/stack_operations/reverse_rotate.c \
$(SRC_DIR)/stack_operations/ft_lst_before.c \
$(SRC_DIR)/utils/first_content.c \
$(SRC_DIR)/utils/second_content.c \
$(SRC_DIR)/utils/third_content.c \
$(SRC_DIR)/utils/exit_with_error.c \
$(SRC_DIR)/utils/clear_stack.c
$(SRC_DIR)/utils/clear_stack.c \
$(SRC_DIR)/utils/stack_size.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
70 changes: 41 additions & 29 deletions include/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,60 @@
/* By: reasuke <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
5 changes: 2 additions & 3 deletions src/check_args.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/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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/generate_stack.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/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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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++;
}
Expand Down
7 changes: 4 additions & 3 deletions src/main.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/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 */
/* */
/* ************************************************************************** */

Expand All @@ -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");
}
Expand Down
26 changes: 26 additions & 0 deletions src/sort/large_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* large_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
2 changes: 1 addition & 1 deletion src/sort/micro_sort.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/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 */
/* */
/* ************************************************************************** */

Expand Down
15 changes: 5 additions & 10 deletions src/sort/nano_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@
/* By: reasuke <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/sort/sort.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/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 */
/* */
/* ************************************************************************** */

Expand Down
2 changes: 1 addition & 1 deletion src/stack_operations/ft_lst_before.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/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 */
/* */
/* ************************************************************************** */

Expand Down
2 changes: 1 addition & 1 deletion src/stack_operations/push.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/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 */
/* */
/* ************************************************************************** */

Expand Down
46 changes: 46 additions & 0 deletions src/stack_operations/reverse_rotate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* reverse_rotate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <reasuke@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
29 changes: 11 additions & 18 deletions src/stack_operations/rotate.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/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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
}
9 changes: 8 additions & 1 deletion src/stack_operations/swap.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/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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
}
Loading

0 comments on commit 3cff634

Please sign in to comment.