-
Notifications
You must be signed in to change notification settings - Fork 85
/
wrappers.dart
751 lines (572 loc) · 23.1 KB
/
wrappers.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
// Copyright (c) 2013, 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.
import "dart:collection";
import "dart:math" as math;
import "typed_wrappers.dart";
import "unmodifiable_wrappers.dart";
typedef K _KeyForValue<K, V>(V value);
/// A base class for delegating iterables.
///
/// Subclasses can provide a [_base] that should be delegated to. Unlike
/// [DelegatingIterable], this allows the base to be created on demand.
abstract class _DelegatingIterableBase<E> implements Iterable<E> {
Iterable<E> get _base;
const _DelegatingIterableBase();
bool any(bool test(E element)) => _base.any(test);
// TODO: Dart 2.0 requires this method to be implemented.
Iterable<T> cast<T>() {
throw new UnimplementedError('cast');
}
bool contains(Object element) => _base.contains(element);
E elementAt(int index) => _base.elementAt(index);
bool every(bool test(E element)) => _base.every(test);
Iterable<T> expand<T>(Iterable<T> f(E element)) => _base.expand(f);
E get first => _base.first;
E firstWhere(bool test(E element), {E orElse()}) =>
_base.firstWhere(test, orElse: orElse);
T fold<T>(T initialValue, T combine(T previousValue, E element)) =>
_base.fold(initialValue, combine);
// TODO: Dart 2.0 requires this method to be implemented.
Iterable<E> followedBy(Iterable<E> other) {
throw new UnimplementedError('followedBy');
}
void forEach(void f(E element)) => _base.forEach(f);
bool get isEmpty => _base.isEmpty;
bool get isNotEmpty => _base.isNotEmpty;
Iterator<E> get iterator => _base.iterator;
String join([String separator = ""]) => _base.join(separator);
E get last => _base.last;
E lastWhere(bool test(E element), {E orElse()}) =>
_base.lastWhere(test, orElse: orElse);
int get length => _base.length;
Iterable<T> map<T>(T f(E element)) => _base.map(f);
E reduce(E combine(E value, E element)) => _base.reduce(combine);
// TODO: Dart 2.0 requires this method to be implemented.
Iterable<T> retype<T>() {
throw new UnimplementedError('retype');
}
E get single => _base.single;
E singleWhere(bool test(E element), {E orElse()}) {
if (orElse != null) throw new UnimplementedError('singleWhere:orElse');
return _base.singleWhere(test);
}
Iterable<E> skip(int n) => _base.skip(n);
Iterable<E> skipWhile(bool test(E value)) => _base.skipWhile(test);
Iterable<E> take(int n) => _base.take(n);
Iterable<E> takeWhile(bool test(E value)) => _base.takeWhile(test);
List<E> toList({bool growable: true}) => _base.toList(growable: growable);
Set<E> toSet() => _base.toSet();
Iterable<E> where(bool test(E element)) => _base.where(test);
// TODO: Dart 2.0 requires this method to be implemented.
Iterable<T> whereType<T>() {
throw new UnimplementedError("whereType");
}
String toString() => _base.toString();
}
/// An [Iterable] that delegates all operations to a base iterable.
///
/// This class can be used to hide non-`Iterable` methods of an iterable object,
/// or it can be extended to add extra functionality on top of an existing
/// iterable object.
class DelegatingIterable<E> extends _DelegatingIterableBase<E> {
final Iterable<E> _base;
/// Creates a wrapper that forwards operations to [base].
const DelegatingIterable(Iterable<E> base) : _base = base;
/// Creates a wrapper that asserts the types of values in [base].
///
/// This soundly converts an [Iterable] without a generic type to an
/// `Iterable<E>` by asserting that its elements are instances of `E` whenever
/// they're accessed. If they're not, it throws a [CastError].
///
/// This forwards all operations to [base], so any changes in [base] will be
/// reflected in [this]. If [base] is already an `Iterable<E>`, it's returned
/// unmodified.
static Iterable<E> typed<E>(Iterable base) =>
base is Iterable<E> ? base : new TypeSafeIterable<E>(base);
}
/// A [List] that delegates all operations to a base list.
///
/// This class can be used to hide non-`List` methods of a list object, or it
/// can be extended to add extra functionality on top of an existing list
/// object.
class DelegatingList<E> extends DelegatingIterable<E> implements List<E> {
const DelegatingList(List<E> base) : super(base);
/// Creates a wrapper that asserts the types of values in [base].
///
/// This soundly converts a [List] without a generic type to a `List<E>` by
/// asserting that its elements are instances of `E` whenever they're
/// accessed. If they're not, it throws a [CastError]. Note that even if an
/// operation throws a [CastError], it may still mutate the underlying
/// collection.
///
/// This forwards all operations to [base], so any changes in [base] will be
/// reflected in [this]. If [base] is already a `List<E>`, it's returned
/// unmodified.
static List<E> typed<E>(List base) =>
base is List<E> ? base : new TypeSafeList<E>(base);
List<E> get _listBase => _base;
E operator [](int index) => _listBase[index];
void operator []=(int index, E value) {
_listBase[index] = value;
}
// TODO: Dart 2.0 requires this method to be implemented.
List<E> operator +(List<E> other) {
throw new UnimplementedError('+');
}
void add(E value) {
_listBase.add(value);
}
void addAll(Iterable<E> iterable) {
_listBase.addAll(iterable);
}
Map<int, E> asMap() => _listBase.asMap();
// TODO: Dart 2.0 requires this method to be implemented.
List<T> cast<T>() {
throw new UnimplementedError('cast');
}
void clear() {
_listBase.clear();
}
void fillRange(int start, int end, [E fillValue]) {
_listBase.fillRange(start, end, fillValue);
}
// TODO: Dart 2.0 requires this method to be implemented.
set first(E value) {
if (this.isEmpty) throw new RangeError.index(0, this);
this[0] = value;
}
Iterable<E> getRange(int start, int end) => _listBase.getRange(start, end);
int indexOf(E element, [int start = 0]) => _listBase.indexOf(element, start);
// TODO: Dart 2.0 requires this method to be implemented.
int indexWhere(bool test(E element), [int start = 0]) {
throw new UnimplementedError('indexWhere');
}
void insert(int index, E element) {
_listBase.insert(index, element);
}
insertAll(int index, Iterable<E> iterable) {
_listBase.insertAll(index, iterable);
}
// TODO: Dart 2.0 requires this method to be implemented.
set last(E value) {
if (this.isEmpty) throw new RangeError.index(0, this);
this[this.length - 1] = value;
}
int lastIndexOf(E element, [int start]) =>
_listBase.lastIndexOf(element, start);
// TODO: Dart 2.0 requires this method to be implemented.
int lastIndexWhere(bool test(E element), [int start]) {
throw new UnimplementedError('lastIndexWhere');
}
set length(int newLength) {
_listBase.length = newLength;
}
bool remove(Object value) => _listBase.remove(value);
E removeAt(int index) => _listBase.removeAt(index);
E removeLast() => _listBase.removeLast();
void removeRange(int start, int end) {
_listBase.removeRange(start, end);
}
void removeWhere(bool test(E element)) {
_listBase.removeWhere(test);
}
void replaceRange(int start, int end, Iterable<E> iterable) {
_listBase.replaceRange(start, end, iterable);
}
void retainWhere(bool test(E element)) {
_listBase.retainWhere(test);
}
// TODO: Dart 2.0 requires this method to be implemented.
List<T> retype<T>() {
throw new UnimplementedError('retype');
}
Iterable<E> get reversed => _listBase.reversed;
void setAll(int index, Iterable<E> iterable) {
_listBase.setAll(index, iterable);
}
void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
_listBase.setRange(start, end, iterable, skipCount);
}
void shuffle([math.Random random]) {
_listBase.shuffle(random);
}
void sort([int compare(E a, E b)]) {
_listBase.sort(compare);
}
List<E> sublist(int start, [int end]) => _listBase.sublist(start, end);
}
/// A [Set] that delegates all operations to a base set.
///
/// This class can be used to hide non-`Set` methods of a set object, or it can
/// be extended to add extra functionality on top of an existing set object.
class DelegatingSet<E> extends DelegatingIterable<E> implements Set<E> {
const DelegatingSet(Set<E> base) : super(base);
/// Creates a wrapper that asserts the types of values in [base].
///
/// This soundly converts a [Set] without a generic type to a `Set<E>` by
/// asserting that its elements are instances of `E` whenever they're
/// accessed. If they're not, it throws a [CastError]. Note that even if an
/// operation throws a [CastError], it may still mutate the underlying
/// collection.
///
/// This forwards all operations to [base], so any changes in [base] will be
/// reflected in [this]. If [base] is already a `Set<E>`, it's returned
/// unmodified.
static Set<E> typed<E>(Set base) =>
base is Set<E> ? base : new TypeSafeSet<E>(base);
Set<E> get _setBase => _base;
bool add(E value) => _setBase.add(value);
void addAll(Iterable<E> elements) {
_setBase.addAll(elements);
}
// TODO: Dart 2.0 requires this method to be implemented.
Set<T> cast<T>() {
throw new UnimplementedError('cast');
}
void clear() {
_setBase.clear();
}
bool containsAll(Iterable<Object> other) => _setBase.containsAll(other);
Set<E> difference(Set<Object> other) => _setBase.difference(other);
Set<E> intersection(Set<Object> other) => _setBase.intersection(other);
E lookup(Object element) => _setBase.lookup(element);
bool remove(Object value) => _setBase.remove(value);
void removeAll(Iterable<Object> elements) {
_setBase.removeAll(elements);
}
void removeWhere(bool test(E element)) {
_setBase.removeWhere(test);
}
void retainAll(Iterable<Object> elements) {
_setBase.retainAll(elements);
}
// TODO: Dart 2.0 requires this method to be implemented.
Set<T> retype<T>() {
throw new UnimplementedError('retype');
}
void retainWhere(bool test(E element)) {
_setBase.retainWhere(test);
}
Set<E> union(Set<E> other) => _setBase.union(other);
Set<E> toSet() => new DelegatingSet<E>(_setBase.toSet());
}
/// A [Queue] that delegates all operations to a base queue.
///
/// This class can be used to hide non-`Queue` methods of a queue object, or it
/// can be extended to add extra functionality on top of an existing queue
/// object.
class DelegatingQueue<E> extends DelegatingIterable<E> implements Queue<E> {
const DelegatingQueue(Queue<E> queue) : super(queue);
/// Creates a wrapper that asserts the types of values in [base].
///
/// This soundly converts a [Queue] without a generic type to a `Queue<E>` by
/// asserting that its elements are instances of `E` whenever they're
/// accessed. If they're not, it throws a [CastError]. Note that even if an
/// operation throws a [CastError], it may still mutate the underlying
/// collection.
///
/// This forwards all operations to [base], so any changes in [base] will be
/// reflected in [this]. If [base] is already a `Queue<E>`, it's returned
/// unmodified.
static Queue<E> typed<E>(Queue base) =>
base is Queue<E> ? base : new TypeSafeQueue<E>(base);
Queue<E> get _baseQueue => _base;
void add(E value) {
_baseQueue.add(value);
}
void addAll(Iterable<E> iterable) {
_baseQueue.addAll(iterable);
}
void addFirst(E value) {
_baseQueue.addFirst(value);
}
void addLast(E value) {
_baseQueue.addLast(value);
}
// TODO: Dart 2.0 requires this method to be implemented.
Queue<T> cast<T>() {
throw new UnimplementedError('cast');
}
void clear() {
_baseQueue.clear();
}
bool remove(Object object) => _baseQueue.remove(object);
void removeWhere(bool test(E element)) {
_baseQueue.removeWhere(test);
}
void retainWhere(bool test(E element)) {
_baseQueue.retainWhere(test);
}
// TODO: Dart 2.0 requires this method to be implemented.
Queue<T> retype<T>() {
throw new UnimplementedError('retype');
}
E removeFirst() => _baseQueue.removeFirst();
E removeLast() => _baseQueue.removeLast();
}
/// A [Map] that delegates all operations to a base map.
///
/// This class can be used to hide non-`Map` methods of an object that extends
/// `Map`, or it can be extended to add extra functionality on top of an
/// existing map object.
class DelegatingMap<K, V> implements Map<K, V> {
final Map<K, V> _base;
const DelegatingMap(Map<K, V> base) : _base = base;
/// Creates a wrapper that asserts the types of keys and values in [base].
///
/// This soundly converts a [Map] without generic types to a `Map<K, V>` by
/// asserting that its keys are instances of `E` and its values are instances
/// of `V` whenever they're accessed. If they're not, it throws a [CastError].
/// Note that even if an operation throws a [CastError], it may still mutate
/// the underlying collection.
///
/// This forwards all operations to [base], so any changes in [base] will be
/// reflected in [this]. If [base] is already a `Map<K, V>`, it's returned
/// unmodified.
static Map<K, V> typed<K, V>(Map base) =>
base is Map<K, V> ? base : new TypeSafeMap<K, V>(base);
V operator [](Object key) => _base[key];
void operator []=(K key, V value) {
_base[key] = value;
}
void addAll(Map<K, V> other) {
_base.addAll(other);
}
// TODO: Dart 2.0 requires this method to be implemented.
void addEntries(Iterable<Object> entries) {
// Change Iterable<Object> to Iterable<MapEntry<K, V>> when
// the MapEntry class has been added.
throw new UnimplementedError('addEntries');
}
void clear() {
_base.clear();
}
// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> cast<K2, V2>() {
throw new UnimplementedError('cast');
}
bool containsKey(Object key) => _base.containsKey(key);
bool containsValue(Object value) => _base.containsValue(value);
// TODO: Dart 2.0 requires this method to be implemented.
Iterable<Null> get entries {
// Change Iterable<Null> to Iterable<MapEntry<K, V>> when
// the MapEntry class has been added.
throw new UnimplementedError('entries');
}
void forEach(void f(K key, V value)) {
_base.forEach(f);
}
bool get isEmpty => _base.isEmpty;
bool get isNotEmpty => _base.isNotEmpty;
Iterable<K> get keys => _base.keys;
int get length => _base.length;
// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> map<K2, V2>(Object transform(K key, V value)) {
// Change Object to MapEntry<K2, V2> when
// the MapEntry class has been added.
throw new UnimplementedError('map');
}
V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent);
V remove(Object key) => _base.remove(key);
// TODO: Dart 2.0 requires this method to be implemented.
void removeWhere(bool test(K key, V value)) {
throw new UnimplementedError('removeWhere');
}
// TODO: Dart 2.0 requires this method to be implemented.
Map<K2, V2> retype<K2, V2>() {
throw new UnimplementedError('retype');
}
Iterable<V> get values => _base.values;
String toString() => _base.toString();
// TODO: Dart 2.0 requires this method to be implemented.
V update(K key, V update(V value), {V ifAbsent()}) {
throw new UnimplementedError('update');
}
// TODO: Dart 2.0 requires this method to be implemented.
void updateAll(V update(K key, V value)) {
throw new UnimplementedError('updateAll');
}
}
/// An unmodifiable [Set] view of the keys of a [Map].
///
/// The set delegates all operations to the underlying map.
///
/// A `Map` can only contain each key once, so its keys can always
/// be viewed as a `Set` without any loss, even if the [Map.keys]
/// getter only shows an [Iterable] view of the keys.
///
/// Note that [lookup] is not supported for this set.
class MapKeySet<E> extends _DelegatingIterableBase<E>
with UnmodifiableSetMixin<E> {
final Map<E, dynamic> _baseMap;
MapKeySet(Map<E, dynamic> base) : _baseMap = base;
Iterable<E> get _base => _baseMap.keys;
// TODO: Dart 2.0 requires this method to be implemented.
Set<T> cast<T>() {
throw new UnimplementedError('cast');
}
bool contains(Object element) => _baseMap.containsKey(element);
bool get isEmpty => _baseMap.isEmpty;
bool get isNotEmpty => _baseMap.isNotEmpty;
int get length => _baseMap.length;
String toString() => "{${_base.join(', ')}}";
bool containsAll(Iterable<Object> other) => other.every(contains);
/// Returns a new set with the the elements of [this] that are not in [other].
///
/// That is, the returned set contains all the elements of this [Set] that are
/// not elements of [other] according to `other.contains`.
///
/// Note that the returned set will use the default equality operation, which
/// may be different than the equality operation [this] uses.
Set<E> difference(Set<Object> other) =>
where((element) => !other.contains(element)).toSet();
/// Returns a new set which is the intersection between [this] and [other].
///
/// That is, the returned set contains all the elements of this [Set] that are
/// also elements of [other] according to `other.contains`.
///
/// Note that the returned set will use the default equality operation, which
/// may be different than the equality operation [this] uses.
Set<E> intersection(Set<Object> other) => where(other.contains).toSet();
/// Throws an [UnsupportedError] since there's no corresponding method for
/// [Map]s.
E lookup(Object element) =>
throw new UnsupportedError("MapKeySet doesn't support lookup().");
// TODO: Dart 2.0 requires this method to be implemented.
Set<T> retype<T>() {
throw new UnimplementedError('retype');
}
/// Returns a new set which contains all the elements of [this] and [other].
///
/// That is, the returned set contains all the elements of this [Set] and all
/// the elements of [other].
///
/// Note that the returned set will use the default equality operation, which
/// may be different than the equality operation [this] uses.
Set<E> union(Set<E> other) => toSet()..addAll(other);
}
/// Creates a modifiable [Set] view of the values of a [Map].
///
/// The `Set` view assumes that the keys of the `Map` can be uniquely determined
/// from the values. The `keyForValue` function passed to the constructor finds
/// the key for a single value. The `keyForValue` function should be consistent
/// with equality. If `value1 == value2` then `keyForValue(value1)` and
/// `keyForValue(value2)` should be considered equal keys by the underlying map,
/// and vice versa.
///
/// Modifying the set will modify the underlying map based on the key returned
/// by `keyForValue`.
///
/// If the `Map` contents are not compatible with the `keyForValue` function,
/// the set will not work consistently, and may give meaningless responses or do
/// inconsistent updates.
///
/// This set can, for example, be used on a map from database record IDs to the
/// records. It exposes the records as a set, and allows for writing both
/// `recordSet.add(databaseRecord)` and `recordMap[id]`.
///
/// Effectively, the map will act as a kind of index for the set.
class MapValueSet<K, V> extends _DelegatingIterableBase<V> implements Set<V> {
final Map<K, V> _baseMap;
final _KeyForValue<K, V> _keyForValue;
/// Creates a new [MapValueSet] based on [base].
///
/// [keyForValue] returns the key in the map that should be associated with
/// the given value. The set's notion of equality is identical to the equality
/// of the return values of [keyForValue].
MapValueSet(Map<K, V> base, K keyForValue(V value))
: _baseMap = base,
_keyForValue = keyForValue;
Iterable<V> get _base => _baseMap.values;
// TODO: Dart 2.0 requires this method to be implemented.
Set<T> cast<T>() {
throw new UnimplementedError('cast');
}
bool contains(Object element) {
if (element != null && element is! V) return false;
var key = _keyForValue(element as V);
return _baseMap.containsKey(key);
}
bool get isEmpty => _baseMap.isEmpty;
bool get isNotEmpty => _baseMap.isNotEmpty;
int get length => _baseMap.length;
String toString() => toSet().toString();
bool add(V value) {
K key = _keyForValue(value);
bool result = false;
_baseMap.putIfAbsent(key, () {
result = true;
return value;
});
return result;
}
void addAll(Iterable<V> elements) => elements.forEach(add);
void clear() => _baseMap.clear();
bool containsAll(Iterable<Object> other) => other.every(contains);
/// Returns a new set with the the elements of [this] that are not in [other].
///
/// That is, the returned set contains all the elements of this [Set] that are
/// not elements of [other] according to `other.contains`.
///
/// Note that the returned set will use the default equality operation, which
/// may be different than the equality operation [this] uses.
Set<V> difference(Set<Object> other) =>
where((element) => !other.contains(element)).toSet();
/// Returns a new set which is the intersection between [this] and [other].
///
/// That is, the returned set contains all the elements of this [Set] that are
/// also elements of [other] according to `other.contains`.
///
/// Note that the returned set will use the default equality operation, which
/// may be different than the equality operation [this] uses.
Set<V> intersection(Set<Object> other) => where(other.contains).toSet();
V lookup(Object element) {
if (element != null && element is! V) return null;
var key = _keyForValue(element as V);
return _baseMap[key];
}
bool remove(Object element) {
if (element != null && element is! V) return false;
var key = _keyForValue(element as V);
if (!_baseMap.containsKey(key)) return false;
_baseMap.remove(key);
return true;
}
void removeAll(Iterable<Object> elements) => elements.forEach(remove);
void removeWhere(bool test(V element)) {
var toRemove = [];
_baseMap.forEach((key, value) {
if (test(value)) toRemove.add(key);
});
toRemove.forEach(_baseMap.remove);
}
void retainAll(Iterable<Object> elements) {
var valuesToRetain = new Set<V>.identity();
for (var element in elements) {
if (element != null && element is! V) continue;
var key = _keyForValue(element as V);
if (!_baseMap.containsKey(key)) continue;
valuesToRetain.add(_baseMap[key]);
}
var keysToRemove = [];
_baseMap.forEach((k, v) {
if (!valuesToRetain.contains(v)) keysToRemove.add(k);
});
keysToRemove.forEach(_baseMap.remove);
}
void retainWhere(bool test(V element)) =>
removeWhere((element) => !test(element));
// TODO: Dart 2.0 requires this method to be implemented.
Set<T> retype<T>() {
throw new UnimplementedError('retype');
}
/// Returns a new set which contains all the elements of [this] and [other].
///
/// That is, the returned set contains all the elements of this [Set] and all
/// the elements of [other].
///
/// Note that the returned set will use the default equality operation, which
/// may be different than the equality operation [this] uses.
Set<V> union(Set<V> other) => toSet()..addAll(other);
}