From 9c97f7d5a8f3b87a4c8741e1b64385b84527d6e7 Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 16 Dec 2025 19:30:14 +0100 Subject: [PATCH] Added counters to main --- Makefile | 2 +- main_pushswap.c | 35 ++++++++++++++++++++++++++++------ op_output.c | 9 +++++++-- op_count.c => ops_count.c | 8 ++++---- ops_groups.c => ops_stackops.c | 21 +++++++++++++------- pushswap.h | 23 ++++------------------ 6 files changed, 59 insertions(+), 39 deletions(-) rename op_count.c => ops_count.c (85%) rename ops_groups.c => ops_stackops.c (72%) diff --git a/Makefile b/Makefile index 4de4d74..f743838 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME=push_swap -SRCS=algorithm_complex.c algorithm_leafsort.c algorithm_simple.c cheatalloc.c clist.c closure.c leaf_sort.c leaf_sort_index.c leaf_sort_parse.c list.c main_pushswap.c op_count.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 splitsort.c splitsort_final.c splitsort_iter.c splitsort_part.c stack.c stacks.c stacks_get.c +SRCS=algorithm_complex.c algorithm_leafsort.c algorithm_simple.c cheatalloc.c clist.c closure.c leaf_sort.c leaf_sort_index.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_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 RESSRCS=_res_leaf_sort_lookup.h diff --git a/main_pushswap.c b/main_pushswap.c index b9c59bd..a64c52c 100644 --- a/main_pushswap.c +++ b/main_pushswap.c @@ -6,7 +6,7 @@ /* By: clefrere +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 22:15:12 by agilliar #+# #+# */ -/* Updated: 2025/12/16 14:55:34 by agilliar ### ########.fr */ +/* Updated: 2025/12/16 19:13:54 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,11 +40,34 @@ static void arg_step(char *str, t_args *arg) stack_push_back(&arg->state.a, psval_parse(str)); } -int main(int argc, char **argv) +typedef struct s_benchres +{ + t_ops_count pre_opt; + t_ops_count post_opt; + t_slice strategy; + float disorder; +} t_benchres; + +static t_benchres pushswap_solve(t_args *args) { - t_args args; t_ops ops; t_optimizer opt; + t_benchres res; + + 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); + ops_optimizer_commit(&opt, &args->state); + return (res); +} + +#include +int main(int argc, char **argv) +{ + t_args args; + t_benchres res; int i; args.state = stacks_new(); @@ -53,9 +76,9 @@ int main(int argc, char **argv) i = 1; while (i < argc) arg_step(argv[i++], &args); - ops = ops_compose(ops_stackops(), ops_optimizer(ops_output(), &opt)); - stacks_apply(&args.state, &ops, args.algo); - ops_optimizer_commit(&opt, &args.state); + res = pushswap_solve(&args); output_flush(); + printf("pre: %lu\n", res.pre_opt.counters[OP_RR]); + printf("post: %lu\n", res.post_opt.counters[OP_RR]); cheatexit(!stacks_is_solved(&args.state)); } diff --git a/op_output.c b/op_output.c index 30569e4..5622c8b 100644 --- a/op_output.c +++ b/op_output.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/03 12:28:28 by agilliar #+# #+# */ -/* Updated: 2025/12/16 12:48:13 by agilliar ### ########.fr */ +/* Updated: 2025/12/16 19:07:40 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ static void op_output_inner(const char *data, t_stack *stack) output_str_ln(data); } -t_closure op_output(t_op op) +static t_closure op_output(t_op op) { t_closure res; const char *const ops[11] = { @@ -39,3 +39,8 @@ t_closure op_output(t_op op) res.func = &op_output_inner; return (res); } + +t_ops ops_output(void) +{ + return (ops_init(op_output)); +} diff --git a/op_count.c b/ops_count.c similarity index 85% rename from op_count.c rename to ops_count.c index cd2337e..973a96b 100644 --- a/op_count.c +++ b/ops_count.c @@ -1,16 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* op_count.c :+: :+: :+: */ +/* ops_count.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: clefrere +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/16 12:28:05 by clefrere #+# #+# */ -/* Updated: 2025/12/16 15:40:32 by clefrere ### ########.fr */ +/* Updated: 2025/12/16 18:10:40 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ -#include "leaf_sort.h" +#include "pushswap.h" static void op_count_inner(size_t *data, t_stacks *stack) { @@ -18,7 +18,7 @@ static void op_count_inner(size_t *data, t_stacks *stack) data[0]++; } -t_closure op_count(size_t *dst) +static t_closure op_count(size_t *dst) { t_closure res; diff --git a/ops_groups.c b/ops_stackops.c similarity index 72% rename from ops_groups.c rename to ops_stackops.c index 63b7f06..589a2fa 100644 --- a/ops_groups.c +++ b/ops_stackops.c @@ -1,17 +1,29 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ops_groups.c :+: :+: :+: */ +/* ops_stackops.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 17:02:23 by agilliar #+# #+# */ -/* Updated: 2025/12/08 17:56:36 by agilliar ### ########.fr */ +/* Updated: 2025/12/16 19:08:41 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ #include "pushswap.h" +t_closure op_pa(void); +t_closure op_pb(void); +t_closure op_ra(void); +t_closure op_rb(void); +t_closure op_rr(void); +t_closure op_rra(void); +t_closure op_rrb(void); +t_closure op_rrr(void); +t_closure op_sa(void); +t_closure op_sb(void); +t_closure op_ss(void); + t_ops ops_stackops(void) { const t_ops ops = { @@ -32,8 +44,3 @@ t_ops ops_stackops(void) return (ops); } - -t_ops ops_output(void) -{ - return (ops_init(op_output)); -} diff --git a/pushswap.h b/pushswap.h index 5677b7e..e9a077a 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/16 15:40:10 by clefrere ### ########.fr */ +/* Updated: 2025/12/16 19:08:25 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,7 @@ typedef struct s_ops t_closure ops[11]; } t_ops; -typedef struct s__ops_count +typedef struct s_ops_count { size_t counters[11]; } t_ops_count; @@ -140,23 +140,6 @@ const t_stack *stacks_getb(const t_stacks *stacks, bool mirrored); t_op op_parse(t_slice s); -t_closure op_pa(void); -t_closure op_pb(void); -t_closure op_ra(void); -t_closure op_rb(void); -t_closure op_rr(void); -t_closure op_rra(void); -t_closure op_rrb(void); -t_closure op_rrr(void); -t_closure op_sa(void); -t_closure op_sb(void); -t_closure op_ss(void); - -t_closure op_output(t_op op); - -t_closure op_count(size_t *dst); -t_ops ops_count(t_ops_count *counter); - t_closure op_compose(t_closure first, t_closure second); t_ops ops_compose(t_ops lhs, t_ops rhs); t_ops ops_init(t_closure (*gen)(t_op)); @@ -164,6 +147,8 @@ t_ops ops_init(t_closure (*gen)(t_op)); t_ops ops_stackops(void); t_ops ops_output(void); +t_ops ops_count(t_ops_count *counter); + t_ops ops_optimizer(t_ops commit, t_optimizer *data); void ops_optimizer_commit(t_optimizer *data, t_stacks *stacks); -- 2.51.0