OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.collection; | 5 part of dart.collection; |
6 | 6 |
7 /** | 7 /** |
8 * This class provides default implementations for Iterables (including Lists). | 8 * This class provides default implementations for Iterables (including Lists). |
9 * | 9 * |
10 * Once Dart receives Mixins it will be replaced with mixin classes. | 10 * Once Dart receives Mixins it will be replaced with mixin classes. |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 static dynamic reduce(Iterable iterable, | 40 static dynamic reduce(Iterable iterable, |
41 dynamic initialValue, | 41 dynamic initialValue, |
42 dynamic combine(dynamic previousValue, element)) { | 42 dynamic combine(dynamic previousValue, element)) { |
43 for (final element in iterable) { | 43 for (final element in iterable) { |
44 initialValue = combine(initialValue, element); | 44 initialValue = combine(initialValue, element); |
45 } | 45 } |
46 return initialValue; | 46 return initialValue; |
47 } | 47 } |
48 | 48 |
| 49 /** |
| 50 * Simple implementation for [Collection.removeAll]. |
| 51 * |
| 52 * This implementation assumes that [Collection.remove] on [collection] |
| 53 * is efficient. The [:remove:] method on [List] objects is typically |
| 54 * not efficient since it requires linear search to find an element. |
| 55 */ |
| 56 static void removeAll(Collection collection, Iterable elementsToRemove) { |
| 57 for (Object object in elementsToRemove) { |
| 58 collection.remove(object); |
| 59 } |
| 60 } |
| 61 |
| 62 /** |
| 63 * Implementation of [Collection.removeAll] for lists. |
| 64 * |
| 65 * This implementation assumes that [Collection.remove] is not efficient |
| 66 * (as it usually isn't on a [List]) and uses [Collection.removeMathcing] |
| 67 * instead of just repeatedly calling remove. |
| 68 */ |
| 69 static void removeAllList(Collection collection, Iterable elementsToRemove) { |
| 70 Set setToRemove; |
| 71 // Assume contains is efficient on a Set. |
| 72 if (elementsToRemove is Set) { |
| 73 setToRemove = elementsToRemove; |
| 74 } else { |
| 75 setToRemove = elementsToRemove.toSet(); |
| 76 } |
| 77 collection.removeMatching(setToRemve.contains); |
| 78 } |
| 79 |
| 80 /** |
| 81 * Simple implemenation for [Collection.retainAll]. |
| 82 * |
| 83 * This implementation assumes that [Collecton.retainMatching] on [collection] |
| 84 * is efficient. |
| 85 */ |
| 86 static void retainAll(Collection collection, Iterable elementsToRetain) { |
| 87 Set lookup; |
| 88 if (elementsToRetain is Set) { |
| 89 lookup = elementsToRetain; |
| 90 } else { |
| 91 lookup = elementsToRetain.toSet(); |
| 92 } |
| 93 collection.retainMatching(lookup.contains); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Simple implemenation for [Collection.removeMatching]. |
| 98 * |
| 99 * This implementation assumes that [Collecton.removeAll] on [collection] is |
| 100 * efficient. |
| 101 */ |
| 102 static void removeMatching(Collection collection, bool test(var element)) { |
| 103 List elementsToRemove = []; |
| 104 for (var element in collection) { |
| 105 if (test(element)) elementsToRemove.add(element); |
| 106 } |
| 107 collection.removeAll(elementsToRemove); |
| 108 } |
| 109 |
| 110 /** |
| 111 * Simple implemenation for [Collection.retainMatching]. |
| 112 * |
| 113 * This implementation assumes that [Collecton.removeAll] on [collection] is |
| 114 * efficient. |
| 115 */ |
| 116 static void retainMatching(Collection collection, bool test(var element)) { |
| 117 List elementsToRemove = []; |
| 118 for (var element in collection) { |
| 119 if (!test(element)) elementsToRemove.add(element); |
| 120 } |
| 121 collection.removeAll(elementsToRemove); |
| 122 } |
49 static bool isEmpty(Iterable iterable) { | 123 static bool isEmpty(Iterable iterable) { |
50 return !iterable.iterator.moveNext(); | 124 return !iterable.iterator.moveNext(); |
51 } | 125 } |
52 | 126 |
53 static dynamic first(Iterable iterable) { | 127 static dynamic first(Iterable iterable) { |
54 Iterator it = iterable.iterator; | 128 Iterator it = iterable.iterator; |
55 if (!it.moveNext()) { | 129 if (!it.moveNext()) { |
56 throw new StateError("No elements"); | 130 throw new StateError("No elements"); |
57 } | 131 } |
58 return it.current; | 132 return it.current; |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 * Returns true if the specified collection contains the specified object | 511 * Returns true if the specified collection contains the specified object |
438 * reference. | 512 * reference. |
439 */ | 513 */ |
440 static _containsRef(Collection c, Object ref) { | 514 static _containsRef(Collection c, Object ref) { |
441 for (var e in c) { | 515 for (var e in c) { |
442 if (identical(e, ref)) return true; | 516 if (identical(e, ref)) return true; |
443 } | 517 } |
444 return false; | 518 return false; |
445 } | 519 } |
446 } | 520 } |
OLD | NEW |