Skip to content

Commit

Permalink
Merge pull request #592 from durban/issue590
Browse files Browse the repository at this point in the history
HashMap: fix removal with collision
  • Loading branch information
armanbilge authored Sep 12, 2023
2 parents 1f9b457 + b82c5e6 commit 5528ce8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/collections/HashMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ object HashMap extends HashMapInstances with compat.HashMapCompatCompanion {
this
else if (contents.toVector.lengthCompare(2) == 0) {
// There will no longer be any collisions once the key is removed
val keepIndex = ~keyIndex
val keepIndex = 1 - keyIndex
// This is a singleton node so the depth doesn't matter;
// we only need to index into it to inline the value in our parent node
val mask = Node.maskFrom(collisionHash, depth = 0)
Expand Down
13 changes: 13 additions & 0 deletions tests/src/test/scala/cats/collections/HashMapSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cats.collections

import cats.collections.arbitrary.hashmap._
import cats.collections.tests.compat._
import cats.kernel.Hash
import cats.kernel.laws.discipline.CommutativeMonoidTests
import cats.kernel.laws.discipline.HashTests
import cats.laws.discipline.SerializableTests
Expand Down Expand Up @@ -164,6 +165,18 @@ class HashMapSuite extends DisciplineSuite with ScalaVersionSpecificSyntax {
}
}

test("removed with collision") {
val myHash: Hash[Int] = new Hash[Int] {
final override def eqv(x: Int, y: Int): Boolean =
(x % 8) === (y % 8)
final override def hash(x: Int): Int =
x % 4
}
val m1 = HashMap(1 -> 1, 8 -> 8, 4 -> 4)(myHash)
val m2 = m1.removed(16)
assertEquals(m2, HashMap(1 -> 1, 4 -> 4)(myHash))
}

test("concat") {
forAll { (left: HashMap[Int, String], right: HashMap[Int, String]) =>
val leftKvs = left.iterator.toList
Expand Down

0 comments on commit 5528ce8

Please sign in to comment.