Skip to content

Commit 821df5b

Browse files
committed
Removing redundant internal functions
1 parent f18901f commit 821df5b

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

src/k_smallest.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use alloc::vec::IntoIter;
2-
use core::cmp::{Ord, Ordering, Reverse};
2+
use core::cmp::{Ordering, Reverse};
33

44
/// Consumes a given iterator, returning the minimum elements in **ascending** order.
5-
fn k_smallest_general<T, I: Iterator<Item = T>>(
5+
pub(crate) fn k_smallest_general<T, I: Iterator<Item = T>>(
66
mut iter: I,
77
k: usize,
88
mut comparator: impl FnMut(&T, &T) -> Ordering,
@@ -57,36 +57,6 @@ where
5757
move |a| Reverse(f(a))
5858
}
5959

60-
pub(crate) fn k_smallest<T, I>(iter: I, k: usize) -> IntoIter<T>
61-
where
62-
T: Ord,
63-
I: Iterator<Item = T>,
64-
{
65-
k_smallest_general(iter, k, T::cmp)
66-
}
67-
68-
pub(crate) fn k_smallest_by<T, I, F>(iter: I, k: usize, cmp: F) -> IntoIter<T>
69-
where
70-
I: Iterator<Item = T>,
71-
F: Fn(&T, &T) -> Ordering,
72-
{
73-
k_smallest_general(iter, k, cmp)
74-
}
75-
76-
pub(crate) fn k_smallest_by_key<T, I, F, K>(iter: I, k: usize, key: F) -> IntoIter<T>
77-
where
78-
I: Iterator<Item = T>,
79-
F: Fn(&T) -> K,
80-
K: Ord,
81-
{
82-
let iter = iter.map(|v| (key(&v), v));
83-
84-
let results: Vec<_> = k_smallest_general(iter, k, |(k, _), (l, _)| k.cmp(&l))
85-
.map(|(_, t)| t)
86-
.collect();
87-
results.into_iter()
88-
}
89-
9060
/// Sift the element currently at `origin` **away** from the root until it is properly ordered
9161
fn sift_down<T>(
9262
heap: &mut [T],

src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,7 +2737,7 @@ pub trait Itertools : Iterator {
27372737
Self: Sized,
27382738
Self::Item: Ord,
27392739
{
2740-
crate::k_smallest::k_smallest(self, k)
2740+
crate::k_smallest::k_smallest_general(self, k, Self::Item::cmp)
27412741
}
27422742

27432743
/// Sort the k smallest elements into a new iterator using the provided comparison.
@@ -2751,7 +2751,7 @@ pub trait Itertools : Iterator {
27512751
Self: Sized,
27522752
F: Fn(&Self::Item, &Self::Item) -> Ordering,
27532753
{
2754-
crate::k_smallest::k_smallest_by(self, k, cmp)
2754+
crate::k_smallest::k_smallest_general(self, k, cmp)
27552755
}
27562756

27572757
/// Return the elements producing the k smallest outputs of the provided function
@@ -2766,7 +2766,13 @@ pub trait Itertools : Iterator {
27662766
F: Fn(&Self::Item) -> K,
27672767
K: Ord,
27682768
{
2769-
crate::k_smallest::k_smallest_by_key(self, k, key)
2769+
let iter = self.map(|v| (key(&v), v));
2770+
2771+
let results: Vec<_> =
2772+
crate::k_smallest::k_smallest_general(iter, k, |(k, _), (l, _)| k.cmp(&l))
2773+
.map(|(_, t)| t)
2774+
.collect();
2775+
results.into_iter()
27702776
}
27712777

27722778
/// Sort the k largest elements into a new iterator, in descending order.
@@ -2789,7 +2795,7 @@ pub trait Itertools : Iterator {
27892795
Self: Sized,
27902796
Self::Item: Ord,
27912797
{
2792-
crate::k_smallest::k_smallest_by(self, k, k_smallest::reverse_cmp(Self::Item::cmp))
2798+
self.k_smallest_by(k, k_smallest::reverse_cmp(Self::Item::cmp))
27932799
}
27942800

27952801
/// Sort the k largest elements into a new iterator using the provided comparison.
@@ -2800,7 +2806,7 @@ pub trait Itertools : Iterator {
28002806
Self: Sized,
28012807
F: Fn(&Self::Item, &Self::Item) -> Ordering,
28022808
{
2803-
crate::k_smallest::k_smallest_by(self, k, k_smallest::reverse_cmp(cmp))
2809+
self.k_smallest_by(k, k_smallest::reverse_cmp(cmp))
28042810
}
28052811

28062812
/// Return the elements producing the k largest outputs of the provided function
@@ -2812,7 +2818,7 @@ pub trait Itertools : Iterator {
28122818
K: Ord,
28132819
{
28142820
let key = crate::k_smallest::reverse_key(key);
2815-
crate::k_smallest::k_smallest_by_key(self, k, key)
2821+
self.k_smallest_by_key(k, key)
28162822
}
28172823

28182824
/// Collect all iterator elements into one of two

0 commit comments

Comments
 (0)