-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathhash-table.js
More file actions
34 lines (34 loc) · 972 Bytes
/
hash-table.js
File metadata and controls
34 lines (34 loc) · 972 Bytes
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
class HashTable {
constructor(size) {
// размер таблицы
this.size = size;
this.table = [];
}
hashKey(key) {
let hash = 0;
// «Хеш-код» как остаток от деления на число всех возможных «хешей»
hash = Math.floor(key % this.size);
return hash;
}
get(key) {
const address = this.hashKey(key);
return this.table[address].value;
}
set(key, value) {
const address = this.hashKey(key);
this.table[address] = { key: key, value: value };
}
remove(key) {
const address = this.hashKey(key);
delete this.table[address];
}
print() {
for (var i = 0; i < this.size; i++) {
if (!!this.table[i]) {
console.log(
`Key: ${this.table[i].key}, Value: ${this.table[i].value}`
);
}
}
}
}