Skip to content

Commit 89251f6

Browse files
committed
Neaten up some things
1 parent 58a2652 commit 89251f6

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/map.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -834,13 +834,13 @@ where
834834
K: Hash + Eq,
835835
S: BuildHasher,
836836
{
837-
fn probe_action<'a, Sz, A>(&'a mut self, key: K, action: A) -> A::Output
837+
fn insert_phase_1<'a, Sz, A>(&'a mut self, key: K, action: A) -> A::Output
838838
where
839839
Sz: Size,
840840
A: ProbeAction<'a, Sz, K, V>,
841841
{
842842
let hash = hash_elem_using(&self.hash_builder, &key);
843-
self.core.probe_action::<Sz, A>(hash, key, action)
843+
self.core.insert_phase_1::<Sz, A>(hash, key, action)
844844
}
845845

846846
/// Remove all key-value pairs in the map, while preserving its capacity.
@@ -904,7 +904,7 @@ where
904904
/// or if you need to get the index of the corresponding key-value pair.
905905
pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) {
906906
self.reserve_one();
907-
dispatch_32_vs_64!(self.probe_action::<_>(key, InsertValue(value)))
907+
dispatch_32_vs_64!(self.insert_phase_1::<_>(key, InsertValue(value)))
908908
}
909909

910910
/// Get the given key’s corresponding entry in the map for insertion and/or
@@ -913,7 +913,7 @@ where
913913
/// Computes in **O(1)** time (amortized average).
914914
pub fn entry(&mut self, key: K) -> Entry<K, V> {
915915
self.reserve_one();
916-
dispatch_32_vs_64!(self.probe_action::<_>(key, MakeEntry))
916+
dispatch_32_vs_64!(self.insert_phase_1::<_>(key, MakeEntry))
917917
}
918918

919919
/// Return an iterator over the key-value pairs of the map, in their order
@@ -1406,7 +1406,7 @@ impl<K, V> OrderMapCore<K, V> {
14061406
Some(self.swap_remove_found(probe, found))
14071407
}
14081408

1409-
fn probe_action<'a, Sz, A>(&'a mut self, hash: HashValue, key: K, action: A) -> A::Output
1409+
fn insert_phase_1<'a, Sz, A>(&'a mut self, hash: HashValue, key: K, action: A) -> A::Output
14101410
where
14111411
Sz: Size,
14121412
K: Eq,
@@ -1696,21 +1696,24 @@ impl<K, V> OrderMapCore<K, V> {
16961696

16971697
trait ProbeAction<'a, Sz: Size, K, V>: Sized {
16981698
type Output;
1699+
// handle an occupied spot in the map
16991700
fn hit(self, entry: OccupiedEntry<'a, K, V>) -> Self::Output;
1701+
// handle an empty spot in the map
17001702
fn empty(self, entry: VacantEntry<'a, K, V>) -> Self::Output;
1701-
fn steal(self, entry: VacantEntry<'a, K, V>) -> Self::Output {
1702-
self.empty(entry)
1703-
}
1703+
// robin hood: handle a that you should steal because it's better for you
1704+
fn steal(self, entry: VacantEntry<'a, K, V>) -> Self::Output;
17041705
}
17051706

17061707
struct InsertValue<V>(V);
17071708

17081709
impl<'a, Sz: Size, K, V> ProbeAction<'a, Sz, K, V> for InsertValue<V> {
17091710
type Output = (usize, Option<V>);
1711+
17101712
fn hit(self, entry: OccupiedEntry<'a, K, V>) -> Self::Output {
17111713
let old = replace(&mut entry.map.entries[entry.index].value, self.0);
17121714
(entry.index, Some(old))
17131715
}
1716+
17141717
fn empty(self, entry: VacantEntry<'a, K, V>) -> Self::Output {
17151718
let pos = &mut entry.map.indices[entry.probe];
17161719
let index = entry.map.entries.len();
@@ -1722,15 +1725,10 @@ impl<'a, Sz: Size, K, V> ProbeAction<'a, Sz, K, V> for InsertValue<V> {
17221725
});
17231726
(index, None)
17241727
}
1728+
17251729
fn steal(self, entry: VacantEntry<'a, K, V>) -> Self::Output {
17261730
let index = entry.map.entries.len();
1727-
let old_pos = Pos::with_hash::<Sz>(index, entry.hash);
1728-
entry.map.entries.push(Bucket {
1729-
hash: entry.hash,
1730-
key: entry.key,
1731-
value: self.0,
1732-
});
1733-
entry.map.insert_phase_2::<Sz>(entry.probe, old_pos);
1731+
entry.insert_impl::<Sz>(self.0);
17341732
(index, None)
17351733
}
17361734
}
@@ -1745,6 +1743,9 @@ impl<'a, Sz: Size, K: 'a, V: 'a> ProbeAction<'a, Sz, K, V> for MakeEntry {
17451743
fn empty(self, entry: VacantEntry<'a, K, V>) -> Self::Output {
17461744
Entry::Vacant(entry)
17471745
}
1746+
fn steal(self, entry: VacantEntry<'a, K, V>) -> Self::Output {
1747+
Entry::Vacant(entry)
1748+
}
17481749
}
17491750

17501751
/// Find, in the indices, an entry that already exists at a known position

0 commit comments

Comments
 (0)