-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
set.dart
636 lines (528 loc) · 16.4 KB
/
set.dart
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.6
/// Base implementations of [Set].
part of dart.collection;
/// Mixin implementation of [Set].
///
/// This class provides a base implementation of a `Set` that depends only
/// on the abstract members: [add], [contains], [lookup], [remove],
/// [iterator], [length] and [toSet].
///
/// Some of the methods assume that `toSet` creates a modifiable set.
/// If using this mixin for an unmodifiable set,
/// where `toSet` should return an unmodifiable set,
/// it's necessary to reimplement
/// [retainAll], [union], [intersection] and [difference].
///
/// Implementations of `Set` using this mixin should consider also implementing
/// `clear` in constant time. The default implementation works by removing every
/// element.
abstract class SetMixin<E> implements Set<E> {
// This class reimplements all of [IterableMixin].
// If/when Dart mixins get more powerful, we should just create a single
// Mixin class from IterableMixin and the new methods of this class.
bool add(E value);
bool contains(Object element);
E lookup(Object element);
bool remove(Object value);
Iterator<E> get iterator;
Set<E> toSet();
int get length;
bool get isEmpty => length == 0;
bool get isNotEmpty => length != 0;
Set<R> cast<R>() => Set.castFrom<E, R>(this);
Iterable<E> followedBy(Iterable<E> other) =>
FollowedByIterable<E>.firstEfficient(this, other);
Iterable<T> whereType<T>() => WhereTypeIterable<T>(this);
void clear() {
removeAll(toList());
}
void addAll(Iterable<E> elements) {
for (E element in elements) add(element);
}
void removeAll(Iterable<Object> elements) {
for (Object element in elements) remove(element);
}
void retainAll(Iterable<Object> elements) {
// Create a copy of the set, remove all of elements from the copy,
// then remove all remaining elements in copy from this.
Set<E> toRemove = toSet();
for (Object o in elements) {
toRemove.remove(o);
}
removeAll(toRemove);
}
void removeWhere(bool test(E element)) {
List toRemove = [];
for (E element in this) {
if (test(element)) toRemove.add(element);
}
removeAll(toRemove);
}
void retainWhere(bool test(E element)) {
List toRemove = [];
for (E element in this) {
if (!test(element)) toRemove.add(element);
}
removeAll(toRemove);
}
bool containsAll(Iterable<Object> other) {
for (Object o in other) {
if (!contains(o)) return false;
}
return true;
}
Set<E> union(Set<E> other) {
return toSet()..addAll(other);
}
Set<E> intersection(Set<Object> other) {
Set<E> result = toSet();
for (E element in this) {
if (!other.contains(element)) result.remove(element);
}
return result;
}
Set<E> difference(Set<Object> other) {
Set<E> result = toSet();
for (E element in this) {
if (other.contains(element)) result.remove(element);
}
return result;
}
List<E> toList({bool growable = true}) {
List<E> result = growable ? (<E>[]..length = length) : List<E>(length);
int i = 0;
for (E element in this) result[i++] = element;
return result;
}
Iterable<T> map<T>(T f(E element)) =>
EfficientLengthMappedIterable<E, T>(this, f);
E get single {
if (length > 1) throw IterableElementError.tooMany();
Iterator<E> it = iterator;
if (!it.moveNext()) throw IterableElementError.noElement();
E result = it.current;
return result;
}
String toString() => IterableBase.iterableToFullString(this, '{', '}');
// Copied from IterableMixin.
// Should be inherited if we had multi-level mixins.
Iterable<E> where(bool f(E element)) => WhereIterable<E>(this, f);
Iterable<T> expand<T>(Iterable<T> f(E element)) =>
ExpandIterable<E, T>(this, f);
void forEach(void f(E element)) {
for (E element in this) f(element);
}
E reduce(E combine(E value, E element)) {
Iterator<E> iterator = this.iterator;
if (!iterator.moveNext()) {
throw IterableElementError.noElement();
}
E value = iterator.current;
while (iterator.moveNext()) {
value = combine(value, iterator.current);
}
return value;
}
T fold<T>(T initialValue, T combine(T previousValue, E element)) {
var value = initialValue;
for (E element in this) value = combine(value, element);
return value;
}
bool every(bool f(E element)) {
for (E element in this) {
if (!f(element)) return false;
}
return true;
}
String join([String separator = ""]) {
Iterator<E> iterator = this.iterator;
if (!iterator.moveNext()) return "";
StringBuffer buffer = StringBuffer();
if (separator == null || separator == "") {
do {
buffer.write(iterator.current);
} while (iterator.moveNext());
} else {
buffer.write(iterator.current);
while (iterator.moveNext()) {
buffer.write(separator);
buffer.write(iterator.current);
}
}
return buffer.toString();
}
bool any(bool test(E element)) {
for (E element in this) {
if (test(element)) return true;
}
return false;
}
Iterable<E> take(int n) {
return TakeIterable<E>(this, n);
}
Iterable<E> takeWhile(bool test(E value)) {
return TakeWhileIterable<E>(this, test);
}
Iterable<E> skip(int n) {
return SkipIterable<E>(this, n);
}
Iterable<E> skipWhile(bool test(E value)) {
return SkipWhileIterable<E>(this, test);
}
E get first {
Iterator<E> it = iterator;
if (!it.moveNext()) {
throw IterableElementError.noElement();
}
return it.current;
}
E get last {
Iterator<E> it = iterator;
if (!it.moveNext()) {
throw IterableElementError.noElement();
}
E result;
do {
result = it.current;
} while (it.moveNext());
return result;
}
E firstWhere(bool test(E value), {E orElse()}) {
for (E element in this) {
if (test(element)) return element;
}
if (orElse != null) return orElse();
throw IterableElementError.noElement();
}
E lastWhere(bool test(E value), {E orElse()}) {
E result;
bool foundMatching = false;
for (E element in this) {
if (test(element)) {
result = element;
foundMatching = true;
}
}
if (foundMatching) return result;
if (orElse != null) return orElse();
throw IterableElementError.noElement();
}
E singleWhere(bool test(E value), {E orElse()}) {
E result;
bool foundMatching = false;
for (E element in this) {
if (test(element)) {
if (foundMatching) {
throw IterableElementError.tooMany();
}
result = element;
foundMatching = true;
}
}
if (foundMatching) return result;
if (orElse != null) return orElse();
throw IterableElementError.noElement();
}
E elementAt(int index) {
ArgumentError.checkNotNull(index, "index");
RangeError.checkNotNegative(index, "index");
int elementIndex = 0;
for (E element in this) {
if (index == elementIndex) return element;
elementIndex++;
}
throw RangeError.index(index, this, "index", null, elementIndex);
}
}
/// Base implementation of [Set].
///
/// This class provides a base implementation of a `Set` that depends only
/// on the abstract members: [add], [contains], [lookup], [remove],
/// [iterator], [length] and [toSet].
///
/// Some of the methods assume that `toSet` creates a modifiable set.
/// If using this base class for an unmodifiable set,
/// where `toSet` should return an unmodifiable set,
/// it's necessary to reimplement
/// [retainAll], [union], [intersection] and [difference].
///
/// Implementations of `Set` using this base should consider also implementing
/// `clear` in constant time. The default implementation works by removing every
/// element.
abstract class SetBase<E> extends Object with SetMixin<E> {
/// Converts a [Set] to a [String].
///
/// Converts [set] to a string by converting each element to a string (by
/// calling [Object.toString]), joining them with ", ", and wrapping the
/// result in "{" and "}".
///
/// Handles circular references where converting one of the elements
/// to a string ends up converting [set] to a string again.
static String setToString(Set set) =>
IterableBase.iterableToFullString(set, '{', '}');
}
/// Common internal implementation of some [Set] methods.
// TODO(35548): Make this mix-in SetMixin, by adding `with SetMixin<E>`
// and removing the copied members below,
// when analyzer supports const constructors for mixin applications.
abstract class _SetBase<E> implements Set<E> {
// The following two methods override the ones in SetBase.
// It's possible to be more efficient if we have a way to create an empty
// set of the correct type.
const _SetBase();
Set<E> _newSet();
Set<R> _newSimilarSet<R>();
Set<R> cast<R>() => Set.castFrom<E, R>(this, newSet: _newSimilarSet);
Set<E> difference(Set<Object> other) {
Set<E> result = _newSet();
for (var element in this) {
if (!other.contains(element)) result.add(element);
}
return result;
}
Set<E> intersection(Set<Object> other) {
Set<E> result = _newSet();
for (var element in this) {
if (other.contains(element)) result.add(element);
}
return result;
}
// Subclasses can optimize this further.
Set<E> toSet() => _newSet()..addAll(this);
/// TODO(35548): Remove the following declarations again when the analyzer
/// supports mixins with const constructors, and mix in `SetMixin` instead.
bool get isEmpty => length == 0;
bool get isNotEmpty => length != 0;
Iterable<E> followedBy(Iterable<E> other) =>
FollowedByIterable<E>.firstEfficient(this, other);
Iterable<T> whereType<T>() => WhereTypeIterable<T>(this);
void clear() {
removeAll(toList());
}
void addAll(Iterable<E> elements) {
for (E element in elements) add(element);
}
void removeAll(Iterable<Object> elements) {
for (Object element in elements) remove(element);
}
void retainAll(Iterable<Object> elements) {
// Create a copy of the set, remove all of elements from the copy,
// then remove all remaining elements in copy from this.
Set<E> toRemove = toSet();
for (Object o in elements) {
toRemove.remove(o);
}
removeAll(toRemove);
}
void removeWhere(bool test(E element)) {
List toRemove = [];
for (E element in this) {
if (test(element)) toRemove.add(element);
}
removeAll(toRemove);
}
void retainWhere(bool test(E element)) {
List toRemove = [];
for (E element in this) {
if (!test(element)) toRemove.add(element);
}
removeAll(toRemove);
}
bool containsAll(Iterable<Object> other) {
for (Object o in other) {
if (!contains(o)) return false;
}
return true;
}
Set<E> union(Set<E> other) {
return toSet()..addAll(other);
}
List<E> toList({bool growable = true}) {
List<E> result = growable ? (<E>[]..length = length) : List<E>(length);
int i = 0;
for (E element in this) result[i++] = element;
return result;
}
Iterable<T> map<T>(T f(E element)) =>
EfficientLengthMappedIterable<E, T>(this, f);
E get single {
if (length > 1) throw IterableElementError.tooMany();
Iterator<E> it = iterator;
if (!it.moveNext()) throw IterableElementError.noElement();
E result = it.current;
return result;
}
String toString() => IterableBase.iterableToFullString(this, '{', '}');
Iterable<E> where(bool f(E element)) => WhereIterable<E>(this, f);
Iterable<T> expand<T>(Iterable<T> f(E element)) =>
ExpandIterable<E, T>(this, f);
void forEach(void f(E element)) {
for (E element in this) f(element);
}
E reduce(E combine(E value, E element)) {
Iterator<E> iterator = this.iterator;
if (!iterator.moveNext()) {
throw IterableElementError.noElement();
}
E value = iterator.current;
while (iterator.moveNext()) {
value = combine(value, iterator.current);
}
return value;
}
T fold<T>(T initialValue, T combine(T previousValue, E element)) {
var value = initialValue;
for (E element in this) value = combine(value, element);
return value;
}
bool every(bool f(E element)) {
for (E element in this) {
if (!f(element)) return false;
}
return true;
}
String join([String separator = ""]) {
Iterator<E> iterator = this.iterator;
if (!iterator.moveNext()) return "";
StringBuffer buffer = StringBuffer();
if (separator == null || separator == "") {
do {
buffer.write(iterator.current);
} while (iterator.moveNext());
} else {
buffer.write(iterator.current);
while (iterator.moveNext()) {
buffer.write(separator);
buffer.write(iterator.current);
}
}
return buffer.toString();
}
bool any(bool test(E element)) {
for (E element in this) {
if (test(element)) return true;
}
return false;
}
Iterable<E> take(int n) {
return TakeIterable<E>(this, n);
}
Iterable<E> takeWhile(bool test(E value)) {
return TakeWhileIterable<E>(this, test);
}
Iterable<E> skip(int n) {
return SkipIterable<E>(this, n);
}
Iterable<E> skipWhile(bool test(E value)) {
return SkipWhileIterable<E>(this, test);
}
E get first {
Iterator<E> it = iterator;
if (!it.moveNext()) {
throw IterableElementError.noElement();
}
return it.current;
}
E get last {
Iterator<E> it = iterator;
if (!it.moveNext()) {
throw IterableElementError.noElement();
}
E result;
do {
result = it.current;
} while (it.moveNext());
return result;
}
E firstWhere(bool test(E value), {E orElse()}) {
for (E element in this) {
if (test(element)) return element;
}
if (orElse != null) return orElse();
throw IterableElementError.noElement();
}
E lastWhere(bool test(E value), {E orElse()}) {
E result;
bool foundMatching = false;
for (E element in this) {
if (test(element)) {
result = element;
foundMatching = true;
}
}
if (foundMatching) return result;
if (orElse != null) return orElse();
throw IterableElementError.noElement();
}
E singleWhere(bool test(E value), {E orElse()}) {
E result;
bool foundMatching = false;
for (E element in this) {
if (test(element)) {
if (foundMatching) {
throw IterableElementError.tooMany();
}
result = element;
foundMatching = true;
}
}
if (foundMatching) return result;
if (orElse != null) return orElse();
throw IterableElementError.noElement();
}
E elementAt(int index) {
ArgumentError.checkNotNull(index, "index");
RangeError.checkNotNegative(index, "index");
int elementIndex = 0;
for (E element in this) {
if (index == elementIndex) return element;
elementIndex++;
}
throw RangeError.index(index, this, "index", null, elementIndex);
}
}
/// Class used to implement const sets.
class _UnmodifiableSet<E> extends _SetBase<E> {
final Map<E, Null> _map;
const _UnmodifiableSet(this._map);
Set<E> _newSet() => LinkedHashSet<E>();
Set<R> _newSimilarSet<R>() => LinkedHashSet<R>();
// Lookups use map methods.
bool contains(Object element) => _map.containsKey(element);
Iterator<E> get iterator => _map.keys.iterator;
int get length => _map.length;
E lookup(Object element) {
for (var key in _map.keys) {
if (key == element) return key;
}
return null;
}
// Mutating methods throw.
bool add(E value) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void clear() {
throw UnsupportedError("Cannot change unmodifiable set");
}
void addAll(Iterable<E> elements) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void removeAll(Iterable<Object> elements) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void retainAll(Iterable<Object> elements) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void removeWhere(bool test(E element)) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void retainWhere(bool test(E element)) {
throw UnsupportedError("Cannot change unmodifiable set");
}
bool remove(Object value) {
throw UnsupportedError("Cannot change unmodifiable set");
}
}