Hash tables are one of the most powerful ways for searching data based on a key. The main issue with traditional hash function is the hash value collisions:
The search algorithm must recheck the key against a list of element sharing the same hash. This can cause some major performance issues:Some hash values remains unused (e.g. 1 here) while other are heavily loaded
To avoid this issue, a new family of algorithms have been developed to create minimal perfect hash functions:Perfect because for a predefined set of keys there is no collision
With such hash functions, the hash table can be limited to a simple pointer array. The search time will always be constant. However, if the set of keys changes (by adding a record for example) then the hash function must be regenerated.
The following C# project is an adaptation of the CMPH implementation of the “Compress, Hash and Displace” algorithm:
The code is in full C# and has been tested with Win32 and Win64.
2013-03-01 Edit: I received the following email from Dr. Amjad Daoud asking me to quote that part of the CHD Algorithm is very close/identical to his own CACM1992 Algorithm II he published in his thesis in 1992, 17 years before the CDH paper from F. C. Botelho, D. Belazzougui and M. Dietzfelbinger. For more information, please see this page.