Skip to content

Commit f7c27de

Browse files
authored
Merge pull request #67 from euchangxian/refactor/OA-HashSet
refactor: Update OpenAddressing HashSet implementation
2 parents 63ea5ec + 439a817 commit f7c27de

File tree

4 files changed

+238
-133
lines changed

4 files changed

+238
-133
lines changed

src/main/java/dataStructures/hashSet/chaining/HashSet.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public HashSet() {
6767
* @return the number of elements in this set (its cardinality)
6868
*/
6969
public int size() {
70-
return this.size;
70+
return size;
7171
}
7272

7373
/**
@@ -76,13 +76,12 @@ public int size() {
7676
* @return true if this set contains no elements
7777
*/
7878
public boolean isEmpty() {
79-
return this.size() == 0;
79+
return size() == 0;
8080
}
8181

8282
/**
83-
* TODO formal documentation.
8483
* Simple hash function to hash the element into their respective bucket.
85-
* Currently uses the division method (k % m).
84+
* Currently, uses the division method (k % m).
8685
* T must override both Object::equals and Object::hashCode.
8786
*
8887
* @param element the specified element to be hashed.
@@ -104,12 +103,12 @@ private int hashFunction(T element) {
104103
* element
105104
*/
106105
public boolean add(T element) {
107-
int bucket = this.hashFunction(element);
108-
LinkedList<T> bucketLinkedList = this.buckets[bucket];
106+
int bucket = hashFunction(element);
107+
LinkedList<T> bucketLinkedList = buckets[bucket];
109108
if (bucketLinkedList.search(element) != -1) {
110109
return false; // element is already in the set.
111110
}
112-
++this.size; // updates the cardinality of this hashset.
111+
++size; // updates the cardinality of this hashset.
113112
return bucketLinkedList.insertFront(element);
114113
}
115114

@@ -120,8 +119,8 @@ public boolean add(T element) {
120119
* @return true if this set contains the specified element
121120
*/
122121
public boolean contains(T element) {
123-
int bucket = this.hashFunction(element);
124-
LinkedList<T> bucketLinkedList = this.buckets[bucket];
122+
int bucket = hashFunction(element);
123+
LinkedList<T> bucketLinkedList = buckets[bucket];
125124
return bucketLinkedList.search(element) != -1;
126125
}
127126

@@ -137,14 +136,14 @@ public boolean contains(T element) {
137136
* @return true if this set contained the specified element
138137
*/
139138
public boolean remove(T element) {
140-
int bucket = this.hashFunction(element);
141-
LinkedList<T> bucketLinkedList = this.buckets[bucket];
139+
int bucket = hashFunction(element);
140+
LinkedList<T> bucketLinkedList = buckets[bucket];
142141
int index = bucketLinkedList.search(element);
143142
if (index == -1) {
144143
return false; // If the element is not in the hashset.
145144
}
146145
bucketLinkedList.remove(index);
147-
--this.size; // updates the cardinality of the hash set.
146+
--size; // updates the cardinality of the hash set.
148147
return true;
149148
}
150149

@@ -155,7 +154,7 @@ public boolean remove(T element) {
155154
*/
156155
public List<T> toList() {
157156
List<T> outputList = new ArrayList<>();
158-
for (LinkedList<T> bucket : this.buckets) {
157+
for (LinkedList<T> bucket : buckets) {
159158
while (bucket.size() != 0) {
160159
outputList.add(bucket.pop());
161160
}

0 commit comments

Comments
 (0)