@@ -3792,7 +3792,36 @@ pub trait Itertools: Iterator {
3792
3792
Self : Iterator < Item = ( K , V ) > + Sized ,
3793
3793
K : Hash + Eq ,
3794
3794
{
3795
- group_map:: into_group_map ( self )
3795
+ group_map:: into_group_map_with_hasher ( self , RandomState :: new ( ) )
3796
+ }
3797
+
3798
+ /// Return a `HashMap` of keys mapped to `Vec`s of values, using the hash builder for hashing.
3799
+ /// See [.into_group_map()](crate::Itertools::into_group_map) for more information.
3800
+ ///
3801
+ /// Warning: `hash_builder` is normally randomly generated, and is designed to allow it's
3802
+ /// users to be resistant to attacks that cause many collisions and very poor performance.
3803
+ /// Setting it manually using this function can expose a DoS attack vector.
3804
+ ///
3805
+ /// ```
3806
+ /// use std::hash::RandomState;
3807
+ /// use itertools::Itertools;
3808
+ ///
3809
+ /// let data = vec![(0, 10), (2, 12), (3, 13), (0, 20), (3, 33), (2, 42)];
3810
+ /// let lookup = data.into_iter().into_group_map_with_hasher(RandomState::new());
3811
+ ///
3812
+ /// assert_eq!(lookup[&0], vec![10, 20]);
3813
+ /// assert_eq!(lookup.get(&1), None);
3814
+ /// assert_eq!(lookup[&2], vec![12, 42]);
3815
+ /// assert_eq!(lookup[&3], vec![13, 33]);
3816
+ /// ```
3817
+ #[ cfg( feature = "use_std" ) ]
3818
+ fn into_group_map_with_hasher < K , V , S > ( self , hash_builder : S ) -> HashMap < K , Vec < V > , S >
3819
+ where
3820
+ Self : Iterator < Item = ( K , V ) > + Sized ,
3821
+ K : Hash + Eq ,
3822
+ S : BuildHasher ,
3823
+ {
3824
+ group_map:: into_group_map_with_hasher ( self , hash_builder)
3796
3825
}
3797
3826
3798
3827
/// Return a `HashMap` of keys mapped to `Vec`s of values. The key is specified
@@ -3829,7 +3858,52 @@ pub trait Itertools: Iterator {
3829
3858
K : Hash + Eq ,
3830
3859
F : FnMut ( & V ) -> K ,
3831
3860
{
3832
- group_map:: into_group_map_by ( self , f)
3861
+ group_map:: into_group_map_by_with_hasher ( self , f, RandomState :: new ( ) )
3862
+ }
3863
+
3864
+ /// Return a `HashMap` of keys mapped to `Vec`s of values, using the hash builder for hashing.
3865
+ /// See [.into_group_map_by()](crate::Itertools::into_group_map_by) for more information.
3866
+ ///
3867
+ /// Warning: `hash_builder` is normally randomly generated, and is designed to allow it's
3868
+ /// users to be resistant to attacks that cause many collisions and very poor performance.
3869
+ /// Setting it manually using this function can expose a DoS attack vector.
3870
+ ///
3871
+ /// ```
3872
+ /// use itertools::Itertools;
3873
+ /// use std::collections::HashMap;
3874
+ /// use std::hash::RandomState;
3875
+ ///
3876
+ /// let data = vec![(0, 10), (2, 12), (3, 13), (0, 20), (3, 33), (2, 42)];
3877
+ /// let lookup: HashMap<u32,Vec<(u32, u32)>> =
3878
+ /// data.clone().into_iter().into_group_map_by_with_hasher(|a| a.0, RandomState::new());
3879
+ ///
3880
+ /// assert_eq!(lookup[&0], vec![(0,10), (0,20)]);
3881
+ /// assert_eq!(lookup.get(&1), None);
3882
+ /// assert_eq!(lookup[&2], vec![(2,12), (2,42)]);
3883
+ /// assert_eq!(lookup[&3], vec![(3,13), (3,33)]);
3884
+ ///
3885
+ /// assert_eq!(
3886
+ /// data.into_iter()
3887
+ /// .into_group_map_by_with_hasher(|x| x.0, RandomState::new())
3888
+ /// .into_iter()
3889
+ /// .map(|(key, values)| (key, values.into_iter().fold(0,|acc, (_,v)| acc + v )))
3890
+ /// .collect::<HashMap<u32,u32>>()[&0],
3891
+ /// 30,
3892
+ /// );
3893
+ /// ```
3894
+ #[ cfg( feature = "use_std" ) ]
3895
+ fn into_group_map_by_with_hasher < K , V , F , S > (
3896
+ self ,
3897
+ f : F ,
3898
+ hash_builder : S ,
3899
+ ) -> HashMap < K , Vec < V > , S >
3900
+ where
3901
+ Self : Iterator < Item = V > + Sized ,
3902
+ K : Hash + Eq ,
3903
+ F : FnMut ( & V ) -> K ,
3904
+ S : BuildHasher ,
3905
+ {
3906
+ group_map:: into_group_map_by_with_hasher ( self , f, hash_builder)
3833
3907
}
3834
3908
3835
3909
/// Constructs a `GroupingMap` to be used later with one of the efficient
0 commit comments