Switch hash tables to longs, as longs guarantee 32 bits.

The default FNV hash requires 32 bits at least.
This commit is contained in:
Danila Fedorin 2017-01-27 21:29:28 -08:00
parent 0271248472
commit c8d733e410
2 changed files with 9 additions and 9 deletions

View File

@ -45,7 +45,7 @@ struct ht_s {
* @param input the data being hashed. * @param input the data being hashed.
* @return the data's hash. * @return the data's hash.
*/ */
unsigned int (*hash_func)(void* input); unsigned long (*hash_func)(void* input);
/** /**
* Function to compare two keys. * Function to compare two keys.
* @param given_key the key being used to retrieve data * @param given_key the key being used to retrieve data
@ -76,7 +76,7 @@ typedef struct ht_s ht;
* @param key the data to hash. * @param key the data to hash.
* @return the produced hashed integer. * @return the produced hashed integer.
*/ */
unsigned int ht_default_hash_func(void* key); unsigned long ht_default_hash_func(void* key);
/** /**
* The default key comparison function. * The default key comparison function.
* Assumes both keys are strings and uses strcmp. * Assumes both keys are strings and uses strcmp.

View File

@ -2,10 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
unsigned int ht_default_hash_func(void* data) { unsigned long ht_default_hash_func(void* data) {
const unsigned int fnv_prime = 16777619; const unsigned long fnv_prime = 16777619;
const unsigned int fnv_offset_basis = 2166136261; const unsigned long fnv_offset_basis = 2166136261;
unsigned int hash; unsigned long hash;
char* string; char* string;
hash = fnv_offset_basis; hash = fnv_offset_basis;
string = data; string = data;
@ -48,7 +48,7 @@ void ht_free(ht* ht) {
libds_result ht_put(ht* ht, void* key, void* data) { libds_result ht_put(ht* ht, void* key, void* data) {
libds_result result = LIBDS_SUCCESS; libds_result result = LIBDS_SUCCESS;
ht_node* new_node = NULL; ht_node* new_node = NULL;
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
new_node = malloc(sizeof(*new_node)); new_node = malloc(sizeof(*new_node));
if (new_node) { if (new_node) {
@ -76,7 +76,7 @@ libds_result ht_put(ht* ht, void* key, void* data) {
void* ht_get(ht* ht, void* key) { void* ht_get(ht* ht, void* key) {
void* data = NULL; void* data = NULL;
ht_node* search_node; ht_node* search_node;
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
search_node = ht->data[key_int]; search_node = ht->data[key_int];
while (search_node && data == NULL) { while (search_node && data == NULL) {
@ -90,7 +90,7 @@ void* ht_get(ht* ht, void* key) {
} }
void ht_remove(ht* ht, void* key) { void ht_remove(ht* ht, void* key) {
ht_node** search_ptr; ht_node** search_ptr;
unsigned int key_int = ht->hash_func(key) % LIBDS_HT_SIZE; unsigned long key_int = ht->hash_func(key) % LIBDS_HT_SIZE;
search_ptr = &ht->data[key_int]; search_ptr = &ht->data[key_int];
while (*search_ptr) { while (*search_ptr) {