MCPcopy Create free account
hub / github.com/9chu/LuaSTGPlus / generated_mpf

Function generated_mpf

LuaSTGPlus/string2enum.py:143–254  ·  view source on GitHub ↗
(filename)

Source from the content-addressed store, hash-verified

141 return (g_table[keyhash(key, T1, idx_list, n)] + g_table[keyhash(key, T2, idx_list, n)]) % m
142
143def generated_mpf(filename):
144 with open(filename, "r") as f:
145 options = json.load(f)
146 keys = []
147 enums = []
148 keydict = {} # key->enum
149 enumdict = {} # enum->key
150 for item in options["keys"]:
151 keys.append(item[0])
152 enums.append(item[1])
153 keydict[item[0]] = item[1]
154 enumdict[item[1]] = item[0]
155 # step1: find best indices
156 best_indices = find_best_indices(keys)
157 # step2: generate random table and graph
158 hash_table1, hash_table2, graph = generate_graph(keys, best_indices, options["factor"] if options.has_key("factor") else 2.0)
159 # step3: generate function g
160 hash_func_g = find_func_g(graph, len(keys))
161 # check step
162 for i in range(0, len(keys)):
163 hash_check = final_hash_func(keys[i], best_indices, hash_table1, hash_table2, hash_func_g, len(keys))
164 # print("key %s hash %d" % (keys[i], hash_check))
165 assert(i == hash_check)
166 # print results
167 print("*** Results:")
168 print("n = " + str(len(graph)))
169 print("m = " + str(len(keys)))
170 print("best_indices = " + str(best_indices))
171 print("hash_table1 = " + str(hash_table1))
172 print("hash_table2 = " + str(hash_table2))
173 print("hashfunc_g = " + str(hash_func_g))
174 # generate C++ source file
175 print("*** generating C++ source file...")
176 char_type = "wchar_t" if options["wide_char"] else "char"
177 with open(options["output"], "w") as out_file:
178 out_file.write(u"#pragma once\n")
179 out_file.write(u"#include <cstring>\n")
180 #out_file.write(u"#include <cstdint>\n")
181 out_file.write(u"\n")
182 out_file.write(u"namespace %s\n" % options["namespace"])
183 out_file.write(u"{\n")
184 out_file.write(u"\tenum class %s\n" % options["enum_name"])
185 out_file.write(u"\t{\n")
186 for i in range(0, len(enums)):
187 out_file.write(u"\t\t%s = %d,\n" % (enums[i], i))
188 out_file.write(u"\t\t_KEY_NOT_FOUND = -1\n")
189 out_file.write(u"\t};\n")
190 out_file.write(u"\n")
191 out_file.write(u"\tinline %s %s(const %s* key)\n" % (options["enum_name"], options["hashfunc_name"], char_type))
192 out_file.write(u"\t{\n")
193 out_file.write(u"\t\tstatic const %s* s_orgKeyList[] =\n" % char_type)
194 out_file.write(u"\t\t{\n")
195 for i in range(0, len(keys)):
196 out_file.write(u'\t\t\t%s"%s",\n' % (u'L' if options["wide_char"] else u'', keys[i]))
197 out_file.write(u"\t\t};\n")
198 out_file.write(u"\t\t\n")
199 out_file.write(u"\t\tstatic const unsigned int s_bestIndices[] =\n")
200 out_file.write(u"\t\t{")

Callers 1

string2enum.pyFile · 0.85

Calls 4

find_best_indicesFunction · 0.85
generate_graphFunction · 0.85
find_func_gFunction · 0.85
final_hash_funcFunction · 0.85

Tested by

no test coverage detected