Skip to content

Commit a889bf9

Browse files
committed
Update search logic and add detailed documentation
1 parent 0af87ad commit a889bf9

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/dataStructures/hashSet/openAddressing/HashSet.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,21 @@ private int search(T element) {
199199

200200
int currentBucketIndex = startingProbeIndex;
201201
for (int i = 0; i < this.capacity() - 1; i++) {
202-
// if bucket contains null or Tombstone, skip checking for equality.
203-
if (!this.isEmptyBucket(currentBucketIndex)) {
204-
// Checks equality of elements using Object::equals and Object::hashCode.
205-
if (this.buckets[currentBucketIndex].equals(element)
206-
&& this.buckets[currentBucketIndex].hashCode() == element.hashCode()) {
207-
return currentBucketIndex;
208-
}
202+
// if bucket contains NULL, means element is not present because deleted elements are marked with TOMBSTONE.
203+
// That is to say given an arbitrary probe sequence of index 1, 2, 3, ..., there can never be a case where
204+
// there is a NULL bucket in the middle of the probe sequence; only TOMBSTONE markers.
205+
if (this.isNullBucket(currentBucketIndex)) {
206+
return ELEMENT_NOT_FOUND;
207+
}
208+
// if bucket contains TOMBSTONE, skip checking current bucket index for equality.
209+
if (this.isTombstoneBucket(currentBucketIndex)) {
210+
continue;
211+
}
212+
213+
// Checks equality of elements using Object::equals and Object::hashCode.
214+
if (this.buckets[currentBucketIndex].equals(element)
215+
&& this.buckets[currentBucketIndex].hashCode() == element.hashCode()) {
216+
return currentBucketIndex;
209217
}
210218
currentBucketIndex = (currentBucketIndex + 1) % this.capacity();
211219
}

0 commit comments

Comments
 (0)