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