@@ -2008,12 +2008,7 @@ 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
+ self . max_by ( Ord :: cmp)
2017
2012
}
2018
2013
2019
2014
/// Returns the minimum element of an iterator.
@@ -2038,12 +2033,7 @@ pub trait Iterator {
2038
2033
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2039
2034
fn min ( self ) -> Option < Self :: Item > where Self : Sized , Self :: Item : Ord
2040
2035
{
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)
2036
+ self . min_by ( Ord :: cmp)
2047
2037
}
2048
2038
2049
2039
/// Returns the element that gives the maximum value from the
@@ -2062,15 +2052,11 @@ pub trait Iterator {
2062
2052
/// ```
2063
2053
#[ inline]
2064
2054
#[ stable( feature = "iter_cmp_by_key" , since = "1.6.0" ) ]
2065
- fn max_by_key < B : Ord , F > ( self , f : F ) -> Option < Self :: Item >
2055
+ fn max_by_key < B : Ord , F > ( self , mut f : F ) -> Option < Self :: Item >
2066
2056
where Self : Sized , F : FnMut ( & Self :: Item ) -> B ,
2067
2057
{
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)
2058
+ // switch to y even if it is only equal, to preserve stability.
2059
+ select_fold1 ( self . map ( |x| ( f ( & x) , x) ) , |( x_p, _) , ( y_p, _) | x_p <= y_p) . map ( |( _, x) | x)
2074
2060
}
2075
2061
2076
2062
/// Returns the element that gives the maximum value with respect to the
@@ -2092,12 +2078,8 @@ pub trait Iterator {
2092
2078
fn max_by < F > ( self , mut compare : F ) -> Option < Self :: Item >
2093
2079
where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
2094
2080
{
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)
2081
+ // switch to y even if it is only equal, to preserve stability.
2082
+ select_fold1 ( self , |x, y| compare ( x, y) != Ordering :: Greater )
2101
2083
}
2102
2084
2103
2085
/// Returns the element that gives the minimum value from the
@@ -2115,15 +2097,11 @@ pub trait Iterator {
2115
2097
/// assert_eq!(*a.iter().min_by_key(|x| x.abs()).unwrap(), 0);
2116
2098
/// ```
2117
2099
#[ stable( feature = "iter_cmp_by_key" , since = "1.6.0" ) ]
2118
- fn min_by_key < B : Ord , F > ( self , f : F ) -> Option < Self :: Item >
2100
+ fn min_by_key < B : Ord , F > ( self , mut f : F ) -> Option < Self :: Item >
2119
2101
where Self : Sized , F : FnMut ( & Self :: Item ) -> B ,
2120
2102
{
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)
2103
+ // only switch to y if it is strictly smaller, to preserve stability.
2104
+ select_fold1 ( self . map ( |x| ( f ( & x) , x) ) , |( x_p, _) , ( y_p, _) | x_p > y_p) . map ( |( _, x) | x)
2127
2105
}
2128
2106
2129
2107
/// Returns the element that gives the minimum value with respect to the
@@ -2145,12 +2123,8 @@ pub trait Iterator {
2145
2123
fn min_by < F > ( self , mut compare : F ) -> Option < Self :: Item >
2146
2124
where Self : Sized , F : FnMut ( & Self :: Item , & Self :: Item ) -> Ordering ,
2147
2125
{
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)
2126
+ // only switch to y if it is strictly smaller, to preserve stability.
2127
+ select_fold1 ( self , |x, y| compare ( x, y) == Ordering :: Greater )
2154
2128
}
2155
2129
2156
2130
@@ -2693,34 +2667,23 @@ pub trait Iterator {
2693
2667
}
2694
2668
}
2695
2669
2696
- /// Select an element from an iterator based on the given "projection "
2697
- /// and "comparison" function.
2670
+ /// Select an element from an iterator based on the given "comparison "
2671
+ /// function.
2698
2672
///
2699
2673
/// This is an idiosyncratic helper to try to factor out the
2700
2674
/// commonalities of {max,min}{,_by}. In particular, this avoids
2701
2675
/// having to implement optimizations several times.
2702
2676
#[ 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
2677
+ fn select_fold1 < I , F > ( mut it : I , mut f : F ) -> Option < I :: Item >
2678
+ where
2679
+ I : Iterator ,
2680
+ F : FnMut ( & I :: Item , & I :: Item ) -> bool ,
2709
2681
{
2710
2682
// start with the first element as our selection. This avoids
2711
2683
// having to use `Option`s inside the loop, translating to a
2712
2684
// sizeable performance gain (6x in one case).
2713
2685
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
- } )
2686
+ it. fold ( first, |sel, x| if f ( & sel, & x) { x } else { sel } )
2724
2687
} )
2725
2688
}
2726
2689
0 commit comments