Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class PrimitiveKeyOpenHashMap[@specialized(Long, Int) K: ClassTag,

override def size = _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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}