aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/common.h37
-rw-r--r--src/common/stack.c103
-rw-r--r--src/common/stack_core.c56
-rw-r--r--src/common/stack_helper.c41
-rw-r--r--src/common/stack_op.c63
5 files changed, 177 insertions, 123 deletions
diff --git a/src/common/common.h b/src/common/common.h
index b914d5e..0cfd687 100644
--- a/src/common/common.h
+++ b/src/common/common.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/18 11:20:54 by cacharle #+# #+# */
-/* Updated: 2020/01/18 11:21:03 by cacharle ### ########.fr */
+/* Updated: 2020/01/19 06:43:24 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
@@ -24,7 +24,7 @@ typedef struct
} t_stack;
/*
-** stack.c
+** stack_core.c
*/
t_stack *stack_new(int size);
@@ -32,27 +32,24 @@ 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);
-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);
-t_bool stack_empty(t_stack *stack);
-int stack_length(t_stack *stack);
/*
-** action.c
+** 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);
+
+/*
+** stack_helper.c
*/
-void swap_a(t_stack *a);
-void swap_b(t_stack *b);
-void swap_both(t_stack *a, t_stack *b);
-void push_a(t_stack *a, t_stack *b);
-void push_b(t_stack *b, t_stack *a);
-void rotate_a(t_stack *a);
-void rotate_b(t_stack *b);
-void rotate_both(t_stack *a, t_stack *b);
-void reverse_rotate_a(t_stack *a);
-void reverse_rotate_b(t_stack *b);
-void reverse_rotate_both(t_stack *a, t_stack *b);
+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);
#endif
diff --git a/src/common/stack.c b/src/common/stack.c
deleted file mode 100644
index 269dbc4..0000000
--- a/src/common/stack.c
+++ /dev/null
@@ -1,103 +0,0 @@
-#include "common.h"
-
-t_stack *stack_new(int size)
-{
- t_stack *stack;
-
- if ((stack = (t_stack*)malloc(sizeof(t_stack))) == NULL)
- return (NULL);
- if ((stack->elements = (int*)malloc(sizeof(int) * size)) == NULL)
- {
- free(stack);
- return (NULL);
- }
- stack->top = -1;
- return (stack);
-}
-
-void stack_destroy(t_stack *stack)
-{
- if (stack == NULL)
- return ;
- free(stack->elements);
- free(stack);
-}
-
-void stack_push(t_stack *stack, int n)
-{
- stack->top++;
- printf("%d\n", stack->top);
- stack->elements[stack->top] = n;
-}
-
-void stack_pop(t_stack *stack)
-{
- if (stack_empty(stack))
- return ;
- stack->top--;
-}
-
-int stack_peek(t_stack *stack)
-{
- if (stack_empty(stack))
- return (0);
- return (stack->elements[stack->top]);
-}
-
-void stack_swap(t_stack *stack)
-{
- int first;
- int second;
-
- if (stack_length(stack) < 2)
- return ;
- first = stack_peek(stack);
- stack_pop(stack);
- second = stack_peek(stack);
- stack_pop(stack);
- stack_push(stack, first);
- stack_push(stack, second);
-}
-
-void stack_push_to(t_stack *from, t_stack *to)
-{
- int tmp;
-
- if (stack_empty(from))
- return ;
- tmp = stack_peek(from);
- stack_pop(from);
- stack_push(to, tmp);
-}
-
-void stack_rotate(t_stack *stack)
-{
- int tmp;
-
- if (stack_length(stack) < 2)
- return ;
- tmp = stack_peek(stack);
- ft_memmove(&stack->elements[1], stack->elements, sizeof(int) * stack->top);
- stack->elements[0] = tmp;
-}
-
-void stack_reverse_rotate(t_stack *stack)
-{
- int tmp;
-
- if (stack_length(stack) < 2)
- return ;
- tmp = stack->elements[0];
- ft_memmove(stack->elements, &stack->elements[1], sizeof(int) * stack->top);
- stack_push(stack, tmp);
-}
-
-t_bool stack_empty(t_stack *stack)
-{
- return (stack->top == -1);
-}
-
-int stack_length(t_stack *stack)
-{
- return (stack->top + 1);
-}
diff --git a/src/common/stack_core.c b/src/common/stack_core.c
new file mode 100644
index 0000000..6875862
--- /dev/null
+++ b/src/common/stack_core.c
@@ -0,0 +1,56 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* stack_core.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 06:37:45 by cacharle #+# #+# */
+/* Updated: 2020/01/19 06:50:50 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+t_stack *stack_new(int size)
+{
+ t_stack *stack;
+
+ if ((stack = (t_stack*)malloc(sizeof(t_stack))) == NULL)
+ return (NULL);
+ if ((stack->elements = (int*)malloc(sizeof(int) * size)) == NULL)
+ {
+ free(stack);
+ return (NULL);
+ }
+ stack->top = -1;
+ return (stack);
+}
+
+void stack_destroy(t_stack *stack)
+{
+ if (stack == NULL)
+ return ;
+ free(stack->elements);
+ free(stack);
+}
+
+void stack_push(t_stack *stack, int n)
+{
+ stack->top++;
+ stack->elements[stack->top] = n;
+}
+
+void stack_pop(t_stack *stack)
+{
+ if (stack_empty(stack))
+ return ;
+ stack->top--;
+}
+
+int stack_peek(t_stack *stack)
+{
+ if (stack_empty(stack))
+ return (0);
+ return (stack->elements[stack->top]);
+}
diff --git a/src/common/stack_helper.c b/src/common/stack_helper.c
new file mode 100644
index 0000000..6ea7cef
--- /dev/null
+++ b/src/common/stack_helper.c
@@ -0,0 +1,41 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* stack_helper.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 06:40:18 by cacharle #+# #+# */
+/* Updated: 2020/01/19 07:03:32 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+void stack_swap_2(t_stack *stack_a, t_stack *stack_b)
+{
+ stack_swap(stack_a);
+ stack_swap(stack_b);
+}
+
+void stack_rotate_2(t_stack *stack_a, t_stack *stack_b)
+{
+ stack_rotate(stack_a);
+ stack_rotate(stack_b);
+}
+
+void stack_reverse_rotate_2(t_stack *stack_a, t_stack *stack_b)
+{
+ stack_reverse_rotate(stack_a);
+ stack_reverse_rotate(stack_b);
+}
+
+t_bool stack_empty(t_stack *stack)
+{
+ return (stack->top == -1);
+}
+
+int stack_length(t_stack *stack)
+{
+ return (stack->top + 1);
+}
diff --git a/src/common/stack_op.c b/src/common/stack_op.c
new file mode 100644
index 0000000..e1c095b
--- /dev/null
+++ b/src/common/stack_op.c
@@ -0,0 +1,63 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* stack.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/19 06:37:44 by cacharle #+# #+# */
+/* Updated: 2020/01/19 07:06:58 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "common.h"
+
+void stack_swap(t_stack *stack)
+{
+ int first;
+ int second;
+
+ if (stack_length(stack) < 2)
+ return ;
+ first = stack_peek(stack);
+ stack_pop(stack);
+ second = stack_peek(stack);
+ stack_pop(stack);
+ stack_push(stack, first);
+ stack_push(stack, second);
+}
+
+void stack_push_to(t_stack *from, t_stack *to)
+{
+ int tmp;
+
+ if (stack_empty(from))
+ return ;
+ tmp = stack_peek(from);
+ stack_pop(from);
+ stack_push(to, tmp);
+}
+
+void stack_rotate(t_stack *stack)
+{
+ int tmp;
+
+ if (stack_length(stack) < 2)
+ return ;
+ tmp = stack_peek(stack);
+ ft_memmove(stack->elements + 1, stack->elements,
+ (stack_length(stack) - 1) * sizeof(int));
+ stack->elements[0] = tmp;
+}
+
+void stack_reverse_rotate(t_stack *stack)
+{
+ int tmp;
+
+ if (stack_length(stack) < 2)
+ return ;
+ tmp = stack->elements[0];
+ ft_memmove(stack->elements, stack->elements + 1,
+ (stack_length(stack) - 1) * sizeof(int));
+ stack->elements[stack->top] = tmp;
+}