Skip to content

Commit

Permalink
Implement greedy sort (#20)
Browse files Browse the repository at this point in the history
* implement set_selection_cost

* format test

* add test set_cost_forward

* refactor with get_content

* implement set_insertion_cost

* refactor with set_cost

* fix build for gcc

* implement set_opt_method

* refactor with libft

* refactor set_opt

* add min_cost

* add is_opt

* fix build for gcc

* fix test for gcc

* fix test_sort for gcc

* implement greedy_operation

* implement is_sorted_stack

* implement sort

* refactor greedy_operation

* rename get_index

* refactor with initialization

* update sort

* update Makefile
  • Loading branch information
rask24 committed Feb 10, 2024
1 parent 3cff634 commit e0b1c3a
Show file tree
Hide file tree
Showing 33 changed files with 1,196 additions and 261 deletions.
51 changes: 35 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@ TEST_DIR = test
GTEST_DIR = test/gtest

SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/check_args.c \
$(SRC_DIR)/generate_stack.c \
$(SRC_DIR)/initialization/check_args.c \
$(SRC_DIR)/initialization/exit_with_error.c \
$(SRC_DIR)/initialization/generate_stack.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)/sort/is_sorted_stack.c \
$(SRC_DIR)/sort/large_sort/large_sort.c \
$(SRC_DIR)/sort/large_sort/set_cost.c \
$(SRC_DIR)/sort/large_sort/set_opt.c \
$(SRC_DIR)/sort/large_sort/greedy_operation.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)/stack_operations/do_single_n_operations.c \
$(SRC_DIR)/stack_operations/do_double_n_operations.c \
$(SRC_DIR)/utils/get_first_index.c \
$(SRC_DIR)/utils/get_second_index.c \
$(SRC_DIR)/utils/get_third_index.c \
$(SRC_DIR)/utils/clear_stack.c \
$(SRC_DIR)/utils/get_content.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))
Expand All @@ -40,8 +46,11 @@ TEST_SRC = $(TEST_DIR)/test_check_args.cpp \
$(TEST_DIR)/test_reverse_rotate_stack.cpp \
$(TEST_DIR)/test_rotate_stack.cpp \
$(TEST_DIR)/test_swap_stack.cpp \
$(TEST_DIR)/test_sort.cpp \
$(TEST_DIR)/test_ft_lst_before.cpp
$(TEST_DIR)/test_set_cost.cpp \
$(TEST_DIR)/test_set_opt_method.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))
DEPFLAGS = -MMD -MP
GTEST_SRC = $(GTEST_DIR)/gtest_main.cc $(GTEST_DIR)/gtest-all.cc
Expand Down Expand Up @@ -97,20 +106,30 @@ leak: $(NAME)

releak: fclean leak

test: all $(GTEST_OBJ) $(TEST_OBJ)
@echo "$(BLUE)test$(RESET)"
$(CXX) -L $(LIBFT_DIR) -lft -lpthread $(OBJ_FILTER_MAIN) $(TEST_OBJ) $(GTEST_OBJ) -o $(TEST_NAME)
test: test_clean test_main

test_main: all $(GTEST_OBJ) $(TEST_OBJ)
@echo "$(BLUE)\ntest linking$(RESET)"
@$(CXX) -L $(LIBFT_DIR) -lft -lpthread $(OBJ_FILTER_MAIN) $(TEST_OBJ) $(GTEST_OBJ) -o $(TEST_NAME)
./$(TEST_NAME)
@$(RM) $(TEST_NAME)

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

$(BUILD_DIR)/$(TEST_DIR)/%.o: $(TEST_DIR)/%.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $< -o $@
@$(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_main.cc -o $(BUILD_DIR)/$(GTEST_DIR)/gtest_main.o
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $(GTEST_DIR)/gtest-all.cc -o $(BUILD_DIR)/$(GTEST_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
@printf "$(GREEN).$(RESET)"

$(GTEST_DIR):
@echo "fetching google test"
Expand Down
49 changes: 35 additions & 14 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/02/07 15:04:00 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:18:02 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,22 +20,40 @@
# 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 delta_a;
int delta_b;
int target;
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, 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 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);
Expand All @@ -48,18 +66,21 @@ 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);
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);
int third_content(t_stack **p_stack);
void exit_with_error(void);
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
2 changes: 1 addition & 1 deletion libft
Submodule libft updated 5 files
+2 −0 Makefile
+20 −0 integer/ft_abs.c
+80 −76 libft.h
+27 −0 list/ft_lstbefore.c
+86 −40 test/gtest.cpp
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 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/02/07 15:12:35 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:19:08 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,7 +41,7 @@ int main(int argc, char **argv)
check_args(argc, argv);
a = generate_stack(argc, argv);
b = NULL;
sort(&a, &b, argc - 1);
sort(&a, &b);
clear_stack(&a, free);
return (0);
}
28 changes: 15 additions & 13 deletions src/sort/large_sort.c → src/sort/is_sorted_stack.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* large_sort.c :+: :+: :+: */
/* is_sorted_stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */
/* Updated: 2024/02/06 17:45:36 by reasuke ### ########.fr */
/* Created: 2024/02/10 18:47:11 by reasuke #+# #+# */
/* Updated: 2024/02/10 19:12:48 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)
bool is_sorted_stack(t_stack *st)
{
(void)p_a;
(void)p_b;
(void)num_a;
int current;

current = 1;
while (st)
{
if (get_content(st)->index != current)
return (false);
current++;
st = st->next;
}
return (true);
}
96 changes: 96 additions & 0 deletions src/sort/large_sort/greedy_operation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* greedy_operation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 10:43:16 by reasuke #+# #+# */
/* Updated: 2024/02/10 20:34:46 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

static t_stack *_find_opt_st_b(t_stack **p_b)
{
t_stack *st_b;

st_b = *p_b;
while (!get_content(st_b)->is_opt)
st_b = st_b->next;
return (st_b);
}

static void _do_alined_operation(t_stack **p_a, t_stack **p_b,
t_stack *opt_st_b)
{
int sf_abs;
int sr_abs;
int if_abs;
int ir_abs;

sf_abs = ft_abs(get_content(opt_st_b)->sf_cost);
sr_abs = ft_abs(get_content(opt_st_b)->sr_cost);
if_abs = ft_abs(get_content(opt_st_b)->if_cost);
ir_abs = ft_abs(get_content(opt_st_b)->ir_cost);
if (get_content(opt_st_b)->opt_method == FF)
{
do_double_n_operations(p_a, p_b, ft_min(sf_abs, if_abs), operate_rr);
if (sf_abs > if_abs)
do_single_n_operations(p_b, sf_abs - if_abs, operate_rb);
else if (sf_abs < if_abs)
do_single_n_operations(p_a, if_abs - sf_abs, operate_ra);
}
if (get_content(opt_st_b)->opt_method == RR)
{
do_double_n_operations(p_a, p_b, ft_min(sr_abs, ir_abs), operate_rrr);
if (sr_abs > ir_abs)
do_single_n_operations(p_b, sr_abs - ir_abs, operate_rrb);
else if (sr_abs < ir_abs)
do_single_n_operations(p_a, ir_abs - sr_abs, operate_rra);
}
}

static void _do_mixed_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b)
{
int sf_abs;
int sr_abs;
int if_abs;
int ir_abs;

sf_abs = ft_abs(get_content(opt_st_b)->sf_cost);
sr_abs = ft_abs(get_content(opt_st_b)->sr_cost);
if_abs = ft_abs(get_content(opt_st_b)->if_cost);
ir_abs = ft_abs(get_content(opt_st_b)->ir_cost);
if (get_content(opt_st_b)->opt_method == FR)
{
do_single_n_operations(p_b, sf_abs, operate_rb);
do_single_n_operations(p_a, ir_abs, operate_rra);
}
if (get_content(opt_st_b)->opt_method == RF)
{
do_single_n_operations(p_b, sr_abs, operate_rrb);
do_single_n_operations(p_a, if_abs, operate_ra);
}
}

static void _do_opt_operation(t_stack **p_a, t_stack **p_b, t_stack *opt_st_b)
{
t_method opt_method;

opt_method = get_content(opt_st_b)->opt_method;
if (opt_method == FF || opt_method == RR)
_do_alined_operation(p_a, p_b, opt_st_b);
else if (opt_method == FR || opt_method == RF)
_do_mixed_operation(p_a, p_b, opt_st_b);
operate_pa(p_b, p_a);
}

void greedy_operation(t_stack **p_a, t_stack **p_b)
{
t_stack *opt_st_b;

opt_st_b = _find_opt_st_b(p_b);
_do_opt_operation(p_a, p_b, opt_st_b);
}
51 changes: 51 additions & 0 deletions src/sort/large_sort/large_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* large_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */
/* Updated: 2024/02/10 21:24:58 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

static void _push_b_n_times(t_stack **p_a, t_stack **p_b, int n)
{
while (n--)
operate_pb(p_a, p_b);
}

static void _sort_stack_a(t_stack **p_a, int num_a)
{
if (get_content(*p_a)->index > num_a / 2)
{
while (!is_sorted_stack(*p_a))
operate_ra(p_a);
}
else
{
while (!is_sorted_stack(*p_a))
operate_rra(p_a);
}
}

void large_sort(t_stack **p_a, t_stack **p_b)
{
int size_a;
int size_b;

size_a = stack_size(*p_a);
size_b = size_a - 3;
_push_b_n_times(p_a, p_b, size_b);
nano_sort(p_a);
while (size_b--)
{
set_cost(p_a, p_b);
set_opt(p_b);
greedy_operation(p_a, p_b);
}
_sort_stack_a(p_a, size_a);
}
Loading

0 comments on commit e0b1c3a

Please sign in to comment.