Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: pkg/collection/lib/wrappers.dart

Issue 277563007: Add MapKeySet and MapValueSet to the collection package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/collection/CHANGELOG.md ('k') | pkg/collection/pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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.
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;
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();
377
378 bool containsAll(Iterable<Object> other) => other.every(contains);
379
380 Set<E> difference(Set<E> other) =>
381 where((element) => !other.contains(element)).toSet();
382
383 Set<E> intersection(Set<Object> other) =>
384 where((element) => other.contains(element)).toSet();
385
386 E lookup(E element) => firstWhere((key) => element == key,
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
kevmoo 2014/05/08 20:40:15 I'm a bit confused by this sentence. What's the us
nweiz 2014/05/08 21:14:56 See my comment on the issue: https://code.google.c
kevmoo 2014/05/08 23:01:53 Your comment makes the usage clear, but the docs h
nweiz 2014/05/09 02:48:50 Done.
396 * values look like a `Set`.
397 */
398 class MapValueSet<K, V> extends _DelegatingIterableBase<V> implements Set<V> {
399 final Map<K, V> _baseMap;
400 final Function _keyForValue;
401
402 Iterable<V> get _base => _baseMap.values;
403
404 /**
405 * Creates a new [MapValueSet] based on [base].
406 *
407 * [keyForValue] returns the key in the map that should be associated with the
408 * given value. The set's notion of equality is identical to the equality of
409 * the return values of [keyForValue].
410 */
411 MapValueSet(Map<K, V> base, K keyForValue(V value))
412 : _baseMap = base,
413 _keyForValue = keyForValue;
414
415 bool contains(Object element) {
416 if (element is! V) return false;
417 return _baseMap.containsKey(_keyForValue(element));
418 }
419
420 bool get isEmpty => _baseMap.isEmpty;
421
422 bool get isNotEmpty => _baseMap.isNotEmpty;
423
424 int get length => _baseMap.length;
425
426 String toString() => toSet().toString();
427
428 bool add(V value) {
429 var key = _keyForValue(value);
430 if (_baseMap.containsKey(key)) return false;
431 _baseMap[key] = value;
432 return true;
433 }
434
435 void addAll(Iterable<V> elements) => elements.forEach(add);
436
437 void clear() => _baseMap.clear();
438
439 bool containsAll(Iterable<Object> other) => other.every(contains);
440
441 Set<V> difference(Set<V> other) =>
442 where((element) => !other.contains(element)).toSet();
443
444 Set<V> intersection(Set<Object> other) =>
445 where((element) => other.contains(element)).toSet();
446
447 V lookup(V element) => _baseMap[_keyForValue(element)];
448
449 bool remove(Object value) {
450 if (value is! V) return false;
451 var key = _keyForValue(value);
452 if (!_baseMap.containsKey(key)) return false;
453 _baseMap.remove(key);
454 return true;
455 }
456
457 void removeAll(Iterable<Object> elements) => elements.forEach(remove);
458
459 void removeWhere(bool test(V element)) {
460 new Map.from(_baseMap).forEach((key, value) {
461 if (test(value)) remove(key);
462 });
463 }
464
465 void retainAll(Iterable<Object> elements) {
466 var retainKeys = elements.where((element) => element is V)
467 .map((element) => _keyForValue(element)).toSet();
468 new Map.from(_baseMap).forEach((key, value) {
469 if (!retainKeys.contains(key)) remove(key);
470 });
471 }
472
473 void retainWhere(bool test(V element)) =>
474 removeWhere((element) => !test(element));
475
476 Set<V> union(Set<V> other) => toSet()..addAll(other);
477 }
OLDNEW
« no previous file with comments | « pkg/collection/CHANGELOG.md ('k') | pkg/collection/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698