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(); |
| 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 /** |
| 408 * Throws an [UnsupportedError] since there's no corresponding method for |
| 409 * [Map]s. |
| 410 */ |
| 411 E lookup(E element) => throw new UnsupportedError( |
| 412 "MapKeySet doesn't support lookup()."); |
| 413 |
| 414 /** |
| 415 * Returns a new set which contains all the elements of [this] and [other]. |
| 416 * |
| 417 * That is, the returned set contains all the elements of this [Set] and all |
| 418 * the elements of [other]. |
| 419 * |
| 420 * Note that the returned set will use the default equality operation, which |
| 421 * may be different than the equality operation [this] uses. |
| 422 */ |
| 423 Set<E> union(Set<E> other) => toSet()..addAll(other); |
| 424 } |
| 425 |
| 426 /** |
| 427 * Creates a modifiable [Set] view of the values of a [Map]. |
| 428 * |
| 429 * The `Set` view assumes that the keys of the `Map` can be uniquely determined |
| 430 * from the values. The `keyForValue` function passed to the constructor finds |
| 431 * the key for a single value. The `keyForValue` function should be consistent |
| 432 * with equality. If `value1 == value2` then `keyForValue(value1)` and |
| 433 * `keyForValue(value2)` should be considered equal keys by the underlying map, |
| 434 * and vice versa. |
| 435 * |
| 436 * Modifying the set will modify the underlying map based on the key returned by |
| 437 * `keyForValue`. |
| 438 * |
| 439 * If the `Map` contents are not compatible with the `keyForValue` function, the |
| 440 * set will not work consistently, and may give meaningless responses or do |
| 441 * inconsistent updates. |
| 442 * |
| 443 * This set can, for example, be used on a map from database record IDs to the |
| 444 * records. It exposes the records as a set, and allows for writing both |
| 445 * `recordSet.add(databaseRecord)` and `recordMap[id]`. |
| 446 * |
| 447 * Effectively, the map will act as a kind of index for the set. |
| 448 */ |
| 449 class MapValueSet<K, V> extends _DelegatingIterableBase<V> implements Set<V> { |
| 450 final Map<K, V> _baseMap; |
| 451 final Function _keyForValue; |
| 452 |
| 453 /** |
| 454 * Creates a new [MapValueSet] based on [base]. |
| 455 * |
| 456 * [keyForValue] returns the key in the map that should be associated with the |
| 457 * given value. The set's notion of equality is identical to the equality of |
| 458 * the return values of [keyForValue]. |
| 459 */ |
| 460 MapValueSet(Map<K, V> base, K keyForValue(V value)) |
| 461 : _baseMap = base, |
| 462 _keyForValue = keyForValue; |
| 463 |
| 464 Iterable<V> get _base => _baseMap.values; |
| 465 |
| 466 bool contains(Object element) { |
| 467 if (element != null && element is! V) return false; |
| 468 return _baseMap.containsKey(_keyForValue(element)); |
| 469 } |
| 470 |
| 471 bool get isEmpty => _baseMap.isEmpty; |
| 472 |
| 473 bool get isNotEmpty => _baseMap.isNotEmpty; |
| 474 |
| 475 int get length => _baseMap.length; |
| 476 |
| 477 String toString() => toSet().toString(); |
| 478 |
| 479 bool add(V value) { |
| 480 K key = _keyForValue(value); |
| 481 bool result = false; |
| 482 _baseMap.putIfAbsent(key, () { |
| 483 result = true; |
| 484 return value; |
| 485 }); |
| 486 return result; |
| 487 } |
| 488 |
| 489 void addAll(Iterable<V> elements) => elements.forEach(add); |
| 490 |
| 491 void clear() => _baseMap.clear(); |
| 492 |
| 493 bool containsAll(Iterable<Object> other) => other.every(contains); |
| 494 |
| 495 /** |
| 496 * Returns a new set with the the elements of [this] that are not in [other]. |
| 497 * |
| 498 * That is, the returned set contains all the elements of this [Set] that are |
| 499 * not elements of [other] according to `other.contains`. |
| 500 * |
| 501 * Note that the returned set will use the default equality operation, which |
| 502 * may be different than the equality operation [this] uses. |
| 503 */ |
| 504 Set<V> difference(Set<V> other) => |
| 505 where((element) => !other.contains(element)).toSet(); |
| 506 |
| 507 /** |
| 508 * Returns a new set which is the intersection between [this] and [other]. |
| 509 * |
| 510 * That is, the returned set contains all the elements of this [Set] that are |
| 511 * also elements of [other] according to `other.contains`. |
| 512 * |
| 513 * Note that the returned set will use the default equality operation, which |
| 514 * may be different than the equality operation [this] uses. |
| 515 */ |
| 516 Set<V> intersection(Set<Object> other) => where(other.contains).toSet(); |
| 517 |
| 518 V lookup(V element) => _baseMap[_keyForValue(element)]; |
| 519 |
| 520 bool remove(Object value) { |
| 521 if (value != null && value is! V) return false; |
| 522 var key = _keyForValue(value); |
| 523 if (!_baseMap.containsKey(key)) return false; |
| 524 _baseMap.remove(key); |
| 525 return true; |
| 526 } |
| 527 |
| 528 void removeAll(Iterable<Object> elements) => elements.forEach(remove); |
| 529 |
| 530 void removeWhere(bool test(V element)) { |
| 531 var toRemove = []; |
| 532 _baseMap.forEach((key, value) { |
| 533 if (test(value)) toRemove.add(key); |
| 534 }); |
| 535 toRemove.forEach(_baseMap.remove); |
| 536 } |
| 537 |
| 538 void retainAll(Iterable<Object> elements) { |
| 539 var valuesToRetain = new Set<V>.identity(); |
| 540 for (var element in elements) { |
| 541 if (element != null && element is! V) continue; |
| 542 var key = _keyForValue(element); |
| 543 if (!_baseMap.containsKey(key)) continue; |
| 544 valuesToRetain.add(_baseMap[key]); |
| 545 } |
| 546 |
| 547 var keysToRemove = []; |
| 548 _baseMap.forEach((k, v) { |
| 549 if (!valuesToRetain.contains(v)) keysToRemove.add(k); |
| 550 }); |
| 551 keysToRemove.forEach(_baseMap.remove); |
| 552 } |
| 553 |
| 554 void retainWhere(bool test(V element)) => |
| 555 removeWhere((element) => !test(element)); |
| 556 |
| 557 /** |
| 558 * Returns a new set which contains all the elements of [this] and [other]. |
| 559 * |
| 560 * That is, the returned set contains all the elements of this [Set] and all |
| 561 * the elements of [other]. |
| 562 * |
| 563 * Note that the returned set will use the default equality operation, which |
| 564 * may be different than the equality operation [this] uses. |
| 565 */ |
| 566 Set<V> union(Set<V> other) => toSet()..addAll(other); |
| 567 } |
OLD | NEW |