@@ -359,7 +359,7 @@ where
359359 where
360360 V : Ord ,
361361 {
362- self . min_by ( |_ , v1 , v2| V :: cmp ( v1 , v2 ) )
362+ self . min_in ( HashMap :: new ( ) )
363363 }
364364
365365 /// Groups elements from the `GroupingMap` source by key and finds the minimum of each group
@@ -381,14 +381,11 @@ where
381381 /// assert_eq!(lookup[&2], 8);
382382 /// assert_eq!(lookup.len(), 3);
383383 /// ```
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 >
385385 where
386386 F : FnMut ( & K , & V , & V ) -> Ordering ,
387387 {
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 ( ) )
392389 }
393390
394391 /// Groups elements from the `GroupingMap` source by key and finds the element of each group
@@ -410,12 +407,12 @@ where
410407 /// assert_eq!(lookup[&2], 8);
411408 /// assert_eq!(lookup.len(), 3);
412409 /// ```
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 >
414411 where
415412 F : FnMut ( & K , & V ) -> CK ,
416413 CK : Ord ,
417414 {
418- self . min_by ( |key , v1 , v2| f ( key , v1 ) . cmp ( & f ( key , v2 ) ) )
415+ self . min_by_key_in ( f , HashMap :: new ( ) )
419416 }
420417
421418 /// Groups elements from the `GroupingMap` source by key and find the maximum and minimum of
@@ -708,4 +705,41 @@ where
708705 {
709706 self . max_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
710707 }
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+ }
711745}
0 commit comments