Is it possible to make a minimal perfect hash function in this situation?

前端 未结 4 739
孤独总比滥情好
孤独总比滥情好 2021-02-15 11:07

I want to create a Hash Map (or another structure, if you have any suggestions) to store key value pairs. The keys will all be inserted at once at the same time as the map is cr

4条回答
  •  悲哀的现实
    2021-02-15 12:06

    Try GPL'd gperf, or Bob Jenkins' public domain implementation in C

    Procedure:

    • receive query string and identify domain of perfect hash function by enumerating the list of keys

    • provide these keys and list size (the range will be 1..size) to the perfect hash generation function derived from above reference implementations

    • Use the perfect hash function generated to create the HashMap

    • Use the same perfect hash function to process the get requests in the HashMap

    Edit Necrolis noted in the comment below that the reference implementations output perfect hash functions in C source code, so you'll need to modify them to generate something like a bytecode for a VM instead. You could also use an interpretative language like embedded Scheme or Lua.

    It would be interesting to know if this is worth the effort over a simple (non-perfect) HashMap when the overhead of creating the perfect hash function is amortized over the lookups

    Another option is Cuckoo hashing which also has O(1) lookups

提交回复
热议问题