Skip to content

Commit 0af87ad

Browse files
committed
Replace tombstone method with a instance field
1 parent 6d5d01a commit 0af87ad

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/dataStructures/hashSet/openAddressing/HashSet.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import java.util.stream.Collectors;
66

77
/**
8-
* Implementation of a HashSet that uses Open Addressing to resolve collisions.
8+
* Implementation of a HashSet that uses Open Addressing and linear probing to resolve collisions.
99
*
1010
* <p>The time complexity of operations in this HashSet implementation consists of two components. Firstly, there is the time to
1111
* compute the hash value, which is typically a constant-time operation. Secondly, there is the time to access the corresponding
12-
* bucket, which involves traversing the linked list in case of collisions. On average, these operations have a constant-time
13-
* complexity.
12+
* bucket, which involves probing the buckets using linear probing.
1413
*
1514
* <p>Public methods (along with their time-complexity):
1615
* boolean add(T element) adds the given element into the HashSet. Expected O(1) assuming SUHA.
@@ -29,6 +28,7 @@ public class HashSet<T>{
2928
private final int ELEMENT_NOT_FOUND = -1;
3029
private int size; // Number of elements present in the Set (its cardinality).
3130
private T[] buckets;
31+
private final T TOMBSTONE;
3232

3333
/**
3434
* Creates a HashSet with an initial capacity of 16.
@@ -40,6 +40,11 @@ public HashSet() {
4040
T[] tempBuckets = (T[]) new Object[INITIAL_CAPACITY];
4141
this.buckets = tempBuckets;
4242
this.size = 0;
43+
44+
// There is no way to retrieve an instance of Tombstone. Therefore, it is safe to cast Tombstone to T.
45+
@SuppressWarnings("unchecked")
46+
T tempVar = (T) Tombstone.TOMBSTONE;
47+
this.TOMBSTONE = tempVar;
4348
}
4449

4550
/**
@@ -76,7 +81,7 @@ public boolean remove(T element) {
7681
if (bucketIndex == ELEMENT_NOT_FOUND) {
7782
return false; // If the index returned by the probe function contains an empty bucket, then the element is not present in the set.
7883
}
79-
this.buckets[bucketIndex] = this.tombstone(); // marks the current bucket with a TOMBSTONE.
84+
this.buckets[bucketIndex] = this.TOMBSTONE; // marks the current bucket with a TOMBSTONE.
8085
this.size--;
8186
return true;
8287
}
@@ -124,7 +129,7 @@ public int size() {
124129
*/
125130
public List<T> toList() {
126131
return Arrays.stream(this.buckets)
127-
.filter(element -> element != null || this.tombstone().equals(element))
132+
.filter(element -> element != null || this.TOMBSTONE.equals(element))
128133
.collect(Collectors.toList());
129134
}
130135

@@ -208,14 +213,6 @@ private int search(T element) {
208213
return ELEMENT_NOT_FOUND; // element is not in the HashSet.
209214
}
210215

211-
// This method returns an instance of Tombstone, which is used to mark removed elements.
212-
private T tombstone() {
213-
// There is no way to retrieve an instance of Tombstone. Therefore, it is safe to cast Tombstone to T.
214-
@SuppressWarnings("unchecked")
215-
T deleted = (T) Tombstone.TOMBSTONE;
216-
return deleted;
217-
}
218-
219216
/**
220217
* Returns true if the bucket at the given bucketIndex contains no elements (Either null or Tombstone).
221218
*
@@ -243,7 +240,7 @@ private boolean isNullBucket(int bucketIndex) {
243240
* @return true if the bucket contains a Tombstone at the given bucketIndex.
244241
*/
245242
private boolean isTombstoneBucket(int bucketIndex) {
246-
return this.tombstone().equals(this.buckets[bucketIndex]);
243+
return this.TOMBSTONE.equals(this.buckets[bucketIndex]);
247244
}
248245

249246
/**

0 commit comments

Comments
 (0)