Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify hasher for unique? #462

Open
camsteffen opened this issue Jul 16, 2020 · 5 comments
Open

Specify hasher for unique? #462

camsteffen opened this issue Jul 16, 2020 · 5 comments
Labels
generic-container Generic vector/hasher/map

Comments

@camsteffen
Copy link

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.

@phimuemue
Copy link
Member

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)?

@camsteffen
Copy link
Author

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

@phimuemue
Copy link
Member

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.

@camsteffen
Copy link
Author

What's wrong with just using HashMap as is?

@phimuemue
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generic-container Generic vector/hasher/map
Projects
None yet
Development

No branches or pull requests

3 participants