-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashTable.js
85 lines (71 loc) · 1.73 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
class HashMap {
length = null
constructor(length = 10) {
this.length = length
}
_map = new Map()
static _hasher = function (str, len) {
let hash = 0
for (let char of str) {
hash = (hash + char.charCodeAt(0)) % len
}
return hash
}
_getKey(str) {
return HashMap._hasher(str, this.length)
}
set(key, value) {
const keyForMap = this._getKey(key)
const currValues = this._map.get(keyForMap) || []
this._map.set(keyForMap, [...currValues, [key, value]])
}
get(key) {
const keyForMap = this._getKey(key)
const currValues = this._map.get(keyForMap)
if (currValues === undefined) {
return undefined
}
return currValues.find(([currKey]) => currKey === key)[1]
}
delete(key) {
const keyForMap = this._getKey(key)
const currValues = this._map.get(keyForMap)
const currIndex = currValues.findIndex(([currKey]) => {
return key === currKey
});
if (currIndex === undefined) {
return undefined
}
const toDelete = currValues[currIndex]
currValues.splice(currIndex, 1)
return toDelete[1]
}
update(key, value) {
const keyForMap = this._getKey(key)
const currValues = this._map.get(keyForMap)
const currIndex = currValues.findIndex(([currKey]) => {
return key === currKey
});
currValues[currIndex][1] = value
return true
}
}
const hash = new HashMap()
hash.set('a', 1)
hash.set('b', 2)
hash.set('c', 3)
hash.set('d', 4)
hash.set('e', 5)
hash.set('f', 6)
hash.set('g', 7)
hash.set('h', 8)
hash.set('i', 9)
hash.set('j', 10)
hash.set('k', 11)
hash.set('l', 12)
hash.set('m', 13)
hash.set('n', 14)
console.log(hash.get('a'))
hash.update('k', 100)
console.log(hash.delete('k'))
console.log(hash._map)