summaryrefslogtreecommitdiff
path: root/components/rtgui/utils/perfect_hash/example-Python/generate_hash.py
blob: dfe9f9218673bb321654e1a04bc251e633938c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
"""
This example shows how to use the function generate_hash.

generate_hash(kdic, Hash)

returns hash functions f1 and f2, and G for a perfect minimal hash.
Input is dictionary 'kdic' with the keys and desired hash values.
'Hash' is a random hash function generator, that means Hash(N) returns a
returns a random hash function which returns hash values from 0..N-1.
"""

import sys
import random, string

sys.path.append('..')
from perfect_hash import generate_hash



month = dict(zip('jan feb mar apr may jun jul aug sep oct mov dec'.split(),
                 range(1, 13)))

def mkRandHash(N):
    """
    Return a random hash function which returns hash values from 0..N-1.
    """
    junk = "".join(random.choice(string.letters + string.digits)
                   for i in xrange(10))
    return lambda key: hash(junk + str(key)) % N


f1, f2, G = generate_hash(month, mkRandHash)

for k, h in month.items():
    assert h == ( G[f1(k)] + G[f2(k)] ) % len(G)

print 'OK'