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