Skip to content

Commit 5528ce8

Browse files
authored
Merge pull request #592 from durban/issue590
HashMap: fix removal with collision
2 parents 1f9b457 + b82c5e6 commit 5528ce8

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

core/src/main/scala/cats/collections/HashMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ object HashMap extends HashMapInstances with compat.HashMapCompatCompanion {
617617
this
618618
else if (contents.toVector.lengthCompare(2) == 0) {
619619
// There will no longer be any collisions once the key is removed
620-
val keepIndex = ~keyIndex
620+
val keepIndex = 1 - keyIndex
621621
// This is a singleton node so the depth doesn't matter;
622622
// we only need to index into it to inline the value in our parent node
623623
val mask = Node.maskFrom(collisionHash, depth = 0)

tests/src/test/scala/cats/collections/HashMapSuite.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package cats.collections
2323

2424
import cats.collections.arbitrary.hashmap._
2525
import cats.collections.tests.compat._
26+
import cats.kernel.Hash
2627
import cats.kernel.laws.discipline.CommutativeMonoidTests
2728
import cats.kernel.laws.discipline.HashTests
2829
import cats.laws.discipline.SerializableTests
@@ -164,6 +165,18 @@ class HashMapSuite extends DisciplineSuite with ScalaVersionSpecificSyntax {
164165
}
165166
}
166167

168+
test("removed with collision") {
169+
val myHash: Hash[Int] = new Hash[Int] {
170+
final override def eqv(x: Int, y: Int): Boolean =
171+
(x % 8) === (y % 8)
172+
final override def hash(x: Int): Int =
173+
x % 4
174+
}
175+
val m1 = HashMap(1 -> 1, 8 -> 8, 4 -> 4)(myHash)
176+
val m2 = m1.removed(16)
177+
assertEquals(m2, HashMap(1 -> 1, 4 -> 4)(myHash))
178+
}
179+
167180
test("concat") {
168181
forAll { (left: HashMap[Int, String], right: HashMap[Int, String]) =>
169182
val leftKvs = left.iterator.toList

0 commit comments

Comments
 (0)