#iterator #sorting #merging #elements #ordered #order #input

merging-iterator

An iterator to return the elements of two independently ordered iterators in order

4 stable releases

Uses old Rust 2015

1.3.0 Nov 18, 2020
1.2.0 Sep 15, 2019
1.1.0 Oct 14, 2018
1.0.0 Oct 14, 2018

#1091 in Algorithms

Download history 2/week @ 2024-08-24 10/week @ 2024-08-31 15/week @ 2024-09-07 15/week @ 2024-09-14 38/week @ 2024-09-21 25/week @ 2024-09-28 68/week @ 2024-10-05 25/week @ 2024-10-12 47/week @ 2024-10-19 12/week @ 2024-10-26 18/week @ 2024-11-02 4/week @ 2024-11-09 3/week @ 2024-11-16 14/week @ 2024-11-23 50/week @ 2024-11-30 36/week @ 2024-12-07

104 downloads per month

MIT/Apache

11KB
186 lines

Merging Iterator

Rust Crates.io docs.rs License License

This crate implements an iterator, that takes two independent iterators and returns their elements in order, given the two input iterators are sorted itself.

Important note: This iterator only works if the input iterators are already sorted since only the respective next elements of each iterator are compared. There are no checks in place to validate this property.

Example

extern crate merging_iterator;

use merging_iterator::MergeIter;
let a = vec![0, 2, 4, 6, 8];
let b = vec![1, 3, 5, 7, 9];
let merger = MergeIter::new(a, b);
assert_eq!(
    vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    merger.collect::<Vec<_>>()
);

You can also merge more than two sorted iterators like this:

extern crate merging_iterator;

use merging_iterator::MergeIter;
let a = vec![1, 4, 7];
let b = vec![2, 5, 8];
let c = vec![3, 6, 9];
let merger = MergeIter::new(
    MergeIter::new(a, b),
    c
);
assert_eq!(
    vec![1, 2, 3, 4, 5, 6, 7, 8, 9],
    merger.collect::<Vec<_>>()
);

License

merging-iterator is licensed under either of the following, at your option:

No runtime deps