|
8 | 8 | Implementation of k-way merge.
|
9 | 9 |
|
10 | 10 | This package implements the `KWayMerger` type.
|
11 |
| -It is a stateful, lazy iterator of the elements in an iterator of iterators, similar to `Iterators.flatten`. However, the elements of the inner iterators will be yielded in an order given by a predicate optionally passed to `KWayMerger` (default: `isless`). |
12 |
| -If the inner iterators are sorted by the predicate, the output of the `KWayMerger` is also guaranteed to be sorted. |
| 11 | +It is a stateful, lazy iterator of the elements in an iterator of iterators. |
| 12 | +The elements of the inner iterators will be yielded in an order given by a predicate optionally passed to `KWayMerger` (default: `isless`). |
| 13 | +Therefore, if the inner iterators are sorted by the predicate, the output of the `KWayMerger` is also guaranteed to be sorted. |
13 | 14 |
|
14 | 15 | The primary purpose of `KWayMerger` is to efficiently merge N sorted iterables into one sorted stream.
|
15 | 16 |
|
16 |
| -The function `peek` can be used to check the next element without advancing the iterator. |
| 17 | +The iterator yields `(i::Int, x)` tuples, where `x` is the next element of one of the iterators, and `i` is the 1-based index of the iterator that yielded `x`: |
| 18 | + |
| 19 | +```julia |
| 20 | +julia> it = KWayMerger([[2, 3], [1, 4]]); |
| 21 | + |
| 22 | +julia> first(it) |
| 23 | +(2, 1) |
| 24 | + |
| 25 | +julia> println(collect(it)) |
| 26 | +[(1, 2), (1, 3), (2, 4)] |
| 27 | +``` |
| 28 | + |
| 29 | +The function `peek` can be used to check the next element without advancing the iterator: |
| 30 | + |
| 31 | +```julia |
| 32 | +julia> it = KWayMerger([1]); |
| 33 | + |
| 34 | +julia> peek(it) |
| 35 | +(1, 1) |
| 36 | + |
| 37 | +julia> iterate(it); peek(it) === nothing |
| 38 | +true |
| 39 | +``` |
17 | 40 |
|
18 | 41 | ## Documentation
|
19 |
| -This package's two public functions are the `KWayMerger` constructor, and its `Base.peek` method. |
| 42 | +This package's public functionality are the `KWayMerger` type, and its `Base.peek` method. |
20 | 43 | See their docstrings for more details.
|
21 | 44 |
|
| 45 | +## Performance |
| 46 | +When merging I iterables with a total length of N: |
| 47 | +* A `KWayMerger` allocates O(I) space upon construction |
| 48 | +* Producing each element takes O(log(I)) time |
| 49 | + |
| 50 | +Therefore, merging I sorted iterables with N total elements using a KWayMerger therefore takes O(N * log(I)) time. |
| 51 | +It is generally faster than flattening the iterators and sorting, when I << N. |
| 52 | + |
22 | 53 | ## Contributing
|
23 | 54 | We appreciate contributions from users including reporting bugs, fixing
|
24 | 55 | issues, improving performance and adding new features.
|
|
0 commit comments