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