Line data Source code
1 : // Copyright (c) 2017, 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 : import 'dart:collection'; 6 : 7 : import 'combined_iterator.dart'; 8 : 9 : /// A view of several iterables combined sequentially into a single iterable. 10 : /// 11 : /// All methods and accessors treat the [CombinedIterableView] as if it were a 12 : /// single concatenated iterable, but the underlying implementation is based on 13 : /// lazily accessing individual iterable instances. This means that if the 14 : /// underlying iterables change, the [CombinedIterableView] will reflect those 15 : /// changes. 16 : class CombinedIterableView<T> extends IterableBase<T> { 17 : /// The iterables that this combines. 18 : final Iterable<Iterable<T>> _iterables; 19 : 20 : /// Creates a combined view of [iterables]. 21 0 : const CombinedIterableView(this._iterables); 22 : 23 0 : @override 24 : Iterator<T> get iterator => 25 0 : CombinedIterator<T>(_iterables.map((i) => i.iterator).iterator); 26 : 27 : // Special cased contains/isEmpty/length since many iterables have an 28 : // efficient implementation instead of running through the entire iterator. 29 : 30 0 : @override 31 0 : bool contains(Object? element) => _iterables.any((i) => i.contains(element)); 32 : 33 0 : @override 34 0 : bool get isEmpty => _iterables.every((i) => i.isEmpty); 35 : 36 0 : @override 37 0 : int get length => _iterables.fold(0, (length, i) => length + i.length); 38 : }