From 5556948e2d886ab02acfe7c74f4e48ea9370bfa1 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 1 May 2025 17:16:08 -0500 Subject: [PATCH] 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"); }