-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.js
96 lines (85 loc) · 1.92 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Hash Table
*/
export class HashTable {
table = new Array(200);
numItems = 0;
/**
* set item to the hash table
* @param {*} key
* @param {*} value
*/
setItem = (key, value) => {
// increment counter
this.numItems++;
// resize array size
const loadFactor = this.numItems / this.table.length;
if (loadFactor > 0.8) {
this.resizeArray();
}
// set item
const idx = this.hash(key, this.table.length);
if (this.table[idx]) {
this.table[idx].push([key, value]);
} else {
this.table[idx] = [[key, value]];
}
};
/**
* get item from hash table
* @param {*} key
*/
getItem = (key) => {
// index
const idx = this.hash(key, this.table.length);
// value at provided index not exists
if (!this.table[idx]) return null;
// find the item from the list
const item = this.table[idx].find(x => x[0] === key);
return !!item ? item[1] : null;
};
/**
* delete item from hash table
* @param {*} key
*/
deleteItem = (key) => {
// index
const idx = this.hash(key, this.table.length);
// value at provided index not exists
if (!this.table[idx]) return null;
// filter out the item from the list
const remainingItems = this.table[idx].filter(x => x[0] !== key);
this.table[idx] = remainingItems.length ? remainingItems : null;
};
/**
* resize array size
*/
resizeArray = () => {
const newTable = new Array(this.table.length * 2);
this.table.forEach(item => {
if (item && item.length) {
item.forEach(([key, value]) => {
const idx = this.hash(key, newTable.length);
if (newTable[idx]) {
newTable[idx].push([key, value]);
} else {
newTable[idx] = [[key, value]];
}
});
}
});
this.table = newTable;
};
/**
* hash string to index
* @param {*} key
* @param {*} tableSize
*/
hash(key, tableSize) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash += key.charCodeAt(i);
}
return hash % tableSize;
}
}