Chromium Code Reviews| 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 * The [Collections] class implements static methods useful when | 8 * The [Collections] class implements static methods useful when |
| 9 * writing a class that implements [Collection] and the [iterator] | 9 * writing a class that implements [Collection] and the [iterator] |
| 10 * method. | 10 * method. |
| (...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, which it isn't for, e.g., [List]s. | |
|
floitsch
2013/01/17 13:36:58
too much "." and "," and latin.
Lasse Reichstein Nielsen
2013/01/18 11:41:48
Done.
| |
| 54 */ | |
| 55 static void removeAll(Collection collection, Iterable elementsToRemove) { | |
| 56 for (Object object in elementsToRemove) { | |
| 57 collection.remove(object); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * Simple implemenation for [Collection.retainAll]. | |
| 63 * | |
| 64 * This implementation assumes that [Collecton.retainMatching] on [collection] | |
| 65 * is efficient. | |
| 66 */ | |
| 67 static void retainAll(Collection collection, Iterable elementsToRetain) { | |
| 68 Set lookup; | |
| 69 if (elementsToRetain is Set) { | |
| 70 lookup = elementsToRetain; | |
| 71 } else { | |
| 72 lookup = elementsToRetain.toSet(); | |
| 73 } | |
| 74 collection.retainMatching(lookup.contains) | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Simple implemenation for [Collection.removeMatching]. | |
| 79 * | |
| 80 * This implementation assumes that [Collecton.removeAll] on [collection] is | |
| 81 * efficient. | |
| 82 */ | |
| 83 static void removeMatching(Collection collection, bool test(var element)) { | |
| 84 List elementsToRemove = []; | |
| 85 for (var element in collection) { | |
| 86 if (test(element)) elementsToRemove.add(element); | |
| 87 } | |
| 88 collection.removeAll(elementsToRemove); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Simple implemenation for [Collection.retainMatching]. | |
| 93 * | |
| 94 * This implementation assumes that [Collecton.removeAll] on [collection] is | |
| 95 * efficient. | |
| 96 */ | |
| 97 static void retainMatching(Collection collection, bool test(var element)) { | |
| 98 List elementsToRemove = []; | |
| 99 for (var element in collection) { | |
| 100 if (!test(element)) elementsToRemove.add(element); | |
| 101 } | |
| 102 collection.removeAll(elementsToRemove); | |
| 103 } | |
| 49 static bool isEmpty(Iterable iterable) { | 104 static bool isEmpty(Iterable iterable) { |
| 50 return !iterable.iterator.moveNext(); | 105 return !iterable.iterator.moveNext(); |
| 51 } | 106 } |
| 52 | 107 |
| 53 static dynamic first(Iterable iterable) { | 108 static dynamic first(Iterable iterable) { |
| 54 Iterator it = iterable.iterator; | 109 Iterator it = iterable.iterator; |
| 55 if (!it.moveNext()) { | 110 if (!it.moveNext()) { |
| 56 throw new StateError("No elements"); | 111 throw new StateError("No elements"); |
| 57 } | 112 } |
| 58 return it.current; | 113 return it.current; |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 * Returns true if the specified collection contains the specified object | 352 * Returns true if the specified collection contains the specified object |
| 298 * reference. | 353 * reference. |
| 299 */ | 354 */ |
| 300 static _containsRef(Collection c, Object ref) { | 355 static _containsRef(Collection c, Object ref) { |
| 301 for (var e in c) { | 356 for (var e in c) { |
| 302 if (identical(e, ref)) return true; | 357 if (identical(e, ref)) return true; |
| 303 } | 358 } |
| 304 return false; | 359 return false; |
| 305 } | 360 } |
| 306 } | 361 } |
| OLD | NEW |