Let's say I want to build a perfect hash table to search for an array where the predefined keys are 12 months, so I would like to
hash("January")==0
hash("December")==11
I run the names of the Month via gperf and get a good hash function, but it seems to have issued 16 buckets (or rather, the range is 16)!
#define MIN_HASH_VALUE 3
#define MAX_HASH_VALUE 18
Looking at the generated gperf code, its function hash code simply returns a len plus char to search for a value from a table of size 256. Somehow, in my head I introduced a fantastic function ... :)
What if I want exactly 12 buckets (that is, I don’t want to skip unused buckets)? For small sets like this, it really doesn't matter, but when I have 1000 predefined keys and you want exactly 1000 buckets in a row?
Is there a deterministic way to do this?
source
share