From 875b69c407e71ce3b4bf1299e3b78a3096868997 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 8 Feb 2024 02:48:08 -0700 Subject: [PATCH 1/4] added stack datatype to std --- CMakeLists.txt | 1 + include/arc/std/vector.h | 11 +++++++---- src/std/vector.c | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a748f21..bf660ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ set(ARCHEUS_STD_SOURCES src/std/handler.c src/std/hashtable.c src/std/io.c + src/std/stack.c src/std/string.c src/std/vector.c src/std/defaults/config.c diff --git a/include/arc/std/vector.h b/include/arc/std/vector.h index dcf4ff2..f38ec27 100644 --- a/include/arc/std/vector.h +++ b/include/arc/std/vector.h @@ -18,7 +18,7 @@ typedef struct ARC_Vector ARC_Vector; * @param a first data struct * @param b second data struct * - * @return 0 when a == b + * @return 0 when a == b, TODO: return and ARC_Bool */ typedef int32_t (* ARC_Vector_CompareDataFn)(void *a, void *b); @@ -45,8 +45,9 @@ void ARC_Vector_Add(ARC_Vector *vector, void *data); /** * @brief remove from ARC_Vector * - * @param vector ARC_Vector to remove from - * @param data data that is being removed + * @param vector ARC_Vector to remove from + * @param data data that is being removed + * @param compare comparison callback to get check if data should be removed */ void ARC_Vector_Remove(ARC_Vector *vector, void *data, ARC_Vector_CompareDataFn compare); @@ -62,6 +63,8 @@ void ARC_Vector_RemoveIndex(ARC_Vector *vector, uint32_t *index); * @brief gets size of vector * * @param vector ARC_Vector to get size from + * + * @return the pointer of the vectors size, TODO: don't pass as a pointer */ uint32_t *ARC_Vector_Size(ARC_Vector *vector); @@ -79,4 +82,4 @@ void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index); } #endif -#endif //ARC_STD_VECTOR_H_ +#endif //ARC_STD_VECTOR_H_ \ No newline at end of file diff --git a/src/std/vector.c b/src/std/vector.c index 7e9158b..6ddba58 100644 --- a/src/std/vector.c +++ b/src/std/vector.c @@ -107,4 +107,4 @@ void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index){ } return vector->data[*index]; -} +} \ No newline at end of file From c3b4a4e209c7aa72fbfd075ebc4f7b530a35632f Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 8 Feb 2024 02:48:14 -0700 Subject: [PATCH 2/4] 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 From 314f490bef5fdefc345fed3c12e816745f96583d Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 8 Feb 2024 03:12:37 -0700 Subject: [PATCH 3/4] basic queue written, still need to test --- include/arc/std/queue.h | 57 +++++++++++++++++++++++++++++++ include/arc/std/stack.h | 6 ++-- src/std/queue.c | 76 +++++++++++++++++++++++++++++++++++++++++ src/std/stack.c | 7 ++-- 4 files changed, 140 insertions(+), 6 deletions(-) diff --git a/include/arc/std/queue.h b/include/arc/std/queue.h index e69de29..e833c48 100644 --- a/include/arc/std/queue.h +++ b/include/arc/std/queue.h @@ -0,0 +1,57 @@ +#ifndef ARC_STD_QUEUE_H_ +#define ARC_STD_QUEUE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/** + * @brief a queue type + */ +typedef struct ARC_Queue ARC_Queue; + +/** + * @brief creates ARC_Queue type + * + * @param queue ARC_Queue to initialize + */ +void ARC_Queue_Create(ARC_Queue **queue); + +/** + * @brief destroyes ARC_Queue type + */ +void ARC_Queue_Destroy(ARC_Queue *queue); + +/** + * @brief pushes value to end of queue + * + * @param queue ARC_Queue to push to + * @param data data that is being pushed + */ +void ARC_Queue_Push(ARC_Queue *queue, void *data); + +/** + * @brief pops the front off of the ARC_Queue + * + * @param queue ARC_Queue to remove from + * + * @return the poped data from the queue as a void * + */ +void *ARC_Queue_Pop(ARC_Queue *queue); + +/** + * @brief gets size of queue + * + * @param vector ARC_Queue to get size from + * + * @return the queues size + */ +uint32_t ARC_Queue_Size(ARC_Queue *queue); + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_QUEUE_H_ \ No newline at end of file diff --git a/include/arc/std/stack.h b/include/arc/std/stack.h index b4a0f90..daf1496 100644 --- a/include/arc/std/stack.h +++ b/include/arc/std/stack.h @@ -42,9 +42,9 @@ void ARC_Stack_Push(ARC_Stack *stack, void *data); void *ARC_Stack_Pop(ARC_Stack *stack); /** - * @brief gets size of vector + * @brief gets size of stack * - * @param vector ARC_Vector to get size from + * @param stack ARC_Stack to get size from * * @return the stacks size */ @@ -54,4 +54,4 @@ uint32_t ARC_Stack_Size(ARC_Stack *stack); } #endif -#endif //ARC_STD_STACK_H_ +#endif //ARC_STD_STACK_H_ \ No newline at end of file diff --git a/src/std/queue.c b/src/std/queue.c index e69de29..2baaec4 100644 --- a/src/std/queue.c +++ b/src/std/queue.c @@ -0,0 +1,76 @@ +#include "arc/std/queue.h" + +#include "arc/std/errno.h" +#include + +typedef struct ARC_QueueNode ARC_QueueNode; + +struct ARC_QueueNode { + ARC_QueueNode *next; + void *data; +}; + +struct ARC_Queue { + uint32_t currentSize; + ARC_QueueNode *node; +}; + +void ARC_Queue_Create(ARC_Queue **queue){ + *queue = (ARC_Queue *)malloc(sizeof(ARC_Queue)); + (*queue)->currentSize = 0; + (*queue)->node = NULL; +} + +void ARC_Queue_Destroy(ARC_Queue *queue){ + if(queue->currentSize != 0 || queue->node != NULL){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Queue_Destroy(queue) called, but queue was not empty"); + return; + } + + free(queue); +} + +void ARC_Queue_Push(ARC_Queue *queue, void *data){ + //TODO: check if size exceeds uint32_t + ARC_QueueNode *end = queue->node; + for(uint32_t i = 0; i < queue->currentSize; i++){ + //TODO: check if end->next exists + end = end->next; + } + + ARC_QueueNode *node = (ARC_QueueNode *)malloc(sizeof(ARC_QueueNode)); + node->data = data; + node->next = NULL; + + end->next = node; + + queue->currentSize++; +} + +void *ARC_Queue_Pop(ARC_Queue *queue){ + if(queue->currentSize == 0){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but queue was not empty"); + return NULL; + } + + ARC_QueueNode *node = queue->node; + if(node == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but node was NULL"); + return NULL; + } + + queue->node = node->next; + void *nodeData = node->data; + free(node); + + queue->currentSize--; + + return nodeData; +} + +uint32_t ARC_Queue_Size(ARC_Queue *queue){ + return queue->currentSize; +} \ No newline at end of file diff --git a/src/std/stack.c b/src/std/stack.c index 2593126..0cbed3a 100644 --- a/src/std/stack.c +++ b/src/std/stack.c @@ -24,7 +24,7 @@ void ARC_Stack_Create(ARC_Stack **stack){ 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"); + ARC_DEBUG_ERR("ARC_Stack_Destroy(stack) called, but stack was not empty"); return; } @@ -32,6 +32,7 @@ void ARC_Stack_Destroy(ARC_Stack *stack){ } void ARC_Stack_Push(ARC_Stack *stack, void *data){ + //TODO: check if size exceeds uint32_t ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode)); node->data = data; node->next = NULL; @@ -45,14 +46,14 @@ void ARC_Stack_Push(ARC_Stack *stack, void *data){ 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"); + ARC_DEBUG_ERR("ARC_Stack_Pop(stack) 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"); + ARC_DEBUG_ERR("ARC_Stack_Pop(stack) called, but node was NULL"); return NULL; } From b4eaee524256a3832c785bd42b6dd6cec5a4c367 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 8 Feb 2024 03:17:04 -0700 Subject: [PATCH 4/4] added a few error checks to stack and queue, need to test --- src/std/queue.c | 16 +++++++++++++--- src/std/stack.c | 7 ++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/std/queue.c b/src/std/queue.c index 2baaec4..1c1e10d 100644 --- a/src/std/queue.c +++ b/src/std/queue.c @@ -32,10 +32,20 @@ void ARC_Queue_Destroy(ARC_Queue *queue){ } void ARC_Queue_Push(ARC_Queue *queue, void *data){ - //TODO: check if size exceeds uint32_t + if(queue->currentSize == ~(uint32_t)0){ + arc_errno = ARC_ERRNO_OVERFLOW; + ARC_DEBUG_ERR("ARC_Queue_Push(queue) called, size of queue is maxed, cannot add another node"); + return; + } + ARC_QueueNode *end = queue->node; for(uint32_t i = 0; i < queue->currentSize; i++){ - //TODO: check if end->next exists + if(end == NULL){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_Queue_Push(queue), when getting end node encountered a NULL node"); + return; + } + end = end->next; } @@ -53,7 +63,7 @@ void *ARC_Queue_Pop(ARC_Queue *queue){ arc_errno = ARC_ERRNO_DATA; ARC_DEBUG_ERR("ARC_Queue_Pop(queue) called, but queue was not empty"); return NULL; - } + } ARC_QueueNode *node = queue->node; if(node == NULL){ diff --git a/src/std/stack.c b/src/std/stack.c index 0cbed3a..ca4f8a2 100644 --- a/src/std/stack.c +++ b/src/std/stack.c @@ -32,7 +32,12 @@ void ARC_Stack_Destroy(ARC_Stack *stack){ } void ARC_Stack_Push(ARC_Stack *stack, void *data){ - //TODO: check if size exceeds uint32_t + if(stack->currentSize == ~(uint32_t)0){ + arc_errno = ARC_ERRNO_OVERFLOW; + ARC_DEBUG_ERR("ARC_Stack_Push(stack) called, size of stack is maxed, cannot add another node"); + return; + } + ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode)); node->data = data; node->next = NULL;