itertools icon indicating copy to clipboard operation
itertools copied to clipboard

Specify hasher for unique?

Open camsteffen opened this issue 5 years ago • 5 comments

Would there be interest in adding a variant of unique which allows you to specify the hasher? How about the name unique_default? The name isn't great but it's the best I can think of. It is inspired by HashMap which allows you to specify the hasher using HashMap::default().

Example:

 iter.unique_default::<BuildHasherDefault<AHasher>>();

I have implemented this on my machine but I want to get feedback before opening a PR.

camsteffen avatar Jul 16 '20 15:07 camsteffen

I could imagine that some users want to check uniqueness with more efficient containers, but shouldn't we at least think about generalizing a bit more so that users can essentially specify a container that can insert an element, returning if the element already has been in there or not (such as BTreeSet, or even sorted Vecs)?

phimuemue avatar Jul 16 '20 17:07 phimuemue

I don't know how it could be that generic. There's always .filter(move |item| set.insert(item)).

camsteffen avatar Jul 16 '20 17:07 camsteffen

It would be a bit of work, but I imagine one could e.g. define a trait that encapsulates inserting an element into the container and - in the same method - returning if the element was already present. This trait could then be implemented for HashMap, BTreeMap, Vec and so on.

phimuemue avatar Jul 16 '20 18:07 phimuemue

What's wrong with just using HashMap as is?

camsteffen avatar Jul 16 '20 19:07 camsteffen

Nothing per se - it just struck me that if a user wants to specify a custom hasher, they might as well want something else. But without trying I confess that generalizing this could well result in too much work to be worth it.

phimuemue avatar Jul 16 '20 19:07 phimuemue