-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor generate_stack * update Makefile test * rename main * add header to initialization * fix module * fix 0 indexed
- Loading branch information
Showing
13 changed files
with
122 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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) | ||
|
@@ -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++; | ||
} | ||
|
Oops, something went wrong.