Repository navigation
Expand file tree
/
Copy pathhashTable.js
More file actions
123 lines (111 loc) · 2.95 KB
/
Copy pathhashTable.js
File metadata and controls
123 lines (111 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Hash table
*/
class HashTable {
constructor(size = 10) {
this.table = new Array(parseInt(size));
this.size = size;
this.count = 0;
}
/**
* Get the hash value of a key.
* There are many hash algorithms available.
* Here is just one simple example.
* @param {string} key - The key to get the hash value of.
* @returns {number} The hash value of the key.
*/
hash(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % this.size;
}
/**
* Resize the hash table to a new size.
* This method will rehash all existing key-value pairs and insert them into the new table.
* @param {number} newSize - The new size of the hash table.
*/
resize(newSize) {
newSize = parseInt(newSize);
const oldTable = this.table;
this.table = new Array(newSize);
this.size = newSize;
this.count = 0;
oldTable.forEach((bucket) => {
if (bucket) {
bucket.forEach(([key, value]) => {
this.put(key, value);
});
}
});
}
/**
* Inserts a key-value pair into the hash table.
* If the load factor exceeds 0.75, the table is resized to double its current size.
* @param {string} key - The key to insert.
* @param {*} value - The value to insert.
*/
put(key, value) {
const index = this.hash(key);
let bucket = this.table[index];
if (!bucket) {
this.table[index] = bucket = [];
}
// Overwrite the value if the key already exists
for (let i = 0; i < bucket.length; i++) {
if (bucket[i][0] === key) {
bucket[i][1] = value;
return;
}
}
this.table[index].push([key, value]);
this.count++;
if (this.count > this.size * 0.75) {
this.resize(this.size * 2);
}
}
/**
* Get the value of a key in the hash table.
* @param {string} key - The key to get the value of.
* @returns {*} The value of the key. Returns undefined if the key is not found.
*/
get(key) {
const index = this.hash(key);
const bucket = this.table[index];
if (!bucket) {
return undefined;
}
for (const [k, v] of bucket) {
if (k === key) {
return v;
}
}
return undefined;
}
/**
* Remove a key-value pair from the hash table.
* If the load factor falls below 0.25, the table is resized to half its current size.
* @param {string} key - The key to remove.
* @returns {*} The value of the removed key. Returns undefined if the key is not found.
*/
remove(key) {
const index = this.hash(key);
const bucket = this.table[index];
if (!bucket) {
return;
}
for (let i = 0; i < bucket.length; i++) {
const node = bucket[i];
if (node[0] === key) {
bucket.splice(i, 1);
this.count--;
if (this.count < this.size * 0.25) {
this.resize(this.size / 2);
}
return node[1];
}
}
}
}
module.exports = { HashTable };