diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/checker/checker.h | 36 | ||||
| -rw-r--r-- | src/common/action.c | 72 | ||||
| -rw-r--r-- | src/common/common.h | 51 | ||||
| -rw-r--r-- | src/common/stack_core.c | 12 | ||||
| -rw-r--r-- | src/common/stack_helper.c | 8 | ||||
| -rw-r--r-- | src/push_swap/main.c | 18 | ||||
| -rw-r--r-- | src/push_swap/push_swap.h | 4 | ||||
| -rw-r--r-- | src/push_swap/sort.c | 46 | ||||
| -rw-r--r-- | src/push_swap/stack_wrapper.c | 2 |
9 files changed, 83 insertions, 166 deletions
diff --git a/src/checker/checker.h b/src/checker/checker.h index 808e408..f6d50ef 100644 --- a/src/checker/checker.h +++ b/src/checker/checker.h @@ -6,10 +6,9 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:12 by cacharle #+# #+# */ -/* Updated: 2020/01/19 09:09:29 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 10:42:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdio.h> #ifndef CHECKER_H # define CHECKER_H @@ -20,29 +19,32 @@ # define ACTION_ID_BUF_SIZE 4 -typedef enum +typedef enum e_flag_arg { FLAG_ARG_A, FLAG_ARG_B, FLAG_ARG_A_B, FLAG_ARG_B_A -} t_flag_arg; +} t_flag_arg; -typedef struct +typedef void (*t_func_arg_1)(t_stack *); +typedef void (*t_func_arg_2)(t_stack *, t_stack *); + +typedef struct s_action { - const char *id; - t_flag_arg args; + const char *id; + t_flag_arg args; union { - void (*arg_1)(t_stack *); - void (*arg_2)(t_stack *, t_stack *); - } func; -} t_action; - -t_status check(t_stack *a, t_stack *b); -t_status read_action(t_stack *a, t_stack *b); -t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], - t_stack *a, t_stack *b); -t_bool stack_sorted(t_stack *stack); + t_func_arg_1 arg_1; + t_func_arg_2 arg_2; + } func; +} t_action; + +t_status check(t_stack *a, t_stack *b); +t_status read_action(t_stack *a, t_stack *b); +t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], + t_stack *a, t_stack *b); +t_bool stack_sorted(t_stack *stack); #endif diff --git a/src/common/action.c b/src/common/action.c deleted file mode 100644 index c095e59..0000000 --- a/src/common/action.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "common.h" - -void swap_a(t_stack *a) -{ - stack_swap(a); -} - -void swap_b(t_stack *b) -{ - stack_swap(b); -} - -void swap_both(t_stack *a, t_stack *b) -{ - swap_a(a); - swap_b(b); -} - -void push_a(t_stack *a, t_stack *b) -{ - int tmp; - - if (stack_empty(b)) - return ; - tmp = stack_peek(b); - stack_pop(b); - stack_push(a, tmp); -} - -void push_b(t_stack *b, t_stack *a) -{ - int tmp; - - if (stack_empty(a)) - return ; - tmp = stack_peek(a); - stack_pop(a); - stack_push(b, tmp); - -} - -void rotate_a(t_stack *a) -{ - stack_rotate(a); -} - -void rotate_b(t_stack *b) -{ - stack_rotate(b); -} - -void rotate_both(t_stack *a, t_stack *b) -{ - rotate_a(a); - rotate_b(b); -} - -void reverse_rotate_a(t_stack *a) -{ - stack_reverse_rotate(a); -} - -void reverse_rotate_b(t_stack *b) -{ - stack_reverse_rotate(b); -} - -void reverse_rotate_both(t_stack *a, t_stack *b) -{ - reverse_rotate_a(a); - reverse_rotate_b(b); -} diff --git a/src/common/common.h b/src/common/common.h index 97881c8..87c4655 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -6,16 +6,15 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 11:20:54 by cacharle #+# #+# */ -/* Updated: 2020/01/19 13:31:27 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 10:25:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include <stdio.h> -#ifndef STACK_H -# define STACK_H +#ifndef COMMON_H +# define COMMON_H -#include <stdlib.h> -#include "libft.h" +# include <stdlib.h> +# include "libft.h" typedef enum { @@ -23,55 +22,55 @@ typedef enum STATUS_FAILURE, STATUS_ERROR, STATUS_EOF, -} t_status; +} t_status; typedef enum { STACK_NO_TAG, STACK_A, STACK_B -} t_stack_tag; +} t_stack_tag; -typedef struct +typedef struct s_stack { - int *elements; + int *elements; t_stack_tag tag; - int top; + int top; } t_stack; /* ** stack_core.c */ -t_stack *stack_new(int size); -void stack_destroy(t_stack *stack); -void stack_push(t_stack *stack, int n); -void stack_pop(t_stack *stack); -int stack_peek(t_stack *stack); +t_stack *stack_new(int size); +void stack_destroy(t_stack *stack); +void stack_push(t_stack *stack, int n); +void stack_pop(t_stack *stack); +int stack_peek(t_stack *stack); /* ** stack_op.c */ -void stack_swap(t_stack *stack); -void stack_push_to(t_stack *from, t_stack *to); -void stack_rotate(t_stack *stack); -void stack_reverse_rotate(t_stack *stack); +void stack_swap(t_stack *stack); +void stack_push_to(t_stack *from, t_stack *to); +void stack_rotate(t_stack *stack); +void stack_reverse_rotate(t_stack *stack); /* ** stack_helper.c */ -void stack_swap_2(t_stack *stack_a, t_stack *stack_b); -void stack_rotate_2(t_stack *stack_a, t_stack *stack_b); -void stack_reverse_rotate_2(t_stack *stack_a, t_stack *stack_b); -t_bool stack_empty(t_stack *stack); -int stack_length(t_stack *stack); +void stack_swap_2(t_stack *stack_a, t_stack *stack_b); +void stack_rotate_2(t_stack *stack_a, t_stack *stack_b); +void stack_reverse_rotate_2(t_stack *stack_a, t_stack *stack_b); +t_bool stack_empty(t_stack *stack); +int stack_length(t_stack *stack); /* ** parse.c */ -t_status parse(int argc, char **argv, t_stack *a); +t_status parse(int argc, char **argv, t_stack *a); #endif diff --git a/src/common/stack_core.c b/src/common/stack_core.c index bf766d4..adab5b1 100644 --- a/src/common/stack_core.c +++ b/src/common/stack_core.c @@ -6,13 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 06:37:45 by cacharle #+# #+# */ -/* Updated: 2020/01/19 13:33:08 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 10:23:18 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -t_stack *stack_new(int size) +t_stack *stack_new(int size) { t_stack *stack; @@ -28,7 +28,7 @@ t_stack *stack_new(int size) return (stack); } -void stack_destroy(t_stack *stack) +void stack_destroy(t_stack *stack) { if (stack == NULL) return ; @@ -36,20 +36,20 @@ void stack_destroy(t_stack *stack) free(stack); } -void stack_push(t_stack *stack, int n) +void stack_push(t_stack *stack, int n) { stack->top++; stack->elements[stack->top] = n; } -void stack_pop(t_stack *stack) +void stack_pop(t_stack *stack) { if (stack_empty(stack)) return ; stack->top--; } -int stack_peek(t_stack *stack) +int stack_peek(t_stack *stack) { if (stack_empty(stack)) return (0); diff --git a/src/common/stack_helper.c b/src/common/stack_helper.c index 31d0fea..968060f 100644 --- a/src/common/stack_helper.c +++ b/src/common/stack_helper.c @@ -6,25 +6,25 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 06:40:18 by cacharle #+# #+# */ -/* Updated: 2020/01/21 11:08:06 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 10:22:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "common.h" -inline void stack_swap_2(t_stack *stack_a, t_stack *stack_b) +inline void stack_swap_2(t_stack *stack_a, t_stack *stack_b) { stack_swap(stack_a); stack_swap(stack_b); } -inline void stack_rotate_2(t_stack *stack_a, t_stack *stack_b) +inline void stack_rotate_2(t_stack *stack_a, t_stack *stack_b) { stack_rotate(stack_a); stack_rotate(stack_b); } -inline void stack_reverse_rotate_2(t_stack *stack_a, t_stack *stack_b) +inline void stack_reverse_rotate_2(t_stack *stack_a, t_stack *stack_b) { stack_reverse_rotate(stack_a); stack_reverse_rotate(stack_b); diff --git a/src/push_swap/main.c b/src/push_swap/main.c index b97feb3..fa34b35 100644 --- a/src/push_swap/main.c +++ b/src/push_swap/main.c @@ -6,13 +6,13 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 09:09:59 by cacharle #+# #+# */ -/* Updated: 2020/01/19 13:37:26 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 10:36:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -int main(int argc, char **argv) +int main(int argc, char **argv) { t_stack *a; t_stack *b; @@ -28,22 +28,10 @@ int main(int argc, char **argv) stack_destroy(a); return (1); } - a->tag = STACK_A; b->tag = STACK_B; - push_swap_qsort(a, b); - /* push_swap_qsort(a, b); */ - /* push_swap_qsort(a, b); */ - /* push_swap_qsort(a, b); */ - /* push_swap_qsort(a, b); */ - - /* printf("\na: "); */ - /* stack_print(a); */ - /* printf("b: "); */ - /* stack_print(b); */ - stack_destroy(a); stack_destroy(b); - return 0; + return (0); } diff --git a/src/push_swap/push_swap.h b/src/push_swap/push_swap.h index 0bffca5..87443a1 100644 --- a/src/push_swap/push_swap.h +++ b/src/push_swap/push_swap.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 09:10:11 by cacharle #+# #+# */ -/* Updated: 2020/01/22 08:59:59 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 10:42:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ ** sort.c */ -void push_swap_sort(t_stack *main, t_stack *tmp); +void push_swap_qsort(t_stack *main, t_stack *tmp); /* ** stack_wrapper.c diff --git a/src/push_swap/sort.c b/src/push_swap/sort.c index c712d50..1639769 100644 --- a/src/push_swap/sort.c +++ b/src/push_swap/sort.c @@ -6,24 +6,15 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 09:12:02 by cacharle #+# #+# */ -/* Updated: 2020/01/22 08:55:55 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 11:08:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -void stack_print(t_stack *stack) +static int frame_length(t_stack *st, int frame_index) { - printf("%s> ", stack->tag == STACK_A ? "A" : "B"); - for (int i = 0; i <= stack->top; i++) - printf("%2d | ", stack->elements[i]); - printf("\n"); -} - - -static int frame_length(t_stack *st, int frame_index) -{ - return stack_length(st) - frame_index; + return (stack_length(st) - frame_index); } /* @@ -45,7 +36,8 @@ static int frame_length(t_stack *st, int frame_index) ** each of those will be sorted in the next recursion */ -static void push_swap_qsort_partition(t_stack *main, t_stack *tmp, int main_frame) +static void push_swap_qsort_partition(t_stack *main, t_stack *tmp, + int main_frame) { int pivot; int hidden_count; @@ -57,7 +49,8 @@ static void push_swap_qsort_partition(t_stack *main, t_stack *tmp, int main_fram frame_len = frame_length(main, main_frame) - 1; while (frame_len > 0) { - if (main->tag == STACK_A ? (stack_peek(main) < pivot) : (stack_peek(main) > pivot)) + if (main->tag == STACK_A ? + (stack_peek(main) < pivot) : (stack_peek(main) > pivot)) stack_push_to_print(main, tmp); else { @@ -66,6 +59,8 @@ static void push_swap_qsort_partition(t_stack *main, t_stack *tmp, int main_fram } frame_len--; } + if (stack_peek(main) == pivot) + return ; while (hidden_count-- > 0) stack_reverse_rotate_print(main); } @@ -84,24 +79,29 @@ static void push_swap_qsort_partition(t_stack *main, t_stack *tmp, int main_fram ** push all elements of tmp on main */ -static void push_swap_qsort_rec(t_stack *main, t_stack *tmp, int main_frame, int tmp_frame) +static void push_swap_qsort_rec(t_stack *main, t_stack *tmp, + int main_frame, int tmp_frame) { + int tmp_frame_len; + if (frame_length(main, main_frame) < 2) return ; - + if (frame_length(main, main_frame) == 2) + { + if (main->tag == STACK_A ? + main->elements[main->top] > main->elements[main->top - 1] + : main->elements[main->top] < main->elements[main->top - 1]) + stack_swap_print(main); + return ; + } push_swap_qsort_partition(main, tmp, main_frame); - push_swap_qsort_rec(tmp, main, tmp_frame, stack_length(main)); stack_push_to_print(main, tmp); push_swap_qsort_rec(main, tmp, main_frame, stack_length(tmp)); stack_push_to_print(tmp, main); - - int tmp_frame_len = frame_length(tmp, tmp_frame); - while (tmp_frame_len != 0) - { + tmp_frame_len = frame_length(tmp, tmp_frame); + while (tmp_frame_len-- > 0) stack_push_to_print(tmp, main); - tmp_frame_len--; - } } /* diff --git a/src/push_swap/stack_wrapper.c b/src/push_swap/stack_wrapper.c index 798f174..be18c0e 100644 --- a/src/push_swap/stack_wrapper.c +++ b/src/push_swap/stack_wrapper.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 13:05:58 by cacharle #+# #+# */ -/* Updated: 2020/01/21 10:19:58 by cacharle ### ########.fr */ +/* Updated: 2020/01/22 10:04:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ |
