Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: pkg/sequence_zip/lib/iterable_zip.dart

Issue 18015002: Add package with iterable and stream "zipping" functionality. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. More tests. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/pkg.status ('k') | pkg/sequence_zip/lib/sequence_zip.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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,
18 * the zipped iterator also stops.
19 */
20 class IterableZip extends IterableBase<List> {
21 final Iterable<Iterable> _iterables;
22 IterableZip(Iterable<Iterable> iterables)
23 : this._iterables = iterables;
24
25 /**
26 * Returns an iterator that combines values of the iterables' iterators
27 * as long as they all have values.
28 */
29 Iterator<List> get iterator {
30 List iterators = _iterables.map((x) => x.iterator).toList(growable: false);
31 // TODO(lrn): Return an empty iterator directly if iterators is empty?
32 return new _IteratorZip(iterators);
33 }
34 }
35
36 class _IteratorZip implements Iterator<List> {
37 final List<Iterator> _iterators;
38 List _current;
39 _IteratorZip(List iterators) : _iterators = iterators;
40 bool moveNext() {
41 if (_iterators.isEmpty) return false;
42 for (int i = 0; i < _iterators.length; i++) {
43 if (!_iterators[i].moveNext()) {
44 _current = null;
45 return false;
46 }
47 }
48 _current = new List(_iterators.length);
49 for (int i = 0; i < _iterators.length; i++) {
50 _current[i] = _iterators[i].current;
51 }
52 return true;
53 }
54
55 List get current => _current;
56 }
OLDNEW
« no previous file with comments | « pkg/pkg.status ('k') | pkg/sequence_zip/lib/sequence_zip.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698