mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
Bench algo working
This commit is contained in:
4
Makefile
4
Makefile
@@ -24,13 +24,13 @@ INCLUDES = headers
|
|||||||
# ALL FILES WITHOUT PATH
|
# 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
|
INSERTION = insertion.c
|
||||||
|
|
||||||
FLAGS_FILES = algorithms_sort.c flag.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
|
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c
|
||||||
|
|
||||||
|
|||||||
114
flags/bench.c
Normal file
114
flags/bench.c
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* bench.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
24
ft_putnbr.c
Normal file
24
ft_putnbr.c
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_putnbr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/01/07 08:03:08 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);
|
void free_tab(char **tab);
|
||||||
int checker(int argc, char **argv);
|
int checker(int argc, char **argv);
|
||||||
int len_split(char **tab);
|
int len_split(char **tab);
|
||||||
|
char *ft_itoa(int n);
|
||||||
|
int ft_isdigit(int c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/12/08 14:18:06 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 */
|
/* RADIX */
|
||||||
void radix(t_stacks *stacks);
|
void radix(t_stacks *stacks);
|
||||||
|
|
||||||
|
void secure_write(int fd, char *str, int len);
|
||||||
|
void ft_putnbr_fd(int nbr, int fd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
18
parsing/ft_isdigit.c
Normal file
18
parsing/ft_isdigit.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_isdigit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
62
parsing/ft_itoa.c
Normal file
62
parsing/ft_itoa.c
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_itoa.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 14:13:00 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 14:13:27 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
20
secure_write.c
Normal file
20
secure_write.c
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* secure_write.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */
|
||||||
|
/* Updated: 2026/01/08 14:32:19 by dgaillet ### ########lyon.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void secure_write(int fd, char *str, int len)
|
||||||
|
{
|
||||||
|
if (write(fd, str, len) < 0)
|
||||||
|
exit ( EXIT_FAILURE );
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user