| 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 library iterable_zip; |
| 6 |
| 7 import "dart:collection"; |
| 8 |
| 9 /** |
| 10 * Iterable that iterates over lists of values from other iterables. |
| 11 * |
| 12 * When [iterator] is read, an [Iterator] is created for each [Iterable] in |
| 13 * the [Iterable] passed to the constructor. |
| 14 * |
| 15 * As long as all these iterators have a next value, those next values are |
| 16 * combined into a single list, which becomes the next value of this |
| 17 * [Iterable]'s [Iterator]. As soon as any of the iterators run out, |
| 18 * the zipped iterator also stops. |
| 19 */ |
| 20 class IterableZip extends IterableBase<List> { |
| 21 final Iterable<Iterable> _iterables; |
| 22 IterableZip(Iterable<Iterable> iterables) |
| 23 : this._iterables = iterables; |
| 24 |
| 25 /** |
| 26 * Returns an iterator that combines values of the iterables' iterators |
| 27 * as long as they all have values. |
| 28 */ |
| 29 Iterator<List> get iterator { |
| 30 List iterators = _iterables.map((x) => x.iterator).toList(growable: false); |
| 31 // TODO(lrn): Return an empty iterator directly if iterators is empty? |
| 32 return new _IteratorZip(iterators); |
| 33 } |
| 34 } |
| 35 |
| 36 class _IteratorZip implements Iterator<List> { |
| 37 final List<Iterator> _iterators; |
| 38 List _current; |
| 39 _IteratorZip(List iterators) : _iterators = iterators; |
| 40 bool moveNext() { |
| 41 if (_iterators.isEmpty) return false; |
| 42 for (int i = 0; i < _iterators.length; i++) { |
| 43 if (!_iterators[i].moveNext()) { |
| 44 _current = null; |
| 45 return false; |
| 46 } |
| 47 } |
| 48 _current = new List(_iterators.length); |
| 49 for (int i = 0; i < _iterators.length; i++) { |
| 50 _current[i] = _iterators[i].current; |
| 51 } |
| 52 return true; |
| 53 } |
| 54 |
| 55 List get current => _current; |
| 56 } |
| OLD | NEW |