Skip to content

Commit 02fec4f

Browse files
committed
update: options added to refect corner case testing
1 parent 3a09fef commit 02fec4f

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/_DataStructures_/HashTable/index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const HashEntry = require('./HashEntry');
22

33
class HashTable {
4-
constructor(slots) {
4+
constructor(slots, { allowResize = true, strongHash = true, custonHash = null }) {
55
// init with a default set of slots
66
this.slot = slots || 19;
77
// size to hold the current size
@@ -11,6 +11,11 @@ class HashTable {
1111
this.threshold = 0.7;
1212
// the main bucket
1313
this.bucket = new Array(this.slot);
14+
this.allowResize = allowResize;
15+
this.strongHash = strongHash;
16+
if (custonHash) {
17+
this._hash = custonHash;
18+
}
1419

1520
// fill the bucket with null
1621
for (let i = 0; i < this.slot; i += 1) this.bucket[i] = null;
@@ -31,7 +36,9 @@ class HashTable {
3136
const char = stringKey[i];
3237
const value = char.charCodeAt(0) - 96;
3338
index = (index * PRIME_MULTIPLIER + value) % this.bucket.length;
34-
index = (index + PRIME_ADDER) % this.bucket.length;
39+
if (this.strongHash) {
40+
index = (index + PRIME_ADDER) % this.bucket.length;
41+
}
3542
}
3643
return index;
3744
}
@@ -113,7 +120,7 @@ class HashTable {
113120
* resize the hash table
114121
*/
115122
const loadFactor = Number((this.size / this.slot).toFixed(1));
116-
if (loadFactor > this.threshold) {
123+
if (loadFactor > this.threshold && this.allowResize) {
117124
// console.log('Resizing hash table');
118125
// eslint-disable-next-line no-underscore-dangle
119126
this._resize();
@@ -146,15 +153,17 @@ class HashTable {
146153
// eslint-disable-next-line no-underscore-dangle
147154
const vals = this._values(index, key);
148155

156+
let newHead = null; // to hold the start of the new SLL
149157
while (head !== null) {
150158
if (head.key === key) {
151159
// we have to delete current node
152-
head = head.next;
160+
newHead = head.next;
153161
}
162+
head = head.next;
154163
}
155164
// update the index with the lastest head value
156-
this.bucket[index] = head;
157-
return vals;
165+
this.bucket[index] = newHead;
166+
return { key: vals };
158167
}
159168

160169
getSize() {
@@ -181,7 +190,7 @@ class HashTable {
181190
// console.log(ht.bucket);
182191

183192
// console.log('deleting hello........');
184-
// ht.delete('hello');
193+
// console.log(ht.remove('hello'));
185194
// console.log(ht.bucket);
186195

187196
module.exports = HashTable;

0 commit comments

Comments
 (0)