Skip to content

Commit 44c158a

Browse files
committed
fixes: better hash function, improved comments
1 parent ae5ea99 commit 44c158a

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/_DataStructures_/HashTable/index.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class HashTable {
88
// and help to resize when the table is half filled
99
this.size = 0;
1010
// threshold (let it be 70%)
11-
this.threshold = 0.7;
11+
this.threshold = 0.8;
1212
// the main bucket
1313
this.bucket = new Array(this.slot);
1414

@@ -21,15 +21,17 @@ class HashTable {
2121
const stringKey = String(key);
2222

2323
let index = 0;
24-
const PRIME = 1801;
24+
const PRIME_MULTIPLIER = 1801; // Random prime number
25+
const PRIME_ADDER = 2029; // Random prime number
2526

26-
// loop till the length of the key or mamx 100
27+
// loop till the length of the key or max 100
2728
const loopTill = Math.min(stringKey.length, 100);
2829

2930
for (let i = 0; i < loopTill; i += 1) {
3031
const char = stringKey[i];
3132
const value = char.charCodeAt(0) - 96;
32-
index = (index + PRIME + value) % this.bucket.length;
33+
index = (index * PRIME_MULTIPLIER + value) % this.bucket.length;
34+
index = (index + PRIME_ADDER) % this.bucket.length;
3335
}
3436
return index;
3537
}
@@ -47,7 +49,6 @@ class HashTable {
4749

4850
for (let i = 0; i < oldSlot; i += 1) {
4951
if (oldBucket[i]) {
50-
// get all the nodes associated here
5152
let head = oldBucket[i];
5253

5354
while (head !== null) {
@@ -64,8 +65,8 @@ class HashTable {
6465

6566
_push(index, value) {
6667
/**
67-
* Util to add a SSL to the index in case of more than once
68-
* value for the same key exixts
68+
* Utility to add a SLL to the index in case of more than one
69+
* key hashes to the same index
6970
*/
7071
const node = new HashEntry(value);
7172
if (!this.bucket[index]) {
@@ -87,7 +88,7 @@ class HashTable {
8788

8889
_values(index, key) {
8990
/**
90-
* Util to return the values as an array
91+
* Utility to return the values as an array for a given key
9192
*/
9293
const res = [];
9394
let head = this.bucket[index];
@@ -111,7 +112,7 @@ class HashTable {
111112
* calculate the load factor, if it's greater than threshold
112113
* resize the hash table
113114
*/
114-
const loadFactor = (this.size / this.slot).toFixed(1);
115+
const loadFactor = Number((this.size / this.slot).toFixed(1));
115116
if (loadFactor > this.threshold) {
116117
// console.log('Resizing hash table');
117118
// eslint-disable-next-line no-underscore-dangle
@@ -120,7 +121,7 @@ class HashTable {
120121
}
121122

122123
get(key) {
123-
// get the index
124+
// get the index for the given key
124125
// eslint-disable-next-line no-underscore-dangle
125126
const index = this._hash(key);
126127
if (!this.bucket[index]) return null;
@@ -137,14 +138,18 @@ class HashTable {
137138
}
138139
}
139140

140-
// const ht = new HashTable(5);
141-
// ht.set('hello', 'I am a new value');
142-
// ht.set('hello', 'I am a yet another value');
143-
// ht.set('maroon', 'I maroon');
144-
// ht.set('yellow', 'I am yellow');
141+
const ht = new HashTable(5);
142+
console.log('HT slots = ', ht.slot);
143+
ht.set('maroon', 'I maroon');
144+
ht.set('hello', 'I am a new value');
145+
console.log(ht.bucket);
146+
ht.set('hell', 'Bad value');
147+
ht.set('hello', 'I am a yet another value');
148+
console.log('HT slots = ', ht.slot);
149+
ht.set('yellow', 'I am yellow');
145150

146151
// console.log(ht.get('hello'));
147152
// console.log(ht.get('maroon'));
148-
// console.log(ht.bucket);
153+
console.log(ht.bucket);
149154

150155
module.exports = HashTable;

0 commit comments

Comments
 (0)