How to create an effective static hash table?

I need to create medium sized static hash tables from it. As a rule, they will have 5-100 entries. When the hash table is created, all key hashes are known in advance (that is, the keys have already been hashed.) I am currently creating a HashMap that sorts the keys, so I get an O (log n) search, which 3-5 searches on average in size that excite me. Wikipediaclaims that a simple hash table with a chain will lead to an average search result of 3 tables for the full table, so this is not a problem for me (i.e. take the% n hash as the first record and make the chain). Given that I know all the hashes at the beginning, it seems like there should be an easy way to get a fast, static perfect hash - but I just couldn't find a good pointer. That is, O (1) access is amortized without additional (insignificant?) Additional overhead. How to implement such a static table?

Memory usage is important, so the less I need to store, the better.

Edit: Please note that this is normal if I need to resolve one collision or so manually. That is, if I could make some chain, which, on average, had direct access and, for example, 3 items, for example, a fine. It is not that I need a perfect hash.

+5
source share
3 answers

For c or c ++ you can use gperf

GNU gperf is an ideal hash generator. For this list of strings, it creates a hash function and a hash table in the form of C or C ++ code to find the value depending on the input string. The hash function is perfect, which means the hash table has no collisions, and searching the hash table requires only one string comparison.

GNU gperf . C ++, switch ifs - , gperf.

+3

C , :

swich (hash_string(*p))
{
case HASH_S16("test"):
    ...
    break;
case HASH_S256("An example with a long text!!!!!!!!!!!!!!!!!"):
    ...
    break;
}

@http://www.heeden.nl/statichashc.htm

+3

You can use Sux4j to create a minimal perfect hash in Java or C ++. (I'm not sure you use Java, but you mentioned HashMap, so I assume.) For C, you can use cmph .

0
source

All Articles