Chromium Code Reviews| 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. | |
|
floitsch
2013/07/04 14:31:22
unfinished sentence.
Lasse Reichstein Nielsen
2013/07/09 06:16:00
Done.
| |
| 18 */ | |
| 19 class IterableZip extends IterableBase<List> { | |
| 20 final Iterable<Iterable> _iterables; | |
| 21 IterableZip(Iterable<Iterable> iterables) | |
| 22 : this._iterables = iterables; | |
| 23 | |
| 24 /** | |
| 25 * Returns an iterator that combines values of the iterables' iterators | |
| 26 * as long as they all have values. | |
| 27 */ | |
| 28 Iterator<List> get iterator { | |
| 29 List iterators = _iterables.map((x) => x.iterator).toList(growable: false); | |
| 30 // TODO(lrn): Return an empty iterator directly if iterators is empty? | |
| 31 return new _IteratorZip(iterators); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 class _IteratorZip implements Iterator<List> { | |
| 36 final List<Iterator> _iterators; | |
| 37 List _current; | |
| 38 _IteratorZip(List iterators) : _iterators = iterators; | |
| 39 bool moveNext() { | |
| 40 if (_iterators.isEmpty) return false; | |
| 41 for (int i = 0; i < _iterators.length; i++) { | |
| 42 if (!_iterators[i].moveNext()) { | |
| 43 _current = null; | |
| 44 return false; | |
| 45 } | |
| 46 } | |
| 47 _current = new List(_iterators.length); | |
| 48 for (int i = 0; i < _iterators.length; i++) { | |
| 49 _current[i] = _iterators[i].current; | |
| 50 } | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 List get current => _current; | |
| 55 } | |
| OLD | NEW |