/rust/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.11.0/src/group_map.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #![cfg(feature = "use_std")] |
2 | | |
3 | | use std::collections::HashMap; |
4 | | use std::hash::Hash; |
5 | | use std::iter::Iterator; |
6 | | |
7 | | /// Return a `HashMap` of keys mapped to a list of their corresponding values. |
8 | | /// |
9 | | /// See [`.into_group_map()`](crate::Itertools::into_group_map) |
10 | | /// for more information. |
11 | 0 | pub fn into_group_map<I, K, V>(iter: I) -> HashMap<K, Vec<V>> |
12 | 0 | where I: Iterator<Item=(K, V)>, |
13 | 0 | K: Hash + Eq, |
14 | 0 | { |
15 | 0 | let mut lookup = HashMap::new(); |
16 | 0 |
|
17 | 0 | iter.for_each(|(key, val)| { |
18 | 0 | lookup.entry(key).or_insert_with(Vec::new).push(val); |
19 | 0 | }); |
20 | 0 |
|
21 | 0 | lookup |
22 | 0 | } |
23 | | |
24 | 0 | pub fn into_group_map_by<I, K, V>(iter: I, f: impl Fn(&V) -> K) -> HashMap<K, Vec<V>> |
25 | 0 | where |
26 | 0 | I: Iterator<Item=V>, |
27 | 0 | K: Hash + Eq, |
28 | 0 | { |
29 | 0 | into_group_map( |
30 | 0 | iter.map(|v| (f(&v), v)) |
31 | 0 | ) |
32 | 0 | } |