From 173279d8e83e81fd1d451adb2153103dc0506ad8 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 24 Apr 2024 14:59:53 +0200 Subject: Make norminette happy and some more error handling --- sorting.c | 78 +++------------------------------------------------------------ 1 file changed, 3 insertions(+), 75 deletions(-) (limited to 'sorting.c') diff --git a/sorting.c b/sorting.c index c7522eb..aa27f55 100644 --- a/sorting.c +++ b/sorting.c @@ -6,7 +6,7 @@ /* By: dkaiser b->size + 1) / 2) - moves_to_top = pos; - else - moves_to_top = data->b->size - pos; - i = 0; - while (i < data->a->size && data->a->stack[i] > data->b->stack[pos]) - i++; - if (i < (data->a->size + 1) / 2) - moves_to_spot = 2 * i + 1; - else - moves_to_spot = 2 * ((data->a->size - i) + 1); - return (moves_to_top + moves_to_spot); -} - -static void move_to_top(t_psdata *data, int idx) -{ - if (idx < (data->b->size + 1) / 2) - { - while (--idx > 0) - run_command(data, RB); - } - else - { - idx = data->b->size - idx; - while (idx--) - run_command(data, RRB); - } -} - -static void move_to_spot(t_psdata *data, int idx) -{ - int pos; - int i; - - pos = 0; - while (pos < data->a->size && data->b->stack[0] > data->a->stack[pos]) - pos++; - if (pos >= data->a->size) - { - move_to_top(data, idx); - run_command(data, PA); - run_command(data, RA); - } - else if (pos < (data->a->size + 1) / 2) - { - i = pos; - while (i--) - run_command(data, RA); - move_to_top(data, idx); - run_command(data, PA); - i = pos; - while (i--) - run_command(data, RRA); - } - else - { - i = data->a->size - pos; - while (i--) - run_command(data, RRA); - move_to_top(data, idx); - run_command(data, PA); - i = data->a->size - pos; - while (i--) - run_command(data, RA); - } -} - static void scoresort(t_psdata *data) { int *scores; @@ -118,7 +45,8 @@ static void scoresort(t_psdata *data) int min_score; scores = malloc(sizeof(int) * data->b->size); - // Error if allocation fails + if (!scores) + return ; i = 0; while (i < data->b->size) { -- cgit v1.2.3