Skip to content

Commit 1af6c66

Browse files
Philippe-Choletphimuemue
authored andcommitted
Fuse the iterator in k_smallest
1 parent 8ed734b commit 1af6c66

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2949,7 +2949,7 @@ pub trait Itertools: Iterator {
29492949
/// itertools::assert_equal(five_smallest, 0..5);
29502950
/// ```
29512951
#[cfg(feature = "use_alloc")]
2952-
fn k_smallest(mut self, k: usize) -> VecIntoIter<Self::Item>
2952+
fn k_smallest(self, k: usize) -> VecIntoIter<Self::Item>
29532953
where
29542954
Self: Sized,
29552955
Self::Item: Ord,
@@ -2963,9 +2963,10 @@ pub trait Itertools: Iterator {
29632963
return Vec::new().into_iter();
29642964
}
29652965

2966-
let mut heap = self.by_ref().take(k).collect::<BinaryHeap<_>>();
2966+
let mut iter = self.fuse();
2967+
let mut heap: BinaryHeap<_> = iter.by_ref().take(k).collect();
29672968

2968-
self.for_each(|i| {
2969+
iter.for_each(|i| {
29692970
debug_assert_eq!(heap.len(), k);
29702971
// Equivalent to heap.push(min(i, heap.pop())) but more efficient.
29712972
// This should be done with a single `.peek_mut().unwrap()` but

0 commit comments

Comments
 (0)