| OLD | NEW |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 part of quiver.iterables; | 15 part of quiver.iterables; |
| 16 | 16 |
| 17 /** | 17 /// Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth |
| 18 * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth | 18 /// element of [iterable] and its index. |
| 19 * element of [iterable] and its index. | |
| 20 */ | |
| 21 Iterable<IndexedValue> enumerate(Iterable iterable) => | 19 Iterable<IndexedValue> enumerate(Iterable iterable) => |
| 22 new EnumerateIterable(iterable); | 20 new EnumerateIterable(iterable); |
| 23 | 21 |
| 24 class IndexedValue<V> { | 22 class IndexedValue<V> { |
| 25 final int index; | 23 final int index; |
| 26 final V value; | 24 final V value; |
| 27 | 25 |
| 28 IndexedValue(this.index, this.value); | 26 IndexedValue(this.index, this.value); |
| 29 | 27 |
| 30 operator ==(o) => o is IndexedValue && o.index == index && o.value == value; | 28 operator ==(o) => o is IndexedValue && o.index == index && o.value == value; |
| 31 int get hashCode => index * 31 + value.hashCode; | 29 int get hashCode => index * 31 + value.hashCode; |
| 32 String toString() => '($index, $value)'; | 30 String toString() => '($index, $value)'; |
| 33 } | 31 } |
| 34 | 32 |
| 35 /** | 33 /// An [Iterable] of [IndexedValue]s where the nth value holds the nth |
| 36 * An [Iterable] of [IndexedValue]s where the nth value holds the nth | 34 /// element of [iterable] and its index. See [enumerate]. |
| 37 * element of [iterable] and its index. See [enumerate]. | |
| 38 */ | |
| 39 // This was inspired by MappedIterable internal to Dart collections. | 35 // This was inspired by MappedIterable internal to Dart collections. |
| 40 class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> { | 36 class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> { |
| 41 final Iterable<V> _iterable; | 37 final Iterable<V> _iterable; |
| 42 | 38 |
| 43 EnumerateIterable(this._iterable); | 39 EnumerateIterable(this._iterable); |
| 44 | 40 |
| 45 Iterator<IndexedValue<V>> get iterator => | 41 Iterator<IndexedValue<V>> get iterator => |
| 46 new EnumerateIterator<V>(_iterable.iterator); | 42 new EnumerateIterator<V>(_iterable.iterator); |
| 47 | 43 |
| 48 // Length related functions are independent of the mapping. | 44 // Length related functions are independent of the mapping. |
| 49 int get length => _iterable.length; | 45 int get length => _iterable.length; |
| 50 bool get isEmpty => _iterable.isEmpty; | 46 bool get isEmpty => _iterable.isEmpty; |
| 51 | 47 |
| 52 // Index based lookup can be done before transforming. | 48 // Index based lookup can be done before transforming. |
| 53 IndexedValue<V> get first => new IndexedValue<V>(0, _iterable.first); | 49 IndexedValue<V> get first => new IndexedValue<V>(0, _iterable.first); |
| 54 IndexedValue<V> get last => new IndexedValue<V>(length - 1, _iterable.last); | 50 IndexedValue<V> get last => new IndexedValue<V>(length - 1, _iterable.last); |
| 55 IndexedValue<V> get single => new IndexedValue<V>(0, _iterable.single); | 51 IndexedValue<V> get single => new IndexedValue<V>(0, _iterable.single); |
| 56 IndexedValue<V> elementAt(int index) => | 52 IndexedValue<V> elementAt(int index) => |
| 57 new IndexedValue<V>(index, _iterable.elementAt(index)); | 53 new IndexedValue<V>(index, _iterable.elementAt(index)); |
| 58 } | 54 } |
| 59 | 55 |
| 60 /** The [Iterator] returned by [EnumerateIterable.iterator]. */ | 56 /// The [Iterator] returned by [EnumerateIterable.iterator]. |
| 61 class EnumerateIterator<V> extends Iterator<IndexedValue<V>> { | 57 class EnumerateIterator<V> extends Iterator<IndexedValue<V>> { |
| 62 final Iterator<V> _iterator; | 58 final Iterator<V> _iterator; |
| 63 int _index = 0; | 59 int _index = 0; |
| 64 IndexedValue<V> _current; | 60 IndexedValue<V> _current; |
| 65 | 61 |
| 66 EnumerateIterator(this._iterator); | 62 EnumerateIterator(this._iterator); |
| 67 | 63 |
| 68 IndexedValue<V> get current => _current; | 64 IndexedValue<V> get current => _current; |
| 69 | 65 |
| 70 bool moveNext() { | 66 bool moveNext() { |
| 71 if (_iterator.moveNext()) { | 67 if (_iterator.moveNext()) { |
| 72 _current = new IndexedValue(_index++, _iterator.current); | 68 _current = new IndexedValue(_index++, _iterator.current); |
| 73 return true; | 69 return true; |
| 74 } | 70 } |
| 75 _current = null; | 71 _current = null; |
| 76 return false; | 72 return false; |
| 77 } | 73 } |
| 78 } | 74 } |
| OLD | NEW |