From 352281790df250bb740562aa41acf5f4272c6ec2 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sat, 21 Jun 2025 05:17:21 -0500 Subject: [PATCH] refactor(datastructures): added naming to stack and queue datastructure macro --- Include/Fledasty/Core/Queue.h | 30 +++++++++++++++--------------- Include/Fledasty/Core/Stack.h | 28 ++++++++++++++-------------- Tests/Main.c | 10 +++++----- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index b2752d4..9c5585c 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -31,26 +31,26 @@ #include "../Utils/Error.h" -#define FLEDASTY_QUEUE_DEFINE(type) \ +#define FLEDASTY_QUEUE_DEFINE(type, name) \ typedef struct { \ size_t size, capacity; \ size_t head, tail; \ type *buffer; \ -} FledastyQueue_##type; \ +} FledastyQueue_##name; \ \ -FledastyError fledasty_queue_##type##_free(FledastyQueue_##type *current_queue); \ +FledastyError fledasty_queue_##name##_free(FledastyQueue_##name *current_queue); \ \ -FledastyError fledasty_queue_##type##_push(FledastyQueue_##type *current_queue, type value); \ -type fledasty_queue_##type##_pop(FledastyQueue_##type *current_queue); \ +FledastyError fledasty_queue_##name##_push(FledastyQueue_##name *current_queue, type value); \ +type fledasty_queue_##name##_pop(FledastyQueue_##name *current_queue); \ \ -FledastyError fledasty_queue_##type##_clear(FledastyQueue_##type *current_queue); \ -FledastyError fledasty_queue_##type##_shrink_to_fit(FledastyQueue_##type *current_queue); \ +FledastyError fledasty_queue_##name##_clear(FledastyQueue_##name *current_queue); \ +FledastyError fledasty_queue_##name##_shrink_to_fit(FledastyQueue_##name *current_queue); \ \ -static inline type fledasty_queue_##type##_peek(const FledastyQueue_##type *current_queue) { return (current_queue != NULL && current_queue->size > 0) ? current_queue->buffer[current_queue->size - 1] : 0; } \ -static inline bool fledasty_queue_##type##_is_empty(const FledastyQueue_##type *current_queue) { return current_queue == NULL || current_queue->size == 0; } +static inline type fledasty_queue_##name##_peek(const FledastyQueue_##name *current_queue) { return (current_queue != NULL && current_queue->size > 0) ? current_queue->buffer[current_queue->head] : 0; } \ +static inline bool fledasty_queue_##name##_is_empty(const FledastyQueue_##name *current_queue) { return current_queue == NULL || current_queue->size == 0; } -#define FLEDASTY_QUEUE_IMPLEMENT(type) \ -FledastyError fledasty_queue_##type##_free(FledastyQueue_##type *current_queue) { \ +#define FLEDASTY_QUEUE_IMPLEMENT(type, name) \ +FledastyError fledasty_queue_##name##_free(FledastyQueue_##name *current_queue) { \ if (current_queue == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ @@ -66,7 +66,7 @@ FledastyError fledasty_queue_##type##_free(FledastyQueue_##type *current_queue) return FLEDASTY_ERROR_NONE; \ } \ \ -FledastyError fledasty_queue_##type##_push(FledastyQueue_##type *current_queue, type value) { \ +FledastyError fledasty_queue_##name##_push(FledastyQueue_##name *current_queue, type value) { \ if (current_queue == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ @@ -101,7 +101,7 @@ FledastyError fledasty_queue_##type##_push(FledastyQueue_##type *current_queue, return FLEDASTY_ERROR_NONE; \ } \ \ -type fledasty_queue_##type##_pop(FledastyQueue_##type *current_queue) { \ +type fledasty_queue_##name##_pop(FledastyQueue_##name *current_queue) { \ if (current_queue == NULL || current_queue->size == 0) { \ return 0; \ } \ @@ -115,7 +115,7 @@ type fledasty_queue_##type##_pop(FledastyQueue_##type *current_queue) { return current_queue->buffer[current_queue->head - 1]; \ } \ \ -FledastyError fledasty_queue_##type##_clear(FledastyQueue_##type *current_queue) { \ +FledastyError fledasty_queue_##name##_clear(FledastyQueue_##name *current_queue) { \ if (current_queue == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ @@ -124,7 +124,7 @@ FledastyError fledasty_queue_##type##_clear(FledastyQueue_##type *current_queue) return FLEDASTY_ERROR_NONE; \ } \ \ -FledastyError fledasty_queue_##type##_shrink_to_fit(FledastyQueue_##type *current_queue) { \ +FledastyError fledasty_queue_##name##_shrink_to_fit(FledastyQueue_##name *current_queue) { \ if (current_queue == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index 70da942..3eddfc9 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -31,25 +31,25 @@ #include "../Utils/Error.h" -#define FLEDASTY_STACK_DEFINE(type) \ +#define FLEDASTY_STACK_DEFINE(type, name) \ typedef struct { \ size_t size, capacity; \ type *buffer; \ -} FledastyStack_##type; \ +} FledastyStack_##name; \ \ -FledastyError fledasty_stack_##type##_free(FledastyStack_##type *current_stack); \ +FledastyError fledasty_stack_##name##_free(FledastyStack_##name *current_stack); \ \ -FledastyError fledasty_stack_##type##_push(FledastyStack_##type *current_stack, type value); \ +FledastyError fledasty_stack_##name##_push(FledastyStack_##name *current_stack, type value); \ \ -FledastyError fledasty_stack_##type##_clear(FledastyStack_##type *current_stack); \ -FledastyError fledasty_stack_##type##_shrink_to_fit(FledastyStack_##type *current_stack); \ +FledastyError fledasty_stack_##name##_clear(FledastyStack_##name *current_stack); \ +FledastyError fledasty_stack_##name##_shrink_to_fit(FledastyStack_##name *current_stack); \ \ -static inline type fledasty_stack_##type##_pop(FledastyStack_##type *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[--current_stack->size] : 0; } \ -static inline type fledasty_stack_##type##_peek(const FledastyStack_##type *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[current_stack->size - 1] : 0; } \ -static inline bool fledasty_stack_##type##_is_empty(const FledastyStack_##type *current_stack) { return current_stack == NULL || current_stack->size == 0; } +static inline type fledasty_stack_##name##_pop(FledastyStack_##name *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[--current_stack->size] : 0; } \ +static inline type fledasty_stack_##name##_peek(const FledastyStack_##name *current_stack) { return (current_stack != NULL && current_stack->size > 0) ? current_stack->buffer[current_stack->size - 1] : 0; } \ +static inline bool fledasty_stack_##name##_is_empty(const FledastyStack_##name *current_stack) { return current_stack == NULL || current_stack->size == 0; } -#define FLEDASTY_STACK_IMPLEMENT(type) \ -FledastyError fledasty_stack_##type##_free(FledastyStack_##type *current_stack) { \ +#define FLEDASTY_STACK_IMPLEMENT(type, name) \ +FledastyError fledasty_stack_##name##_free(FledastyStack_##name *current_stack) { \ if (current_stack == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ @@ -65,7 +65,7 @@ FledastyError fledasty_stack_##type##_free(FledastyStack_##type *current_stack) return FLEDASTY_ERROR_NONE; \ } \ \ -FledastyError fledasty_stack_##type##_push(FledastyStack_##type *current_stack, type value) { \ +FledastyError fledasty_stack_##name##_push(FledastyStack_##name *current_stack, type value) { \ if (current_stack == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ @@ -83,7 +83,7 @@ FledastyError fledasty_stack_##type##_push(FledastyStack_##type *current_stack, return FLEDASTY_ERROR_NONE; \ } \ \ -FledastyError fledasty_stack_##type##_clear(FledastyStack_##type *current_stack) { \ +FledastyError fledasty_stack_##name##_clear(FledastyStack_##name *current_stack) { \ if (current_stack == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ @@ -92,7 +92,7 @@ FledastyError fledasty_stack_##type##_clear(FledastyStack_##type *current_stack) return FLEDASTY_ERROR_NONE; \ } \ \ -FledastyError fledasty_stack_##type##_shrink_to_fit(FledastyStack_##type *current_stack) { \ +FledastyError fledasty_stack_##name##_shrink_to_fit(FledastyStack_##name *current_stack) { \ if (current_stack == NULL) { \ return FLEDASTY_ERROR_INVALID_POINTER; \ } \ diff --git a/Tests/Main.c b/Tests/Main.c index 8c5cb59..afa2f1f 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -33,11 +33,11 @@ static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; } static inline size_t integer_hash_function(const void *key) { return *(int*)key; } -FLEDASTY_STACK_DEFINE(int) -FLEDASTY_STACK_IMPLEMENT(int) +FLEDASTY_STACK_DEFINE(int, int) +FLEDASTY_STACK_IMPLEMENT(int, int) -FLEDASTY_QUEUE_DEFINE(int) -FLEDASTY_QUEUE_IMPLEMENT(int) +FLEDASTY_QUEUE_DEFINE(int, int) +FLEDASTY_QUEUE_IMPLEMENT(int, int) FLEDASTY_DYNAMIC_ARRAY_DEFINE(int, int) FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(int, int, compare_integers) @@ -50,7 +50,7 @@ int main() { fledasty_queue_int_shrink_to_fit(&test_queue); printf("Queue schrinked to fit %s\n", (test_queue.capacity == test_queue.size) ? "succeeded" : "failed"); - fledasty_queue_int_push(&test_queue, 0); + fledasty_queue_int_push(&test_queue, 10); printf("Queue peeked: %d\n", fledasty_queue_int_peek(&test_queue)); for (int i = test_queue.size; i > 0; i -= 1) {