@@ -8,7 +8,7 @@ class HashTable {
8
8
// and help to resize when the table is half filled
9
9
this . size = 0 ;
10
10
// threshold (let it be 70%)
11
- this . threshold = 0.7 ;
11
+ this . threshold = 0.8 ;
12
12
// the main bucket
13
13
this . bucket = new Array ( this . slot ) ;
14
14
@@ -21,15 +21,17 @@ class HashTable {
21
21
const stringKey = String ( key ) ;
22
22
23
23
let index = 0 ;
24
- const PRIME = 1801 ;
24
+ const PRIME_MULTIPLIER = 1801 ; // Random prime number
25
+ const PRIME_ADDER = 2029 ; // Random prime number
25
26
26
- // loop till the length of the key or mamx 100
27
+ // loop till the length of the key or max 100
27
28
const loopTill = Math . min ( stringKey . length , 100 ) ;
28
29
29
30
for ( let i = 0 ; i < loopTill ; i += 1 ) {
30
31
const char = stringKey [ i ] ;
31
32
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 ;
33
35
}
34
36
return index ;
35
37
}
@@ -47,7 +49,6 @@ class HashTable {
47
49
48
50
for ( let i = 0 ; i < oldSlot ; i += 1 ) {
49
51
if ( oldBucket [ i ] ) {
50
- // get all the nodes associated here
51
52
let head = oldBucket [ i ] ;
52
53
53
54
while ( head !== null ) {
@@ -64,8 +65,8 @@ class HashTable {
64
65
65
66
_push ( index , value ) {
66
67
/**
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
69
70
*/
70
71
const node = new HashEntry ( value ) ;
71
72
if ( ! this . bucket [ index ] ) {
@@ -87,7 +88,7 @@ class HashTable {
87
88
88
89
_values ( index , key ) {
89
90
/**
90
- * Util to return the values as an array
91
+ * Utility to return the values as an array for a given key
91
92
*/
92
93
const res = [ ] ;
93
94
let head = this . bucket [ index ] ;
@@ -111,7 +112,7 @@ class HashTable {
111
112
* calculate the load factor, if it's greater than threshold
112
113
* resize the hash table
113
114
*/
114
- const loadFactor = ( this . size / this . slot ) . toFixed ( 1 ) ;
115
+ const loadFactor = Number ( ( this . size / this . slot ) . toFixed ( 1 ) ) ;
115
116
if ( loadFactor > this . threshold ) {
116
117
// console.log('Resizing hash table');
117
118
// eslint-disable-next-line no-underscore-dangle
@@ -120,7 +121,7 @@ class HashTable {
120
121
}
121
122
122
123
get ( key ) {
123
- // get the index
124
+ // get the index for the given key
124
125
// eslint-disable-next-line no-underscore-dangle
125
126
const index = this . _hash ( key ) ;
126
127
if ( ! this . bucket [ index ] ) return null ;
@@ -137,14 +138,18 @@ class HashTable {
137
138
}
138
139
}
139
140
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' ) ;
145
150
146
151
// console.log(ht.get('hello'));
147
152
// console.log(ht.get('maroon'));
148
- // console.log(ht.bucket);
153
+ console . log ( ht . bucket ) ;
149
154
150
155
module . exports = HashTable ;
0 commit comments