merged with herbglitch/master
This commit is contained in:
commit
c822075173
8 changed files with 303 additions and 5 deletions
86
src/std/queue.c
Normal file
86
src/std/queue.c
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#include "arc/std/queue.h"
|
||||
|
||||
#include "arc/std/errno.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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){
|
||||
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++){
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
76
src/std/stack.c
Normal file
76
src/std/stack.c
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include "arc/std/stack.h"
|
||||
|
||||
#include "arc/std/errno.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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(stack) called, but stack was not empty");
|
||||
return;
|
||||
}
|
||||
|
||||
free(stack);
|
||||
}
|
||||
|
||||
void ARC_Stack_Push(ARC_Stack *stack, void *data){
|
||||
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;
|
||||
|
||||
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(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(stack) 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;
|
||||
}
|
||||
|
|
@ -107,4 +107,4 @@ void *ARC_Vector_Get(ARC_Vector *vector, uint32_t *index){
|
|||
}
|
||||
|
||||
return vector->data[*index];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue