itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Make grouping maps container agnostic

Open farnoy opened this issue 3 years ago • 1 comments

Could we make the grouping operations generic so they're able to produce collections other than std::collections::HashMap?

I'm proposing turning

    fn into_group_map<K, V>(self) -> HashMap<K, Vec<V>>

into

    fn into_group_map<K, V, C>(self) -> C where C: FromIterator<(K, Vec<V>)>

And similarly with into_grouping_map() and that family of types.

My use case is to directly produce HashMaps from the hashbrown crate and not std, but this should enable different applications as well.

Since the HashMap itself is a key component of how the grouping is achieved, it's probably insufficient to use FromIterator (we don't want to allocate an intermediate collection). I want to start a conversation and see what others think.

farnoy avatar Mar 03 '22 14:03 farnoy

I needed this so I build a version that has a trait implemented for both std's and hashbrown's map: https://github.com/rust-itertools/itertools/compare/master...msrd0:itertools:faa514f

As a proof of concept that works, but to be useful in the wild that trait probably needs to live outside of itertools in a small crate used and implemented by all the alternative map implementations in the wild.

msrd0 avatar Mar 11 '25 08:03 msrd0