Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/structs/set.js
4501 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Datastructure: Set.
9
*
10
*
11
* This class implements a set data structure. Adding and removing is O(1). It
12
* supports both object and primitive values. Be careful because you can add
13
* both 1 and new Number(1), because these are not the same. You can even add
14
* multiple new Number(1) because these are not equal.
15
*/
16
17
18
goog.provide('goog.structs.Set');
19
20
goog.require('goog.structs');
21
goog.require('goog.structs.Collection');
22
goog.require('goog.structs.Map');
23
goog.requireType('goog.iter.Iterator');
24
goog.require('goog.utils');
25
26
/**
27
* A set that can contain both primitives and objects. Adding and removing
28
* elements is O(1). Primitives are treated as identical if they have the same
29
* type and convert to the same string. Objects are treated as identical only
30
* if they are references to the same object. WARNING: A goog.structs.Set can
31
* contain both 1 and (new Number(1)), because they are not the same. WARNING:
32
* Adding (new Number(1)) twice will yield two distinct elements, because they
33
* are two different objects. WARNING: Any object that is added to a
34
* goog.structs.Set will be modified! Because goog.getUid() is used to
35
* identify objects, every object in the set will be mutated.
36
* @param {Array<T>|Object<?,T>=} opt_values Initial values to start with.
37
* @constructor
38
* @implements {goog.structs.Collection<T>}
39
* @implements {Iterable<T>}
40
* @final
41
* @template T
42
* @deprecated This type is misleading: use ES6 Set instead.
43
*/
44
goog.structs.Set = function(opt_values) {
45
'use strict';
46
this.map_ = new goog.structs.Map();
47
48
49
/**
50
* The number of items in this set.
51
* @const {number}
52
*/
53
this.size = 0;
54
55
if (opt_values) {
56
this.addAll(opt_values);
57
}
58
};
59
60
/**
61
* A function that returns a unique id.
62
* @private @const {function(?Object): number}
63
*/
64
goog.structs.Set.getUid_ = goog.utils.getUid;
65
66
67
/**
68
* Obtains a unique key for an element of the set. Primitives will yield the
69
* same key if they have the same type and convert to the same string. Object
70
* references will yield the same key only if they refer to the same object.
71
* @param {*} val Object or primitive value to get a key for.
72
* @return {string} A unique key for this value/object.
73
* @private
74
*/
75
goog.structs.Set.getKey_ = function(val) {
76
'use strict';
77
var type = typeof val;
78
if (type == 'object' && val || type == 'function') {
79
return 'o' + goog.structs.Set.getUid_(/** @type {Object} */ (val));
80
} else {
81
return type.slice(0, 1) + val;
82
}
83
};
84
85
86
/**
87
* @return {number} The number of elements in the set.
88
* @override
89
* @deprecated Use the `size` property instead, for alignment with ES6 Set.
90
*/
91
goog.structs.Set.prototype.getCount = function() {
92
'use strict';
93
return this.map_.size;
94
};
95
96
97
/**
98
* Add a primitive or an object to the set.
99
* @param {T} element The primitive or object to add.
100
* @override
101
*/
102
goog.structs.Set.prototype.add = function(element) {
103
'use strict';
104
this.map_.set(goog.structs.Set.getKey_(element), element);
105
this.setSizeInternal_(this.map_.size);
106
};
107
108
109
/**
110
* Adds all the values in the given collection to this set.
111
* @param {Array<T>|goog.structs.Collection<T>|Object<?,T>} col A collection
112
* containing the elements to add.
113
* @deprecated Use `goog.collections.sets.addAll(thisSet, col)` instead,
114
* converting Objects to their values using `Object.values`, for alignment
115
* with ES6 Set.
116
*/
117
goog.structs.Set.prototype.addAll = function(col) {
118
'use strict';
119
var values = goog.structs.getValues(col);
120
var l = values.length;
121
for (var i = 0; i < l; i++) {
122
this.add(values[i]);
123
}
124
this.setSizeInternal_(this.map_.size);
125
};
126
127
128
/**
129
* Removes all values in the given collection from this set.
130
* @param {Array<T>|goog.structs.Collection<T>|Object<?,T>} col A collection
131
* containing the elements to remove.
132
* @deprecated Use `goog.collections.sets.removeAll(thisSet, col)` instead,
133
* converting Objects to their values using `Object.values`, for alignment
134
* with ES6 Set.
135
*/
136
goog.structs.Set.prototype.removeAll = function(col) {
137
'use strict';
138
var values = goog.structs.getValues(col);
139
var l = values.length;
140
for (var i = 0; i < l; i++) {
141
this.remove(values[i]);
142
}
143
this.setSizeInternal_(this.map_.size);
144
};
145
146
147
/**
148
* Removes the given element from this set.
149
* @param {T} element The primitive or object to remove.
150
* @return {boolean} Whether the element was found and removed.
151
*/
152
goog.structs.Set.prototype.delete = function(element) {
153
'use strict';
154
const rv = this.map_.remove(goog.structs.Set.getKey_(element));
155
this.setSizeInternal_(this.map_.size);
156
return rv;
157
};
158
159
/**
160
* Removes the given element from this set.
161
* @param {T} element The primitive or object to remove.
162
* @return {boolean} Whether the element was found and removed.
163
* @override
164
* @deprecated Use `delete`, for alignment with ES6 Set.
165
*/
166
goog.structs.Set.prototype.remove = function(element) {
167
'use strict';
168
return this.delete(element);
169
};
170
171
172
/**
173
* Removes all elements from this set.
174
*/
175
goog.structs.Set.prototype.clear = function() {
176
'use strict';
177
this.map_.clear();
178
this.setSizeInternal_(0);
179
};
180
181
182
/**
183
* Tests whether this set is empty.
184
* @return {boolean} True if there are no elements in this set.
185
* @deprecated Use the size property and compare against 0, for alignment with
186
* ES6 Set.
187
*/
188
goog.structs.Set.prototype.isEmpty = function() {
189
'use strict';
190
return this.map_.size === 0;
191
};
192
193
194
/**
195
* Tests whether this set contains the given element.
196
* @param {T} element The primitive or object to test for.
197
* @return {boolean} True if this set contains the given element.
198
*/
199
goog.structs.Set.prototype.has = function(element) {
200
'use strict';
201
return this.map_.containsKey(goog.structs.Set.getKey_(element));
202
};
203
204
/**
205
* Tests whether this set contains the given element.
206
* @param {T} element The primitive or object to test for.
207
* @return {boolean} True if this set contains the given element.
208
* @override
209
* @deprecated Use `has` instead, for alignment with ES6 Set.
210
*/
211
goog.structs.Set.prototype.contains = function(element) {
212
'use strict';
213
return this.map_.containsKey(goog.structs.Set.getKey_(element));
214
};
215
216
217
/**
218
* Tests whether this set contains all the values in a given collection.
219
* Repeated elements in the collection are ignored, e.g. (new
220
* goog.structs.Set([1, 2])).containsAll([1, 1]) is True.
221
* @param {goog.structs.Collection<T>|Object} col A collection-like object.
222
* @return {boolean} True if the set contains all elements.
223
* @deprecated Use `goog.collections.sets.hasAll(thisSet, col)`, converting
224
* Objects to arrays using Object.values, for alignment with ES6 Set.
225
*/
226
goog.structs.Set.prototype.containsAll = function(col) {
227
'use strict';
228
return goog.structs.every(col, this.contains, this);
229
};
230
231
232
/**
233
* Finds all values that are present in both this set and the given collection.
234
* @param {Array<S>|Object<?,S>} col A collection.
235
* @return {!goog.structs.Set<T|S>} A new set containing all the values
236
* (primitives or objects) present in both this set and the given
237
* collection.
238
* @template S
239
* @deprecated Use `goog.collections.sets.intersection(thisSet, col)`,
240
* converting Objects to arrays using Object.values, instead for alignment
241
* with ES6 Set.
242
*/
243
goog.structs.Set.prototype.intersection = function(col) {
244
'use strict';
245
var result = new goog.structs.Set();
246
247
var values = goog.structs.getValues(col);
248
for (var i = 0; i < values.length; i++) {
249
var value = values[i];
250
if (this.contains(value)) {
251
result.add(value);
252
}
253
}
254
255
return result;
256
};
257
258
259
/**
260
* Finds all values that are present in this set and not in the given
261
* collection.
262
* @param {Array<T>|goog.structs.Collection<T>|Object<?,T>} col A collection.
263
* @return {!goog.structs.Set} A new set containing all the values
264
* (primitives or objects) present in this set but not in the given
265
* collection.
266
*/
267
goog.structs.Set.prototype.difference = function(col) {
268
'use strict';
269
var result = this.clone();
270
result.removeAll(col);
271
return result;
272
};
273
274
275
/**
276
* Returns an array containing all the elements in this set.
277
* @return {!Array<T>} An array containing all the elements in this set.
278
* @deprecated Use `Array.from(set.values())` instead, for alignment with ES6
279
* Set.
280
*/
281
goog.structs.Set.prototype.getValues = function() {
282
'use strict';
283
return this.map_.getValues();
284
};
285
286
/**
287
* @returns {!IteratorIterable<T>} An ES6 Iterator that iterates over the values
288
* in the set.
289
*/
290
goog.structs.Set.prototype.values = function() {
291
'use strict';
292
return this.map_.values();
293
};
294
295
/**
296
* Creates a shallow clone of this set.
297
* @return {!goog.structs.Set<T>} A new set containing all the same elements as
298
* this set.
299
* @deprecated Use `new Set(thisSet.values())` for alignment with ES6 Set.
300
*/
301
goog.structs.Set.prototype.clone = function() {
302
'use strict';
303
return new goog.structs.Set(this);
304
};
305
306
307
/**
308
* Tests whether the given collection consists of the same elements as this set,
309
* regardless of order, without repetition. Primitives are treated as equal if
310
* they have the same type and convert to the same string; objects are treated
311
* as equal if they are references to the same object. This operation is O(n).
312
* @param {goog.structs.Collection<T>|Object} col A collection.
313
* @return {boolean} True if the given collection consists of the same elements
314
* as this set, regardless of order, without repetition.
315
* @deprecated Use `goog.collections.equals(thisSet, col)`, converting Objects
316
* to arrays using Object.values, instead for alignment with ES6 Set.
317
*/
318
goog.structs.Set.prototype.equals = function(col) {
319
'use strict';
320
return this.getCount() == goog.structs.getCount(col) && this.isSubsetOf(col);
321
};
322
323
324
/**
325
* Tests whether the given collection contains all the elements in this set.
326
* Primitives are treated as equal if they have the same type and convert to the
327
* same string; objects are treated as equal if they are references to the same
328
* object. This operation is O(n).
329
* @param {goog.structs.Collection<T>|Object} col A collection.
330
* @return {boolean} True if this set is a subset of the given collection.
331
* @deprecated Use `goog.collections.isSubsetOf(thisSet, col)`, converting
332
* Objects to arrays using Object.values, instead for alignment with ES6
333
* Set.
334
*/
335
goog.structs.Set.prototype.isSubsetOf = function(col) {
336
'use strict';
337
var colCount = goog.structs.getCount(col);
338
if (this.getCount() > colCount) {
339
return false;
340
}
341
if (!(col instanceof goog.structs.Set) && colCount > 5) {
342
// Convert to a goog.structs.Set so that goog.structs.contains runs in
343
// O(1) time instead of O(n) time.
344
col = new goog.structs.Set(col);
345
}
346
return goog.structs.every(this, function(value) {
347
'use strict';
348
return goog.structs.contains(col, value);
349
});
350
};
351
352
353
/**
354
* Returns an iterator that iterates over the elements in this set.
355
* @param {boolean=} opt_keys This argument is ignored.
356
* @return {!goog.iter.Iterator} An iterator over the elements in this set.
357
* @deprecated Call `values` and use native iteration, for alignment with ES6
358
* Set.
359
*/
360
goog.structs.Set.prototype.__iterator__ = function(opt_keys) {
361
'use strict';
362
return this.map_.__iterator__(false);
363
};
364
365
/**
366
* @return {!IteratorIterable<T>} An ES6 Iterator that iterates over the values
367
* in the set.
368
*/
369
goog.structs.Set.prototype[Symbol.iterator] = function() {
370
return this.values();
371
};
372
373
/**
374
* Assigns to the size property to isolate supressions of const assignment
375
* to only where they are needed.
376
* @param {number} newSize The size to update to.
377
* @private
378
*/
379
goog.structs.Set.prototype.setSizeInternal_ = function(newSize) {
380
/** @suppress {const} */
381
this.size = newSize;
382
};
383
384