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

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: Address comments. 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
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/js_array.dart ('k') | sdk/lib/core/collection.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/collection/collections.dart
diff --git a/sdk/lib/collection/collections.dart b/sdk/lib/collection/collections.dart
index 95b6ddac4320a92c0a50eef3fe36e5dcda9907c5..d8fff38be0a4fba05de531ae8059a681205de10e 100644
--- a/sdk/lib/collection/collections.dart
+++ b/sdk/lib/collection/collections.dart
@@ -46,6 +46,80 @@ class IterableMixinWorkaround {
return initialValue;
}
+ /**
+ * Simple implementation for [Collection.removeAll].
+ *
+ * This implementation assumes that [Collection.remove] on [collection]
+ * is efficient. The [:remove:] method on [List] objects is typically
+ * not efficient since it requires linear search to find an element.
+ */
+ static void removeAll(Collection collection, Iterable elementsToRemove) {
+ for (Object object in elementsToRemove) {
+ collection.remove(object);
+ }
+ }
+
+ /**
+ * Implementation of [Collection.removeAll] for lists.
+ *
+ * This implementation assumes that [Collection.remove] is not efficient
+ * (as it usually isn't on a [List]) and uses [Collection.removeMathcing]
+ * instead of just repeatedly calling remove.
+ */
+ static void removeAllList(Collection collection, Iterable elementsToRemove) {
+ Set setToRemove;
+ // Assume contains is efficient on a Set.
+ if (elementsToRemove is Set) {
+ setToRemove = elementsToRemove;
+ } else {
+ setToRemove = elementsToRemove.toSet();
+ }
+ collection.removeMatching(setToRemve.contains);
+ }
+
+ /**
+ * 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();
}
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/lib/js_array.dart ('k') | sdk/lib/core/collection.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698