Line data Source code
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 : import 'dart:collection'; 6 : 7 : /// Iterable that iterates over lists of values from other iterables. 8 : /// 9 : /// When [iterator] is read, an [Iterator] is created for each [Iterable] in 10 : /// the [Iterable] passed to the constructor. 11 : /// 12 : /// As long as all these iterators have a next value, those next values are 13 : /// combined into a single list, which becomes the next value of this 14 : /// [Iterable]'s [Iterator]. As soon as any of the iterators run out, 15 : /// the zipped iterator also stops. 16 : class IterableZip<T> extends IterableBase<List<T>> { 17 : final Iterable<Iterable<T>> _iterables; 18 : 19 0 : IterableZip(Iterable<Iterable<T>> iterables) : _iterables = iterables; 20 : 21 : /// Returns an iterator that combines values of the iterables' iterators 22 : /// as long as they all have values. 23 0 : @override 24 : Iterator<List<T>> get iterator { 25 0 : var iterators = _iterables.map((x) => x.iterator).toList(growable: false); 26 0 : return _IteratorZip<T>(iterators); 27 : } 28 : } 29 : 30 : class _IteratorZip<T> implements Iterator<List<T>> { 31 : final List<Iterator<T>> _iterators; 32 : List<T>? _current; 33 : 34 0 : _IteratorZip(List<Iterator<T>> iterators) : _iterators = iterators; 35 : 36 0 : @override 37 : bool moveNext() { 38 0 : if (_iterators.isEmpty) return false; 39 0 : for (var i = 0; i < _iterators.length; i++) { 40 0 : if (!_iterators[i].moveNext()) { 41 0 : _current = null; 42 : return false; 43 : } 44 : } 45 0 : _current = List.generate(_iterators.length, (i) => _iterators[i].current, 46 : growable: false); 47 : return true; 48 : } 49 : 50 0 : @override 51 0 : List<T> get current => _current ?? (throw StateError('No element')); 52 : }