only gets better B)

ditched GNL for ft_getline in checker
norm cleanup
This commit is contained in:
Tanguy MAZE 2019-03-17 17:29:32 +01:00
parent e7d88896e7
commit 558f8edfab
7 changed files with 355 additions and 18 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/02/23 14:30:57 by tmaze #+# #+# #
# Updated: 2019/03/16 17:21:14 by tmaze ### ########.fr #
# Updated: 2019/03/17 15:11:39 by tmaze ### ########.fr #
# #
#******************************************************************************#
@ -48,13 +48,13 @@ SRC = ps_clean.c \
ps_sorttools.c \
ps_specsort.c
MAIN1 = checker.c
MAIN2 = push_swap.c
MAIN1 = checker.c
MAIN2 = push_swap.c
OBJ = $(SRC:.c=.o)
OBJ1 = $(MAIN1:.c=.o)
OBJ2 = $(MAIN2:.c=.o)
OBJ1 = $(MAIN1:.c=.o)
OBJ2 = $(MAIN2:.c=.o)
LIB = ft
LIBFILE = libft.a
@ -63,7 +63,7 @@ LIBFILE = libft.a
OBJP = $(addprefix $(OBJDIR)/, $(SRC:.c=.o))
OBJP1 = $(addprefix $(OBJDIR)/, $(MAIN1:.c=.o))
OBJP2 = $(addprefix $(OBJDIR)/, $(MAIN2:.c=.o))
INCP = $(foreach dir, $(INCDIR), -I$(dir))
INCP = $(foreach dir, $(INCDIR), -I$(dir))
# Default Rule
DRULE = all
@ -78,13 +78,10 @@ all : $(NAME1) $(NAME2)
# Compilation rules
$(OBJDIR)/%.o : $(SRCDIR)/%.c $(OBJDIR) includes/push_swap.h
$(OBJDIR)/%.o : $(SRCDIR)/%.c includes/push_swap.h
@mkdir -p $(OBJDIR)
$(CC) $(FLAGS) -c -o $@ $< $(INCP)
$(OBJDIR) :
@echo -e "$(YEL)===> $(GRE)$(NAME2) : $(YEL) Directory Creation <===$(DEF)"
mkdir -p $(OBJDIR)
$(LIBDIR)/$(LIBFILE) :
@echo -e "$(YEL)===> $(GRE)$(NAME2) : $(YEL) Librairy Compilation <===$(DEF)"
$(MAKE) -C $(LIBDIR) all
@ -101,10 +98,12 @@ $(NAME2) : $(OBJP2) $(OBJP) $(LIBDIR)/$(LIBFILE)
clean :
@echo -e "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Object Files <===$(DEF)"
@$(RM) -rf $(OBJDIR)
@$(MAKE) -C $(LIBDIR) clean
fclean : clean
@echo -e "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Binary File <===$(DEF)"
@$(RM) -f $(NAME1) $(NAME2)
@$(MAKE) -C $(LIBDIR) fclean
re : fclean default

2
libft

@ -1 +1 @@
Subproject commit acf5431d1458dc07ea37302d134d7125f15aebf0
Subproject commit d5eecbed888ecaa348527f7bd18302ed2f9a276b

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
/* Updated: 2019/03/15 16:52:54 by tmaze ### ########.fr */
/* Updated: 2019/03/17 17:24:43 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -70,19 +70,25 @@ int read_ops(t_psdata *data)
{
t_list *nop;
char *buff;
char tmp[4];
int ret;
buff = NULL;
while ((ret = get_next_line(0, &buff)) > 0)
ft_bzero(tmp, 4);
while ((ret = ft_getline(&buff)) > 0)
{
ft_strncpy(tmp, buff, 3);
if (ft_strlen(buff) > 4 || !is_op(buff)
|| (nop = ft_lstnew(buff, 4)) == NULL)
|| (nop = ft_lstnew(tmp, 4)) == NULL)
{
ft_putendl_fd("Error", 2);
ft_strdel(&buff);
return (1);
}
ft_lstaddend(&(data->op), nop);
ft_strdel(&buff);
}
ft_strdel(&buff);
return (0);
}
@ -92,10 +98,10 @@ void check_stack(t_psdata *data)
int nb;
ind = data->a;
nb = ind->nb;
while (ind)
{
if (ind->nb < nb)
nb = ind->nb;
if (ind->nb < nb || data->size_b > 0)
{
ft_putendl("KO");
return ;
@ -103,7 +109,7 @@ void check_stack(t_psdata *data)
nb = ind->nb;
ind = ind->next;
}
if (!ind)
if (!ind && data->size_b == 0)
ft_putendl("OK");
}

61
srcs/ps_clean.c Normal file
View File

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ps_clean.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/16 13:14:52 by tmaze #+# #+# */
/* Updated: 2019/03/16 13:29:57 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void clean_push(t_psdata *data, t_list ***prec, t_list **it)
{
**prec = (*it)->next->next;
ft_memdel(&((*it)->next->content));
ft_memdel((void**)(&((*it)->next)));
ft_memdel(&((*it)->content));
ft_memdel((void**)it);
*it = data->op;
*prec = &(data->op);
}
static void clean_swap(t_psdata *data, t_list ***prec, t_list **it)
{
**prec = (*it)->next;
ft_strcpy((char*)((*it)->next->next->content), "sa");
ft_memdel(&((*it)->content));
ft_memdel((void**)it);
*it = data->op;
*prec = &(data->op);
}
void clean_op(t_psdata *data)
{
t_list *it;
t_list **prec;
it = data->op;
prec = &(data->op);
while (it && it->next)
{
if (((char*)(it->content))[0] == 'p'
&& ((char*)(it->next->content))[0] == 'p'
&& ((char*)(it->content))[1] != ((char*)(it->next->content))[1])
clean_push(data, &prec, &it);
else if (it->next->next && ((char*)(it->content))[0] == 'p'
&& ((char*)(it->content))[1] == 'b'
&& ((char*)(it->next->next->content))[0] == 'p'
&& ((char*)(it->next->next->content))[1] == 'a'
&& ft_strcmp((char*)(it->next->content), "rra") == 0)
clean_swap(data, &prec, &it);
else
{
prec = &(it->next);
it = it->next;
}
}
}

86
srcs/ps_sort.c Normal file
View File

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ps_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/16 13:21:02 by tmaze #+# #+# */
/* Updated: 2019/03/16 13:27:43 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int join_lists(t_psdata *data, t_stack **last, int inds, int inds2)
{
while ((*last)->ind == inds2 || data->size_b > 0)
if (((*last)->ind != inds2 && data->size_b > 0)
|| (data->size_b > 0 && data->b->nb > (*last)->nb))
{
if (add_op(data, "pa\0") == NULL)
return (0);
ps_push(data, 'a');
data->a->ind = inds;
}
else if (data->size_b == 0 || data->b->nb < (*last)->nb)
{
if (add_op(data, "rra") == NULL)
return (0);
ps_rerot(data, 'a');
data->a->ind = inds;
(*last) = get_last_a(data);
}
return (1);
}
static int rotate_list(t_psdata *data, t_stack **last)
{
t_list *tmp;
if (get_nb_groups(data) > 2)
{
if (get_nb_groups(data) != 2
&& data->a->ind != (*last)->ind && add_op(data, "rra") == NULL)
return (0);
else if (get_nb_groups(data) != 2 && data->a->ind != (*last)->ind)
{
ps_rerot(data, 'a');
(*last) = get_last_a(data);
while ((*last)->ind == data->a->ind
&& (tmp = add_op(data, "rra")) != NULL)
{
ps_rerot(data, 'a');
(*last) = get_last_a(data);
}
}
}
return (1);
}
int sort(t_psdata *data)
{
t_list *tmp;
t_stack *last;
int inds;
int inds2;
last = get_last_a(data);
inds = last->ind + 1;
while (data->a->ind != last->ind)
{
inds2 = data->a->ind;
while (data->a && data->a->ind == inds2
&& (tmp = add_op(data, "pb\0")) != NULL)
ps_push(data, 'b');
if (tmp == NULL)
return (0);
inds2 = last->ind;
if (join_lists(data, &last, inds, inds2) == 0)
return (0);
if (rotate_list(data, &last) == 0)
return (0);
inds++;
}
return (1);
}

100
srcs/ps_sorttools.c Normal file
View File

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ps_sorttools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/16 13:23:27 by tmaze #+# #+# */
/* Updated: 2019/03/16 13:23:57 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void mark_groups(t_psdata *data)
{
t_stack *ptr;
int inds;
ptr = data->a;
inds = 1;
while (ptr)
{
ptr->ind = inds;
if (ptr->next && ptr->nb > ptr->next->nb)
inds++;
ptr = ptr->next;
}
}
t_stack *get_last_a(t_psdata *data)
{
t_stack *ptr;
ptr = data->a;
while (ptr && ptr->next)
ptr = ptr->next;
return (ptr);
}
int get_size_firsts(t_psdata *data)
{
t_stack *ptr;
int inds;
int i;
inds = data->a->ind;
i = 0;
ptr = data->a;
while (ptr && ptr->ind == inds && (i += 1))
ptr = ptr->next;
if (ptr)
{
inds = ptr->ind;
while (ptr && ptr->ind == inds && (i += 1))
ptr = ptr->next;
}
return (i);
}
int get_size_last(t_psdata *data)
{
t_stack *ptr;
int inds;
int i;
i = 0;
ptr = data->a;
while (ptr && ptr->next)
ptr = ptr->next;
inds = ptr->ind;
ptr = data->a;
while (ptr)
{
i += (ptr->ind == inds) ? 1 : 0;
ptr = ptr->next;
}
return (i);
}
int get_nb_groups(t_psdata *data)
{
t_stack *ptr;
int ind;
int ret;
ret = 0;
ptr = data->a;
ind = ptr->ind;
while (ptr)
{
if (ret == 0 || ptr->ind != ind)
{
ret++;
ind = ptr->ind;
}
ptr = ptr->next;
}
return (ret);
}

85
srcs/ps_specsort.c Normal file
View File

@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ps_specsort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/16 13:18:14 by tmaze #+# #+# */
/* Updated: 2019/03/16 13:28:12 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void get_min_max(t_psdata *data, int *min_max, int *ind_min_max)
{
t_stack *ptr;
int i;
min_max[0] = FT_INT_MAX;
min_max[1] = FT_INT_MIN;
ptr = data->a;
i = 0;
while (ptr)
{
if (ptr->nb < min_max[0] && (ind_min_max[0] = i) == i)
min_max[0] = ptr->nb;
if (ptr->nb > min_max[1] && (ind_min_max[1] = i) == i)
min_max[1] = ptr->nb;
i++;
ptr = ptr->next;
}
}
void sortfor3(t_psdata *data)
{
int min_max[2];
int ind_min_max[2];
get_min_max(data, min_max, ind_min_max);
while (ind_min_max[0] != 0 || ind_min_max[1] != 2)
{
if (ind_min_max[0] == ind_min_max[1] - 1)
ft_printf("sa\n");
if (ind_min_max[0] == ind_min_max[1] - 1)
ps_swap(data, 'a');
if (ind_min_max[1] == 0)
ft_printf("ra\n");
if (ind_min_max[1] == 0)
ps_rot(data, 'a');
if (ind_min_max[0] == 2 && ind_min_max[1] == 1)
ft_printf("rra\n");
if (ind_min_max[0] == 2 && ind_min_max[1] == 1)
ps_rerot(data, 'a');
get_min_max(data, min_max, ind_min_max);
}
}
void sortfor5(t_psdata *data)
{
ft_printf("pb\n");
ps_push(data, 'b');
ft_printf("pb\n");
ps_push(data, 'b');
if (data->b->nb > data->b->next->nb)
ft_printf("sb\n");
if (data->b->nb > data->b->next->nb)
ps_swap(data, 'b');
sortfor3(data);
data->b->ind = 1;
data->b->next->ind = 1;
data->a->ind = 2;
data->a->next->ind = 2;
data->a->next->next->ind = 2;
while (data->a->ind == 2 || data->size_b > 0)
{
if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb))
ft_printf("pa\n");
if (data->a->ind != 2 || (data->b && data->b->nb < data->a->nb))
ps_push(data, 'a');
data->a->ind = 3;
ft_printf("ra\n");
ps_rot(data, 'a');
}
}