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