Skip to content

Commit

Permalink
Simplify some common find_or_find_insert_slot
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Sep 18, 2024
1 parent 2f5de33 commit 57f60db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
22 changes: 17 additions & 5 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1741,11 +1741,7 @@ where
#[cfg_attr(feature = "inline-more", inline)]
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
let hash = make_hash::<K, S>(&self.hash_builder, &k);
let hasher = make_hasher::<_, V, S>(&self.hash_builder);
match self
.table
.find_or_find_insert_slot(hash, equivalent_key(&k), hasher)
{
match self.find_or_find_insert_slot(hash, &k) {
Ok(bucket) => Some(mem::replace(unsafe { &mut bucket.as_mut().1 }, v)),
Err(slot) => {
unsafe {
Expand All @@ -1756,6 +1752,22 @@ where
}
}

#[cfg_attr(feature = "inline-more", inline)]
pub(crate) fn find_or_find_insert_slot<Q>(
&mut self,
hash: u64,
key: &Q,
) -> Result<Bucket<(K, V)>, crate::raw::InsertSlot>
where
Q: Equivalent<K> + ?Sized,
{
self.table.find_or_find_insert_slot(
hash,
equivalent_key(key),
make_hasher(&self.hash_builder),
)
}

/// Insert a key-value pair into the map without checking
/// if the key already exists in the map.
///
Expand Down
30 changes: 7 additions & 23 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::hash::{BuildHasher, Hash};
use core::iter::{Chain, FusedIterator};
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
use core::{fmt, mem};
use map::{equivalent_key, make_hash, make_hasher};
use map::make_hash;

use super::map::{self, HashMap, Keys};
use crate::raw::{Allocator, Global, RawExtractIf};
Expand Down Expand Up @@ -911,11 +911,7 @@ where
#[cfg_attr(feature = "inline-more", inline)]
pub fn get_or_insert(&mut self, value: T) -> &T {
let hash = make_hash(&self.map.hash_builder, &value);
let bucket = match self.map.table.find_or_find_insert_slot(
hash,
equivalent_key(&value),
make_hasher(&self.map.hash_builder),
) {
let bucket = match self.map.find_or_find_insert_slot(hash, &value) {
Ok(bucket) => bucket,
Err(slot) => unsafe { self.map.table.insert_in_slot(hash, slot, (value, ())) },
};
Expand Down Expand Up @@ -953,12 +949,8 @@ where
Q: Hash + Equivalent<T> + ?Sized,
F: FnOnce(&Q) -> T,
{
let hash = make_hash(&self.map.hash_builder, &value);
let bucket = match self.map.table.find_or_find_insert_slot(
hash,
equivalent_key(value),
make_hasher(&self.map.hash_builder),
) {
let hash = make_hash(&self.map.hash_builder, value);
let bucket = match self.map.find_or_find_insert_slot(hash, value) {
Ok(bucket) => bucket,
Err(slot) => {
let new = f(value);
Expand Down Expand Up @@ -1141,11 +1133,7 @@ where
#[cfg_attr(feature = "inline-more", inline)]
pub fn replace(&mut self, value: T) -> Option<T> {
let hash = make_hash(&self.map.hash_builder, &value);
match self.map.table.find_or_find_insert_slot(
hash,
equivalent_key(&value),
make_hasher(&self.map.hash_builder),
) {
match self.map.find_or_find_insert_slot(hash, &value) {
Ok(bucket) => Some(mem::replace(unsafe { &mut bucket.as_mut().0 }, value)),
Err(slot) => {
unsafe {
Expand Down Expand Up @@ -1581,12 +1569,8 @@ where
/// ```
fn bitxor_assign(&mut self, rhs: &HashSet<T, S, A>) {
for item in rhs {
let hash = make_hash(&self.map.hash_builder, &item);
match self.map.table.find_or_find_insert_slot(
hash,
equivalent_key(item),
make_hasher(&self.map.hash_builder),
) {
let hash = make_hash(&self.map.hash_builder, item);
match self.map.find_or_find_insert_slot(hash, item) {
Ok(bucket) => unsafe {
self.map.table.remove(bucket);
},
Expand Down

0 comments on commit 57f60db

Please sign in to comment.