Skip to content

Commit bc89020

Browse files
committed
Update README
1 parent 34c807e commit bc89020

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,48 @@
88
Implementation of k-way merge.
99

1010
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.
1314

1415
The primary purpose of `KWayMerger` is to efficiently merge N sorted iterables into one sorted stream.
1516

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+
```
1740

1841
## 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.
2043
See their docstrings for more details.
2144

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+
2253
## Contributing
2354
We appreciate contributions from users including reporting bugs, fixing
2455
issues, improving performance and adding new features.

0 commit comments

Comments
 (0)