Add missing files from previous commit.

Review URL: https://codereview.chromium.org//118783004

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/collection@31268 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/lib/iterable_zip.dart b/lib/iterable_zip.dart
new file mode 100644
index 0000000..772b07e
--- /dev/null
+++ b/lib/iterable_zip.dart
@@ -0,0 +1,59 @@
+// 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.
+
+/**
+ * Zipping multiple iterables into one iterable of tuples of values.
+ */
+library dart.pkg.collection.iterable_zip;
+
+import "dart:collection" show IterableBase;
+
+/**
+ * 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 extends IterableBase<List> {
+  final Iterable<Iterable> _iterables;
+  IterableZip(Iterable<Iterable> iterables)
+      : this._iterables = iterables;
+
+  /**
+   * Returns an iterator that combines values of the iterables' iterators
+   * as long as they all have values.
+   */
+  Iterator<List> get iterator {
+    List iterators = _iterables.map((x) => x.iterator).toList(growable: false);
+    // TODO(lrn): Return an empty iterator directly if iterators is empty?
+    return new _IteratorZip(iterators);
+  }
+}
+
+class _IteratorZip implements Iterator<List> {
+  final List<Iterator> _iterators;
+  List _current;
+  _IteratorZip(List 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 get current => _current;
+}
diff --git a/test/iterable_zip_test.dart b/test/iterable_zip_test.dart
new file mode 100644
index 0000000..ec191fe
--- /dev/null
+++ b/test/iterable_zip_test.dart
@@ -0,0 +1,112 @@
+// 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";
+import "package:collection/iterable_zip.dart";
+import "package:unittest/unittest.dart";
+
+/// Iterable like [base] except that it throws when value equals [errorValue].
+Iterable iterError(Iterable base, int errorValue) {
+  return base.map((x) => x == errorValue ? throw "BAD" : x);
+}
+
+main() {
+  test("Basic", () {
+    expect(new IterableZip([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
+           equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]]));
+  });
+
+  test("Uneven length 1", () {
+    expect(new IterableZip([[1, 2, 3, 99, 100], [4, 5, 6], [7, 8, 9]]),
+           equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]]));
+  });
+
+  test("Uneven length 2", () {
+    expect(new IterableZip([[1, 2, 3], [4, 5, 6, 99, 100], [7, 8, 9]]),
+           equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]]));
+  });
+
+  test("Uneven length 3", () {
+    expect(new IterableZip([[1, 2, 3], [4, 5, 6], [7, 8, 9, 99, 100]]),
+           equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]]));
+  });
+
+  test("Uneven length 3", () {
+    expect(new IterableZip([[1, 2, 3, 98], [4, 5, 6], [7, 8, 9, 99, 100]]),
+           equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]]));
+  });
+
+  test("Empty 1", () {
+    expect(new IterableZip([[], [4, 5, 6], [7, 8, 9]]), equals([]));
+  });
+
+  test("Empty 2", () {
+    expect(new IterableZip([[1, 2, 3], [], [7, 8, 9]]), equals([]));
+  });
+
+  test("Empty 3", () {
+    expect(new IterableZip([[1, 2, 3], [4, 5, 6], []]), equals([]));
+  });
+
+  test("Empty source", () {
+    expect(new IterableZip([]), equals([]));
+  });
+
+  test("Single Source", () {
+    expect(new IterableZip([[1, 2, 3]]), equals([[1], [2], [3]]));
+  });
+
+  test("Not-lists", () {
+    // Use other iterables than list literals.
+    Iterable it1 = [1, 2, 3, 4, 5, 6].where((x) => x < 4);
+    Set it2 = new LinkedHashSet()..add(4)..add(5)..add(6);
+    Iterable it3 = (new LinkedHashMap()..[7] = 0 ..[8] = 0 ..[9] = 0).keys;
+    Iterable<Iterable> allIts =
+        new Iterable.generate(3, (i) => [it1, it2, it3][i]);
+    expect(new IterableZip(allIts),
+           equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]]));
+  });
+
+  test("Error 1", () {
+    expect(() => new IterableZip([iterError([1, 2, 3], 2),
+                                  [4, 5, 6],
+                                  [7, 8, 9]]).toList(),
+           throwsA(equals("BAD")));
+  });
+
+  test("Error 2", () {
+    expect(() => new IterableZip([[1, 2, 3],
+                                  iterError([4, 5, 6], 5),
+                                  [7, 8, 9]]).toList(),
+           throwsA(equals("BAD")));
+  });
+
+  test("Error 3", () {
+    expect(() => new IterableZip([[1, 2, 3],
+                                  [4, 5, 6],
+                                  iterError([7, 8, 9], 8)]).toList(),
+           throwsA(equals("BAD")));
+  });
+
+  test("Error at end", () {
+    expect(() => new IterableZip([[1, 2, 3],
+                                  iterError([4, 5, 6], 6),
+                                  [7, 8, 9]]).toList(),
+           throwsA(equals("BAD")));
+  });
+
+  test("Error before first end", () {
+    expect(() => new IterableZip([iterError([1, 2, 3, 4], 4),
+                                  [4, 5, 6],
+                                  [7, 8, 9]]).toList(),
+           throwsA(equals("BAD")));
+  });
+
+  test("Error after first end", () {
+    expect(new IterableZip([[1, 2, 3],
+                            [4, 5, 6],
+                            iterError([7, 8, 9, 10], 10)]),
+           equals([[1, 4, 7], [2, 5, 8], [3, 6, 9]]));
+  });
+}