OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "dart:collection"; | 5 import "dart:collection" show IterableBase; |
nweiz
2016/03/21 20:09:18
Generally we don't use show/hide with import unles
| |
6 | 6 |
7 /// Iterable that iterates over lists of values from other iterables. | 7 /// Iterable that iterates over lists of values from other iterables. |
8 /// | 8 /// |
9 /// When [iterator] is read, an [Iterator] is created for each [Iterable] in | 9 /// When [iterator] is read, an [Iterator] is created for each [Iterable] in |
10 /// the [Iterable] passed to the constructor. | 10 /// the [Iterable] passed to the constructor. |
11 /// | 11 /// |
12 /// As long as all these iterators have a next value, those next values are | 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 | 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, | 14 /// [Iterable]'s [Iterator]. As soon as any of the iterators run out, |
15 /// the zipped iterator also stops. | 15 /// the zipped iterator also stops. |
16 class IterableZip extends IterableBase<List> { | 16 class IterableZip<T> extends IterableBase<List<T>> { |
17 final Iterable<Iterable> _iterables; | 17 final Iterable<Iterable<T>> _iterables; |
18 IterableZip(Iterable<Iterable> iterables) | 18 |
19 IterableZip(Iterable<Iterable<T>> iterables) | |
19 : this._iterables = iterables; | 20 : this._iterables = iterables; |
20 | 21 |
21 /// Returns an iterator that combines values of the iterables' iterators | 22 /// Returns an iterator that combines values of the iterables' iterators |
22 /// as long as they all have values. | 23 /// as long as they all have values. |
23 Iterator<List> get iterator { | 24 Iterator<List<T>> get iterator { |
24 List iterators = _iterables.map((x) => x.iterator).toList(growable: false); | 25 var iterators = _iterables.map((x) => x.iterator).toList(growable: false); |
25 // TODO(lrn): Return an empty iterator directly if iterators is empty? | 26 // TODO(lrn): Return an empty iterator directly if iterators is empty? |
26 return new _IteratorZip(iterators); | 27 return new _IteratorZip<T>(iterators); |
27 } | 28 } |
28 } | 29 } |
29 | 30 |
30 class _IteratorZip implements Iterator<List> { | 31 class _IteratorZip<T> implements Iterator<List<T>> { |
31 final List<Iterator> _iterators; | 32 final List<Iterator<T>> _iterators; |
32 List _current; | 33 List<T> _current; |
33 _IteratorZip(List iterators) : _iterators = iterators; | 34 |
35 _IteratorZip(List<Iterator<T>> iterators) : _iterators = iterators; | |
36 | |
34 bool moveNext() { | 37 bool moveNext() { |
35 if (_iterators.isEmpty) return false; | 38 if (_iterators.isEmpty) return false; |
36 for (int i = 0; i < _iterators.length; i++) { | 39 for (int i = 0; i < _iterators.length; i++) { |
37 if (!_iterators[i].moveNext()) { | 40 if (!_iterators[i].moveNext()) { |
38 _current = null; | 41 _current = null; |
39 return false; | 42 return false; |
40 } | 43 } |
41 } | 44 } |
42 _current = new List(_iterators.length); | 45 _current = new List(_iterators.length); |
43 for (int i = 0; i < _iterators.length; i++) { | 46 for (int i = 0; i < _iterators.length; i++) { |
44 _current[i] = _iterators[i].current; | 47 _current[i] = _iterators[i].current; |
45 } | 48 } |
46 return true; | 49 return true; |
47 } | 50 } |
48 | 51 |
49 List get current => _current; | 52 List<T> get current => _current; |
50 } | 53 } |
OLD | NEW |