-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrongMapOfStrongSets.mts
386 lines (333 loc) · 9.03 KB
/
StrongMapOfStrongSets.mts
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* @file
* This is generated code. Do not edit.
*
* Generator: https://github.com/ajvincent/composite-collection/
* Template: Strong/OneMapOfOneStrongSet
* @license MPL-2.0
* @author Alexander J. Vincent <[email protected]>
* @copyright © 2021-2022 Alexander J. Vincent
*/
import { DefaultMap } from "./keys/DefaultMap.mjs";
class StrongMapOfStrongSets<
__MK0__,
__SK0__
>
{
/** @typedef {Set<*>} __StrongMapOfStrongSets__InnerMap__ */
/** @type {Map<*, __StrongMapOfStrongSets__InnerMap__>} @constant */
#outerMap: DefaultMap<__MK0__, Set<__SK0__>> = new DefaultMap();
/** @type {number} */
#sizeOfAll = 0;
constructor(iterable?: [__MK0__, __SK0__][]) {
if (iterable) {
for (const [mapKey, setKey] of iterable) {
this.add(mapKey, setKey);
}
}
}
/**
* The number of elements in this collection.
*
* @returns {number} The element count.
* @public
* @constant
*/
get size() : number
{
return this.#sizeOfAll;
}
/**
* Get the size of a particular set.
*
* @param {*} mapKey The map key.
* @returns {number} The set size.
* @public
*/
getSizeOfSet(mapKey: __MK0__) : number
{
const __innerSet__ = this.#outerMap.get(mapKey)
return __innerSet__?.size || 0;
}
/**
* The number of maps in this collection.
*
* @returns {number} The map count.
* @public
* @constant
*/
get mapSize() : number
{
return this.#outerMap.size;
}
/**
* Add a key set to this collection.
*
* @param {*} mapKey The map key.
* @param {*} setKey The set key.
* @returns {StrongMapOfStrongSets} This collection.
* @public
*/
add(mapKey: __MK0__, setKey: __SK0__) : this
{
const __innerSet__ = this.#outerMap.getDefault(mapKey, () => new Set);
if (!__innerSet__.has(setKey)) {
__innerSet__.add(setKey);
this.#sizeOfAll++;
}
return this;
}
/**
* Add several sets to a map in this collection.
*
* @param {*} mapKey The map key.
* @param {Set[]} __sets__ The sets to add.
* @returns {StrongMapOfStrongSets} This collection.
* @public
*/
addSets(mapKey: __MK0__, __sets__: [__SK0__][]) : this
{
if (__sets__.length === 0)
return this;
const __innerSet__ = this.#outerMap.getDefault(mapKey, () => new Set);
__sets__.forEach(([setKey]) => {
if (!__innerSet__.has(setKey)) {
__innerSet__.add(setKey);
this.#sizeOfAll++;
}
});
return this;
}
/**
* Clear the collection.
*
* @public
*/
clear() : void
{
this.#outerMap.clear();
this.#sizeOfAll = 0;
}
/**
* Delete an element from the collection by the given key sequence.
*
* @param {*} mapKey The map key.
* @param {*} setKey The set key.
* @returns {boolean} True if we found the value and deleted it.
* @public
*/
delete(mapKey: __MK0__, setKey: __SK0__) : boolean
{
const __innerSet__ = this.#outerMap.get(mapKey)
if (!__innerSet__)
return false;
if (!__innerSet__.has(setKey))
return false;
__innerSet__.delete(setKey);
this.#sizeOfAll--;
if (__innerSet__.size === 0) {
this.#outerMap.delete(mapKey);
}
return true;
}
/**
* Delete all sets from the collection by the given map sequence.
*
* @param {*} mapKey The map key.
* @returns {boolean} True if we found the value and deleted it.
* @public
*/
deleteSets(mapKey: __MK0__) : boolean
{
const __innerSet__ = this.#outerMap.get(mapKey)
if (!__innerSet__)
return false;
this.#outerMap.delete(mapKey);
this.#sizeOfAll -= __innerSet__.size;
return true;
}
/**
* Iterate over the keys.
*
* @param {__StrongMapOfStrongSets_ForEachCallback__} __callback__ A function to invoke for each iteration.
* @param {object} __thisArg__ Value to use as this when executing callback.
* @public
*/
forEach(
__callback__: (
mapKey: __MK0__,
setKey: __SK0__,
__collection__: StrongMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
) : void
{
this.#outerMap.forEach(
(__innerSet__, mapKey) => __innerSet__.forEach(
setKey => __callback__.apply(__thisArg__, [mapKey, setKey, this])
)
);
}
/**
* An user-provided callback to .forEach().
*
* @callback __StrongMapOfStrongSets_ForEachCallback__
* @param {*} mapKey The map key.
* @param {*} setKey The set key.
* @param {StrongMapOfStrongSets} __collection__ This collection.
*/
/**
* Iterate over the map keys.
*
* @param {__StrongMapOfStrongSets_ForEachMapCallback__} __callback__ A function to invoke for each iteration.
* @param {object} __thisArg__ Value to use as this when executing callback.
* @public
*/
forEachMap(
__callback__: (
mapKey: __MK0__,
__collection__: StrongMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
) : void
{
for (const mapKey of this.#outerMap.keys()) {
__callback__.apply(__thisArg__, [mapKey, this]);
}
}
/**
* An user-provided callback to .forEachMap().
*
* @callback __StrongMapOfStrongSets_ForEachMapCallback__
* @param {*} mapKey The map key.
* @param {StrongMapOfStrongSets} __collection__ This collection.
*/
/**
* Iterate over the keys under a map in this collection.
*
* @param {*} mapKey The map key.
* @param {__StrongMapOfStrongSets_ForEachCallback__} __callback__ A function to invoke for each iteration.
* @param {object} __thisArg__ Value to use as this when executing callback.
* @public
*/
forEachSet(
mapKey: __MK0__,
__callback__: (
mapKey: __MK0__,
setKey: __SK0__,
__collection__: StrongMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
): void
{
const __innerSet__ = this.#outerMap.get(mapKey)
if (!__innerSet__)
return;
__innerSet__.forEach(
setKey => __callback__.apply(__thisArg__, [mapKey, setKey, this])
);
}
/**
* Report if the collection has a value for a key set.
*
* @param {*} mapKey The map key.
* @param {*} setKey The set key.
* @returns {boolean} True if the key set refers to a value in the collection.
* @public
*/
has(mapKey: __MK0__, setKey: __SK0__) : boolean
{
const __innerSet__ = this.#outerMap.get(mapKey)
if (!__innerSet__)
return false;
return __innerSet__.has(setKey);
}
/**
* Report if the collection has any sets for a map.
*
* @param {*} mapKey The map key.
* @returns {boolean} True if the key set refers to a value in the collection.
* @public
*/
hasSets(mapKey: __MK0__) : boolean
{
const __innerSet__ = this.#outerMap.get(mapKey)
return Boolean(__innerSet__);
}
/**
* Yield the values of the collection.
*
* @yields {*} The value.
* @public
*/
* values() : IterableIterator<[__MK0__, __SK0__]>
{
const __outerIter__ = this.#outerMap.entries();
for (const [mapKey, __innerSet__] of __outerIter__) {
for (const setKey of __innerSet__.values())
yield [mapKey, setKey];
}
}
/**
* Yield the sets of the collection in a map.
*
* @param {*} mapKey The map key.
* @yields {*} The sets.
* @public
*/
* valuesSet(mapKey: __MK0__) : IterableIterator<[__MK0__, __SK0__]>
{
const __innerSet__ = this.#outerMap.get(mapKey)
if (!__innerSet__)
return;
for (const setKey of __innerSet__.values())
yield [mapKey, setKey];
}
[Symbol.iterator]() : IterableIterator<[__MK0__, __SK0__]>
{
return this.values();
}
[Symbol.toStringTag] = "StrongMapOfStrongSets";
}
Object.freeze(StrongMapOfStrongSets);
Object.freeze(StrongMapOfStrongSets.prototype);
export type ReadonlyStrongMapOfStrongSets<
__MK0__,
__SK0__
> =
Pick<
StrongMapOfStrongSets<__MK0__, __SK0__>,
"size" | "getSizeOfSet" | "mapSize" | "has" | "hasSets" | "values" | "valuesSet"
> &
{
forEach(
__callback__: (
mapKey: __MK0__,
setKey: __SK0__,
__collection__: ReadonlyStrongMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
) : void;
forEachMap(
__callback__: (
mapKey: __MK0__,
__collection__: ReadonlyStrongMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
) : void;
forEachSet(
mapKey: __MK0__,
__callback__: (
mapKey: __MK0__,
setKey: __SK0__,
__collection__: ReadonlyStrongMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
): void;
}
export default StrongMapOfStrongSets;