Here's the problem:
X is a positive integer (include 0) set which has n different elements I know in advance. All of them is less equal than m. And I want to have an occ-free hash function as simple as possible to map them to 0-n-1.
For example:
X = [31,223,121,100,123,71], so n = 6, m = 223.
I want to find a hash function to map them to [0, 1, 2, 3, 4, 5].
If mapping to 0-n-1 is too difficult, then how to mapping X to a small range is also a problem.
Finding such a function is not too difficult, but to be simple and easy to be generated is hard.
It's better to preserve the order of the X.
Any clues?
My favorite perfect hash is pretty easy.
The hash function you generate has the form:
h1andh2are randomly generated hash functions. In your case, you can generate random constants and then haveh1(key)=key*C1/mandh2(key)=key*C2/mor something similarly simpleTo generated the perfect hash:
table1slots andtable2slots as vertices and an edge for each key betweentable1[h1(key)%N]andtable2[h2(key)%N]. Run a DFS to see if the graph is acyclic. If not, go back to step 1.table1andtable2however you like to give it whateverhashyou like.That's it. All of steps (2), (3) and (4) can be combined into a single DFS traversal pretty easily.
The complete description and analysis is in this paper.