-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash-table.js
72 lines (65 loc) · 1.84 KB
/
hash-table.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
// complete in sometime
class HashTable {
constructor(size) {
this.table = new Array(size);
this.totalItems = 0;
}
_resize() {
const newTable = new Array(this.table.length * 2);
this.table.forEach(item => {
if (item) {
item.forEach(([key, value]) => {
const idx = this._hashStringtoInt(key, newTable.length);
if (newTable[idx]) {
newTable[idx].push([key, value]);
} else {
newTable[idx] = [[key, value]];
}
});
}
})
this.table = newTable;
}
_hashStringtoInt(str, tableSize) {
let hash = 13;
for (let i = 0; i < str.length; i++) {
hash = (17 * hash * str.charCodeAt(i)) % tableSize;
}
return hash;
}
setItem(key, value) {
const idx = this._hashStringtoInt(key, this.table.length);
if (this.totalItems / this.table.length > 0.8) { // load factor
this._resize();
}
if (this.table[idx]) {
// if (this.table[idx].find(item => item[0] === key)) {
// return -1;
// }
this.table[idx].push([key, value]);
} else {
this.table[idx] = [[key, value]];
}
this.totalItems++;
}
getItem(key) {
try {
const idx = this._hashStringtoInt(key, this.table.length);
return this.table[idx].find(arr => arr[0] === key)[1];
} catch (error) {
return undefined;
}
}
}
const hashTable = new HashTable(3);
console.log('----------------- result ------------------')
hashTable.setItem('lastName', 'saif');
hashTable.setItem('bro', 'omar');
hashTable.setItem('welcome', 'ahmed');
hashTable.setItem('hello', 'welcome');
hashTable.setItem('email', 'alhashmi');
hashTable.setItem('password', 'nobody');
console.log(hashTable.getItem('email'))
console.log(hashTable.getItem('name'))
console.log(hashTable.getItem('welcome'))
console.log(hashTable.getItem('this'))