diff options
Diffstat (limited to 'src/k_smallest.rs')
-rw-r--r-- | src/k_smallest.rs | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/k_smallest.rs b/src/k_smallest.rs index d58ec70..acaea59 100644 --- a/src/k_smallest.rs +++ b/src/k_smallest.rs @@ -6,7 +6,7 @@ pub(crate) fn k_smallest<T: Ord, I: Iterator<Item = T>>(mut iter: I, k: usize) - let mut heap = iter.by_ref().take(k).collect::<BinaryHeap<_>>(); - for i in iter { + iter.for_each(|i| { debug_assert_eq!(heap.len(), k); // Equivalent to heap.push(min(i, heap.pop())) but more efficient. // This should be done with a single `.peek_mut().unwrap()` but @@ -14,7 +14,7 @@ pub(crate) fn k_smallest<T: Ord, I: Iterator<Item = T>>(mut iter: I, k: usize) - if *heap.peek().unwrap() > i { *heap.peek_mut().unwrap() = i; } - } + }); heap } |