Skip to content

Commit 840065f

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

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

src/raw/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,14 +1124,14 @@ impl<T, A: Allocator> RawTable<T, A> {
11241124
bucket
11251125
}
11261126

1127-
/// Temporary removes a bucket, applying the given function to the removed
1127+
/// Temporarily 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 tag for bucket if the bucket is emptied out.
11311131
///
11321132
/// This does not check if the given bucket is actually occupied.
11331133
#[cfg_attr(feature = "inline-more", inline)]
1134-
pub unsafe fn replace_bucket_with<F>(&mut self, bucket: Bucket<T>, f: F) -> bool
1134+
pub(crate) unsafe fn replace_bucket_with<F>(&mut self, bucket: Bucket<T>, f: F) -> Option<Tag>
11351135
where
11361136
F: FnOnce(T) -> Option<T>,
11371137
{
@@ -1145,9 +1145,9 @@ impl<T, A: Allocator> RawTable<T, A> {
11451145
self.table.set_ctrl(index, old_ctrl);
11461146
self.table.items += 1;
11471147
self.bucket(index).write(new_item);
1148-
true
1148+
None
11491149
} else {
1150-
false
1150+
Some(old_ctrl)
11511151
}
11521152
}
11531153

src/raw_entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,13 +1282,13 @@ 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 tag = self
12861286
.table
12871287
.replace_bucket_with(self.elem.clone(), |(key, value)| {
12881288
f(&key, value).map(|new_value| (key, new_value))
12891289
});
12901290

1291-
if still_occupied {
1291+
if tag.is_none() {
12921292
RawEntryMut::Occupied(self)
12931293
} else {
12941294
RawEntryMut::Vacant(RawVacantEntryMut {

src/table.rs

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

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

0 commit comments

Comments
 (0)