]> thyrsus.com Git - comparator.git/commitdiff
Initial revision
authorEric S. Raymond <esr@thyrsus.com>
Mon, 15 Sep 2003 16:54:59 +0000 (16:54 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 15 Sep 2003 16:54:59 +0000 (16:54 +0000)
hashgen.py [new file with mode: 0755]

diff --git a/hashgen.py b/hashgen.py
new file mode 100755 (executable)
index 0000000..85bad6c
--- /dev/null
@@ -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)
+