Skip to content

Commit 484a71b

Browse files
committed
Update README
1 parent 129e2f9 commit 484a71b

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,62 @@
66

77
Implementation of k-way merge.
88

9-
This package implements the `KWayMerger` type.
10-
It is a stateful, lazy iterator of the elements in an iterator of iterators.
11-
The elements of the inner iterators will be yielded in an order given by a predicate optionally passed to `KWayMerger` (default: `isless`).
12-
Therefore, if the inner iterators are sorted by the predicate, the output of the `KWayMerger` is also guaranteed to be sorted.
9+
This package exports the function `kway_merge`.
10+
It constructs a `KWayMerger` - a stateful, lazy iterator of the elements in an iterator of iterators.
11+
The elements of the inner iterators will be yielded in order, as specified by the optional ordering (default: `Forward`).
12+
Therefore, if the inner iterators are sorted by the order, the yielded elements of the `KWayMerger` is also guaranteed to be sorted.
1313

14-
The primary purpose of `KWayMerger` is to efficiently merge N sorted iterables into one sorted stream.
14+
The primary purpose of `kway_merge` is to efficiently merge N sorted iterables into one sorted stream.
1515

16-
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`:
16+
The iterator yields `@NamedTuple{from_iter::Int, value::T}`, where the value field has the next element of one of the iterators, and the from_iter field contains the 1-based index of the iterator that yielded the value:
1717

1818
```julia
19-
julia> it = KWayMerger([[2, 3], [1, 4]]);
19+
julia> it = kway_merge([[2, 3], [1, 4]]);
2020

2121
julia> first(it)
22-
(2, 1)
22+
(from_iter = 2, value = 1)
2323

24-
julia> println(collect(it))
24+
julia> println(map(Tuple, it))
2525
[(1, 2), (1, 3), (2, 4)]
2626
```
2727

2828
The function `peek` can be used to check the next element without advancing the iterator:
2929

3030
```julia
31-
julia> it = KWayMerger([1]);
31+
julia> it = kway_merge([1]);
3232

3333
julia> peek(it)
34-
(1, 1)
34+
(from_iter = 1, value = 1)
3535

3636
julia> first(it)
37-
(1, 1)
37+
(from_iter = 1, value = 1)
3838

3939
julia> peek(it) === nothing
4040
true
4141
```
4242

4343
## Documentation
44-
This package's public functionality are the `KWayMerger` type, and its `Base.peek` method.
44+
This package's public functionality are the `kway_merge` function, the (unexported) `KWayMerger` type, and its `Base.peek` method.
4545
See their docstrings for more details.
4646

4747
## Performance
48-
When merging I iterables with a total length of N:
48+
When merging I iterables:
4949
* A `KWayMerger` allocates O(I) space upon construction
5050
* Producing each element takes O(log(I)) time
5151

52-
Therefore, merging I sorted iterables with N total elements using a KWayMerger therefore takes O(N * log(I)) time.
53-
It is generally faster than flattening the iterators and sorting, when I << N.
52+
Therefore, merging I sorted iterables with N total elements using `kway_merge` takes O(N * log(I)) time.
53+
This is similar to the O(N * log(N)) time taken for comparison-based sorts.
54+
That's no co-incidence: One can take a list with N elements, separate it into N 1-element lists, then merge them with a kway-merge. That is a variant of merge sort.
55+
56+
However, compared to a comparison-based sort like quicksort, using a kway merge has the following differences:
57+
* Usually, we have I << N, and therefore, kway merge is usually faster.
58+
* For large I, quicksort is faster in practice because its overhead per element is smaller.
59+
5460
Note that Julia uses radix sort for integers, which sorts in O(N), and therefore usually beats a k-way merge.
5561

5662
## Contributing
5763
We appreciate contributions from users including reporting bugs, fixing
58-
issues, improving performance and adding new features.
64+
issues, improving performance and adding new fea oftentures.
5965

6066
Take a look at the [contributing files](https://github.com/BioJulia/Contributing)
6167
detailed contributor and maintainer guidelines, and code of conduct.

0 commit comments

Comments
 (0)