diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index abc9e63..e018653 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -40,5 +40,6 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value); void *fledasty_queue_peek(FledastyQueue *current_queue); +void *fledasty_queue_pop(FledastyQueue *current_queue); #endif \ No newline at end of file diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index c700c58..1711c8d 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -60,7 +60,7 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) { current_queue->buffer = NULL; return FLEDASTY_ERROR_NONE; } - +#include FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { if (current_queue == NULL || value == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; @@ -73,18 +73,19 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { if (current_queue->buffer == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - - size_t new_tail = current_queue->capacity - current_queue->tail; - hallocy_move_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), current_queue->buffer + (new_tail * current_queue->element_byte_size), current_queue->size - current_queue->tail); - current_queue->tail = new_tail; + printf("Allocated\n"); + size_t new_head = current_queue->capacity - current_queue->head; + hallocy_move_memory(current_queue->buffer + (current_queue->head * current_queue->element_byte_size), current_queue->buffer + (new_head * current_queue->element_byte_size), current_queue->size - current_queue->tail); + current_queue->head = new_head; } - hallocy_copy_memory(current_queue->buffer + (current_queue->head * current_queue->element_byte_size), value, current_queue->element_byte_size); + hallocy_copy_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), value, current_queue->element_byte_size); current_queue->size += 1; - current_queue->head += 1; - if (current_queue->head >= current_queue->capacity) { - current_queue->head = 0; + current_queue->tail += 1; + if (current_queue->tail >= current_queue->capacity) { + printf("To 0\n"); + current_queue->tail = 0; } return FLEDASTY_ERROR_NONE; @@ -95,5 +96,22 @@ void *fledasty_queue_peek(FledastyQueue *current_queue) { return NULL; } - return current_queue->buffer + (current_queue->tail * current_queue->element_byte_size); + return current_queue->buffer + (current_queue->head * current_queue->element_byte_size); +} + +void *fledasty_queue_pop(FledastyQueue *current_queue) { + if (current_queue == NULL || current_queue->size == 0) { + return NULL; + } + + void *value_address = current_queue->buffer + (current_queue->head * current_queue->element_byte_size); + + current_queue->size -= 1; + if (current_queue->head == current_queue->capacity) { + current_queue->head = 0; + } else { + current_queue->head += 1; + } + + return value_address; } \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index 1a03f6f..d2222fa 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -33,6 +33,11 @@ int main() { int *peek_data = (int*)fledasty_queue_peek(&test_queue); printf("Peeked: %d\n", *peek_data); + for (int i = test_queue.size; i > 0; i -= 1) { + int *popped_data = (int*)fledasty_queue_pop(&test_queue); + printf("Popped: %d\n", *popped_data); + } + fledasty_queue_destroy(&test_queue); printf("Done\n");