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

Unified Diff: packages/collection/lib/src/iterable_zip.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « packages/collection/lib/src/functions.dart ('k') | packages/collection/lib/src/priority_queue.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: packages/collection/lib/src/iterable_zip.dart
diff --git a/packages/collection/lib/src/iterable_zip.dart b/packages/collection/lib/src/iterable_zip.dart
new file mode 100644
index 0000000000000000000000000000000000000000..b9834370673fdb614c51e9f972b202415b5a376b
--- /dev/null
+++ b/packages/collection/lib/src/iterable_zip.dart
@@ -0,0 +1,52 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "dart:collection";
+
+/// Iterable that iterates over lists of values from other iterables.
+///
+/// When [iterator] is read, an [Iterator] is created for each [Iterable] in
+/// the [Iterable] passed to the constructor.
+///
+/// As long as all these iterators have a next value, those next values are
+/// combined into a single list, which becomes the next value of this
+/// [Iterable]'s [Iterator]. As soon as any of the iterators run out,
+/// the zipped iterator also stops.
+class IterableZip<T> extends IterableBase<List<T>> {
+ final Iterable<Iterable<T>> _iterables;
+
+ IterableZip(Iterable<Iterable<T>> iterables) : this._iterables = iterables;
+
+ /// Returns an iterator that combines values of the iterables' iterators
+ /// as long as they all have values.
+ Iterator<List<T>> get iterator {
+ var iterators = _iterables.map((x) => x.iterator).toList(growable: false);
+ // TODO(lrn): Return an empty iterator directly if iterators is empty?
+ return new _IteratorZip<T>(iterators);
+ }
+}
+
+class _IteratorZip<T> implements Iterator<List<T>> {
+ final List<Iterator<T>> _iterators;
+ List<T> _current;
+
+ _IteratorZip(List<Iterator<T>> iterators) : _iterators = iterators;
+
+ bool moveNext() {
+ if (_iterators.isEmpty) return false;
+ for (int i = 0; i < _iterators.length; i++) {
+ if (!_iterators[i].moveNext()) {
+ _current = null;
+ return false;
+ }
+ }
+ _current = new List(_iterators.length);
+ for (int i = 0; i < _iterators.length; i++) {
+ _current[i] = _iterators[i].current;
+ }
+ return true;
+ }
+
+ List<T> get current => _current;
+}
« no previous file with comments | « packages/collection/lib/src/functions.dart ('k') | packages/collection/lib/src/priority_queue.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698