-
Notifications
You must be signed in to change notification settings - Fork 13.3k
pre-RFC: HashSet::pop() #37986
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
Comments
The more interesting question is how you'd implement this without quadratic behaviour |
Duplicate-ish to #33549 |
I believe these kinds of issues should also be filed on the RFCs repo rather than here. |
@jonas-schievink You could keep a "hint" in the set for the next bucket to check for an element, and increment that after popping out an element. That would avoid traversing all the empty buckets every time. So, unless you had a loop where every insert happened to put a new element at the point directly behind the current hint, you shouldn't get quadratic behavior. @nagisa Not quite, I think. An iterator over the entries wouldn't allow mutating the set at the same time. @sfackler You're right, I should have filed this on the RFCs repository. |
OrderMap has an O(1) (average) Like jonas said, needs shrewd plan for how to do it in HashSet/HashMap if they don't change implementation. |
Closing in favor of the RFCs issue. |
I'd like to have a
pop()
method available forHashSet
, which removes an item from the set if any, and returns anOption<T>
(returningNone
if empty). That would make it easy to iterate over a set withwhile let element = set.pop() { ... }
, without holding a mutable reference to the set asHashSet::drain()
does, so you can insert more items into the set as you iterate.Does that seem like a reasonable method to add to HashSet? Does this need an RFC, or just a patch to std?
The text was updated successfully, but these errors were encountered: