| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 Google Inc. All Rights Reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 part of quiver.iterables; | |
| 16 | |
| 17 /** | |
| 18 * Returns the result of merging an [Iterable] of [Iterable]s, according to | |
| 19 * the order specified by the [compare] function. This function assumes the | |
| 20 * provided iterables are already sorted according to the provided [compare] | |
| 21 * function. It will not check for this condition or sort the iterables. | |
| 22 * | |
| 23 * The compare function must act as a [Comparator]. If [compare] is omitted, | |
| 24 * [Comparable.compare] is used. | |
| 25 * | |
| 26 * If any of the [iterables] contain null elements, an exception will be | |
| 27 * thrown. | |
| 28 */ | |
| 29 Iterable merge(Iterable<Iterable> iterables, | |
| 30 [Comparator compare = Comparable.compare]) => | |
| 31 (iterables.isEmpty) ? const [] : new _Merge(iterables, compare); | |
| 32 | |
| 33 class _Merge extends IterableBase { | |
| 34 final Iterable<Iterable> _iterables; | |
| 35 final Comparator _compare; | |
| 36 | |
| 37 _Merge(this._iterables, this._compare); | |
| 38 | |
| 39 Iterator get iterator => new _MergeIterator( | |
| 40 _iterables.map((i) => i.iterator).toList(growable: false), _compare); | |
| 41 | |
| 42 String toString() => this.toList().toString(); | |
| 43 } | |
| 44 | |
| 45 /// Like [Iterator] but one element ahead. | |
| 46 class _IteratorPeeker { | |
| 47 final Iterator _iterator; | |
| 48 bool _hasCurrent; | |
| 49 | |
| 50 _IteratorPeeker(Iterator iterator) | |
| 51 : _iterator = iterator, | |
| 52 _hasCurrent = iterator.moveNext(); | |
| 53 | |
| 54 moveNext() { | |
| 55 _hasCurrent = _iterator.moveNext(); | |
| 56 } | |
| 57 | |
| 58 get current => _iterator.current; | |
| 59 } | |
| 60 | |
| 61 class _MergeIterator implements Iterator { | |
| 62 final List<_IteratorPeeker> _peekers; | |
| 63 final Comparator _compare; | |
| 64 var _current; | |
| 65 | |
| 66 _MergeIterator(List<Iterator> iterators, this._compare) | |
| 67 : _peekers = iterators.map((i) => new _IteratorPeeker(i)).toList(); | |
| 68 | |
| 69 bool moveNext() { | |
| 70 // Pick the peeker that's peeking at the puniest piece | |
| 71 _IteratorPeeker minIter = null; | |
| 72 for (var p in _peekers) { | |
| 73 if (p._hasCurrent) { | |
| 74 if (minIter == null || _compare(p.current, minIter.current) < 0) { | |
| 75 minIter = p; | |
| 76 } | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 if (minIter == null) { | |
| 81 return false; | |
| 82 } | |
| 83 _current = minIter.current; | |
| 84 minIter.moveNext(); | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 get current => _current; | |
| 89 } | |
| OLD | NEW |