Add CombinedIterableView (#52)
diff --git a/lib/collection.dart b/lib/collection.dart
index 2ea73a7..7012432 100644
--- a/lib/collection.dart
+++ b/lib/collection.dart
@@ -4,6 +4,7 @@
export "src/algorithms.dart";
export "src/canonicalized_map.dart";
+export "src/combined_wrappers/combined_iterable.dart";
export "src/combined_wrappers/combined_list.dart";
export "src/comparators.dart";
export "src/equality.dart";
diff --git a/lib/src/combined_wrappers/combined_iterable.dart b/lib/src/combined_wrappers/combined_iterable.dart
new file mode 100644
index 0000000..62e02be
--- /dev/null
+++ b/lib/src/combined_wrappers/combined_iterable.dart
@@ -0,0 +1,53 @@
+// Copyright (c) 2017, 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';
+
+/// A view of several iterables combined sequentially into a single iterable.
+///
+/// All methods and accessors treat the [CombinedIterableView] as if it were a
+/// single concatenated iterable, but the underlying implementation is based on
+/// lazily accessing individual iterable instances. This means that if the
+/// underlying iterables change, the [CombinedIterableView] will reflect those
+/// changes.
+class CombinedIterableView<T> extends IterableBase<T> {
+ /// The iterables that this combines.
+ final Iterable<Iterable<T>> _iterables;
+
+ /// Creates a combined view of [iterables].
+ const CombinedIterableView(this._iterables);
+
+ Iterator<T> get iterator =>
+ new _CombinedIterator<T>(_iterables.map((i) => i.iterator).iterator);
+
+ // Special cased isEmpty/length since many iterables have an efficient
+ // implementation instead of running through the entire iterator.
+
+ bool get isEmpty => _iterables.every((i) => i.isEmpty);
+
+ int get length => _iterables.fold(0, (length, i) => length + i.length);
+}
+
+/// The iterator for [CombinedIterableView].
+///
+/// This moves through each iterable's iterators in sequence.
+class _CombinedIterator<T> implements Iterator<T> {
+ /// The iterators that this combines.
+ ///
+ /// Because this comes from a call to [Iterable.map], it's lazy and will
+ /// avoid instantiating unnecessary iterators.
+ final Iterator<Iterator<T>> _iterators;
+
+ _CombinedIterator(this._iterators);
+
+ T get current => _iterators.current?.current;
+
+ bool moveNext() {
+ var current = _iterators.current;
+ if (current != null && current.moveNext()) {
+ return true;
+ }
+ return _iterators.moveNext() && moveNext();
+ }
+}
diff --git a/test/combined_wrapper/iterable_test.dart b/test/combined_wrapper/iterable_test.dart
new file mode 100644
index 0000000..a473a02
--- /dev/null
+++ b/test/combined_wrapper/iterable_test.dart
@@ -0,0 +1,60 @@
+// Copyright (c) 2017, 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 'package:collection/collection.dart';
+import 'package:test/test.dart';
+
+void main() {
+ var iterable1 = new Iterable.generate(3);
+ var iterable2 = new Iterable.generate(3, (i) => i + 3);
+ var iterable3 = new Iterable.generate(3, (i) => i + 6);
+
+ test('should combine multiple iterables when iterating', () {
+ var combined = new CombinedIterableView([iterable1, iterable2, iterable3]);
+ expect(combined, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
+ });
+
+ test('should combine multiple iterables with some empty ones', () {
+ var combined = new CombinedIterableView(
+ [iterable1, [], iterable2, [], iterable3, []]);
+ expect(combined, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
+ });
+
+ test('should function as an empty iterable when no iterables are passed', () {
+ var empty = new CombinedIterableView([]);
+ expect(empty, isEmpty);
+ });
+
+ test('should function as an empty iterable with all empty iterables', () {
+ var empty = new CombinedIterableView([[], [], []]);
+ expect(empty, isEmpty);
+ });
+
+ test('should reflect changes from the underlying iterables', () {
+ var list1 = [];
+ var list2 = [];
+ var combined = new CombinedIterableView([list1, list2]);
+ expect(combined, isEmpty);
+ list1.addAll([1, 2]);
+ list2.addAll([3, 4]);
+ expect(combined, [1, 2, 3, 4]);
+ expect(combined.last, 4);
+ expect(combined.first, 1);
+ });
+
+ test('should reflect changes from the iterable of iterables', () {
+ var iterables = <Iterable>[];
+ var combined = new CombinedIterableView(iterables);
+ expect(combined, isEmpty);
+ expect(combined, hasLength(0));
+
+ iterables.add(iterable1);
+ expect(combined, isNotEmpty);
+ expect(combined, hasLength(3));
+
+ iterables.clear();
+ expect(combined, isEmpty);
+ expect(combined, hasLength(0));
+ });
+}
diff --git a/test/combined_list_view_test.dart b/test/combined_wrapper/list_test.dart
similarity index 96%
rename from test/combined_list_view_test.dart
rename to test/combined_wrapper/list_test.dart
index d61e51a..e072097 100644
--- a/test/combined_list_view_test.dart
+++ b/test/combined_wrapper/list_test.dart
@@ -5,7 +5,7 @@
import 'package:collection/collection.dart';
import 'package:test/test.dart';
-import 'unmodifiable_collection_test.dart' as common;
+import '../unmodifiable_collection_test.dart' as common;
void main() {
var list1 = [1, 2, 3];