From 6e8385521643818a6a885ea86383df455c6b90db Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 24 Apr 2025 10:34:57 -0500 Subject: [PATCH] feat(dynamic array): added dynamic array datastructure with init and destroy function --- Include/Fledasty/Core/DynamicArray.h | 41 ++++++++++++++++ Src/Core/DynamicArray.c | 71 ++++++++++++++++++++++++++++ Src/Core/Queue.c | 2 +- Src/Core/Stack.c | 2 +- Tests/Main.c | 5 ++ 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 Include/Fledasty/Core/DynamicArray.h create mode 100644 Src/Core/DynamicArray.c diff --git a/Include/Fledasty/Core/DynamicArray.h b/Include/Fledasty/Core/DynamicArray.h new file mode 100644 index 0000000..15549b5 --- /dev/null +++ b/Include/Fledasty/Core/DynamicArray.h @@ -0,0 +1,41 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: DynamicArray.h + * Description: + * This file contains the Dynamic Array structure and the functions for modifying it. + * It includes functions to append, Insert before, Insert after, Insert at index, + * Get, Remove element, Remove at index, Check if has element and Check if is empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ + #ifndef FLEDASTY_DYNAMIC_ARRAY + #define FLEDASTY_DYNAMIC_ARRAY + + #include + + #include "../Utils/Error.h" + +typedef struct { + size_t size, capacity; + + size_t element_byte_size; + unsigned char *buffer; +} FledastyDynamicArray; + +FledastyError fledasty_dynamic_array_initialize(FledastyDynamicArray *new_dynamic_array, void *values, const size_t values_size, const size_t element_byte_size); +FledastyError fledasty_dynamic_array_destroy(FledastyDynamicArray *current_dynamic_array); + + #endif \ No newline at end of file diff --git a/Src/Core/DynamicArray.c b/Src/Core/DynamicArray.c new file mode 100644 index 0000000..7d902f1 --- /dev/null +++ b/Src/Core/DynamicArray.c @@ -0,0 +1,71 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: DynamicArray.c + * Description: + * This file contains the functions for modifying the Dynamic Array. It includes + * functions to append, Insert before, Insert after, Insert at index, + * Get, Remove element, Remove at index, Check if has element and Check if is empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Core/DynamicArray.h" + +#include +#include +#include + +FledastyError fledasty_dynamic_array_initialize(FledastyDynamicArray *new_dynamic_array, void *values, const size_t values_size, const size_t element_byte_size) { + if (new_dynamic_array == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + new_dynamic_array->element_byte_size = element_byte_size; + if (values == NULL || values_size == 0) { + new_dynamic_array->size = 0; + new_dynamic_array->capacity = 10; + + new_dynamic_array->buffer = (unsigned char*)hallocy_malloc(new_dynamic_array->capacity * element_byte_size); + if (new_dynamic_array->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } else { + new_dynamic_array->size = values_size; + new_dynamic_array->capacity = new_dynamic_array->size + new_dynamic_array->size; + + new_dynamic_array->buffer = (unsigned char*)hallocy_malloc(new_dynamic_array->capacity * element_byte_size); + if (new_dynamic_array->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(new_dynamic_array->buffer, values, values_size * element_byte_size); + } + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_dynamic_array_destroy(FledastyDynamicArray *current_dynamic_array) { + if (current_dynamic_array == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + HallocyError result = hallocy_free(current_dynamic_array->buffer); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + current_dynamic_array->buffer = NULL; + return FLEDASTY_ERROR_NONE; +} \ No newline at end of file diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index f06898e..9b1be7c 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -43,7 +43,7 @@ FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values, } } else { new_queue->size = values_size; - new_queue->capacity = values_size + (values_size / 2); + new_queue->capacity = new_queue->size + new_queue->size; new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size); if (new_queue->buffer == NULL) { diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index b35b517..8988e58 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -42,7 +42,7 @@ FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values, } } else { new_stack->size = values_size; - new_stack->capacity = values_size + (values_size / 2); + new_stack->capacity = new_stack->size + new_stack->size; new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size); if (new_stack->buffer == NULL) { diff --git a/Tests/Main.c b/Tests/Main.c index 383bf48..3a9a95c 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -22,6 +22,7 @@ #include #include #include +#include int main() { FledastyQueue test_queue; @@ -66,6 +67,10 @@ int main() { fledasty_stack_destroy(&test_stack); + FledastyDynamicArray test_dynamic_array; + fledasty_dynamic_array_initialize(&test_dynamic_array, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); + + fledasty_dynamic_array_destroy(&test_dynamic_array); printf("Done\n"); return 0; } \ No newline at end of file