Exemple #1
0
def main():
    for N in range(1, 100):
        print(N)
        for _ in range(100):
            keys = set()
            while len(keys) < N:
                keys.add(random_key())
            keys = list(keys)
            generate_hash(keys)
Exemple #2
0
def mkPerfHash(keys, Hash):
    """
    Given a list of keys and a hash function generator, return a perfect
    hash function for the keys, i.e. each key is mapped to it's index in
    the list.
    """
    f1, f2, G = generate_hash(keys, Hash)
    return lambda k: (G[f1(k)] + G[f2(k)]) % len(G)
def generate_api(path, rpc_table_in):
    current_path = os.path.dirname(os.path.realpath(__file__))

    rpc_table = rpc_table_in

    rpc_funcs = list(rpc_table.keys())

    rpc_funcs.insert(0, "__version")
    rpc_table["__version"] = []

    rpc_funcs.insert(0, "__ping")
    rpc_table["__ping"] = []

    rpc_funcs.insert(0, "__funcs")
    rpc_table["__funcs"] = []

    print(rpc_funcs)
    f1, f2, G = generate_hash(rpc_funcs, Hash=IntSaltHash)

    env = jinja2.Environment(loader=jinja2.FileSystemLoader(current_path),
                             trim_blocks=True,
                             block_start_string='@@',
                             block_end_string='@@',
                             variable_start_string='@=',
                             variable_end_string='=@')

    c_template = env.get_template("rpc_api.c.jinja2")
    h_template = env.get_template("rpc_api.h.jinja2")

    rpc_functions = []
    for index, key in enumerate(rpc_funcs):
        val = rpc_table[key]
        tmp = [key, ', '.join(x.name for x in val)]
        rpc_functions.append(tmp)

    template_args = {
        'rpc_functions': rpc_functions,
        'salt1': ', '.join("0x{:02X}".format(x) for x in f1.salt),
        'salt2': ', '.join("0x{:02X}".format(x) for x in f2.salt),
        'graph': ', '.join(str(x) for x in G),
        'keys': ', '.join('"{}"'.format(x) for x in rpc_funcs)
    }

    with open(os.path.join(path, "rpc_api.c"), 'w') as f:
        f.write(c_template.render(**template_args))
        f.write("\n")  # add a new line for pedantic warnings

    with open(os.path.join(path, "rpc_api.h"), 'w') as f:
        f.write(h_template.render(**template_args))
        f.write("\n")
#!/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.ascii_letters + string.digits)
                   for i in range(10))
    return lambda key: hash(junk + str(key)) % N
#!/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