I need to construct a perfect hash function that maps a set of integer [1..2^64 - 1] to itself (this function is actually some some complex permutation).
To explain this problem suppose that we have sequence of integer primary keys in database. We need to show construct a number (which we show to users) in a way that close numbers corresponds to primary keys that as far from one another as possible.
So, basically I need a bijective function for large set of integers. E.g.
Any suggestions or references will be appreciated.
To space out any two numbers maximally in a space from 0 to upperlimit
(excluding) i would set their distance to roughly one-half of upperlimit
.
In python it would look like this (code only works if upperlimit
is even, otherwise the last element collides):
def my_hash(n, upperlimit):
return n * upperlimit / 2 % upperlimit + n / 2
def my_unhash(n, upperlimit):
return n % (upperlimit / 2) * 2 + n / (upperlimit / 2)
Example result:
upperlimit = 16
for i in range(upperlimit):
h = my_hash(i, upperlimit)
u = my_unhash(h, upperlimit)
print "%02d -> %02d -> %02d" % (i, h, u)
00 -> 00 -> 00
01 -> 08 -> 01
02 -> 01 -> 02
03 -> 09 -> 03
04 -> 02 -> 04
05 -> 10 -> 05
06 -> 03 -> 06
07 -> 11 -> 07
08 -> 04 -> 08
09 -> 12 -> 09
10 -> 05 -> 10
11 -> 13 -> 11
12 -> 06 -> 12
13 -> 14 -> 13
14 -> 07 -> 14
15 -> 15 -> 15
The second column shows the hashed values. You can exclude the 0 if you want, because it maps to itself.