@@ -281,7 +281,7 @@ where
281
281
where
282
282
V : Ord ,
283
283
{
284
- self . max_by ( |_ , v1 , v2| V :: cmp ( v1 , v2 ) )
284
+ self . max_in ( HashMap :: new ( ) )
285
285
}
286
286
287
287
/// Groups elements from the `GroupingMap` source by key and finds the maximum of each group
@@ -303,14 +303,11 @@ where
303
303
/// assert_eq!(lookup[&2], 5);
304
304
/// assert_eq!(lookup.len(), 3);
305
305
/// ```
306
- pub fn max_by < F > ( self , mut compare : F ) -> HashMap < K , V >
306
+ pub fn max_by < F > ( self , compare : F ) -> HashMap < K , V >
307
307
where
308
308
F : FnMut ( & K , & V , & V ) -> Ordering ,
309
309
{
310
- self . reduce ( |acc, key, val| match compare ( key, & acc, & val) {
311
- Ordering :: Less | Ordering :: Equal => val,
312
- Ordering :: Greater => acc,
313
- } )
310
+ self . max_by_in ( compare, HashMap :: new ( ) )
314
311
}
315
312
316
313
/// Groups elements from the `GroupingMap` source by key and finds the element of each group
@@ -332,12 +329,12 @@ where
332
329
/// assert_eq!(lookup[&2], 5);
333
330
/// assert_eq!(lookup.len(), 3);
334
331
/// ```
335
- pub fn max_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , V >
332
+ pub fn max_by_key < F , CK > ( self , f : F ) -> HashMap < K , V >
336
333
where
337
334
F : FnMut ( & K , & V ) -> CK ,
338
335
CK : Ord ,
339
336
{
340
- self . max_by ( |key , v1 , v2| f ( key , v1 ) . cmp ( & f ( key , v2 ) ) )
337
+ self . max_by_key_in ( f , HashMap :: new ( ) )
341
338
}
342
339
343
340
/// Groups elements from the `GroupingMap` source by key and finds the minimum of each group.
@@ -674,4 +671,41 @@ where
674
671
675
672
map
676
673
}
674
+
675
+ /// Apply [`max`](Self::max) with a provided empty map
676
+ /// (`BTreeMap` or `HashMap` with any hasher).
677
+ pub fn max_in < M > ( self , map : M ) -> M
678
+ where
679
+ V : Ord ,
680
+ M : Map < Key = K , Value = V > ,
681
+ {
682
+ self . max_by_in ( |_, v1, v2| V :: cmp ( v1, v2) , map)
683
+ }
684
+
685
+ /// Apply [`max_by`](Self::max_by) with a provided empty map
686
+ /// (`BTreeMap` or `HashMap` with any hasher).
687
+ pub fn max_by_in < F , M > ( self , mut compare : F , map : M ) -> M
688
+ where
689
+ F : FnMut ( & K , & V , & V ) -> Ordering ,
690
+ M : Map < Key = K , Value = V > ,
691
+ {
692
+ self . reduce_in (
693
+ |acc, key, val| match compare ( key, & acc, & val) {
694
+ Ordering :: Less | Ordering :: Equal => val,
695
+ Ordering :: Greater => acc,
696
+ } ,
697
+ map,
698
+ )
699
+ }
700
+
701
+ /// Apply [`max_by_key`](Self::max_by_key) with a provided empty map
702
+ /// (`BTreeMap` or `HashMap` with any hasher).
703
+ pub fn max_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
704
+ where
705
+ F : FnMut ( & K , & V ) -> CK ,
706
+ CK : Ord ,
707
+ M : Map < Key = K , Value = V > ,
708
+ {
709
+ self . max_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
710
+ }
677
711
}
0 commit comments