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

Unified Diff: sdk/lib/collection/collections.dart

Issue 11931034: Add methods to Collection. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Also on DoubleLinkedQueue. Created 7 years, 11 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 side-by-side diff with in-line comments
Download patch
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();
}

Powered by Google App Engine
This is Rietveld 408576698