@@ -2008,12 +2008,8 @@ pub trait Iterator {
2008
2008
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2009
2009
fn max ( self ) -> Option < Self :: Item > where Self : Sized , Self :: Item : Ord
2010
2010
{
2011
- select_fold1 ( self ,
2012
- |_| ( ) ,
2013
- // switch to y even if it is only equal, to preserve
2014
- // stability.
2015
- |_, x, _, y| * x <= * y)
2016
- . map ( |( _, x) | x)
2011
+ // switch to y even if it is only equal, to preserve stability.
2012
+ select_fold1 ( self , |x, y| x <= y)
2017
2013
}
2018
2014
2019
2015
/// Returns the minimum element of an iterator.
@@ -2038,12 +2034,8 @@ pub trait Iterator {
2038
2034
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2039
2035
fn min ( self ) -> Option < Self :: Item > where Self : Sized , Self :: Item : Ord
2040
2036
{
2041
- select_fold1 ( self ,
2042
- |_| ( ) ,
2043
- // only switch to y if it is strictly smaller, to
2044
- // preserve stability.
2045
- |_, x, _, y| * x > * y)
2046
- . map ( |( _, x) | x)
2037
+ // only switch to y if it is strictly smaller, to preserve stability.
2038
+ select_fold1 ( self , |x, y| x > y)
2047
2039
}
2048
2040
2049
2041
/// Returns the element that gives the maximum value from the
@@ -2062,15 +2054,11 @@ pub trait Iterator {
2062
2054
/// ```
2063
2055
#[ inline]
2064
2056
#[ stable( feature = "iter_cmp_by_key" , since = "1.6.0" ) ]
2065
- fn max_by_key < B : Ord , F > ( self , f : F ) -> Option < Self :: Item >
2057
+ fn max_by_key < B : Ord , F > ( self , mut f : F ) -> Option < Self :: Item >
2066
2058
where Self : Sized , F : FnMut ( & Self :: Item ) -> B ,
2067
2059
{
2068
- select_fold1 ( self ,
2069
- f,
2070
- // switch to y even if it is only equal, to preserve
2071
- // stability.
2072
- |x_p, _, y_p, _| x_p <= y_p)
2073
- . map ( |( _, x) | x)
2060
+ // switch to y even if it is only equal, to preserve stability.
2061
+ select_fold1 ( self . map ( |x| ( f ( & x) , x) ) , |( x_p, _) , ( y_p, _) | x_p <= y_p) . map ( |( _, x) | x)
2074
2062
}
2075
2063
2076
2064
/// Returns the element that gives the maximum value with respect to the
@@ -2092,12 +2080,8 @@ pub trait Iterator {
2092
2080
fn max_by < F > ( self , mut compare : F ) -> Option < Self :: Item >
2093
2081
where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
2094
2082
{
2095
- select_fold1 ( self ,
2096
- |_| ( ) ,
2097
- // switch to y even if it is only equal, to preserve
2098
- // stability.
2099
- |_, x, _, y| Ordering :: Greater != compare ( x, y) )
2100
- . map ( |( _, x) | x)
2083
+ // switch to y even if it is only equal, to preserve stability.
2084
+ select_fold1 ( self , |x, y| compare ( x, y) != Ordering :: Greater )
2101
2085
}
2102
2086
2103
2087
/// Returns the element that gives the minimum value from the
@@ -2115,15 +2099,11 @@ pub trait Iterator {
2115
2099
/// assert_eq!(*a.iter().min_by_key(|x| x.abs()).unwrap(), 0);
2116
2100
/// ```
2117
2101
#[ stable( feature = "iter_cmp_by_key" , since = "1.6.0" ) ]
2118
- fn min_by_key < B : Ord , F > ( self , f : F ) -> Option < Self :: Item >
2102
+ fn min_by_key < B : Ord , F > ( self , mut f : F ) -> Option < Self :: Item >
2119
2103
where Self : Sized , F : FnMut ( & Self :: Item ) -> B ,
2120
2104
{
2121
- select_fold1 ( self ,
2122
- f,
2123
- // only switch to y if it is strictly smaller, to
2124
- // preserve stability.
2125
- |x_p, _, y_p, _| x_p > y_p)
2126
- . map ( |( _, x) | x)
2105
+ // only switch to y if it is strictly smaller, to preserve stability.
2106
+ select_fold1 ( self . map ( |x| ( f ( & x) , x) ) , |( x_p, _) , ( y_p, _) | x_p > y_p) . map ( |( _, x) | x)
2127
2107
}
2128
2108
2129
2109
/// Returns the element that gives the minimum value with respect to the
@@ -2145,12 +2125,8 @@ pub trait Iterator {
2145
2125
fn min_by < F > ( self , mut compare : F ) -> Option < Self :: Item >
2146
2126
where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
2147
2127
{
2148
- select_fold1 ( self ,
2149
- |_| ( ) ,
2150
- // switch to y even if it is strictly smaller, to
2151
- // preserve stability.
2152
- |_, x, _, y| Ordering :: Greater == compare ( x, y) )
2153
- . map ( |( _, x) | x)
2128
+ // switch to y even if it is strictly smaller, to preserve stability.
2129
+ select_fold1 ( self , |x, y| compare ( x, y) == Ordering :: Greater )
2154
2130
}
2155
2131
2156
2132
@@ -2693,34 +2669,23 @@ pub trait Iterator {
2693
2669
}
2694
2670
}
2695
2671
2696
- /// Select an element from an iterator based on the given "projection "
2697
- /// and "comparison" function.
2672
+ /// Select an element from an iterator based on the given "comparison "
2673
+ /// function.
2698
2674
///
2699
2675
/// This is an idiosyncratic helper to try to factor out the
2700
2676
/// commonalities of {max,min}{,_by}. In particular, this avoids
2701
2677
/// having to implement optimizations several times.
2702
2678
#[ inline]
2703
- fn select_fold1 < I , B , FProj , FCmp > ( mut it : I ,
2704
- mut f_proj : FProj ,
2705
- mut f_cmp : FCmp ) -> Option < ( B , I :: Item ) >
2706
- where I : Iterator ,
2707
- FProj : FnMut ( & I :: Item ) -> B ,
2708
- FCmp : FnMut ( & B , & I :: Item , & B , & I :: Item ) -> bool
2679
+ fn select_fold1 < I , F > ( mut it : I , mut f : F ) -> Option < I :: Item >
2680
+ where
2681
+ I : Iterator ,
2682
+ F : FnMut ( & I :: Item , & I :: Item ) -> bool ,
2709
2683
{
2710
2684
// start with the first element as our selection. This avoids
2711
2685
// having to use `Option`s inside the loop, translating to a
2712
2686
// sizeable performance gain (6x in one case).
2713
2687
it. next ( ) . map ( |first| {
2714
- let first_p = f_proj ( & first) ;
2715
-
2716
- it. fold ( ( first_p, first) , |( sel_p, sel) , x| {
2717
- let x_p = f_proj ( & x) ;
2718
- if f_cmp ( & sel_p, & sel, & x_p, & x) {
2719
- ( x_p, x)
2720
- } else {
2721
- ( sel_p, sel)
2722
- }
2723
- } )
2688
+ it. fold ( first, |sel, x| if f ( & sel, & x) { x } else { sel } )
2724
2689
} )
2725
2690
}
2726
2691
0 commit comments