ref: 10aacae6e0d47aa51731453998dbfcec1bbb6641
parent: 6380f0c2b7a4c6e950c5fbced2fd24749850bea5
author: Ori Bernstein <[email protected]>
date: Fri Oct 18 13:40:00 EDT 2013
Add some useful hash functions
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -8,6 +8,7 @@
error.myr \
extremum.myr \
fmt.myr \
+ hashfuncs.myr \
htab.myr \
intparse.myr \
now.myr \
--- /dev/null
+++ b/libstd/hashfuncs.myr
@@ -1,0 +1,57 @@
+use "sleq.use"
+use "types.use"
+
+pkg std =
+ const strhash : (s : byte[:] -> uint32)
+ const streq : (a : byte[:], b : byte[:] -> bool)
+
+ generic ptrhash : (p : @a# -> uint32)
+ generic ptreq : (a : @a#, b : @a# -> bool)
+
+ generic inthash : (v : @a::(tcint,tcnum,tctest) -> uint32)
+ generic inteq : (a : @a::(tcint,tcnum,tctest), b : @a::(tcint,tcnum,tctest) -> bool)
+;;
+
+/* Supremely simple djb hash. */
+const strhash = {s
+ var h
+ var i
+
+ h = 5381
+ for i = 0; i < s.len; i++
+ h = (h << 5) + h + (s[i] castto(uint32))
+ ;;
+ -> h
+}
+
+const streq = {a, b
+ -> sleq(a, b)
+}
+
+/* FIXME: come up with a *good* hash function */
+generic ptrhash = {p
+ var x
+
+ x = p castto(intptr)
+ /* Mix the top bits in to the bottom, and multiply by a large prime. */
+ x = x ^ (x >> 32) * 357913941
+ -> x castto(uint32)
+}
+
+generic ptreq = {a, b
+ -> a == b
+}
+
+/* FIXME: come up with a *good* hash function */
+generic inthash = {v
+ var x
+
+ x = v castto(uint64)
+ /* Mix the top bits in to the bottom, and multiply by a large prime. */
+ x = x ^ (x >> 32) * 357913941
+ -> x castto(uint32)
+}
+
+generic inteq = {a, b
+ -> a == b
+}