-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhashmap.js
49 lines (40 loc) · 1.13 KB
/
hashmap.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
class HashMap {
constructor(size) {
this.initSize = 10;
this.size = size || this.initSize;
this.store = new Array(this.size);
}
hash(key) {
if (typeof key == 'string') {
let sum = 0;
for (let i = 0; i < key.length; i++)
sum += key.charCodeAt(i);
return sum % this.store.length;
}
else if (typeof key == 'number') {
return key % this.store.length;
};
}
add(key, value) {
this.store[this.hash(key)] = this.store[this.hash(key)] || [];
this.store[this.hash(key)].push({ key, value });
}
get(key) {
return this.store[this.hash(key)].find(el => el.key === key).value;
}
remove(key) {
//найти значение и удалить
}
clear() {
//очистить таблицу
}
each() {
//пройтись по всем ключам
}
increase() {
// создать массив аналогичной длины и concat
}
}
const map = new HashMap();
map.add('one', '505');
console.log(map.get('one'));