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

Unified Diff: sdk/lib/core/set.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/core/set.dart
diff --git a/sdk/lib/core/set.dart b/sdk/lib/core/set.dart
index 95a633a447ca6b9c5165e0475d6cb8e9145e5948..b4f34db50e769ef34495e9556fb08aef6dfd421a 100644
--- a/sdk/lib/core/set.dart
+++ b/sdk/lib/core/set.dart
@@ -32,7 +32,7 @@ abstract class Set<E> extends Collection<E> {
* in the set. Returns false otherwise. The method has no effect
* if [value] value was not in the set.
*/
- bool remove(E value);
+ bool remove(Object value);
floitsch 2013/01/17 13:36:58 make it "var" ?
Lasse Reichstein Nielsen 2013/01/18 11:41:48 Object is correct here. We only use ==, possibly h
/**
* Adds all the elements of the given [iterable] to the set.
@@ -40,11 +40,6 @@ abstract class Set<E> extends Collection<E> {
void addAll(Iterable<E> iterable);
/**
- * Removes all the elements of the given collection from the set.
- */
- void removeAll(Iterable<E> iterable);
-
- /**
* Returns true if [collection] contains all the elements of this
* collection.
*/
@@ -66,9 +61,9 @@ abstract class Set<E> extends Collection<E> {
* Removes all elements in the set.
*/
void clear();
-
}
+
abstract class HashSet<E> extends Set<E> {
factory HashSet() => new _HashSetImpl<E>();
@@ -105,12 +100,42 @@ class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> {
return _backingMap.containsKey(value);
}
- bool remove(E value) {
+ bool remove(Object value) {
if (!_backingMap.containsKey(value)) return false;
_backingMap.remove(value);
return true;
}
+ void removeAll(Iterable elements) {
floitsch 2013/01/17 13:36:58 should not be necessary if Collection provides def
Lasse Reichstein Nielsen 2013/01/18 11:41:48 Done.
+ for (var element in elements) {
+ remove(element);
+ }
+ }
+
+ void retainAll(Iterable elements) {
+ Set other;
+ if (elements is Set) {
+ other = elements
+ } else {
+ other = new Set.from(elements);
+ }
+ removeMatching((E e) => !set.contains(e));
+ }
+
+ void removeMatching(bool test(E element)) {
+ List<E> toRemove = <E>[];
+ // TODO(lrn): Consider making HashSet able to handle remove
+ // during iteration.
+ for (E element in this) {
+ if (test(element)) {
+ toRemove.add(element);
+ }
+ }
+ for (E element in toRemove) {
+ remove(element);
+ }
+ }
+
void addAll(Iterable<E> iterable) {
for (E element in iterable) {
add(element);
@@ -129,12 +154,6 @@ class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> {
return new Set<E>.from(other).containsAll(this);
}
- void removeAll(Iterable<E> iterable) {
- for (E value in iterable) {
- remove(value);
- }
- }
-
bool containsAll(Collection<E> collection) {
return collection.every((E value) {
return contains(value);

Powered by Google App Engine
This is Rietveld 408576698