Skip to content

Commit

Permalink
Using alloc instead of std
Browse files Browse the repository at this point in the history
  • Loading branch information
ejmount committed Oct 20, 2022
1 parent 8966757 commit e5a7709
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/k_smallest.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use core::cmp::{Ord, Ordering, Reverse};
use core::mem::{replace, transmute, MaybeUninit};
use alloc::vec::IntoIter;

fn k_smallest_dynamic<T, I: Iterator<Item = T>>(
iter: I,
k: usize,
order: impl Fn(&T, &T) -> Ordering,
) -> std::vec::IntoIter<T> {
) -> IntoIter<T> {
let mut storage = Vec::new();
storage.resize_with(k, MaybeUninit::uninit);
let num_elements = capped_heapsort(iter, &mut storage, order);
Expand All @@ -31,19 +32,19 @@ where
pub(crate) fn k_smallest<T: Ord, I: Iterator<Item = T>>(
iter: I,
k: usize,
) -> std::vec::IntoIter<T> {
) -> IntoIter<T> {
k_smallest_dynamic(iter, k, T::cmp)
}

pub(crate) fn k_smallest_by<T, I, F>(iter: I, k: usize, cmp: F) -> std::vec::IntoIter<T>
pub(crate) fn k_smallest_by<T, I, F>(iter: I, k: usize, cmp: F) -> IntoIter<T>
where
I: Iterator<Item = T>,
F: Fn(&T, &T) -> Ordering,
{
k_smallest_dynamic(iter, k, cmp)
}

pub(crate) fn k_smallest_by_key<T, I, F, K>(iter: I, k: usize, key: F) -> std::vec::IntoIter<T>
pub(crate) fn k_smallest_by_key<T, I, F, K>(iter: I, k: usize, key: F) -> IntoIter<T>
where
I: Iterator<Item = T>,
F: Fn(&T) -> K,
Expand Down

0 comments on commit e5a7709

Please sign in to comment.