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

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

Issue 12049065: Fix bugs in GrowableList.remove*. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comment. Fix typo in filtered list. 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 0b3936491ebfd1e31830f815a107bfbf711c5a72..a8e89cc1f3de68dfbb2dce3b9df1d5a2b8ec478a 100644
--- a/sdk/lib/collection/collections.dart
+++ b/sdk/lib/collection/collections.dart
@@ -68,7 +68,7 @@ class IterableMixinWorkaround {
*/
static void removeAllList(Collection collection, Iterable elementsToRemove) {
Set setToRemove;
- // Assume contains is efficient on a Set.
+ // Assume [contains] is efficient on a Set.
if (elementsToRemove is Set) {
setToRemove = elementsToRemove;
} else {
@@ -90,6 +90,10 @@ class IterableMixinWorkaround {
} else {
lookup = elementsToRetain.toSet();
}
+ if (lookup.isEmpty) {
+ collection.clear();
+ return;
+ }
collection.retainMatching(lookup.contains);
}
@@ -108,6 +112,32 @@ class IterableMixinWorkaround {
}
/**
+ * Removes elements matching [test] from [list].
+ *
+ * This is performed in two steps, to avoid exposing an inconsistent state
+ * to the [test] function. First the elements to ratain are found, and then
+ * the original list is updated to contain those elements.
+ */
+ static void removeMatchingList(List list, bool test(var element)) {
+ List retained = [];
+ int length = list.length;
+ for (int i = 0; i < length; i++) {
+ var element = list[i];
+ if (!test(element)) {
+ retained.add(element);
+ }
+ if (length != list.length) {
+ throw new ConcurrentModificationError(list);
+ }
+ }
+ if (retained.length == length) return;
+ for (int i = 0; i < retained.length; i++) {
+ list[i] = retained[i];
+ }
+ list.length = retained.length;
+ }
+
+ /**
* Simple implemenation for [Collection.retainMatching].
*
* This implementation assumes that [Collecton.removeAll] on [collection] is
@@ -120,6 +150,7 @@ class IterableMixinWorkaround {
}
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