feat(hash table): implemented clear function

This commit is contained in:
Mineplay 2025-05-01 17:16:08 -05:00
parent d6f635cb5a
commit 5556948e2d
3 changed files with 21 additions and 0 deletions

View file

@ -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; }

View file

@ -21,6 +21,8 @@
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/HashTable.h"
#include "Fledasty/Core/DynamicArray.h"
#include "Fledasty/Utils/Error.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
@ -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(&current_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;

View file

@ -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");
}