Index: sdk/lib/collection/hash_set.dart |
diff --git a/sdk/lib/collection/hash_set.dart b/sdk/lib/collection/hash_set.dart |
index 977dcfff5bec0b6a5f12ae072933053af03be6c6..04516fc02d7a759ebd3fca97ca8092b70c02cde2 100644 |
--- a/sdk/lib/collection/hash_set.dart |
+++ b/sdk/lib/collection/hash_set.dart |
@@ -4,7 +4,57 @@ |
part of dart.collection; |
-class HashSet<E> extends Collection<E> implements Set<E> { |
+/** Common parts of [HashSet] and [LinkedHashSet] implementations. */ |
+abstract class _HashSetBase<E> extends Collection<E> implements Set<E> { |
+ // Set. |
+ bool isSubsetOf(Collection<E> other) { |
+ // Deprecated, and using old signature. |
+ Set otherSet; |
+ if (other is Set) { |
+ otherSet = other; |
+ } else { |
+ otherSet = other.toSet(); |
+ } |
+ return otherSet.containsAll(this); |
+ } |
+ |
+ bool containsAll(Iterable<E> other) { |
+ for (E object in other) { |
+ if (!this.contains(object)) return false; |
+ } |
+ return true; |
+ } |
+ |
+ Set<E> intersection(Set<E> other) { |
+ Set<E> result = _newSet(); |
+ if (other.length < this.length) { |
+ for (E element in other) { |
+ if (this.contains(element)) result.add(element); |
+ } |
+ } else { |
+ for (E element in this) { |
+ if (other.contains(element)) result.add(element); |
+ } |
+ } |
+ return result; |
+ } |
+ |
+ Set<E> union(Set<E> other) { |
+ return _newSet()..addAll(this)..addAll(other); |
+ } |
+ |
+ Set<E> difference(Set<E> other) { |
+ HashSet<E> result = _newSet(); |
+ for (E element in this) { |
+ if (!other.contains(element)) result.add(element); |
+ } |
+ return result; |
+ } |
+ |
+ String toString() => Collections.collectionToString(this); |
+} |
+ |
+class HashSet<E> extends _HashSetBase<E> { |
external HashSet(); |
factory HashSet.from(Iterable<E> iterable) { |
@@ -30,7 +80,13 @@ class HashSet<E> extends Collection<E> implements Set<E> { |
external void removeAll(Iterable objectsToRemove); |
void retainAll(Iterable objectsToRetain) { |
- IterableMixinWorkaround.retainAll(this, objectsToRetain); |
+ Set retainSet; |
+ if (objectsToRetain is Set) { |
+ retainSet = objectsToRetain; |
+ } else { |
+ retainSet = objectsToRetain.toSet(); |
+ } |
+ retainWhere(retainSet.contains); |
} |
external void removeWhere(bool test(E element)); |
@@ -40,33 +96,5 @@ class HashSet<E> extends Collection<E> implements Set<E> { |
external void clear(); |
// Set. |
- bool isSubsetOf(Collection<E> other) { |
- // Deprecated, and using old signature. |
- Set otherSet; |
- if (other is Set) { |
- otherSet = other; |
- } else { |
- otherSet = other.toSet(); |
- } |
- return IterableMixinWorkaround.setContainsAll(otherSet, this); |
- } |
- |
- bool containsAll(Iterable<E> other) { |
- return IterableMixinWorkaround.setContainsAll(this, other); |
- } |
- |
- Set<E> intersection(Set<E> other) { |
- return IterableMixinWorkaround.setIntersection( |
- this, other, new HashSet<E>()); |
- } |
- |
- Set<E> union(Set<E> other) { |
- return IterableMixinWorkaround.setUnion(this, other, new HashSet<E>()); |
- } |
- |
- Set<E> difference(Set<E> other) { |
- return IterableMixinWorkaround.setDifference(this, other, new HashSet<E>()); |
- } |
- |
- String toString() => Collections.collectionToString(this); |
+ Set<E> _newSet() => new HashSet<E>(); |
} |