From 2afa2131ee799439ca692c840798f110be97258c Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 09:42:11 -0500 Subject: [PATCH 01/12] feat(hash table): added hash table structure and implemented initialize and destroy --- Include/Fledasty/Core/DoublyLinkedList.h | 9 ++-- Include/Fledasty/Core/HashTable.h | 48 +++++++++++++++++++++ Include/Fledasty/Core/LinkedList.h | 4 +- Src/Core/DoublyLinkedList.c | 1 - Src/Core/HashTable.c | 55 ++++++++++++++++++++++++ Tests/Main.c | 8 ++++ 6 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 Include/Fledasty/Core/HashTable.h create mode 100644 Src/Core/HashTable.c diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 5a0575b..7013824 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -12,11 +12,12 @@ * limitations under the License. * * ----------------------------------------------------------------------------- - * File: DoublyLinkedList.c + * File: DoublyLinkedList.h * Description: - * This file contains the functions for modifying the Doubly Linked List. 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. + * This file contains the Doubly Linked List 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 * ----------------------------------------------------------------------------- diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h new file mode 100644 index 0000000..684c8bb --- /dev/null +++ b/Include/Fledasty/Core/HashTable.h @@ -0,0 +1,48 @@ +/* + * 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: HashTable.h + * Description: + * This file contains the HashTable structure and the functions for modifying it. + * It includes functions to get, Insert, Remove, check if has key and check if + * empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_HASH_TABLE +#define FLEDASTY_HASH_TABLE + +#include +#include + +#include "../Utils/Error.h" + +typedef struct { + void *key, *value; +} FledastyHashTablePair; + +typedef struct { + size_t size, capacity; + + size_t element_byte_size; + FledastyHashTablePair **Table; + + size_t (*hash_function)(void *key); +} FledastyHashtable; + +FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)); +FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table); + +#endif \ No newline at end of file diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 718d3fe..7089340 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -14,8 +14,8 @@ * ----------------------------------------------------------------------------- * File: LinkedList.h * Description: - * This file contains the functions for modifying the Linked List. It includes - * functions to append, Insert before, Insert after, Insert at index, + * This file contains the Linked List 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 diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index c1bdb2f..62bf88d 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -22,7 +22,6 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/DoublyLinkedList.h" -#include "Fledasty/Utils/Error.h" #include #include diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c new file mode 100644 index 0000000..ff46de1 --- /dev/null +++ b/Src/Core/HashTable.c @@ -0,0 +1,55 @@ +/* + * 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: HashTable.c + * Description: + * This file contains the functions for modifying the Hash Table. It includes functions + * to get, Insert, Remove, check if has key and check if empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Core/HashTable.h" + +#include +#include +#include + +FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)) { + if (new_hash_table == NULL || hash_function == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + new_hash_table->element_byte_size = element_byte_size; + new_hash_table->hash_function = hash_function; + + new_hash_table->size = 0; + new_hash_table->capacity = 1024; + new_hash_table->Table = (FledastyHashTablePair**)hallocy_calloc(sizeof(FledastyHashTablePair*), new_hash_table->capacity); + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table) { + if (current_hash_table == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + for (size_t i = 0; i < current_hash_table->capacity; i += 1) { + hallocy_free(current_hash_table->Table[i]); + } + + hallocy_free(current_hash_table->Table); + return FLEDASTY_ERROR_NONE; +} \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index f474731..b335fe4 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -25,6 +25,9 @@ #include #include #include +#include + +static inline size_t integer_hash_function(void *key) { return *(size_t*)key; } int main() { FledastyQueue test_queue; @@ -179,6 +182,11 @@ int main() { } fledasty_doubly_list_destroy(&test_doubly_linked_list); + + FledastyHashtable test_hash_table; + fledasty_hash_table_initialize(&test_hash_table, sizeof(int), integer_hash_function); + + fledasty_hash_table_destroy(&test_hash_table); printf("Done\n"); return 0; } \ No newline at end of file From 39010a65bc916943b2547f6aba8a8b74477ff15b Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 10:53:09 -0500 Subject: [PATCH 02/12] feat(hash table): implemented insert function and changed table to dynamic array --- Include/Fledasty/Core/HashTable.h | 13 +++++---- Src/Core/HashTable.c | 45 ++++++++++++++++++++++++++----- Tests/Main.c | 9 +++++-- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 684c8bb..0aae87e 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -28,6 +28,7 @@ #include #include "../Utils/Error.h" +#include "DynamicArray.h" typedef struct { void *key, *value; @@ -36,13 +37,15 @@ typedef struct { typedef struct { size_t size, capacity; - size_t element_byte_size; - FledastyHashTablePair **Table; + size_t key_byte_size, value_byte_size; + FledastyDynamicArray *Table; size_t (*hash_function)(void *key); -} FledastyHashtable; +} FledastyHashTable; -FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)); -FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table); +FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, size_t key_byte_size, size_t value_byte_size, size_t (*hash_function)(void *key)); +FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table); + +FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value); #endif \ No newline at end of file diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index ff46de1..6312365 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -21,35 +21,68 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/HashTable.h" +#include "Fledasty/Core/DynamicArray.h" +#include "Fledasty/Utils/Error.h" #include #include #include -FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)) { +static const int FLEDASTY_HASH_TABLE_SIZE_THRESHOLD = 75; + +FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, size_t key_byte_size, size_t value_byte_size, size_t (*hash_function)(void *key)) { if (new_hash_table == NULL || hash_function == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - new_hash_table->element_byte_size = element_byte_size; + new_hash_table->key_byte_size = key_byte_size; + new_hash_table->value_byte_size = value_byte_size; new_hash_table->hash_function = hash_function; new_hash_table->size = 0; new_hash_table->capacity = 1024; - new_hash_table->Table = (FledastyHashTablePair**)hallocy_calloc(sizeof(FledastyHashTablePair*), new_hash_table->capacity); + new_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), new_hash_table->capacity); return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table) { +FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table) { if (current_hash_table == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } for (size_t i = 0; i < current_hash_table->capacity; i += 1) { - hallocy_free(current_hash_table->Table[i]); + fledasty_dynamic_array_destroy(¤t_hash_table->Table[i]); } hallocy_free(current_hash_table->Table); return FLEDASTY_ERROR_NONE; -} \ No newline at end of file +} + +FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value) { + if (current_hash_table == NULL || key == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_hash_table->size += 1; + if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) { + current_hash_table->capacity += current_hash_table->capacity; + + FledastyDynamicArray *previous_table = current_hash_table->Table; + current_hash_table->Table = (FledastyDynamicArray*)hallocy_realloc(current_hash_table->Table, current_hash_table->capacity * sizeof(FledastyDynamicArray)); + + if (previous_table != current_hash_table->Table) { + hallocy_set_memory(current_hash_table->Table + (current_hash_table->size - 2), 0, current_hash_table->capacity); + } + } + + size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; + if (current_hash_table->Table[index].buffer == NULL) { + fledasty_dynamic_array_initialize(¤t_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair)); + } + + FledastyHashTablePair pair = { .key=key, .value=value }; + fledasty_dynamic_array_append(¤t_hash_table->Table[index], &pair); + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index b335fe4..5aeeb58 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -183,8 +183,13 @@ int main() { fledasty_doubly_list_destroy(&test_doubly_linked_list); - FledastyHashtable test_hash_table; - fledasty_hash_table_initialize(&test_hash_table, sizeof(int), integer_hash_function); + FledastyHashTable test_hash_table; + fledasty_hash_table_initialize(&test_hash_table, sizeof(int), sizeof(int), integer_hash_function); + + for (int i = 0; i < 10; i += 1) { + int pow_value = i * i; + fledasty_hash_table_insert(&test_hash_table, &pow_value, &i); + } fledasty_hash_table_destroy(&test_hash_table); printf("Done\n"); From c83015c52fbf6d7ccc2311beb97aa5685bd7af4a Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 14:51:27 -0500 Subject: [PATCH 03/12] feat(hash table): implemented get function --- Include/Fledasty/Core/HashTable.h | 2 ++ Src/Core/HashTable.c | 30 +++++++++++++++++++++++++++++- Tests/Main.c | 6 ++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 0aae87e..d05f84c 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -48,4 +48,6 @@ FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table) FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value); +void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key); + #endif \ No newline at end of file diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index 6312365..214e062 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -58,6 +58,7 @@ FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table) hallocy_free(current_hash_table->Table); return FLEDASTY_ERROR_NONE; } +#include FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value) { if (current_hash_table == NULL || key == NULL || value == NULL) { @@ -81,8 +82,35 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, fledasty_dynamic_array_initialize(¤t_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair)); } - FledastyHashTablePair pair = { .key=key, .value=value }; + FledastyHashTablePair pair; + + pair.key = hallocy_malloc(current_hash_table->key_byte_size); + hallocy_copy_memory(pair.key, key, current_hash_table->key_byte_size); + + pair.value = hallocy_malloc(current_hash_table->value_byte_size); + hallocy_copy_memory(pair.value, value, current_hash_table->value_byte_size); + fledasty_dynamic_array_append(¤t_hash_table->Table[index], &pair); return FLEDASTY_ERROR_NONE; } + +void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) { + if (current_hash_table == NULL || key == NULL) { + return NULL; + } + + size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; + + size_t list_index = 0; + while (list_index < current_hash_table->Table[list_index].size) { + FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index); + if (value != NULL && hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) { + return value->value; + } + + list_index += 1; + } + + return NULL; +} diff --git a/Tests/Main.c b/Tests/Main.c index 5aeeb58..a50e311 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -191,6 +191,12 @@ int main() { fledasty_hash_table_insert(&test_hash_table, &pow_value, &i); } + for (int i = 0; i < 10; i += 1) { + int pow_value = i * i; + int *hash_table_value = (int*)fledasty_hash_table_get(&test_hash_table, &pow_value); + printf("Hash table get: %d\n", *hash_table_value); + } + fledasty_hash_table_destroy(&test_hash_table); printf("Done\n"); return 0; From 29b62b1df41da98e6f1e71b12fded799a78f935c Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 15:37:55 -0500 Subject: [PATCH 04/12] feat(hash table): implemented remove function --- Include/Fledasty/Core/HashTable.h | 2 +- Include/Fledasty/Utils/Error.h | 1 + Src/Core/HashTable.c | 35 ++++++++++++++++++++++++++++--- Tests/Main.c | 9 ++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index d05f84c..5493434 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -47,7 +47,7 @@ FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table); FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value); - void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key); +FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key); #endif \ No newline at end of file diff --git a/Include/Fledasty/Utils/Error.h b/Include/Fledasty/Utils/Error.h index 0e955ef..e52af8f 100644 --- a/Include/Fledasty/Utils/Error.h +++ b/Include/Fledasty/Utils/Error.h @@ -29,6 +29,7 @@ typedef enum { FLEDASTY_ERROR_INVALID_POINTER = 2, FLEDASTY_ERROR_INDEX_OUT_OF_RANGE = 3, FLEDASTY_ERROR_VALUE_NOT_FOUND = 4, + FLEDASTY_ERROR_KEY_NOT_FOUND = 5, } FledastyError; #endif \ No newline at end of file diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index 214e062..7961359 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -58,7 +58,6 @@ FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table) hallocy_free(current_hash_table->Table); return FLEDASTY_ERROR_NONE; } -#include FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value) { if (current_hash_table == NULL || key == NULL || value == NULL) { @@ -101,11 +100,14 @@ void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) } size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; + if (current_hash_table->Table[index].buffer == NULL) { + return NULL; + } size_t list_index = 0; - while (list_index < current_hash_table->Table[list_index].size) { + while (list_index < current_hash_table->Table[index].size) { FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index); - if (value != NULL && hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) { + if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) { return value->value; } @@ -114,3 +116,30 @@ void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) return NULL; } + +FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key) { + if (current_hash_table == NULL || key == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; + if (current_hash_table->Table[index].buffer == NULL) { + return FLEDASTY_ERROR_KEY_NOT_FOUND; + } + + size_t list_index = 0; + while (list_index < current_hash_table->Table[index].size) { + FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index); + if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) { + hallocy_free(value->key); + hallocy_free(value->value); + + fledasty_dynamic_array_remove_at_index(¤t_hash_table->Table[index], list_index); + return FLEDASTY_ERROR_NONE; + } + + list_index += 1; + } + + return FLEDASTY_ERROR_KEY_NOT_FOUND; +} diff --git a/Tests/Main.c b/Tests/Main.c index a50e311..d8b7601 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -191,6 +191,15 @@ int main() { fledasty_hash_table_insert(&test_hash_table, &pow_value, &i); } + int hash_table_value = 5; + int hash_table_remove = 2; + fledasty_hash_table_insert(&test_hash_table, &hash_table_remove, &hash_table_value); + fledasty_hash_table_remove(&test_hash_table, &hash_table_remove); + + if (fledasty_hash_table_get(&test_hash_table, &hash_table_remove) != NULL) { + printf("Value not removed from hash table\n"); + } + for (int i = 0; i < 10; i += 1) { int pow_value = i * i; int *hash_table_value = (int*)fledasty_hash_table_get(&test_hash_table, &pow_value); From 448f37521dee831eda1a15f335e7dc7656c1f0dc Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 16:20:44 -0500 Subject: [PATCH 05/12] feat(hash table): implemented is empty function --- Include/Fledasty/Core/HashTable.h | 2 ++ Src/Core/HashTable.c | 1 + Tests/Main.c | 4 ++++ 3 files changed, 7 insertions(+) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 5493434..7f052c6 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -50,4 +50,6 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key); FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key); +static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; } + #endif \ No newline at end of file diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index 7961359..a235897 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -135,6 +135,7 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, hallocy_free(value->value); fledasty_dynamic_array_remove_at_index(¤t_hash_table->Table[index], list_index); + current_hash_table->size -= 1; return FLEDASTY_ERROR_NONE; } diff --git a/Tests/Main.c b/Tests/Main.c index d8b7601..a5238da 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -206,6 +206,10 @@ int main() { printf("Hash table get: %d\n", *hash_table_value); } + if (fledasty_hash_table_is_empty(&test_hash_table)) { + printf("Hash table is empty\n"); + } + fledasty_hash_table_destroy(&test_hash_table); printf("Done\n"); return 0; From d6f635cb5aa31f78eaf59010f409cc1a85929d69 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 16:29:56 -0500 Subject: [PATCH 06/12] feat(hash table): implemented has key function --- Include/Fledasty/Core/HashTable.h | 1 + Src/Core/HashTable.c | 25 +++++++++++++++++++++++-- Tests/Main.c | 5 +++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 7f052c6..508d991 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -50,6 +50,7 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key); FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key); +bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key); static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; } #endif \ No newline at end of file diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index a235897..661f918 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -21,8 +21,6 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/HashTable.h" -#include "Fledasty/Core/DynamicArray.h" -#include "Fledasty/Utils/Error.h" #include #include @@ -144,3 +142,26 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, return FLEDASTY_ERROR_KEY_NOT_FOUND; } + +bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key) { + if (current_hash_table == NULL || key == NULL) { + return false; + } + + size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; + if (current_hash_table->Table[index].buffer == NULL) { + return false; + } + + size_t list_index = 0; + while (list_index < current_hash_table->Table[index].size) { + FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index); + if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) { + return true; + } + + list_index += 1; + } + + return false; +} diff --git a/Tests/Main.c b/Tests/Main.c index a5238da..e09ecdd 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -206,6 +206,11 @@ int main() { printf("Hash table get: %d\n", *hash_table_value); } + hash_table_value = 4; + if (fledasty_hash_table_has_key(&test_hash_table, &hash_table_value)) { + printf("Hash table contains key %d\n", hash_table_value); + } + if (fledasty_hash_table_is_empty(&test_hash_table)) { printf("Hash table is empty\n"); } From 5556948e2d886ab02acfe7c74f4e48ea9370bfa1 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 17:16:08 -0500 Subject: [PATCH 07/12] feat(hash table): implemented clear function --- Include/Fledasty/Core/HashTable.h | 2 ++ Src/Core/HashTable.c | 18 ++++++++++++++++++ Tests/Main.c | 1 + 3 files changed, 21 insertions(+) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 508d991..5cf270b 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -50,6 +50,8 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key); FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key); +FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table); + bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key); static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; } diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index 661f918..ace74d5 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -21,6 +21,8 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/HashTable.h" +#include "Fledasty/Core/DynamicArray.h" +#include "Fledasty/Utils/Error.h" #include #include @@ -143,6 +145,22 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, return FLEDASTY_ERROR_KEY_NOT_FOUND; } +FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) { + if (current_hash_table == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + for (size_t index = 0; index < current_hash_table->capacity; index += 1) { + if (current_hash_table->Table[index].buffer != NULL) { + fledasty_dynamic_array_destroy(¤t_hash_table->Table[index]); + } + } + + current_hash_table->size = 0; + + return FLEDASTY_ERROR_NONE; +} + bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key) { if (current_hash_table == NULL || key == NULL) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index e09ecdd..29646b0 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -211,6 +211,7 @@ int main() { printf("Hash table contains key %d\n", hash_table_value); } + fledasty_hash_table_clear(&test_hash_table); if (fledasty_hash_table_is_empty(&test_hash_table)) { printf("Hash table is empty\n"); } From f0c224d87988c95a744b6d5a8aaa0cc87c95e740 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 17:26:52 -0500 Subject: [PATCH 08/12] feat(dynamic array): implemented clear function --- Include/Fledasty/Core/DynamicArray.h | 2 ++ Src/Core/DynamicArray.c | 10 ++++++++++ Src/Core/HashTable.c | 30 +++++++++++++++++++++++----- Tests/Main.c | 1 + 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Include/Fledasty/Core/DynamicArray.h b/Include/Fledasty/Core/DynamicArray.h index ab03b99..1f64b18 100644 --- a/Include/Fledasty/Core/DynamicArray.h +++ b/Include/Fledasty/Core/DynamicArray.h @@ -50,6 +50,8 @@ void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_arr FledastyError fledasty_dynamic_array_remove_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index); FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_dynamic_array, void *value); +FledastyError fledasty_dynamic_array_clear(FledastyDynamicArray *current_dynamic_array); + bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value); inline static bool fledasty_dynamic_array_is_empty(const FledastyDynamicArray *current_dynamic_array) { return current_dynamic_array->size == 0; } diff --git a/Src/Core/DynamicArray.c b/Src/Core/DynamicArray.c index 16c6197..e7e6f7c 100644 --- a/Src/Core/DynamicArray.c +++ b/Src/Core/DynamicArray.c @@ -22,6 +22,7 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/DynamicArray.h" +#include "Fledasty/Utils/Error.h" #include #include @@ -220,6 +221,15 @@ FledastyError fledasty_dynamic_array_remove_value(FledastyDynamicArray *current_ return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_dynamic_array_clear(FledastyDynamicArray *current_dynamic_array) { + if (current_dynamic_array == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_dynamic_array->size = 0; + return FLEDASTY_ERROR_NONE; +} + bool fledasty_dynamic_array_has_value(const FledastyDynamicArray *current_dynamic_array, void *value) { if (current_dynamic_array == NULL) { return false; diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index ace74d5..67f90ef 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -21,8 +21,6 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/HashTable.h" -#include "Fledasty/Core/DynamicArray.h" -#include "Fledasty/Utils/Error.h" #include #include @@ -42,6 +40,9 @@ FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, new_hash_table->size = 0; new_hash_table->capacity = 1024; new_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), new_hash_table->capacity); + if (new_hash_table->Table == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } return FLEDASTY_ERROR_NONE; } @@ -55,7 +56,11 @@ FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table) fledasty_dynamic_array_destroy(¤t_hash_table->Table[i]); } - hallocy_free(current_hash_table->Table); + HallocyError result = hallocy_free(current_hash_table->Table); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + return FLEDASTY_ERROR_NONE; } @@ -84,9 +89,17 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, FledastyHashTablePair pair; pair.key = hallocy_malloc(current_hash_table->key_byte_size); + if (pair.key == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + hallocy_copy_memory(pair.key, key, current_hash_table->key_byte_size); pair.value = hallocy_malloc(current_hash_table->value_byte_size); + if (pair.value == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + hallocy_copy_memory(pair.value, value, current_hash_table->value_byte_size); fledasty_dynamic_array_append(¤t_hash_table->Table[index], &pair); @@ -131,8 +144,15 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, while (list_index < current_hash_table->Table[index].size) { FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index); if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) { - hallocy_free(value->key); - hallocy_free(value->value); + HallocyError result = hallocy_free(value->key); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + result = hallocy_free(value->value); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } fledasty_dynamic_array_remove_at_index(¤t_hash_table->Table[index], list_index); current_hash_table->size -= 1; diff --git a/Tests/Main.c b/Tests/Main.c index 29646b0..fc1d82e 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -101,6 +101,7 @@ int main() { printf("Dynamic array contains %d\n", insert_value); } + fledasty_dynamic_array_clear(&test_dynamic_array); if (fledasty_dynamic_array_is_empty(&test_dynamic_array)) { printf("Dynamic array is empty\n"); } From 0dfa753a56e2f15245ad1b3be2d23c26d6fc6419 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 17:30:53 -0500 Subject: [PATCH 09/12] feat(queue): implemented clear function --- Include/Fledasty/Core/Queue.h | 2 ++ Src/Core/Queue.c | 9 +++++++++ Tests/Main.c | 1 + 3 files changed, 12 insertions(+) diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index 2b88630..9a4b16a 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -43,6 +43,8 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value); void *fledasty_queue_peek(const FledastyQueue *current_queue); void *fledasty_queue_pop(FledastyQueue *current_queue); +FledastyError fledasty_queue_clear(FledastyQueue *current_queue); + static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; } #endif diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index 4206615..1fc4686 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -125,3 +125,12 @@ void *fledasty_queue_pop(FledastyQueue *current_queue) { return value_address; } + +FledastyError fledasty_queue_clear(FledastyQueue *current_queue) { + if (current_queue == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_queue->size = 0; + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index fc1d82e..cb241b1 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -45,6 +45,7 @@ int main() { printf("Queue popped: %d\n", *popped_data); } + fledasty_queue_clear(&test_queue); if (fledasty_queue_is_empty(&test_queue)) { printf("Queue is empty\n"); } From 3c8cc19cab972b39b1f93550c16b915249bcc648 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 17:32:26 -0500 Subject: [PATCH 10/12] feat(stack): implemented clear function --- Include/Fledasty/Core/Stack.h | 2 ++ Src/Core/Stack.c | 9 +++++++++ Tests/Main.c | 1 + 3 files changed, 12 insertions(+) diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index ca5349d..bd17de3 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -42,6 +42,8 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value); void *fledasty_stack_peek(const FledastyStack *current_stack); void *fledasty_stack_pop(FledastyStack *current_stack); +FledastyError fledasty_stack_clear(FledastyStack *current_stack); + static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; } #endif diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index fd13ec9..916ff04 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -107,3 +107,12 @@ void *fledasty_stack_pop(FledastyStack *current_stack) { return value_address; } + +FledastyError fledasty_stack_clear(FledastyStack *current_stack) { + if (current_stack == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_stack->size = 0; + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index cb241b1..3489d3b 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -67,6 +67,7 @@ int main() { printf("Stack popped: %d\n", *popped_data); } + fledasty_stack_clear(&test_stack); if (fledasty_stack_is_empty(&test_stack)) { printf("Stack is empty\n"); } From 8a8909ab7f53a69faa122afe4723e444573a84a9 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 2 May 2025 06:13:04 -0500 Subject: [PATCH 11/12] feat(linked list): implemented clear function --- Include/Fledasty/Core/HashTable.h | 6 ++--- Include/Fledasty/Core/LinkedList.h | 2 ++ Src/Core/DoublyLinkedList.c | 6 ++--- Src/Core/HashTable.c | 4 ++-- Src/Core/LinkedList.c | 35 +++++++++++++++++++++++++++--- Tests/Main.c | 4 ++-- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 5cf270b..1ccd35b 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -47,12 +47,12 @@ FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table); FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value); -void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key); +void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key); FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key); FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table); -bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key); -static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; } +bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key); +static inline bool fledasty_hash_table_is_empty(const FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; } #endif \ No newline at end of file diff --git a/Include/Fledasty/Core/LinkedList.h b/Include/Fledasty/Core/LinkedList.h index 7089340..7505a73 100644 --- a/Include/Fledasty/Core/LinkedList.h +++ b/Include/Fledasty/Core/LinkedList.h @@ -51,6 +51,8 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_linked_list, const size_t index); FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_linked_list, void *value); +FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list); + bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value); static inline bool fledasty_linked_list_is_empty(const FledastyLinkedList *current_linked_list) { return current_linked_list->size == 0; } diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 62bf88d..967bb1c 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -76,12 +76,12 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou current_node = current_node->next; HallocyError result = hallocy_free(current_node->previous->value); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } result = hallocy_free(current_node->previous); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } @@ -312,7 +312,7 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList } } - hallocy_free(current_node->next); + hallocy_free(current_node->value); hallocy_free(current_node); current_doubly_linked_list->size -= 1; diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index 67f90ef..8e92a31 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -107,7 +107,7 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, return FLEDASTY_ERROR_NONE; } -void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) { +void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key) { if (current_hash_table == NULL || key == NULL) { return NULL; } @@ -181,7 +181,7 @@ FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) { return FLEDASTY_ERROR_NONE; } -bool fledasty_hash_table_has_key(FledastyHashTable *current_hash_table, void *key) { +bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key) { if (current_hash_table == NULL || key == NULL) { return false; } diff --git a/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index b13864c..76d90e1 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -74,12 +74,12 @@ FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_li current_node = current_node->next; HallocyError result = hallocy_free(previous_node->value); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } result = hallocy_free(previous_node); - if (result == HALLOCY_ERROR_NONE) { + if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } @@ -265,13 +265,42 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link previous_node->next = current_node->next; } - hallocy_free(current_node->next); + hallocy_free(current_node->value); hallocy_free(current_node); current_linked_list->size -= 1; return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list) { + if (current_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyLinkedListNode *previous_node = NULL; + FledastyLinkedListNode *current_node = current_linked_list->start; + while (current_node != NULL) { + previous_node = current_node; + current_node = current_node->next; + + HallocyError result = hallocy_free(previous_node->value); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + result = hallocy_free(previous_node); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + current_linked_list->size = 0; + current_linked_list->start = NULL; + current_linked_list->end = NULL; + + return FLEDASTY_ERROR_NONE; +} + bool fledasty_linked_list_has_value(const FledastyLinkedList *current_linked_list, void *value) { if (current_linked_list == NULL || value == NULL) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index 3489d3b..2404d84 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -139,15 +139,15 @@ int main() { printf("Linked list contains %d\n", insert_value); } + fledasty_linked_list_clear(&test_linked_list); if (fledasty_linked_list_is_empty(&test_linked_list)) { printf("Linked list is empty\n"); } fledasty_linked_list_destroy(&test_linked_list); - + FledastyDoublyLinkedList test_doubly_linked_list; fledasty_doubly_linked_list_initialize(&test_doubly_linked_list, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); - for (int i = 0; i < 10; i += 1) { fledasty_doubly_linked_list_append(&test_doubly_linked_list, &i); } From 52a81e6aee57bf1bf74e777738115bf7f1c32458 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 2 May 2025 06:19:22 -0500 Subject: [PATCH 12/12] feat(doubly linked list): implemented clear function --- Include/Fledasty/Core/DoublyLinkedList.h | 2 ++ Src/Core/DoublyLinkedList.c | 42 +++++++++++++++++++----- Tests/Main.c | 1 + 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Include/Fledasty/Core/DoublyLinkedList.h b/Include/Fledasty/Core/DoublyLinkedList.h index 7013824..c588131 100644 --- a/Include/Fledasty/Core/DoublyLinkedList.h +++ b/Include/Fledasty/Core/DoublyLinkedList.h @@ -52,6 +52,8 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedList *current_doubly_linked_list, const size_t index); FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList *current_doubly_linked_list, void *value); +FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list); + bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value); static inline bool fledasty_doubly_linked_list_is_empty(const FledastyDoublyLinkedList *current_doubly_linked_list) { return current_doubly_linked_list->size == 0; } diff --git a/Src/Core/DoublyLinkedList.c b/Src/Core/DoublyLinkedList.c index 967bb1c..0c06d23 100644 --- a/Src/Core/DoublyLinkedList.c +++ b/Src/Core/DoublyLinkedList.c @@ -71,26 +71,23 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou return FLEDASTY_ERROR_INVALID_POINTER; } + FledastyDoublyLinkedListNode *previous_node = NULL; FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start; - while (current_node->next != NULL) { + while (current_node != NULL) { + previous_node = current_node; current_node = current_node->next; - HallocyError result = hallocy_free(current_node->previous->value); + HallocyError result = hallocy_free(previous_node->value); if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - result = hallocy_free(current_node->previous); + result = hallocy_free(previous_node); if (result != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } - if (current_node != NULL) { - hallocy_free(current_node->value); - hallocy_free(current_node); - } - return FLEDASTY_ERROR_NONE; } @@ -319,6 +316,35 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList return FLEDASTY_ERROR_NONE; } +FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *current_doubly_linked_list) { + if (current_doubly_linked_list == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyDoublyLinkedListNode *previous_node = NULL; + FledastyDoublyLinkedListNode *current_node = current_doubly_linked_list->start; + while (current_node != NULL) { + previous_node = current_node; + current_node = current_node->next; + + HallocyError result = hallocy_free(previous_node->value); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + result = hallocy_free(previous_node); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } + + current_doubly_linked_list->size = 0; + current_doubly_linked_list->start = NULL; + current_doubly_linked_list->end = NULL; + + return FLEDASTY_ERROR_NONE; +} + bool fledasty_doubly_linked_list_has_value(const FledastyDoublyLinkedList *current_doubly_linked_list, void *value) { if (current_doubly_linked_list == NULL || value == NULL) { return false; diff --git a/Tests/Main.c b/Tests/Main.c index 2404d84..f0cd693 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -180,6 +180,7 @@ int main() { printf("Doubly linked list contains %d\n", insert_value); } + fledasty_doubly_linked_list_clear(&test_doubly_linked_list); if (fledasty_doubly_linked_list_is_empty(&test_doubly_linked_list)) { printf("Doubly linked list is empty\n"); }