@@ -2981,9 +2981,27 @@ pub trait Itertools: Iterator {
2981
2981
2982
2982
/// Sort the k smallest elements into a new iterator using the provided comparison.
2983
2983
///
2984
+ /// The sorted iterator, if directly collected to a `Vec`, is converted
2985
+ /// without any extra copying or allocation cost.
2986
+ ///
2984
2987
/// 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
+ ///
2986
2991
/// 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
+ /// ```
2987
3005
#[ cfg( feature = "use_alloc" ) ]
2988
3006
fn k_smallest_by < F > ( self , k : usize , cmp : F ) -> VecIntoIter < Self :: Item >
2989
3007
where
@@ -2993,10 +3011,29 @@ pub trait Itertools: Iterator {
2993
3011
k_smallest:: k_smallest_general ( self , k, cmp) . into_iter ( )
2994
3012
}
2995
3013
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.
2997
3015
///
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
+ /// ```
3000
3037
#[ cfg( feature = "use_alloc" ) ]
3001
3038
fn k_smallest_by_key < F , K > ( self , k : usize , key : F ) -> VecIntoIter < Self :: Item >
3002
3039
where
@@ -3008,9 +3045,15 @@ pub trait Itertools: Iterator {
3008
3045
}
3009
3046
3010
3047
/// 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
+ ///
3014
3057
/// ```
3015
3058
/// use itertools::Itertools;
3016
3059
///
@@ -3021,7 +3064,7 @@ pub trait Itertools: Iterator {
3021
3064
/// .into_iter()
3022
3065
/// .k_largest(5);
3023
3066
///
3024
- /// itertools::assert_equal(five_largest, vec![14,13,12,11,10]);
3067
+ /// itertools::assert_equal(five_largest, vec![14, 13, 12, 11, 10]);
3025
3068
/// ```
3026
3069
#[ cfg( feature = "use_alloc" ) ]
3027
3070
fn k_largest ( self , k : usize ) -> VecIntoIter < Self :: Item >
@@ -3033,7 +3076,25 @@ pub trait Itertools: Iterator {
3033
3076
}
3034
3077
3035
3078
/// 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
+ /// ```
3037
3098
#[ cfg( feature = "use_alloc" ) ]
3038
3099
fn k_largest_by < F > ( self , k : usize , cmp : F ) -> VecIntoIter < Self :: Item >
3039
3100
where
@@ -3043,7 +3104,26 @@ pub trait Itertools: Iterator {
3043
3104
self . k_smallest_by ( k, move |a, b| cmp ( b, a) )
3044
3105
}
3045
3106
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
+ /// ```
3047
3127
#[ cfg( feature = "use_alloc" ) ]
3048
3128
fn k_largest_by_key < F , K > ( self , k : usize , key : F ) -> VecIntoIter < Self :: Item >
3049
3129
where
0 commit comments