Skip to content

Commit daf6db7

Browse files
committed
Process hashcode bits in util.HashMap and HashSet
The code was taken from scala.collection.mutable.AnyRefMap
1 parent 3ad578e commit daf6db7

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

compiler/src/dotty/tools/dotc/util/HashMap.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ extends GenericHashMap[Key, Value](initialCapacity, capacityMultiple):
1111
/** Hashcode is left-shifted by 1, so lowest bit is not lost
1212
* when taking the index.
1313
*/
14-
final def hash(x: Key): Int = x.hashCode << 1
14+
final def hash(key: Key): Int =
15+
val h = key.hashCode
16+
// Part of the MurmurHash3 32 bit finalizer
17+
val i = (h ^ (h >>> 16)) * 0x85EBCA6B
18+
val j = (i ^ (i >>> 13)) & 0x7FFFFFFF
19+
(if j==0 then 0x41081989 else j) << 1
1520

1621
final def isEqual(x: Key, y: Key): Boolean = x.equals(y)
1722

compiler/src/dotty/tools/dotc/util/HashSet.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
4848

4949
protected def isDense = limit < DenseLimit
5050

51-
/** Hashcode, by defualt `x.hashCode`, can be overridden */
52-
protected def hash(x: T): Int = x.hashCode
51+
/** Hashcode, by default a processed `x.hashCode`, can be overridden */
52+
protected def hash(key: T): Int =
53+
val h = key.hashCode
54+
// Part of the MurmurHash3 32 bit finalizer
55+
val i = (h ^ (h >>> 16)) * 0x85EBCA6B
56+
val j = (i ^ (i >>> 13)) & 0x7FFFFFFF
57+
if j==0 then 0x41081989 else j
5358

5459
/** Hashcode, by default `equals`, can be overridden */
5560
protected def isEqual(x: T, y: T): Boolean = x.equals(y)

0 commit comments

Comments
 (0)