Skip to content

Commit

Permalink
Fix content index (#26)
Browse files Browse the repository at this point in the history
* refactor generate_stack

* update Makefile test

* rename main

* add header to initialization

* fix module

* fix 0 indexed
  • Loading branch information
rask24 committed Feb 13, 2024
1 parent 3ca79cc commit e7080ac
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 128 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME = push_swap
CFLAGS = -Werror -Wextra -Wall
CXXFLAGS = -std=c++17 -Wall -Wextra -Werror
PROD_FLAGS = -O3
DEV_FLAGS = -g -fsanitize=address -O0 -D DEV
DEV_FLAGS = -g -fsanitize=address,integer,undefined -O0 -D DEV
LEAK_FLAGS = -O0 -D DEV -D LEAK
DEPFLAGS = -MMD -MP
INCLUDE = -I $(INC_DIR)
Expand Down Expand Up @@ -109,9 +109,7 @@ leak: $(NAME)

releak: fclean leak

test: test_clean test_main

test_main: all $(GTEST_OBJ) $(TEST_OBJ)
test: all $(GTEST_OBJ) $(TEST_OBJ)
@echo "$(BLUE)\ntest linking$(RESET)"
@$(CXX) -L $(LIBFT_DIR) -lft -lpthread $(OBJ_FILTER_MAIN) $(TEST_OBJ) $(GTEST_OBJ) -o $(TEST_NAME)
./$(TEST_NAME)
Expand All @@ -121,6 +119,8 @@ test_clean:
@echo "$(BLUE)test cleaning$(RESET)"
@$(RM) -r $(TEST_BUILD_DIR)

retest: test_clean test

$(TEST_BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp
@mkdir -p $(@D)
@$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -c $< -o $@
Expand Down
7 changes: 6 additions & 1 deletion include/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:58:12 by reasuke ### ########.fr */
/* Updated: 2024/02/13 12:45:48 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,4 +21,9 @@
# include <stdlib.h>
# include <unistd.h>

int check_args(int argc, char **argv);
t_stack *generate_stack(int argc, char **argv);

void sort(t_stack **p_a, t_stack **p_b);

#endif
3 changes: 2 additions & 1 deletion src/initialization/check_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:44:55 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:35:17 by reasuke ### ########.fr */
/* Updated: 2024/02/13 11:45:54 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "initialization.h"
#include "push_swap.h"

static bool _has_not_digit(int argc, char **argv)
{
Expand Down
3 changes: 2 additions & 1 deletion src/initialization/exit_with_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:38:17 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:35:14 by reasuke ### ########.fr */
/* Updated: 2024/02/13 11:46:04 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "initialization.h"
#include "push_swap.h"

void exit_with_error(void)
{
Expand Down
79 changes: 39 additions & 40 deletions src/initialization/generate_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,107 +6,106 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:04:40 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:35:32 by reasuke ### ########.fr */
/* Updated: 2024/02/13 14:01:23 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "initialization.h"
#include "push_swap.h"

int *_generate_int_array(int array_size)
int *_allocate_int_array(int size_a)
{
int *array;

array = malloc(sizeof(int) * array_size);
array = malloc(sizeof(int) * size_a);
if (!array)
exit_with_error();
return (array);
}

int *_generate_numberd_array(int array_size, char **argv)
int *_generate_numberd_array(int size_a, char **argv)
{
int i;
int *numberd_array;
int *numberd;

numberd_array = _generate_int_array(array_size);
numberd = _allocate_int_array(size_a);
i = 0;
while (i < array_size)
while (i < size_a)
{
numberd_array[i] = ft_atoi(argv[i + 1]);
numberd[i] = ft_atoi(argv[i + 1]);
i++;
}
return (numberd_array);
return (numberd);
}

int *_generate_sorted_array(int argc, char **argv)
int *_generate_sorted_array(int size_a, char **argv)
{
int array_size;
int *sorted_array;
int *sorted;
int i;
int j;

array_size = argc - 1;
sorted_array = _generate_numberd_array(array_size, argv);
i = array_size - 1;
sorted = _generate_numberd_array(size_a, argv);
i = size_a - 1;
while (i > 0)
{
j = 0;
while (j < i)
{
if (sorted_array[j] > sorted_array[j + 1])
ft_swap(sorted_array + j, sorted_array + j + 1);
if (sorted[j] > sorted[j + 1])
ft_swap(sorted + j, sorted + j + 1);
j++;
}
i--;
}
return (sorted_array);
return (sorted);
}

int *_coordinate_compression(int argc, char **argv)
int *_coordinate_compression(int size_a, char **argv)
{
int *compressred_array;
int *sorted_array;
int array_size;
int *comp;
int *sorted;
int i;
int j;

array_size = argc - 1;
compressred_array = _generate_int_array(array_size);
sorted_array = _generate_sorted_array(argc, argv);
comp = _allocate_int_array(size_a);
sorted = _generate_sorted_array(size_a, argv);
i = 0;
while (i < array_size)
while (i < size_a)
{
j = 0;
while (j < array_size)
while (j < size_a)
{
if (ft_atoi(argv[i + 1]) == sorted_array[j])
compressred_array[i] = j + 1;
if (ft_atoi(argv[i + 1]) == sorted[j])
comp[i] = j;
j++;
}
i++;
}
free(sorted_array);
return (compressred_array);
free(sorted);
return (comp);
}

t_stack *generate_stack(int argc, char **argv)
{
t_stack *stack;
int *compressed_array;
t_stack *st;
int *comp;
int i;
t_content *ptr;
int size_a;

compressed_array = _coordinate_compression(argc, argv);
size_a = argc - 1;
comp = _coordinate_compression(size_a, argv);
i = 0;
stack = NULL;
while (i < argc - 1)
st = NULL;
while (i < size_a)
{
ptr = ft_calloc(1, sizeof(t_content));
if (!ptr)
exit_with_error();
ptr->index = compressed_array[i];
ft_lstadd_back(&stack, ft_lstnew(ptr));
ptr->index = comp[i];
ft_lstadd_back(&st, ft_lstnew(ptr));
i++;
}
free(compressed_array);
return (stack);
free(comp);
return (st);
}
24 changes: 14 additions & 10 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:54:10 by reasuke ### ########.fr */
/* Updated: 2024/02/13 15:00:02 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "initialization.h"
#include "push_swap.h"
#include "sort.h"

#ifdef LEAK
# ifdef __APPLE__

void leak_chek(void) __attribute__((destructor));
void leak_chek(void) __attribute__((destructor));

void leak_chek(void)
{
Expand All @@ -35,15 +33,21 @@ void put_void(void *content)

#endif

static void _put_void(void *p_content)
{
ft_printf("%d\n", ((t_content *)p_content)->index);
}

int main(int argc, char **argv)
{
t_stack *a;
t_stack *b;
t_stack *st_a;
t_stack *st_b;

check_args(argc, argv);
a = generate_stack(argc, argv);
b = NULL;
sort(&a, &b);
clear_stack(&a, free);
st_a = generate_stack(argc, argv);
st_b = NULL;
ft_lstiter(st_a, _put_void);
sort(&st_a, &st_b);
clear_stack(&st_a, free);
return (0);
}
4 changes: 2 additions & 2 deletions src/sort/is_sorted_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/10 18:47:11 by reasuke #+# #+# */
/* Updated: 2024/02/11 12:54:04 by reasuke ### ########.fr */
/* Updated: 2024/02/13 14:01:58 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,7 +17,7 @@ bool is_sorted_stack(t_stack *st)
{
int current;

current = 1;
current = 0;
while (st)
{
if (get_content(st)->index != current)
Expand Down
2 changes: 1 addition & 1 deletion src/sort/large_sort/large_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */
/* Updated: 2024/02/12 23:57:46 by reasuke ### ########.fr */
/* Updated: 2024/02/13 13:28:59 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
24 changes: 4 additions & 20 deletions src/sort/large_sort/push_b_segmented.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,14 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 22:57:28 by reasuke #+# #+# */
/* Updated: 2024/02/12 23:28:29 by reasuke ### ########.fr */
/* Updated: 2024/02/13 15:01:23 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "large_sort.h"
#include "push_swap.h"
#include "stack_operations.h"

// 1 ~ 100
// segment1: 1 ~ 20
// segment1: 21 ~ 40
// segment1: 41 ~ 60
// segment1: 61 ~ 80
// segment1: 81 ~ 100

// 123: 24
// [0, 24) -> [0, seg_size)
// [24, 48) -> [seg_size, seg_size * 2)
// [48, 72) -> [seg_size * 2, seg_size * 3)
// [72, 96) -> [seg_size * 3, seg_size * 4)
// [96, 123) -> [seg_size * 4, N)

static int _calc_segment_id(int target, int n, int segs)
{
int seg_size;
Expand All @@ -48,8 +34,6 @@ static int _calc_segment_id(int target, int n, int segs)
return (-1);
}

// ft_printf("ns: %d, index: %d, pushed: %d, n: %d\n", num_segment, index,
// pushed, n);
static bool _should_push_b(int index, int pushed, int n, int segs)
{
int seg_size;
Expand All @@ -70,8 +54,8 @@ static bool _should_push_b(int index, int pushed, int n, int segs)
sup = seg_size * (cur_id + 1);
}
if (cur_id == segs - 1)
return (inf < index && index <= n);
return (inf < index && index <= sup);
return (inf <= index);
return (inf <= index && index < sup);
}

void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs)
Expand All @@ -86,7 +70,7 @@ void push_b_segmented(t_stack **p_a, t_stack **p_b, int n, int segs)
if (_should_push_b(index, pushed, n, segs))
{
operate_pb(p_a, p_b);
if (_calc_segment_id(index - 1, n, segs) % 2 == 1)
if (_calc_segment_id(index, n, segs) % 2 == 1)
operate_rb(p_b);
pushed++;
}
Expand Down
Loading

0 comments on commit e7080ac

Please sign in to comment.