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

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

Issue 11410086: Use iterator, moveNext(), current. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Address comments. Created 8 years, 1 month 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 2083e91ca66eb7df1ca1dd99679a3c4413a20f70..a7bb9de03e4090c24068f681ec5109ff122ccfaf 100644
--- a/sdk/lib/core/set.dart
+++ b/sdk/lib/core/set.dart
@@ -184,9 +184,7 @@ class _HashSetImpl<E> implements HashSet<E> {
return _backingMap.length;
}
- Iterator<E> iterator() {
- return new _HashSetIterator<E>(this);
- }
+ Iterator<E> get iterator => new _HashSetIterator<E>(this);
String toString() {
return Collections.collectionToString(this);
@@ -200,48 +198,27 @@ class _HashSetImpl<E> implements HashSet<E> {
class _HashSetIterator<E> implements Iterator<E> {
- // TODO(4504458): Replace set_ with set.
- _HashSetIterator(_HashSetImpl<E> set_)
- : _nextValidIndex = -1,
- _entries = set_._backingMap._keys {
- _advance();
- }
-
- bool get hasNext {
- if (_nextValidIndex >= _entries.length) return false;
- if (identical(_entries[_nextValidIndex], _HashMapImpl._DELETED_KEY)) {
- // This happens in case the set was modified in the meantime.
- // A modification on the set may make this iterator misbehave,
- // but we should never return the sentinel.
- _advance();
- }
- return _nextValidIndex < _entries.length;
- }
+ _HashSetIterator(_HashSetImpl<E> set)
+ : _keysIterator = set._backingMap._keys.iterator;
- E next() {
- if (!hasNext) {
- throw new StateError("No more elements");
+ E get current {
+ var result = _keysIterator.current;
+ if (identical(result, _HashMapImpl._DELETED_KEY)) {
+ // TODO(floitsch): improve the error reporting.
+ throw new StateError("Concurrent modification.");
}
- E res = _entries[_nextValidIndex];
- _advance();
- return res;
+ return result;
}
- void _advance() {
- int length = _entries.length;
- var entry;
- final deletedKey = _HashMapImpl._DELETED_KEY;
+ bool moveNext() {
+ bool result;
do {
- if (++_nextValidIndex >= length) break;
- entry = _entries[_nextValidIndex];
- } while ((entry == null) || identical(entry, deletedKey));
+ result = _keysIterator.moveNext();
+ } while (result &&
+ (_keysIterator.current == null ||
+ identical(_keysIterator.current, _HashMapImpl._DELETED_KEY)));
+ return result;
}
- // The entries in the set. May contain null or the sentinel value.
- List<E> _entries;
-
- // The next valid index in [_entries] or the length of [entries_].
- // If it is the length of [_entries], calling [hasNext] on the
- // iterator will return false.
- int _nextValidIndex;
+ Iterator _keysIterator;
}

Powered by Google App Engine
This is Rietveld 408576698