blob: 2d93252eedc4c1b43647d16d58436f8215a097e4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_optimization.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 16:42:34 by dkaiser #+# #+# */
/* Updated: 2024/04/13 17:31:18 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/ft_printf.h"
#include "libft/libft.h"
#include "push_swap.h"
/* void optimize_commands(t_list **cmds) */
/* { */
/* int optimizations; */
/* t_list *cur; */
/* t_list *last; */
/* optimizations = 0; */
/* cur = *cmds; */
/* last = NULL; */
/* while (cur->next) */
/* { */
/* if ((*(enum e_pscmd *)cur->content == PA */
/* && *(enum e_pscmd *)cur->next->content == PB) */
/* || (*(enum e_pscmd *)cur->content == PA */
/* && *(enum e_pscmd *)cur->next->content == PB)) */
/* { */
/* if (last) */
/* last->next = cur->next->next; */
/* else */
/* (*cmds)->next = cur->next->next; */
/* ft_lstdelone(cur->next, free); */
/* ft_lstdelone(cur, free); */
/* optimizations++; */
/* } */
/* if (!optimizations) */
/* { */
/* last = cur; */
/* cur = cur->next; */
/* } */
/* else */
/* break; */
/* } */
/* if (optimizations) */
/* optimize_commands(cmds); */
/* } */
static enum e_pscmd get_cmd(t_list *cmd)
{
if (cmd)
return (*(enum e_pscmd*)cmd->content);
else
return NO_CMD;
}
void optimize_commands(t_list **cmds)
{
t_list *cur;
t_list *last;
int optimizations;
cur = *cmds;
last = cur;
optimizations = 0;
while (cur->next)
{
if ((get_cmd(cur) == PA && get_cmd(cur->next) == PB) || (get_cmd(cur) == PB && get_cmd(cur->next) == PA))
{
last->next = cur->next->next;
ft_lstdelone(cur->next, free);
ft_lstdelone(cur, free);
optimizations++;
}
last = last->next;
cur = last->next;
}
if (optimizations)
optimize_commands(cmds);
}
|