@@ -30,7 +30,7 @@ class PerfectHashing[Key](initialCapacity: Int = 8, capacityMultiple: Int = 2):
3030
3131 protected def allocate (capacity : Int ) =
3232 keys = new Array [AnyRef ](capacity)
33- if capacity > DenseLimit then
33+ if ! isDense then
3434 table = new Array [Int ](capacity * roundToPower(capacityMultiple))
3535
3636 private def roundToPower (n : Int ) =
@@ -50,10 +50,17 @@ class PerfectHashing[Key](initialCapacity: Int = 8, capacityMultiple: Int = 2):
5050
5151 private final def isDense = capacity <= DenseLimit
5252
53- /** Hashcode, by default `x.hashCode`, can be overridden */
54- protected def hash (x : Key ): Int = x.hashCode
55-
56- /** Hashcode, by default `equals`, can be overridden */
53+ /** Hashcode, by default a post-processed versoon of `k.hashCode`,
54+ * can be overridden
55+ */
56+ protected def hash (k : Key ): Int =
57+ val h = k.hashCode
58+ // Part of the MurmurHash3 32 bit finalizer
59+ val i = (h ^ (h >>> 16 )) * 0x85EBCA6B
60+ val j = (i ^ (i >>> 13 )) & 0x7FFFFFFF
61+ if (j== 0 ) 0x41081989 else j
62+
63+ /** Equality test, by default `equals`, can be overridden */
5764 protected def isEqual (x : Key , y : Key ): Boolean = x.equals(y)
5865
5966 private def matches (entry : Int , k : Key ) = isEqual(key(entry), k)
@@ -64,7 +71,6 @@ class PerfectHashing[Key](initialCapacity: Int = 8, capacityMultiple: Int = 2):
6471
6572 /** The key at index `idx` */
6673 def key (idx : Int ) = keys(idx).asInstanceOf [Key ]
67-
6874 private def setKey (e : Int , k : Key ) = keys(e) = k.asInstanceOf [AnyRef ]
6975
7076 private def entry (idx : Int ): Int = table(idx) - 1
0 commit comments