]> Untitled Git - axy/ft/pushswap.git/commitdiff
Main refactor things
author= <=>
Tue, 16 Dec 2025 20:16:26 +0000 (21:16 +0100)
committer= <=>
Tue, 16 Dec 2025 20:16:26 +0000 (21:16 +0100)
main_pushswap.c
pushswap.h

index a64c52c20e5897ae0ad66927c559a0bf48571783..8aee6128b7ec038fb9994e08e16060c2e9dff57a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 22:15:12 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/16 19:13:54 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/16 20:28:03 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -22,18 +22,75 @@ static bool ft_streq(char *str1, char *str2)
        return (str1[i] == '\0' && str2[i] == '\0');
 }
 
+typedef enum   e_algovar
+{
+       ALGORITHM_SIMPLE,
+       ALGORITHM_MEDIUM,
+       ALGORITHM_COMPLEX,
+       ALGORITHM_ADAPTIVE,
+}      t_algovar;
+
+typedef struct s_args
+{
+       t_stacks        state;
+       t_algovar       algo;
+       t_algorithm     *algo_func;
+       bool            bench;
+       float           disorder;
+}      t_args;
+
+void algorithm_medium(const t_stacks *stacks, t_closure cb)
+{
+       algorithm_complex(stacks, cb);
+}
+
+static t_algorithm     *algorithm_get(t_algovar var, float disorder)
+{
+       if (var == ALGORITHM_SIMPLE)
+               return (&algorithm_simple);
+       if (var == ALGORITHM_MEDIUM)
+               return (&algorithm_medium);
+       if (var == ALGORITHM_COMPLEX)
+               return (&algorithm_complex);
+       if (disorder < 0.2)
+               return (&algorithm_simple);
+       if (disorder < 0.5)
+               return (&algorithm_medium);
+       return (&algorithm_complex);
+}
+
+#define COMPLEXITY_SIMPLE "Simple / O(N^2)"
+#define COMPLEXITY_MEDIUM "Simple / O(N^(3/2))"
+#define COMPLEXITY_COMPLEX "Simple / O(N log N)"
+#define COMPLEXITY_ADAPTIVE_LOW "Adaptive / O(N^2)"
+#define COMPLEXITY_ADAPTIVE_MEDIUM "Adaptive / O(N^(3/2))"
+#define COMPLEXITY_ADAPTIVE_HIGH "Adaptive / O(N log N)"
+
+t_slice        complexity_get(t_algovar var, float disorder)
+{
+       if (var == ALGORITHM_SIMPLE)
+               return (slice_new(COMPLEXITY_SIMPLE));
+       if (var == ALGORITHM_MEDIUM)
+               return (slice_new(COMPLEXITY_MEDIUM));
+       if (var == ALGORITHM_COMPLEX)
+               return (slice_new(COMPLEXITY_COMPLEX));
+       if (disorder < 0.2)
+               return (slice_new(COMPLEXITY_ADAPTIVE_LOW));
+       if (disorder < 0.5)
+               return (slice_new(COMPLEXITY_ADAPTIVE_MEDIUM));
+       return (slice_new(COMPLEXITY_ADAPTIVE_HIGH));
+}
+
 static void    arg_step(char *str, t_args *arg)
 {
        if (ft_streq(str, "--simple"))
-               arg->algo = &algorithm_simple;
+               arg->algo = ALGORITHM_SIMPLE;
        else if (ft_streq(str, "--medium"))
-               arg->algo = NULL;
+               arg->algo = ALGORITHM_MEDIUM;
        else if (ft_streq(str, "--complex"))
-               arg->algo = &algorithm_complex;
+               arg->algo = ALGORITHM_COMPLEX;
        else if (ft_streq(str, "--adaptive"))
-               arg->algo = NULL;
-       else if (ft_streq(str, "--leaf"))
-               arg->algo = &algorithm_leafsort;
+               arg->algo = ALGORITHM_ADAPTIVE;
        else if (ft_streq(str, "--bench"))
                arg->bench = true;
        else
@@ -54,11 +111,12 @@ static t_benchres  pushswap_solve(t_args *args)
        t_optimizer     opt;
        t_benchres      res;
 
+       res.disorder = args->disorder;
        ops = ops_compose(ops_output(), ops_count(&res.post_opt));
        ops = ops_optimizer(ops, &opt);
        ops = ops_compose(ops_stackops(), ops);
        ops = ops_compose(ops_count(&res.pre_opt), ops);
-       stacks_apply(&args->state, &ops, args->algo);
+       stacks_apply(&args->state, &ops, args->algo_func);
        ops_optimizer_commit(&opt, &args->state);
        return (res);
 }
@@ -72,10 +130,12 @@ int        main(int argc, char **argv)
 
        args.state = stacks_new();
        args.bench = false;
-       args.algo = NULL;
+       args.algo = ALGORITHM_ADAPTIVE;
+       args.disorder = 1.0;
        i = 1;
        while (i < argc)
                arg_step(argv[i++], &args);
+       args.algo_func = algorithm_get(args.algo, args.disorder);
        res = pushswap_solve(&args);
        output_flush();
        printf("pre: %lu\n", res.pre_opt.counters[OP_RR]);
index e9a077aaf394ddc92a1b0b450a86e9e608b3c2aa..e75801925cad50a2812ab1a4921619190e7c7629 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 11:02:44 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/16 19:08:25 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/16 20:18:54 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -49,13 +49,6 @@ typedef struct s_closure
 // Takes a stack and repeatedly calls the closure with the op to be applied
 typedef void   (t_algorithm)(const t_stacks *, t_closure);
 
-typedef struct s_args
-{
-       t_stacks        state;
-       t_algorithm     *algo;
-       bool            bench;
-}      t_args;
-
 typedef enum e_op
 {
        OP_INVALID = -1,