OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Delegating wrappers for [Iterable], [List], [Set], [Queue] and [Map]. | 6 * Delegating wrappers for [Iterable], [List], [Set], [Queue] and [Map]. |
7 * | 7 * |
8 * Also adds unmodifiable views for `Set` and `Map`, and a fixed length | 8 * Also adds unmodifiable views for `Set` and `Map`, and a fixed length |
9 * view for `List`. The unmodifable list view from `dart:collection` is exported | 9 * view for `List`. The unmodifable list view from `dart:collection` is exported |
10 * as well, just for completeness. | 10 * as well, just for completeness. |
11 */ | 11 */ |
12 library dart.pkg.collection.wrappers; | 12 library dart.pkg.collection.wrappers; |
13 | 13 |
14 import "dart:collection"; | 14 import "dart:collection"; |
15 import "dart:math" show Random; | 15 import "dart:math" show Random; |
16 | 16 |
17 export "dart:collection" show UnmodifiableListView; | 17 export "dart:collection" show UnmodifiableListView; |
18 | 18 |
19 part "src/unmodifiable_wrappers.dart"; | 19 part "src/unmodifiable_wrappers.dart"; |
20 | 20 |
21 /** | 21 /** |
22 * Creates an [Iterable] that delegates all operations to a base iterable. | 22 * A base class for delegating iterables. |
23 * | 23 * |
24 * This class can be used hide non-`Iterable` methods of an iterable object, | 24 * Subclasses can provide a [_base] that should be delegated to. Unlike |
25 * or it can be extended to add extra functionality on top of an existing | 25 * [DelegatingIterable], this allows the base to be created on demand. |
26 * iterable object. | |
27 */ | 26 */ |
28 class DelegatingIterable<E> implements Iterable<E> { | 27 abstract class _DelegatingIterableBase<E> implements Iterable<E> { |
29 final Iterable<E> _base; | 28 Iterable<E> get _base; |
30 | 29 |
31 /** | 30 const _DelegatingIterableBase(); |
32 * Create a wrapper that forwards operations to [base]. | |
33 */ | |
34 const DelegatingIterable(Iterable<E> base) : _base = base; | |
35 | 31 |
36 bool any(bool test(E element)) => _base.any(test); | 32 bool any(bool test(E element)) => _base.any(test); |
37 | 33 |
38 bool contains(Object element) => _base.contains(element); | 34 bool contains(Object element) => _base.contains(element); |
39 | 35 |
40 E elementAt(int index) => _base.elementAt(index); | 36 E elementAt(int index) => _base.elementAt(index); |
41 | 37 |
42 bool every(bool test(E element)) => _base.every(test); | 38 bool every(bool test(E element)) => _base.every(test); |
43 | 39 |
44 Iterable expand(Iterable f(E element)) => _base.expand(f); | 40 Iterable expand(Iterable f(E element)) => _base.expand(f); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 | 82 |
87 List<E> toList({bool growable: true}) => _base.toList(growable: growable); | 83 List<E> toList({bool growable: true}) => _base.toList(growable: growable); |
88 | 84 |
89 Set<E> toSet() => _base.toSet(); | 85 Set<E> toSet() => _base.toSet(); |
90 | 86 |
91 Iterable<E> where(bool test(E element)) => _base.where(test); | 87 Iterable<E> where(bool test(E element)) => _base.where(test); |
92 | 88 |
93 String toString() => _base.toString(); | 89 String toString() => _base.toString(); |
94 } | 90 } |
95 | 91 |
92 /** | |
93 * Creates an [Iterable] that delegates all operations to a base iterable. | |
94 * | |
95 * This class can be used hide non-`Iterable` methods of an iterable object, | |
96 * or it can be extended to add extra functionality on top of an existing | |
97 * iterable object. | |
98 */ | |
99 class DelegatingIterable<E> extends _DelegatingIterableBase<E> { | |
100 final Iterable<E> _base; | |
101 | |
102 /** | |
103 * Create a wrapper that forwards operations to [base]. | |
104 */ | |
105 const DelegatingIterable(Iterable<E> base) : _base = base; | |
106 } | |
107 | |
96 | 108 |
97 /** | 109 /** |
98 * Creates a [List] that delegates all operations to a base list. | 110 * Creates a [List] that delegates all operations to a base list. |
99 * | 111 * |
100 * This class can be used hide non-`List` methods of a list object, | 112 * This class can be used hide non-`List` methods of a list object, |
101 * or it can be extended to add extra functionality on top of an existing | 113 * or it can be extended to add extra functionality on top of an existing |
102 * list object. | 114 * list object. |
103 */ | 115 */ |
104 class DelegatingList<E> extends DelegatingIterable<E> implements List<E> { | 116 class DelegatingList<E> extends DelegatingIterable<E> implements List<E> { |
105 const DelegatingList(List<E> base) : super(base); | 117 const DelegatingList(List<E> base) : super(base); |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
330 int get length => _base.length; | 342 int get length => _base.length; |
331 | 343 |
332 V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent); | 344 V putIfAbsent(K key, V ifAbsent()) => _base.putIfAbsent(key, ifAbsent); |
333 | 345 |
334 V remove(Object key) => _base.remove(key); | 346 V remove(Object key) => _base.remove(key); |
335 | 347 |
336 Iterable<V> get values => _base.values; | 348 Iterable<V> get values => _base.values; |
337 | 349 |
338 String toString() => _base.toString(); | 350 String toString() => _base.toString(); |
339 } | 351 } |
352 | |
353 /** | |
354 * An unmodifiable [Set] view of the keys of a [Map]. | |
355 * | |
356 * The set delegates all operations to the underlying map. | |
357 * | |
358 * A `Map` can only contain each key once, so its keys can always | |
359 * be viewed as a `Set` without any loss, even if the [Map.keys] | |
360 * getter only shows an [Iterable] view of the keys. | |
361 * | |
362 * Note that [lookup] is not supported for this set. | |
363 */ | |
364 class MapKeySet<E> extends _DelegatingIterableBase<E> | |
365 with UnmodifiableSetMixin<E> { | |
366 final Map<E, dynamic> _baseMap; | |
367 | |
368 MapKeySet(Map<E, dynamic> base) : _baseMap = base; | |
369 | |
370 Iterable<E> get _base => _baseMap.keys; | |
371 | |
372 bool contains(Object element) => _baseMap.containsKey(element); | |
373 | |
374 bool get isEmpty => _baseMap.isEmpty; | |
375 | |
376 bool get isNotEmpty => _baseMap.isNotEmpty; | |
377 | |
378 int get length => _baseMap.length; | |
379 | |
380 String toString() => "{${_base.join(', ')}}"; | |
381 | |
382 bool containsAll(Iterable<Object> other) => other.every(contains); | |
383 | |
384 /** | |
385 * Returns a new set with the the elements of [this] that are not in [other]. | |
386 * | |
387 * That is, the returned set contains all the elements of this [Set] that are | |
388 * not elements of [other] according to `other.contains`. | |
389 * | |
390 * Note that the returned set will use the default equality operation, which | |
391 * may be different than the equality operation [this] uses. | |
392 */ | |
393 Set<E> difference(Set<E> other) => | |
394 where((element) => !other.contains(element)).toSet(); | |
Lasse Reichstein Nielsen
2014/05/20 12:26:31
Maybe do:
toSet()..removeWhere(other.contains)
nweiz
2014/05/20 21:35:24
If only there were some way to extract the equalit
Lasse Reichstein Nielsen
2014/05/21 05:57:44
I thought about that, but you need more than just
nweiz
2014/05/21 18:00:55
At this point, even adding a getter to Map or Set
Lasse Reichstein Nielsen
2014/05/21 19:01:06
It's not impossible to add methods to existing cla
| |
395 | |
396 /** | |
397 * Returns a new set which is the intersection between [this] and [other]. | |
398 * | |
399 * That is, the returned set contains all the elements of this [Set] that are | |
400 * also elements of [other] according to `other.contains`. | |
401 * | |
402 * Note that the returned set will use the default equality operation, which | |
403 * may be different than the equality operation [this] uses. | |
404 */ | |
405 Set<E> intersection(Set<Object> other) => where(other.contains).toSet(); | |
406 | |
407 E lookup(E element) => throw new UnsupportedError( | |
Lasse Reichstein Nielsen
2014/05/20 12:26:31
Add documentation on lookup that says that it is u
nweiz
2014/05/20 21:35:24
Done.
| |
408 "MapKeySet doesn't support lookup()."); | |
409 | |
410 /** | |
411 * Returns a new set which contains all the elements of [this] and [other]. | |
412 * | |
413 * That is, the returned set contains all the elements of this [Set] and all | |
414 * the elements of [other]. | |
415 * | |
416 * Note that the returned set will use the default equality operation, which | |
417 * may be different than the equality operation [this] uses. | |
418 */ | |
419 Set<E> union(Set<E> other) => toSet()..addAll(other); | |
420 } | |
421 | |
422 /** | |
423 * Creates a modifiable [Set] view of the values of a [Map]. | |
424 * | |
425 * The `Set` view assumes that the keys of the `Map` can be uniquely determined | |
426 * from the values. The `keyForValue` function passed to the constructor finds | |
427 * the key for a single value. The `keyForValue` function should be consistent | |
428 * with equality. If `value1 == value2` then `keyForValue(value1)` and | |
429 * `keyForValue(value2)` should be considered equal keys by the underlying map, | |
430 * and vice versa. | |
431 * | |
432 * Modifying the set will modify the underlying map based on the key returned by | |
433 * `keyForValue` | |
Lasse Reichstein Nielsen
2014/05/20 12:26:31
End with '.'
nweiz
2014/05/20 21:35:24
Done.
| |
434 * | |
435 * If the `Map` contents are not compatible with the `keyForValue` function, the | |
436 * set will not work consistently, and may give meaningless responses or do | |
437 * inconsistent updates. | |
438 * | |
439 * This set can, for example, be used on a map from database record IDs to the | |
440 * records. It exposes the records as a set, and allows for writing both | |
441 * `recordSet.add(databaseRecord)` and `recordMap[id]`. | |
442 * | |
443 * Effectively, the map will act as a kind of index for the set. | |
444 */ | |
445 class MapValueSet<K, V> extends _DelegatingIterableBase<V> implements Set<V> { | |
446 final Map<K, V> _baseMap; | |
447 final Function _keyForValue; | |
448 | |
449 /** | |
450 * Creates a new [MapValueSet] based on [base]. | |
451 * | |
452 * [keyForValue] returns the key in the map that should be associated with the | |
453 * given value. The set's notion of equality is identical to the equality of | |
454 * the return values of [keyForValue]. | |
455 */ | |
456 MapValueSet(Map<K, V> base, K keyForValue(V value)) | |
457 : _baseMap = base, | |
458 _keyForValue = keyForValue; | |
459 | |
460 Iterable<V> get _base => _baseMap.values; | |
461 | |
462 bool contains(Object element) { | |
463 if (element != null && element is! V) return false; | |
Lasse Reichstein Nielsen
2014/05/20 12:26:31
Consider having an optional 'validKey' function as
nweiz
2014/05/20 21:35:24
I think I'll save that for a future addition.
| |
464 return _baseMap.containsKey(_keyForValue(element)); | |
465 } | |
466 | |
467 bool get isEmpty => _baseMap.isEmpty; | |
468 | |
469 bool get isNotEmpty => _baseMap.isNotEmpty; | |
470 | |
471 int get length => _baseMap.length; | |
472 | |
473 String toString() => toSet().toString(); | |
474 | |
475 bool add(V value) { | |
476 K key = _keyForValue(value); | |
477 bool result = false; | |
478 _baseMap.putIfAbsent(key, () { | |
479 result = true; | |
480 return value; | |
481 }); | |
482 return result; | |
483 } | |
484 | |
485 void addAll(Iterable<V> elements) => elements.forEach(add); | |
486 | |
487 void clear() => _baseMap.clear(); | |
488 | |
489 bool containsAll(Iterable<Object> other) => other.every(contains); | |
490 | |
491 /** | |
492 * Returns a new set with the the elements of [this] that are not in [other]. | |
493 * | |
494 * That is, the returned set contains all the elements of this [Set] that are | |
495 * not elements of [other] according to `other.contains`. | |
496 * | |
497 * Note that the returned set will use the default equality operation, which | |
498 * may be different than the equality operation [this] uses. | |
499 */ | |
500 Set<V> difference(Set<V> other) => | |
501 where((element) => !other.contains(element)).toSet(); | |
502 | |
503 /** | |
504 * Returns a new set which is the intersection between [this] and [other]. | |
505 * | |
506 * That is, the returned set contains all the elements of this [Set] that are | |
507 * also elements of [other] according to `other.contains`. | |
508 * | |
509 * Note that the returned set will use the default equality operation, which | |
510 * may be different than the equality operation [this] uses. | |
511 */ | |
512 Set<V> intersection(Set<Object> other) => where(other.contains).toSet(); | |
513 | |
514 V lookup(V element) => _baseMap[_keyForValue(element)]; | |
515 | |
516 bool remove(Object value) { | |
517 if (value != null && value is! V) return false; | |
518 var key = _keyForValue(value); | |
519 if (!_baseMap.containsKey(key)) return false; | |
520 _baseMap.remove(key); | |
521 return true; | |
522 } | |
523 | |
524 void removeAll(Iterable<Object> elements) => elements.forEach(remove); | |
525 | |
526 void removeWhere(bool test(V element)) { | |
527 var toRemove = []; | |
528 _baseMap.forEach((key, value) { | |
529 if (test(value)) toRemove.add(key); | |
530 }); | |
531 toRemove.forEach(_baseMap.remove); | |
532 } | |
533 | |
534 void retainAll(Iterable<Object> elements) { | |
535 var toRetain = elements.where((element) => element is V) | |
536 .map((element) => _keyForValue(element)).toSet(); | |
Lasse Reichstein Nielsen
2014/05/20 12:26:31
This still looks dangerous if toSet generates a se
nweiz
2014/05/20 21:35:24
Okay, I think I've come up with an implementation
| |
537 var toRemove = []; | |
538 _baseMap.forEach((key, value) { | |
539 if (!toRetain.contains(key)) toRemove.add(key); | |
540 }); | |
541 toRemove.forEach(_baseMap.remove); | |
542 } | |
543 | |
544 void retainWhere(bool test(V element)) => | |
545 removeWhere((element) => !test(element)); | |
546 | |
547 /** | |
548 * Returns a new set which contains all the elements of [this] and [other]. | |
549 * | |
550 * That is, the returned set contains all the elements of this [Set] and all | |
551 * the elements of [other]. | |
552 * | |
553 * Note that the returned set will use the default equality operation, which | |
554 * may be different than the equality operation [this] uses. | |
555 */ | |
556 Set<V> union(Set<V> other) => toSet()..addAll(other); | |
557 } | |
OLD | NEW |