From f6a144987cc403de8ad6bddd055f756120f8d405 Mon Sep 17 00:00:00 2001 From: Ethan Werner Date: Wed, 17 Apr 2024 16:00:00 -0700 Subject: archival commit --- include/stack.h | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 include/stack.h (limited to 'include/stack.h') diff --git a/include/stack.h b/include/stack.h new file mode 100644 index 0000000..7e0f711 --- /dev/null +++ b/include/stack.h @@ -0,0 +1,84 @@ +#ifndef STACK_H +#define STACK_H + +#include "./type.h" +#include +#include +#include +#include +#include + +typedef struct stack { + stack_value_t *data; + stack_value_t *head; + uint_t size; + uint_t capacity; +} stack_t; + +void stack_init(stack_t *); +stack_t *stack_from_array(stack_value_t *, uint_t); +void stack_push(stack_t *, stack_value_t); +stack_value_t stack_pop(stack_t *); +stack_value_t stack_peek(stack_t *); + +#endif + +#ifdef STACK_IMPLEMENTATION + +const stack_value_t ZERO = 0; + +void stack_init(stack_t *stack) { + if (stack->capacity == 0) { + stack->capacity = 16; + } + + stack->size = 0; + stack->data = calloc(stack->capacity, sizeof(stack_value_t)); + stack->head = stack->data; +} + +stack_t *stack_from_array(stack_value_t *array, uint_t size) { + stack_t *stack = calloc(1, sizeof(stack_t)); + + stack->size = size; + stack->capacity = size; + stack->data = calloc(size, sizeof(stack_value_t)); + memcpy(stack->data, array, sizeof(stack_value_t) * size); + stack->head = stack->data; + + return stack; +} + +void stack_push(stack_t *stack, stack_value_t x) { + if (stack->size == stack->capacity) { + stack->capacity *= 2; + stack->data = realloc(stack->data, sizeof(stack_value_t) * stack->capacity); + stack->head = &(stack->data[stack->size - 1]); + } + + *stack->head = x; + stack->head++; + stack->size++; +} + +stack_value_t stack_pop(stack_t *stack) { + if (stack->size == 0) { + return ZERO; + } + + stack_value_t x = stack->head[-1]; + stack->size--; + stack->head--; + + return x; +} + +stack_value_t stack_peek(stack_t *stack) { + if (stack->size == 0) { + return ZERO; + } + + return stack->head[-1]; +} + +#endif \ No newline at end of file -- cgit v1.3.1