| 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 |
pub fn into_group_map_with_hasher<I, K, V, S>(iter: I, hash_builder: S) -> HashMap<K, Vec<V>, S> |
| 13 |
where |
| 14 |
I: Iterator<Item = (K, V)>, |
| 15 |
K: Hash + Eq, |
| 16 |
S: BuildHasher, |
| 17 |
{ |
| 18 |
let mut lookup = HashMap::<K, Vec<V>, S>::with_hasher(hash_builder); |
| 19 |
|
| 20 |
iter.for_each(|(key, val)| { |
| 21 |
lookup.entry(key).or_default().push(val); |
| 22 |
}); |
| 23 |
|
| 24 |
lookup |
| 25 |
} |
| 26 |
|
| 27 |
pub fn into_group_map_by_with_hasher<I, K, V, F, S>( |
| 28 |
iter: I, |
| 29 |
mut f: F, |
| 30 |
hash_builder: S, |
| 31 |
) -> HashMap<K, Vec<V>, S> |
| 32 |
where |
| 33 |
I: Iterator<Item = V>, |
| 34 |
K: Hash + Eq, |
| 35 |
F: FnMut(&V) -> K, |
| 36 |
S: BuildHasher, |
| 37 |
{ |
| 38 |
into_group_map_with_hasher(iter.map(|v| (f(&v), v)), hash_builder) |
| 39 |
} |