itertools
itertools copied to clipboard
Specify hasher for unique?
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.
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)?
I don't know how it could be that generic. There's always .filter(move |item| set.insert(item)).
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.
What's wrong with just using HashMap as is?
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.