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