-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashTable.js
74 lines (58 loc) · 1.43 KB
/
hashTable.js
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
class HashTable {
table = new Array(2);
item = 0;
set(key, value) {
this.item++;
const laodBalancer = this.item / this.table.length;
if (laodBalancer > 0.8) this.resize();
const id = this.hash(key, this.table.length);
if (this.table[id]) {
this.table[id].push([key, value]);
} else {
this.table[id] = [[key, value]];
}
}
get(key) {
const id = this.hash(key, this.table.length);
let store = this.table[id].find((x) => x[0] === key)[1];
return console.log(`${key}: =>`, store);
}
getAll() {
this.table;
}
getSize() {
console.log("");
console.log("size => ", this.table.length);
console.log("");
}
hash(key, size) {
let hash = 17;
for (let i = 0; i < key.length; i++) {
hash = (13 * hash * key.charCodeAt(i)) % size;
}
return hash;
}
resize() {
const resizeArray = new Array(this.table.length * 2);
this.table.forEach((element) => {
if (element) {
element.forEach(([key, value]) => {
const id = this.hash(key, resizeArray.length);
if (resizeArray[id]) {
resizeArray[id].push([key, value]);
} else {
resizeArray[id] = [[key, value]];
}
});
}
});
this.table = resizeArray;
}
}
let data = new HashTable();
data.getSize();
data.set("name", "Harsimran");
data.set("age", 24);
data.get("name");
data.get("age");
data.getSize();