From 38eae5ab1600b47356405b3c0c33fb1b3ed2051b Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Thu, 8 Jan 2026 15:28:07 +0000 Subject: [PATCH 1/2] Bench algo working --- Makefile | 4 +- flags/bench.c | 114 +++++++++++++++++++++++++++++++++++++++++++ ft_putnbr.c | 24 +++++++++ headers/parsing.h | 4 +- headers/push_swap.h | 5 +- parsing/ft_isdigit.c | 18 +++++++ parsing/ft_itoa.c | 62 +++++++++++++++++++++++ secure_write.c | 20 ++++++++ 8 files changed, 247 insertions(+), 4 deletions(-) create mode 100644 flags/bench.c create mode 100644 ft_putnbr.c create mode 100644 parsing/ft_isdigit.c create mode 100644 parsing/ft_itoa.c create mode 100644 secure_write.c diff --git a/Makefile b/Makefile index 443234b..9c2e1f7 100644 --- a/Makefile +++ b/Makefile @@ -24,13 +24,13 @@ INCLUDES = headers # ALL FILES WITHOUT PATH #============================ -SRC = main.c test_one.c +SRC = main.c test_one.c ft_putnbr.c secure_write.c INSERTION = insertion.c FLAGS_FILES = algorithms_sort.c flag.c -PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c checker.c +PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c checker.c ft_itoa.c ft_isdigit.c STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c diff --git a/flags/bench.c b/flags/bench.c new file mode 100644 index 0000000..eda181c --- /dev/null +++ b/flags/bench.c @@ -0,0 +1,114 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bench.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/08 13:59:52 by dgaillet #+# #+# */ +/* Updated: 2026/01/08 15:26:30 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swaps.h" +#include +#include + +static void print_disorder(t_stacks *stacks) +{ + int nbr; + char *str; + + nbr = (int) stacks->disorder * 10000; + str = ft_itoa(nbr); + if (!str) + exit ( EXIT_FAILURE ); + secure_write(2, "[bench] disorder: ", 18); + if (ft_strlen(str) == 2) + secure_write(2, "0", 1); + else + secure_write(2, str, ft_strlen(str) - 2); + secure_write(2, ".", 1); + secure_write(2, &str[ft_strlen(str) - 2], 2); + secure_write(2, "%\n", 2); + free(str); +} + +static void print_strategy(t_stacks *stacks) +{ + secure_write(2, "[bench] strategy: ", 18); + if (stacks->strategy == 0) + { + secure_write(2, "Adaptative", 10); + if (stacks->disorder < 0.2) + secure_write(2, " / O(n2n)\n", 10); + else if (stacks->disorder >= 0.5) + secure_write(2, " / O(n√n)\n", 10); + else + secure_write(2, " / O(nlogn)\n", 12); + } + else if (stacks->strategy == 1) + secure_write(2, "Simple / O(n2n)\n", 16); + else if (stacks->strategy == 2) + secure_write(2, "Medium / O(nlogn)\n", 18); + else if (stacks->strategy == 3) + secure_write(2, "Complex / O(n√n)\n", 17); +} + +static void print_total_ops(t_stacks *stacks) +{ + unsigned int total_ops; + + total_ops = 0; + total_ops += stacks->sa; + total_ops += stacks->sb; + total_ops += stacks->ss; + total_ops += stacks->pa; + total_ops += stacks->pb; + total_ops += stacks->ra; + total_ops += stacks->rb; + total_ops += stacks->rr; + total_ops += stacks->rra; + total_ops += stacks->rrb; + total_ops += stacks->rrr; + secure_write(2, "[bench] total_ops: ", 18); + ft_putnbr_fd((int) total_ops, 2); + secure_write(2, "\n"); +} + +static void print_ops(t_stacks *stacks) +{ + secure_write(2, "[bench] sa: ", 11); + ft_putnbr_fd((int) stacks->sa, 2); + secure_write(2, " sb: ", 5); + ft_putnbr_fd((int) stacks->sb, 2); + secure_write(2, " ss: ", 5); + ft_putnbr_fd((int) stacks->ss, 2); + secure_write(2, " pa: ", 5); + ft_putnbr_fd((int) stacks->pa, 2); + secure_write(2, " pb: ", 5); + ft_putnbr_fd((int) stacks->pb, 2); + secure_write(2, "\n", 1); + secure_write(2, "[bench] ra: ", 5); + ft_putnbr_fd((int) stacks->ra, 2); + secure_write(2, " rb: ", 5); + ft_putnbr_fd((int) stacks->rb, 2); + secure_write(2, " rr: ", 5); + ft_putnbr_fd((int) stacks->rr, 2); + secure_write(2, " rra: ", 6); + ft_putnbr_fd((int) stacks->rra, 2); + secure_write(2, " rrb: ", 6); + ft_putnbr_fd((int) stacks->rrb, 2); + secure_write(2, " rrr: ", 6); + ft_putnbr_fd((int) stacks->rrr, 2); + secure_write(2, "\n", 1); +} + +void bench(t_stacks *stacks) +{ + print_disorder(stacks) + print_strategy(stacks); + print_total_ops(stacks); + print_ops(stacks); + +} diff --git a/ft_putnbr.c b/ft_putnbr.c new file mode 100644 index 0000000..db933ec --- /dev/null +++ b/ft_putnbr.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/08 15:17:28 by dgaillet #+# #+# */ +/* Updated: 2026/01/08 15:19:30 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void ft_putnbr_fd(int nbr, int fd) +{ + char c; + + if (!nbr) + return ; + ft_putnbr_fd(nbr / 10, fd); + c = (nbr % 10) + '0'; + secure_write(fd, &c, 1); +} diff --git a/headers/parsing.h b/headers/parsing.h index 07a8326..c9b3a13 100644 --- a/headers/parsing.h +++ b/headers/parsing.h @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/07 08:03:08 by mteriier #+# #+# */ -/* Updated: 2026/01/08 12:56:19 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/08 14:15:37 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -25,5 +25,7 @@ char **ft_split(char const *s, char c); void free_tab(char **tab); int checker(int argc, char **argv); int len_split(char **tab); +char *ft_itoa(int n); +int ft_isdigit(int c); #endif diff --git a/headers/push_swap.h b/headers/push_swap.h index 43917e8..64221ec 100644 --- a/headers/push_swap.h +++ b/headers/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2026/01/08 14:01:04 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/08 15:20:03 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -83,4 +83,7 @@ int test1(int argc, char **argv); /* RADIX */ void radix(t_stacks *stacks); +void secure_write(int fd, char *str, int len); +void ft_putnbr_fd(int nbr, int fd); + #endif diff --git a/parsing/ft_isdigit.c b/parsing/ft_isdigit.c new file mode 100644 index 0000000..bae5f1c --- /dev/null +++ b/parsing/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/08 14:13:54 by dgaillet #+# #+# */ +/* Updated: 2026/01/08 14:13:59 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (1); + return (0); +} diff --git a/parsing/ft_itoa.c b/parsing/ft_itoa.c new file mode 100644 index 0000000..057aba4 --- /dev/null +++ b/parsing/ft_itoa.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/08 14:13:00 by dgaillet #+# #+# */ +/* Updated: 2026/01/08 14:13:27 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static size_t count_digits(int n) +{ + size_t size; + + size = 0; + if (n <= 0) + size++; + while (n) + { + n /= 10; + size++; + } + return (size); +} + +static void insert_char(char *str, unsigned int nbr, size_t index) +{ + while (nbr) + { + str[index--] = nbr % 10 + '0'; + nbr /= 10; + } +} + +char *ft_itoa(int n) +{ + unsigned int nbr; + char *str; + size_t size; + + nbr = n; + if (n < 0) + nbr = n * -1; + size = count_digits(n); + str = malloc(sizeof(char) * (size + 1)); + if (!str) + return (NULL); + str[size] = '\0'; + if (nbr == 0) + str[0] = '0'; + else + { + if (n < 0) + str[0] = '-'; + insert_char(str, nbr, size - 1); + } + return (str); +} diff --git a/secure_write.c b/secure_write.c new file mode 100644 index 0000000..97e8a77 --- /dev/null +++ b/secure_write.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* secure_write.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */ +/* Updated: 2026/01/08 14:32:19 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +void secure_write(int fd, char *str, int len) +{ + if (write(fd, str, len) < 0) + exit ( EXIT_FAILURE ); +} From 9a20072f366fdbfb9bf2fba49f15bde8abdf6538 Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Thu, 8 Jan 2026 16:09:37 +0000 Subject: [PATCH 2/2] Add flags to makefile and fix some compile error --- Makefile | 14 +++++++++----- flags/algorithms_sort.c | 7 +++++-- flags/bench.c | 26 +++++++++++++------------- flags/flag.c | 3 ++- headers/flags.h | 4 +++- headers/push_swap.h | 2 +- main.c | 3 +-- test_one.c | 4 +++- 8 files changed, 37 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 9c2e1f7..97037e6 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ SRC = main.c test_one.c ft_putnbr.c secure_write.c INSERTION = insertion.c -FLAGS_FILES = algorithms_sort.c flag.c +FLAGS_FILES = algorithms_sort.c flag.c bench.c PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c checker.c ft_itoa.c ft_isdigit.c @@ -46,7 +46,8 @@ ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \ $(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \ - $(INSERT_DIR)/$(INSERTION) $(ALGO_DIR)/$(COMPLEX_DIR)/$(COMPLEX_ALGO) + $(INSERT_DIR)/$(INSERTION) $(ALGO_DIR)/$(COMPLEX_DIR)/$(COMPLEX_ALGO) \ + $(FLAGS_DIR)/$(FLAGS_FILES) OBJ_DIR = obj @@ -69,9 +70,6 @@ $(NAME): $(OBJ) @echo "======= PUSH SWAP COMPILED =========" @echo "====================================" -$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR) - $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ - $(OBJ_DIR)/%.o: $(PARS_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ @@ -93,6 +91,12 @@ $(OBJ_DIR)/%.o: $(ALGO_DIR)/$(COMPLEX_DIR)/%.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(ALGO_UTILS_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ +$(OBJ_DIR)/%.o: $(FLAGS_DIR)/%.c | $(OBJ_DIR) + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + +$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR) + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + $(OBJ_DIR): @mkdir -p $(OBJ_DIR) diff --git a/flags/algorithms_sort.c b/flags/algorithms_sort.c index a0f9b28..ab9c266 100644 --- a/flags/algorithms_sort.c +++ b/flags/algorithms_sort.c @@ -6,12 +6,12 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/07 12:15:02 by mteriier #+# #+# */ -/* Updated: 2026/01/07 12:15:05 by mteriier ### ########lyon.fr */ +/* Updated: 2026/01/08 16:07:00 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -#include "medium_algo.h" +#include "medium_headers.h" void simple(t_stacks *piles) { @@ -33,10 +33,13 @@ void medium(t_stacks *piles) void complex(t_stacks *piles) { + (void)piles; return ; } void adaptive(t_stacks *piles) { + (void)piles; return ; } + diff --git a/flags/bench.c b/flags/bench.c index eda181c..662a923 100644 --- a/flags/bench.c +++ b/flags/bench.c @@ -6,11 +6,12 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/08 13:59:52 by dgaillet #+# #+# */ -/* Updated: 2026/01/08 15:26:30 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/08 16:09:11 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ -#include "push_swaps.h" +#include "push_swap.h" +#include "parsing.h" #include #include @@ -34,10 +35,10 @@ static void print_disorder(t_stacks *stacks) free(str); } -static void print_strategy(t_stacks *stacks) +static void print_algo(t_stacks *stacks) { - secure_write(2, "[bench] strategy: ", 18); - if (stacks->strategy == 0) + secure_write(2, "[bench] algo: ", 18); + if (stacks->algo == 0) { secure_write(2, "Adaptative", 10); if (stacks->disorder < 0.2) @@ -47,11 +48,11 @@ static void print_strategy(t_stacks *stacks) else secure_write(2, " / O(nlogn)\n", 12); } - else if (stacks->strategy == 1) + else if (stacks->algo == 1) secure_write(2, "Simple / O(n2n)\n", 16); - else if (stacks->strategy == 2) + else if (stacks->algo == 2) secure_write(2, "Medium / O(nlogn)\n", 18); - else if (stacks->strategy == 3) + else if (stacks->algo == 3) secure_write(2, "Complex / O(n√n)\n", 17); } @@ -73,7 +74,7 @@ static void print_total_ops(t_stacks *stacks) total_ops += stacks->rrr; secure_write(2, "[bench] total_ops: ", 18); ft_putnbr_fd((int) total_ops, 2); - secure_write(2, "\n"); + secure_write(2, "\n", 1); } static void print_ops(t_stacks *stacks) @@ -104,11 +105,10 @@ static void print_ops(t_stacks *stacks) secure_write(2, "\n", 1); } -void bench(t_stacks *stacks) +void print_bench(t_stacks *stacks) { - print_disorder(stacks) - print_strategy(stacks); + print_disorder(stacks); + print_algo(stacks); print_total_ops(stacks); print_ops(stacks); - } diff --git a/flags/flag.c b/flags/flag.c index 31e0811..d097a9a 100644 --- a/flags/flag.c +++ b/flags/flag.c @@ -6,12 +6,13 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/07 12:39:29 by mteriier #+# #+# */ -/* Updated: 2026/01/07 12:39:31 by mteriier ### ########lyon.fr */ +/* Updated: 2026/01/08 16:07:29 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" #include "parsing.h" +#include "flags.h" void flags(int pos, char **argv, t_stacks *piles) { diff --git a/headers/flags.h b/headers/flags.h index 90130e2..0e1915d 100644 --- a/headers/flags.h +++ b/headers/flags.h @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/07 13:05:52 by mteriier #+# #+# */ -/* Updated: 2026/01/07 13:05:53 by mteriier ### ########lyon.fr */ +/* Updated: 2026/01/08 15:32:54 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -19,4 +19,6 @@ void complex(t_stacks *piles); void adaptive(t_stacks *piles); void flags(int pos, char **argv, t_stacks *piles); +void print_bench(t_stacks *stacks); + #endif diff --git a/headers/push_swap.h b/headers/push_swap.h index 64221ec..e83c07b 100644 --- a/headers/push_swap.h +++ b/headers/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2026/01/08 15:20:03 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/08 15:32:04 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ diff --git a/main.c b/main.c index cdd0ad0..6750253 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 18:32:35 by mteriier #+# #+# */ -/* Updated: 2026/01/08 12:57:04 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/08 15:34:59 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,6 @@ int main(int argc, char **argv) write(2, "Error !\n", 8); return (1); } - if (argc > 1) test1(argc, argv); return (0); diff --git a/test_one.c b/test_one.c index 1ed6bf7..160ba63 100644 --- a/test_one.c +++ b/test_one.c @@ -6,11 +6,12 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */ -/* Updated: 2026/01/08 12:57:57 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/08 15:35:43 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" +#include "flags.h" #include "parsing.h" #include "medium_headers.h" #include @@ -25,6 +26,7 @@ int test1(int argc, char **argv) if (argc > 1) { piles = init_piles(argc, argv, 0); + print_bench(piles); preset = get_tabs(piles->a, range_bucket(piles->a)); bucket_algo(piles, preset, range_bucket(piles->a)); }