Friday 15 March 2013

c - Writing a robust integer hash function -


I want to write a good integer hash function for a hash table. Even though I think my hash table will not be too big (say 36 elements), the "key" that generates a hash value can vary greatly in values ​​of 0,20, 31, 11456,13444 etc. . Such questions have been posted here before, and my hash functions are inspired by the proposed answers provided.

The structure of my table is as follows:

  typedef struct _list_t_ {Int key; Intestine value; Struct_list_t_ * Next; } List_t; Typedef struct _hash_table_t_ {integer size; / * Table size * / list_t ** table; / * Table element * /} hash_table_t;   

The following is my current hash function:

  Unsigned integer hash (hash_tables_t * hashtable, int key) {unsigned is fully functional; Hashaval = 0; Hashaval = Key; Hashaval = ((Hashil>> 16) ^ Hashaval) * 0x45d9f3b; Hashaval = ((Hashil>> 16) ^ Hashaval) * 0x45d9f3b; Hashaval = ((Hashil>> 16) ^ Hashaval); Returns is% ofshabelable-> Shape; // MOD is used to keep the table within the size limit}   

As the hash value described above of "key" is generated (from 0, 20, 31, value ). ... 11456,13444 etc.) The problem is that I meditate to create this hash function with the same hash values. Is there any way I can do it more so that there is a high chance of ending with a new hash value.

  unsigned integer hash (hash_table_t * hashtable, int key)   

This is a very rare opportunity to create a perfect hash function. A function that generates a unique value for each specific input value. You probably can not do better in this case because the number of input bits is equal to the number of output bits. Specific hash functions require more input bits and limited number of output bits to be handled. Which creates the mandatory problem of a hash collision. There is no problem with the right hash.

In this case the complete hash function, as always, is trivial:

  unsigned int getslot (hash_tables_t * hashtable, int key) { Return (unsigned) key% hashtable -> Size; }   

Keep in mind that you need to differentiate between the hash function and map that hash to a slot or bucket. I added them in a festival because they are very trivial and gave it a proper name. Also keep in mind that adding any entropy like you is useless, as a result can not be distributed from the original. It only makes sense if you have more input values ​​and they can be correlated.

No comments:

Post a Comment