Skip to content

Commit

Permalink
insert: Use &mut Pos
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Sep 18, 2016
1 parent d390778 commit ee15cbc
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,18 +423,18 @@ impl<K, V, S> OrderMap<K, V, S>
let insert_kind;
debug_assert!(self.len() < self.raw_capacity());
probe_loop!(probe < self.indices.len(), {
if let Some((i, hash_proxy)) = self.indices[probe].resolve::<Sz>() {
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 {
// robin hood: steal the spot if it's better for us
let index = self.entries.len();
let mut pos = Pos::with_hash::<Sz>(index, hash);
swap(&mut pos, &mut self.indices[probe]);
let old_pos = replace(pos, Pos::with_hash::<Sz>(index, hash));
insert_kind = Inserted::RobinHood {
probe: probe,
old_pos: pos,
old_pos: old_pos,
dist: their_dist,
};
break;
Expand All @@ -444,7 +444,7 @@ impl<K, V, S> OrderMap<K, V, S>
} else {
// empty bucket, insert here
let index = self.entries.len();
self.indices[probe] = Pos::with_hash::<Sz>(index, hash);
*pos = Pos::with_hash::<Sz>(index, hash);
insert_kind = Inserted::Done;
break;
}
Expand All @@ -458,16 +458,17 @@ impl<K, V, S> OrderMap<K, V, S>
where Sz: Size
{
probe_loop!(probe < self.indices.len(), {
if let Some((i, hash_proxy)) = self.indices[probe].resolve::<Sz>() {
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, &mut self.indices[probe]);
swap(&mut old_pos, pos);
dist = their_dist;
}
} else {
self.indices[probe] = old_pos;
*pos = old_pos;
break;
}
dist += 1;
Expand Down

0 comments on commit ee15cbc

Please sign in to comment.