@@ -447,7 +447,7 @@ where
447
447
where
448
448
V : Ord ,
449
449
{
450
- self . minmax_by ( |_ , v1 , v2| V :: cmp ( v1 , v2 ) )
450
+ self . minmax_in ( HashMap :: new ( ) )
451
451
}
452
452
453
453
/// Groups elements from the `GroupingMap` source by key and find the maximum and minimum of
@@ -473,32 +473,11 @@ where
473
473
/// assert_eq!(lookup[&2], OneElement(5));
474
474
/// assert_eq!(lookup.len(), 3);
475
475
/// ```
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 > >
477
477
where
478
478
F : FnMut ( & K , & V , & V ) -> Ordering ,
479
479
{
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 ( ) )
502
481
}
503
482
504
483
/// Groups elements from the `GroupingMap` source by key and find the elements of each group
@@ -524,12 +503,12 @@ where
524
503
/// assert_eq!(lookup[&2], OneElement(5));
525
504
/// assert_eq!(lookup.len(), 3);
526
505
/// ```
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 > >
528
507
where
529
508
F : FnMut ( & K , & V ) -> CK ,
530
509
CK : Ord ,
531
510
{
532
- self . minmax_by ( |key , v1 , v2| f ( key , v1 ) . cmp ( & f ( key , v2 ) ) )
511
+ self . minmax_by_key_in ( f , HashMap :: new ( ) )
533
512
}
534
513
535
514
/// Groups elements from the `GroupingMap` source by key and sums them.
@@ -729,4 +708,56 @@ where
729
708
{
730
709
self . min_by_in ( |key, v1, v2| f ( key, v1) . cmp ( & f ( key, v2) ) , map)
731
710
}
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
+ }
732
763
}
0 commit comments