Skip to content

Commit 90b4d4b

Browse files
committed
Add hash_table::OccupiedEntry::replace_entry_with to mirror HashMap API
1 parent bba4a01 commit 90b4d4b

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed

src/raw/mod.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,11 +1127,12 @@ impl<T, A: Allocator> RawTable<T, A> {
11271127
/// Temporary removes a bucket, applying the given function to the removed
11281128
/// element and optionally put back the returned value in the same bucket.
11291129
///
1130-
/// Returns `true` if the bucket still contains an element
1130+
/// Returns `true` if the bucket still contains an element. Always returns the
1131+
/// associated bucket index and tag.
11311132
///
11321133
/// This does not check if the given bucket is actually occupied.
11331134
#[cfg_attr(feature = "inline-more", inline)]
1134-
pub unsafe fn replace_bucket_with<F>(&mut self, bucket: Bucket<T>, f: F) -> bool
1135+
pub unsafe fn replace_bucket_with<F>(&mut self, bucket: Bucket<T>, f: F) -> (bool, usize, Tag)
11351136
where
11361137
F: FnOnce(T) -> Option<T>,
11371138
{
@@ -1140,15 +1141,19 @@ impl<T, A: Allocator> RawTable<T, A> {
11401141
debug_assert!(self.is_bucket_full(index));
11411142
let old_growth_left = self.table.growth_left;
11421143
let item = self.remove(bucket).0;
1143-
if let Some(new_item) = f(item) {
1144-
self.table.growth_left = old_growth_left;
1145-
self.table.set_ctrl(index, old_ctrl);
1146-
self.table.items += 1;
1147-
self.bucket(index).write(new_item);
1148-
true
1149-
} else {
1150-
false
1151-
}
1144+
(
1145+
if let Some(new_item) = f(item) {
1146+
self.table.growth_left = old_growth_left;
1147+
self.table.set_ctrl(index, old_ctrl);
1148+
self.table.items += 1;
1149+
self.bucket(index).write(new_item);
1150+
true
1151+
} else {
1152+
false
1153+
},
1154+
index,
1155+
old_ctrl,
1156+
)
11521157
}
11531158

11541159
/// Searches for an element in the table. If the element is not found,

src/raw_entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ impl<'a, K, V, S, A: Allocator> RawOccupiedEntryMut<'a, K, V, S, A> {
12821282
F: FnOnce(&K, V) -> Option<V>,
12831283
{
12841284
unsafe {
1285-
let still_occupied = self
1285+
let (still_occupied, _, _) = self
12861286
.table
12871287
.replace_bucket_with(self.elem.clone(), |(key, value)| {
12881288
f(&key, value).map(|new_value| (key, new_value))

src/table.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,81 @@ where
22462246
pub fn bucket_index(&self) -> usize {
22472247
unsafe { self.table.raw.bucket_index(&self.bucket) }
22482248
}
2249+
2250+
/// Provides owned access to the value of the entry and allows to replace or
2251+
/// remove it based on the value of the returned option.
2252+
///
2253+
/// # Safety
2254+
///
2255+
/// The hash of the new item must be the same as the old item.
2256+
///
2257+
/// # Examples
2258+
///
2259+
/// ```
2260+
/// # #[cfg(feature = "nightly")]
2261+
/// # fn test() {
2262+
/// use hashbrown::{HashTable, DefaultHashBuilder};
2263+
/// use hashbrown::hash_table::Entry;
2264+
/// use std::hash::BuildHasher;
2265+
///
2266+
/// let mut table = HashTable::new();
2267+
/// let hasher = DefaultHashBuilder::default();
2268+
/// let hasher = |(ref key, _): &_| hasher.hash_one(key);
2269+
/// table.insert_unique(hasher(&("poneyland", 42)), ("poneyland", 42), hasher);
2270+
///
2271+
/// let entry = match table.entry(hasher(&("poneyland", 42)), |entry| entry.0 == "poneyland", hasher) {
2272+
/// Entry::Occupied(e) => unsafe {
2273+
/// e.replace_entry_with(|(k, v)| {
2274+
/// assert_eq!(k, "poneyland");
2275+
/// assert_eq!(v, 42);
2276+
/// Some(("poneyland", v + 1))
2277+
/// })
2278+
/// }
2279+
/// Entry::Vacant(_) => panic!(),
2280+
/// };
2281+
///
2282+
/// match entry {
2283+
/// Entry::Occupied(e) => {
2284+
/// assert_eq!(e.get(), &("poneyland", 43));
2285+
/// }
2286+
/// Entry::Vacant(_) => panic!(),
2287+
/// }
2288+
///
2289+
/// let entry = match table.entry(hasher(&("poneyland", 43)), |entry| entry.0 == "poneyland", hasher) {
2290+
/// Entry::Occupied(e) => unsafe { e.replace_entry_with(|(_k, _v)| None) },
2291+
/// Entry::Vacant(_) => panic!(),
2292+
/// };
2293+
///
2294+
/// match entry {
2295+
/// Entry::Vacant(e) => {
2296+
/// // nice!
2297+
/// }
2298+
/// Entry::Occupied(_) => panic!(),
2299+
/// }
2300+
///
2301+
/// assert!(table.is_empty());
2302+
/// # }
2303+
/// # fn main() {
2304+
/// # #[cfg(feature = "nightly")]
2305+
/// # test()
2306+
/// # }
2307+
/// ```
2308+
#[cfg_attr(feature = "inline-more", inline)]
2309+
pub unsafe fn replace_entry_with<F>(self, f: F) -> Entry<'a, T, A>
2310+
where
2311+
F: FnOnce(T) -> Option<T>,
2312+
{
2313+
unsafe {
2314+
match self.table.raw.replace_bucket_with(self.bucket.clone(), f) {
2315+
(true, _, _) => Entry::Occupied(self),
2316+
(false, index, tag) => Entry::Vacant(VacantEntry {
2317+
tag,
2318+
index,
2319+
table: self.table,
2320+
}),
2321+
}
2322+
}
2323+
}
22492324
}
22502325

22512326
/// A view into a vacant entry in a `HashTable`.

0 commit comments

Comments
 (0)