window-sort-iterator

Crates.io Crates.io Released API docs

An iterator adapter that sorts items within a sliding window.

Implementation

This keeps a BinaryHeap of the items within a sliding window on top of an iterator. The algorithm works like this: - As long as the window is not full, requests elements from the underlying iterator and inserts them into the heap. - If the window is full, pops the next sorted element from the heap. - If the underlying iterator does not produce any more items, drains the remaining elements in-order from the heap.

By default, this uses a max-heap, which results in the highest item being yielded first. You can use a min-heap by wrapping items with std::cmp::Reverse.

Usage

```rust use windowsortiterator::WindowSortIterExt;

let a = &[4, 2, 3, 1]; let mut it = a.iter().cloned().windowsort(2); asserteq!(Some(4), it.next()); asserteq!(Some(3), it.next()); asserteq!(Some(2), it.next()); asserteq!(Some(1), it.next()); asserteq!(None, it.next()); ```

License

MIT, see LICENSE.