Skip to content

Commit a0f329a

Browse files
committed
Change into_group_map_by to return an HashMap and use the into_group_map.
1 parent b640523 commit a0f329a

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

src/group_map.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::collections::HashMap;
44
use std::hash::Hash;
55
use std::iter::Iterator;
6-
use HashMapIntoIter;
76

87
/// Return a `HashMap` of keys mapped to a list of their corresponding values.
98
///
@@ -23,18 +22,14 @@ pub fn into_group_map<I, K, V>(iter: I) -> HashMap<K, Vec<V>>
2322
lookup
2423
}
2524

26-
pub fn into_group_map_by<I, K, V>(iter: I, f: impl Fn(&V) -> K) ->HashMapIntoIter<K,Vec<V>>
25+
pub fn into_group_map_by<I, K, V>(iter: I, f: impl Fn(&V) -> K) -> HashMap<K, Vec<V>>
2726
where
2827
I: Iterator<Item=V>,
2928
K: Hash + Eq,
3029
{
31-
let mut lookup = HashMap::new();
32-
33-
for val in iter {
34-
let key = f(&val);
35-
lookup.entry(key).or_insert(Vec::new()).push(val);
36-
}
37-
38-
lookup.into_iter()
30+
into_group_map(
31+
iter.map(|v| (f(&v), v))
32+
)
3933
}
4034

35+

src/lib.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ use std::fmt::Write;
6767
#[cfg(feature = "use_std")]
6868
type VecIntoIter<T> = ::std::vec::IntoIter<T>;
6969

70-
#[cfg(feature = "use_std")]
71-
type HashMapIntoIter<K, V> = ::std::collections::hash_map::IntoIter<K, V>;
72-
7370
use std::iter::FromIterator;
7471

7572
#[macro_use]
@@ -2196,9 +2193,8 @@ pub trait Itertools: Iterator {
21962193
/// use std::collections::HashMap;
21972194
///
21982195
/// let data = vec![(0, 10), (2, 12), (3, 13), (0, 20), (3, 33), (2, 42)];
2199-
/// let lookup: HashMap<u32,Vec<(u32, u32)>> = data.clone().into_iter().into_group_map_by(|a|
2200-
/// a.0)
2201-
/// .collect();
2196+
/// let lookup: HashMap<u32,Vec<(u32, u32)>> =
2197+
/// data.clone().into_iter().into_group_map_by(|a| a.0);
22022198
///
22032199
/// assert_eq!(lookup[&0], vec![(0,10),(0,20)]);
22042200
/// assert_eq!(lookup.get(&1), None);
@@ -2208,11 +2204,12 @@ pub trait Itertools: Iterator {
22082204
/// assert_eq!(
22092205
/// data.into_iter()
22102206
/// .into_group_map_by(|x| x.0)
2207+
/// .into_iter()
22112208
/// .map(|(key, values)| (key, values.into_iter().fold(0,|acc, (_,v)| acc + v )))
22122209
/// .collect::<HashMap<u32,u32>>()[&0], 30)
22132210
/// ```
22142211
#[cfg(feature = "use_std")]
2215-
fn into_group_map_by<K, V, F>(self, f: F) -> HashMapIntoIter<K, Vec<V>>
2212+
fn into_group_map_by<K, V, F>(self, f: F) -> HashMap<K, Vec<V>>
22162213
where
22172214
Self: Iterator<Item=V> + Sized,
22182215
K: Hash + Eq,

0 commit comments

Comments
 (0)