-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathMapUtilities.ts
95 lines (80 loc) · 1.96 KB
/
MapUtilities.ts
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
// Copyright (c) 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
export const inverse = function<K, V>(map: Map<K, V>): Multimap<V, K> {
const result = new Multimap<V, K>();
for (const [key, value] of map.entries()) {
result.set(value, key);
}
return result;
};
export class Multimap<K, V> {
private map = new Map<K, Set<V>>();
set(key: K, value: V): void {
let set = this.map.get(key);
if (!set) {
set = new Set();
this.map.set(key, set);
}
set.add(value);
}
get(key: K): Set<V> {
return this.map.get(key) || new Set();
}
has(key: K): boolean {
return this.map.has(key);
}
hasValue(key: K, value: V): boolean {
const set = this.map.get(key);
if (!set) {
return false;
}
return set.has(value);
}
get size(): number {
return this.map.size;
}
delete(key: K, value: V): boolean {
const values = this.get(key);
if (!values) {
return false;
}
const result = values.delete(value);
if (!values.size) {
this.map.delete(key);
}
return result;
}
deleteAll(key: K): void {
this.map.delete(key);
}
keysArray(): K[] {
return [...this.map.keys()];
}
keys(): IterableIterator<K> {
return this.map.keys();
}
valuesArray(): V[] {
const result = [];
for (const set of this.map.values()) {
result.push(...set.values());
}
return result;
}
clear(): void {
this.map.clear();
}
}
/**
* Gets value for key, assigning a default if value is falsy.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export function getWithDefault<K extends {}, V>(
map: WeakMap<K, V>|Map<K, V>, key: K, defaultValueFactory: (key?: K) => V): V {
let value = map.get(key);
if (value === undefined || value === null) {
value = defaultValueFactory(key);
map.set(key, value);
}
return value;
}