Coverage Report

Created: 2025-07-18 06:49

/rust/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.12.1/src/k_smallest.rs
Line
Count
Source (jump to first uncovered line)
1
use alloc::collections::BinaryHeap;
2
use core::cmp::Ord;
3
4
0
pub(crate) fn k_smallest<T: Ord, I: Iterator<Item = T>>(mut iter: I, k: usize) -> BinaryHeap<T> {
5
0
    if k == 0 {
6
0
        return BinaryHeap::new();
7
0
    }
8
0
9
0
    let mut heap = iter.by_ref().take(k).collect::<BinaryHeap<_>>();
10
0
11
0
    iter.for_each(|i| {
12
0
        debug_assert_eq!(heap.len(), k);
13
        // Equivalent to heap.push(min(i, heap.pop())) but more efficient.
14
        // This should be done with a single `.peek_mut().unwrap()` but
15
        //  `PeekMut` sifts-down unconditionally on Rust 1.46.0 and prior.
16
0
        if *heap.peek().unwrap() > i {
17
0
            *heap.peek_mut().unwrap() = i;
18
0
        }
19
0
    });
20
0
21
0
    heap
22
0
}