Skip to content

Commit 20cdd0c

Browse files
authored
Merge pull request scala/scala#8762 from retronym/topic/map-hashcode
2 parents 8c3da48 + 93d5518 commit 20cdd0c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

library/src/scala/collection/mutable/HashMap.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package mutable
1616
import scala.annotation.tailrec
1717
import scala.collection.Stepper.EfficientSplit
1818
import scala.collection.generic.DefaultSerializationProxy
19+
import scala.util.hashing.MurmurHash3
1920

2021
/** This class implements mutable maps using a hashtable.
2122
*
@@ -531,6 +532,21 @@ class HashMap[K, V](initialCapacity: Int, loadFactor: Double)
531532
override def mapFactory: MapFactory[HashMap] = HashMap
532533

533534
override protected[this] def stringPrefix = "HashMap"
535+
536+
override def hashCode: Int = {
537+
if (isEmpty) MurmurHash3.emptyMapHash
538+
else {
539+
val tupleHashIterator = new HashMapIterator[Any] {
540+
var hash: Int = 0
541+
override def hashCode: Int = hash
542+
override protected[this] def extract(nd: Node[K, V]): Any = {
543+
hash = MurmurHash3.tuple2Hash(unimproveHash(nd.hash), nd.value.##)
544+
this
545+
}
546+
}
547+
MurmurHash3.unorderedHash(tupleHashIterator, MurmurHash3.mapSeed)
548+
}
549+
}
534550
}
535551

536552
/**

library/src/scala/collection/mutable/HashSet.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package mutable
1616
import scala.annotation.tailrec
1717
import scala.collection.Stepper.EfficientSplit
1818
import scala.collection.generic.DefaultSerializationProxy
19+
import scala.util.hashing.MurmurHash3
1920

2021
/** This class implements mutable sets using a hashtable.
2122
*
@@ -360,6 +361,21 @@ final class HashSet[A](initialCapacity: Int, loadFactor: Double)
360361
protected[this] def writeReplace(): AnyRef = new DefaultSerializationProxy(new HashSet.DeserializationFactory[A](table.length, loadFactor), this)
361362

362363
override protected[this] def className = "HashSet"
364+
365+
override def hashCode: Int = {
366+
val setIterator = this.iterator
367+
val hashIterator: Iterator[Any] =
368+
if (setIterator.isEmpty) setIterator
369+
else new HashSetIterator[Any] {
370+
var hash: Int = 0
371+
override def hashCode: Int = hash
372+
override protected[this] def extract(nd: Node[A]): Any = {
373+
hash = unimproveHash(nd.hash)
374+
this
375+
}
376+
}
377+
MurmurHash3.unorderedHash(hashIterator, MurmurHash3.setSeed)
378+
}
363379
}
364380

365381
/**

0 commit comments

Comments
 (0)