diff --git a/Include/Fledasty/Core/HashTable.h b/Include/Fledasty/Core/HashTable.h index 684c8bb..0aae87e 100644 --- a/Include/Fledasty/Core/HashTable.h +++ b/Include/Fledasty/Core/HashTable.h @@ -28,6 +28,7 @@ #include #include "../Utils/Error.h" +#include "DynamicArray.h" typedef struct { void *key, *value; @@ -36,13 +37,15 @@ typedef struct { typedef struct { size_t size, capacity; - size_t element_byte_size; - FledastyHashTablePair **Table; + size_t key_byte_size, value_byte_size; + FledastyDynamicArray *Table; size_t (*hash_function)(void *key); -} FledastyHashtable; +} FledastyHashTable; -FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)); -FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table); +FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, size_t key_byte_size, size_t value_byte_size, size_t (*hash_function)(void *key)); +FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table); + +FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value); #endif \ No newline at end of file diff --git a/Src/Core/HashTable.c b/Src/Core/HashTable.c index ff46de1..6312365 100644 --- a/Src/Core/HashTable.c +++ b/Src/Core/HashTable.c @@ -21,35 +21,68 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/HashTable.h" +#include "Fledasty/Core/DynamicArray.h" +#include "Fledasty/Utils/Error.h" #include #include #include -FledastyError fledasty_hash_table_initialize(FledastyHashtable *new_hash_table, size_t element_byte_size, size_t (*hash_function)(void *key)) { +static const int FLEDASTY_HASH_TABLE_SIZE_THRESHOLD = 75; + +FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, size_t key_byte_size, size_t value_byte_size, size_t (*hash_function)(void *key)) { if (new_hash_table == NULL || hash_function == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - new_hash_table->element_byte_size = element_byte_size; + new_hash_table->key_byte_size = key_byte_size; + new_hash_table->value_byte_size = value_byte_size; new_hash_table->hash_function = hash_function; new_hash_table->size = 0; new_hash_table->capacity = 1024; - new_hash_table->Table = (FledastyHashTablePair**)hallocy_calloc(sizeof(FledastyHashTablePair*), new_hash_table->capacity); + new_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), new_hash_table->capacity); return FLEDASTY_ERROR_NONE; } -FledastyError fledasty_hash_table_destroy(FledastyHashtable *current_hash_table) { +FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table) { if (current_hash_table == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } for (size_t i = 0; i < current_hash_table->capacity; i += 1) { - hallocy_free(current_hash_table->Table[i]); + fledasty_dynamic_array_destroy(¤t_hash_table->Table[i]); } hallocy_free(current_hash_table->Table); return FLEDASTY_ERROR_NONE; -} \ No newline at end of file +} + +FledastyError fledasty_hash_table_insert(FledastyHashTable *current_hash_table, void *key, void *value) { + if (current_hash_table == NULL || key == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + current_hash_table->size += 1; + if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) { + current_hash_table->capacity += current_hash_table->capacity; + + 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 (previous_table != current_hash_table->Table) { + hallocy_set_memory(current_hash_table->Table + (current_hash_table->size - 2), 0, current_hash_table->capacity); + } + } + + 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(¤t_hash_table->Table[index], NULL, 0, sizeof(FledastyHashTablePair)); + } + + FledastyHashTablePair pair = { .key=key, .value=value }; + fledasty_dynamic_array_append(¤t_hash_table->Table[index], &pair); + + return FLEDASTY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index b335fe4..5aeeb58 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -183,8 +183,13 @@ int main() { fledasty_doubly_list_destroy(&test_doubly_linked_list); - FledastyHashtable test_hash_table; - fledasty_hash_table_initialize(&test_hash_table, sizeof(int), integer_hash_function); + FledastyHashTable test_hash_table; + fledasty_hash_table_initialize(&test_hash_table, sizeof(int), sizeof(int), integer_hash_function); + + for (int i = 0; i < 10; i += 1) { + int pow_value = i * i; + fledasty_hash_table_insert(&test_hash_table, &pow_value, &i); + } fledasty_hash_table_destroy(&test_hash_table); printf("Done\n");