feat(hash table): implemented remove function

This commit is contained in:
Mineplay 2025-05-01 15:37:55 -05:00
parent c83015c52f
commit 29b62b1df4
4 changed files with 43 additions and 4 deletions

View file

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

View file

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

View file

@ -58,7 +58,6 @@ FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table)
hallocy_free(current_hash_table->Table);
return FLEDASTY_ERROR_NONE;
}
#include <stdio.h>
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(&current_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(&current_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(&current_hash_table->Table[index], list_index);
return FLEDASTY_ERROR_NONE;
}
list_index += 1;
}
return FLEDASTY_ERROR_KEY_NOT_FOUND;
}

View file

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