]> Untitled Git - axy/ft/pushswap.git/commitdiff
Leaf sort parsing
author= <=>
Wed, 10 Dec 2025 18:20:50 +0000 (19:20 +0100)
committer= <=>
Wed, 10 Dec 2025 18:20:50 +0000 (19:20 +0100)
.gitignore
Makefile
leaf_sort.h [new file with mode: 0644]
leaf_sort_parse.c [new file with mode: 0644]
main_pushswap.c
op_parse.c [new file with mode: 0644]
pushswap.h
slice.c

index 08077beadd67879029505259b3a9a83aa0c6e13c..e6065975be55bf5c037c3d505093ebdd27150083 100644 (file)
@@ -1,3 +1,4 @@
 .build
+*.swp
 push_swap
 checker
index 83ad8fd50e01686d9008205f5bf346411cbf99a9..6986a10e825932cdc5ac5b60676dc15a6e403cc3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 NAME=push_swap
 
-SRCS=algorithm_quicksort.c algorithm_selection_sort.c cheatalloc.c clist.c list.c main_pushswap.c op_output.c op_p.c op_r.c op_rr.c op_s.c ops.c ops_groups.c ops_optimizer.c output.c psval.c slice.c stack.c stacks.c
+SRCS=algorithm_quicksort.c algorithm_selection_sort.c cheatalloc.c clist.c leaf_sort_parse.c list.c main_pushswap.c op_output.c op_p.c op_parse.c op_r.c op_rr.c op_s.c ops.c ops_groups.c ops_optimizer.c output.c psval.c slice.c stack.c stacks.c
 
 RESSRCS=_res_leaf_sort_lookup.h
 
@@ -42,7 +42,7 @@ re : fclean all
 
 remakefile : clean
        sed -i "s/^SRCS=.*/$$(echo -n SRCS=; echo -n *.c)/" Makefile
-       sed -i "s/^BINSRCS=.*/$$(echo -n ASSETSRCS=; echo -n _res_*.h)/" Makefile
+       sed -i "s/^RESSRCS=.*/$$(echo -n RESSRCS=; echo -n _res_*.h)/" Makefile
 
 .PHONY : all clean fclean re bonus remakefile
 
diff --git a/leaf_sort.h b/leaf_sort.h
new file mode 100644 (file)
index 0000000..1850198
--- /dev/null
@@ -0,0 +1,46 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   leaf_sort.h                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/10 18:01:04 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/10 18:35:51 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#ifndef LEAF_SORT_H
+# define LEAF_SORT_H
+
+# include "pushswap.h"
+
+# define LEAF_OPS_MAX 12
+
+typedef struct s_leaf_solution
+{
+       size_t  used;
+       t_op    store[LEAF_OPS_MAX];
+}      t_leaf_solution;
+
+typedef struct s_leaf_lookup
+{
+       t_leaf_solution zero[1];
+       t_leaf_solution one[1];
+       t_leaf_solution two[2];
+       t_leaf_solution three[6];
+       t_leaf_solution four[24];
+}      t_leaf_lookup;
+
+typedef struct s_leaf_lookups
+{
+       t_leaf_lookup   local;
+       t_leaf_lookup   other;
+}      t_leaf_lookups;
+
+extern void            _binary__build_leaf_sort_lookup_res_start(void);
+extern void            _binary__build_leaf_sort_lookup_res_end(void);
+
+t_leaf_lookups *leaf_lookups(void);
+
+#endif
diff --git a/leaf_sort_parse.c b/leaf_sort_parse.c
new file mode 100644 (file)
index 0000000..2afd729
--- /dev/null
@@ -0,0 +1,83 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   leaf_sort_parse.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/10 17:59:06 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/10 19:15:53 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "leaf_sort.h"
+
+static t_leaf_solution leaf_solution_parse(t_slice *iter)
+{
+       t_leaf_solution res;
+       t_slice                 rem;
+       t_slice                 curr;
+       t_op                    op;
+
+       rem = slice_split(iter, '\n');
+       res.used = 0;
+       while (rem.len)
+       {
+               curr = slice_split(&rem, ' ');
+               op = op_parse(curr);
+               if (op == OP_INVALID || res.used >= LEAF_OPS_MAX)
+                       cheatexit(1);
+               res.store[res.used++] = op;
+       }
+       return (res);
+}
+
+static t_leaf_lookup   leaf_lookup_parse(t_slice *iter)
+{
+       int                             i;
+       t_leaf_lookup   res;
+
+       i = 0;
+       while (i < 1)
+               res.zero[i++] = leaf_solution_parse(iter);
+       i = 0;
+       while (i < 1)
+               res.one[i++] = leaf_solution_parse(iter);
+       i = 0;
+       while (i < 2)
+               res.two[i++] = leaf_solution_parse(iter);
+       i = 0;
+       while (i < 6)
+               res.three[i++] = leaf_solution_parse(iter);
+       i = 0;
+       while (i < 24)
+               res.four[i++] = leaf_solution_parse(iter);
+       return (res);
+}
+
+static t_leaf_lookups  leaf_lookups_parse(t_slice iter)
+{
+       t_leaf_lookups  res;
+
+       res.local = leaf_lookup_parse(&iter);
+       res.other = leaf_lookup_parse(&iter);
+       if (iter.len)
+               cheatexit(1);
+       return (res);
+}
+
+t_leaf_lookups *leaf_lookups(void)
+{
+       static t_leaf_lookups   *store = NULL;
+       t_slice                                 raw;
+
+       if (store)
+               return (store);
+       store = cheatalloc(sizeof(t_leaf_lookups));
+       raw = slice_new_range(
+                       (void *)&_binary__build_leaf_sort_lookup_res_start,
+                       (void *)&_binary__build_leaf_sort_lookup_res_end
+                       );
+       *store = leaf_lookups_parse(raw);
+       return (store);
+}
index ea9cb23987b9d5ac70cf154f98c02ec22cb5537d..06be176a551c13471c8c5ab82b6e711978209077 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 22:15:12 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/10 17:56:45 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/10 19:17:52 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -38,19 +38,20 @@ static void arg_step(char *str, t_args *arg)
                stack_push_back(&arg->state.a, psval_parse(str));
 }
 
+#include "leaf_sort.h"
+#include <stdio.h>
+
 int    main(int argc, char **argv)
 {
        t_args          args;
        t_ops           ops;
        t_optimizer     opt;
 
-       slice_output(slice_new_range(
-                       &_binary__build_leaf_sort_lookup_res_start,
-                       &_binary__build_leaf_sort_lookup_res_end
-                       ));
        args.state = stacks_new();
        args.bench = false;
        args.algo = NULL;
+       t_leaf_lookups *test = leaf_lookups();
+       printf("%i", test->other.four[0].store[2]);
        for (int i = 1; i < argc; i++)
                arg_step(argv[i], &args);
        ops = ops_compose(ops_stackops(), ops_optimizer(ops_output(), &opt));
diff --git a/op_parse.c b/op_parse.c
new file mode 100644 (file)
index 0000000..d5195aa
--- /dev/null
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   op_parse.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2025/12/10 18:11:35 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/10 18:19:50 by agilliar         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pushswap.h"
+
+t_op   op_parse(t_slice s)
+{
+       if (slice_eq(s, slice_new("sa")))
+               return (OP_SA);
+       if (slice_eq(s, slice_new("sb")))
+               return (OP_SB);
+       if (slice_eq(s, slice_new("ss")))
+               return (OP_SS);
+       if (slice_eq(s, slice_new("pa")))
+               return (OP_PA);
+       if (slice_eq(s, slice_new("pb")))
+               return (OP_PB);
+       if (slice_eq(s, slice_new("ra")))
+               return (OP_RA);
+       if (slice_eq(s, slice_new("rb")))
+               return (OP_RB);
+       if (slice_eq(s, slice_new("rr")))
+               return (OP_RR);
+       if (slice_eq(s, slice_new("rra")))
+               return (OP_RRA);
+       if (slice_eq(s, slice_new("rrb")))
+               return (OP_RRB);
+       if (slice_eq(s, slice_new("rrr")))
+               return (OP_RRR);
+       return (OP_INVALID);
+}
index 41bdfeb28cbb0e58b6c9b945675312d77409be3b..857221263ec4fb28e8c01890e3e68b7b001abc10 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 11:02:44 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/10 17:44:35 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/10 18:19:34 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -58,7 +58,7 @@ typedef struct s_args
 
 typedef enum e_op
 {
-       OP_SENTINEL = -1,
+       OP_INVALID = -1,
        OP_SA = 0,
        OP_SB = 1,
        OP_SS = 2,
@@ -96,9 +96,6 @@ typedef struct s_slice
        const char      *ptr;
 }      t_slice;
 
-extern void    _binary__build_leaf_sort_lookup_res_start(void);
-extern void    _binary__build_leaf_sort_lookup_res_end(void);
-
 void           cheatexit(int errcode);
 void           *cheatalloc(size_t len);
 
@@ -128,6 +125,8 @@ void                stack_move(t_stack *src, t_stack *dst);
 t_psval                psval_parse(const char *s);
 void           psval_output(t_psval val);
 
+t_op           op_parse(t_slice s);
+
 t_closure      op_pa(void);
 t_closure      op_pb(void);
 t_closure      op_ra(void);
@@ -159,7 +158,7 @@ void                list_sort(t_list list);
 t_slice                slice_new(const char *s);
 t_slice                slice_new_range(void *start, void *end);
 bool           slice_eq(t_slice lhs, t_slice rhs);
-t_slice                slice_split_first(t_slice *rem, char at);
+t_slice                slice_split(t_slice *rem, char at);
 void           slice_output(t_slice s);
 
 void           algorithm_selection_sort(const t_stacks *stacks, t_closure cb);
diff --git a/slice.c b/slice.c
index f9bf5ef4c6c78d1e9ae9a15cfc3e6ce793246d16..28536a655581ff2f0f277a6457922df37b59b88a 100644 (file)
--- a/slice.c
+++ b/slice.c
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/10 17:08:21 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/10 17:56:18 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/10 18:41:15 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -44,7 +44,7 @@ bool  slice_eq(t_slice lhs, t_slice rhs)
        return (i == lhs.len);
 }
 
-t_slice        slice_split_first(t_slice *rem, char at)
+t_slice        slice_split(t_slice *rem, char at)
 {
        t_slice res;
 
@@ -52,7 +52,7 @@ t_slice       slice_split_first(t_slice *rem, char at)
        res.ptr = rem->ptr;
        while (rem->len)
        {
-               if (*rem->ptr == at)
+               if (*(rem->ptr) == at)
                        break ;
                rem->len--;
                rem->ptr++;