perf(hash table): changed hash table to be defined using macros to improve performance
This commit is contained in:
parent
352281790d
commit
dfb24b1716
3 changed files with 227 additions and 306 deletions
|
|
@ -26,38 +26,220 @@
|
|||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
#include <Hallocy/Utils/Error.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
#include "DynamicArray.h"
|
||||
#include "Hallocy/Core/Memory.h"
|
||||
|
||||
typedef struct {
|
||||
void *key, *value;
|
||||
} FledastyHashTablePair;
|
||||
#define FLEDASTY_HASH_TABLE_DEFINE(key_type, value_type, name, compare_key_function) \
|
||||
typedef struct { \
|
||||
key_type key; \
|
||||
value_type value; \
|
||||
} FledastyHashTablePair_##name; \
|
||||
\
|
||||
FLEDASTY_DYNAMIC_ARRAY_DEFINE(FledastyHashTablePair_##name, fledasty_internal_pair) \
|
||||
\
|
||||
typedef struct { \
|
||||
size_t size, capacity; \
|
||||
FledastyDynamicArray_fledasty_internal_pair *Table; \
|
||||
\
|
||||
size_t (*hash_function)(const key_type key); \
|
||||
} FledastyHashTable_##name; \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_free(FledastyHashTable_##name *current_hash_table); \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *current_hash_table, key_type key, value_type value); \
|
||||
value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key); \
|
||||
FledastyError fledasty_hash_table_##name##_remove(FledastyHashTable_##name *current_hash_table, key_type key); \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_clear(FledastyHashTable_##name *current_hash_table); \
|
||||
FledastyError fledasty_hash_table_##name##_shrink_to_fit(FledastyHashTable_##name *current_hash_table); \
|
||||
\
|
||||
bool fledasty_hash_table_##name##_has_key(const FledastyHashTable_##name *current_hash_table, key_type key); \
|
||||
static inline bool fledasty_hash_table_##name##_is_empty(const FledastyHashTable_##name *current_hash_table) { return current_hash_table == NULL || current_hash_table->size == 0; } \
|
||||
static inline bool fledasty_hash_table_##name##_compare_pair(const FledastyHashTablePair_##name first_pair, const FledastyHashTablePair_##name second_pair) { return compare_key_function(first_pair.key, second_pair.key); }
|
||||
|
||||
FLEDASTY_DYNAMIC_ARRAY_DEFINE(FledastyHashTablePair, pair)
|
||||
|
||||
typedef struct {
|
||||
size_t size, capacity;
|
||||
|
||||
size_t key_byte_size, value_byte_size;
|
||||
FledastyDynamicArray_pair *Table;
|
||||
|
||||
size_t (*hash_function)(const void *key);
|
||||
} FledastyHashTable;
|
||||
|
||||
FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, const size_t key_byte_size, const size_t value_byte_size, size_t (*hash_function)(const void *key));
|
||||
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(const FledastyHashTable *current_hash_table, void *key);
|
||||
FledastyError fledasty_hash_table_remove(FledastyHashTable *current_hash_table, void *key);
|
||||
|
||||
FledastyError fledasty_hash_table_clear(FledastyHashTable *current_hash_table);
|
||||
FledastyError fledasty_hash_table_shrink_to_fit(FledastyHashTable *current_hash_table);
|
||||
|
||||
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key);
|
||||
static inline bool fledasty_hash_table_is_empty(const FledastyHashTable *current_hash_table) { return current_hash_table == NULL || current_hash_table->size == 0; }
|
||||
static inline bool fledasty_hash_table_compare_pair(const FledastyHashTablePair first_pair, const FledastyHashTablePair second_pair) { return first_pair.key == second_pair.key && first_pair.value == second_pair.value; }
|
||||
#define FLEDASTY_HASH_TABLE_IMPLEMENT(key_type, value_type, name, compare_key_function) \
|
||||
FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(FledastyHashTablePair_##name, fledasty_internal_pair, fledasty_hash_table_##name##_compare_pair) \
|
||||
static const int FLEDASTY_HASH_TABLE_SIZE_THRESHOLD = 75; \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_free(FledastyHashTable_##name *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) { \
|
||||
FledastyError result = fledasty_dynamic_array_fledasty_internal_pair_free(¤t_hash_table->Table[i]); \
|
||||
if (result != FLEDASTY_ERROR_NONE) { \
|
||||
return result; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
if (hallocy_free(current_hash_table->Table) != HALLOCY_ERROR_NONE) { \
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION; \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_insert(FledastyHashTable_##name *current_hash_table, key_type key, value_type value) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) { \
|
||||
const size_t old_capacity = current_hash_table->capacity; \
|
||||
FledastyDynamicArray_fledasty_internal_pair *previous_table = current_hash_table->Table; \
|
||||
\
|
||||
current_hash_table->capacity += current_hash_table->capacity ? current_hash_table->capacity : 1024; \
|
||||
current_hash_table->Table = (FledastyDynamicArray_fledasty_internal_pair*)hallocy_calloc(sizeof(FledastyDynamicArray_fledasty_internal_pair), 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) { \
|
||||
FledastyDynamicArray_fledasty_internal_pair *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_##name pair = current_dynamic_array->buffer[list_index]; \
|
||||
\
|
||||
const size_t key_index = current_hash_table->hash_function(pair.key) % current_hash_table->capacity; \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_append(current_hash_table->Table + key_index, pair); \
|
||||
} \
|
||||
\
|
||||
fledasty_dynamic_array_fledasty_internal_pair_free(current_dynamic_array); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
hallocy_free(previous_table); \
|
||||
} \
|
||||
\
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \
|
||||
\
|
||||
FledastyHashTablePair_##name pair = { .key = key, .value = value }; \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_append(¤t_hash_table->Table[index], pair); \
|
||||
\
|
||||
current_hash_table->size += 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
value_type fledasty_hash_table_##name##_get(const FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \
|
||||
if (current_hash_table->Table[index].buffer == NULL) { \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
for (size_t list_index = 0; list_index < current_hash_table->Table[index].size; list_index += 1) { \
|
||||
const FledastyHashTablePair_##name value = current_hash_table->Table[index].buffer[list_index]; \
|
||||
if (compare_key_function(value.key, key)) { \
|
||||
return value.value; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_remove(FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
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; \
|
||||
} \
|
||||
\
|
||||
for (size_t list_index = 0; list_index < current_hash_table->Table[index].size; list_index += 1) { \
|
||||
FledastyHashTablePair_##name value = current_hash_table->Table[index].buffer[list_index]; \
|
||||
if (compare_key_function(value.key, key)) { \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_remove_at_index(¤t_hash_table->Table[index], list_index); \
|
||||
current_hash_table->size -= 1; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_clear(FledastyHashTable_##name *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) { \
|
||||
FledastyError result = fledasty_dynamic_array_fledasty_internal_pair_free(¤t_hash_table->Table[index]); \
|
||||
if (result != FLEDASTY_ERROR_NONE) { \
|
||||
return result; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
current_hash_table->size = 0; \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
FledastyError fledasty_hash_table_##name##_shrink_to_fit(FledastyHashTable_##name *current_hash_table) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return FLEDASTY_ERROR_INVALID_POINTER; \
|
||||
} \
|
||||
\
|
||||
const size_t old_capacity = current_hash_table->capacity; \
|
||||
FledastyDynamicArray_fledasty_internal_pair *previous_table = current_hash_table->Table; \
|
||||
\
|
||||
if (current_hash_table->size == 0) { \
|
||||
current_hash_table->capacity = 1024; \
|
||||
} else { \
|
||||
current_hash_table->capacity = (current_hash_table->size * 100) / FLEDASTY_HASH_TABLE_SIZE_THRESHOLD; \
|
||||
} \
|
||||
\
|
||||
current_hash_table->Table = (FledastyDynamicArray_fledasty_internal_pair*)hallocy_calloc(sizeof(FledastyDynamicArray_fledasty_internal_pair), 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) { \
|
||||
FledastyDynamicArray_fledasty_internal_pair *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_##name pair = current_dynamic_array->buffer[list_index]; \
|
||||
\
|
||||
const size_t key_index = current_hash_table->hash_function(pair.key) % current_hash_table->capacity; \
|
||||
fledasty_dynamic_array_fledasty_internal_pair_append(current_hash_table->Table + key_index, pair); \
|
||||
} \
|
||||
\
|
||||
fledasty_dynamic_array_fledasty_internal_pair_free(current_dynamic_array); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
hallocy_free(previous_table); \
|
||||
return FLEDASTY_ERROR_NONE; \
|
||||
} \
|
||||
\
|
||||
bool fledasty_hash_table_##name##_has_key(const FledastyHashTable_##name *current_hash_table, key_type key) { \
|
||||
if (current_hash_table == NULL) { \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity; \
|
||||
if (current_hash_table->Table[index].buffer == NULL) { \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
for (size_t list_index = 0; list_index < current_hash_table->Table[index].size; list_index += 1) { \
|
||||
const FledastyHashTablePair_##name value = current_hash_table->Table[index].buffer[list_index]; \
|
||||
if (compare_key_function(value.key, key)) { \
|
||||
return true; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,257 +0,0 @@
|
|||
/*
|
||||
* 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>
|
||||
|
||||
FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(FledastyHashTablePair, pair, fledasty_hash_table_compare_pair)
|
||||
|
||||
static const int FLEDASTY_HASH_TABLE_SIZE_THRESHOLD = 75;
|
||||
|
||||
FledastyError fledasty_hash_table_initialize(FledastyHashTable *new_hash_table, const size_t key_byte_size, const size_t value_byte_size, size_t (*hash_function)(const void *key)) {
|
||||
if (new_hash_table == NULL || hash_function == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
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 = (FledastyDynamicArray_pair*)hallocy_calloc(sizeof(FledastyDynamicArray_pair), new_hash_table->capacity);
|
||||
if (new_hash_table->Table == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
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) {
|
||||
FledastyError result = fledasty_dynamic_array_pair_free(¤t_hash_table->Table[i]);
|
||||
if (result != FLEDASTY_ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if (hallocy_free(current_hash_table->Table) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (current_hash_table->size >= (current_hash_table->capacity * FLEDASTY_HASH_TABLE_SIZE_THRESHOLD) / 100) {
|
||||
const size_t old_capacity = current_hash_table->capacity;
|
||||
FledastyDynamicArray_pair *previous_table = current_hash_table->Table;
|
||||
|
||||
current_hash_table->capacity += current_hash_table->capacity;
|
||||
current_hash_table->Table = (FledastyDynamicArray_pair*)hallocy_calloc(sizeof(FledastyDynamicArray_pair), 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) {
|
||||
FledastyDynamicArray_pair *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 = current_dynamic_array->buffer[list_index];
|
||||
|
||||
const size_t key_index = current_hash_table->hash_function(pair.key) % current_hash_table->capacity;
|
||||
fledasty_dynamic_array_pair_append(current_hash_table->Table + key_index, pair);
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_pair_free(current_dynamic_array);
|
||||
}
|
||||
}
|
||||
|
||||
hallocy_free(previous_table);
|
||||
}
|
||||
|
||||
const size_t index = current_hash_table->hash_function(key) % current_hash_table->capacity;
|
||||
|
||||
FledastyHashTablePair pair;
|
||||
pair.key = hallocy_malloc(current_hash_table->key_byte_size);
|
||||
if (pair.key == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(pair.key, key, current_hash_table->key_byte_size);
|
||||
|
||||
pair.value = hallocy_malloc(current_hash_table->value_byte_size);
|
||||
if (pair.value == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(pair.value, value, current_hash_table->value_byte_size);
|
||||
fledasty_dynamic_array_pair_append(¤t_hash_table->Table[index], pair);
|
||||
|
||||
current_hash_table->size += 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
void *fledasty_hash_table_get(const FledastyHashTable *current_hash_table, void *key) {
|
||||
if (current_hash_table == NULL || key == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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) {
|
||||
const FledastyHashTablePair value = current_hash_table->Table[index].buffer[list_index];
|
||||
if (hallocy_compare_memory(value.key, key, current_hash_table->key_byte_size)) {
|
||||
return value.value;
|
||||
}
|
||||
|
||||
list_index += 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
size_t list_index = 0;
|
||||
while (list_index < current_hash_table->Table[index].size) {
|
||||
FledastyHashTablePair value = current_hash_table->Table[index].buffer[list_index];
|
||||
if (hallocy_compare_memory(value.key, key, current_hash_table->key_byte_size)) {
|
||||
if (hallocy_free(value.key) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
if (hallocy_free(value.value) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_pair_remove_at_index(¤t_hash_table->Table[index], list_index);
|
||||
current_hash_table->size -= 1;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
list_index += 1;
|
||||
}
|
||||
|
||||
return FLEDASTY_ERROR_KEY_NOT_FOUND;
|
||||
}
|
||||
|
||||
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) {
|
||||
FledastyError result = fledasty_dynamic_array_pair_free(¤t_hash_table->Table[index]);
|
||||
if (result != FLEDASTY_ERROR_NONE) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_hash_table->size = 0;
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
FledastyError fledasty_hash_table_shrink_to_fit(FledastyHashTable *current_hash_table) {
|
||||
if (current_hash_table == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
const size_t old_capacity = current_hash_table->capacity;
|
||||
FledastyDynamicArray_pair *previous_table = current_hash_table->Table;
|
||||
|
||||
if (current_hash_table->size == 0) {
|
||||
current_hash_table->capacity = 1024;
|
||||
} else {
|
||||
current_hash_table->capacity = (current_hash_table->size * 100) / FLEDASTY_HASH_TABLE_SIZE_THRESHOLD;
|
||||
}
|
||||
|
||||
current_hash_table->Table = (FledastyDynamicArray_pair*)hallocy_calloc(sizeof(FledastyDynamicArray_pair), 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) {
|
||||
FledastyDynamicArray_pair *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 = current_dynamic_array->buffer[list_index];
|
||||
|
||||
const size_t key_index = current_hash_table->hash_function(pair.key) % current_hash_table->capacity;
|
||||
fledasty_dynamic_array_pair_append(current_hash_table->Table + key_index, pair);
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_pair_free(current_dynamic_array);
|
||||
}
|
||||
}
|
||||
|
||||
hallocy_free(previous_table);
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
bool fledasty_hash_table_has_key(const FledastyHashTable *current_hash_table, void *key) {
|
||||
if (current_hash_table == NULL || key == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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) {
|
||||
const FledastyHashTablePair value = current_hash_table->Table[index].buffer[list_index];
|
||||
if (hallocy_compare_memory(value.key, key, current_hash_table->key_byte_size)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
list_index += 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
38
Tests/Main.c
38
Tests/Main.c
|
|
@ -31,7 +31,7 @@
|
|||
#include <Fledasty/Algorithms/Hashing.h>
|
||||
|
||||
static inline bool compare_integers(const int first_value, const int second_value) { return first_value == second_value; }
|
||||
static inline size_t integer_hash_function(const void *key) { return *(int*)key; }
|
||||
static inline size_t integer_hash_function(const int key) { return key; }
|
||||
|
||||
FLEDASTY_STACK_DEFINE(int, int)
|
||||
FLEDASTY_STACK_IMPLEMENT(int, int)
|
||||
|
|
@ -42,6 +42,9 @@ FLEDASTY_QUEUE_IMPLEMENT(int, int)
|
|||
FLEDASTY_DYNAMIC_ARRAY_DEFINE(int, int)
|
||||
FLEDASTY_DYNAMIC_ARRAY_IMPLEMENT(int, int, compare_integers)
|
||||
|
||||
FLEDASTY_HASH_TABLE_DEFINE(int, int, int_int, compare_integers)
|
||||
FLEDASTY_HASH_TABLE_IMPLEMENT(int, int, int_int, compare_integers)
|
||||
|
||||
int main() {
|
||||
FledastyQueue_int test_queue = { 0, 0, 0, 0, NULL };
|
||||
for (int i = 0; i < 10; i += 1) {
|
||||
|
|
@ -191,42 +194,35 @@ int main() {
|
|||
|
||||
fledasty_doubly_list_destroy(&test_doubly_linked_list);
|
||||
|
||||
FledastyHashTable test_hash_table;
|
||||
fledasty_hash_table_initialize(&test_hash_table, sizeof(int), sizeof(int), integer_hash_function);
|
||||
|
||||
FledastyHashTable_int_int test_hash_table = { .size = 0, .capacity = 0, .Table = NULL, .hash_function = (size_t(*)(const 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_int_int_insert(&test_hash_table, i * i, i);
|
||||
}
|
||||
|
||||
fledasty_hash_table_shrink_to_fit(&test_hash_table);
|
||||
fledasty_hash_table_int_int_shrink_to_fit(&test_hash_table);
|
||||
|
||||
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);
|
||||
fledasty_hash_table_int_int_insert(&test_hash_table, 2, 5);
|
||||
fledasty_hash_table_int_int_remove(&test_hash_table, 2);
|
||||
|
||||
if (fledasty_hash_table_get(&test_hash_table, &hash_table_remove) != NULL) {
|
||||
if (fledasty_hash_table_int_int_get(&test_hash_table, 2) != 0) {
|
||||
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);
|
||||
printf("Hash table get: %d\n", *hash_table_value);
|
||||
printf("Hash table get: %d\n", fledasty_hash_table_int_int_get(&test_hash_table, i * i));
|
||||
}
|
||||
|
||||
hash_table_value = 4;
|
||||
if (fledasty_hash_table_has_key(&test_hash_table, &hash_table_value)) {
|
||||
printf("Hash table contains key %d\n", hash_table_value);
|
||||
if (fledasty_hash_table_int_int_has_key(&test_hash_table, 4)) {
|
||||
printf("Hash table contains key 4\n");
|
||||
}
|
||||
|
||||
fledasty_hash_table_clear(&test_hash_table);
|
||||
if (fledasty_hash_table_is_empty(&test_hash_table)) {
|
||||
fledasty_hash_table_int_int_clear(&test_hash_table);
|
||||
if (fledasty_hash_table_int_int_is_empty(&test_hash_table)) {
|
||||
printf("Hash table is empty\n");
|
||||
}
|
||||
|
||||
fledasty_hash_table_destroy(&test_hash_table);
|
||||
fledasty_hash_table_int_int_free(&test_hash_table);
|
||||
|
||||
FledastyUtf8String test_utf8_string;
|
||||
unsigned char *test_string = (unsigned char*)"😀€Testing";
|
||||
fledasty_utf8_string_initialize(&test_utf8_string, test_string, 14);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue