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

Side by Side Diff: sdk/lib/core/queue.dart

Issue 11410086: Use iterator, moveNext(), current. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * A [Queue] is a collection that can be manipulated at both ends. One 6 * A [Queue] is a collection that can be manipulated at both ends. One
7 * can iterate over the elements of a queue through [forEach] or with 7 * can iterate over the elements of a queue through [forEach] or with
8 * an [Iterator]. 8 * an [Iterator].
9 */ 9 */
10 abstract class Queue<E> extends Collection<E> { 10 abstract class Queue<E> extends Collection<E> {
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 Queue<E> other = new Queue<E>(); 300 Queue<E> other = new Queue<E>();
301 DoubleLinkedQueueEntry<E> entry = _sentinel._next; 301 DoubleLinkedQueueEntry<E> entry = _sentinel._next;
302 while (!identical(entry, _sentinel)) { 302 while (!identical(entry, _sentinel)) {
303 DoubleLinkedQueueEntry<E> nextEntry = entry._next; 303 DoubleLinkedQueueEntry<E> nextEntry = entry._next;
304 if (f(entry._element)) other.addLast(entry._element); 304 if (f(entry._element)) other.addLast(entry._element);
305 entry = nextEntry; 305 entry = nextEntry;
306 } 306 }
307 return other; 307 return other;
308 } 308 }
309 309
310 _DoubleLinkedQueueIterator<E> iterator() { 310 _DoubleLinkedQueueIterator<E> get iterator {
311 return new _DoubleLinkedQueueIterator<E>(_sentinel); 311 return new _DoubleLinkedQueueIterator<E>(_sentinel);
312 } 312 }
313 313
314 String toString() { 314 String toString() {
315 return Collections.collectionToString(this); 315 return Collections.collectionToString(this);
316 } 316 }
317 } 317 }
318 318
319 class _DoubleLinkedQueueIterator<E> implements Iterator<E> { 319 class _DoubleLinkedQueueIterator<E> implements Iterator<E> {
320 final _DoubleLinkedQueueEntrySentinel<E> _sentinel; 320 final _DoubleLinkedQueueEntrySentinel<E> _sentinel;
321 DoubleLinkedQueueEntry<E> _currentEntry; 321 DoubleLinkedQueueEntry<E> _currentEntry = null;
322 322
323 _DoubleLinkedQueueIterator(_DoubleLinkedQueueEntrySentinel this._sentinel) { 323 _DoubleLinkedQueueIterator(this._sentinel);
324 _currentEntry = _sentinel; 324
325 bool moveNext() {
326 if (identical(_currentEntry, _sentinel)) return false;
Lasse Reichstein Nielsen 2012/11/15 10:24:37 The 'null' represents the initial state, right? Do
floitsch 2012/11/16 17:51:58 in theory there is no need to use identical since
327 if (_currentEntry == null) _currentEntry = _sentinel;
328 _currentEntry = _currentEntry._next;
329 return _currentEntry != _sentinel;
325 } 330 }
326 331
327 bool get hasNext { 332 E get current {
328 return !identical(_currentEntry._next, _sentinel); 333 if (_currentEntry != null && _currentEntry != _sentinel) {
329 } 334 return _currentEntry.element;
330
331 E next() {
332 if (!hasNext) {
333 throw new StateError("No more elements");
334 } 335 }
335 _currentEntry = _currentEntry._next; 336 // TODO(floitsch): adapt error message.
336 return _currentEntry.element; 337 throw new StateError("No more elements");
337 } 338 }
338 } 339 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698