@@ -282,7 +282,7 @@ where
282282 where
283283 V : Ord ,
284284 {
285- self . max_by ( |_ , v1 , v2| V :: cmp ( v1 , v2 ) )
285+ self . max_in ( HashMap :: new ( ) )
286286 }
287287
288288 /// Groups elements from the `GroupingMap` source by key and finds the maximum of each group
@@ -304,14 +304,11 @@ where
304304 /// assert_eq!(lookup[&2], 5);
305305 /// assert_eq!(lookup.len(), 3);
306306 /// ```
307- pub fn max_by < F > ( self , mut compare : F ) -> HashMap < K , V >
307+ pub fn max_by < F > ( self , compare : F ) -> HashMap < K , V >
308308 where
309309 F : FnMut ( & K , & V , & V ) -> Ordering ,
310310 {
311- self . reduce ( |acc, key, val| match compare ( key, & acc, & val) {
312- Ordering :: Less | Ordering :: Equal => val,
313- Ordering :: Greater => acc,
314- } )
311+ self . max_by_in ( compare, HashMap :: new ( ) )
315312 }
316313
317314 /// Groups elements from the `GroupingMap` source by key and finds the element of each group
@@ -333,12 +330,12 @@ where
333330 /// assert_eq!(lookup[&2], 5);
334331 /// assert_eq!(lookup.len(), 3);
335332 /// ```
336- pub fn max_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , V >
333+ pub fn max_by_key < F , CK > ( self , f : F ) -> HashMap < K , V >
337334 where
338335 F : FnMut ( & K , & V ) -> CK ,
339336 CK : Ord ,
340337 {
341- self . max_by ( |key , v1 , v2| f ( key , v1 ) . cmp ( & f ( key , v2 ) ) )
338+ self . max_by_key_in ( f , HashMap :: new ( ) )
342339 }
343340
344341 /// Groups elements from the `GroupingMap` source by key and finds the minimum of each group.
@@ -667,4 +664,38 @@ where
667664
668665 map
669666 }
667+
668+ /// Apply [`max`](Self::max) with a provided map.
669+ pub fn max_in < M > ( self , map : M ) -> M
670+ where
671+ V : Ord ,
672+ M : Map < Key = K , Value = V > ,
673+ {
674+ self . max_by_in ( |_, v1, v2| V :: cmp ( v1, v2) , map)
675+ }
676+
677+ /// Apply [`max_by`](Self::max_by) with a provided map.
678+ pub fn max_by_in < F , M > ( self , mut compare : F , map : M ) -> M
679+ where
680+ F : FnMut ( & K , & V , & V ) -> Ordering ,
681+ M : Map < Key = K , Value = V > ,
682+ {
683+ self . reduce_in (
684+ |acc, key, val| match compare ( key, & acc, & val) {
685+ Ordering :: Less | Ordering :: Equal => val,
686+ Ordering :: Greater => acc,
687+ } ,
688+ map,
689+ )
690+ }
691+
692+ /// Apply [`max_by_key`](Self::max_by_key) with a provided map.
693+ pub fn max_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
694+ where
695+ F : FnMut ( & K , & V ) -> CK ,
696+ CK : Ord ,
697+ M : Map < Key = K , Value = V > ,
698+ {
699+ self . max_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
700+ }
670701}
0 commit comments