@@ -282,7 +282,7 @@ where
282
282
where
283
283
V : Ord ,
284
284
{
285
- self . max_by ( |_ , v1 , v2| V :: cmp ( v1 , v2 ) )
285
+ self . max_in ( HashMap :: new ( ) )
286
286
}
287
287
288
288
/// Groups elements from the `GroupingMap` source by key and finds the maximum of each group
@@ -304,14 +304,11 @@ where
304
304
/// assert_eq!(lookup[&2], 5);
305
305
/// assert_eq!(lookup.len(), 3);
306
306
/// ```
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 >
308
308
where
309
309
F : FnMut ( & K , & V , & V ) -> Ordering ,
310
310
{
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 ( ) )
315
312
}
316
313
317
314
/// Groups elements from the `GroupingMap` source by key and finds the element of each group
@@ -333,12 +330,12 @@ where
333
330
/// assert_eq!(lookup[&2], 5);
334
331
/// assert_eq!(lookup.len(), 3);
335
332
/// ```
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 >
337
334
where
338
335
F : FnMut ( & K , & V ) -> CK ,
339
336
CK : Ord ,
340
337
{
341
- self . max_by ( |key , v1 , v2| f ( key , v1 ) . cmp ( & f ( key , v2 ) ) )
338
+ self . max_by_key_in ( f , HashMap :: new ( ) )
342
339
}
343
340
344
341
/// Groups elements from the `GroupingMap` source by key and finds the minimum of each group.
@@ -667,4 +664,38 @@ where
667
664
668
665
map
669
666
}
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
+ }
670
701
}
0 commit comments