Switch hash tables to longs, as longs guarantee 32 bits.
The default FNV hash requires 32 bits at least.
This commit is contained in:
parent
0271248472
commit
c8d733e410
|
@ -45,7 +45,7 @@ struct ht_s {
|
|||
* @param input the data being hashed.
|
||||
* @return the data's hash.
|
||||
*/
|
||||
unsigned int (*hash_func)(void* input);
|
||||
unsigned long (*hash_func)(void* input);
|
||||
/**
|
||||
* Function to compare two keys.
|
||||
* @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.
|
||||
* @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.
|
||||
* Assumes both keys are strings and uses strcmp.
|
||||
|
|
14
src/ht.c
14
src/ht.c
|
@ -2,10 +2,10 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
unsigned int ht_default_hash_func(void* data) {
|
||||
const unsigned int fnv_prime = 16777619;
|
||||
const unsigned int fnv_offset_basis = 2166136261;
|
||||
unsigned int hash;
|
||||
unsigned long ht_default_hash_func(void* data) {
|
||||
const unsigned long fnv_prime = 16777619;
|
||||
const unsigned long fnv_offset_basis = 2166136261;
|
||||
unsigned long hash;
|
||||
char* string;
|
||||
hash = fnv_offset_basis;
|
||||
string = data;
|
||||
|
@ -48,7 +48,7 @@ void ht_free(ht* ht) {
|
|||
libds_result ht_put(ht* ht, void* key, void* data) {
|
||||
libds_result result = LIBDS_SUCCESS;
|
||||
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));
|
||||
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* data = NULL;
|
||||
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];
|
||||
while (search_node && data == NULL) {
|
||||
|
@ -90,7 +90,7 @@ void* ht_get(ht* ht, void* key) {
|
|||
}
|
||||
void ht_remove(ht* ht, void* key) {
|
||||
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];
|
||||
while (*search_ptr) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user