Skip to main content

Perfect hashing utilities for Python

Project description

https://travis-ci.org/eddieantonio/perfection.svg?branch=master

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

Install

pip install perfection

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.

Beta Features

Can import a minimal perfect (ordered!) hash function with the same API using:

import perfection.czech

API is not yet finalized!

Credit

Algorithm described by Thomas Gettys.

Python code © 2014, 2016 Eddie Antonio Santos. MIT licensed.

With contributions by Thomas Calmant.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

perfection-2.0.1.tar.gz (11.5 kB view details)

Uploaded Source

File details

Details for the file perfection-2.0.1.tar.gz.

File metadata

  • Download URL: perfection-2.0.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.12

File hashes

Hashes for perfection-2.0.1.tar.gz
Algorithm Hash digest
SHA256 d98e34034dbbb60027d0fb87a7d91cd0eaf5bc7e69804e9c505778195430cf7c
MD5 2db1322206cd7ef66e531e4684e4c6f9
BLAKE2b-256 cb78e8d7888396038fc25fb1301a678123da03ea680768f6476795968aac245a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page