Bring source code to compliance with the 42 Norm
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 1 Mar 2024 09:01:33 +0000 (10:01 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 1 Mar 2024 09:01:33 +0000 (10:01 +0100)
Makefile
src/checker.h
src/circular_lis.c
src/helpers.c
src/push.c
src/special_a.c [new file with mode: 0644]

index a1c984c024d1550fad9e6287200b1f5a217c9edb..644f05d7d78f3137e415400a9732ac77f66c89df 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,7 @@ PUSHSOURCES :=        push.c                                  \
                                prepare.c                               \
                                find.c                                  \
                                circular_lis.c                  \
+                               special_a.c                             \
 
 PUSHSOURCES := $(addprefix $(PUSHSRCDIR)/, $(PUSHSOURCES))
 
index d9aeec0e236f98ae777893e25951e0315aeab4bb..2d09590e455ea415bd27e9009ab311994764b370 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/01/24 12:13:51 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/29 16:27:28 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/01 09:49:02 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -77,4 +77,7 @@ size_t                binary_search(t_vec *sorted, t_stack *stack, size_t val_ind);
 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);
+void           rotate_a_to_sort(t_stacks *s);
+void           isolate_lis_in_a(t_stacks *s, t_stack *lis);
+void           a_sort_three_or_less(t_stacks *s);
 #endif //CHECKER_H
index b1ccd9b7a16ffae4df402c3af28a01dae813a3ba..aaef1ccc727044819362783e98263f0c6a1afbae 100644 (file)
@@ -6,14 +6,15 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/02/27 15:18:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/29 16:59:55 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/01 09:59:03 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "checker.h"
 #include "libft.h"
 
-static t_arr_stat reconstruct_lis(t_stack *lis, t_stack *stack, t_vec *lowest, t_vec *predecessors)
+static t_arr_stat      reconstruct_lis(t_stack *lis, t_stack *stack,
+               t_vec *lowest, t_vec *predecessors)
 {
        size_t          i;
        size_t          j;
@@ -36,7 +37,8 @@ static t_arr_stat reconstruct_lis(t_stack *lis, t_stack *stack, t_vec *lowest, t
        return (success);
 }
 
-static t_arr_stat get_lis_loop(t_stack *stack, t_vec *inds_of_lowest, t_vec *inds_of_predecessor)
+static t_arr_stat      get_lis_loop(t_stack *stack, t_vec *inds_of_lowest,
+               t_vec *inds_of_predecessor)
 {
        size_t                          i;
        size_t                          new_ind;
@@ -52,13 +54,14 @@ static t_arr_stat get_lis_loop(t_stack *stack, t_vec *inds_of_lowest, t_vec *ind
                else
                        *(size_t *)ft_vec_access(inds_of_lowest, new_ind) = i;
                if (res != success)
-                       break;
+                       break ;
                if (new_ind == 0)
                        res = ft_vec_append(inds_of_predecessor, &invalid);
                else
-                       res = ft_vec_append(inds_of_predecessor, ft_vec_access(inds_of_lowest, new_ind - 1));
+                       res = ft_vec_append(inds_of_predecessor,
+                                       ft_vec_access(inds_of_lowest, new_ind - 1));
                if (res != success)
-                       break;
+                       break ;
                ++i;
        }
        return (res);
@@ -66,7 +69,7 @@ static t_arr_stat get_lis_loop(t_stack *stack, t_vec *inds_of_lowest, t_vec *ind
 
 /*     This is the algorithm for finding the longest increasing subsequence
        found on wikipedia adjusted for circular sequences (stack).*/
-static t_arr_stat get_lis(t_stack *lis, t_stack *stack, size_t ind)
+static t_arr_stat      get_lis(t_stack *lis, t_stack *stack, size_t ind)
 {
        t_vec                   inds_of_lowest;
        t_vec                   inds_of_predecessor;
index ee17362638a6a1673df568e9963547f85e3c3926..b68558da795f12b0969b6aeb13e3f97a33c8b50a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/02/22 14:22:08 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/29 16:20:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/01 09:59:03 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -97,7 +97,8 @@ size_t        binary_search(t_vec *sorted, t_stack *stack, size_t val_ind)
        mid = lo + (hi - lo) / 2;
        while (lo != hi)
        {
-               if (val > unrolled_stack_access(stack, *(size_t *)ft_vec_access(sorted, mid)))
+               if (val > unrolled_stack_access(stack,
+                               *(size_t *)ft_vec_access(sorted, mid)))
                {
                        lo = mid + 1;
                }
@@ -109,4 +110,3 @@ size_t      binary_search(t_vec *sorted, t_stack *stack, size_t val_ind)
        }
        return (mid);
 }
-
index 8bc0fb5014cf307e7d7ebe9d9adbae5644280519..ae09a0f1808ef07054886638a108aad547be319e 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/01/24 13:17:32 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/02/29 14:41:41 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/01 09:59:04 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 
 #include "checker.h"
 #include "libft.h"
-#include <limits.h>
 #include <stddef.h>
 
-void   a_sort_three_or_less(t_stacks *s)
-{
-       int             last;
-       int             max;
-       size_t  steps_to_smaller;
-       size_t  i;
-
-       i = 0;
-       last = stack_top(&s->a);
-       max = INT_MIN;
-       steps_to_smaller = 0;
-       while (i < s->a.stack.size)
-       {
-               stack_rotate(&s->a, 1);
-               steps_to_smaller += (stack_top(&s->a) < last);
-               last = stack_top(&s->a);
-               if (max < stack_top(&s->a))
-                       max = stack_top(&s->a);
-               ++i;
-       }
-       if (steps_to_smaller == 2)
-               print_swap(s, a);
-       return ;
-}
-
 void   get_push_indeces(t_stacks *s, int *ind_a, int *ind_b)
 {
        t_vec   target_a_indeces;
@@ -68,27 +42,6 @@ void get_push_indeces(t_stacks *s, int *ind_a, int *ind_b)
        return ;
 }
 
-void   rotate_a_to_sort(t_stacks *s)
-{
-       size_t          minind;
-       t_action        *print_action;
-
-       minind = find_minind(&s->a);
-       if (minind <= s->a.stack.size / 2)
-               print_action = print_rotate;
-       else
-       {
-               minind -= s->a.stack.size / 2;
-               print_action = print_reverse_rotate;
-       }
-       while (minind > 0)
-       {
-               print_action(s, a);
-               --minind;
-       }
-       return ;
-}
-
 void   sort(t_stacks *s)
 {
        int             ind_a;
@@ -97,16 +50,7 @@ void sort(t_stacks *s)
 
        init_stack(&lis);
        get_circular_lis(&lis, &s->a);
-       while (s->a.stack.size > lis.stack.size && s->a.stack.size > 3)
-       {
-               if (stack_top(&lis) == stack_top(&s->a))
-               {
-                       stack_rotate(&lis, 1);
-                       print_rotate(s, a);
-               }
-               else
-                       print_push(s, b);
-       }
+       isolate_lis_in_a(s, &lis);
        free_stack(&lis);
        if (lis.stack.size < 3)
                a_sort_three_or_less(s);
diff --git a/src/special_a.c b/src/special_a.c
new file mode 100644 (file)
index 0000000..919a73b
--- /dev/null
@@ -0,0 +1,77 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   special_a.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/01 09:45:01 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/03/01 09:59:04 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "checker.h"
+#include "libft.h"
+#include <limits.h>
+#include <stddef.h>
+
+void   rotate_a_to_sort(t_stacks *s)
+{
+       size_t          minind;
+       t_action        *print_action;
+
+       minind = find_minind(&s->a);
+       if (minind <= s->a.stack.size / 2)
+               print_action = print_rotate;
+       else
+       {
+               minind -= s->a.stack.size / 2;
+               print_action = print_reverse_rotate;
+       }
+       while (minind > 0)
+       {
+               print_action(s, a);
+               --minind;
+       }
+       return ;
+}
+
+void   isolate_lis_in_a(t_stacks *s, t_stack *lis)
+{
+       while (s->a.stack.size > lis->stack.size && s->a.stack.size > 3)
+       {
+               if (stack_top(lis) == stack_top(&s->a))
+               {
+                       stack_rotate(lis, 1);
+                       print_rotate(s, a);
+               }
+               else
+                       print_push(s, b);
+       }
+       return ;
+}
+
+void   a_sort_three_or_less(t_stacks *s)
+{
+       int             last;
+       int             max;
+       size_t  steps_to_smaller;
+       size_t  i;
+
+       i = 0;
+       last = stack_top(&s->a);
+       max = INT_MIN;
+       steps_to_smaller = 0;
+       while (i < s->a.stack.size)
+       {
+               stack_rotate(&s->a, 1);
+               steps_to_smaller += (stack_top(&s->a) < last);
+               last = stack_top(&s->a);
+               if (max < stack_top(&s->a))
+                       max = stack_top(&s->a);
+               ++i;
+       }
+       if (steps_to_smaller == 2)
+               print_swap(s, a);
+       return ;
+}