Perfect hashing utilities for Python
Project description
A module that creates perfect hash functions for a known set of integer inputs.
>>> import perfection >>> l = (0, 3, 4, 7 ,10, 13, 15, 18, 19, 21, 22, 24, 26, 29, 30, 34) >>> hf = perfection.make_hash(l) >>> hf(19) 1
Main features
make_hash() that generates an honest-to-goodness perfect hash function for the given keys.
make_dict() creates a dictionary subclass that implements the MutableMapping interface (thus, acts exactly like a dict), and uses the hash function created in the equivalent call to make_hash().
Additionally, hash_parameters() may be used to output the parameters of making a perfect hash for the given set of input keys. These parameters can then be used to implement a perfect hash function in a language of your choice.
For example, generate t and r parameters using hash_parameters():
>>> l = (0, 3, 4, 7 ,10, 13, 15, 18, 19, 21, 22, 24, 26, 29, 30, 34) >>> params = hash_parameters(l) >>> params.t 6 >>> params.r (2, 7, 12, 0, 7, 10)
Then, the hash function, in pseudocode is as follows:
function hash(i): static r = { 2, 7, 12, 0, 7, 10 } static t = 6 x = i mod t y = i div t return x + r[y]
Note that div stands for floor (integer) division.
Credit
Algorithm described by Thomas Gettys.
Python code © 2014 Eddie Antonio Santos. MIT licensed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file perfection-1.0.0.tar.gz
.
File metadata
- Download URL: perfection-1.0.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b610cef6fca80e97862b7693bc4ff902ca2eff1c1cfbf2c5cee7f03a2a8fbfe |
|
MD5 | 677582cc6d88e4d6abfca0705fef3758 |
|
BLAKE2b-256 | 36014c7828155962189f198e93a50175126a355b1ffca54563a8caad17a9e2a7 |