Skip to content

Commit 9948ad2

Browse files
HashMap.js.
0 parents  commit 9948ad2

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

src/HashMap.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
Copyright (c) 2019 LieutenantPeacock
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
*/
10+
class HashMap {
11+
#map
12+
#size = 0
13+
14+
constructor(map){
15+
this.#map = map instanceof HashMap ? new Map(map.#map) : new Map;
16+
}
17+
18+
put(key, value){
19+
let arr = this.#getBucket(key);
20+
if(!arr) this.#map.set(HashMap.#hash(key), arr = []);
21+
const idx = this.#findKeyIndex(key);
22+
let prev;
23+
if(idx !== -1){
24+
prev = arr[idx].value;
25+
arr[idx].value = value;
26+
} else {
27+
arr.push({key,value});
28+
this.#size++;
29+
}
30+
return prev;
31+
}
32+
33+
putIfAbsent(key, value){
34+
if(!this.containsKey(key)){
35+
return this.put(key, value);
36+
}
37+
}
38+
39+
get(key){
40+
const idx = this.#findKeyIndex(key);
41+
if(idx !== -1) return this.#getBucket(key)[idx].value;
42+
}
43+
44+
getOrDefault(key, defaultValue){
45+
return containsKey(key) ? get(key) : defaultValue;
46+
}
47+
48+
containsKey(key){
49+
return this.#findKeyIndex(key) !== -1;
50+
}
51+
52+
containsValue(value){
53+
for(const arr of this.#map.values()){
54+
if(arr.some(entry => HashMap.#equals(entry.value, value))){
55+
return true;
56+
}
57+
}
58+
return false;
59+
}
60+
61+
clear(){
62+
this.#size = 0;
63+
this.#map.clear();
64+
}
65+
66+
remove(key){
67+
const idx = this.#findKeyIndex(key);
68+
if(idx !== -1){
69+
this.#size--;
70+
return this.#getBucket(key).splice(idx, 1)[0].value;
71+
}
72+
}
73+
74+
forEach(consumer){
75+
[...this].forEach(arr => consumer(arr[0], arr[1]));
76+
}
77+
78+
isEmpty(){
79+
return this.#size === 0;
80+
}
81+
82+
size(){
83+
return this.#size;
84+
}
85+
86+
hashCode(){
87+
let h = 0;
88+
for(const [hash, vals] of this.#map){
89+
if(arr.length) h += hash * vals.length;
90+
}
91+
return h;
92+
}
93+
94+
equals(obj){
95+
if(!(obj instanceof HashMap) || this.size() != obj.size()) return false;
96+
for(const val of this.#map.values()){
97+
if(val.some(({key, value})=>!obj.containsKey(key) || !HashMap.#equals(value, obj.get(key)))) return false;
98+
}
99+
return true;
100+
}
101+
102+
[Symbol.iterator]() {
103+
return [...this.#map.values()].flatMap(arr => arr.map(({key,value})=>[key,value])).values();
104+
}
105+
106+
#findKeyIndex(key){
107+
const arr = this.#getBucket(key);
108+
return arr ? arr.findIndex(entry => HashMap.#equals(entry.key, key)): -1;
109+
}
110+
111+
#getBucket(key){
112+
return this.#map.get(HashMap.#hash(key));
113+
}
114+
115+
static #hash(key){
116+
return typeof key?.hashCode !== 'function' ? key: key.hashCode();
117+
}
118+
119+
static #equals(a, b){
120+
return a === b || typeof a?.equals === 'function' && a.equals(b);
121+
}
122+
}

0 commit comments

Comments
 (0)