Skip to content

Commit c19e205

Browse files
committed
Add hash_table::OccupiedEntry::replace_entry_with to mirror HashMap API
1 parent 13f3a22 commit c19e205

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

src/raw/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,11 +1127,16 @@ 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(crate) unsafe fn replace_bucket_with<F>(
1136+
&mut self,
1137+
bucket: Bucket<T>,
1138+
f: F,
1139+
) -> (bool, usize, Tag)
11351140
where
11361141
F: FnOnce(T) -> Option<T>,
11371142
{
@@ -1140,15 +1145,19 @@ impl<T, A: Allocator> RawTable<T, A> {
11401145
debug_assert!(self.is_bucket_full(index));
11411146
let old_growth_left = self.table.growth_left;
11421147
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-
}
1148+
(
1149+
if let Some(new_item) = f(item) {
1150+
self.table.growth_left = old_growth_left;
1151+
self.table.set_ctrl(index, old_ctrl);
1152+
self.table.items += 1;
1153+
self.bucket(index).write(new_item);
1154+
true
1155+
} else {
1156+
false
1157+
},
1158+
index,
1159+
old_ctrl,
1160+
)
11521161
}
11531162

11541163
/// 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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,79 @@ where
22662266
pub fn bucket_index(&self) -> usize {
22672267
unsafe { self.table.raw.bucket_index(&self.bucket) }
22682268
}
2269+
2270+
/// Provides owned access to the value of the entry and allows to replace or
2271+
/// remove it based on the value of the returned option.
2272+
///
2273+
/// The hash of the new item should be the same as the old item.
2274+
///
2275+
/// # Examples
2276+
///
2277+
/// ```
2278+
/// # #[cfg(feature = "nightly")]
2279+
/// # fn test() {
2280+
/// use hashbrown::{HashTable, DefaultHashBuilder};
2281+
/// use hashbrown::hash_table::Entry;
2282+
/// use std::hash::BuildHasher;
2283+
///
2284+
/// let mut table = HashTable::new();
2285+
/// let hasher = DefaultHashBuilder::default();
2286+
/// let hasher = |(ref key, _): &_| hasher.hash_one(key);
2287+
/// table.insert_unique(hasher(&("poneyland", 42)), ("poneyland", 42), hasher);
2288+
///
2289+
/// let entry = match table.entry(hasher(&("poneyland", 42)), |entry| entry.0 == "poneyland", hasher) {
2290+
/// Entry::Occupied(e) => unsafe {
2291+
/// e.replace_entry_with(|(k, v)| {
2292+
/// assert_eq!(k, "poneyland");
2293+
/// assert_eq!(v, 42);
2294+
/// Some(("poneyland", v + 1))
2295+
/// })
2296+
/// }
2297+
/// Entry::Vacant(_) => panic!(),
2298+
/// };
2299+
///
2300+
/// match entry {
2301+
/// Entry::Occupied(e) => {
2302+
/// assert_eq!(e.get(), &("poneyland", 43));
2303+
/// }
2304+
/// Entry::Vacant(_) => panic!(),
2305+
/// }
2306+
///
2307+
/// let entry = match table.entry(hasher(&("poneyland", 43)), |entry| entry.0 == "poneyland", hasher) {
2308+
/// Entry::Occupied(e) => unsafe { e.replace_entry_with(|(_k, _v)| None) },
2309+
/// Entry::Vacant(_) => panic!(),
2310+
/// };
2311+
///
2312+
/// match entry {
2313+
/// Entry::Vacant(e) => {
2314+
/// // nice!
2315+
/// }
2316+
/// Entry::Occupied(_) => panic!(),
2317+
/// }
2318+
///
2319+
/// assert!(table.is_empty());
2320+
/// # }
2321+
/// # fn main() {
2322+
/// # #[cfg(feature = "nightly")]
2323+
/// # test()
2324+
/// # }
2325+
/// ```
2326+
#[cfg_attr(feature = "inline-more", inline)]
2327+
pub fn replace_entry_with<F>(self, f: F) -> Entry<'a, T, A>
2328+
where
2329+
F: FnOnce(T) -> Option<T>,
2330+
{
2331+
unsafe {
2332+
match self.table.raw.replace_bucket_with(self.bucket.clone(), f) {
2333+
(true, _, _) => Entry::Occupied(self),
2334+
(false, index, tag) => Entry::Vacant(VacantEntry {
2335+
tag,
2336+
index,
2337+
table: self.table,
2338+
}),
2339+
}
2340+
}
2341+
}
22692342
}
22702343

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

0 commit comments

Comments
 (0)