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