From e483769e9192e52a72fb802c70b313da388a5904 Mon Sep 17 00:00:00 2001 From: Corentin Lefrere Date: Thu, 18 Dec 2025 17:53:01 +0100 Subject: [PATCH] Finished up --- Makefile | 2 +- main_pushswap.c | 97 ++++------------------------------------------- main_pushswap.h | 52 +++++++++++++++++++++++++ output_disorder.c | 36 ++++++++++++++++++ pushswap.h | 4 +- pushswap_args.c | 69 +++++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+), 92 deletions(-) create mode 100644 main_pushswap.h create mode 100644 output_disorder.c create mode 100644 pushswap_args.c diff --git a/Makefile b/Makefile index fbe903f..1209138 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ NAME=push_swap NAME_BONUS=checker -SRCS=algorithm_complex.c algorithm_medium.c algorithm_simple.c algorithm_stdin.c cheatalloc.c clist.c closure.c disorder.c eoutput.c frac.c ft_memcpy.c get_next_op.c leaf_sort.c leaf_sort_index.c leaf_sort_parse.c list.c numutils.c op_output.c op_p.c op_parse.c op_r.c op_rr.c op_s.c ops.c ops_count.c ops_optimizer.c ops_stackops.c output.c psval.c slice.c splitsort.c splitsort_final.c splitsort_iter.c splitsort_part.c stack.c stacks.c stacks_get.c +SRCS=algorithm_complex.c algorithm_medium.c algorithm_simple.c algorithm_stdin.c cheatalloc.c clist.c closure.c disorder.c eoutput.c frac.c ft_memcpy.c get_next_op.c leaf_sort.c leaf_sort_index.c leaf_sort_parse.c list.c numutils.c op_output.c op_p.c op_parse.c op_r.c op_rr.c op_s.c ops.c ops_count.c ops_optimizer.c ops_stackops.c output.c output_disorder.c psval.c pushswap_args.c slice.c splitsort.c splitsort_final.c splitsort_iter.c splitsort_part.c stack.c stacks.c stacks_get.c RESSRCS=_res_leaf_sort_lookup.h diff --git a/main_pushswap.c b/main_pushswap.c index 1669b92..9209c64 100644 --- a/main_pushswap.c +++ b/main_pushswap.c @@ -6,97 +6,11 @@ /* By: clefrere +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 22:15:12 by agilliar #+# #+# */ -/* Updated: 2025/12/18 16:12:13 by agilliar ### ########.fr */ +/* Updated: 2025/12/18 17:44:57 by clefrere ### ########.fr */ /* */ /* ************************************************************************** */ -#include "pushswap.h" - -static bool ft_streq(char *str1, char *str2) -{ - size_t i; - - i = 0; - while (str1[i] && str2[i] && str1[i] == str2[i]) - i++; - 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; - bool bench; -} t_args; - -static t_algorithm *algorithm_get(t_algovar var, t_frac disorder) -{ - if (var == ALGORITHM_SIMPLE) - return (&algorithm_simple); - if (var == ALGORITHM_MEDIUM) - return (&algorithm_medium); - if (var == ALGORITHM_COMPLEX) - return (&algorithm_complex); - if (frac_lt(disorder, frac_new(1, 5))) - return (&algorithm_simple); - if (frac_lt(disorder, frac_new(1, 2))) - return (&algorithm_medium); - return (&algorithm_complex); -} - -#define COMPLEXITY_SIMPLE "Simple / O(N^2)" -#define COMPLEXITY_MEDIUM "Medium / O(N^(3/2))" -#define COMPLEXITY_COMPLEX "Complex / 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)" - -static const char *complexity_get(t_algovar var, t_frac disorder) -{ - if (var == ALGORITHM_SIMPLE) - return (COMPLEXITY_SIMPLE); - if (var == ALGORITHM_MEDIUM) - return (COMPLEXITY_MEDIUM); - if (var == ALGORITHM_COMPLEX) - return (COMPLEXITY_COMPLEX); - if (frac_lt(disorder, frac_new(1, 5))) - return (COMPLEXITY_ADAPTIVE_LOW); - if (frac_lt(disorder, frac_new(1, 2))) - return (COMPLEXITY_ADAPTIVE_MEDIUM); - return (COMPLEXITY_ADAPTIVE_HIGH); -} - -static void arg_step(char *str, t_args *arg) -{ - if (ft_streq(str, "--simple")) - arg->algo = ALGORITHM_SIMPLE; - else if (ft_streq(str, "--medium")) - arg->algo = ALGORITHM_MEDIUM; - else if (ft_streq(str, "--complex")) - arg->algo = ALGORITHM_COMPLEX; - else if (ft_streq(str, "--adaptive")) - arg->algo = ALGORITHM_ADAPTIVE; - else if (ft_streq(str, "--bench")) - arg->bench = true; - else - stack_push_back(&arg->state.a, psval_parse(str)); -} - -typedef struct s_benchres -{ - t_ops_count pre_opt; - t_ops_count post_opt; - const char *strategy; - t_frac disorder; -} t_benchres; +#include "main_pushswap.h" static t_benchres pushswap_solve(t_args *args) { @@ -141,6 +55,9 @@ static void output_ops(t_ops_count ops) static void output_bench(t_benchres res) { eoutput_str_ln("[Bench]"); + eoutput_str("\tDisorder:\t"); + output_disorder(&res.disorder); + eoutput_str_ln("%"); eoutput_str("\tStrategy:\t"); eoutput_str_ln(res.strategy); eoutput_str("\tPost-Opt:\t"); @@ -149,7 +66,6 @@ static void output_bench(t_benchres res) output_ops(res.pre_opt); } -#include int main(int argc, char **argv) { t_args args; @@ -163,7 +79,8 @@ int main(int argc, char **argv) while (i < argc) arg_step(argv[i++], &args); res = pushswap_solve(&args); - output_bench(res); + if (args.bench) + output_bench(res); output_flush(); eoutput_flush(); cheatexit(!stacks_is_solved(&args.state)); diff --git a/main_pushswap.h b/main_pushswap.h new file mode 100644 index 0000000..9b31970 --- /dev/null +++ b/main_pushswap.h @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main_pushswap.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: clefrere +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/18 17:32:58 by clefrere #+# #+# */ +/* Updated: 2025/12/18 17:48:02 by clefrere ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MAIN_PUSHSWAP_H +# define MAIN_PUSHSWAP_H + +# define COMPLEXITY_SIMPLE "Simple / O(N^2)" +# define COMPLEXITY_MEDIUM "Medium / O(N^(3/2))" +# define COMPLEXITY_COMPLEX "Complex / 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)" + +# include "pushswap.h" + +typedef enum e_algovar +{ + ALGORITHM_SIMPLE, + ALGORITHM_MEDIUM, + ALGORITHM_COMPLEX, + ALGORITHM_ADAPTIVE, +} t_algovar; + +typedef struct s_args +{ + t_stacks state; + t_algovar algo; + bool bench; +} t_args; + +typedef struct s_benchres +{ + t_ops_count pre_opt; + t_ops_count post_opt; + const char *strategy; + t_frac disorder; +} t_benchres; + +t_algorithm *algorithm_get(t_algovar var, t_frac disorder); +const char *complexity_get(t_algovar var, t_frac disorder); +void arg_step(char *str, t_args *arg); + +#endif \ No newline at end of file diff --git a/output_disorder.c b/output_disorder.c new file mode 100644 index 0000000..6b30d2a --- /dev/null +++ b/output_disorder.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* output_disorder.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: clefrere +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/18 15:55:08 by clefrere #+# #+# */ +/* Updated: 2025/12/18 17:31:47 by clefrere ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pushswap.h" + +void output_disorder(t_frac *frac) +{ + size_t nbr; + size_t div; + bool print; + + div = 10000; + nbr = 0; + if (frac->den != 0) + nbr = frac->num * 10000 / frac->den; + print = false; + while (div) + { + if (div == 100 || nbr >= div) + print = true; + if (div == 10) + eoutput_char('.'); + if (print) + eoutput_char(((nbr / div) % 10) + '0'); + div /= 10; + } +} diff --git a/pushswap.h b/pushswap.h index d579485..e8dc26e 100644 --- a/pushswap.h +++ b/pushswap.h @@ -6,7 +6,7 @@ /* By: clefrere +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 11:02:44 by agilliar #+# #+# */ -/* Updated: 2025/12/18 16:04:35 by agilliar ### ########.fr */ +/* Updated: 2025/12/18 17:47:21 by clefrere ### ########.fr */ /* */ /* ************************************************************************** */ @@ -194,4 +194,6 @@ void *ft_memcpy(void *restrict dest, const void *src, size_t n); bool get_next_op(t_op *res); +void output_disorder(t_frac *frac); + #endif diff --git a/pushswap_args.c b/pushswap_args.c new file mode 100644 index 0000000..1be7a6e --- /dev/null +++ b/pushswap_args.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pushswap_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: clefrere +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/18 17:36:59 by clefrere #+# #+# */ +/* Updated: 2025/12/18 17:41:18 by clefrere ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "main_pushswap.h" + +static bool ft_streq(char *str1, char *str2) +{ + size_t i; + + i = 0; + while (str1[i] && str2[i] && str1[i] == str2[i]) + i++; + return (str1[i] == '\0' && str2[i] == '\0'); +} + +t_algorithm *algorithm_get(t_algovar var, t_frac disorder) +{ + if (var == ALGORITHM_SIMPLE) + return (&algorithm_simple); + if (var == ALGORITHM_MEDIUM) + return (&algorithm_medium); + if (var == ALGORITHM_COMPLEX) + return (&algorithm_complex); + if (frac_lt(disorder, frac_new(1, 5))) + return (&algorithm_simple); + if (frac_lt(disorder, frac_new(1, 2))) + return (&algorithm_medium); + return (&algorithm_complex); +} + +const char *complexity_get(t_algovar var, t_frac disorder) +{ + if (var == ALGORITHM_SIMPLE) + return (COMPLEXITY_SIMPLE); + if (var == ALGORITHM_MEDIUM) + return (COMPLEXITY_MEDIUM); + if (var == ALGORITHM_COMPLEX) + return (COMPLEXITY_COMPLEX); + if (frac_lt(disorder, frac_new(1, 5))) + return (COMPLEXITY_ADAPTIVE_LOW); + if (frac_lt(disorder, frac_new(1, 2))) + return (COMPLEXITY_ADAPTIVE_MEDIUM); + return (COMPLEXITY_ADAPTIVE_HIGH); +} + +void arg_step(char *str, t_args *arg) +{ + if (ft_streq(str, "--simple")) + arg->algo = ALGORITHM_SIMPLE; + else if (ft_streq(str, "--medium")) + arg->algo = ALGORITHM_MEDIUM; + else if (ft_streq(str, "--complex")) + arg->algo = ALGORITHM_COMPLEX; + else if (ft_streq(str, "--adaptive")) + arg->algo = ALGORITHM_ADAPTIVE; + else if (ft_streq(str, "--bench")) + arg->bench = true; + else + stack_push_back(&arg->state.a, psval_parse(str)); +} -- 2.51.0