From cb76bcf7d2358ad04281d123141acfa175e31841 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Fri, 14 Apr 2023 22:55:43 -0400 Subject: i made things faster --- src/stack.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/stack.c (limited to 'src/stack.c') diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..3491581 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,47 @@ +#include "types.h" + +#include + +void stack_init(Stack* stack, uint32_t len) { + stack->index = 0; + stack->len = len; + stack->data = malloc(stack->len * sizeof(union stored)); +} + +void stack_push(Stack* stack, void* value) { + if (stack->index == stack->len) { + stack->len *= 2; + stack->data = realloc(stack->data, stack->len * sizeof(union stored)); + } + stack->data[stack->index].p = value; + stack->index++; +} + +void* stack_pop(Stack* stack) { + if (stack->index == 0) { + return NULL; + } + stack->index--; + return stack->data[stack->index].p; +} + +void stack_pushi(Stack* stack, uint32_t value) { + if (stack->index == stack->len) { + stack->len *= 2; + stack->data = realloc(stack->data, stack->len * sizeof(union stored)); + } + stack->data[stack->index].i = value; + stack->index++; +} + +uint32_t stack_popi(Stack* stack) { + if (stack->index == 0) { + return 0; + } + stack->index--; + return stack->data[stack->index].i; +} + +void stack_free(Stack* stack) { + free(stack->data); +} -- cgit v1.2.3-freya