From: Axy Date: Tue, 2 Dec 2025 22:19:31 +0000 (+0100) Subject: Comments and shenanigans X-Git-Url: https://git.uwuaxy.net/?a=commitdiff_plain;h=157e46b5bc5cf2f1b4c80031d63320dd443332ae;p=axy%2Fft%2Fpushswap.git Comments and shenanigans --- diff --git a/.gitignore b/.gitignore index 47cb25a..612d16d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ **.a **.o +pushswap +checker diff --git a/a.out b/a.out new file mode 100755 index 0000000..e052e40 Binary files /dev/null and b/a.out differ diff --git a/cheatalloc.c b/cheatalloc.c index 3696e89..46492e6 100644 --- a/cheatalloc.c +++ b/cheatalloc.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 15:07:21 by agilliar #+# #+# */ -/* Updated: 2025/12/02 16:41:27 by agilliar ### ########.fr */ +/* Updated: 2025/12/02 23:19:25 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,19 @@ void cheatexit(int errcode) exit(errcode); } +// I present to you: "null garbage collector" allocator +// When your program is on a device intended to explode in 5 seconds, +// you do not need to worry about frees, the world is your garbage collector +// +// Since our program has a bounded amount of allocations per run, and no +// allocations are used as scrath working space, we would not need to free +// except on program termination. +// The OS is an excellent garbage collector, however some consider memory leaks +// errors. +// +// We just keep a in-allocation linked list that gets automatically freed on +// exit. The only requirement is that `cheatexit` be called in all termination +// paths. void *cheatalloc(size_t len) { void **new; diff --git a/clist.c b/clist.c index cbfb0de..4a78d4b 100644 --- a/clist.c +++ b/clist.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 11:02:13 by agilliar #+# #+# */ -/* Updated: 2025/12/02 17:37:32 by agilliar ### ########.fr */ +/* Updated: 2025/12/02 23:05:13 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/pushswap.c b/main_pushswap.c similarity index 68% rename from pushswap.c rename to main_pushswap.c index 29a20e5..9c4970e 100644 --- a/pushswap.c +++ b/main_pushswap.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* pushswap.c :+: :+: :+: */ +/* main_pushswap.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 22:15:12 by agilliar #+# #+# */ -/* Updated: 2025/12/02 22:16:49 by agilliar ### ########.fr */ +/* Updated: 2025/12/02 23:15:12 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,13 @@ int main(int argc, char **argv) { - (void) argc; - (void) argv; + t_stacks stacks; + + stacks = stacks_new(); + for (int i = 1; i < argc; i++) + stacks_insert_init(&stacks, psval_parse(argv[i])); + for (int i = 1; i < argc; i++) + psval_output(psval_parse(argv[i])); + output_flush(); + cheatexit(!stacks_is_solved(&stacks)); } diff --git a/output.c b/output.c index 4c2235e..c1fed0b 100644 --- a/output.c +++ b/output.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 16:47:00 by agilliar #+# #+# */ -/* Updated: 2025/12/02 22:11:55 by agilliar ### ########.fr */ +/* Updated: 2025/12/02 22:42:38 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ void output_flush(void) store->written = 0; } -static void output_char(char c) +void output_char(char c) { t_output_store *store; diff --git a/psval.c b/psval.c new file mode 100644 index 0000000..a565da9 --- /dev/null +++ b/psval.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* psval.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: agilliar +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/02 22:35:19 by agilliar #+# #+# */ +/* Updated: 2025/12/02 22:54:24 by agilliar ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pushswap.h" + +static void psval_output_no_sign(t_psval val) +{ + if (val > 9 || val < -9) + psval_output_no_sign(val / 10); + val %= 10; + if (val < 0) + val = -val; + output_char("0123456789"[val]); +} + +void psval_output(t_psval val) +{ + if (val < 0) + output_char('-'); + psval_output_no_sign(val); +} + +t_psval psval_parse(const char *s) +{ + t_psval sign; + t_psval res; + + sign = 1; + res = 0; + if (*s == '-') + { + s++; + sign = -1; + } + while (*s) + { + if (*s < '0' || *s > '9') + cheatexit(1); + if (__builtin_mul_overflow(res, 10, &res)) + cheatexit(1); + if (__builtin_add_overflow(res, sign * (*s - '0'), &res)) + cheatexit(1); + s++; + } + return (res); +} diff --git a/pushswap.h b/pushswap.h index 57219e0..7015368 100644 --- a/pushswap.h +++ b/pushswap.h @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 11:02:44 by agilliar #+# #+# */ -/* Updated: 2025/12/02 18:40:21 by agilliar ### ########.fr */ +/* Updated: 2025/12/02 23:05:01 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,19 +70,29 @@ typedef struct s_ops t_closure ops[11]; } t_ops; -void cheatexit(int errcode); -void *cheatalloc(size_t len); +void cheatexit(int errcode); +void *cheatalloc(size_t len); -t_clist *clist_new(t_psval val); +t_clist *clist_new(t_psval val); // Positive numbers are forward, which wraps back to end of stack -t_psval clist_get_at(const t_clist *list, int i); -t_clist *clist_pop(t_clist **src); -// *dst: : Nullable -void clist_push(t_clist **dst, t_clist *lst); -// *dst: : Nullable -void clist_push_back(t_clist **dst, t_clist *lst); - -void output_str(char *s); -void output_str_ln(char *s); +t_psval clist_get_at(const t_clist *list, int i); +t_clist *clist_pop(t_clist **src); +// *dst : Nullable +void clist_push(t_clist **dst, t_clist *lst); +// *dst : Nullable +void clist_push_back(t_clist **dst, t_clist *lst); + +void output_char(char c); +void output_str(char *s); +void output_str_ln(char *s); +void output_flush(void); + +t_stacks stacks_new(void); +void stacks_insert_init(t_stacks *stacks, t_psval val); +bool stacks_is_solved(const t_stacks *stacks); +void stacks_apply(t_stacks *stacks, const t_ops *ops, t_algorithm algo); + +t_psval psval_parse(const char *s); +void psval_output(t_psval val); #endif diff --git a/stacks.c b/stacks.c index 85844c6..b8a0a82 100644 --- a/stacks.c +++ b/stacks.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 15:57:41 by agilliar #+# #+# */ -/* Updated: 2025/12/02 22:17:14 by agilliar ### ########.fr */ +/* Updated: 2025/12/02 22:25:54 by agilliar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ void stacks_insert_init(t_stacks *stacks, t_psval val) stacks->a_len++; } -bool stack_is_solved(const t_stacks *stacks) +bool stacks_is_solved(const t_stacks *stacks) { size_t i;