Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified insert_phase_2 #3

Merged
merged 2 commits into from
Oct 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 12 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ enum Inserted<V> {
RobinHood {
probe: usize,
old_pos: Pos,
dist: usize,
}
}

Expand Down Expand Up @@ -438,8 +437,6 @@ pub struct VacantEntry<'a, K: 'a, V: 'a, S: 'a = RandomState> {
probe: usize,
#[allow(dead_code)]
index: usize,
dist: usize,
stealing_bucket: bool,
}

impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
Expand All @@ -458,11 +455,8 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
{
let index = self.map.entries.len();
self.map.entries.push(Bucket { hash: self.hash, key: self.key, value: value });
let old_pos = replace(&mut self.map.indices[self.probe],
Pos::with_hash::<Sz>(index, self.hash));
if self.stealing_bucket {
self.map.insert_phase_2::<Sz>(self.probe, old_pos, self.dist);
}
let old_pos = Pos::with_hash::<Sz>(index, self.hash);
self.map.insert_phase_2::<Sz>(self.probe, old_pos);
&mut {self.map}.entries[index].value
}
}
Expand Down Expand Up @@ -509,9 +503,7 @@ impl<K, V, S> OrderMap<K, V, S>
hash: hash,
key: key,
probe: probe,
stealing_bucket: true,
index: i,
dist: their_dist,
});
} else if entry_hash == hash && self.entries[i].key == key {
return Entry::Occupied(OccupiedEntry {
Expand All @@ -529,9 +521,7 @@ impl<K, V, S> OrderMap<K, V, S>
hash: hash,
key: key,
probe: probe,
stealing_bucket: false,
index: 0,
dist: 0,
});
}
dist += 1;
Expand Down Expand Up @@ -574,11 +564,9 @@ impl<K, V, S> OrderMap<K, V, S>
if their_dist < dist {
// robin hood: steal the spot if it's better for us
let index = self.entries.len();
let old_pos = replace(pos, Pos::with_hash::<Sz>(index, hash));
insert_kind = Inserted::RobinHood {
probe: probe,
old_pos: old_pos,
dist: their_dist,
old_pos: Pos::with_hash::<Sz>(index, hash),
};
break;
} else if entry_hash == hash && self.entries[i].key == key {
Expand Down Expand Up @@ -688,17 +676,17 @@ impl<K, V, S> OrderMap<K, V, S>
match self.insert_phase_1::<u64>(key, value) {
Inserted::Swapped { prev_value } => Some(prev_value),
Inserted::Done => None,
Inserted::RobinHood { probe, old_pos, dist } => {
self.insert_phase_2::<u64>(probe, old_pos, dist);
Inserted::RobinHood { probe, old_pos } => {
self.insert_phase_2::<u64>(probe, old_pos);
None
}
}
} else {
match self.insert_phase_1::<u32>(key, value) {
Inserted::Swapped { prev_value } => Some(prev_value),
Inserted::Done => None,
Inserted::RobinHood { probe, old_pos, dist } => {
self.insert_phase_2::<u32>(probe, old_pos, dist);
Inserted::RobinHood { probe, old_pos } => {
self.insert_phase_2::<u32>(probe, old_pos);
None
}
}
Expand Down Expand Up @@ -902,26 +890,18 @@ impl<K, V, S> OrderMap<K, V, S> {
self.remove_found(probe, found)
}

/// phase 2 is post-insert where we swap `Pos` in the indices around to
/// adjust after a bucket was stolen.
fn insert_phase_2<Sz>(&mut self, mut probe: usize, mut old_pos: Pos, mut dist: usize)
/// phase 2 is post-insert where we forward-shift `Pos` in the indices.
fn insert_phase_2<Sz>(&mut self, mut probe: usize, mut old_pos: Pos)
where Sz: Size
{
probe_loop!(probe < self.indices.len(), {
let pos = &mut self.indices[probe];
if let Some((i, hash_proxy)) = pos.resolve::<Sz>() {
let entry_hash = hash_proxy.get_short_hash(&self.entries, i);
// if existing element probed less than us, swap
let their_dist = probe_distance(self.mask, entry_hash.into_hash(), probe);
if their_dist < dist {
swap(&mut old_pos, pos);
dist = their_dist;
}
} else {
if pos.is_none() {
*pos = old_pos;
break;
} else {
old_pos = replace(pos, old_pos);
}
dist += 1;
});
}

Expand Down