Skip to content

Commit c14ddd9

Browse files
mengxrAndrew Or
authored andcommitted
[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 <[email protected]> Closes #5176 from mengxr/SPARK-6515 and squashes the following commits: 134494d [Xiangrui Meng] update OpenHashSet impl
1 parent 9459865 commit c14ddd9

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

core/src/main/scala/org/apache/spark/util/collection/OpenHashSet.scala

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
122122
*/
123123
def addWithoutResize(k: T): Int = {
124124
var pos = hashcode(hasher.hash(k)) & _mask
125-
var i = 1
125+
var delta = 1
126126
while (true) {
127127
if (!_bitset.get(pos)) {
128128
// This is a new key.
@@ -134,14 +134,12 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
134134
// Found an existing key.
135135
return pos
136136
} else {
137-
val delta = i
137+
// quadratic probing with values increase by 1, 2, 3, ...
138138
pos = (pos + delta) & _mask
139-
i += 1
139+
delta += 1
140140
}
141141
}
142-
// Never reached here
143-
assert(INVALID_POS != INVALID_POS)
144-
INVALID_POS
142+
throw new RuntimeException("Should never reach here.")
145143
}
146144

147145
/**
@@ -163,21 +161,19 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
163161
*/
164162
def getPos(k: T): Int = {
165163
var pos = hashcode(hasher.hash(k)) & _mask
166-
var i = 1
167-
val maxProbe = _data.size
168-
while (i < maxProbe) {
164+
var delta = 1
165+
while (true) {
169166
if (!_bitset.get(pos)) {
170167
return INVALID_POS
171168
} else if (k == _data(pos)) {
172169
return pos
173170
} else {
174-
val delta = i
171+
// quadratic probing with values increase by 1, 2, 3, ...
175172
pos = (pos + delta) & _mask
176-
i += 1
173+
delta += 1
177174
}
178175
}
179-
// Never reached here
180-
INVALID_POS
176+
throw new RuntimeException("Should never reach here.")
181177
}
182178

183179
/** Return the value at the specified position. */

0 commit comments

Comments
 (0)