]> Untitled Git - axy/ft/pushswap.git/commitdiff
Ops
author= <=>
Wed, 3 Dec 2025 13:01:30 +0000 (14:01 +0100)
committer= <=>
Wed, 3 Dec 2025 13:01:30 +0000 (14:01 +0100)
15 files changed:
.gitignore
Makefile
main_pushswap.c
ops.c [new file with mode: 0644]
ops_inner.h [new file with mode: 0644]
ops_output.c [new file with mode: 0644]
ops_p.c [new file with mode: 0644]
ops_r.c [new file with mode: 0644]
ops_rr.c [new file with mode: 0644]
ops_s.c [new file with mode: 0644]
output.c
output_inner.h
pushswap.h
stack.c [new file with mode: 0644]
stacks.c

index 2e2555a60ff1a79e589a352ac463ad7c59a2ae83..78fb45056fa52e3e25432eeb73a5c47b9450b8f8 100644 (file)
@@ -1,4 +1,4 @@
 **.out
 **.o
-pushswap
+push_swap
 checker
index 7faa4eaaebc16693e4c18b17524cb1b2279f3758..c26fefc63d862ed47f148585c166155aeb801eae 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 NAME=push_swap
 
-SRCS=
+SRCS=cheatalloc.c clist.c main_pushswap.c ops.c ops_output.c ops_p.c ops_r.c ops_rr.c ops_s.c output.c psval.c stack.c stacks.c
 
 OBJS=${SRCS:.c=.o}
 
@@ -26,4 +26,7 @@ re : fclean all
 
 bonus : ${NAME}
 
+remakefile :
+       sed -i "s/^SRCS=.*/$$(echo -n SRCS=; echo -n *.c)/" Makefile
+
 .PHONY : all clean fclean re bonus
index 9c4970efcf1d65dedf26a3275709ebc55be98705..d5f26ccef4c3fb6091bb4710013d109f7d4fafb9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: agilliar <agilliar@student.42mulhouse.fr>  +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 22:15:12 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/02 23:15:12 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/03 12:52:57 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,7 +18,7 @@ int   main(int argc, char **argv)
 
        stacks = stacks_new();
        for (int i = 1; i < argc; i++)
-               stacks_insert_init(&stacks, psval_parse(argv[i]));
+               stack_push_back(&stacks.a, psval_parse(argv[i]));
        for (int i = 1; i < argc; i++)
                psval_output(psval_parse(argv[i]));
        output_flush();
diff --git a/ops.c b/ops.c
new file mode 100644 (file)
index 0000000..b848200
--- /dev/null
+++ b/ops.c
@@ -0,0 +1,33 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ops.c                                              :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 10:57:30 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 13:12:42 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+#include "ops_inner.h"
+
+static void    ops_compose_fn(t_ops_compose *data, t_stacks *stacks)
+{
+       (data->first.func)(data->first.data, stacks);
+       (data->second.func)(data->second.data, stacks);
+}
+
+t_closure      ops_compose(t_closure first, t_closure second)
+{
+       t_ops_compose   *data;
+       t_closure               closure;
+
+       data = cheatalloc(sizeof(t_ops_compose));
+       data->first = first;
+       data->second = second;
+       closure.func = &ops_compose_fn;
+       closure.data = data;
+       return (closure);
+}
diff --git a/ops_inner.h b/ops_inner.h
new file mode 100644 (file)
index 0000000..1a826eb
--- /dev/null
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ops_inner.h                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 11:13:51 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 11:14:05 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef OPS_INNER_H
+# define OPS_INNER_H
+
+typedef struct s_ops_compose
+{
+       t_closure       first;
+       t_closure       second;
+}      t_ops_compose;
+
+#endif
diff --git a/ops_output.c b/ops_output.c
new file mode 100644 (file)
index 0000000..15ed678
--- /dev/null
@@ -0,0 +1,41 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ops_output.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 12:28:28 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 12:34:38 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+static void    ops_output_inner(void *data, t_op op)
+{
+       const char *const       array[11] = {
+               "sa",
+               "sb",
+               "ss",
+               "pa",
+               "pb",
+               "ra",
+               "rb",
+               "rr",
+               "rra",
+               "rrb",
+               "rrr",
+       };
+       (void) data;
+       output_str_ln(array[op]);
+}
+
+t_closure      ops_output(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_output_inner;
+       return (res);
+}
diff --git a/ops_p.c b/ops_p.c
new file mode 100644 (file)
index 0000000..c2118c3
--- /dev/null
+++ b/ops_p.c
@@ -0,0 +1,43 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ops_p.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 13:20:25 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 13:22:22 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+static void    ops_pa_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_move(&stacks->b, &stacks->a);
+}
+
+t_closure      ops_pa(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_pa_inner;
+       return (res);
+}
+
+static void    ops_pb_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_move(&stacks->a, &stacks->b);
+}
+
+t_closure      ops_pb(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_pb_inner;
+       return (res);
+}
diff --git a/ops_r.c b/ops_r.c
new file mode 100644 (file)
index 0000000..69fb078
--- /dev/null
+++ b/ops_r.c
@@ -0,0 +1,48 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ops_r.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 13:15:06 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 13:17:00 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+static void    ops_ra_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_rotate_n(&stacks->a, 1);
+}
+
+t_closure      ops_ra(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_ra_inner;
+       return (res);
+}
+
+static void    ops_rb_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_rotate_n(&stacks->b, 1);
+}
+
+t_closure      ops_rb(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_rb_inner;
+       return (res);
+}
+
+t_closure      ops_rr(void)
+{
+       return (ops_compose(ops_ra(), ops_rb()));
+}
diff --git a/ops_rr.c b/ops_rr.c
new file mode 100644 (file)
index 0000000..4d40bb6
--- /dev/null
+++ b/ops_rr.c
@@ -0,0 +1,48 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ops_rr.c                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 13:17:23 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 13:19:47 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+static void    ops_rra_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_rotate_n(&stacks->a, 1);
+}
+
+t_closure      ops_rra(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_rra_inner;
+       return (res);
+}
+
+static void    ops_rrb_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_rotate_n(&stacks->b, 1);
+}
+
+t_closure      ops_rrb(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_rrb_inner;
+       return (res);
+}
+
+t_closure      ops_rrr(void)
+{
+       return (ops_compose(ops_rra(), ops_rrb()));
+}
diff --git a/ops_s.c b/ops_s.c
new file mode 100644 (file)
index 0000000..61ed417
--- /dev/null
+++ b/ops_s.c
@@ -0,0 +1,48 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ops_s.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 12:28:28 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 13:14:52 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+static void    ops_sa_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_swap_top(&stacks->a);
+}
+
+t_closure      ops_sa(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_sa_inner;
+       return (res);
+}
+
+static void    ops_sb_inner(void *data, t_stacks *stacks)
+{
+       (void) data;
+       stack_swap_top(&stacks->b);
+}
+
+t_closure      ops_sb(void)
+{
+       t_closure       res;
+
+       res.data = NULL;
+       res.func = &ops_sb_inner;
+       return (res);
+}
+
+t_closure      ops_ss(void)
+{
+       return (ops_compose(ops_sa(), ops_sb()));
+}
index c1fed0b8528179259bf84f643e1246495b604982..7239040e79fb8b8a49fc66ee1a65a98911b7a5d5 100644 (file)
--- a/output.c
+++ b/output.c
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 16:47:00 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/02 22:42:38 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/03 12:35:06 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -49,13 +49,13 @@ void        output_char(char c)
        store->buf[store->written++] = c;
 }
 
-void   output_str(char *s)
+void   output_str(const char *s)
 {
        while (*s)
                output_char(*(s++));
 }
 
-void   output_str_ln(char *s)
+void   output_str_ln(const char *s)
 {
        output_str(s);
        output_char('\n');
index b9b7e52937e58965f23d7f44950011afdc6b7aeb..6d327882027dbbd326f30014d411e952ecb404b8 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 16:51:14 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/02 17:09:26 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/03 09:51:35 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,7 +21,7 @@
 typedef struct s_output_store
 {
        size_t  written;
-       char    buf[1024];
+       char    buf[OUTPUT_STORE_SIZE];
 }      t_output_store;
 
 #endif
index 701536873304e726ca723435a8b20b485847444f..a7eecee83d94bfae84effcdf0574b19d7d15f855 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 11:02:44 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/02 23:05:01 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/03 13:25:21 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -25,12 +25,17 @@ typedef struct s_clist
        struct s_clist  *prev;
 }      t_clist;
 
+// list        : Nullable
+typedef struct s_stack
+{
+       t_clist *list;
+       size_t  len;
+}      t_stack;
+
 typedef struct s_stacks
 {
-       t_clist *a_list;
-       size_t  a_len;
-       t_clist *b_list;
-       size_t  b_len;
+       t_stack a;
+       t_stack b;
 }      t_stacks;
 
 // Func always receives as a first argument `data`
@@ -83,16 +88,34 @@ void                clist_push(t_clist **dst, t_clist *lst);
 void           clist_push_back(t_clist **dst, t_clist *lst);
 
 void           output_char(char c);
-void           output_str(char *s);
-void           output_str_ln(char *s);
+void           output_str(const char *s);
+void           output_str_ln(const char *s);
 void           output_flush(void);
 
 t_stacks       stacks_new(void);
-void           stacks_insert_init(t_stacks *stacks, t_psval val);
 bool           stacks_is_solved(const t_stacks *stacks);
 void           stacks_apply(t_stacks *stacks, const t_ops *ops, t_algorithm algo);
 
+void           stack_push_back(t_stack *stack, t_psval val);
+void           stack_rotate_n(t_stack *stack, int n);
+void           stack_swap_top(t_stack *stack);
+void           stack_move(t_stack *src, t_stack *dst);
+
 t_psval                psval_parse(const char *s);
 void           psval_output(t_psval val);
 
+t_closure      ops_compose(t_closure first, t_closure second);
+t_closure      ops_output(void);
+t_closure      ops_pa(void);
+t_closure      ops_pb(void);
+t_closure      ops_ra(void);
+t_closure      ops_rb(void);
+t_closure      ops_rr(void);
+t_closure      ops_rra(void);
+t_closure      ops_rrb(void);
+t_closure      ops_rrr(void);
+t_closure      ops_sa(void);
+t_closure      ops_sb(void);
+t_closure      ops_ss(void);
+
 #endif
diff --git a/stack.c b/stack.c
new file mode 100644 (file)
index 0000000..91071e3
--- /dev/null
+++ b/stack.c
@@ -0,0 +1,55 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   stack.c                                            :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/03 12:53:25 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/03 13:08:25 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+void   stack_push_back(t_stack *stack, t_psval val)
+{
+       clist_push_back(&stack->list, clist_new(val));
+       stack->len++;
+}
+
+void   stack_rotate_n(t_stack *stack, int n)
+{
+       if (stack->len == 0)
+               return ;
+       while (n < 0)
+       {
+               stack->list = stack->list->next;
+               n++;
+       }
+       while (n > 0)
+       {
+               stack->list = stack->list->next;
+               n--;
+       }
+}
+
+void   stack_swap_top(t_stack *stack)
+{
+       t_psval tmp;
+
+       if (stack->len == 0)
+               return ;
+       tmp = stack->list->val;
+       stack->list->val = stack->list->next->val;
+       stack->list->next->val = tmp;
+}
+
+void   stack_move(t_stack *src, t_stack *dst)
+{
+       if (src->len == 0)
+               return ;
+       src->len--;
+       dst->len++;
+       clist_push(&dst->list, clist_pop(&src->list));
+}
index b8a0a8201f46a01abf7f21cec49d647dd066671d..886c6078b15f9d2a5bab8313abdcc17eb6dd8d10 100644 (file)
--- a/stacks.c
+++ b/stacks.c
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 15:57:41 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/02 22:25:54 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/03 12:53:20 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -17,28 +17,22 @@ t_stacks    stacks_new(void)
 {
        t_stacks        res;
 
-       res.a_list = NULL;
-       res.a_len = 0;
-       res.b_list = NULL;
-       res.b_len = 0;
+       res.a.list = NULL;
+       res.a.len = 0;
+       res.b.list = NULL;
+       res.b.len = 0;
        return (res);
 }
 
-void   stacks_insert_init(t_stacks *stacks, t_psval val)
-{
-       clist_push_back(&stacks->a_list, clist_new(val));
-       stacks->a_len++;
-}
-
 bool   stacks_is_solved(const t_stacks *stacks)
 {
        size_t  i;
 
-       if (stacks->b_len != 0)
+       if (stacks->b.len != 0)
                return (false);
        i = 0;
-       while (++i < stacks->a_len)
-               if (clist_get_at(stacks->a_list, 0) > clist_get_at(stacks->a_list, -1))
+       while (++i < stacks->a.len)
+               if (clist_get_at(stacks->a.list, 0) > clist_get_at(stacks->a.list, -1))
                        return (false);
        return (true);
 }