A perfect hash function for a set S of keys maps all keys in S to different numbers. That means that for the set S, the hash function is collision-free, or perfect. Further, a perfect hash function is called "minimal" when it maps N keys to N consecutive integers, usually in the range from 0 to N-1. (PyPi perfect-hash Documentation)
We will use the program Perfect_hash.py to generate a minimal perfect hash function (in Python) for a given set of keys.
There are two ways to obtain the tool perfect_hash.py.
pip install perfect-hash
What actually happens is the program perfect_hash.py is downloaded and made available as a Python module. At the end of the install messages the location of the installed program is displayed.
There are two ways to access it within a Python program...
import sys from perfect_hash import main sys.exit(main()) Note: a. This uses a Python program run the Python program perfect_hash.py b. Put program arguments on the command line
import perfect_hash as ph ph.read_table(...) Note: This code allow access to internal methods
Note: This may be the best and simplest way to use the tool.
You can get perfect_hash.py from the install step above or locate and download it from the web. Either way, copy it to the location of your choice. For example to use it ...
Note: you may need replace the first line of the file with "#!/usr/bin/python3" or something like it../perfect_hash.py [options] KEYS_FILE [TMPL_FILE]
To see examples click
HERE
.
To see the command line help click
HERE
.
For more information about the
PyPi Perfect-Hash Project click
HERE
.
More perfect-hash Documentation
.
perfect_hash.py
(code found on the web)
Create Minimum perfect hash for sparse 64-bit unsigned integer
perfection module
(A module that creates perfect hash functions for a known set of integer inputs)
Generic Perfect Hash Generator
Hash function
(Wikipedia)
Hashing Tutorial 6 - Introduction to Perfect Hashing
(YouTube)
Hashing Tutorial 7 - Building Perfect Hashing
(YouTube)
Build a Hash Table in Python With TDD
Python documentation: Testing for Python Keywords
GNU Perfect Hash Function Generator (Gperf)
Gperf Home
Gperf Manual
Gperf for Windows
Gperf Documentation