/rust/registry/src/index.crates.io-1949cf8c6b5b557f/itertools-0.15.0/src/group_map.rs
Line | Count | Source |
1 | | #![cfg(feature = "use_std")] |
2 | | |
3 | | use core::hash::BuildHasher; |
4 | | use std::collections::HashMap; |
5 | | use std::hash::Hash; |
6 | | use std::iter::Iterator; |
7 | | |
8 | | /// Return a `HashMap` of keys mapped to a list of their corresponding values. |
9 | | /// |
10 | | /// See [`.into_group_map()`](crate::Itertools::into_group_map) |
11 | | /// for more information. |
12 | 0 | pub fn into_group_map_with_hasher<I, K, V, S>(iter: I, hash_builder: S) -> HashMap<K, Vec<V>, S> |
13 | 0 | where |
14 | 0 | I: Iterator<Item = (K, V)>, |
15 | 0 | K: Hash + Eq, |
16 | 0 | S: BuildHasher, |
17 | | { |
18 | 0 | let mut lookup = HashMap::<K, Vec<V>, S>::with_hasher(hash_builder); |
19 | | |
20 | 0 | iter.for_each(|(key, val)| { |
21 | 0 | lookup.entry(key).or_default().push(val); |
22 | 0 | }); |
23 | | |
24 | 0 | lookup |
25 | 0 | } |
26 | | |
27 | 0 | pub fn into_group_map_by_with_hasher<I, K, V, F, S>( |
28 | 0 | iter: I, |
29 | 0 | mut f: F, |
30 | 0 | hash_builder: S, |
31 | 0 | ) -> HashMap<K, Vec<V>, S> |
32 | 0 | where |
33 | 0 | I: Iterator<Item = V>, |
34 | 0 | K: Hash + Eq, |
35 | 0 | F: FnMut(&V) -> K, |
36 | 0 | S: BuildHasher, |
37 | | { |
38 | 0 | into_group_map_with_hasher(iter.map(|v| (f(&v), v)), hash_builder) |
39 | 0 | } |