Refactor push.c so as to conform with the Norm
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 24 Feb 2024 13:13:56 +0000 (14:13 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 24 Feb 2024 13:13:56 +0000 (14:13 +0100)
Makefile
src/checker.h
src/find.c [new file with mode: 0644]
src/helpers.c [new file with mode: 0644]
src/is_sorted.c [deleted file]
src/prepare.c
src/push.c

index 3044f308e2baac53a28ebbb95ed82d1e39692571..037c67320648d28b49ad11f1014ab23e126fda0e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,7 @@ CHECKSOURCES :=       checker.c                               \
                                stack_manipulation.c    \
                                arg_parsing.c                   \
                                stacks_mem.c                    \
-                               is_sorted.c                             \
+                               helpers.c                               \
 
 CHECKSOURCES := $(addprefix $(CHECKSRCDIR)/, $(CHECKSOURCES))
 
@@ -31,8 +31,9 @@ PUSHSOURCES :=        push.c                                  \
                                stacks_mem.c                    \
                                actions.c                               \
                                prints.c                                \
-                               is_sorted.c                             \
+                               helpers.c                               \
                                prepare.c                               \
+                               find.c                                  \
 
 PUSHSOURCES := $(addprefix $(PUSHSRCDIR)/, $(PUSHSOURCES))
 
index 2b7f3d7166e8584fde3d51bb5d0f852dca61456d..52e74bfbef149e0e51e99c6e0c77b9634b951410 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/01/24 12:13:51 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/23 09:48:21 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/02/24 14:06:52 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -67,9 +67,11 @@ void stack_rotate(t_stack *s, int amount);
 void   stack_pop(t_stack *s);
 int            stack_top(t_stack *s);
 
-int    is_sorted(t_stack *s);
-int    parse(int argc, char **argv, t_stack *s);
-
+int            is_sorted(t_stack *s);
+int            parse(int argc, char **argv, t_stack *s);
 void   prepare_stacks(t_stacks *s, size_t ind_a, size_t ind_b);
 size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus);
+size_t find_best(t_stacks *s, t_vec *target_a_indeces);
+size_t find_target_ind(t_stack *stack, int val);
+size_t find_minind(t_stack *s);
 #endif //CHECKER_H
diff --git a/src/find.c b/src/find.c
new file mode 100644 (file)
index 0000000..d64cf98
--- /dev/null
@@ -0,0 +1,112 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   find.c                                             :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/02/24 11:37:22 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/02/24 14:10:54 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "checker.h"
+#include "libft.h"
+#include <limits.h>
+#include <stddef.h>
+
+size_t find_minind(t_stack *s)
+{
+       size_t          minind;
+       size_t          i;
+       int                     min;
+
+       min = INT_MAX;
+       i = 0;
+       while (i < s->stack.size)
+       {
+               if (min >= stack_top(s))
+               {
+                       minind = i;
+                       min = stack_top(s);
+               }
+               stack_rotate(s, 1);
+               ++i;
+       }
+       return (minind);
+}
+
+static int     max_or_min(t_stack *stack, int val)
+{
+       int             max;
+       int             min;
+       size_t  i;
+
+       i = 0;
+       max = INT_MIN;
+       min = INT_MAX;
+       while (i < stack->stack.size)
+       {
+               if (stack_top(stack) > max)
+               {
+                       max = stack_top(stack);
+               }
+               if (stack_top(stack) < min)
+               {
+                       min = stack_top(stack);
+               }
+               stack_rotate(stack, 1);
+               ++i;
+       }
+       return (val < min || val > max);
+}
+
+size_t find_target_ind(t_stack *stack, int val)
+{
+       size_t          i;
+       size_t          res;
+       const int       minind = find_minind(stack);
+
+       i = 0;
+       if (max_or_min(stack, val))
+               return (minind);
+       while (i < stack->stack.size && val < stack_top(stack))
+       {
+               stack_rotate(stack, 1);
+               ++i;
+       }
+       while (i < stack->stack.size && val > stack_top(stack))
+       {
+               stack_rotate(stack, 1);
+               ++i;
+       }
+       res = i;
+       while (i < stack->stack.size)
+       {
+               stack_rotate(stack, 1);
+               ++i;
+       }
+       return (res);
+}
+
+size_t find_best(t_stacks *s, t_vec *target_a_indeces)
+{
+       size_t  res_ind;
+       size_t  cur;
+       size_t  best;
+       size_t  i;
+
+       best = SIZE_MAX;
+       i = 0;
+       while (i < s->b.stack.size)
+       {
+               cur = cost(s, i, *(size_t *)ft_vec_access(target_a_indeces, i));
+               if (cur < best)
+               {
+                       res_ind = i;
+                       best = cur;
+               }
+               ++i;
+       }
+       return (res_ind);
+}
diff --git a/src/helpers.c b/src/helpers.c
new file mode 100644 (file)
index 0000000..ef0a1c3
--- /dev/null
@@ -0,0 +1,74 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   helpers.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/02/22 14:22:08 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/02/24 14:09:17 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "checker.h"
+#include "libft.h"
+#include <stddef.h>
+
+int    is_sorted(t_stack *s)
+{
+       const size_t    start_ind = s->ind;
+       int                             prev;
+       int                             cur;
+
+       prev = stack_top(s);
+       stack_rotate(s, 1);
+       while (s->ind != start_ind)
+       {
+               cur = stack_top(s);
+               if (cur <= prev)
+               {
+                       s->ind = start_ind;
+                       return (0);
+               }
+               prev = cur;
+               stack_rotate(s, 1);
+       }
+       return (1);
+}
+
+// Let's split the stacks by the index of the current value
+//
+//   a         b
+//   |         |
+//   | a+      |
+//   |         |
+//   |         | b+
+//  ---        |
+//   |         |
+//   |         |
+//   |         |
+//   | a-     ---
+//   |         |
+//   |         | b-
+//   |         |
+//
+// Either we have to stack_rotate both in the same direction
+// in which case the cost is the higher of those
+//  - max(a+, b+) or max(a-, b-)
+// Or we have to stack_rotate them in the opposite direction
+// in which case we have to add the moves
+//  - a+ + b- or a- + b+
+//
+//  The minimum cost is the lowest of these four values
+size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus)
+{
+       size_t                  res;
+       const size_t    a_minus = s->a.stack.size - a_plus;
+       const size_t    b_minus = s->b.stack.size - b_plus;
+
+       res = ft_maxs(a_plus, b_plus);
+       res = ft_mins(res, ft_maxs(a_minus, b_minus));
+       res = ft_mins(res, a_plus + b_minus);
+       res = ft_mins(res, a_minus + b_plus);
+       return (res);
+}
diff --git a/src/is_sorted.c b/src/is_sorted.c
deleted file mode 100644 (file)
index 15e2a88..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/* ************************************************************************** */
-/*                                                                            */
-/*                                                        :::      ::::::::   */
-/*   is_sorted.c                                        :+:      :+:    :+:   */
-/*                                                    +:+ +:+         +:+     */
-/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
-/*                                                +#+#+#+#+#+   +#+           */
-/*   Created: 2024/02/22 14:22:08 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/23 10:58:13 by ljiriste         ###   ########.fr       */
-/*                                                                            */
-/* ************************************************************************** */
-
-#include "checker.h"
-#include <stddef.h>
-
-int    is_sorted(t_stack *s)
-{
-       const size_t    start_ind = s->ind;
-       int                             prev;
-       int                             cur;
-
-       prev = stack_top(s);
-       stack_rotate(s, 1);
-       while (s->ind != start_ind)
-       {
-               cur = stack_top(s);
-               if (cur <= prev)
-               {
-                       s->ind = start_ind;
-                       return (0);
-               }
-               prev = cur;
-               stack_rotate(s, 1);
-       }
-       return (1);
-}
index 51e4c1fb0d171d10b489107187f1a4394eeb11cb..f67b9609e28211ce5bbad51b9db31c19edce9b61 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/02/23 09:35:45 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/23 09:54:39 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/02/24 11:29:15 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,7 +21,7 @@ static void   prepare_plus_together(t_stacks *s, size_t rot_a, size_t rot_b)
        i = 0;
        while (i < ft_mins(rot_a, rot_b))
        {
-               print_rotate(s, both); 
+               print_rotate(s, both);
                ++i;
        }
        if (rot_a > rot_b)
@@ -50,7 +50,7 @@ static void   prepare_minus_together(t_stacks *s, size_t rot_a, size_t rot_b)
        i = 0;
        while (i < ft_mins(rot_a, rot_b))
        {
-               print_reverse_rotate(s, both); 
+               print_reverse_rotate(s, both);
                ++i;
        }
        if (rot_a > rot_b)
@@ -117,7 +117,8 @@ void        prepare_stacks(t_stacks *s, size_t ind_a, size_t ind_b)
        if (c == ft_maxs(ind_a, ind_b))
                prepare_plus_together(s, ind_a, ind_b);
        else if (c == ft_maxs(s->a.stack.size - ind_a, s->b.stack.size - ind_b))
-               prepare_minus_together(s, s->a.stack.size - ind_a, s->b.stack.size - ind_b);
+               prepare_minus_together(s, s->a.stack.size - ind_a,
+                       s->b.stack.size - ind_b);
        else if (c == ind_a + (s->b.stack.size - ind_b))
                prepare_a_plus_against(s, ind_a, s->b.stack.size - ind_b);
        else
index c3b67a9a94235f98029cce4ae1ec4b42adc7d454..fcf5b9d6ca9f50edb4e0860bef50861656dc0b83 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/01/24 13:17:32 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/24 11:15:29 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/02/24 14:10:54 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,6 +18,7 @@
 #include "checker.h"
 #include "libft.h"
 #include <limits.h>
+#include <stddef.h>
 
 void   a_sort_three_or_less(t_stacks *s)
 {
@@ -30,7 +31,7 @@ void  a_sort_three_or_less(t_stacks *s)
        last = stack_top(&s->a);
        max = INT_MIN;
        steps_to_smaller = 0;
-       while  (i < s->a.stack.size)
+       while (i < s->a.stack.size)
        {
                stack_rotate(&s->a, 1);
                steps_to_smaller += (stack_top(&s->a) < last);
@@ -44,139 +45,6 @@ void        a_sort_three_or_less(t_stacks *s)
        return ;
 }
 
-size_t find_minind(t_stack *s)
-{
-       size_t          minind;
-       size_t          i;
-       int                     min;
-
-       min = INT_MAX;
-       i = 0;
-       while (i < s->stack.size)
-       {
-               if (min >= stack_top(s))
-               {
-                       minind = i;
-                       min = stack_top(s);
-               }
-               stack_rotate(s, 1);
-               ++i;
-       }
-       return (minind);
-}
-
-int    max_or_min(t_stack *stack, int val)
-{
-       int             max;
-       int             min;
-       size_t  i;
-
-       i = 0;
-       max = INT_MIN;
-       min = INT_MAX;
-       while (i < stack->stack.size)
-       {
-               if (stack_top(stack) > max)
-               {
-                       max = stack_top(stack);
-               }
-               if (stack_top(stack) < min)
-               {
-                       min = stack_top(stack);
-               }
-               stack_rotate(stack, 1);
-               ++i;
-       }
-       return (val < min || val > max);
-}
-
-size_t find_target_ind(t_stack *stack, int     val)
-{
-       size_t          i;
-       size_t          res;
-       const int       minind = find_minind(stack);
-
-       i = 0;
-       if (max_or_min(stack, val))
-               return (minind);
-       while (i < stack->stack.size && val < stack_top(stack))
-       {
-               stack_rotate(stack, 1);
-               ++i;
-       }
-       while (i < stack->stack.size && val > stack_top(stack))
-       {
-               stack_rotate(stack, 1);
-               ++i;
-       }
-       res = i;
-       while (i < stack->stack.size)
-       {
-               stack_rotate(stack, 1);
-               ++i;
-       }
-       return (res);
-}
-
-// Let's split the stacks by the index of the current value
-//
-//   a         b
-//   |         |
-//   | a+      |
-//   |         |
-//   |         | b+
-//  ---        |
-//   |         |
-//   |         |
-//   |         |
-//   | a-     ---
-//   |         |
-//   |         | b-
-//   |         |
-//
-// Either we have to stack_rotate both in the same direction
-// in which case the cost is the higher of those
-//  - max(a+, b+) or max(a-, b-)
-// Or we have to stack_rotate them in the opposite direction
-// in which case we have to add the moves
-//  - a+ + b- or a- + b+
-//
-//  The minimum cost is the lowest of these four values
-size_t cost(t_stacks *s, const size_t b_plus, const size_t a_plus)
-{
-       size_t  res;
-       const size_t a_minus = s->a.stack.size - a_plus;
-       const size_t b_minus = s->b.stack.size - b_plus;
-
-       res = ft_maxs(a_plus, b_plus);
-       res = ft_mins(res, ft_maxs(a_minus, b_minus));
-       res = ft_mins(res, a_plus + b_minus);
-       res = ft_mins(res, a_minus + b_plus);
-       return (res);
-}
-
-size_t find_best(t_stacks *s, t_vec *target_a_indeces)
-{
-       size_t  res_ind;
-       size_t  cur;
-       size_t  best;
-       size_t  i;
-
-       best = SIZE_MAX;
-       i = 0;
-       while (i < s->b.stack.size)
-       {
-               cur = cost(s, i, *(size_t *)ft_vec_access(target_a_indeces, i));
-               if (cur < best)
-               {
-                       res_ind = i;
-                       best = cur;
-               }
-               ++i;
-       }
-       return (res_ind);
-}
-
 void   get_push_indeces(t_stacks *s, int *ind_a, int *ind_b)
 {
        t_vec   target_a_indeces;