Skip to content
Snippets Groups Projects
Commit c14ddd97 authored by Xiangrui Meng's avatar Xiangrui Meng Committed by Andrew Or
Browse files

[SPARK-6515] update OpenHashSet impl

Though I don't see any bug in the existing code, the update in this PR makes it read better. rxin

Author: Xiangrui Meng <meng@databricks.com>

Closes #5176 from mengxr/SPARK-6515 and squashes the following commits:

134494d [Xiangrui Meng] update OpenHashSet impl
parent 94598653
No related branches found
No related tags found
No related merge requests found
......@@ -122,7 +122,7 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
*/
def addWithoutResize(k: T): Int = {
var pos = hashcode(hasher.hash(k)) & _mask
var i = 1
var delta = 1
while (true) {
if (!_bitset.get(pos)) {
// This is a new key.
......@@ -134,14 +134,12 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
// Found an existing key.
return pos
} else {
val delta = i
// quadratic probing with values increase by 1, 2, 3, ...
pos = (pos + delta) & _mask
i += 1
delta += 1
}
}
// Never reached here
assert(INVALID_POS != INVALID_POS)
INVALID_POS
throw new RuntimeException("Should never reach here.")
}
/**
......@@ -163,21 +161,19 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
*/
def getPos(k: T): Int = {
var pos = hashcode(hasher.hash(k)) & _mask
var i = 1
val maxProbe = _data.size
while (i < maxProbe) {
var delta = 1
while (true) {
if (!_bitset.get(pos)) {
return INVALID_POS
} else if (k == _data(pos)) {
return pos
} else {
val delta = i
// quadratic probing with values increase by 1, 2, 3, ...
pos = (pos + delta) & _mask
i += 1
delta += 1
}
}
// Never reached here
INVALID_POS
throw new RuntimeException("Should never reach here.")
}
/** Return the value at the specified position. */
......
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