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

Unified Diff: sdk/lib/core/set.dart

Issue 11644072: Add ordered set. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Created 8 years 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 | « no previous file | tests/corelib/ordered_set_iterator_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/set.dart
diff --git a/sdk/lib/core/set.dart b/sdk/lib/core/set.dart
index 95a633a447ca6b9c5165e0475d6cb8e9145e5948..33e65b9851d68da3f85c1fe6deda95c3b5868a7f 100644
--- a/sdk/lib/core/set.dart
+++ b/sdk/lib/core/set.dart
@@ -78,21 +78,16 @@ abstract class HashSet<E> extends Set<E> {
factory HashSet.from(Iterable<E> other) => new _HashSetImpl<E>.from(other);
}
+abstract class _MapBackedSet<E> extends Iterable<E> implements HashSet<E> {
Lasse Reichstein Nielsen 2013/01/04 09:39:06 Why implement HashSet? Being a HashSet is an imple
+ _MapBackedSet(this._backingMap);
-class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> {
-
- _HashSetImpl() {
- _backingMap = new _HashMapImpl<E, E>();
- }
-
- factory _HashSetImpl.from(Iterable<E> other) {
- Set<E> set = new _HashSetImpl<E>();
- for (final e in other) {
- set.add(e);
+ _MapBackedSet.from(this._backingMap, Iterable<E> iterable) {
+ for (final e in iterable) {
+ add(e);
}
- return set;
}
+
void clear() {
_backingMap.clear();
}
@@ -155,7 +150,7 @@ class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> {
return _backingMap.length;
}
- Iterator<E> get iterator => new _HashSetIterator<E>(this);
+ Iterator<E> get iterator => _backingMap.keys.iterator;
String toString() {
return Collections.collectionToString(this);
@@ -164,32 +159,19 @@ class _HashSetImpl<E> extends Iterable<E> implements HashSet<E> {
// The map backing this set. The associations in this map are all
// of the form element -> element. If a value is not in the map,
// then it is not in the set.
- _HashMapImpl<E, E> _backingMap;
+ Map<E, E> _backingMap;
}
-class _HashSetIterator<E> implements Iterator<E> {
-
- _HashSetIterator(_HashSetImpl<E> set)
- : _keysIterator = set._backingMap._keys.iterator;
+class _HashSetImpl<E> extends _MapBackedSet<E> implements HashSet<E> {
+ _HashSetImpl.from(Iterable<E> iterable)
+ : super.from(new _HashMapImpl<E, E>(), iterable);
- E get current {
- var result = _keysIterator.current;
- if (identical(result, _HashMapImpl._DELETED_KEY)) {
- // TODO(floitsch): improve the error reporting.
- throw new StateError("Concurrent modification.");
- }
- return result;
- }
+ _HashSetImpl() : super(new _HashMapImpl<E, E>());
+}
- bool moveNext() {
- bool result;
- do {
- result = _keysIterator.moveNext();
- } while (result &&
- (_keysIterator.current == null ||
- identical(_keysIterator.current, _HashMapImpl._DELETED_KEY)));
- return result;
- }
+class OrderedSet<E> extends _MapBackedSet<E> implements Set<E> {
Lasse Reichstein Nielsen 2013/01/04 09:39:06 I don't like "OrderedSet" as a name. It doesn't sa
sra1 2013/01/04 09:56:15 +1. From the CL title I thought at last we had a
sra1 2013/01/04 10:02:43 +1. From the CL title I thought at last we had a
+ OrderedSet.from(Iterable<E> iterable)
+ : super.from(new _LinkedHashMapImpl<E, E>(), iterable);
- Iterator _keysIterator;
+ OrderedSet() : super(new _LinkedHashMapImpl<E, E>());
Lasse Reichstein Nielsen 2013/01/04 09:39:06 I still think it's silly to have a HashMap interfa
}
« no previous file with comments | « no previous file | tests/corelib/ordered_set_iterator_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698