From 5427f29f7f8b4a6691330c2fc8a1c24027d8fe71 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sat, 13 Apr 2024 17:49:06 +0200 Subject: Add cmd optimization After the sorting algorithm was executed remove unneccessary commands. --- cmd_optimization.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 cmd_optimization.c (limited to 'cmd_optimization.c') diff --git a/cmd_optimization.c b/cmd_optimization.c new file mode 100644 index 0000000..2d93252 --- /dev/null +++ b/cmd_optimization.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmd_optimization.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser 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); +} -- cgit v1.2.3