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

Side by Side Diff: sdk/lib/core/iterator.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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 * The [Iterator] class provides methods to iterate over an object. It 6 * The [Iterator] class provides methods to iterate over an object. It
7 * is transparently used by the for-in construct to test for the end 7 * is transparently used by the for-in construct to test for the end
8 * of the iteration, and to get the elements. 8 * of the iteration, and to get the elements.
9 * 9 *
10 * If the object iterated over is changed during the iteration, the 10 * If the object iterated over is changed during the iteration, the
11 * behavior is unspecified. 11 * behavior is unspecified.
12 *
13 * The [Iterator] is initially positioned before the first element. Before
14 * accessing the first element the iterator must thus be advanced ([moveNext])
15 * to point to the first element. If there is no element left, then [moveNext]
16 * returns false.
12 */ 17 */
13 abstract class Iterator<E> { 18 abstract class Iterator<E> {
14 /** 19 /**
15 * Gets the next element in the iteration. Throws a 20 * Moves to the next element. Returns true if [current] contains the next
16 * [StateError] if no element is left. 21 * element. Returns false, if no element was left. It the latter case
22 * trying to read [current] will throw a [StateError].
23 *
24 * It is safe to invoke [moveNext] even when the iterator is already
25 * positioned after the last element. In this case [moveNext] has no effect.
17 */ 26 */
18 E next(); 27 bool moveNext();
19 28
20 /** 29 /**
21 * Returns whether the [Iterator] has elements left. 30 * Returns the current element.
31 *
32 * Throws a [StateError] if the iterator has not yet been moved to the first
33 * element, or if the iterator has been moved after the last element of the
34 * [Iterable].
22 */ 35 */
23 bool get hasNext; 36 E get current;
24 } 37 }
38
39 class HasNextIterator<E> {
40 static const int _HAS_NEXT_AND_NEXT_IN_CURRENT = 0;
41 static const int _NO_NEXT = 1;
42 static const int _NOT_MOVED_YET = 2;
43
44 Iterator _iterator;
45 int _state = _NOT_MOVED_YET;
46
47 HasNextIterator(this._iterator);
48
49 bool get hasNext {
50 switch(_state) {
51 case _NOT_MOVED_YET:
52 bool currentIsValid = _iterator.moveNext();
53 if (currentIsValid) {
54 _state = _HAS_NEXT_AND_NEXT_IN_CURRENT;
55 } else {
56 _state = _NO_NEXT;
57 }
58 return currentIsValid;
59 case _HAS_NEXT_AND_NEXT_IN_CURRENT:
60 return true;
61 case _NO_NEXT:
62 return false;
63 }
64 }
65
66 E next() {
67 if (!hasNext) throw new StateError("No more elements");
68 assert(_state == _HAS_NEXT_AND_NEXT_IN_CURRENT);
69 return _iterator.current;
70 }
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698