1
1
export default class HashTable {
2
2
constructor ( limit = 100 ) {
3
+ // Initialize the storage and limit variables
3
4
this . storage = new Array ( limit )
4
5
this . limit = limit
5
6
}
6
7
8
+ // Hash function:
7
9
_hash ( key , max ) {
10
+ // Initialize the hash variable to 0
8
11
let hash = 0
12
+ // Iterate through the key and add the character code at each iteration to the hash variable
9
13
for ( let i = 0 ; i < key . length ; i ++ ) hash += key . charCodeAt ( i )
14
+ // Return the hash modulo the max
10
15
return hash % max
11
16
}
12
17
18
+ // Insert a key-value pair into the hash table
13
19
set ( key , value ) {
20
+ // Hash the key
14
21
const index = this . _hash ( key , this . limit )
22
+ // If the index is empty, insert the key-value pair and create a bucket
15
23
if ( this . storage [ index ] === undefined ) this . storage [ index ] = [ [ key , value ] ]
16
24
else {
25
+ // If the index is not empty, iterate through the bucket (collision handling)
17
26
let inserted = false
18
27
for ( let i = 0 ; i < this . storage [ index ] . length ; i ++ ) {
19
28
if ( this . storage [ index ] [ i ] [ 0 ] === key ) {
29
+ // If the key exists, update the value
20
30
this . storage [ index ] [ i ] [ 1 ] = value
21
31
inserted = true
22
32
}
23
33
}
34
+ // If the key does not exist, insert the key-value pair
24
35
if ( inserted === false ) this . storage [ index ] . push ( [ key , value ] )
25
36
}
26
37
}
27
38
28
39
// Get a value from the hash table
29
40
get ( key ) {
41
+ // Hash the key
30
42
const index = this . _hash ( key , this . limit )
43
+ // If the index is empty, return undefined
31
44
if ( this . storage [ index ] === undefined ) return undefined
32
45
else {
46
+ // If the index is not empty, iterate through the bucket
33
47
for ( let i = 0 ; i < this . storage [ index ] . length ; i ++ ) {
48
+ // If the key exists, return the value
34
49
if ( this . storage [ index ] [ i ] [ 0 ] === key ) return this . storage [ index ] [ i ] [ 1 ]
35
50
}
36
51
}
37
52
}
38
53
54
+ // Remove a key-value pair from the hash table
39
55
remove ( key ) {
56
+ // Hash the key
40
57
const index = this . _hash ( key , this . limit )
58
+ // Check if the bucket exists
41
59
if ( this . storage [ index ] ) {
60
+ // If the key matches the key at the index and there is only one item in the bucket, delete the bucket
42
61
if (
43
62
this . storage [ index ] . length === 1 &&
44
63
this . storage [ index ] [ 0 ] [ 0 ] === key
45
64
) {
46
65
delete this . storage [ index ]
47
66
} else {
67
+ // If the index is not empty, iterate through the bucket
48
68
for ( let i = 0 ; i < this . storage [ index ] . length ; i ++ ) {
69
+ // If the key exists, delete the key-value pair
49
70
if ( this . storage [ index ] [ i ] [ 0 ] === key ) delete this . storage [ index ] [ i ]
50
71
}
51
72
}
52
73
}
53
74
}
54
75
76
+ // Check if a key exists in the hash table
55
77
has ( key ) {
78
+ // Hash the key to find the index
56
79
const index = this . _hash ( key , this . limit )
80
+ // Check if the bucket at the index exists
57
81
if ( this . storage [ index ] ) {
82
+ // Iterate through the bucket's key-value pairs
58
83
for ( let i = 0 ; i < this . storage [ index ] . length ; i ++ ) {
84
+ // Compare the current key with the target key and if the key is found, return true
59
85
if ( this . storage [ index ] [ i ] [ 0 ] === key ) return true
60
86
}
61
87
}
88
+ // If the key is not found, return false
62
89
return false
63
90
}
64
91
92
+ // Print all keys/values in the table
65
93
printTable ( ) {
66
94
for ( let i = 0 ; i < this . storage . length ; i ++ ) {
67
95
if ( this . storage [ i ] !== undefined )
@@ -70,6 +98,7 @@ export default class HashTable {
70
98
}
71
99
}
72
100
101
+ // Clear all key/values
73
102
clear ( ) {
74
103
this . storage = new Array ( this . limit )
75
104
}
0 commit comments