OLD | NEW |
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 part of html; | 5 part of html; |
6 | 6 |
7 // Iterator for arrays with fixed size. | 7 // Iterator for arrays with fixed size. |
8 class FixedSizeListIterator<T> extends _VariableSizeListIterator<T> { | 8 class FixedSizeListIterator<T> implements Iterator<T> { |
| 9 final List<T> _array; |
| 10 final int _length; // Cache array length for faster access. |
| 11 int _position; |
| 12 T _current; |
| 13 |
9 FixedSizeListIterator(List<T> array) | 14 FixedSizeListIterator(List<T> array) |
10 : super(array), | 15 : _array = array, |
| 16 _position = -1, |
11 _length = array.length; | 17 _length = array.length; |
12 | 18 |
13 bool get hasNext => _length > _pos; | 19 bool moveNext() { |
| 20 int nextPosition = _position + 1; |
| 21 if (nextPosition < _length) { |
| 22 _current = _array[nextPosition]; |
| 23 _position = nextPosition; |
| 24 return true; |
| 25 } |
| 26 _current = null; |
| 27 _position = _length; |
| 28 return false; |
| 29 } |
14 | 30 |
15 final int _length; // Cache array length for faster access. | 31 T get current => _current; |
16 } | 32 } |
17 | 33 |
18 // Iterator for arrays with variable size. | 34 // Iterator for arrays with variable size. |
19 class _VariableSizeListIterator<T> implements Iterator<T> { | 35 class _VariableSizeListIterator<T> implements Iterator<T> { |
| 36 final List<T> _array; |
| 37 int _position; |
| 38 T _current; |
| 39 |
20 _VariableSizeListIterator(List<T> array) | 40 _VariableSizeListIterator(List<T> array) |
21 : _array = array, | 41 : _array = array, |
22 _pos = 0; | 42 _position = -1; |
23 | 43 |
24 bool get hasNext => _array.length > _pos; | 44 bool moveNext() { |
25 | 45 int nextPosition = _position + 1; |
26 T next() { | 46 if (nextPosition < _array.length) { |
27 if (!hasNext) { | 47 _current = _array[nextPosition]; |
28 throw new StateError("No more elements"); | 48 _position = nextPosition; |
| 49 return true; |
29 } | 50 } |
30 return _array[_pos++]; | 51 _current = null; |
| 52 _position = _array.length; |
| 53 return false; |
31 } | 54 } |
32 | 55 |
33 final List<T> _array; | 56 T get current => _current; |
34 int _pos; | |
35 } | 57 } |
OLD | NEW |