-
Notifications
You must be signed in to change notification settings - Fork 314
Commit aa8c30b
committed
Auto merge of #190 - SkiFire13:replace-entry-with, r=Amanieu
Implement `replace_entry_with`
Adds `OccupiedEntry::replace_entry_with` that consumes `self`, takes an `FnOnce(&K, V) -> Option<V>` and replaces/removes the value based on the value returned from the closure.
This function is intended to be used when just having a mutable reference is not enough and it is more efficient than just using `remove` optionally followed by `insert`. This is achieved by saving the index and the ctrl byte before removing the element from the bucket, allowing to skip the rehash and bucket search in case the element is reinserted after the function returns.
All of this relies on a new function `RawTable::replace_bucket_with` that does all exactly the same thing (replaces/removes the value based on the value returned from the closure) but at the `RawTable` level.
There's also a `Entry::and_replace_entry_with` (that does nothing when the entry is a `VacantEntry`) and the analogous functions for `RawEntryMut` and `RawOccupiedEntryMut`.
To be settled:
- [x] ~~Missing tests for `RawEntryMut::and_replace_entry_with` and `RawOccupiedEntryMut::replace_entry_with`~~
- [ ] I'm not sure if I should add this to the Rustc version of the entry API (I guess it should only mirror the stdlib version of HashMap, so no?).
- [x] ~~It's possible for these functions to return an `Entry`/`RawEntryMut`, but this requires to add an extra field to `OccupiedEntry`/`RawOccupiedEntryMut`. I've implemented it [in another branch](https://github.com/SkiFire13/hashbrown/tree/replace-entry-with-return), should I merge it?~~
Also see #189
0 commit comments