Package: perfect-hash
Generate a minimal perfect hash function for a given set of keys.
A given code template is filled with parameters, such that the
output is code which implements the hash function.
Templates can easily be constructed for any programming language.InstallationThe minimal perfect hash function generator is written in pure Python,
and can be installed using:$pipinstallperfect-hashThe code supports Python 2.7 and Python 3.5 or higher.
However, some of the examples do not support Python 2 anymore.IntroductionA perfect hash function of a certain set S of keys is a hash function
which 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 Nconsecutiveintegers, usually in the range from 0 to N-1.UsageGiven a set of keys which are character strings, the program returns a minimal
perfect hash function.  This hash function is returned in the form of Python
code by default.  Suppose we have a file with keys:# 'animals.txt'
Elephant
Horse
Camel
Python
Dog
CatThe exact way this file is parsed can be specified using command line
options, for example it is possible to only read one column from a file
which contains different items in each row.
The program is invoked like this:$perfect-hashanimals.txt# =======================================================================# ================= Python code for perfect hash function ===============# =======================================================================G=[0,3,6,0,4,1,5]defhash_f(key,T):returnsum(ord(T[i%8])*ord(c)fori,cinenumerate(key))%7defperfect_hash(key):return(G[hash_f(key,"1mmhoNMG")]+G[hash_f(key,"gf53KKbH")])%7# ============================ Sanity check =============================K=["Elephant","Horse","Camel","Python","Dog","Cat"]assertlen(K)==6forh,kinenumerate(K):assertperfect_hash(k)==hThe way the program works is by filling a code template with the calculated
parameters.  The program can take such a template in form of a file and
fill in the calculated parameters, this allows the generation of perfect
hash function in any programming language.  The hash function is kept quite
simple and does not require machine or language specific byte level operations
which might be hard to implement in the target language.
The following parameters are available in the template:stringexpands to$NSlength of S1 and S2 salt$S1S1 salt$S2S2 salt$NGlength of array G$Garray of integers G$NKnumber of keys, i.e. length of array K$Karray with (quoted) keys$$$ (a literal dollar sign)Since the syntax for arrays is not the same in all programming languages,
some specifics can be adjusted using command line options.
The built-in template which creates the above code is:G=[$G]defhash_f(key,T):returnsum(ord(T[i%$NS])*ord(c)fori,cinenumerate(str(key)))%$NGdefperfect_hash(key):return(G[hash_f(key,"$S1")]+G[hash_f(key,"$S2")])%$NGUsing code templates, makes this program very flexible.  The source repository
includes several complete examples for C.  There are many choices one
faces when implementing a static hash table: Do the parameter lists go into
a separate header file?  Should the API for the table only contain the hash
values, but not the objects being mapped?  And so on.
All these various choices are possible because of the template is simply
filled with the parameters, no matter what else is inside the template.Hash function typesOne important option theperfect-hashcommand provides is--hftwhich
is short of “hash function type”.  There are two types to choose from:A random hash function generation which creates hash function with a
random string being used as it’s salt.   This is the default.
Since the generated random hash function does not include large enough
output for a very large number of keys (over 10,000), the perfect hash
function generation will fail for such large keys.  However, the
implementation of this hash function is quite simple and fast.A random hash function generation which creates hash function with a
random integers being used as it’s salt.  Using this option will always
succeed, but an implementation requires two additional integer
arrays (apart from the always present arrayG).ExamplesThe source repository contains many useful examples (inexamples/) which
illustrate how to use theperfect-hashcommand, as well
aspython_hash.pyas a library.License of outputperfect-hash is released under the BSD license.  However, that does not
cause the output produced by perfect-hash to be under BSD.  The reason is
that the output contains only small pieces of text that come directly from
perfect-hash’s source code – less than 10 lines long if the default template
is being used, which serves more for illustration purposes - too small for
being significant.  Therefore the output is not “work based on perfect-hash”.The output produced by perfect-hash contains essentially all of the
input data.  Therefore the output is a “derivative work” of the input (in
the sense of U.S. copyright law); and its copyright status depends on the
copyright of the input.  For most software licenses, the result is that the
output is under the same license, with the same copyright holder, as the
input that was passed to perfect-hash.AcknowledgmentsPart of the code is based on an a program A.M. Kuchling wrote:http://www.amk.ca/python/code/perfect-hashThe algorithm this library is based on is described in the paper
“Optimal algorithms for minimal perfect hashing”,
Z. J. Czech, G. Havas and B.S. Majewski.http://cmph.sourceforge.net/papers/chm92.pdfI tried to illustrate the algorithm and explain how it works on:http://ilan.schnell-web.net/prog/perfect-hash/algo.html