-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.js
More file actions
159 lines (135 loc) · 3.85 KB
/
HashMap.js
File metadata and controls
159 lines (135 loc) · 3.85 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
export default class {
#buckets;
#capacity = 16;
#nItems = 0;
#nUsedBuckets = 0;
#loadFactor = 0.75;
constructor() {
this.#buckets = new Array(this.#capacity);
this.#buckets.fill(null)
}
get length() {return this.#nItems}
get nUsedBuckets() {return this.#nUsedBuckets}
get capacity() {return this.#capacity}
#hash(key) {
let hashCode = 0;
const primeNumber = 31;
for (let i = 0; i < key.length; i++) {
hashCode = (primeNumber * hashCode + key.charCodeAt(i)) % this.#capacity;
}
return hashCode;
}
// Doubles the number of buckets (also rehashes entries)
#resizeBuckets() {
const oldBuckets = this.#buckets;
this.#capacity = this.#capacity * 2;
this.clear();
oldBuckets.forEach((bucket) => {
if(bucket != null) {
bucket.forEach((elem) => {
this.set(elem.key, elem.value);
})
}
})
}
set(key, value) {
const index = this.#hash(key);
if(this.#buckets[index] === null) {
this.#buckets[index] = [];
this.#buckets[index].push({key, value})
this.#nItems++;
this.#nUsedBuckets++;
if((this.#nItems / this.#capacity) > this.#loadFactor) {
this.#resizeBuckets();
}
return
}
const foundPair = this.#buckets[index].find(elem => elem.key === key)
if(foundPair === undefined) {
this.#buckets[index].push({key, value});
this.#nItems++;
if((this.#nItems / this.#capacity) > this.#loadFactor) {
this.#resizeBuckets();
}
}
else {
foundPair.value = value;
}
}
get(key) {
const index = this.#hash(key);
if(this.#buckets[index] === null) {
return null;
}
const foundPair = this.#buckets[index].find(elem => elem.key === key);
if(foundPair === undefined) {
return null;
}
else {
return foundPair.value;
}
}
has(key) {
const foundValue = this.get(key);
if(foundValue === null) {
return false;
}
else {
return true;
}
}
remove(key) {
if(this.has(key)){
const index = this.#hash(key);
const foundList = this.#buckets[index];
const elemIndex = foundList.indexOf(foundList.find(elem => elem.key === key));
foundList.splice(elemIndex, 1);
if(foundList.length === 0) {
this.#buckets[index] = null;
this.#nUsedBuckets--;
}
this.#nItems--;
return true;
}
return false
}
clear() {
this.#buckets = new Array(this.#capacity);
this.#buckets.fill(null);
this.#nItems = 0;
this.#nUsedBuckets = 0;
}
keys() {
const keyArray = [];
this.#buckets.forEach((bucket) => {
if(bucket != null) {
bucket.forEach((elem) => {
keyArray.unshift(elem.key);
})
}
})
return keyArray;
}
values() {
const valueArray = [];
this.#buckets.forEach((bucket) => {
if(bucket != null) {
bucket.forEach((elem) => {
valueArray.unshift(elem.value);
})
}
})
return valueArray;
}
entries() {
const entryArray = [];
this.#buckets.forEach((bucket) => {
if(bucket != null) {
bucket.forEach((elem) => {
entryArray.unshift([elem.key, elem.value]);
})
}
})
return entryArray;
}
}