Skip to content

Commit

Permalink
implement nano sort (#6)
Browse files Browse the repository at this point in the history
* rename _has_overflow

* refactor test_is_invalid_argument

* implement generate_stack

* update generate_stack with coordinate compression

* add dev mode

* implement nano_sort case 2

* implement nano_sort case 3

* update gitignore

* add visualizer to .gitignore

* fix preprocessor in main.c

* update lst->content, (char *) -> (int *)
  • Loading branch information
rask24 committed Jan 15, 2024
1 parent 6e07d3b commit 0627033
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ build/
# Executable File
push_swap
checker_Mac
checker_linux
tester

# Subject File
en.subject.pdf

# Google Test Source
gtest/

# Visualizer
push_swap_visualizer/
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
NAME = push_swap
CFLAGS = -Werror -Wextra -Wall -O3
CFLAGS = -Werror -Wextra -Wall
PROD_FLAGS = -O3
DEV_FLAGS = -g -O0 -D DEV

SRC_DIR = ./src
BUILD_DIR = ./build
INC_DIR = ./include
LIBFT_DIR = ./libft
SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/is_invalid_argument.c \
$(SRC_DIR)/generate_stack.c \
$(SRC_DIR)/nano_sort.c \
$(SRC_DIR)/utils/exit_with_error.c
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
DEP = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.d, $(SRC))
Expand All @@ -30,6 +34,7 @@ BLUE = \033[0;34m
RED = \033[0;31m
RESET = \033[0m

all: CFLAGS += $(PROD_FLAGS)
all: title $(NAME)

$(NAME): $(OBJ)
Expand All @@ -44,14 +49,18 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c

clean:
@make clean -C $(LIBFT_DIR)
@$(RM) $(OBJ)
@$(RM) $(OBJ) $(DEP)

fclean: clean
@make fclean -C $(LIBFT_DIR)
@$(RM) $(NAME)

re: fclean all

dev: CFLAGS += $(DEV_FLAGS)
dev: title
dev: $(NAME)

test: $(GTEST_DIR)
@echo "$(BLUE)test$(RESET)"
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -L $(LIBFT_DIR) -l ft -lpthread -o tester \
Expand All @@ -76,6 +85,6 @@ norm:
title:
@echo "$(BLUE)push_swap$(RESET)"

.PHONY: all clean fclean re test norm title
.PHONY: all clean fclean re dev test norm title

-include $(DEP)
6 changes: 5 additions & 1 deletion 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/11 15:45:14 by reasuke ### ########.fr */
/* Updated: 2024/01/12 16:02:39 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,6 +25,10 @@

bool is_invalid_argument(int argc, char **argv);

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

void nano_sort(t_list *stack, int argc);

void exit_with_error(void);

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

#include "push_swap.h"

int *_generate_int_array(int array_size)
{
int *array;

array = malloc(sizeof(int) * array_size);
if (!array)
exit_with_error();
return (array);
}

int *_generate_numberd_array(int array_size, char **argv)
{
int i;
int *numberd_array;

numberd_array = _generate_int_array(array_size);
i = 0;
while (i < array_size)
{
numberd_array[i] = ft_atoi(argv[i + 1]);
i++;
}
return (numberd_array);
}

int *_generate_sorted_array(int argc, char **argv)
{
int array_size;
int *sorted_array;
int i;
int j;

array_size = argc - 1;
sorted_array = _generate_numberd_array(array_size, argv);
i = array_size - 1;
while (i > 0)
{
j = 0;
while (j < i)
{
if (sorted_array[j] > sorted_array[j + 1])
ft_swap(sorted_array + j, sorted_array + j + 1);
j++;
}
i--;
}
return (sorted_array);
}

int *_coordinate_compression(int argc, char **argv)
{
int *compressred_array;
int *sorted_array;
int array_size;
int i;
int j;

array_size = argc - 1;
compressred_array = _generate_int_array(array_size);
sorted_array = _generate_sorted_array(argc, argv);
i = 0;
while (i < array_size)
{
j = 0;
while (j < array_size)
{
if (ft_atoi(argv[i + 1]) == sorted_array[j])
compressred_array[i] = j + 1;
j++;
}
i++;
}
free(sorted_array);
return (compressred_array);
}

t_list *generate_stack(int argc, char **argv)
{
t_list *stack;
int *compressed_array;
int i;
int *ptr;

compressed_array = _coordinate_compression(argc, argv);
i = 0;
stack = NULL;
while (i < argc - 1)
{
ptr = ft_calloc(1, sizeof(int));
if (!ptr)
exit_with_error();
*ptr = compressed_array[i];
ft_lstadd_back(&stack, ft_lstnew(ptr));
i++;
}
free(compressed_array);
return (stack);
}
6 changes: 3 additions & 3 deletions src/is_invalid_argument.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 14:44:55 by reasuke #+# #+# */
/* Updated: 2024/01/11 16:18:03 by reasuke ### ########.fr */
/* Updated: 2024/01/11 18:16:19 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,7 +28,7 @@ static bool _has_not_digit(int argc, char **argv)
return (false);
}

static bool _has_overflowed(int argc, char **argv)
static bool _has_overflow(int argc, char **argv)
{
int i;
long num;
Expand Down Expand Up @@ -67,6 +67,6 @@ static bool _has_duplicate(int argc, char **argv)
bool is_invalid_argument(int argc, char **argv)
{
return (_has_not_digit(argc, argv)
|| _has_overflowed(argc, argv)
|| _has_overflow(argc, argv)
|| _has_duplicate(argc, argv));
}
27 changes: 26 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,42 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */
/* Updated: 2024/01/11 15:56:52 by reasuke ### ########.fr */
/* Updated: 2024/01/15 12:04:04 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

#ifdef DEV

# ifdef __APPLE__

__attribute__((destructor))
void destructor(void)
{
system("leaks -q push_swap");
}

# endif

void put_void(void *content)
{
puts(content);
}

#endif

int main(int argc, char **argv)
{
t_list *stack_a;

if (argc < 2)
return (0);
if (is_invalid_argument(argc, argv))
exit_with_error();
stack_a = generate_stack(argc, argv);
if (argc < 5)
nano_sort(stack_a, argc);
ft_lstclear(&stack_a, free);
return (0);
}
62 changes: 62 additions & 0 deletions src/nano_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nano_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 15:58:22 by reasuke #+# #+# */
/* Updated: 2024/01/15 12:58:12 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

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

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

static void _handel_3(t_list *stack)
{
int first;
int second;
int third;

first = *(int *)stack->content;
second = *(int *)stack->next->content;
third = *(int *)stack->next->next->content;
if (first == 2 && second == 1 && third == 3)
ft_putendl_fd("sa", STDOUT_FILENO);
else if (first == 3 && second == 2 && third == 1)
{
ft_putendl_fd("sa", STDOUT_FILENO);
ft_putendl_fd("rra", STDOUT_FILENO);
}
else if (first == 3 && second == 1 && third == 2)
ft_putendl_fd("ra", STDOUT_FILENO);
else if (first == 1 && second == 3 && third == 2)
{
ft_putendl_fd("sa", STDOUT_FILENO);
ft_putendl_fd("ra", STDOUT_FILENO);
}
else if (first == 2 && second == 3 && third == 1)
ft_putendl_fd("rra", STDOUT_FILENO);
}

void nano_sort(t_list *stack, int argc)
{
int num_of_element;

num_of_element = argc - 1;
if (num_of_element == 2)
_handle_2(stack);
else if (num_of_element == 3)
_handel_3(stack);
}
Loading

0 comments on commit 0627033

Please sign in to comment.