Creating the perfect hash (all consecutive buckets full), gperf or alternatives?

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
/* maximum key range = 16, duplicates = 0 */

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?

+3
source share
2 answers

The only gperf alternative I know is cmph: http://cmph.sourceforge.net/ , but as Jerome said in the comment, having 16 buckets, you can have some speed advantages.

, CiteseerX, . , gperf cmph , , .

+4

gperf. gperf, , , . cmph, . , C . , ( " " ), . Google mph. mph , . C, "emitc", ,

 mph < systemdictionaryfile | emitc > output.c

( 200 000 ) C, . , . .

+4

Source: https://habr.com/ru/post/1723462/


All Articles