Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfect hash function for strings known in advance

I have 4000 strings and I want to create a perfect hash table with these strings. The strings are known in advance, so my first idea was to use a series of if statements:

 if (name=="aaa")
      return 1;
 else if (name=="bbb")
      return 2;
        .
        .
        .
 // 4000th `if' statement

However, this would be very inefficient. Is there a better way?

like image 918
bambi kirkayak Avatar asked Dec 01 '22 15:12

bambi kirkayak


1 Answers

gperf is a tool that does exactly that:

GNU gperf is a perfect hash function generator. For a given list of strings, it produces a hash function and hash table, in form of C or C++ code, for looking up a value depending on the input string. The hash function is perfect, which means that the hash table has no collisions, and the hash table lookup needs a single string comparison only.

According to the documentation, gperf is used to generate the reserved keyword recogniser for lexers in GNU C, GNU C++, GNU Java, GNU Pascal, GNU Modula 3, and GNU indent.

The way it works is described in GPERF: A Perfect Hash Function Generator by Douglas C. Schmidt.

like image 61
NPE Avatar answered Dec 18 '22 08:12

NPE