Skip to content

Commit 6420cfd

Browse files
committed
Simplify some common find_or_find_insert_slot
1 parent ad3b430 commit 6420cfd

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

src/map.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,11 +1741,7 @@ where
17411741
#[cfg_attr(feature = "inline-more", inline)]
17421742
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
17431743
let hash = make_hash::<K, S>(&self.hash_builder, &k);
1744-
let hasher = make_hasher::<_, V, S>(&self.hash_builder);
1745-
match self
1746-
.table
1747-
.find_or_find_insert_slot(hash, equivalent_key(&k), hasher)
1748-
{
1744+
match self.find_or_find_insert_slot(hash, &k) {
17491745
Ok(bucket) => Some(mem::replace(unsafe { &mut bucket.as_mut().1 }, v)),
17501746
Err(slot) => {
17511747
unsafe {
@@ -1756,6 +1752,22 @@ where
17561752
}
17571753
}
17581754

1755+
#[cfg_attr(feature = "inline-more", inline)]
1756+
pub(crate) fn find_or_find_insert_slot<Q>(
1757+
&mut self,
1758+
hash: u64,
1759+
key: &Q,
1760+
) -> Result<Bucket<(K, V)>, crate::raw::InsertSlot>
1761+
where
1762+
Q: Equivalent<K> + ?Sized,
1763+
{
1764+
self.table.find_or_find_insert_slot(
1765+
hash,
1766+
equivalent_key(key),
1767+
make_hasher(&self.hash_builder),
1768+
)
1769+
}
1770+
17591771
/// Insert a key-value pair into the map without checking
17601772
/// if the key already exists in the map.
17611773
///

src/set.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::hash::{BuildHasher, Hash};
33
use core::iter::{Chain, FusedIterator};
44
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
55
use core::{fmt, mem};
6-
use map::{equivalent_key, make_hash, make_hasher};
6+
use map::make_hash;
77

88
use super::map::{self, HashMap, Keys};
99
use crate::raw::{Allocator, Global, RawExtractIf};
@@ -911,11 +911,7 @@ where
911911
#[cfg_attr(feature = "inline-more", inline)]
912912
pub fn get_or_insert(&mut self, value: T) -> &T {
913913
let hash = make_hash(&self.map.hash_builder, &value);
914-
let bucket = match self.map.table.find_or_find_insert_slot(
915-
hash,
916-
equivalent_key(&value),
917-
make_hasher(&self.map.hash_builder),
918-
) {
914+
let bucket = match self.map.find_or_find_insert_slot(hash, &value) {
919915
Ok(bucket) => bucket,
920916
Err(slot) => unsafe { self.map.table.insert_in_slot(hash, slot, (value, ())) },
921917
};
@@ -953,12 +949,8 @@ where
953949
Q: Hash + Equivalent<T> + ?Sized,
954950
F: FnOnce(&Q) -> T,
955951
{
956-
let hash = make_hash(&self.map.hash_builder, &value);
957-
let bucket = match self.map.table.find_or_find_insert_slot(
958-
hash,
959-
equivalent_key(value),
960-
make_hasher(&self.map.hash_builder),
961-
) {
952+
let hash = make_hash(&self.map.hash_builder, value);
953+
let bucket = match self.map.find_or_find_insert_slot(hash, value) {
962954
Ok(bucket) => bucket,
963955
Err(slot) => {
964956
let new = f(value);
@@ -1141,11 +1133,7 @@ where
11411133
#[cfg_attr(feature = "inline-more", inline)]
11421134
pub fn replace(&mut self, value: T) -> Option<T> {
11431135
let hash = make_hash(&self.map.hash_builder, &value);
1144-
match self.map.table.find_or_find_insert_slot(
1145-
hash,
1146-
equivalent_key(&value),
1147-
make_hasher(&self.map.hash_builder),
1148-
) {
1136+
match self.map.find_or_find_insert_slot(hash, &value) {
11491137
Ok(bucket) => Some(mem::replace(unsafe { &mut bucket.as_mut().0 }, value)),
11501138
Err(slot) => {
11511139
unsafe {
@@ -1591,12 +1579,8 @@ where
15911579
/// ```
15921580
fn bitxor_assign(&mut self, rhs: &HashSet<T, S, A>) {
15931581
for item in rhs {
1594-
let hash = make_hash(&self.map.hash_builder, &item);
1595-
match self.map.table.find_or_find_insert_slot(
1596-
hash,
1597-
equivalent_key(item),
1598-
make_hasher(&self.map.hash_builder),
1599-
) {
1582+
let hash = make_hash(&self.map.hash_builder, item);
1583+
match self.map.find_or_find_insert_slot(hash, item) {
16001584
Ok(bucket) => unsafe {
16011585
self.map.table.remove(bucket);
16021586
},

0 commit comments

Comments
 (0)