@@ -197,6 +197,8 @@ mod combinations_with_replacement;
197197mod exactly_one_err;
198198mod diff;
199199mod flatten_ok;
200+ #[ cfg( feature = "use_std" ) ]
201+ mod extrema_set;
200202mod format;
201203#[ cfg( feature = "use_std" ) ]
202204mod grouping_map;
@@ -2902,6 +2904,194 @@ pub trait Itertools : Iterator {
29022904 grouping_map:: new ( grouping_map:: MapForGrouping :: new ( self , key_mapper) )
29032905 }
29042906
2907+ /// Return all minimum elements of an iterator.
2908+ ///
2909+ /// # Examples
2910+ ///
2911+ /// ```
2912+ /// use itertools::Itertools;
2913+ ///
2914+ /// let a: [i32; 0] = [];
2915+ /// assert_eq!(a.iter().min_set(), Vec::<&i32>::new());
2916+ ///
2917+ /// let a = [1];
2918+ /// assert_eq!(a.iter().min_set(), vec![&1]);
2919+ ///
2920+ /// let a = [1, 2, 3, 4, 5];
2921+ /// assert_eq!(a.iter().min_set(), vec![&1]);
2922+ ///
2923+ /// let a = [1, 1, 1, 1];
2924+ /// assert_eq!(a.iter().min_set(), vec![&1, &1, &1, &1]);
2925+ /// ```
2926+ ///
2927+ /// The elements can be floats but no particular result is guaranteed
2928+ /// if an element is NaN.
2929+ #[ cfg( feature = "use_std" ) ]
2930+ fn min_set ( self ) -> Vec < Self :: Item >
2931+ where Self : Sized , Self :: Item : Ord
2932+ {
2933+ extrema_set:: min_set_impl ( self , |_| ( ) , |x, y, _, _| x. cmp ( y) )
2934+ }
2935+
2936+ /// Return all minimum elements of an iterator, as determined by
2937+ /// the specified function.
2938+ ///
2939+ /// # Examples
2940+ ///
2941+ /// ```
2942+ /// # use std::cmp::Ordering;
2943+ /// use itertools::Itertools;
2944+ ///
2945+ /// let a: [(i32, i32); 0] = [];
2946+ /// assert_eq!(a.iter().min_set_by(|_, _| Ordering::Equal), Vec::<&(i32, i32)>::new());
2947+ ///
2948+ /// let a = [(1, 2)];
2949+ /// assert_eq!(a.iter().min_set_by(|&&(k1,_), &&(k2, _)| k1.cmp(&k2)), vec![&(1, 2)]);
2950+ ///
2951+ /// let a = [(1, 2), (2, 2), (3, 9), (4, 8), (5, 9)];
2952+ /// assert_eq!(a.iter().min_set_by(|&&(_,k1), &&(_,k2)| k1.cmp(&k2)), vec![&(1, 2), &(2, 2)]);
2953+ ///
2954+ /// let a = [(1, 2), (1, 3), (1, 4), (1, 5)];
2955+ /// assert_eq!(a.iter().min_set_by(|&&(k1,_), &&(k2, _)| k1.cmp(&k2)), vec![&(1, 2), &(1, 3), &(1, 4), &(1, 5)]);
2956+ /// ```
2957+ ///
2958+ /// The elements can be floats but no particular result is guaranteed
2959+ /// if an element is NaN.
2960+ #[ cfg( feature = "use_std" ) ]
2961+ fn min_set_by < F > ( self , mut compare : F ) -> Vec < Self :: Item >
2962+ where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering
2963+ {
2964+ extrema_set:: min_set_impl (
2965+ self ,
2966+ |_| ( ) ,
2967+ |x, y, _, _| compare ( x, y)
2968+ )
2969+ }
2970+
2971+ /// Return all minimum elements of an iterator, as determined by
2972+ /// the specified function.
2973+ ///
2974+ /// # Examples
2975+ ///
2976+ /// ```
2977+ /// use itertools::Itertools;
2978+ ///
2979+ /// let a: [(i32, i32); 0] = [];
2980+ /// assert_eq!(a.iter().min_set_by_key(|_| ()), Vec::<&(i32, i32)>::new());
2981+ ///
2982+ /// let a = [(1, 2)];
2983+ /// assert_eq!(a.iter().min_set_by_key(|&&(k,_)| k), vec![&(1, 2)]);
2984+ ///
2985+ /// let a = [(1, 2), (2, 2), (3, 9), (4, 8), (5, 9)];
2986+ /// assert_eq!(a.iter().min_set_by_key(|&&(_, k)| k), vec![&(1, 2), &(2, 2)]);
2987+ ///
2988+ /// let a = [(1, 2), (1, 3), (1, 4), (1, 5)];
2989+ /// assert_eq!(a.iter().min_set_by_key(|&&(k, _)| k), vec![&(1, 2), &(1, 3), &(1, 4), &(1, 5)]);
2990+ /// ```
2991+ ///
2992+ /// The elements can be floats but no particular result is guaranteed
2993+ /// if an element is NaN.
2994+ #[ cfg( feature = "use_std" ) ]
2995+ fn min_set_by_key < K , F > ( self , key : F ) -> Vec < Self :: Item >
2996+ where Self : Sized , K : Ord , F : FnMut ( & Self :: Item ) -> K
2997+ {
2998+ extrema_set:: min_set_impl ( self , key, |_, _, kx, ky| kx. cmp ( ky) )
2999+ }
3000+
3001+ /// Return all maximum elements of an iterator.
3002+ ///
3003+ /// # Examples
3004+ ///
3005+ /// ```
3006+ /// use itertools::Itertools;
3007+ ///
3008+ /// let a: [i32; 0] = [];
3009+ /// assert_eq!(a.iter().max_set(), Vec::<&i32>::new());
3010+ ///
3011+ /// let a = [1];
3012+ /// assert_eq!(a.iter().max_set(), vec![&1]);
3013+ ///
3014+ /// let a = [1, 2, 3, 4, 5];
3015+ /// assert_eq!(a.iter().max_set(), vec![&5]);
3016+ ///
3017+ /// let a = [1, 1, 1, 1];
3018+ /// assert_eq!(a.iter().max_set(), vec![&1, &1, &1, &1]);
3019+ /// ```
3020+ ///
3021+ /// The elements can be floats but no particular result is guaranteed
3022+ /// if an element is NaN.
3023+ #[ cfg( feature = "use_std" ) ]
3024+ fn max_set ( self ) -> Vec < Self :: Item >
3025+ where Self : Sized , Self :: Item : Ord
3026+ {
3027+ extrema_set:: max_set_impl ( self , |_| ( ) , |x, y, _, _| x. cmp ( y) )
3028+ }
3029+
3030+ /// Return all maximum elements of an iterator, as determined by
3031+ /// the specified function.
3032+ ///
3033+ /// # Examples
3034+ ///
3035+ /// ```
3036+ /// # use std::cmp::Ordering;
3037+ /// use itertools::Itertools;
3038+ ///
3039+ /// let a: [(i32, i32); 0] = [];
3040+ /// assert_eq!(a.iter().max_set_by(|_, _| Ordering::Equal), Vec::<&(i32, i32)>::new());
3041+ ///
3042+ /// let a = [(1, 2)];
3043+ /// assert_eq!(a.iter().max_set_by(|&&(k1,_), &&(k2, _)| k1.cmp(&k2)), vec![&(1, 2)]);
3044+ ///
3045+ /// let a = [(1, 2), (2, 2), (3, 9), (4, 8), (5, 9)];
3046+ /// assert_eq!(a.iter().max_set_by(|&&(_,k1), &&(_,k2)| k1.cmp(&k2)), vec![&(3, 9), &(5, 9)]);
3047+ ///
3048+ /// let a = [(1, 2), (1, 3), (1, 4), (1, 5)];
3049+ /// assert_eq!(a.iter().max_set_by(|&&(k1,_), &&(k2, _)| k1.cmp(&k2)), vec![&(1, 2), &(1, 3), &(1, 4), &(1, 5)]);
3050+ /// ```
3051+ ///
3052+ /// The elements can be floats but no particular result is guaranteed
3053+ /// if an element is NaN.
3054+ #[ cfg( feature = "use_std" ) ]
3055+ fn max_set_by < F > ( self , mut compare : F ) -> Vec < Self :: Item >
3056+ where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering
3057+ {
3058+ extrema_set:: max_set_impl (
3059+ self ,
3060+ |_| ( ) ,
3061+ |x, y, _, _| compare ( x, y)
3062+ )
3063+ }
3064+
3065+ /// Return all minimum elements of an iterator, as determined by
3066+ /// the specified function.
3067+ ///
3068+ /// # Examples
3069+ ///
3070+ /// ```
3071+ /// use itertools::Itertools;
3072+ ///
3073+ /// let a: [(i32, i32); 0] = [];
3074+ /// assert_eq!(a.iter().max_set_by_key(|_| ()), Vec::<&(i32, i32)>::new());
3075+ ///
3076+ /// let a = [(1, 2)];
3077+ /// assert_eq!(a.iter().max_set_by_key(|&&(k,_)| k), vec![&(1, 2)]);
3078+ ///
3079+ /// let a = [(1, 2), (2, 2), (3, 9), (4, 8), (5, 9)];
3080+ /// assert_eq!(a.iter().max_set_by_key(|&&(_, k)| k), vec![&(3, 9), &(5, 9)]);
3081+ ///
3082+ /// let a = [(1, 2), (1, 3), (1, 4), (1, 5)];
3083+ /// assert_eq!(a.iter().max_set_by_key(|&&(k, _)| k), vec![&(1, 2), &(1, 3), &(1, 4), &(1, 5)]);
3084+ /// ```
3085+ ///
3086+ /// The elements can be floats but no particular result is guaranteed
3087+ /// if an element is NaN.
3088+ #[ cfg( feature = "use_std" ) ]
3089+ fn max_set_by_key < K , F > ( self , key : F ) -> Vec < Self :: Item >
3090+ where Self : Sized , K : Ord , F : FnMut ( & Self :: Item ) -> K
3091+ {
3092+ extrema_set:: max_set_impl ( self , key, |_, _, kx, ky| kx. cmp ( ky) )
3093+ }
3094+
29053095 /// Return the minimum and maximum elements in the iterator.
29063096 ///
29073097 /// The return type `MinMaxResult` is an enum of three variants:
0 commit comments