Chromium Code Reviews| Index: sdk/lib/collection/collections.dart |
| diff --git a/sdk/lib/collection/collections.dart b/sdk/lib/collection/collections.dart |
| index 4bd1fadb513ce5e84a36fbb51ae347e2db93af69..dafc8a3129801f526fe7f6b2ddb432ab5f0da3ae 100644 |
| --- a/sdk/lib/collection/collections.dart |
| +++ b/sdk/lib/collection/collections.dart |
| @@ -46,6 +46,61 @@ class Collections { |
| return initialValue; |
| } |
| + /** |
| + * Simple implementation for [Collection.removeAll]. |
| + * |
| + * This implementation assumes that [Collection.remove] on [collection] |
| + * 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.
|
| + */ |
| + static void removeAll(Collection collection, Iterable elementsToRemove) { |
| + for (Object object in elementsToRemove) { |
| + collection.remove(object); |
| + } |
| + } |
| + |
| + /** |
| + * Simple implemenation for [Collection.retainAll]. |
| + * |
| + * This implementation assumes that [Collecton.retainMatching] on [collection] |
| + * is efficient. |
| + */ |
| + static void retainAll(Collection collection, Iterable elementsToRetain) { |
| + Set lookup; |
| + if (elementsToRetain is Set) { |
| + lookup = elementsToRetain; |
| + } else { |
| + lookup = elementsToRetain.toSet(); |
| + } |
| + collection.retainMatching(lookup.contains) |
| + } |
| + |
| + /** |
| + * Simple implemenation for [Collection.removeMatching]. |
| + * |
| + * This implementation assumes that [Collecton.removeAll] on [collection] is |
| + * efficient. |
| + */ |
| + static void removeMatching(Collection collection, bool test(var element)) { |
| + List elementsToRemove = []; |
| + for (var element in collection) { |
| + if (test(element)) elementsToRemove.add(element); |
| + } |
| + collection.removeAll(elementsToRemove); |
| + } |
| + |
| + /** |
| + * Simple implemenation for [Collection.retainMatching]. |
| + * |
| + * This implementation assumes that [Collecton.removeAll] on [collection] is |
| + * efficient. |
| + */ |
| + static void retainMatching(Collection collection, bool test(var element)) { |
| + List elementsToRemove = []; |
| + for (var element in collection) { |
| + if (!test(element)) elementsToRemove.add(element); |
| + } |
| + collection.removeAll(elementsToRemove); |
| + } |
| static bool isEmpty(Iterable iterable) { |
| return !iterable.iterator.moveNext(); |
| } |