I want to write a good integer hash function for a hash table. Even though I suspect my hash table will not be too large (say of size 36 elements), the "key" which generates the hash value can vary drastically the values ranging from 0,20, 31, .... 11456,13444 etc. Similar questions have been posted here before, and my hash function is inspired from on the proposed answers provided here.
Following is the structure of my table:
typedef struct _list_t_ {
int key;
int value;
struct _list_t_ *next;
} list_t;
typedef struct _hash_table_t_ {
int size; /* the size of the table */
list_t **table; /* the table elements */
} hash_table_t;
Following is my current hash function:
unsigned int hash(hash_table_t *hashtable, int key)
{
unsigned int hashval;
hashval = 0;
hashval = key;
hashval = ((hashval >> 16) ^ hashval) * 0x45d9f3b;
hashval = ((hashval >> 16) ^ hashval) * 0x45d9f3b;
hashval = ((hashval >> 16) ^ hashval);
return hashval % hashtable->size; // MOD done to keep within the range of the table size
}
As mentioned above the "key" which generates the hash value varies drastically (values ranging from 0,20, 31, .... 11456,13444 etc.). The problem is that I notice this hash function generating the same hash value very frequently. Is there a way I can tweak it so that the chances of ending with a new hash value are more.