feat(hash table): implemented has key function
This commit is contained in:
parent
448f37521d
commit
d6f635cb5a
3 changed files with 29 additions and 2 deletions
|
|
@ -50,6 +50,7 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
|
||||||
void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key);
|
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_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; }
|
static inline bool fledasty_hash_table_is_empty(FledastyHashTable *current_hash_table) { return current_hash_table->size == 0; }
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -21,8 +21,6 @@
|
||||||
* -----------------------------------------------------------------------------
|
* -----------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
#include "../../Include/Fledasty/Core/HashTable.h"
|
#include "../../Include/Fledasty/Core/HashTable.h"
|
||||||
#include "Fledasty/Core/DynamicArray.h"
|
|
||||||
#include "Fledasty/Utils/Error.h"
|
|
||||||
|
|
||||||
#include <Hallocy/Core/Allocator.h>
|
#include <Hallocy/Core/Allocator.h>
|
||||||
#include <Hallocy/Core/Memory.h>
|
#include <Hallocy/Core/Memory.h>
|
||||||
|
|
@ -144,3 +142,26 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table,
|
||||||
|
|
||||||
return FLEDASTY_ERROR_KEY_NOT_FOUND;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -206,6 +206,11 @@ int main() {
|
||||||
printf("Hash table get: %d\n", *hash_table_value);
|
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)) {
|
if (fledasty_hash_table_is_empty(&test_hash_table)) {
|
||||||
printf("Hash table is empty\n");
|
printf("Hash table is empty\n");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue