refactor(hash table): made small changes in insert function and added const to some variables

This commit is contained in:
Mineplay 2025-05-27 14:10:28 -05:00
parent 0c34e6f63d
commit f86cd1ae39

View file

@ -21,12 +21,10 @@
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/HashTable.h"
#include "Fledasty/Core/DynamicArray.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
#include <stddef.h>
static const int FLEDASTY_HASH_TABLE_SIZE_THRESHOLD = 75;
@ -75,21 +73,22 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
current_hash_table->size += 1;
if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) {
size_t old_capacity = current_hash_table->capacity;
current_hash_table->capacity += current_hash_table->capacity;
const size_t old_capacity = current_hash_table->capacity;
FledastyDynamicArray *previous_table = current_hash_table->Table;
current_hash_table->capacity += current_hash_table->capacity;
current_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), current_hash_table->capacity);
if (current_hash_table->Table == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
for (size_t index = 0; index < old_capacity; index += 1) {
if (previous_table[index].buffer != NULL) {
for (size_t list_index = 0; list_index < previous_table[index].size; list_index += 1) {
FledastyHashTablePair *pair = (FledastyHashTablePair*)fledasty_dynamic_array_get(previous_table + index, list_index);
FledastyDynamicArray *current_dynamic_array = previous_table + index;
if (current_dynamic_array->buffer != NULL) {
for (size_t list_index = 0; list_index < current_dynamic_array->size; list_index += 1) {
FledastyHashTablePair *pair = (FledastyHashTablePair*)fledasty_dynamic_array_get(current_dynamic_array, list_index);
size_t key_index = current_hash_table->hash_function(pair->key) % current_hash_table->capacity;
const size_t key_index = current_hash_table->hash_function(pair->key) % current_hash_table->capacity;
if (current_hash_table->Table[key_index].buffer == NULL) {
FledastyError result = fledasty_dynamic_array_initialize(current_hash_table->Table + key_index, NULL, 0, sizeof(FledastyHashTablePair));
if (result != FLEDASTY_ERROR_NONE) {
@ -100,14 +99,14 @@ FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table,
fledasty_dynamic_array_append(current_hash_table->Table + key_index, pair);
}
fledasty_dynamic_array_destroy(previous_table + index);
fledasty_dynamic_array_destroy(current_dynamic_array);
}
}
hallocy_free(previous_table);
}
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
if (current_hash_table->Table[index].buffer == NULL) {
FledastyError result = fledasty_dynamic_array_initialize(&current_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair));
if (result != FLEDASTY_ERROR_NONE) {
@ -130,8 +129,8 @@ 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);
fledasty_dynamic_array_append(&current_hash_table->Table[index], &pair);
return FLEDASTY_ERROR_NONE;
}
@ -140,14 +139,14 @@ void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void
return NULL;
}
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
const 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[index].size) {
FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(&current_hash_table->Table[index], list_index);
const 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)) {
return value->value;
}
@ -163,7 +162,7 @@ FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table,
return FLEDASTY_ERROR_INVALID_POINTER;
}
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
const 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;
}
@ -214,14 +213,14 @@ bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, vo
return false;
}
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
const 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(&current_hash_table->Table[index], list_index);
const 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)) {
return true;
}