-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeakMapOfStrongSets.mts
328 lines (290 loc) · 8.23 KB
/
WeakMapOfStrongSets.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
/*
* 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: Weak/OneMapOfOneStrongSet
* @license MPL-2.0
* @author Alexander J. Vincent <[email protected]>
* @copyright © 2021-2022 Alexander J. Vincent
*/
import { DefaultWeakMap } from "./keys/DefaultMap.mjs";
class WeakMapOfStrongSets<
__MK0__ extends object,
__SK0__
>
{
/** @typedef {Set<*>} __WeakMapOfStrongSets_InnerMap__ */
/**
* @type {WeakMap<object, __WeakMapOfStrongSets_InnerMap__>}
* @constant
* This is two levels. The first level is the map key.
* The second level is the strong set.
*/
#root: DefaultWeakMap<__MK0__, Set<__SK0__>> = new DefaultWeakMap();
constructor(iterable? : [__MK0__, __SK0__][])
{
if (iterable) {
for (const [mapKey, setKey] of iterable) {
this.add(mapKey, setKey);
}
}
}
/**
* Add a key set to this collection.
*
* @param {object} mapKey The map key.
* @param {*} setKey The set key.
* @returns {WeakMapOfStrongSets} This collection.
* @public
*/
add(mapKey: __MK0__, setKey: __SK0__) : this
{
this.#requireValidKey(mapKey, setKey);
const __innerSet__ = this.#root.getDefault(mapKey, () => new Set);
__innerSet__.add(setKey);
return this;
}
/**
* Add several sets to a map in this collection.
*
* @param {object} mapKey The map key.
* @param {Set[]} __sets__ The sets to add.
* @returns {WeakMapOfStrongSets} This collection.
* @public
*/
addSets(mapKey: __MK0__, __sets__: [__SK0__][]) : this
{
this.#requireValidMapKey(mapKey);
if (__sets__.length === 0)
return this;
__sets__.forEach(([setKey]) => {
this.#requireValidKey(mapKey, setKey);
});
const __innerSet__ = this.#root.getDefault(mapKey, () => new Set);
__sets__.forEach(([setKey]) => __innerSet__.add(setKey));
return this;
}
/**
* Delete an element from the collection by the given key sequence.
*
* @param {object} 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
{
this.#requireValidKey(mapKey, setKey);
const __innerSet__ = this.#root.get(mapKey);
if (!__innerSet__)
return false;
// level 2: inner map to set
const __returnValue__ = __innerSet__.delete(setKey);
if (__innerSet__.size === 0) {
this.deleteSets(mapKey);
}
return __returnValue__;
}
/**
* Delete all sets from the collection by the given map sequence.
*
* @param {object} mapKey The map key.
* @returns {boolean} True if we found the value and deleted it.
* @public
*/
deleteSets(mapKey: __MK0__) : boolean
{
this.#requireValidMapKey(mapKey);
return this.#root.delete(mapKey);
}
/**
* Iterate over the keys under a map in this collection.
*
* @param {object} mapKey The map key.
* @param {__WeakMapOfStrongSets_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__: WeakMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
) : void
{
this.#requireValidMapKey(mapKey);
const __innerSet__ = this.#root.get(mapKey);
if (!__innerSet__)
return;
__innerSet__.forEach(
setKey => __callback__.apply(__thisArg__, [mapKey, setKey, this])
);
}
/**
* An user-provided callback to .forEach().
*
* @callback __WeakMapOfStrongSets_ForEachCallback__
* @param {object} mapKey The map key.
* @param {*} setKey The set key.
* @param {WeakMapOfStrongSets} __collection__ This collection.
*/
/**
* Get the size of a particular set.
*
* @param {object} mapKey The map key.
* @returns {number} The set size.
* @public
*/
getSizeOfSet(mapKey: __MK0__) : number
{
this.#requireValidMapKey(mapKey);
const __innerSet__ = this.#root.get(mapKey);
return __innerSet__?.size || 0;
}
/**
* Report if the collection has a value for a key set.
*
* @param {object} 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
{
this.#requireValidKey(mapKey, setKey);
const __innerSet__ = this.#root.get(mapKey);
if (!__innerSet__)
return false;
return __innerSet__.has(setKey);
}
/**
* Report if the collection has any sets for a map.
*
* @param {object} mapKey The map key.
* @returns {boolean} True if the key set refers to a value in the collection.
* @public
*/
hasSets(mapKey: __MK0__) : boolean
{
this.#requireValidMapKey(mapKey);
return this.#root.has(mapKey);
}
/**
* Determine if a set of keys is valid.
*
* @param {object} mapKey The map key.
* @param {*} setKey The set key.
* @returns {boolean} True if the validation passes, false if it doesn't.
* @public
*/
isValidKey(mapKey: __MK0__, setKey: __SK0__) : boolean
{
return this.#isValidKey(mapKey, setKey);
}
/**
* Yield the sets of the collection in a map.
*
* @param {object} mapKey The map key.
* @yields {*} The sets.
* @public
*/
* valuesSet(mapKey: __MK0__) : IterableIterator<[__MK0__, __SK0__]>
{
this.#requireValidMapKey(mapKey);
const __innerSet__ = this.#root.get(mapKey);
if (!__innerSet__)
return;
const __outerIter__ = __innerSet__.values();
for (const setKey of __outerIter__)
yield [mapKey, setKey];
}
/**
* Throw if the key set is not valid.
*
* @param {object} mapKey The map key.
* @param {*} setKey The set key.
* @throws for an invalid key set.
*/
#requireValidKey(mapKey: __MK0__, setKey: __SK0__) : void
{
if (!this.#isValidKey(mapKey, setKey))
throw new Error("The ordered key set is not valid!");
}
/**
* Determine if a set of keys is valid.
*
* @param {object} mapKey The map key.
* @param {*} setKey The set key.
* @returns {boolean} True if the validation passes, false if it doesn't.
*/
#isValidKey(mapKey: __MK0__, setKey: __SK0__) : boolean
{
return this.#isValidMapKey(mapKey) && this.#isValidSetKey(setKey);
}
/**
* Throw if the map key set is not valid.
*
* @param {object} mapKey The map key.
* @throws for an invalid key set.
*/
#requireValidMapKey(mapKey: __MK0__) : void
{
if (!this.#isValidMapKey(mapKey))
throw new Error("The ordered map key set is not valid!");
}
/**
* Determine if a set of map keys is valid.
*
* @param {object} mapKey The map key.
* @returns {boolean} True if the validation passes, false if it doesn't.
*/
#isValidMapKey(mapKey: __MK0__) : boolean
{
if (Object(mapKey) !== mapKey)
return false;
return true;
}
/**
* Determine if a set of set keys is valid.
*
* @param {*} setKey The set key.
* @returns {boolean} True if the validation passes, false if it doesn't.
*/
#isValidSetKey(setKey: __SK0__) : boolean
{
void(setKey);
return true;
}
[Symbol.toStringTag] = "WeakMapOfStrongSets";
}
Object.freeze(WeakMapOfStrongSets);
Object.freeze(WeakMapOfStrongSets.prototype);
export type ReadonlyWeakMapOfStrongSets<
__MK0__ extends object,
__SK0__
> =
Pick<
WeakMapOfStrongSets<__MK0__, __SK0__>,
"getSizeOfSet" | "has" | "hasSets" | "isValidKey" | "valuesSet"
> &
{
forEachSet(
mapKey: __MK0__,
__callback__: (
mapKey: __MK0__,
setKey: __SK0__,
__collection__: ReadonlyWeakMapOfStrongSets<__MK0__, __SK0__>
) => void,
__thisArg__?: unknown
): void;
}
export default WeakMapOfStrongSets;