3
3
use std:: collections:: HashMap ;
4
4
use std:: hash:: Hash ;
5
5
use std:: iter:: Iterator ;
6
- use alloc :: Vec ;
6
+ use HashMapIntoIter ;
7
7
8
8
/// Return a `HashMap` of keys mapped to a list of their corresponding values.
9
9
///
10
10
/// See [`.into_group_map()`](../trait.Itertools.html#method.into_group_map)
11
11
/// for more information.
12
12
pub fn into_group_map < I , K , V > ( iter : I ) -> HashMap < K , Vec < V > >
13
- where
14
- I : Iterator < Item = ( K , V ) > ,
15
- K : Hash + Eq ,
13
+ where
14
+ I : Iterator < Item = ( K , V ) > ,
15
+ K : Hash + Eq ,
16
16
{
17
17
let mut lookup = HashMap :: new ( ) ;
18
18
@@ -23,10 +23,10 @@ where
23
23
lookup
24
24
}
25
25
26
- pub fn into_group_map_by < I , K , V > ( iter : I , f : impl Fn ( & V ) -> K ) -> HashMap < K , Vec < V > >
27
- where
28
- I : Iterator < Item = V > ,
29
- K : Hash + Eq ,
26
+ pub fn into_group_map_by < I , K , V > ( iter : I , f : impl Fn ( & V ) -> K ) ->HashMapIntoIter < K , Vec < V > >
27
+ where
28
+ I : Iterator < Item = V > ,
29
+ K : Hash + Eq ,
30
30
{
31
31
let mut lookup = HashMap :: new ( ) ;
32
32
35
35
lookup. entry ( key) . or_insert ( Vec :: new ( ) ) . push ( val) ;
36
36
}
37
37
38
- lookup
38
+ lookup. into_iter ( )
39
39
}
40
40
41
- pub fn into_group_map_by_fold < I , K , V , Acc , Fold > (
42
- iter : impl Iterator < Item =V > ,
43
- f_key : impl Fn ( & V ) -> K ,
44
- init : Acc ,
45
- fold : Fold ,
46
- ) -> HashMap < K , Acc >
47
- where
48
- K : Hash + Eq ,
49
- Acc : Clone ,
50
- Fold : FnMut ( Acc , V ) -> Acc + Clone ,
51
- {
52
- let mut lookup = HashMap :: new ( ) ;
53
-
54
- for val in iter {
55
- let key = f_key ( & val) ;
56
- lookup. entry ( key) . or_insert ( Vec :: new ( ) ) . push ( val) ;
57
- }
58
-
59
- lookup
60
- . into_iter ( )
61
- . map ( move |( key, value) | ( key, value. into_iter ( ) . fold ( init. clone ( ) , fold. clone ( ) ) ) )
62
- . collect ( )
63
- }
0 commit comments