From ca59eec627e375d502a33176870e16f33b3830ce Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 13:03:03 -0500 Subject: [PATCH] feat(queue): implemented schrink to fit function --- Include/Fledasty/Core/Queue.h | 1 + Include/Fledasty/Core/Stack.h | 2 +- Src/Core/Queue.c | 48 ++++++++++++++++++++++++++++++++--- Src/Core/Stack.c | 4 +-- Tests/Main.c | 4 +++ 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index 0821a74..21ba5fa 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -43,6 +43,7 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value); void *fledasty_queue_pop(FledastyQueue *current_queue); FledastyError fledasty_queue_clear(FledastyQueue *current_queue); +FledastyError fledasty_queue_shrink_to_fit(FledastyQueue *current_queue); static inline void *fledasty_queue_peek(const FledastyQueue *current_queue) { return (current_queue == NULL) ? NULL : current_queue->buffer + (current_queue->head * current_queue->element_byte_size); } static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue == NULL || current_queue->size == 0; } diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index 5286d82..3db1132 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -44,7 +44,7 @@ void *fledasty_stack_pop(FledastyStack *current_stack); FledastyError fledasty_stack_clear(FledastyStack *current_stack); FledastyError fledasty_stack_shrink_to_fit(FledastyStack *current_stack); -static inline void *fledasty_stack_peek(const FledastyStack *current_stack) { (current_stack == NULL) ? NULL : current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size); } +static inline void *fledasty_stack_peek(const FledastyStack *current_stack) { return (current_stack == NULL) ? NULL : current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size); } static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack == NULL || current_stack->size == 0; } #endif diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index f0e55fc..5070b4b 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -21,6 +21,7 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/Queue.h" +#include "Fledasty/Utils/Error.h" #include #include @@ -84,10 +85,17 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - size_t head_length = current_queue->size - current_queue->head; - size_t new_head = current_queue->capacity - head_length; - hallocy_move_memory(current_queue->buffer + (new_head * current_queue->element_byte_size), current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_length * current_queue->element_byte_size); - current_queue->head = new_head; + if (current_queue->tail < current_queue->head) { + size_t head_size = (current_queue->size - current_queue->tail) * current_queue->element_byte_size; + + hallocy_move_memory(current_queue->buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_size); + hallocy_move_memory(current_queue->buffer + head_size, current_queue->buffer, current_queue->tail * current_queue->element_byte_size); + } else { + hallocy_move_memory(current_queue->buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), current_queue->size * current_queue->element_byte_size); + } + + current_queue->head = 0; + current_queue->tail = current_queue->size; } hallocy_copy_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), value, current_queue->element_byte_size); @@ -125,3 +133,35 @@ FledastyError fledasty_queue_clear(FledastyQueue *current_queue) { current_queue->size = 0; return FLEDASTY_ERROR_NONE; } + +FledastyError fledasty_queue_shrink_to_fit(FledastyQueue *current_queue) { + if (current_queue == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + size_t new_capacity = (current_queue->size == 0) ? 10 : current_queue->size; + unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(new_capacity); + if (shrinked_buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + if (current_queue->tail < current_queue->head) { + size_t head_size = (current_queue->size - current_queue->tail) * current_queue->element_byte_size; + + hallocy_copy_memory(shrinked_buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_size); + hallocy_copy_memory(shrinked_buffer + head_size, current_queue->buffer, current_queue->tail * current_queue->element_byte_size); + } else { + hallocy_copy_memory(shrinked_buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), current_queue->size * current_queue->element_byte_size); + } + + if (hallocy_free(current_queue->buffer) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + current_queue->head = 0; + current_queue->tail = current_queue->size; + current_queue->capacity = new_capacity; + current_queue->buffer = shrinked_buffer; + + return FLEDASTY_ERROR_NONE; +} diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index d8ec35c..6b9096b 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -114,12 +114,12 @@ FledastyError fledasty_stack_shrink_to_fit(FledastyStack *current_stack) { } current_stack->capacity = (current_stack->size == 0) ? 10 : current_stack->size; - unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(current_stack->capacity); + unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(current_stack->capacity * current_stack->element_byte_size); if (shrinked_buffer == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - hallocy_copy_memory(shrinked_buffer, current_stack->buffer, current_stack->size); + hallocy_copy_memory(shrinked_buffer, current_stack->buffer, current_stack->size * current_stack->element_byte_size); if (hallocy_free(current_stack->buffer) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } diff --git a/Tests/Main.c b/Tests/Main.c index 9c10342..1d21f87 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -39,6 +39,10 @@ int main() { fledasty_queue_push(&test_queue, &i); } + fledasty_queue_shrink_to_fit(&test_queue); + printf("Queue schrinked to fit %s\n", (test_queue.capacity == test_queue.size) ? "succeeded" : "failed"); + int test = 0; + fledasty_queue_push(&test_queue, &test); int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue); printf("Queue peeked: %d\n", *peeked_queue_data);