This commit is contained in:
Camille Chauvet 2022-12-06 17:35:45 +01:00
parent c4efdc8ff1
commit 3fc423de4a
33 changed files with 103 additions and 206 deletions

View File

@ -6,11 +6,11 @@
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ # # By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/11/28 16:34:37 by cchauvet #+# #+# # # Created: 2022/11/28 16:34:37 by cchauvet #+# #+# #
# Updated: 2022/12/01 16:54:34 by cchauvet ### ########.fr # # Updated: 2022/12/06 13:32:50 by cchauvet ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
SRCS = ft_swap.c ft_putstr.c ft_p.c ft_sort.c ft_tablen.c main.c ft_atoi.c ft_radix.c ft_isnum.c ft_r.c ft_is_sorted.c ft_get_max.c SRCS = ft_swap.c ft_putstr.c ft_p.c ft_sort.c ft_tablen.c main.c ft_atoi.c ft_radix.c ft_isnum.c ft_r.c ft_is_sorted.c ft_get_max.c ft_bitlen.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

View File

@ -1 +0,0 @@
./push_swap $@ | ./checker_linux $@

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 18:27:58 by cchauvet #+# #+# */ /* Created: 2022/11/08 18:27:58 by cchauvet #+# #+# */
/* Updated: 2022/11/09 17:13:11 by cchauvet ### ########.fr */ /* Updated: 2022/12/02 21:39:08 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

BIN
ft_atoi.o

Binary file not shown.

View File

@ -1,29 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bit_finder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/23 17:31:10 by cchauvet #+# #+# */
/* Updated: 2022/11/30 20:07:51 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
int ft_bit_finder_left(int big, int tofind)
{
int i;
i = (int) sizeof(int) * 8 - 1;
while (i != 0 && ((big >> i) & 1) != tofind)
i--;
return (sizeof(int) * 8 - i - 1);
}
int ft_bit_finder_right(int big, int tofind)
{
return ((int) sizeof(int) * 8 - 1 - ft_bit_finder_left(big, tofind));
}

Binary file not shown.

View File

@ -1,31 +1,29 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_get_bit_max.c :+: :+: :+: */ /* ft_bitlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 16:22:14 by cchauvet #+# #+# */ /* Created: 2022/12/06 13:25:38 by cchauvet #+# #+# */
/* Updated: 2022/11/29 15:10:54 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 13:28:14 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
int ft_get_bit_max(int *tab) unsigned int ft_bitlen(unsigned int nb)
{ {
int i; unsigned int i;
int max; unsigned int y;
if (tab[0] == -1) i = 0;
return (INT_MIN); y = 0;
max = tab[0]; while (i < sizeof(unsigned int) * 8)
i = 1;
while (tab[i] != -1)
{ {
if (ft_bit_finder_left(tab[i], 1) > ft_bit_finder_left(max, 1)) if (nb >> i & 1)
max = tab[i]; y = i;
i++; i++;
} }
return (max); return (y);
} }

View File

@ -1,31 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_bit_min.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 16:22:14 by cchauvet #+# #+# */
/* Updated: 2022/11/29 15:10:03 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
int ft_get_bit_min(int *tab)
{
int i;
int min;
if (tab[0] == -1)
return (INT_MIN);
min = tab[0];
i = 1;
while (tab[i] != -1)
{
if (ft_bit_finder_right(tab[i], 1) < ft_bit_finder_right(min, 1))
min = tab[i];
i++;
}
return (min);
}

View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 16:31:57 by cchauvet #+# #+# */ /* Created: 2022/11/14 16:31:57 by cchauvet #+# #+# */
/* Updated: 2022/11/28 18:07:53 by cchauvet ### ########.fr */ /* Updated: 2022/12/02 20:33:17 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
int ft_get_max_index(int *tab, int size) int ft_get_max_index(long int *tab, int size)
{ {
int max; int max;
int i; int i;
@ -28,7 +28,17 @@ int ft_get_max_index(int *tab, int size)
return (max); return (max);
} }
int ft_get_max(int *tab, int size) int ft_get_max(unsigned int *tab)
{ {
return (tab[ft_get_max_index(tab, size)]); unsigned int max;
max = *tab;
tab++;
while (*tab != STOP_VALUE)
{
if (*tab > max)
max = *tab;
tab++;
}
return (max);
} }

Binary file not shown.

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/29 14:41:08 by cchauvet #+# #+# */ /* Created: 2022/11/29 14:41:08 by cchauvet #+# #+# */
/* Updated: 2022/12/01 17:03:47 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 13:30:52 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,10 +16,10 @@ int ft_is_sorted(unsigned int *tab)
{ {
int i; int i;
i = 0; i = 1;
while (tab[i] != STOP_VALUE) while (tab[i] != STOP_VALUE)
{ {
if (tab[1] != 0) if (tab[i] != tab[i - 1] + 1)
return (0); return (0);
i++; i++;
} }

Binary file not shown.

Binary file not shown.

4
ft_p.c
View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 16:38:34 by cchauvet #+# #+# */ /* Created: 2022/11/09 16:38:34 by cchauvet #+# #+# */
/* Updated: 2022/12/01 16:17:17 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 17:22:35 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
void ft_p(unsigned *tab_src, unsigned *tab_dst) void ft_p(unsigned int *tab_src, unsigned int *tab_dst)
{ {
int size_src; int size_src;
int size_dst; int size_dst;

BIN
ft_p.o

Binary file not shown.

Binary file not shown.

10
ft_r.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 16:50:03 by cchauvet #+# #+# */ /* Created: 2022/11/28 16:50:03 by cchauvet #+# #+# */
/* Updated: 2022/12/01 17:10:17 by cchauvet ### ########.fr */ /* Updated: 2022/12/01 17:42:30 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,11 +27,5 @@ static void ft_r(unsigned int *tab)
void ft_ra(unsigned int *tab) void ft_ra(unsigned int *tab)
{ {
ft_r(tab); ft_r(tab);
ft_putstr("rra\n"); ft_putstr("ra\n");
}
void ft_rb(unsigned int *tab)
{
ft_r(tab);
ft_putstr("rb\n");
} }

BIN
ft_r.o

Binary file not shown.

View File

@ -6,38 +6,31 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */ /* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */
/* Updated: 2022/12/01 17:13:35 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 17:22:52 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
void ft_radix_sort(unsigned *tab_a, unsigned *tab_b) void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b)
{ {
int tab_index; unsigned int i;
int loop_index; unsigned int y;
while (!ft_is_sorted(tab_a)) i = 0;
while (!ft_is_sorted(tab_a) && i < sizeof(unsigned int) * 8)
{ {
tab_index = ft_tablen(tab_a) - 1; y = ft_tablen(tab_a);
loop_index = tab_index; while (0 < y && (!ft_is_sorted(tab_a) && !ft_tablen(tab_b)))
while (loop_index >= 0 && !ft_is_sorted(tab_a))
{
if (tab_a[tab_index] % 2 == 1)
{ {
if (((tab_a[ft_tablen(tab_a) - 1] >> i) & 1) == 1)
ft_pb(tab_a, tab_b); ft_pb(tab_a, tab_b);
tab_index--;
}
else else
ft_ra(tab_a); ft_ra(tab_a);
loop_index--; y--;
} }
while (tab_b[0] != STOP_VALUE) while (tab_b[0] != STOP_VALUE)
ft_pa(tab_a, tab_b); ft_pa(tab_a, tab_b);
tab_index = ft_tablen(tab_a); i++;
while (--tab_index >= 0 && !ft_is_sorted(tab_a))
tab_a[tab_index] = tab_a[tab_index] >> 1;
} }
free(tab_a);
free(tab_b);
} }

Binary file not shown.

View File

@ -6,19 +6,19 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */ /* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
/* Updated: 2022/12/01 17:00:04 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 17:27:05 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
static unsigned int *ft_values_simplifier(int *tab_in, int size) static t_tab ft_values_simplifier(long int *tab_in, int size)
{ {
int i; int i;
unsigned int *out; unsigned int *out;
int max_index; int max_index;
out = malloc(sizeof(int) * (size + 1)); out = malloc(sizeof(unsigned int) * (size + 1));
if (out == NULL) if (out == NULL)
{ {
free(tab_in); free(tab_in);
@ -28,8 +28,8 @@ static unsigned int *ft_values_simplifier(int *tab_in, int size)
while (i < size) while (i < size)
{ {
max_index = ft_get_max_index(tab_in, size); max_index = ft_get_max_index(tab_in, size);
out[max_index] = (size - i); out[max_index] = size - i - 1;
tab_in[max_index] = INT_MIN; tab_in[max_index] = LONG_MIN;
i++; i++;
} }
out[size] = STOP_VALUE; out[size] = STOP_VALUE;
@ -37,9 +37,9 @@ static unsigned int *ft_values_simplifier(int *tab_in, int size)
return (out); return (out);
} }
static unsigned int *ft_tab_init(int *tab_in, int size, unsigned **tab_a, unsigned **tab_b) unsigned int *ft_tab_init(long *tab_in, int size, t_tab *tab_a, t_tab*tab_b)
{ {
*tab_b = malloc(sizeof(int) * (size + 1)); *tab_b = malloc(sizeof(unsigned int) * (size + 1));
*tab_a = ft_values_simplifier(tab_in, size); *tab_a = ft_values_simplifier(tab_in, size);
if (tab_a == NULL || tab_b == NULL) if (tab_a == NULL || tab_b == NULL)
{ {
@ -52,7 +52,7 @@ static unsigned int *ft_tab_init(int *tab_in, int size, unsigned **tab_a, unsign
return (*tab_a); return (*tab_a);
} }
void ft_sort(int *tab_in, int size_a) void ft_sort(long int *tab_in, int size_a)
{ {
unsigned int *tab_a; unsigned int *tab_a;
unsigned int *tab_b; unsigned int *tab_b;
@ -62,4 +62,6 @@ void ft_sort(int *tab_in, int size_a)
if (ft_tab_init(tab_in, size_a, &tab_a, &tab_b) == NULL) if (ft_tab_init(tab_in, size_a, &tab_a, &tab_b) == NULL)
return ; return ;
ft_radix_sort(tab_a, tab_b); ft_radix_sort(tab_a, tab_b);
free(tab_a);
free(tab_b);
} }

BIN
ft_sort.o

Binary file not shown.

View File

@ -6,11 +6,11 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */ /* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
/* Updated: 2022/10/28 19:23:45 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 17:28:21 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "pushswap.h"
static void *ft_cancel(char **tab, size_t len) static void *ft_cancel(char **tab, size_t len)
{ {
@ -29,7 +29,29 @@ static void *ft_cancel(char **tab, size_t len)
return (NULL); return (NULL);
} }
static size_t ft_seglen(const char *s, char c) char *ft_substr(char const *s, unsigned int start, size_t len)
{
ssize_t size;
char *ptr;
if (s == NULL)
return (NULL);
size = ft_strlen(s);
size -= start;
if (size < 0)
size = 0;
if ((size_t)size > len)
size = len;
ptr = malloc((size + 1) * sizeof(char));
if (ptr == NULL)
return (NULL);
ptr[size] = '\0';
while (size-- > 0)
ptr[size] = s[start + size];
return (ptr);
}
size_t ft_seglen(const char *s, char c)
{ {
size_t len; size_t len;

View File

@ -1,34 +1,23 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* ft_get_min.c :+: :+: :+: */ /* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/15 13:20:22 by cchauvet #+# #+# */ /* Created: 2022/12/06 17:28:28 by cchauvet #+# #+# */
/* Updated: 2022/11/28 16:46:47 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 17:30:36 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pushswap.h" #include "pushswap.h"
int get_min_index(int *tab, int size) size_t ft_strlen(const char *s)
{ {
int i; int i;
int min;
min = 0; i = 0;
i = 1; while (s[i] != '\0')
while (i < size)
{
if (tab[min] > tab[i])
min = i;
i++; i++;
} return (i);
return (min);
}
int get_min(int *tab, int size)
{
return (tab[get_min_index(tab, size)]);
} }

BIN
ft_swap.o

Binary file not shown.

Binary file not shown.

6
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */ /* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */
/* Updated: 2022/11/30 14:13:09 by cchauvet ### ########.fr */ /* Updated: 2022/12/04 19:44:25 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,9 +15,9 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int i; int i;
int *tab_a; long int *tab_a;
tab_a = malloc(argc * sizeof(int)); tab_a = malloc(argc * sizeof(long int));
if (tab_a == NULL) if (tab_a == NULL)
return (1); return (1);
i = 1; i = 1;

BIN
main.o

Binary file not shown.

BIN
push_swap

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */ /* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */
/* Updated: 2022/12/01 16:51:27 by cchauvet ### ########.fr */ /* Updated: 2022/12/06 17:30:20 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,18 +18,19 @@
# define STOP_VALUE 4294967295 # define STOP_VALUE 4294967295
int ft_isnum(char *str); int ft_isnum(char *str);
size_t ft_strlen(const char *s);
int ft_atoi(char *str); int ft_atoi(char *str);
void ft_putstr(char *str); void ft_putstr(char *str);
char **ft_split(const char *s, char c); char **ft_split(const char *s, char c);
void ft_sort(int *tab, int size); void ft_sort(long int *tab, int size);
int ft_get_max_index(int *tab, int len);
int ft_get_max_index(int *tab, int len);
int ft_get_min_index(int *tab, int len);
int ft_get_min_index(int *tab, int len);
int ft_get_max_index(long int *tab, int size);
int ft_get_max(unsigned int *tab);
int ft_tablen(unsigned int *tab); int ft_tablen(unsigned int *tab);
int ft_is_sorted(unsigned int *in); int ft_is_sorted(unsigned int *in);
unsigned int ft_bitlen(unsigned int nb);
void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b); void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b);
@ -39,4 +40,5 @@ void ft_pb(unsigned int *tab_a, unsigned int *tab_b);
void ft_ra(unsigned int *tab_a); void ft_ra(unsigned int *tab_a);
void ft_rb(unsigned int *tab_b); void ft_rb(unsigned int *tab_b);
typedef unsigned int * t_tab;
#endif #endif

52
test.c
View File

@ -1,52 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/23 16:43:32 by cchauvet #+# #+# */
/* Updated: 2022/11/29 17:11:41 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "pushswap.h"
int main()
{
int tab_a[5] = {0,1,2,3,-1};
int tab_b[5] = {-1};
int i;
i = 0;
printf("a: ");
while (tab_a[i] != -1)
{
printf("%d, ", tab_a[i]);
i++;
}
printf("\nb: ");
i = 0;
while (tab_b[i] != -1)
{
printf("%d, ", tab_b[i]);
i++;
}
printf("\n");
ft_pa(tab_a, tab_b);
i = 0;
printf("a: ");
while (tab_a[i] != -1)
{
printf("%d, ", tab_a[i]);
i++;
}
printf("\nb: ");
i = 0;
while (tab_b[i] != -1)
{
printf("%d, ", tab_b[i]);
i++;
}
}