-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhash.py
More file actions
432 lines (266 loc) · 8.92 KB
/
Copy pathhash.py
File metadata and controls
432 lines (266 loc) · 8.92 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import sys
from optparse import OptionParser
import math
font_output = open('userfont.h','w')
# first level simple hash ... used to disperse patterns using random d values
def hash( d, str ):
#if d == 0: d = 0x01000193
if d == 0: d = 0x811C9DC5
# Use the FNV-1a hash
for c in str:
#h = (h ^ p[i]) * 16777619
#d = ( (d * 0x01000193) ^ ord(c) ) & 0xffffffff;
d = d ^ int(c.encode('hex'),16) * 16777619 & 0xffffffff
return d
def isprime(x):
x = abs(int(x))
if x < 2:
return "Less 2", False
elif x == 2:
return True
elif x % 2 == 0:
return False
else:
for n in range(3, int(x**0.5)+2, 2):
if x % n == 0:
return False
return True
def nextprime(x):
while ( True ):
if isprime(x): break
x += 1
return x
# create PHF with MOS(Map,Order,Search), g is specifications array
def CreatePHF( dict ):
size = len(dict)
size = nextprime(len(dict)+len(dict)/4)
print "Size = %d" % (size)
#nextprime(int(size/(6*math.log(size,2))))
#c=4 corresponds to 4 bits/key
# for fast construction use size/5
# for faster construction use gsize=size
gsize = size/5
print "G array size = %d" % (gsize)
sys.stdout.flush()
#Step 1: Mapping
patterns = [ [] for i in range(gsize) ]
g = [0] * gsize #initialize g
values = [None] * size #initialize values
for key in dict.keys():
patterns[hash(0, key) % gsize].append( key )
# Step 2: Sort patterns in descending order and process
patterns.sort( key= len, reverse=True )
for b in xrange( gsize ):
pattern = patterns[b]
if len(pattern) <= 1: break
d = 1
item = 0
slots = []
# Step 3: rotate patterns and search for suitable displacement
while item < len(pattern):
slot = hash( d, pattern[item] ) % size
if values[slot] != None or slot in slots:
d += 1
if d < 0 : break
item = 0
slots = []
else:
slots.append( slot )
item += 1
if d < 0:
print "failed"
return
g[hash(0, pattern[0]) % gsize] = d
for i in range(len(pattern)):
values[slots[i]] = dict[pattern[i]]
if ( b % 100 ) == 0:
print "%d: pattern %d processed" % (b,len(pattern))
sys.stdout.flush()
# Process patterns with one key and use a negative value of d
freelist = []
for i in xrange(size):
if values[i] == None: freelist.append( i )
for b in xrange(b+1,gsize ):
pattern = patterns[b]
if len(pattern) == 0: break
#if len(pattern) > 1: continue;
slot = freelist.pop()
# subtract one to handle slot zero
g[hash(0, pattern[0]) % gsize] = -slot-1
values[slot] = dict[pattern[0]]
if (b % 1000) == 0:
print "-%d: pattern %d processed" % (b,len(pattern))
sys.stdout.flush()
print "PHF succeeded"
return (g, values)
# Look up a value in the hash table, defined by g and V.
def lookup( g, V, key ):
d = g[hash(0,key) % len(g)]
if d < 0: return V[-d-1]
return V[hash(d, key) % len(V)]
def print_hash_function(g,V):
font_output.write('#define V_size ' + str(len(V))+'\n')
font_output.write('#define g_size ' + str(len(g))+'\n')
font_output.write('const int g[] = {')
lenght = len(g)
for x in xrange(0,lenght-1):
font_output.write(str(g[x])+', ')
pass
font_output.write(str(g[lenght-1])+'};\n')
font_output.write('const int V[] = {')
lenght = len(V)
for x in xrange(0,lenght-1):
if V[x] == None:
font_output.write('NULL, ')
pass
else:
font_output.write(str(V[x])+', ')
pass
font_output.write(str(V[lenght-1])+'};\n')
font_output.write('uint32_t hash(uint32_t d,uint8_t* str,int len){\n if (d == 0)\n d = 0x811C9DC5UL;\n for (int i = 0; i < len; ++i)\n {\n d = d ^ (uint32_t)str[i] * 16777619 & 0xffffffff;\n }\n return d;\n}\nuint32_t lookup(uint8_t* str,int len){\n unsigned long d = g[hash(0,str,len) % g_size];\n if (d<0)\n return V[-d-1];\n return V[hash(d,str,len) % V_size]+95;\n}\n')
pass
def write_ascii(font_ASCII,size):
if size == 24:
arraySize = 48
BytePerline = 2
pass
if size == 15:
arraySize = 15
BytePerline = 1
pass
for x in xrange(0x20,0x80):
if debug:
font_ASCII.seek(x*arraySize)
for y in xrange(0,size):
line = font_ASCII.read(BytePerline)
data = int(line.encode('hex'),16)
print bin(data)[2:].zfill(BytePerline*8)#[:-4]
font_ASCII.seek(x*arraySize)
for y in xrange(0,arraySize):
line = font_ASCII.read(1)
data = int(line.encode('hex'),16)
font_output.write(hex(data)+', ')
font_output.write('\n')
pass
pass
def font_to_code(c,size):
if size == 15:
font = open('font/stdfont.15f','rb')
font_ASCII = open('font/ascfntkc.15','rb')
arraySize = 30
BytePerline = 2
arraySize_ASCII = 15
pass
if size == 24:
font = open('font/stdfont.24f','rb')
font_ASCII = open('font/ascfntkc.24','rb')
arraySize = 72
BytePerline = 3
arraySize_ASCII = 48
pass
font_output.write('const unsigned char user_font[] = {')
write_ascii(font_ASCII,size)
length = 0
for x in c:
read_char(x,font,size)
length +=1
pass
font_output.seek(font_output.tell()-1)
font_output.write('};\n')
font_output.write('const GFXglyph user_fontGlyphs[] = {\n')
if size == 15:
width = 16
hight = 15
width_ASCII = 8
hight_ASCII = 15
halfwidth = 8
pass
if size == 24:
width = 24
hight = 24
width_ASCII = 16
hight_ASCII = 24
halfwidth = 12
pass
for x in xrange(0x20,0x80):
font_output.write("{ %d, %d, %d, %d, 0, 0 },\n" % ((x-0x20)*arraySize_ASCII,width_ASCII,hight_ASCII,halfwidth))
pass
for x in xrange(0,length):
font_output.write("{ %d, %d, %d, %d, 0, 0 },\n" % ((x)*arraySize+(0x7E-0x20+2)*arraySize_ASCII,width,hight,width))
pass
font_output.seek(font_output.tell()-1)
font_output.write('};\n')
font_output.write("const GFXfont user_fontGFXfont PROGMEM = {\n (uint8_t *)user_font,\n (GFXglyph *)user_fontGlyphs,\n 0x20, 0xFFFFFFUL, %d};\n" % int(hight*1.25))
pass
def read_char(c,font,size):
if size == 24:
arraySize = 72
BytePerline = 3
pass
if size == 15:
arraySize = 30
BytePerline = 2
pass
offset = 0
hi = int(c[0].encode('hex'),16)
lo = int(c[1].encode('hex'),16)
if lo>=161:
serCode = (hi - 161) * 157 + lo - 161 + 1 + 63
else:
serCode = (hi - 161) * 157 + lo - 64 + 1
if serCode >= 472 & serCode < 5872:
offset = (serCode - 472) * arraySize
elif serCode >= 6281 & serCode <= 13973:
offset = (serCode - 6281) * arraySize + 5401 * arraySize
pass
if debug:
font.seek(offset)
for x in xrange(0,size):
line = font.read(BytePerline)
data = int(line .encode('hex'),16)
print bin(data)[2:].zfill(BytePerline*8)
pass
font.seek(offset)
for x in xrange(0,arraySize):
data = int(font.read(1).encode('hex'),16)
font_output.write(hex(data)+', ')
font_output.write('\n')
parser = OptionParser(usage="python %prog [options]")
parser.add_option("-s", action="store_true",dest="Small", help="Small Font")
parser.add_option("-p", action="store_true",dest="debug", help="print Font")
(opt, args) = parser.parse_args()
debug = opt.debug
size = 24
if opt.Small:
size = 15
pass
print "Reading dict words"
dict = {}
line = 1
for key in open('discrete.txt', "rt").readlines():
dict[key.strip()] = line
line +=1
#creating phf
print "Creating perfect hash"
print dict
(g, V) = CreatePHF( dict )
#printing phf specification
print "Printing g[]"
print g
print V
print_hash_function(g,V)
test = []
for x in open('discrete.txt', "rt").readlines():
test.append(x.strip('\n').strip('\r').decode('utf-8').encode('big5'))
pass
font_to_code(test,size)
#fast verification for few (key,value) count given by num1
'''
num1 = line
print "Verifying hash values for the first %d words"% (num1)
for key in open('discrete.txt', "rt").readlines():
line = lookup( g, V, key.strip() )
print "Word %s occurs on line %d" % (key.strip(), line)
line += 1
if line > num1: break
'''