cleaned up the sort but it's actually worse =/

added op cleanup function
need to work on optimising sort maybe with bottom-up
This commit is contained in:
Tanguy MAZE 2019-03-10 15:18:55 +01:00
parent d7cd178119
commit b993e0e68a
4 changed files with 72 additions and 37 deletions

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/21 14:13:53 by tmaze #+# #+# */ /* Created: 2019/02/21 14:13:53 by tmaze #+# #+# */
/* Updated: 2019/03/06 11:07:38 by tmaze ### ########.fr */ /* Updated: 2019/03/10 12:14:19 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -53,6 +53,7 @@ void ps_rerot(t_psdata *data, char c);
void ps_rrerot(t_psdata *data); void ps_rrerot(t_psdata *data);
int is_op(char *buff); int is_op(char *buff);
t_list *add_op(t_psdata *data, char *op);
int get_params(t_psdata *data, int ac, char **av); int get_params(t_psdata *data, int ac, char **av);
int check_input(char *in, int *ret); int check_input(char *in, int *ret);

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */ /* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
/* Updated: 2019/03/09 22:44:29 by tmaze ### ########.fr */ /* Updated: 2019/03/10 13:27:02 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "push_swap.h"
#define VISU_SPEED 1000000 #define VISU_SPEED 100000
void debug(t_psdata *data, char *com) void debug(t_psdata *data, char *com)
{ {
@ -26,11 +26,11 @@ void debug(t_psdata *data, char *com)
while (as || bs) while (as || bs)
{ {
if (as && bs) if (as && bs)
ft_printf("%5d %4d|| %5d %4d\n", as->nb, as->ind, bs->nb, bs->ind); ft_printf("%5d || %5d\n", as->nb, bs->nb);
else if (!as && bs) else if (!as && bs)
ft_printf("%5c %4c|| %5d %4d\n", ' ', ' ', bs->nb, bs->ind); ft_printf("%5c || %5d\n", ' ', bs->nb);
else if (as && !bs) else if (as && !bs)
ft_printf("%5d %4d|| %5c %c\n", as->nb, as->ind, ' ', ' '); ft_printf("%5d || %5c\n", as->nb, ' ');
as = (as) ? as->next : as; as = (as) ? as->next : as;
bs = (bs) ? bs->next : bs; bs = (bs) ? bs->next : bs;
} }

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/25 12:44:08 by tmaze #+# #+# */ /* Created: 2019/02/25 12:44:08 by tmaze #+# #+# */
/* Updated: 2019/03/09 15:04:14 by tmaze ### ########.fr */ /* Updated: 2019/03/10 12:27:41 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -77,3 +77,12 @@ int is_op(char *buff)
i++; i++;
return (i < 11); return (i < 11);
} }
t_list *add_op(t_psdata *data, char *op)
{
t_list *new;
if ((new = ft_lstnew(op, 4)) != NULL)
ft_lstaddend(&(data->op), new);
return (new);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/03 11:41:49 by tmaze #+# #+# */ /* Created: 2019/03/03 11:41:49 by tmaze #+# #+# */
/* Updated: 2019/03/09 23:12:23 by tmaze ### ########.fr */ /* Updated: 2019/03/10 15:17:33 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -112,8 +112,9 @@ t_stack *get_last_a(t_psdata *data)
return (ptr); return (ptr);
} }
void sort2(t_psdata *data) int sort2(t_psdata *data)
{ {
t_list *tmp;
t_stack *last; t_stack *last;
int inds; int inds;
int inds2; int inds2;
@ -123,32 +124,32 @@ void sort2(t_psdata *data)
while (data->a->ind != last->ind) while (data->a->ind != last->ind)
{ {
inds2 = data->a->ind; inds2 = data->a->ind;
while (data->a->ind == inds2) while (data->a->ind == inds2 && (tmp = add_op(data, "pb\0")) != NULL)
{
ft_printf("pb\n");
ps_push(data, 'b'); ps_push(data, 'b');
} if (tmp == NULL)
return (0);
inds2 = last->ind; inds2 = last->ind;
while (last->ind == inds2 || data->size_b > 0) while (last->ind == inds2 || data->size_b > 0)
{ {
last = get_last_a(data);
if ((last->ind != inds2 && data->size_b > 0) || (data->b && data->b->nb > last->nb)) if ((last->ind != inds2 && data->size_b > 0) || (data->b && data->b->nb > last->nb))
{ {
ft_printf("pa\n"); if (add_op(data, "pa\0") == NULL)
return (0);
ps_push(data, 'a'); ps_push(data, 'a');
data->a->ind = inds; data->a->ind = inds;
} }
else if (data->size_b == 0 || data->b->nb < last->nb) else if (data->size_b == 0 || data->b->nb < last->nb)
{ {
ft_printf("rra\n"); if (add_op(data, "rra") == NULL)
return (0);
ps_rerot(data, 'a'); ps_rerot(data, 'a');
data->a->ind = inds; data->a->ind = inds;
last = get_last_a(data); last = get_last_a(data);
} }
} }
inds++; inds++;
last = get_last_a(data);
} }
return (1);
} }
void sort(t_psdata *data) void sort(t_psdata *data)
@ -211,11 +212,42 @@ void sort(t_psdata *data)
} }
} }
void clean_op(t_psdata *data)
{
t_list *it;
t_list **prec;
char *ct1;
char *ct2;
it = data->op;
prec = &(data->op);
while (it && it->next)
{
ct1 = (char*)(it->content);
ct2 = (char*)(it->next->content);
if (ct1 && ct2 && ct1[0] == 'p' && ct2[0] == 'p' && ct1[1] != ct2[1])
{
*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);
}
else
{
prec = &(it->next);
it = it->next;
}
}
}
int main(int ac, char **av) int main(int ac, char **av)
{ {
t_list *it;
t_stack *ptr; t_stack *ptr;
t_psdata data; t_psdata data;
int i;
ps_initdata(&data); ps_initdata(&data);
if (!get_params(&data, ac, av)) if (!get_params(&data, ac, av))
@ -241,25 +273,18 @@ int main(int ac, char **av)
return (0); return (0);
} }
mark_groups(&data); mark_groups(&data);
ptr = data.a; if (sort2(&data) == 0)
while (ptr) ft_putendl_fd("Error\n", 2);
// sort(&data);
else
{ {
ft_printf("nb: %d ind: %d\n", ptr->nb, ptr->ind); clean_op(&data);
ptr = ptr->next; it = data.op;
while (it)
{
ft_printf("%s\n", (char*)(it->content));
it = it->next;
} }
ptr = data.a;
i = 0;
while (ptr)
{
ptr = ptr->next;
i++;
}
sort2(&data);
ptr = data.a;
while (ptr)
{
ft_printf("nb: %d ind: %d\n", ptr->nb, ptr->ind);
ptr = ptr->next;
} }
ps_stkclean(&data); ps_stkclean(&data);
return (0); return (0);