a bit of code clean up but not much =|

removed old sort function
This commit is contained in:
Tanguy MAZE 2019-03-11 16:59:44 +01:00
parent 735c6af817
commit 3d54238d24
2 changed files with 56 additions and 74 deletions

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
/* Updated: 2019/03/10 13:27:02 by tmaze ### ########.fr */
/* Updated: 2019/03/11 13:41:27 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,7 +63,7 @@ void exec_actions(t_psdata *data)
ps_rerot(data, buff[2]);
else if (buff[2] == 'r')
ps_rrerot(data);
debug(data, buff);
// debug(data, buff);
nop = nop->next;
}
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/03 11:41:49 by tmaze #+# #+# */
/* Updated: 2019/03/10 22:36:06 by tmaze ### ########.fr */
/* Updated: 2019/03/11 16:30:23 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -152,7 +152,28 @@ int get_size_last(t_psdata *data)
return (i);
}
int sort2(t_psdata *data)
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);
}
int sort(t_psdata *data)
{
t_list *tmp;
t_stack *last;
@ -187,89 +208,33 @@ int sort2(t_psdata *data)
last = get_last_a(data);
}
}
if (data->a->ind != last->ind && add_op(data, "rra") == NULL)
return (0);
else if (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)
// 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);
}
}
}
// }
inds++;
}
return (1);
}
void sort(t_psdata *data)
{
t_stack *ptr;
int inds;
int inds2;
int i;
ptr = data->a;
while (ptr && ptr->next)
ptr = ptr->next;
inds2 = ptr->ind + 1;
while (data->a->ind != ptr->ind)
{
inds = data->a->ind;
while (data->a->ind == inds)
{
ft_printf("pb\n");
ps_push(data, 'b');
if (data->size_b == 2)
ft_printf("sb\n");
if (data->size_b == 2)
ps_swap(data, 'b');
else if (data->size_b > 2)
ft_printf("rb\n");
if (data->size_b > 2)
ps_rot(data, 'b');
}
inds = data->a->ind;
if (data->size_a == 3)
{
sortfor3(data);
data->a->ind = inds;
data->a->next->ind = inds;
data->a->next->next->ind = inds;
}
while (inds == data->a->ind || data->size_b > 0)
{
if (data->a->ind != inds || (data->size_b > 0 && data->b->nb < data->a->nb))
{
ft_printf("pa\n");
ps_push(data, 'a');
}
data->a->ind = inds2;
ft_printf("ra\n");
ps_rot(data, 'a');
}
i = 0;
ptr = data->a;
while (ptr)
{
ptr = ptr->next;
i++;
}
ptr = data->a;
while (ptr && ptr->next)
ptr = ptr->next;
inds2 = ptr->ind + 1;
}
}
void clean_op(t_psdata *data)
{
t_list *it;
t_list **prec;
char *ct1;
char *ct2;
char *ct3;
it = data->op;
prec = &(data->op);
@ -287,6 +252,24 @@ void clean_op(t_psdata *data)
it = data->op;
prec = &(data->op);
}
else if (it->next->next)
{
ct3 = (char*)(it->next->next->content);
if (ct1[0] == 'p' && ct1[1] == 'b' && ct3[0] == 'p' && ct3[1] == 'a' && ft_strcmp(ct2, "rra") == 0)
{
*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);
}
else
{
prec = &(it->next);
it = it->next;
}
}
else
{
prec = &(it->next);
@ -325,9 +308,8 @@ int main(int ac, char **av)
return (0);
}
mark_groups(&data);
if (sort2(&data) == 0)
if (sort(&data) == 0)
ft_putendl_fd("Error\n", 2);
// sort(&data);
else
{
clean_op(&data);