Debian Codegraph @

node/@xterm/xterm/src/vs/base/common/map.ts

raw
1 /*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 * Licensed under the MIT License. See License.txt in the project root for license information.
4 *--------------------------------------------------------------------------------------------*/
5
6 export function getOrSet<K, V>(map: Map<K, V>, key: K, value: V): V {
7 let result = map.get(key);
8 if (result === undefined) {
9 result = value;
10 map.set(key, result);
11 }
12
13 return result;
14 }
15
16 export function mapToString<K, V>(map: Map<K, V>): string {
17 const entries: string[] = [];
18 map.forEach((value, key) => {
19 entries.push(`${key} => ${value}`);
20 });
21
22 return `Map(${map.size}) {${entries.join(', ')}}`;
23 }
24
25 export function setToString<K>(set: Set<K>): string {
26 const entries: K[] = [];
27 set.forEach(value => {
28 entries.push(value);
29 });
30
31 return `Set(${set.size}) {${entries.join(', ')}}`;
32 }
33
34 export const enum Touch {
35 None = 0,
36 AsOld = 1,
37 AsNew = 2
38 }
39
40 export class CounterSet<T> {
41
42 private map = new Map<T, number>();
43
44 add(value: T): CounterSet<T> {
45 this.map.set(value, (this.map.get(value) || 0) + 1);
46 return this;
47 }
48
49 delete(value: T): boolean {
50 let counter = this.map.get(value) || 0;
51
52 if (counter === 0) {
53 return false;
54 }
55
56 counter--;
57
58 if (counter === 0) {
59 this.map.delete(value);
60 } else {
61 this.map.set(value, counter);
62 }
63
64 return true;
65 }
66
67 has(value: T): boolean {
68 return this.map.has(value);
69 }
70 }
71
72 /**
73 * A map that allows access both by keys and values.
74 * **NOTE**: values need to be unique.
75 */
76 export class BidirectionalMap<K, V> {
77
78 private readonly _m1 = new Map<K, V>();
79 private readonly _m2 = new Map<V, K>();
80
81 constructor(entries?: readonly (readonly [K, V])[]) {
82 if (entries) {
83 for (const [key, value] of entries) {
84 this.set(key, value);
85 }
86 }
87 }
88
89 clear(): void {
90 this._m1.clear();
91 this._m2.clear();
92 }
93
94 set(key: K, value: V): void {
95 this._m1.set(key, value);
96 this._m2.set(value, key);
97 }
98
99 get(key: K): V | undefined {
100 return this._m1.get(key);
101 }
102
103 getKey(value: V): K | undefined {
104 return this._m2.get(value);
105 }
106
107 delete(key: K): boolean {
108 const value = this._m1.get(key);
109 if (value === undefined) {
110 return false;
111 }
112 this._m1.delete(key);
113 this._m2.delete(value);
114 return true;
115 }
116
117 forEach(callbackfn: (value: V, key: K, map: BidirectionalMap<K, V>) => void, thisArg?: any): void {
118 this._m1.forEach((value, key) => {
119 callbackfn.call(thisArg, value, key, this);
120 });
121 }
122
123 keys(): IterableIterator<K> {
124 return this._m1.keys();
125 }
126
127 values(): IterableIterator<V> {
128 return this._m1.values();
129 }
130 }
131
132 export class SetMap<K, V> {
133
134 private map = new Map<K, Set<V>>();
135
136 add(key: K, value: V): void {
137 let values = this.map.get(key);
138
139 if (!values) {
140 values = new Set<V>();
141 this.map.set(key, values);
142 }
143
144 values.add(value);
145 }
146
147 delete(key: K, value: V): void {
148 const values = this.map.get(key);
149
150 if (!values) {
151 return;
152 }
153
154 values.delete(value);
155
156 if (values.size === 0) {
157 this.map.delete(key);
158 }
159 }
160
161 forEach(key: K, fn: (value: V) => void): void {
162 const values = this.map.get(key);
163
164 if (!values) {
165 return;
166 }
167
168 values.forEach(fn);
169 }
170
171 get(key: K): ReadonlySet<V> {
172 const values = this.map.get(key);
173 if (!values) {
174 return new Set<V>();
175 }
176 return values;
177 }
178 }
179
180 export function mapsStrictEqualIgnoreOrder(a: Map<unknown, unknown>, b: Map<unknown, unknown>): boolean {
181 if (a === b) {
182 return true;
183 }
184
185 if (a.size !== b.size) {
186 return false;
187 }
188
189 for (const [key, value] of a) {
190 if (!b.has(key) || b.get(key) !== value) {
191 return false;
192 }
193 }
194
195 for (const [key] of b) {
196 if (!a.has(key)) {
197 return false;
198 }
199 }
200
201 return true;
202 }