diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b344245 --- /dev/null +++ b/.clang-format @@ -0,0 +1,31 @@ +Language: C + +AlignConsecutiveDeclarations: false +AlignConsecutiveMacros: false +AlignEscapedNewlinesLeft: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +BinPackArguments: true +BinPackParameters: true +BreakBeforeBraces: Allman +BreakBeforeTernaryOperators: false +ColumnLimit: 80 +IndentWidth: 4 +KeepEmptyLinesAtTheStartOfBlocks: false +MaxEmptyLinesToKeep: 1 +PointerAlignment: Right +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakString: 1 +PenaltyExcessCharacter: 0 +SpaceAfterCStyleCast: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +TabWidth: 4 +UseTab: Always diff --git a/Makefile b/Makefile index dded088..e1662ef 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ INSERTION = insertion.c FLAGS_FILES = algorithms_sort.c flag.c bench.c PARSING = ft_atoi.c parsing.c ft_strncmp.c ft_split.c ft_strlen.c ft_substr.c ft_itoa.c ft_isdigit.c \ - ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c + ft_strjoin.c ft_strlcat.c ft_strlcpy.c parsing_2.c disorder.c indexer.c CHECKER_FILES = check_error.c verif_flag.c verif_is_digit.c verif_overflow.c verif_double.c diff --git a/algorithms/radix/radix.c b/algorithms/radix/radix.c index 31e8a82..249c2d5 100644 --- a/algorithms/radix/radix.c +++ b/algorithms/radix/radix.c @@ -12,17 +12,17 @@ #include "push_swap.h" -static int still_unit_value(t_stacks *stacks, int unit) +static int still_unit_index(t_stacks *stacks, int unit) { t_stack *temp; temp = stacks->a; - if (!temp || temp->value >= unit) + if (!temp || temp->index >= unit) return (1); temp = temp->next; while (temp != stacks->a) { - if (temp->value >= unit) + if (temp->index >= unit) return (1); temp = temp->next; } @@ -43,9 +43,9 @@ static void push_by_number_to_b(t_stacks *stacks, int unit, int nb) temp = temp->next; } i = 0; - while (i < s_len && still_unit_value(stacks, unit)) + while (i < s_len && still_unit_index(stacks, unit)) { - if (stacks->a && (stacks->a->value % (unit * 10)) / unit == nb) + if (stacks->a && (stacks->a->index % (unit * 10)) / unit == nb) pb(stacks); else ra(stacks); @@ -58,7 +58,7 @@ static void rec_sort(t_stacks *stacks, int unit) int i; i = 0; - if (!still_unit_value(stacks, unit)) + if (!still_unit_index(stacks, unit)) return ; while (i <= 9) { diff --git a/checker_linux b/checker_linux new file mode 100755 index 0000000..002d593 Binary files /dev/null and b/checker_linux differ diff --git a/flags/bench.c b/flags/bench.c index 31b5d2a..9c59efe 100644 --- a/flags/bench.c +++ b/flags/bench.c @@ -31,7 +31,7 @@ static void print_disorder(t_stacks *stacks) secure_write(2, ".", 1); if (ft_strlen(str) == 1) secure_write(2, "0", 1); - secure_write(2, &str[ft_strlen(str) - 2], 2); + secure_write(2, &str[ft_strlen(str) - 2], ft_strlen(str) - 2); secure_write(2, "%\n", 2); free(str); } diff --git a/includes/parsing.h b/includes/parsing.h index 9beb240..8501ce0 100644 --- a/includes/parsing.h +++ b/includes/parsing.h @@ -34,5 +34,6 @@ int ft_strlcat(char *dst, const char *src, int size); char *join_all(int argc, char **argv); char **split_all(char *tab); float compute_disorder(char **strs, int pos); +void indexer(t_stacks *stacks); #endif diff --git a/includes/push_swap.h b/includes/push_swap.h index 9105eb4..ec6476f 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -16,6 +16,7 @@ typedef struct s_stack { int value; + long long index; struct s_stack *next; struct s_stack *previous; } t_stack; diff --git a/main.c b/main.c index fbbec01..b4e75b5 100644 --- a/main.c +++ b/main.c @@ -32,7 +32,7 @@ int main(int argc, char **argv) if (check_error(tab, mod)) test1(tab, len, mod); else - write(2, "Error\n", 7); + write(2, "Error\n", 6); free_tab(tab); return (0); } diff --git a/parsing/indexer.c b/parsing/indexer.c new file mode 100644 index 0000000..bdcc097 --- /dev/null +++ b/parsing/indexer.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* indexer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/14 10:14:06 by dgaillet #+# #+# */ +/* Updated: 2026/01/14 10:14:08 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +static t_stack *first_without_index(t_stack *stack) +{ + t_stack *first; + + if (stack->index < 0) + return (stack); + first = stack; + stack = stack->next; + while (stack != first) + { + if (stack->index < 0) + return (stack); + stack = stack->next; + } + return ((void *) 0); +} + +void indexer(t_stacks *stacks) +{ + int i; + int len; + t_stack *lower; + t_stack *temp; + + i = 0; + len = stack_a_len(stacks); + while (i < len) + { + lower = first_without_index(stacks->a); + temp = lower->next; + while (temp != stacks->a) + { + if (lower->value > temp->value && temp->index < 0) + lower = temp; + temp = temp->next; + } + lower->index = i; + i++; + } +} diff --git a/secure_write.c b/secure_write.c index cc75fcc..ddf01e1 100644 --- a/secure_write.c +++ b/secure_write.c @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/01/08 14:30:38 by dgaillet #+# #+# */ -/* Updated: 2026/01/09 12:05:56 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/09 14:55:16 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,6 @@ void secure_write(int fd, char *str, int len) { if (len < 0) return ; - if (write(fd, str, len) < 0) + if (!str || write(fd, str, len) < 0) exit (EXIT_FAILURE); } diff --git a/stack_utils/stack_add.c b/stack_utils/stack_add.c index 5effe0f..18006f3 100644 --- a/stack_utils/stack_add.c +++ b/stack_utils/stack_add.c @@ -21,6 +21,7 @@ t_stack *new_stack(int value) if (!new) return (NULL); new->value = value; + new->index = -1; new->next = NULL; new->previous = NULL; return (new); diff --git a/test_one.c b/test_one.c index d909127..885422b 100644 --- a/test_one.c +++ b/test_one.c @@ -23,6 +23,7 @@ int test1(char **tab, int len, int mod) if (mod == -1) return (0); stacks = init_stacks(len, tab, mod); + indexer(stacks); if (!stacks) return (0); if (check_order(stacks->a))