| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 /** | |
| 6 * Contains functions that are included by default in [PolymerExpressions]. | |
| 7 * | |
| 8 * - [enumerate]: a convenient way to iterate over items and the indexes. | |
| 9 */ | |
| 10 // Code from https://github.com/google/quiver-dart/commit/52edc4baf37e99ff6a8f99
c648b29b135fc0b880 | |
| 11 library polymer_expressions.src.globals; | |
| 12 | |
| 13 import 'dart:collection'; | |
| 14 import 'package:observe/observe.dart' show reflectable; | |
| 15 | |
| 16 /** | |
| 17 * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth | |
| 18 * element of [iterable] and its index. | |
| 19 */ | |
| 20 Iterable<IndexedValue> enumerate(Iterable iterable) => | |
| 21 new EnumerateIterable(iterable); | |
| 22 | |
| 23 @reflectable class IndexedValue<V> { | |
| 24 final int index; | |
| 25 final V value; | |
| 26 | |
| 27 operator==(o) => o is IndexedValue && o.index == index && o.value == value; | |
| 28 int get hashCode => value.hashCode; | |
| 29 String toString() => '($index, $value)'; | |
| 30 | |
| 31 IndexedValue(this.index, this.value); | |
| 32 } | |
| 33 | |
| 34 /** | |
| 35 * An [Iterable] of [IndexedValue]s where the nth value holds the nth | |
| 36 * element of [iterable] and its index. See [enumerate]. | |
| 37 */ | |
| 38 // This was inspired by MappedIterable internal to Dart collections. | |
| 39 class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> { | |
| 40 final Iterable<V> _iterable; | |
| 41 | |
| 42 EnumerateIterable(this._iterable); | |
| 43 | |
| 44 Iterator<IndexedValue<V>> get iterator => | |
| 45 new EnumerateIterator<V>(_iterable.iterator); | |
| 46 | |
| 47 // Length related functions are independent of the mapping. | |
| 48 int get length => _iterable.length; | |
| 49 bool get isEmpty => _iterable.isEmpty; | |
| 50 | |
| 51 // Index based lookup can be done before transforming. | |
| 52 IndexedValue<V> get first => new IndexedValue<V>(0, _iterable.first); | |
| 53 IndexedValue<V> get last => new IndexedValue<V>(length - 1, _iterable.last); | |
| 54 IndexedValue<V> get single => new IndexedValue<V>(0, _iterable.single); | |
| 55 IndexedValue<V> elementAt(int index) => | |
| 56 new IndexedValue<V>(index, _iterable.elementAt(index)); | |
| 57 } | |
| 58 | |
| 59 /** The [Iterator] returned by [EnumerateIterable.iterator]. */ | |
| 60 class EnumerateIterator<V> extends Iterator<IndexedValue<V>> { | |
| 61 final Iterator<V> _iterator; | |
| 62 int _index = 0; | |
| 63 IndexedValue<V> _current; | |
| 64 | |
| 65 EnumerateIterator(this._iterator); | |
| 66 | |
| 67 IndexedValue<V> get current => _current; | |
| 68 | |
| 69 bool moveNext() { | |
| 70 if (_iterator.moveNext()) { | |
| 71 _current = new IndexedValue(_index++, _iterator.current); | |
| 72 return true; | |
| 73 } | |
| 74 _current = null; | |
| 75 return false; | |
| 76 } | |
| 77 } | |
| OLD | NEW |