Expand description
K-way merge iterator. Min-heap of stream heads; pop the minimum, advance that stream, push its next value if any.
Streams must be sorted ascending. Output is the global sorted union.
use subms_merge_iterator::MergeIterator;
let streams: Vec<Box<dyn Iterator<Item = i32>>> = vec![
Box::new(vec![1, 4, 7].into_iter()),
Box::new(vec![2, 5, 8].into_iter()),
Box::new(vec![3, 6, 9].into_iter()),
];
let mut merge = MergeIterator::new(streams);
assert_eq!(merge.peek(), Some(&1));
assert_eq!(merge.live_streams(), 3);
let merged: Vec<_> = merge.collect();
assert_eq!(merged, (1..=9).collect::<Vec<_>>());The iterator is a single-threaded cursor. It owns its sources, so it is
Send when they are, and there is no interior mutability to share across
threads: one merge per consumer.
Full writeup, design notes and measured benchmarks: https://www.submillisecond.com/cookbook/recipes/subms-merge-iterator
Re-exports§
pub use features::dedup::DedupEntry;pub use features::dedup::DedupMergeIterator;pub use features::priority::PriorityEntry;pub use features::priority::PriorityMergeIterator;pub use features::priority::PrioritySource;pub use features::reverse::ReverseMergeIterator;pub use features::seek::SeekableMergeIterator;pub use features::tombstones::TombstoneEntry;pub use features::tombstones::TombstoneMergeIterator;
Modules§
- features
- Opt-in feature catalog. Each submodule is gated by its own Cargo feature flag and adds a specific capability to the base k-way merge iterator without bloating the core build.
- recipe
SubMsRecipeimpl.