Skip to content

Commit dc15e5c

Browse files
Reintroduce GroupingMap[By] with one more generic parameter
This is a breaking change! However, this is mostly inferred. Note that it breaks (only) laziness tests because the `GroupingMap` object is not used (I could write any type but `u8` is one of the possible returned types). If it was used then it would have been inferred. I copied/pasted the code of old `into_grouping_map[_by]` methods, added the generic `R` and updated each function body with `_in(.., HashMap::new())`. The old types are aliases of the new ones.
1 parent 14b6096 commit dc15e5c

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/grouping_map.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,35 @@ use std::cmp::Ordering;
99
use std::iter::Iterator;
1010
use std::ops::{Add, Mul};
1111

12+
#[cfg(feature = "use_std")]
13+
pub use with_hashmap::{GroupingMap, GroupingMapBy};
14+
15+
#[cfg(feature = "use_std")]
16+
mod with_hashmap {
17+
use super::*;
18+
use std::collections::HashMap;
19+
20+
// This is used to infer `K` when `I::Item = (K, V)` since we can't write `I::Item.0`.
21+
pub trait KeyValue {
22+
type Key;
23+
}
24+
25+
impl<K, V> KeyValue for (K, V) {
26+
type Key = K;
27+
}
28+
29+
/// `GroupingMap` is an intermediate struct for efficient group-and-fold operations.
30+
///
31+
/// See [`GroupingGenericMap`] for more informations.
32+
pub type GroupingMap<I, R> =
33+
GroupingGenericMap<I, HashMap<<<I as Iterator>::Item as KeyValue>::Key, R>>;
34+
35+
/// `GroupingMapBy` is an intermediate struct for efficient group-and-fold operations.
36+
///
37+
/// See [`GroupingGenericMap`] for more informations.
38+
pub type GroupingMapBy<I, F, R> = GroupingMap<MapForGrouping<I, F>, R>;
39+
}
40+
1241
/// A wrapper to allow for an easy [`into_grouping_map_by`](crate::Itertools::into_grouping_map_by)
1342
pub type MapForGrouping<I, F> = MapSpecialCase<I, GroupingMapFn<F>>;
1443

src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ pub mod structs {
113113
pub use crate::groupbylazy::{Chunk, ChunkBy, Chunks, Group, Groups, IntoChunks};
114114
#[cfg(feature = "use_alloc")]
115115
pub use crate::grouping_map::{GroupingGenericMap, GroupingGenericMapBy};
116+
#[cfg(feature = "use_std")]
117+
pub use crate::grouping_map::{GroupingMap, GroupingMapBy};
116118
pub use crate::intersperse::{Intersperse, IntersperseWith};
117119
#[cfg(feature = "use_alloc")]
118120
pub use crate::kmerge_impl::{KMerge, KMergeBy};
@@ -3340,6 +3342,42 @@ pub trait Itertools: Iterator {
33403342
group_map::into_group_map_by(self, f)
33413343
}
33423344

3345+
/// Constructs a `GroupingMap` to be used later with one of the efficient
3346+
/// group-and-fold operations it allows to perform.
3347+
///
3348+
/// The input iterator must yield item in the form of `(K, V)` where the
3349+
/// value of type `K` will be used as key to identify the groups and the
3350+
/// value of type `V` as value for the folding operation.
3351+
///
3352+
/// See [`GroupingGenericMap`] for more informations
3353+
/// on what operations are available.
3354+
#[cfg(feature = "use_std")]
3355+
fn into_grouping_map<K, V, R>(self) -> GroupingMap<Self, R>
3356+
where
3357+
Self: Iterator<Item = (K, V)> + Sized,
3358+
K: Hash + Eq,
3359+
{
3360+
self.into_grouping_map_in(HashMap::new())
3361+
}
3362+
3363+
/// Constructs a `GroupingMap` to be used later with one of the efficient
3364+
/// group-and-fold operations it allows to perform.
3365+
///
3366+
/// The values from this iterator will be used as values for the folding operation
3367+
/// while the keys will be obtained from the values by calling `key_mapper`.
3368+
///
3369+
/// See [`GroupingGenericMap`] for more informations
3370+
/// on what operations are available.
3371+
#[cfg(feature = "use_std")]
3372+
fn into_grouping_map_by<K, V, F, R>(self, key_mapper: F) -> GroupingMapBy<Self, F, R>
3373+
where
3374+
Self: Iterator<Item = V> + Sized,
3375+
K: Hash + Eq,
3376+
F: FnMut(&V) -> K,
3377+
{
3378+
self.into_grouping_map_by_in(key_mapper, HashMap::new())
3379+
}
3380+
33433381
/// Constructs a `GroupingGenericMap` to be used later with one of the efficient
33443382
/// group-and-fold operations it allows to perform.
33453383
///

tests/laziness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ must_use_tests! {
229229
}
230230
// Not iterator themselves but still lazy.
231231
into_grouping_map {
232-
let _ = Panicking.map(|x| (x, x + 1)).into_grouping_map();
232+
let _ = Panicking.map(|x| (x, x + 1)).into_grouping_map::<_, _, u8>();
233233
}
234234
into_grouping_map_by {
235-
let _ = Panicking.into_grouping_map_by(|x| *x);
235+
let _ = Panicking.into_grouping_map_by::<_, _, _, u8>(|x| *x);
236236
}
237237
// Macros:
238238
iproduct {

0 commit comments

Comments
 (0)