-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearProbingHash.js
53 lines (39 loc) · 979 Bytes
/
LinearProbingHash.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
const Search = require("../Search")
class LinearProbingHash extends Search {
#keyValuePairsCount
#linearProbingTableSize
#keys
#values
constructor() {
super()
this.#keyValuePairsCount = 0
this.#linearProbingTableSize = 16
this.#keys = []
this.#values = []
}
put (key, value) {
let i
for(i = this.generateHash(key); !!this.#keys[i]; i++) {
const currentKey = this.#keys[i]
const areKeysEqual = this.compareValues(key, currentKey) === 0
if (areKeysEqual) {
this.#values[i] = value
return
}
}
this.#keys[i] = key
this.#values[i] = value
this.#keyValuePairsCount++
}
get (key) {
for (let i = this.generateHash(key); !!this.#keys[i]; i++) {
const currentKey = this.#keys[i]
const areKeysEqual = this.compareValues(key, currentKey) === 0
if (areKeysEqual) {
return this.#values[i]
}
}
return null
}
}
module.exports = LinearProbingHash