Skip to content

Commit 5eb29cf

Browse files
k_smallest variants: update documentation
1 parent 16ce601 commit 5eb29cf

File tree

1 file changed

+90
-10
lines changed

1 file changed

+90
-10
lines changed

src/lib.rs

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,9 +2981,27 @@ pub trait Itertools: Iterator {
29812981

29822982
/// Sort the k smallest elements into a new iterator using the provided comparison.
29832983
///
2984+
/// The sorted iterator, if directly collected to a `Vec`, is converted
2985+
/// without any extra copying or allocation cost.
2986+
///
29842987
/// This corresponds to `self.sorted_by(cmp).take(k)` in the same way that
2985-
/// [Itertools::k_smallest] corresponds to `self.sorted().take(k)`, in both semantics and complexity.
2988+
/// [`k_smallest`](Itertools::k_smallest) corresponds to `self.sorted().take(k)`,
2989+
/// in both semantics and complexity.
2990+
///
29862991
/// Particularly, a custom heap implementation ensures the comparison is not cloned.
2992+
///
2993+
/// ```
2994+
/// use itertools::Itertools;
2995+
///
2996+
/// // A random permutation of 0..15
2997+
/// let numbers = vec![6, 9, 1, 14, 0, 4, 8, 7, 11, 2, 10, 3, 13, 12, 5];
2998+
///
2999+
/// let five_smallest = numbers
3000+
/// .into_iter()
3001+
/// .k_smallest_by(5, |a, b| (a % 7).cmp(&(b % 7)).then(a.cmp(b)));
3002+
///
3003+
/// itertools::assert_equal(five_smallest, vec![0, 7, 14, 1, 8]);
3004+
/// ```
29873005
#[cfg(feature = "use_alloc")]
29883006
fn k_smallest_by<F>(self, k: usize, cmp: F) -> VecIntoIter<Self::Item>
29893007
where
@@ -2993,10 +3011,29 @@ pub trait Itertools: Iterator {
29933011
k_smallest::k_smallest_general(self, k, cmp).into_iter()
29943012
}
29953013

2996-
/// Return the elements producing the k smallest outputs of the provided function
3014+
/// Return the elements producing the k smallest outputs of the provided function.
29973015
///
2998-
/// This corresponds to `self.sorted_by_key(cmp).take(k)` in the same way that
2999-
/// [Itertools::k_smallest] corresponds to `self.sorted().take(k)`, in both semantics and time complexity.
3016+
/// The sorted iterator, if directly collected to a `Vec`, is converted
3017+
/// without any extra copying or allocation cost.
3018+
///
3019+
/// This corresponds to `self.sorted_by_key(key).take(k)` in the same way that
3020+
/// [`k_smallest`](Itertools::k_smallest) corresponds to `self.sorted().take(k)`,
3021+
/// in both semantics and complexity.
3022+
///
3023+
/// Particularly, a custom heap implementation ensures the comparison is not cloned.
3024+
///
3025+
/// ```
3026+
/// use itertools::Itertools;
3027+
///
3028+
/// // A random permutation of 0..15
3029+
/// let numbers = vec![6, 9, 1, 14, 0, 4, 8, 7, 11, 2, 10, 3, 13, 12, 5];
3030+
///
3031+
/// let five_smallest = numbers
3032+
/// .into_iter()
3033+
/// .k_smallest_by_key(5, |n| (n % 7, *n));
3034+
///
3035+
/// itertools::assert_equal(five_smallest, vec![0, 7, 14, 1, 8]);
3036+
/// ```
30003037
#[cfg(feature = "use_alloc")]
30013038
fn k_smallest_by_key<F, K>(self, k: usize, key: F) -> VecIntoIter<Self::Item>
30023039
where
@@ -3008,9 +3045,15 @@ pub trait Itertools: Iterator {
30083045
}
30093046

30103047
/// Sort the k largest elements into a new iterator, in descending order.
3011-
/// Semantically equivalent to `k_smallest` with a reversed `Ord`
3012-
/// However, this is implemented by way of a custom binary heap
3013-
/// which does not have the same performance characteristics for very large `Self::Item`
3048+
///
3049+
/// The sorted iterator, if directly collected to a `Vec`, is converted
3050+
/// without any extra copying or allocation cost.
3051+
///
3052+
/// It is semantically equivalent to [`k_smallest`](Itertools::k_smallest)
3053+
/// with a reversed `Ord`.
3054+
/// However, this is implemented with a custom binary heap which does not
3055+
/// have the same performance characteristics for very large `Self::Item`.
3056+
///
30143057
/// ```
30153058
/// use itertools::Itertools;
30163059
///
@@ -3021,7 +3064,7 @@ pub trait Itertools: Iterator {
30213064
/// .into_iter()
30223065
/// .k_largest(5);
30233066
///
3024-
/// itertools::assert_equal(five_largest, vec![14,13,12,11,10]);
3067+
/// itertools::assert_equal(five_largest, vec![14, 13, 12, 11, 10]);
30253068
/// ```
30263069
#[cfg(feature = "use_alloc")]
30273070
fn k_largest(self, k: usize) -> VecIntoIter<Self::Item>
@@ -3033,7 +3076,25 @@ pub trait Itertools: Iterator {
30333076
}
30343077

30353078
/// Sort the k largest elements into a new iterator using the provided comparison.
3036-
/// Functionally equivalent to `k_smallest_by` with a reversed `Ord`
3079+
///
3080+
/// The sorted iterator, if directly collected to a `Vec`, is converted
3081+
/// without any extra copying or allocation cost.
3082+
///
3083+
/// Functionally equivalent to [`k_smallest_by`](Itertools::k_smallest_by)
3084+
/// with a reversed `Ord`.
3085+
///
3086+
/// ```
3087+
/// use itertools::Itertools;
3088+
///
3089+
/// // A random permutation of 0..15
3090+
/// let numbers = vec![6, 9, 1, 14, 0, 4, 8, 7, 11, 2, 10, 3, 13, 12, 5];
3091+
///
3092+
/// let five_largest = numbers
3093+
/// .into_iter()
3094+
/// .k_largest_by(5, |a, b| (a % 7).cmp(&(b % 7)).then(a.cmp(b)));
3095+
///
3096+
/// itertools::assert_equal(five_largest, vec![13, 6, 12, 5, 11]);
3097+
/// ```
30373098
#[cfg(feature = "use_alloc")]
30383099
fn k_largest_by<F>(self, k: usize, cmp: F) -> VecIntoIter<Self::Item>
30393100
where
@@ -3043,7 +3104,26 @@ pub trait Itertools: Iterator {
30433104
self.k_smallest_by(k, move |a, b| cmp(b, a))
30443105
}
30453106

3046-
/// Return the elements producing the k largest outputs of the provided function
3107+
/// Return the elements producing the k largest outputs of the provided function.
3108+
///
3109+
/// The sorted iterator, if directly collected to a `Vec`, is converted
3110+
/// without any extra copying or allocation cost.
3111+
///
3112+
/// Functionally equivalent to [`k_smallest_by_key`](Itertools::k_smallest_by_key)
3113+
/// with a reversed `Ord`.
3114+
///
3115+
/// ```
3116+
/// use itertools::Itertools;
3117+
///
3118+
/// // A random permutation of 0..15
3119+
/// let numbers = vec![6, 9, 1, 14, 0, 4, 8, 7, 11, 2, 10, 3, 13, 12, 5];
3120+
///
3121+
/// let five_largest = numbers
3122+
/// .into_iter()
3123+
/// .k_largest_by_key(5, |n| (n % 7, *n));
3124+
///
3125+
/// itertools::assert_equal(five_largest, vec![13, 6, 12, 5, 11]);
3126+
/// ```
30473127
#[cfg(feature = "use_alloc")]
30483128
fn k_largest_by_key<F, K>(self, k: usize, key: F) -> VecIntoIter<Self::Item>
30493129
where

0 commit comments

Comments
 (0)