refactor(hash table): improved error checks

This commit is contained in:
Mineplay 2025-05-23 17:42:37 -05:00
parent 892388c218
commit fc5f731397

View file

@ -53,11 +53,13 @@ FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table)
}
for (size_t i = 0; i < current_hash_table->capacity; i += 1) {
fledasty_dynamic_array_destroy(&current_hash_table->Table[i]);
FledastyError result = fledasty_dynamic_array_destroy(&current_hash_table->Table[i]);
if (result != FLEDASTY_ERROR_NONE) {
return result;
}
}
HallocyError result = hallocy_free(current_hash_table->Table);
if (result != HALLOCY_ERROR_NONE) {
if (hallocy_free(current_hash_table->Table) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -75,6 +77,9 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
FledastyDynamicArray *previous_table = current_hash_table->Table;
current_hash_table->Table = (FledastyDynamicArray*)hallocy_realloc(current_hash_table->Table, current_hash_table->capacity * sizeof(FledastyDynamicArray));
if (current_hash_table->Table == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
if (previous_table != current_hash_table->Table) {
hallocy_set_memory(current_hash_table->Table + (current_hash_table->size - 2), 0, current_hash_table->capacity);
@ -83,11 +88,14 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
if (current_hash_table->Table[index].buffer == NULL) {
fledasty_dynamic_array_initialize(&current_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair));
FledastyError result = fledasty_dynamic_array_initialize(&current_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair));
if (result != FLEDASTY_ERROR_NONE) {
return result;
}
}
FledastyHashTablePair pair;
pair.key = hallocy_malloc(current_hash_table->key_byte_size);
if (pair.key == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
@ -101,7 +109,6 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
}
hallocy_copy_memory(pair.value, value, current_hash_table->value_byte_size);
fledasty_dynamic_array_append(&current_hash_table->Table[index], &pair);
return FLEDASTY_ERROR_NONE;
@ -144,13 +151,11 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table,
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)) {
HallocyError result = hallocy_free(value->key);
if (result != HALLOCY_ERROR_NONE) {
if (hallocy_free(value->key) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
result = hallocy_free(value->value);
if (result != HALLOCY_ERROR_NONE) {
if (hallocy_free(value->value) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -172,12 +177,14 @@ FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) {
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]);
FledastyError result = fledasty_dynamic_array_destroy(&current_hash_table->Table[index]);
if (result != FLEDASTY_ERROR_NONE) {
return result;
}
}
}
current_hash_table->size = 0;
return FLEDASTY_ERROR_NONE;
}