From: Eric S. Raymond Date: Mon, 15 Sep 2003 16:54:59 +0000 (+0000) Subject: Initial revision X-Git-Tag: 1.6~2 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=e6a15c558e6f32147026eb0d1f337a127cbd88c3;p=comparator.git Initial revision --- diff --git a/hashgen.py b/hashgen.py new file mode 100755 index 0000000..85bad6c --- /dev/null +++ b/hashgen.py @@ -0,0 +1,29 @@ +#!/bin/env python +# Generate hash function mapping text bytes to an unsigned integral type, using +# MD5 to generate the magic values. Based on an algorithm suggested by +# Ron Rivest. + +import sys, md5 + +def tablegen(maxlen, width): + print "static hashval_t magicbits[256][%d] = " % maxlen + print "{" + for i in range(256): + sys.stdout.write(' /* char %02x */ {' % i) + for j in range(maxlen): + if j % 3 == 0: + sys.stdout.write('\n\t') + m = md5.new() + m.update(`i` + `j`) + sys.stdout.write('0x' + m.hexdigest()[-(width*2):] + "LL,") + print "}, /* end char %02x */" % i + print "};" + + +if __name__ == '__main__': + maxlen = 512 + width = 8 + sys.stderr.write("Table will occupy %dK bytes\n" + % ((maxlen * width * 256) / 1024)) + tablegen(maxlen, width) +