def perfect_hash(keys, name): p = perfection.hash_parameters(keys) def f(i, p=p): i += p.offset x = i % p.t y = i / p.t return x + p.r[y] return { 'PHASHRANGE': p.t - 1 + max(p.r), 'PHASHNKEYS': len(p.slots), 'pyfunc': f, 'code': """ static const int8_t %(name)s_r[] = {%(r)s}; static uint32_t %(name)s_phash(uint32_t i) { i %(offset_sign)s= %(offset)d; uint32_t x = i %% %(t)d; uint32_t y = i / %(t)d; uint32_t h = x; if (y < GPR_ARRAY_SIZE(%(name)s_r)) { uint32_t delta = (uint32_t)%(name)s_r[y]; h += delta; } return h; } """ % { 'name': name, 'r': ','.join('%d' % (r if r is not None else 0) for r in p.r), 't': p.t, 'offset': abs(p.offset), 'offset_sign': '+' if p.offset > 0 else '-' } }
def perfectTester(): count = 0 count2 = 0 # Tests insertion using a perfect hash. perfect_hash_table = perfection.make_hash(valuesToInsert) params = perfection.hash_parameters(valuesToInsert) print("Inserted: ", len(valuesToInsert)) print("Not Inserted: 0") print("Load Factor: ", (len(valuesToInsert) / params.t)) function = u"\nperfect_hash(i):\n static r = {}\n static t = {}\n\n x = i mod t //({})\n y = i div t //({})\n return x + r[y]\n".format( params.r, params.t, params.t, params.t) # print("Perfect Hash Function: ", function) # print(hashTable) print()
["Automatically generated by tools/codegen/core/gen_settings_ids.py"]) print >> H, "#ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H" print >> H, "#define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H" print >> H print >> H, "#include <stdint.h>" print >> H, "#include <stdbool.h>" print >> H print >> C, "#include \"src/core/ext/transport/chttp2/transport/http2_settings.h\"" print >> C print >> C, "#include <grpc/support/useful.h>" print >> C, "#include \"src/core/lib/transport/http2_errors.h\"" print >> C p = perfection.hash_parameters(sorted(x.id for x in _SETTINGS.values())) print p def hash(i): i += p.offset x = i % p.t y = i / p.t return x + p.r[y] decorated_settings = [ DecoratedSetting(hash(setting.id), name, setting) for name, setting in _SETTINGS.iteritems() ]
put_banner([H,C], ["Automatically generated by tools/codegen/core/gen_settings_ids.py"]) print >>H, "#ifndef GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H" print >>H, "#define GRPC_CORE_EXT_TRANSPORT_CHTTP2_TRANSPORT_HTTP2_SETTINGS_H" print >>H print >>H, "#include <stdint.h>" print >>H, "#include <stdbool.h>" print >>H print >>C, "#include \"src/core/ext/transport/chttp2/transport/http2_settings.h\"" print >>C print >>C, "#include <grpc/support/useful.h>" print >>C, "#include \"src/core/lib/transport/http2_errors.h\"" print >>C p = perfection.hash_parameters(sorted(x.id for x in _SETTINGS.values())) print p def hash(i): i += p.offset x = i % p.t y = i / p.t return x + p.r[y] decorated_settings = [DecoratedSetting(hash(setting.id), name, setting) for name, setting in _SETTINGS.iteritems()] print >>H, 'typedef enum {' for decorated_setting in sorted(decorated_settings): print >>H, ' GRPC_CHTTP2_SETTINGS_%s = %d, /* wire id %d */' % ( decorated_setting.name, decorated_setting.enum, decorated_setting.setting.id)