@@ -359,7 +359,7 @@ where
359
359
where
360
360
V : Ord ,
361
361
{
362
- self . min_by ( |_ , v1 , v2| V :: cmp ( v1 , v2 ) )
362
+ self . min_in ( HashMap :: new ( ) )
363
363
}
364
364
365
365
/// Groups elements from the `GroupingMap` source by key and finds the minimum of each group
@@ -381,14 +381,11 @@ where
381
381
/// assert_eq!(lookup[&2], 8);
382
382
/// assert_eq!(lookup.len(), 3);
383
383
/// ```
384
- pub fn min_by < F > ( self , mut compare : F ) -> HashMap < K , V >
384
+ pub fn min_by < F > ( self , compare : F ) -> HashMap < K , V >
385
385
where
386
386
F : FnMut ( & K , & V , & V ) -> Ordering ,
387
387
{
388
- self . reduce ( |acc, key, val| match compare ( key, & acc, & val) {
389
- Ordering :: Less | Ordering :: Equal => acc,
390
- Ordering :: Greater => val,
391
- } )
388
+ self . min_by_in ( compare, HashMap :: new ( ) )
392
389
}
393
390
394
391
/// Groups elements from the `GroupingMap` source by key and finds the element of each group
@@ -410,12 +407,12 @@ where
410
407
/// assert_eq!(lookup[&2], 8);
411
408
/// assert_eq!(lookup.len(), 3);
412
409
/// ```
413
- pub fn min_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , V >
410
+ pub fn min_by_key < F , CK > ( self , f : F ) -> HashMap < K , V >
414
411
where
415
412
F : FnMut ( & K , & V ) -> CK ,
416
413
CK : Ord ,
417
414
{
418
- self . min_by ( |key , v1 , v2| f ( key , v1 ) . cmp ( & f ( key , v2 ) ) )
415
+ self . min_by_key_in ( f , HashMap :: new ( ) )
419
416
}
420
417
421
418
/// Groups elements from the `GroupingMap` source by key and find the maximum and minimum of
@@ -708,4 +705,41 @@ where
708
705
{
709
706
self . max_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
710
707
}
708
+
709
+ /// Apply [`min`](Self::min) with a provided empty map
710
+ /// (`BTreeMap` or `HashMap` with any hasher).
711
+ pub fn min_in < M > ( self , map : M ) -> M
712
+ where
713
+ V : Ord ,
714
+ M : Map < Key = K , Value = V > ,
715
+ {
716
+ self . min_by_in ( |_, v1, v2| V :: cmp ( v1, v2) , map)
717
+ }
718
+
719
+ /// Apply [`min_by`](Self::min_by) with a provided empty map
720
+ /// (`BTreeMap` or `HashMap` with any hasher).
721
+ pub fn min_by_in < F , M > ( self , mut compare : F , map : M ) -> M
722
+ where
723
+ F : FnMut ( & K , & V , & V ) -> Ordering ,
724
+ M : Map < Key = K , Value = V > ,
725
+ {
726
+ self . reduce_in (
727
+ |acc, key, val| match compare ( key, & acc, & val) {
728
+ Ordering :: Less | Ordering :: Equal => acc,
729
+ Ordering :: Greater => val,
730
+ } ,
731
+ map,
732
+ )
733
+ }
734
+
735
+ /// Apply [`min_by_key`](Self::min_by_key) with a provided empty map
736
+ /// (`BTreeMap` or `HashMap` with any hasher).
737
+ pub fn min_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
738
+ where
739
+ F : FnMut ( & K , & V ) -> CK ,
740
+ CK : Ord ,
741
+ M : Map < Key = K , Value = V > ,
742
+ {
743
+ self . min_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
744
+ }
711
745
}
0 commit comments