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 * Creates an unmodifiable [Set] that delegates all operations to a base map's | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
How about:
An unmodifiable [Set] view of the key
nweiz
2014/05/19 20:58:48
Done.
| |
355 * keys. | |
356 * | |
357 * This class can be used to make a `Map` look like a `Set`. Note that [lookup] | |
358 * is not `O(1)` for this set. | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
Or say that it is unsupported, if we can't find a
floitsch
2014/05/09 09:49:41
I think it's ok to have the slow method.
Lasse Reichstein Nielsen
2014/05/09 10:50:48
It's not that it's slow, but that it's wrong.
Test
Lasse Reichstein Nielsen
2014/05/09 10:52:52
Should ofcourse be:
var m = new LinkedHashMap.id
nweiz
2014/05/19 20:58:48
Marked as unsupported.
| |
359 */ | |
360 class MapKeySet<E> extends _DelegatingIterableBase<E> | |
361 with UnmodifiableSetMixin<E> { | |
362 final Map<E, dynamic> _baseMap; | |
363 | |
364 Iterable<E> get _base => _baseMap.keys; | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
Move getter below constructor.
nweiz
2014/05/19 20:58:48
Done.
| |
365 | |
366 MapKeySet(Map<E, dynamic> base) : _baseMap = base; | |
367 | |
368 bool contains(Object element) => _baseMap.containsKey(element); | |
369 | |
370 bool get isEmpty => _baseMap.isEmpty; | |
371 | |
372 bool get isNotEmpty => _baseMap.isNotEmpty; | |
373 | |
374 int get length => _baseMap.length; | |
375 | |
376 String toString() => toSet().toString(); | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
The toSet operation may lose elements if the base
floitsch
2014/05/09 09:49:41
Why not simply return "{${_base.join(', ')}}"; ?
Lasse Reichstein Nielsen
2014/05/09 10:50:48
Yes, I think I mistakenly thought that _baseMap.ke
nweiz
2014/05/19 20:58:48
Done.
| |
377 | |
378 bool containsAll(Iterable<Object> other) => other.every(contains); | |
379 | |
380 Set<E> difference(Set<E> other) => | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
It should be commented that the set returned here
nweiz
2014/05/19 20:58:48
Done.
| |
381 where((element) => !other.contains(element)).toSet(); | |
382 | |
383 Set<E> intersection(Set<Object> other) => | |
384 where((element) => other.contains(element)).toSet(); | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
"(element) => other.contains(element)" can be just
nweiz
2014/05/19 20:58:48
Done.
| |
385 | |
386 E lookup(E element) => firstWhere((key) => element == key, | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
Using == here might be incompatible with the actua
floitsch
2014/05/09 09:49:41
I think that's too unexpected.
We should probably
nweiz
2014/05/19 20:58:48
I'm happy to do whichever you two agree on. For no
| |
387 orElse: () => null); | |
388 | |
389 Set<E> union(Set<E> other) => toSet()..addAll(other); | |
390 } | |
391 | |
392 /** | |
393 * Creates a [Set] that delegates all operations to a base map's values. | |
394 * | |
395 * This class can be used to make a `Map` whose keys are determinable from its | |
396 * values look like a `Set`. | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
How about:
----
Creates a modifiable [Set] view o
nweiz
2014/05/19 20:58:48
Done.
| |
397 * | |
398 * For example, this might be used to expose a map from IDs to database records | |
399 * as a Set. The `keyForValue` function would be `(record) => record.id`, and | |
400 * this would allow users to write both `recordSet.add(databaseRecord)` and | |
401 * `recordMap[id]`. | |
402 */ | |
403 class MapValueSet<K, V> extends _DelegatingIterableBase<V> implements Set<V> { | |
404 final Map<K, V> _baseMap; | |
405 final Function _keyForValue; | |
406 | |
407 Iterable<V> get _base => _baseMap.values; | |
floitsch
2014/05/09 09:49:41
getter below constructor.
nweiz
2014/05/19 20:58:48
Done.
| |
408 | |
409 /** | |
410 * Creates a new [MapValueSet] based on [base]. | |
411 * | |
412 * [keyForValue] returns the key in the map that should be associated with the | |
413 * given value. The set's notion of equality is identical to the equality of | |
414 * the return values of [keyForValue]. | |
415 */ | |
416 MapValueSet(Map<K, V> base, K keyForValue(V value)) | |
417 : _baseMap = base, | |
418 _keyForValue = keyForValue; | |
419 | |
420 bool contains(Object element) { | |
421 if (element is! V) return false; | |
422 return _baseMap.containsKey(_keyForValue(element)); | |
423 } | |
424 | |
425 bool get isEmpty => _baseMap.isEmpty; | |
426 | |
427 bool get isNotEmpty => _baseMap.isNotEmpty; | |
428 | |
429 int get length => _baseMap.length; | |
430 | |
431 String toString() => toSet().toString(); | |
432 | |
433 bool add(V value) { | |
434 var key = _keyForValue(value); | |
floitsch
2014/05/09 09:49:41
K key = _keyForValue...
nweiz
2014/05/19 20:58:48
Done, although this is contrary to the style guide
| |
435 if (_baseMap.containsKey(key)) return false; | |
436 _baseMap[key] = value; | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
Maybe use:
bool result = false;
_baseMap.putIf
nweiz
2014/05/19 20:58:48
Done.
| |
437 return true; | |
438 } | |
439 | |
440 void addAll(Iterable<V> elements) => elements.forEach(add); | |
441 | |
442 void clear() => _baseMap.clear(); | |
443 | |
444 bool containsAll(Iterable<Object> other) => other.every(contains); | |
445 | |
446 Set<V> difference(Set<V> other) => | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
Document that the returned set is not of the same
nweiz
2014/05/19 20:58:48
Done.
| |
447 where((element) => !other.contains(element)).toSet(); | |
448 | |
449 Set<V> intersection(Set<Object> other) => | |
450 where((element) => other.contains(element)).toSet(); | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
where(other.contains).toSet()
nweiz
2014/05/19 20:58:48
Done.
| |
451 | |
452 V lookup(V element) => _baseMap[_keyForValue(element)]; | |
453 | |
454 bool remove(Object value) { | |
455 if (value is! V) return false; | |
456 var key = _keyForValue(value); | |
457 if (!_baseMap.containsKey(key)) return false; | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
return _baseMap.remove(key) != null;
Since null v
nweiz
2014/05/19 20:58:48
I don't think we should necessarily disallow null.
| |
458 _baseMap.remove(key); | |
459 return true; | |
460 } | |
461 | |
462 void removeAll(Iterable<Object> elements) => elements.forEach(remove); | |
463 | |
464 void removeWhere(bool test(V element)) { | |
465 new Map.from(_baseMap).forEach((key, value) { | |
466 if (test(value)) remove(key); | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
-> _baseMap.remove(key)
The new map may not agree
nweiz
2014/05/19 20:58:48
Done.
| |
467 }); | |
468 } | |
469 | |
470 void retainAll(Iterable<Object> elements) { | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
I *so* regret keeping retainAll in the Set interfa
| |
471 var retainKeys = elements.where((element) => element is V) | |
472 .map((element) => _keyForValue(element)).toSet(); | |
Lasse Reichstein Nielsen
2014/05/09 09:00:17
There is a problem with equality here too. The equ
nweiz
2014/05/19 20:58:48
According to the retainAll documentation, element
| |
473 new Map.from(_baseMap).forEach((key, value) { | |
474 if (!retainKeys.contains(key)) remove(key); | |
475 }); | |
476 } | |
477 | |
478 void retainWhere(bool test(V element)) => | |
479 removeWhere((element) => !test(element)); | |
480 | |
481 Set<V> union(Set<V> other) => toSet()..addAll(other); | |
482 } | |
OLD | NEW |