OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of utf; |
| 6 |
| 7 /** |
| 8 * _ListRange in an internal type used to create a lightweight Interable on a |
| 9 * range within a source list. DO NOT MODIFY the underlying list while |
| 10 * iterating over it. The results of doing so are undefined. |
| 11 */ |
| 12 // TODO(floitsch): Consider removing the extend and switch to implements since |
| 13 // that's cheaper to allocate. |
| 14 class _ListRange extends IterableBase { |
| 15 final List _source; |
| 16 final int _offset; |
| 17 final int _length; |
| 18 |
| 19 _ListRange(source, [offset = 0, length]) : |
| 20 this._source = source, |
| 21 this._offset = offset, |
| 22 this._length = (length == null ? source.length - offset : length) { |
| 23 if (_offset < 0 || _offset > _source.length) { |
| 24 throw new RangeError.value(_offset); |
| 25 } |
| 26 if (_length != null && (_length < 0)) { |
| 27 throw new RangeError.value(_length); |
| 28 } |
| 29 if (_length + _offset > _source.length) { |
| 30 throw new RangeError.value(_length + _offset); |
| 31 } |
| 32 } |
| 33 |
| 34 _ListRangeIterator get iterator => |
| 35 new _ListRangeIteratorImpl(_source, _offset, _offset + _length); |
| 36 |
| 37 int get length => _length; |
| 38 } |
| 39 |
| 40 /** |
| 41 * The _ListRangeIterator provides more capabilities than a standard iterator, |
| 42 * including the ability to get the current position, count remaining items, |
| 43 * and move forward/backward within the iterator. |
| 44 */ |
| 45 abstract class _ListRangeIterator implements Iterator<int> { |
| 46 bool moveNext(); |
| 47 int get current; |
| 48 int get position; |
| 49 void backup([by]); |
| 50 int get remaining; |
| 51 void skip([count]); |
| 52 } |
| 53 |
| 54 class _ListRangeIteratorImpl implements _ListRangeIterator { |
| 55 final List<int> _source; |
| 56 int _offset; |
| 57 final int _end; |
| 58 |
| 59 _ListRangeIteratorImpl(this._source, int offset, this._end) |
| 60 : _offset = offset - 1; |
| 61 |
| 62 int get current => _source[_offset]; |
| 63 |
| 64 bool moveNext() => ++_offset < _end; |
| 65 |
| 66 int get position => _offset; |
| 67 |
| 68 void backup([int by = 1]) { |
| 69 _offset -= by; |
| 70 } |
| 71 |
| 72 int get remaining => _end - _offset - 1; |
| 73 |
| 74 void skip([int count = 1]) { |
| 75 _offset += count; |
| 76 } |
| 77 } |
| 78 |
OLD | NEW |