-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrongMapOfStrongSets.mjs
253 lines (253 loc) · 7.88 KB
/
StrongMapOfStrongSets.mjs
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
/*
* 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 {
/** @typedef {Set<*>} __StrongMapOfStrongSets__InnerMap__ */
/** @type {Map<*, __StrongMapOfStrongSets__InnerMap__>} @constant */
#outerMap = new DefaultMap();
/** @type {number} */
#sizeOfAll = 0;
constructor(iterable) {
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() {
return this.#sizeOfAll;
}
/**
* Get the size of a particular set.
*
* @param {*} mapKey The map key.
* @returns {number} The set size.
* @public
*/
getSizeOfSet(mapKey) {
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() {
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, setKey) {
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, __sets__) {
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() {
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, setKey) {
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) {
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__, __thisArg__) {
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__, __thisArg__) {
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, __callback__, __thisArg__) {
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, setKey) {
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) {
const __innerSet__ = this.#outerMap.get(mapKey);
return Boolean(__innerSet__);
}
/**
* Yield the values of the collection.
*
* @yields {*} The value.
* @public
*/
*values() {
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) {
const __innerSet__ = this.#outerMap.get(mapKey);
if (!__innerSet__)
return;
for (const setKey of __innerSet__.values())
yield [mapKey, setKey];
}
[Symbol.iterator]() {
return this.values();
}
[Symbol.toStringTag] = "StrongMapOfStrongSets";
}
Object.freeze(StrongMapOfStrongSets);
Object.freeze(StrongMapOfStrongSets.prototype);
export default StrongMapOfStrongSets;
//# sourceMappingURL=StrongMapOfStrongSets.mjs.map