@@ -447,7 +447,7 @@ where
447447 where
448448 V : Ord ,
449449 {
450- self . minmax_by ( |_ , v1 , v2| V :: cmp ( v1 , v2 ) )
450+ self . minmax_in ( HashMap :: new ( ) )
451451 }
452452
453453 /// Groups elements from the `GroupingMap` source by key and find the maximum and minimum of
@@ -473,32 +473,11 @@ where
473473 /// assert_eq!(lookup[&2], OneElement(5));
474474 /// assert_eq!(lookup.len(), 3);
475475 /// ```
476- pub fn minmax_by < F > ( self , mut compare : F ) -> HashMap < K , MinMaxResult < V > >
476+ pub fn minmax_by < F > ( self , compare : F ) -> HashMap < K , MinMaxResult < V > >
477477 where
478478 F : FnMut ( & K , & V , & V ) -> Ordering ,
479479 {
480- self . aggregate ( |acc, key, val| {
481- Some ( match acc {
482- Some ( MinMaxResult :: OneElement ( e) ) => {
483- if compare ( key, & val, & e) == Ordering :: Less {
484- MinMaxResult :: MinMax ( val, e)
485- } else {
486- MinMaxResult :: MinMax ( e, val)
487- }
488- }
489- Some ( MinMaxResult :: MinMax ( min, max) ) => {
490- if compare ( key, & val, & min) == Ordering :: Less {
491- MinMaxResult :: MinMax ( val, max)
492- } else if compare ( key, & val, & max) != Ordering :: Less {
493- MinMaxResult :: MinMax ( min, val)
494- } else {
495- MinMaxResult :: MinMax ( min, max)
496- }
497- }
498- None => MinMaxResult :: OneElement ( val) ,
499- Some ( MinMaxResult :: NoElements ) => unreachable ! ( ) ,
500- } )
501- } )
480+ self . minmax_by_in ( compare, HashMap :: new ( ) )
502481 }
503482
504483 /// Groups elements from the `GroupingMap` source by key and find the elements of each group
@@ -524,12 +503,12 @@ where
524503 /// assert_eq!(lookup[&2], OneElement(5));
525504 /// assert_eq!(lookup.len(), 3);
526505 /// ```
527- pub fn minmax_by_key < F , CK > ( self , mut f : F ) -> HashMap < K , MinMaxResult < V > >
506+ pub fn minmax_by_key < F , CK > ( self , f : F ) -> HashMap < K , MinMaxResult < V > >
528507 where
529508 F : FnMut ( & K , & V ) -> CK ,
530509 CK : Ord ,
531510 {
532- self . minmax_by ( |key , v1 , v2| f ( key , v1 ) . cmp ( & f ( key , v2 ) ) )
511+ self . minmax_by_key_in ( f , HashMap :: new ( ) )
533512 }
534513
535514 /// Groups elements from the `GroupingMap` source by key and sums them.
@@ -729,4 +708,56 @@ where
729708 {
730709 self . min_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
731710 }
711+
712+ /// Apply [`minmax`](Self::minmax) with a provided map.
713+ pub fn minmax_in < M > ( self , map : M ) -> M
714+ where
715+ V : Ord ,
716+ M : Map < Key = K , Value = MinMaxResult < V > > ,
717+ {
718+ self . minmax_by_in ( |_, v1, v2| V :: cmp ( v1, v2) , map)
719+ }
720+
721+ /// Apply [`minmax_by`](Self::minmax_by) with a provided map.
722+ pub fn minmax_by_in < F , M > ( self , mut compare : F , map : M ) -> M
723+ where
724+ F : FnMut ( & K , & V , & V ) -> Ordering ,
725+ M : Map < Key = K , Value = MinMaxResult < V > > ,
726+ {
727+ self . aggregate_in (
728+ |acc, key, val| {
729+ Some ( match acc {
730+ Some ( MinMaxResult :: OneElement ( e) ) => {
731+ if compare ( key, & val, & e) == Ordering :: Less {
732+ MinMaxResult :: MinMax ( val, e)
733+ } else {
734+ MinMaxResult :: MinMax ( e, val)
735+ }
736+ }
737+ Some ( MinMaxResult :: MinMax ( min, max) ) => {
738+ if compare ( key, & val, & min) == Ordering :: Less {
739+ MinMaxResult :: MinMax ( val, max)
740+ } else if compare ( key, & val, & max) != Ordering :: Less {
741+ MinMaxResult :: MinMax ( min, val)
742+ } else {
743+ MinMaxResult :: MinMax ( min, max)
744+ }
745+ }
746+ None => MinMaxResult :: OneElement ( val) ,
747+ Some ( MinMaxResult :: NoElements ) => unreachable ! ( ) ,
748+ } )
749+ } ,
750+ map,
751+ )
752+ }
753+
754+ /// Apply [`minmax_by_key`](Self::minmax_by_key) with a provided map.
755+ pub fn minmax_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
756+ where
757+ F : FnMut ( & K , & V ) -> CK ,
758+ CK : Ord ,
759+ M : Map < Key = K , Value = MinMaxResult < V > > ,
760+ {
761+ self . minmax_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
762+ }
732763}
0 commit comments