Skip to content
Snippets Groups Projects
Commit 6930e965 authored by Xiangrui Meng's avatar Xiangrui Meng
Browse files

[SPARK-6512] add contains to OpenHashMap

Add `contains` to test whether a key exists in an OpenHashMap. rxin

Author: Xiangrui Meng <meng@databricks.com>

Closes #5171 from mengxr/openhashmap-contains and squashes the following commits:

d6e6f1f [Xiangrui Meng] add contains to primitivekeyopenhashmap
748a69b [Xiangrui Meng] add contains to OpenHashMap
parent 05c2214b
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,15 @@ class OpenHashMap[K : ClassTag, @specialized(Long, Int, Double) V: ClassTag](
override def size: Int = if (haveNullValue) _keySet.size + 1 else _keySet.size
/** Tests whether this map contains a binding for a key. */
def contains(k: K): Boolean = {
if (k == null) {
haveNullValue
} else {
_keySet.getPos(k) != OpenHashSet.INVALID_POS
}
}
/** Get the value for a given key */
def apply(k: K): V = {
if (k == null) {
......
......@@ -48,6 +48,11 @@ class PrimitiveKeyOpenHashMap[@specialized(Long, Int) K: ClassTag,
override def size: Int = _keySet.size
/** Tests whether this map contains a binding for a key. */
def contains(k: K): Boolean = {
_keySet.getPos(k) != OpenHashSet.INVALID_POS
}
/** Get the value for a given key */
def apply(k: K): V = {
val pos = _keySet.getPos(k)
......
......@@ -176,4 +176,14 @@ class OpenHashMapSuite extends FunSuite with Matchers {
assert(map(i.toString) === i.toString)
}
}
test("contains") {
val map = new OpenHashMap[String, Int](2)
map("a") = 1
assert(map.contains("a"))
assert(!map.contains("b"))
assert(!map.contains(null))
map(null) = 0
assert(map.contains(null))
}
}
......@@ -118,4 +118,11 @@ class PrimitiveKeyOpenHashMapSuite extends FunSuite with Matchers {
assert(map(i.toLong) === i.toString)
}
}
test("contains") {
val map = new PrimitiveKeyOpenHashMap[Int, Int](1)
map(0) = 0
assert(map.contains(0))
assert(!map.contains(1))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment