Skip to content

Commit

Permalink
Style tweaks and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
ejmount committed Feb 21, 2024
1 parent 626bdd4 commit 5c8f029
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
9 changes: 1 addition & 8 deletions src/k_smallest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where

let mut is_less_than = move |a: &_, b: &_| comparator(a, b) == Ordering::Less;

// Rearrange the into a valid heap by reordering from the second-bottom-most layer up
// Rearrange the into a valid heap by reordering from the second-bottom-most layer up to the root
// Slightly faster than ordering on each insert, but only by a factor of lg(k)
// The resulting heap has the **largest** item on top
for i in (0..=(storage.len() / 2)).rev() {
Expand Down Expand Up @@ -88,13 +88,6 @@ where
storage
}

pub(crate) fn reverse_cmp<T, F>(cmp: F) -> impl Fn(&T, &T) -> Ordering
where
F: Fn(&T, &T) -> Ordering,
{
move |a, b| cmp(b, a)
}

pub(crate) fn key_to_cmp<T, K, F>(key: F) -> impl Fn(&T, &T) -> Ordering
where
F: Fn(&T) -> K,
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,7 @@ pub trait Itertools: Iterator {
Self::Item: Ord,
{
// The stdlib heap has optimised handling of "holes", which is not included in our heap implementation in k_smallest_general.
// While the difference is unlikely to have practical impact unless `T` is very large, this method uses the stdlib structure
// While the difference is unlikely to have practical impact unless `Self::Item` is very large, this method uses the stdlib structure
// to maintain performance compared to previous versions of the crate.
use alloc::collections::BinaryHeap;

Expand Down Expand Up @@ -3011,7 +3011,7 @@ pub trait Itertools: Iterator {
/// Sort the k largest elements into a new iterator, in descending order.
/// Semantically equivalent to `k_smallest` with a reversed `Ord`
/// However, this is implemented by way of a custom binary heap
/// which does not have the same performance characteristics for very large `T`
/// which does not have the same performance characteristics for very large `Self::Item`
/// ```
/// use itertools::Itertools;
///
Expand All @@ -3030,7 +3030,7 @@ pub trait Itertools: Iterator {
Self: Sized,
Self::Item: Ord,
{
self.k_smallest_by(k, k_smallest::reverse_cmp(Self::Item::cmp))
self.k_largest_by(k, Self::Item::cmp)
}

/// Sort the k largest elements into a new iterator using the provided comparison.
Expand All @@ -3041,7 +3041,7 @@ pub trait Itertools: Iterator {
Self: Sized,
F: Fn(&Self::Item, &Self::Item) -> Ordering,
{
self.k_smallest_by(k, k_smallest::reverse_cmp(cmp))
self.k_smallest_by(k, move |a, b| cmp(b, a))
}

/// Return the elements producing the k largest outputs of the provided function
Expand Down

0 comments on commit 5c8f029

Please # to comment.