2025-05-01 09:42:11 -05:00
|
|
|
/*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*
|
|
|
|
|
* -----------------------------------------------------------------------------
|
|
|
|
|
* File: HashTable.c
|
|
|
|
|
* Description:
|
|
|
|
|
* This file contains the functions for modifying the Hash Table. It includes functions
|
|
|
|
|
* to get, Insert, Remove, check if has key and check if empty.
|
|
|
|
|
*
|
|
|
|
|
* Author: Mineplay
|
|
|
|
|
* -----------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
#include "../../Include/Fledasty/Core/HashTable.h"
|
|
|
|
|
|
|
|
|
|
#include <Hallocy/Core/Allocator.h>
|
|
|
|
|
#include <Hallocy/Core/Memory.h>
|
|
|
|
|
#include <Hallocy/Utils/Error.h>
|
|
|
|
|
|
2025-05-01 10:53:09 -05:00
|
|
|
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)) {
|
2025-05-01 09:42:11 -05:00
|
|
|
if (new_hash_table == NULL || hash_function == NULL) {
|
|
|
|
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 10:53:09 -05:00
|
|
|
new_hash_table->key_byte_size = key_byte_size;
|
|
|
|
|
new_hash_table->value_byte_size = value_byte_size;
|
2025-05-01 09:42:11 -05:00
|
|
|
new_hash_table->hash_function = hash_function;
|
|
|
|
|
|
|
|
|
|
new_hash_table->size = 0;
|
|
|
|
|
new_hash_table->capacity = 1024;
|
2025-05-01 10:53:09 -05:00
|
|
|
new_hash_table->Table = (FledastyDynamicArray*)hallocy_calloc(sizeof(FledastyDynamicArray), new_hash_table->capacity);
|
2025-05-01 17:26:52 -05:00
|
|
|
if (new_hash_table->Table == NULL) {
|
|
|
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
|
|
|
}
|
2025-05-01 09:42:11 -05:00
|
|
|
|
|
|
|
|
return FLEDASTY_ERROR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 10:53:09 -05:00
|
|
|
FledastyError fledasty_hash_table_destroy(FledastyHashTable *current_hash_table) {
|
2025-05-01 09:42:11 -05:00
|
|
|
if (current_hash_table == NULL) {
|
|
|
|
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < current_hash_table->capacity; i += 1) {
|
2025-05-01 10:53:09 -05:00
|
|
|
fledasty_dynamic_array_destroy(¤t_hash_table->Table[i]);
|
2025-05-01 09:42:11 -05:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 17:26:52 -05:00
|
|
|
HallocyError result = hallocy_free(current_hash_table->Table);
|
|
|
|
|
if (result != HALLOCY_ERROR_NONE) {
|
|
|
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 09:42:11 -05:00
|
|
|
return FLEDASTY_ERROR_NONE;
|
2025-05-01 10:53:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 14:51:27 -05:00
|
|
|
FledastyHashTablePair pair;
|
|
|
|
|
|
|
|
|
|
pair.key = hallocy_malloc(current_hash_table->key_byte_size);
|
2025-05-01 17:26:52 -05:00
|
|
|
if (pair.key == NULL) {
|
|
|
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 14:51:27 -05:00
|
|
|
hallocy_copy_memory(pair.key, key, current_hash_table->key_byte_size);
|
|
|
|
|
|
|
|
|
|
pair.value = hallocy_malloc(current_hash_table->value_byte_size);
|
2025-05-01 17:26:52 -05:00
|
|
|
if (pair.value == NULL) {
|
|
|
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 14:51:27 -05:00
|
|
|
hallocy_copy_memory(pair.value, value, current_hash_table->value_byte_size);
|
|
|
|
|
|
2025-05-01 10:53:09 -05:00
|
|
|
fledasty_dynamic_array_append(¤t_hash_table->Table[index], &pair);
|
|
|
|
|
|
|
|
|
|
return FLEDASTY_ERROR_NONE;
|
|
|
|
|
}
|
2025-05-01 14:51:27 -05:00
|
|
|
|
|
|
|
|
void *fledasty_hash_table_get(FledastyHashTable *current_hash_table, void *key) {
|
|
|
|
|
if (current_hash_table == NULL || key == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
|
2025-05-01 15:37:55 -05:00
|
|
|
if (current_hash_table->Table[index].buffer == NULL) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-05-01 14:51:27 -05:00
|
|
|
|
|
|
|
|
size_t list_index = 0;
|
2025-05-01 15:37:55 -05:00
|
|
|
while (list_index < current_hash_table->Table[index].size) {
|
2025-05-01 14:51:27 -05:00
|
|
|
FledastyHashTablePair *value = (FledastyHashTablePair*)fledasty_dynamic_array_get(¤t_hash_table->Table[index], list_index);
|
2025-05-01 15:37:55 -05:00
|
|
|
if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) {
|
2025-05-01 14:51:27 -05:00
|
|
|
return value->value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list_index += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2025-05-01 15:37:55 -05:00
|
|
|
|
|
|
|
|
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(¤t_hash_table->Table[index], list_index);
|
|
|
|
|
if (hallocy_compare_memory(value->key, key, current_hash_table->key_byte_size)) {
|
2025-05-01 17:26:52 -05:00
|
|
|
HallocyError result = hallocy_free(value->key);
|
|
|
|
|
if (result != HALLOCY_ERROR_NONE) {
|
|
|
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = hallocy_free(value->value);
|
|
|
|
|
if (result != HALLOCY_ERROR_NONE) {
|
|
|
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
|
|
|
}
|
2025-05-01 15:37:55 -05:00
|
|
|
|
|
|
|
|
fledasty_dynamic_array_remove_at_index(¤t_hash_table->Table[index], list_index);
|
2025-05-01 16:20:44 -05:00
|
|
|
current_hash_table->size -= 1;
|
2025-05-01 15:37:55 -05:00
|
|
|
return FLEDASTY_ERROR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list_index += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FLEDASTY_ERROR_KEY_NOT_FOUND;
|
|
|
|
|
}
|
2025-05-01 16:29:56 -05:00
|
|
|
|
2025-05-01 17:16:08 -05:00
|
|
|
FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table) {
|
|
|
|
|
if (current_hash_table == NULL) {
|
|
|
|
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t index = 0; index < current_hash_table->capacity; index += 1) {
|
|
|
|
|
if (current_hash_table->Table[index].buffer != NULL) {
|
|
|
|
|
fledasty_dynamic_array_destroy(¤t_hash_table->Table[index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_hash_table->size = 0;
|
|
|
|
|
|
|
|
|
|
return FLEDASTY_ERROR_NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 16:29:56 -05:00
|
|
|
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;
|
|
|
|
|
}
|