From c3b4a4e209c7aa72fbfd075ebc4f7b530a35632f Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 8 Feb 2024 02:48:14 -0700 Subject: [PATCH] added stack datatype to std --- include/arc/std/bool.h | 18 +++++++++++ include/arc/std/queue.h | 0 include/arc/std/stack.h | 57 +++++++++++++++++++++++++++++++++ src/std/queue.c | 0 src/std/stack.c | 70 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 include/arc/std/bool.h create mode 100644 include/arc/std/queue.h create mode 100644 include/arc/std/stack.h create mode 100644 src/std/queue.c create mode 100644 src/std/stack.c diff --git a/include/arc/std/bool.h b/include/arc/std/bool.h new file mode 100644 index 0000000..3a251ff --- /dev/null +++ b/include/arc/std/bool.h @@ -0,0 +1,18 @@ +#ifndef ARC_STD_BOOL_H_ +#define ARC_STD_BOOL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define ARC_Bool bool +#define ARC_True true +#define ARC_False false + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_BOOL_H_ \ No newline at end of file diff --git a/include/arc/std/queue.h b/include/arc/std/queue.h new file mode 100644 index 0000000..e69de29 diff --git a/include/arc/std/stack.h b/include/arc/std/stack.h new file mode 100644 index 0000000..b4a0f90 --- /dev/null +++ b/include/arc/std/stack.h @@ -0,0 +1,57 @@ +#ifndef ARC_STD_STACK_H_ +#define ARC_STD_STACK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief a stack type + */ +typedef struct ARC_Stack ARC_Stack; + +/** + * @brief creates ARC_Stack type + * + * @param stack ARC_Stack to initialize + */ +void ARC_Stack_Create(ARC_Stack **stack); + +/** + * @brief destroyes ARC_Stack type + */ +void ARC_Stack_Destroy(ARC_Stack *stack); + +/** + * @brief pushes value on stack + * + * @param stack ARC_Stack to push to + * @param data data that is being pushed + */ +void ARC_Stack_Push(ARC_Stack *stack, void *data); + +/** + * @brief pops top off of ARC_Stack + * + * @param stack ARC_Stack to remove from + * + * @return the poped data from the stack as a void * + */ +void *ARC_Stack_Pop(ARC_Stack *stack); + +/** + * @brief gets size of vector + * + * @param vector ARC_Vector to get size from + * + * @return the stacks size + */ +uint32_t ARC_Stack_Size(ARC_Stack *stack); + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_STACK_H_ diff --git a/src/std/queue.c b/src/std/queue.c new file mode 100644 index 0000000..e69de29 diff --git a/src/std/stack.c b/src/std/stack.c new file mode 100644 index 0000000..2593126 --- /dev/null +++ b/src/std/stack.c @@ -0,0 +1,70 @@ +#include "arc/std/stack.h" + +#include "arc/std/errno.h" +#include + +typedef struct ARC_StackNode ARC_StackNode; + +struct ARC_StackNode { + ARC_StackNode *next; + void *data; +}; + +struct ARC_Stack { + uint32_t currentSize; + ARC_StackNode *node; +}; + +void ARC_Stack_Create(ARC_Stack **stack){ + *stack = (ARC_Stack *)malloc(sizeof(ARC_Stack)); + (*stack)->currentSize = 0; + (*stack)->node = NULL; +} + +void ARC_Stack_Destroy(ARC_Stack *stack){ + if(stack->currentSize != 0 || stack->node != NULL){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Stack_Destroy called, but stack was not empty"); + return; + } + + free(stack); +} + +void ARC_Stack_Push(ARC_Stack *stack, void *data){ + ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode)); + node->data = data; + node->next = NULL; + + node->next = stack->node; + stack->node = node; + + stack->currentSize++; +} + +void *ARC_Stack_Pop(ARC_Stack *stack){ + if(stack->currentSize == 0){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Stack_Pop called, but stack was not empty"); + return NULL; + } + + ARC_StackNode *node = stack->node; + if(node == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Stack_Pop called, but node was NULL"); + return NULL; + } + + stack->node = node->next; + void *nodeData = node->data; + free(node); + + stack->currentSize--; + + return nodeData; +} + +uint32_t ARC_Stack_Size(ARC_Stack *stack){ + return stack->currentSize; +} \ No newline at end of file