Skip to content

Commit

Permalink
header modulize (#22)
Browse files Browse the repository at this point in the history
* implement header modularization

* update Makefile
  • Loading branch information
rask24 committed Feb 11, 2024
1 parent e0b1c3a commit 0209e74
Show file tree
Hide file tree
Showing 30 changed files with 221 additions and 86 deletions.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ INC_DIR = include
LIBFT_DIR = libft
TEST_DIR = test
GTEST_DIR = test/gtest
TEST_BUILD_DIR = test/build

SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/initialization/check_args.c \
Expand Down Expand Up @@ -51,10 +52,10 @@ TEST_SRC = $(TEST_DIR)/test_check_args.cpp \
$(TEST_DIR)/test_greedy_operation.cpp \
$(TEST_DIR)/test_is_sorted_stack.cpp \
$(TEST_DIR)/test_sort.cpp
TEST_OBJ = $(patsubst $(TEST_DIR)/%.cpp, $(BUILD_DIR)/$(TEST_DIR)/%.o, $(TEST_SRC))
TEST_OBJ = $(patsubst $(TEST_DIR)/%.cpp, $(TEST_BUILD_DIR)/%.o, $(TEST_SRC))
DEPFLAGS = -MMD -MP
GTEST_SRC = $(GTEST_DIR)/gtest_main.cc $(GTEST_DIR)/gtest-all.cc
GTEST_OBJ = $(patsubst $(GTEST_DIR)/%.cc, $(BUILD_DIR)/$(GTEST_DIR)/%.o, $(GTEST_SRC))
GTEST_OBJ = $(patsubst $(GTEST_DIR)/%.cc, $(TEST_BUILD_DIR)/%.o, $(GTEST_SRC))

GTEST_VERSION = 1.14.0
GTEST_ARCHIVE = v$(GTEST_VERSION).tar.gz
Expand Down Expand Up @@ -116,19 +117,19 @@ test_main: all $(GTEST_OBJ) $(TEST_OBJ)

test_clean:
@echo "$(BLUE)test cleaning$(RESET)"
@$(RM) -r $(BUILD_DIR)/$(TEST_DIR)
@$(RM) -r $(TEST_BUILD_DIR)

$(BUILD_DIR)/$(TEST_DIR)/%.o: $(TEST_DIR)/%.cpp
$(TEST_BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp
@mkdir -p $(@D)
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $< -o $@
@printf "$(GREEN).$(RESET)"

$(GTEST_OBJ): $(GTEST_DIR)
@echo "$(BLUE)test compiling$(RESET)"
@mkdir -p $(@D)
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $(GTEST_DIR)/gtest-all.cc -o $(BUILD_DIR)/$(GTEST_DIR)/gtest-all.o
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $(GTEST_DIR)/gtest-all.cc -o $(TEST_BUILD_DIR)/gtest-all.o
@printf "$(GREEN).$(RESET)"
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $(GTEST_DIR)/gtest_main.cc -o $(BUILD_DIR)/$(GTEST_DIR)/gtest_main.o
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $(GTEST_DIR)/gtest_main.cc -o $(TEST_BUILD_DIR)/gtest_main.o
@printf "$(GREEN).$(RESET)"

$(GTEST_DIR):
Expand Down
22 changes: 22 additions & 0 deletions include/initialization.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* initialization.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:30:37 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:33:50 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef INITIALIZATION_H
# define INITIALIZATION_H

# include "push_swap.h"

int check_args(int argc, char **argv);
t_stack *generate_stack(int argc, char **argv);
void exit_with_error(void);

#endif
23 changes: 23 additions & 0 deletions include/large_sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* large_sort.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:36:38 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:37:50 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef LARGE_SORT_H
# define LARGE_SORT_H

# include "push_swap.h"

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);

#endif
68 changes: 3 additions & 65 deletions include/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,19 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */
/* Updated: 2024/02/10 21:18:02 by reasuke ### ########.fr */
/* Updated: 2024/02/11 12:58:12 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PUSH_SWAP_H
# define PUSH_SWAP_H

# include "../libft/libft.h"
# include "types.h"
# include "utils.h"
# include <limits.h>
# include <stdbool.h>
# include <stdlib.h>
# 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 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);
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);
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 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);

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
24 changes: 24 additions & 0 deletions include/sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:42:03 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:45:45 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef SORT_H
# define SORT_H

# include "push_swap.h"

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);
bool is_sorted_stack(t_stack *st);

#endif
39 changes: 39 additions & 0 deletions include/stack_operations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_operations.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:41:01 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:41:49 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef STACK_OPERATIONS_H
# define STACK_OPERATIONS_H

# include "push_swap.h"

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 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);
#endif
38 changes: 38 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:57:41 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:58:04 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef TYPES_H
# define TYPES_H

typedef t_list t_stack;

typedef enum e_method
{
INIT,
FF,
FR,
RF,
RR,
} t_method;

typedef struct s_content
{
int index;
int sf_cost;
int sr_cost;
int if_cost;
int ir_cost;
int min_cost;
t_method opt_method;
bool is_opt;
} t_content;
#endif
25 changes: 25 additions & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 12:39:00 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:39:47 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef UTILS_H
# define UTILS_H

# include "push_swap.h"

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
4 changes: 2 additions & 2 deletions src/initialization/check_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:44:55 by reasuke #+# #+# */
/* Updated: 2024/02/07 15:06:08 by reasuke ### ########.fr */
/* Updated: 2024/02/11 12:35:17 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"
#include "initialization.h"

static bool _has_not_digit(int argc, char **argv)
{
Expand Down
4 changes: 2 additions & 2 deletions src/initialization/exit_with_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:38:17 by reasuke #+# #+# */
/* Updated: 2024/02/07 15:05:34 by reasuke ### ########.fr */
/* Updated: 2024/02/11 12:35:14 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"
#include "initialization.h"

void exit_with_error(void)
{
Expand Down
4 changes: 2 additions & 2 deletions src/initialization/generate_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */
/* Updated: 2024/02/07 15:06:20 by reasuke ### ########.fr */
/* Updated: 2024/02/11 12:35:32 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"
#include "initialization.h"

int *_generate_int_array(int array_size)
{
Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */
/* Updated: 2024/02/10 21:19:08 by reasuke ### ########.fr */
/* Updated: 2024/02/11 12:54:10 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "initialization.h"
#include "push_swap.h"
#include "sort.h"

#ifdef LEAK
# ifdef __APPLE__
Expand Down
3 changes: 2 additions & 1 deletion src/sort/is_sorted_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 18:47:11 by reasuke #+# #+# */
/* Updated: 2024/02/10 19:12:48 by reasuke ### ########.fr */
/* Updated: 2024/02/11 12:54:04 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"
#include "sort.h"

bool is_sorted_stack(t_stack *st)
{
Expand Down
4 changes: 3 additions & 1 deletion src/sort/large_sort/greedy_operation.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */
/* Updated: 2024/02/10 20:34:46 by reasuke ### ########.fr */
/* Updated: 2024/02/11 12:53:46 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "large_sort.h"
#include "push_swap.h"
#include "stack_operations.h"

static t_stack *_find_opt_st_b(t_stack **p_b)
{
Expand Down
Loading

0 comments on commit 0209e74

Please sign in to comment.