Line data Source code
1 : // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 : // for details. All rights reserved. Use of this source code is governed by a
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : import 'dart:collection';
6 : import 'dart:math' as math;
7 :
8 : import 'unmodifiable_wrappers.dart';
9 :
10 : /// A base class for delegating iterables.
11 : ///
12 : /// Subclasses can provide a [_base] that should be delegated to. Unlike
13 : /// [DelegatingIterable], this allows the base to be created on demand.
14 : abstract class _DelegatingIterableBase<E> implements Iterable<E> {
15 : Iterable<E> get _base;
16 :
17 11 : const _DelegatingIterableBase();
18 :
19 0 : @override
20 0 : bool any(bool Function(E) test) => _base.any(test);
21 :
22 0 : @override
23 0 : Iterable<T> cast<T>() => _base.cast<T>();
24 :
25 0 : @override
26 0 : bool contains(Object? element) => _base.contains(element);
27 :
28 0 : @override
29 0 : E elementAt(int index) => _base.elementAt(index);
30 :
31 0 : @override
32 0 : bool every(bool Function(E) test) => _base.every(test);
33 :
34 0 : @override
35 0 : Iterable<T> expand<T>(Iterable<T> Function(E) f) => _base.expand(f);
36 :
37 0 : @override
38 0 : E get first => _base.first;
39 :
40 0 : @override
41 : E firstWhere(bool Function(E) test, {E Function()? orElse}) =>
42 0 : _base.firstWhere(test, orElse: orElse);
43 :
44 0 : @override
45 : T fold<T>(T initialValue, T Function(T previousValue, E element) combine) =>
46 0 : _base.fold(initialValue, combine);
47 :
48 0 : @override
49 0 : Iterable<E> followedBy(Iterable<E> other) => _base.followedBy(other);
50 :
51 0 : @override
52 0 : void forEach(void Function(E) f) => _base.forEach(f);
53 :
54 0 : @override
55 0 : bool get isEmpty => _base.isEmpty;
56 :
57 0 : @override
58 0 : bool get isNotEmpty => _base.isNotEmpty;
59 :
60 0 : @override
61 0 : Iterator<E> get iterator => _base.iterator;
62 :
63 0 : @override
64 0 : String join([String separator = '']) => _base.join(separator);
65 :
66 0 : @override
67 0 : E get last => _base.last;
68 :
69 0 : @override
70 : E lastWhere(bool Function(E) test, {E Function()? orElse}) =>
71 0 : _base.lastWhere(test, orElse: orElse);
72 :
73 0 : @override
74 0 : int get length => _base.length;
75 :
76 0 : @override
77 0 : Iterable<T> map<T>(T Function(E) f) => _base.map(f);
78 :
79 0 : @override
80 0 : E reduce(E Function(E value, E element) combine) => _base.reduce(combine);
81 :
82 0 : @deprecated
83 0 : Iterable<T> retype<T>() => cast<T>();
84 :
85 0 : @override
86 0 : E get single => _base.single;
87 :
88 0 : @override
89 : E singleWhere(bool Function(E) test, {E Function()? orElse}) {
90 0 : return _base.singleWhere(test, orElse: orElse);
91 : }
92 :
93 0 : @override
94 0 : Iterable<E> skip(int n) => _base.skip(n);
95 :
96 0 : @override
97 0 : Iterable<E> skipWhile(bool Function(E) test) => _base.skipWhile(test);
98 :
99 0 : @override
100 0 : Iterable<E> take(int n) => _base.take(n);
101 :
102 0 : @override
103 0 : Iterable<E> takeWhile(bool Function(E) test) => _base.takeWhile(test);
104 :
105 11 : @override
106 22 : List<E> toList({bool growable = true}) => _base.toList(growable: growable);
107 :
108 0 : @override
109 0 : Set<E> toSet() => _base.toSet();
110 :
111 11 : @override
112 22 : Iterable<E> where(bool Function(E) test) => _base.where(test);
113 :
114 0 : @override
115 0 : Iterable<T> whereType<T>() => _base.whereType<T>();
116 :
117 0 : @override
118 0 : String toString() => _base.toString();
119 : }
120 :
121 : /// An [Iterable] that delegates all operations to a base iterable.
122 : ///
123 : /// This class can be used to hide non-`Iterable` methods of an iterable object,
124 : /// or it can be extended to add extra functionality on top of an existing
125 : /// iterable object.
126 : class DelegatingIterable<E> extends _DelegatingIterableBase<E> {
127 : @override
128 : final Iterable<E> _base;
129 :
130 : /// Creates a wrapper that forwards operations to [base].
131 0 : const DelegatingIterable(Iterable<E> base) : _base = base;
132 :
133 : /// Creates a wrapper that asserts the types of values in [base].
134 : ///
135 : /// This soundly converts an [Iterable] without a generic type to an
136 : /// `Iterable<E>` by asserting that its elements are instances of `E` whenever
137 : /// they're accessed. If they're not, it throws a [CastError].
138 : ///
139 : /// This forwards all operations to [base], so any changes in [base] will be
140 : /// reflected in [this]. If [base] is already an `Iterable<E>`, it's returned
141 : /// unmodified.
142 0 : @Deprecated('Use iterable.cast<E> instead.')
143 0 : static Iterable<E> typed<E>(Iterable base) => base.cast<E>();
144 : }
145 :
146 : /// A [List] that delegates all operations to a base list.
147 : ///
148 : /// This class can be used to hide non-`List` methods of a list object, or it
149 : /// can be extended to add extra functionality on top of an existing list
150 : /// object.
151 : class DelegatingList<E> extends _DelegatingIterableBase<E> implements List<E> {
152 : @override
153 : final List<E> _base;
154 :
155 0 : const DelegatingList(List<E> base) : _base = base;
156 :
157 : /// Creates a wrapper that asserts the types of values in [base].
158 : ///
159 : /// This soundly converts a [List] without a generic type to a `List<E>` by
160 : /// asserting that its elements are instances of `E` whenever they're
161 : /// accessed. If they're not, it throws a [CastError]. Note that even if an
162 : /// operation throws a [CastError], it may still mutate the underlying
163 : /// collection.
164 : ///
165 : /// This forwards all operations to [base], so any changes in [base] will be
166 : /// reflected in [this]. If [base] is already a `List<E>`, it's returned
167 : /// unmodified.
168 0 : @Deprecated('Use list.cast<E> instead.')
169 0 : static List<E> typed<E>(List base) => base.cast<E>();
170 :
171 0 : @override
172 0 : E operator [](int index) => _base[index];
173 :
174 0 : @override
175 : void operator []=(int index, E value) {
176 0 : _base[index] = value;
177 : }
178 :
179 0 : @override
180 0 : List<E> operator +(List<E> other) => _base + other;
181 :
182 0 : @override
183 : void add(E value) {
184 0 : _base.add(value);
185 : }
186 :
187 0 : @override
188 : void addAll(Iterable<E> iterable) {
189 0 : _base.addAll(iterable);
190 : }
191 :
192 0 : @override
193 0 : Map<int, E> asMap() => _base.asMap();
194 :
195 0 : @override
196 0 : List<T> cast<T>() => _base.cast<T>();
197 :
198 0 : @override
199 : void clear() {
200 0 : _base.clear();
201 : }
202 :
203 0 : @override
204 : void fillRange(int start, int end, [E? fillValue]) {
205 0 : _base.fillRange(start, end, fillValue);
206 : }
207 :
208 0 : @override
209 : set first(E value) {
210 0 : if (isEmpty) throw RangeError.index(0, this);
211 0 : this[0] = value;
212 : }
213 :
214 0 : @override
215 0 : Iterable<E> getRange(int start, int end) => _base.getRange(start, end);
216 :
217 0 : @override
218 0 : int indexOf(E element, [int start = 0]) => _base.indexOf(element, start);
219 :
220 0 : @override
221 : int indexWhere(bool Function(E) test, [int start = 0]) =>
222 0 : _base.indexWhere(test, start);
223 :
224 0 : @override
225 : void insert(int index, E element) {
226 0 : _base.insert(index, element);
227 : }
228 :
229 0 : @override
230 : void insertAll(int index, Iterable<E> iterable) {
231 0 : _base.insertAll(index, iterable);
232 : }
233 :
234 0 : @override
235 : set last(E value) {
236 0 : if (isEmpty) throw RangeError.index(0, this);
237 0 : this[length - 1] = value;
238 : }
239 :
240 0 : @override
241 0 : int lastIndexOf(E element, [int? start]) => _base.lastIndexOf(element, start);
242 :
243 0 : @override
244 : int lastIndexWhere(bool Function(E) test, [int? start]) =>
245 0 : _base.lastIndexWhere(test, start);
246 :
247 0 : @override
248 : set length(int newLength) {
249 0 : _base.length = newLength;
250 : }
251 :
252 0 : @override
253 0 : bool remove(Object? value) => _base.remove(value);
254 :
255 0 : @override
256 0 : E removeAt(int index) => _base.removeAt(index);
257 :
258 0 : @override
259 0 : E removeLast() => _base.removeLast();
260 :
261 0 : @override
262 : void removeRange(int start, int end) {
263 0 : _base.removeRange(start, end);
264 : }
265 :
266 0 : @override
267 : void removeWhere(bool Function(E) test) {
268 0 : _base.removeWhere(test);
269 : }
270 :
271 0 : @override
272 : void replaceRange(int start, int end, Iterable<E> iterable) {
273 0 : _base.replaceRange(start, end, iterable);
274 : }
275 :
276 0 : @override
277 : void retainWhere(bool Function(E) test) {
278 0 : _base.retainWhere(test);
279 : }
280 :
281 0 : @deprecated
282 : @override
283 0 : List<T> retype<T>() => cast<T>();
284 :
285 0 : @override
286 0 : Iterable<E> get reversed => _base.reversed;
287 :
288 0 : @override
289 : void setAll(int index, Iterable<E> iterable) {
290 0 : _base.setAll(index, iterable);
291 : }
292 :
293 0 : @override
294 : void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]) {
295 0 : _base.setRange(start, end, iterable, skipCount);
296 : }
297 :
298 0 : @override
299 : void shuffle([math.Random? random]) {
300 0 : _base.shuffle(random);
301 : }
302 :
303 0 : @override
304 : void sort([int Function(E, E)? compare]) {
305 0 : _base.sort(compare);
306 : }
307 :
308 0 : @override
309 0 : List<E> sublist(int start, [int? end]) => _base.sublist(start, end);
310 : }
311 :
312 : /// A [Set] that delegates all operations to a base set.
313 : ///
314 : /// This class can be used to hide non-`Set` methods of a set object, or it can
315 : /// be extended to add extra functionality on top of an existing set object.
316 : class DelegatingSet<E> extends _DelegatingIterableBase<E> implements Set<E> {
317 : @override
318 : final Set<E> _base;
319 :
320 11 : const DelegatingSet(Set<E> base) : _base = base;
321 :
322 : /// Creates a wrapper that asserts the types of values in [base].
323 : ///
324 : /// This soundly converts a [Set] without a generic type to a `Set<E>` by
325 : /// asserting that its elements are instances of `E` whenever they're
326 : /// accessed. If they're not, it throws a [CastError]. Note that even if an
327 : /// operation throws a [CastError], it may still mutate the underlying
328 : /// collection.
329 : ///
330 : /// This forwards all operations to [base], so any changes in [base] will be
331 : /// reflected in [this]. If [base] is already a `Set<E>`, it's returned
332 : /// unmodified.
333 0 : @Deprecated('Use set.cast<E> instead.')
334 0 : static Set<E> typed<E>(Set base) => base.cast<E>();
335 :
336 0 : @override
337 0 : bool add(E value) => _base.add(value);
338 :
339 0 : @override
340 : void addAll(Iterable<E> elements) {
341 0 : _base.addAll(elements);
342 : }
343 :
344 0 : @override
345 0 : Set<T> cast<T>() => _base.cast<T>();
346 :
347 0 : @override
348 : void clear() {
349 0 : _base.clear();
350 : }
351 :
352 0 : @override
353 0 : bool containsAll(Iterable<Object?> other) => _base.containsAll(other);
354 :
355 0 : @override
356 0 : Set<E> difference(Set<Object?> other) => _base.difference(other);
357 :
358 0 : @override
359 0 : Set<E> intersection(Set<Object?> other) => _base.intersection(other);
360 :
361 0 : @override
362 0 : E? lookup(Object? element) => _base.lookup(element);
363 :
364 0 : @override
365 0 : bool remove(Object? value) => _base.remove(value);
366 :
367 0 : @override
368 : void removeAll(Iterable<Object?> elements) {
369 0 : _base.removeAll(elements);
370 : }
371 :
372 0 : @override
373 : void removeWhere(bool Function(E) test) {
374 0 : _base.removeWhere(test);
375 : }
376 :
377 0 : @override
378 : void retainAll(Iterable<Object?> elements) {
379 0 : _base.retainAll(elements);
380 : }
381 :
382 0 : @deprecated
383 : @override
384 0 : Set<T> retype<T>() => cast<T>();
385 :
386 0 : @override
387 : void retainWhere(bool Function(E) test) {
388 0 : _base.retainWhere(test);
389 : }
390 :
391 2 : @override
392 4 : Set<E> union(Set<E> other) => _base.union(other);
393 :
394 0 : @override
395 0 : Set<E> toSet() => DelegatingSet<E>(_base.toSet());
396 : }
397 :
398 : /// A [Queue] that delegates all operations to a base queue.
399 : ///
400 : /// This class can be used to hide non-`Queue` methods of a queue object, or it
401 : /// can be extended to add extra functionality on top of an existing queue
402 : /// object.
403 : class DelegatingQueue<E> extends _DelegatingIterableBase<E>
404 : implements Queue<E> {
405 : @override
406 : final Queue<E> _base;
407 :
408 0 : const DelegatingQueue(Queue<E> queue) : _base = queue;
409 :
410 : /// Creates a wrapper that asserts the types of values in [base].
411 : ///
412 : /// This soundly converts a [Queue] without a generic type to a `Queue<E>` by
413 : /// asserting that its elements are instances of `E` whenever they're
414 : /// accessed. If they're not, it throws a [CastError]. Note that even if an
415 : /// operation throws a [CastError], it may still mutate the underlying
416 : /// collection.
417 : ///
418 : /// This forwards all operations to [base], so any changes in [base] will be
419 : /// reflected in [this]. If [base] is already a `Queue<E>`, it's returned
420 : /// unmodified.
421 0 : @Deprecated('Use queue.cast<E> instead.')
422 0 : static Queue<E> typed<E>(Queue base) => base.cast<E>();
423 :
424 0 : @override
425 : void add(E value) {
426 0 : _base.add(value);
427 : }
428 :
429 0 : @override
430 : void addAll(Iterable<E> iterable) {
431 0 : _base.addAll(iterable);
432 : }
433 :
434 0 : @override
435 : void addFirst(E value) {
436 0 : _base.addFirst(value);
437 : }
438 :
439 0 : @override
440 : void addLast(E value) {
441 0 : _base.addLast(value);
442 : }
443 :
444 0 : @override
445 0 : Queue<T> cast<T>() => _base.cast<T>();
446 :
447 0 : @override
448 : void clear() {
449 0 : _base.clear();
450 : }
451 :
452 0 : @override
453 0 : bool remove(Object? object) => _base.remove(object);
454 :
455 0 : @override
456 : void removeWhere(bool Function(E) test) {
457 0 : _base.removeWhere(test);
458 : }
459 :
460 0 : @override
461 : void retainWhere(bool Function(E) test) {
462 0 : _base.retainWhere(test);
463 : }
464 :
465 0 : @deprecated
466 : @override
467 0 : Queue<T> retype<T>() => cast<T>();
468 :
469 0 : @override
470 0 : E removeFirst() => _base.removeFirst();
471 :
472 0 : @override
473 0 : E removeLast() => _base.removeLast();
474 : }
475 :
476 : /// A [Map] that delegates all operations to a base map.
477 : ///
478 : /// This class can be used to hide non-`Map` methods of an object that extends
479 : /// `Map`, or it can be extended to add extra functionality on top of an
480 : /// existing map object.
481 : class DelegatingMap<K, V> implements Map<K, V> {
482 : final Map<K, V> _base;
483 :
484 0 : const DelegatingMap(Map<K, V> base) : _base = base;
485 :
486 : /// Creates a wrapper that asserts the types of keys and values in [base].
487 : ///
488 : /// This soundly converts a [Map] without generic types to a `Map<K, V>` by
489 : /// asserting that its keys are instances of `E` and its values are instances
490 : /// of `V` whenever they're accessed. If they're not, it throws a [CastError].
491 : /// Note that even if an operation throws a [CastError], it may still mutate
492 : /// the underlying collection.
493 : ///
494 : /// This forwards all operations to [base], so any changes in [base] will be
495 : /// reflected in [this]. If [base] is already a `Map<K, V>`, it's returned
496 : /// unmodified.
497 0 : @Deprecated('Use map.cast<K, V> instead.')
498 0 : static Map<K, V> typed<K, V>(Map base) => base.cast<K, V>();
499 :
500 0 : @override
501 0 : V? operator [](Object? key) => _base[key];
502 :
503 0 : @override
504 : void operator []=(K key, V value) {
505 0 : _base[key] = value;
506 : }
507 :
508 0 : @override
509 : void addAll(Map<K, V> other) {
510 0 : _base.addAll(other);
511 : }
512 :
513 0 : @override
514 : void addEntries(Iterable<MapEntry<K, V>> entries) {
515 0 : _base.addEntries(entries);
516 : }
517 :
518 0 : @override
519 : void clear() {
520 0 : _base.clear();
521 : }
522 :
523 0 : @override
524 0 : Map<K2, V2> cast<K2, V2>() => _base.cast<K2, V2>();
525 :
526 0 : @override
527 0 : bool containsKey(Object? key) => _base.containsKey(key);
528 :
529 0 : @override
530 0 : bool containsValue(Object? value) => _base.containsValue(value);
531 :
532 0 : @override
533 0 : Iterable<MapEntry<K, V>> get entries => _base.entries;
534 :
535 0 : @override
536 : void forEach(void Function(K, V) f) {
537 0 : _base.forEach(f);
538 : }
539 :
540 0 : @override
541 0 : bool get isEmpty => _base.isEmpty;
542 :
543 0 : @override
544 0 : bool get isNotEmpty => _base.isNotEmpty;
545 :
546 0 : @override
547 0 : Iterable<K> get keys => _base.keys;
548 :
549 0 : @override
550 0 : int get length => _base.length;
551 :
552 0 : @override
553 : Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> Function(K, V) transform) =>
554 0 : _base.map(transform);
555 :
556 0 : @override
557 : V putIfAbsent(K key, V Function() ifAbsent) =>
558 0 : _base.putIfAbsent(key, ifAbsent);
559 :
560 0 : @override
561 0 : V? remove(Object? key) => _base.remove(key);
562 :
563 0 : @override
564 0 : void removeWhere(bool Function(K, V) test) => _base.removeWhere(test);
565 :
566 0 : @deprecated
567 0 : Map<K2, V2> retype<K2, V2>() => cast<K2, V2>();
568 :
569 0 : @override
570 0 : Iterable<V> get values => _base.values;
571 :
572 0 : @override
573 0 : String toString() => _base.toString();
574 :
575 0 : @override
576 : V update(K key, V Function(V) update, {V Function()? ifAbsent}) =>
577 0 : _base.update(key, update, ifAbsent: ifAbsent);
578 :
579 0 : @override
580 0 : void updateAll(V Function(K, V) update) => _base.updateAll(update);
581 : }
582 :
583 : /// An unmodifiable [Set] view of the keys of a [Map].
584 : ///
585 : /// The set delegates all operations to the underlying map.
586 : ///
587 : /// A `Map` can only contain each key once, so its keys can always
588 : /// be viewed as a `Set` without any loss, even if the [Map.keys]
589 : /// getter only shows an [Iterable] view of the keys.
590 : ///
591 : /// Note that [lookup] is not supported for this set.
592 : class MapKeySet<E> extends _DelegatingIterableBase<E>
593 : with UnmodifiableSetMixin<E> {
594 : final Map<E, dynamic> _baseMap;
595 :
596 0 : MapKeySet(this._baseMap);
597 :
598 0 : @override
599 0 : Iterable<E> get _base => _baseMap.keys;
600 :
601 0 : @override
602 : Set<T> cast<T>() {
603 0 : if (this is MapKeySet<T>) {
604 : return this as MapKeySet<T>;
605 : }
606 0 : return Set.castFrom<E, T>(this);
607 : }
608 :
609 0 : @override
610 0 : bool contains(Object? element) => _baseMap.containsKey(element);
611 :
612 0 : @override
613 0 : bool get isEmpty => _baseMap.isEmpty;
614 :
615 0 : @override
616 0 : bool get isNotEmpty => _baseMap.isNotEmpty;
617 :
618 0 : @override
619 0 : int get length => _baseMap.length;
620 :
621 0 : @override
622 0 : String toString() => SetBase.setToString(this);
623 :
624 0 : @override
625 0 : bool containsAll(Iterable<Object?> other) => other.every(contains);
626 :
627 : /// Returns a new set with the the elements of [this] that are not in [other].
628 : ///
629 : /// That is, the returned set contains all the elements of this [Set] that are
630 : /// not elements of [other] according to `other.contains`.
631 : ///
632 : /// Note that the returned set will use the default equality operation, which
633 : /// may be different than the equality operation [this] uses.
634 0 : @override
635 : Set<E> difference(Set<Object?> other) =>
636 0 : where((element) => !other.contains(element)).toSet();
637 :
638 : /// Returns a new set which is the intersection between [this] and [other].
639 : ///
640 : /// That is, the returned set contains all the elements of this [Set] that are
641 : /// also elements of [other] according to `other.contains`.
642 : ///
643 : /// Note that the returned set will use the default equality operation, which
644 : /// may be different than the equality operation [this] uses.
645 0 : @override
646 0 : Set<E> intersection(Set<Object?> other) => where(other.contains).toSet();
647 :
648 : /// Throws an [UnsupportedError] since there's no corresponding method for
649 : /// [Map]s.
650 0 : @override
651 : E lookup(Object? element) =>
652 0 : throw UnsupportedError("MapKeySet doesn't support lookup().");
653 :
654 0 : @deprecated
655 : @override
656 0 : Set<T> retype<T>() => Set.castFrom<E, T>(this);
657 :
658 : /// Returns a new set which contains all the elements of [this] and [other].
659 : ///
660 : /// That is, the returned set contains all the elements of this [Set] and all
661 : /// the elements of [other].
662 : ///
663 : /// Note that the returned set will use the default equality operation, which
664 : /// may be different than the equality operation [this] uses.
665 0 : @override
666 0 : Set<E> union(Set<E> other) => toSet()..addAll(other);
667 : }
668 :
669 : /// Creates a modifiable [Set] view of the values of a [Map].
670 : ///
671 : /// The `Set` view assumes that the keys of the `Map` can be uniquely determined
672 : /// from the values. The `keyForValue` function passed to the constructor finds
673 : /// the key for a single value. The `keyForValue` function should be consistent
674 : /// with equality. If `value1 == value2` then `keyForValue(value1)` and
675 : /// `keyForValue(value2)` should be considered equal keys by the underlying map,
676 : /// and vice versa.
677 : ///
678 : /// Modifying the set will modify the underlying map based on the key returned
679 : /// by `keyForValue`.
680 : ///
681 : /// If the `Map` contents are not compatible with the `keyForValue` function,
682 : /// the set will not work consistently, and may give meaningless responses or do
683 : /// inconsistent updates.
684 : ///
685 : /// This set can, for example, be used on a map from database record IDs to the
686 : /// records. It exposes the records as a set, and allows for writing both
687 : /// `recordSet.add(databaseRecord)` and `recordMap[id]`.
688 : ///
689 : /// Effectively, the map will act as a kind of index for the set.
690 : class MapValueSet<K, V> extends _DelegatingIterableBase<V> implements Set<V> {
691 : final Map<K, V> _baseMap;
692 : final K Function(V) _keyForValue;
693 :
694 : /// Creates a new [MapValueSet] based on [_baseMap].
695 : ///
696 : /// [_keyForValue] returns the key in the map that should be associated with
697 : /// the given value. The set's notion of equality is identical to the equality
698 : /// of the return values of [_keyForValue].
699 0 : MapValueSet(this._baseMap, this._keyForValue);
700 :
701 0 : @override
702 0 : Iterable<V> get _base => _baseMap.values;
703 :
704 0 : @override
705 : Set<T> cast<T>() {
706 0 : if (this is Set<T>) {
707 : return this as Set<T>;
708 : }
709 0 : return Set.castFrom<V, T>(this);
710 : }
711 :
712 0 : @override
713 : bool contains(Object? element) {
714 0 : if (element is! V) return false;
715 0 : var key = _keyForValue(element);
716 :
717 0 : return _baseMap.containsKey(key);
718 : }
719 :
720 0 : @override
721 0 : bool get isEmpty => _baseMap.isEmpty;
722 :
723 0 : @override
724 0 : bool get isNotEmpty => _baseMap.isNotEmpty;
725 :
726 0 : @override
727 0 : int get length => _baseMap.length;
728 :
729 0 : @override
730 0 : String toString() => toSet().toString();
731 :
732 0 : @override
733 : bool add(V value) {
734 0 : var key = _keyForValue(value);
735 : var result = false;
736 0 : _baseMap.putIfAbsent(key, () {
737 : result = true;
738 : return value;
739 : });
740 : return result;
741 : }
742 :
743 0 : @override
744 0 : void addAll(Iterable<V> elements) => elements.forEach(add);
745 :
746 0 : @override
747 0 : void clear() => _baseMap.clear();
748 :
749 0 : @override
750 0 : bool containsAll(Iterable<Object?> other) => other.every(contains);
751 :
752 : /// Returns a new set with the the elements of [this] that are not in [other].
753 : ///
754 : /// That is, the returned set contains all the elements of this [Set] that are
755 : /// not elements of [other] according to `other.contains`.
756 : ///
757 : /// Note that the returned set will use the default equality operation, which
758 : /// may be different than the equality operation [this] uses.
759 0 : @override
760 : Set<V> difference(Set<Object?> other) =>
761 0 : where((element) => !other.contains(element)).toSet();
762 :
763 : /// Returns a new set which is the intersection between [this] and [other].
764 : ///
765 : /// That is, the returned set contains all the elements of this [Set] that are
766 : /// also elements of [other] according to `other.contains`.
767 : ///
768 : /// Note that the returned set will use the default equality operation, which
769 : /// may be different than the equality operation [this] uses.
770 0 : @override
771 0 : Set<V> intersection(Set<Object?> other) => where(other.contains).toSet();
772 :
773 0 : @override
774 : V? lookup(Object? element) {
775 0 : if (element is! V) return null;
776 0 : var key = _keyForValue(element);
777 :
778 0 : return _baseMap[key];
779 : }
780 :
781 0 : @override
782 : bool remove(Object? element) {
783 0 : if (element is! V) return false;
784 0 : var key = _keyForValue(element);
785 :
786 0 : if (!_baseMap.containsKey(key)) return false;
787 0 : _baseMap.remove(key);
788 : return true;
789 : }
790 :
791 0 : @override
792 0 : void removeAll(Iterable<Object?> elements) => elements.forEach(remove);
793 :
794 0 : @override
795 : void removeWhere(bool Function(V) test) {
796 0 : var toRemove = [];
797 0 : _baseMap.forEach((key, value) {
798 0 : if (test(value)) toRemove.add(key);
799 : });
800 0 : toRemove.forEach(_baseMap.remove);
801 : }
802 :
803 0 : @override
804 : void retainAll(Iterable<Object?> elements) {
805 0 : var valuesToRetain = Set<V>.identity();
806 0 : for (var element in elements) {
807 0 : if (element is! V) continue;
808 0 : var key = _keyForValue(element);
809 :
810 0 : if (!_baseMap.containsKey(key)) continue;
811 0 : valuesToRetain.add(_baseMap[key] ?? null as V);
812 : }
813 :
814 0 : var keysToRemove = [];
815 0 : _baseMap.forEach((k, v) {
816 0 : if (!valuesToRetain.contains(v)) keysToRemove.add(k);
817 : });
818 0 : keysToRemove.forEach(_baseMap.remove);
819 : }
820 :
821 0 : @override
822 : void retainWhere(bool Function(V) test) =>
823 0 : removeWhere((element) => !test(element));
824 :
825 0 : @deprecated
826 : @override
827 0 : Set<T> retype<T>() => Set.castFrom<V, T>(this);
828 :
829 : /// Returns a new set which contains all the elements of [this] and [other].
830 : ///
831 : /// That is, the returned set contains all the elements of this [Set] and all
832 : /// the elements of [other].
833 : ///
834 : /// Note that the returned set will use the default equality operation, which
835 : /// may be different than the equality operation [this] uses.
836 0 : @override
837 0 : Set<V> union(Set<V> other) => toSet()..addAll(other);
838 : }
|