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