Skip to content

Commit c531621

Browse files
committed
Fix handling of null values in ChampHashMap
1 parent 39b3130 commit c531621

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/library/scala/collection/immutable/ChampHashMap.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,16 @@ private class BitmapIndexedMapNode[K, +V](val dataMap: Int, val nodeMap: Int, va
197197

198198
if ((dataMap & bitpos) != 0) {
199199
val index = indexFrom(dataMap, mask, bitpos)
200-
val payload = this.getKey(index)
201-
return if (key == payload) Option(this.getValue(index)) else Option.empty
200+
val key0 = this.getKey(index)
201+
return if (key == key0) Some(this.getValue(index)) else None
202202
}
203203

204204
if ((nodeMap & bitpos) != 0) {
205205
val index = indexFrom(nodeMap, mask, bitpos)
206206
return this.getNode(index).get(key, keyHash, shift + BitPartitionSize)
207207
}
208208

209-
Option.empty
209+
None
210210
}
211211

212212
override def containsKey(key: K, keyHash: Int, shift: Int): Boolean = {
@@ -514,7 +514,7 @@ private class HashCollisionMapNode[K, +V](val hash: Int, val content: Vector[(K,
514514
require(content.size >= 2)
515515

516516
def get(key: K, hash: Int, shift: Int): Option[V] =
517-
if (this.hash == hash) content.find(key == _._1).map(_._2) else Option.empty
517+
if (this.hash == hash) content.find(key == _._1).map(_._2) else None
518518

519519
override def containsKey(key: K, hash: Int, shift: Int): Boolean =
520520
this.hash == hash && content.exists(key == _._1)

test/junit/scala/collection/immutable/ChampMapSmokeTest.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,17 @@ class ChampMapSmokeTest {
238238
val map2 = map1.updated(k1, v1)
239239
assertSame(map1, map2)
240240
}
241+
242+
@Test def nullValue(): Unit = {
243+
val map = emptyMap[Any, Any]
244+
assertEquals(Some(null), map.updated("", null).get(""))
245+
}
246+
247+
@Test def nullValueCollision(): Unit = {
248+
val k0 = new C(0)
249+
val k1 = new C(4)
250+
assertEquals(k0.hashCode, k1.hashCode)
251+
val map = emptyMap[Any, Any].updated(k0, 0).updated(k1, null)
252+
assertEquals(Some(null), map.get(k1))
253+
}
241254
}

0 commit comments

Comments
 (0)