Skip to content

Commit 8f7ebde

Browse files
authored
Merge pull request rust-lang#3 from pczarn/refactor-insert
Simplified insert_phase_2
2 parents 736d3ec + ada8d63 commit 8f7ebde

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

src/lib.rs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ enum Inserted<V> {
237237
RobinHood {
238238
probe: usize,
239239
old_pos: Pos,
240-
dist: usize,
241240
}
242241
}
243242

@@ -438,8 +437,6 @@ pub struct VacantEntry<'a, K: 'a, V: 'a, S: 'a = RandomState> {
438437
probe: usize,
439438
#[allow(dead_code)]
440439
index: usize,
441-
dist: usize,
442-
stealing_bucket: bool,
443440
}
444441

445442
impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
@@ -458,11 +455,8 @@ impl<'a, K, V, S> VacantEntry<'a, K, V, S> {
458455
{
459456
let index = self.map.entries.len();
460457
self.map.entries.push(Bucket { hash: self.hash, key: self.key, value: value });
461-
let old_pos = replace(&mut self.map.indices[self.probe],
462-
Pos::with_hash::<Sz>(index, self.hash));
463-
if self.stealing_bucket {
464-
self.map.insert_phase_2::<Sz>(self.probe, old_pos, self.dist);
465-
}
458+
let old_pos = Pos::with_hash::<Sz>(index, self.hash);
459+
self.map.insert_phase_2::<Sz>(self.probe, old_pos);
466460
&mut {self.map}.entries[index].value
467461
}
468462
}
@@ -509,9 +503,7 @@ impl<K, V, S> OrderMap<K, V, S>
509503
hash: hash,
510504
key: key,
511505
probe: probe,
512-
stealing_bucket: true,
513506
index: i,
514-
dist: their_dist,
515507
});
516508
} else if entry_hash == hash && self.entries[i].key == key {
517509
return Entry::Occupied(OccupiedEntry {
@@ -529,9 +521,7 @@ impl<K, V, S> OrderMap<K, V, S>
529521
hash: hash,
530522
key: key,
531523
probe: probe,
532-
stealing_bucket: false,
533524
index: 0,
534-
dist: 0,
535525
});
536526
}
537527
dist += 1;
@@ -574,11 +564,9 @@ impl<K, V, S> OrderMap<K, V, S>
574564
if their_dist < dist {
575565
// robin hood: steal the spot if it's better for us
576566
let index = self.entries.len();
577-
let old_pos = replace(pos, Pos::with_hash::<Sz>(index, hash));
578567
insert_kind = Inserted::RobinHood {
579568
probe: probe,
580-
old_pos: old_pos,
581-
dist: their_dist,
569+
old_pos: Pos::with_hash::<Sz>(index, hash),
582570
};
583571
break;
584572
} else if entry_hash == hash && self.entries[i].key == key {
@@ -688,17 +676,17 @@ impl<K, V, S> OrderMap<K, V, S>
688676
match self.insert_phase_1::<u64>(key, value) {
689677
Inserted::Swapped { prev_value } => Some(prev_value),
690678
Inserted::Done => None,
691-
Inserted::RobinHood { probe, old_pos, dist } => {
692-
self.insert_phase_2::<u64>(probe, old_pos, dist);
679+
Inserted::RobinHood { probe, old_pos } => {
680+
self.insert_phase_2::<u64>(probe, old_pos);
693681
None
694682
}
695683
}
696684
} else {
697685
match self.insert_phase_1::<u32>(key, value) {
698686
Inserted::Swapped { prev_value } => Some(prev_value),
699687
Inserted::Done => None,
700-
Inserted::RobinHood { probe, old_pos, dist } => {
701-
self.insert_phase_2::<u32>(probe, old_pos, dist);
688+
Inserted::RobinHood { probe, old_pos } => {
689+
self.insert_phase_2::<u32>(probe, old_pos);
702690
None
703691
}
704692
}
@@ -902,26 +890,18 @@ impl<K, V, S> OrderMap<K, V, S> {
902890
self.remove_found(probe, found)
903891
}
904892

905-
/// phase 2 is post-insert where we swap `Pos` in the indices around to
906-
/// adjust after a bucket was stolen.
907-
fn insert_phase_2<Sz>(&mut self, mut probe: usize, mut old_pos: Pos, mut dist: usize)
893+
/// phase 2 is post-insert where we forward-shift `Pos` in the indices.
894+
fn insert_phase_2<Sz>(&mut self, mut probe: usize, mut old_pos: Pos)
908895
where Sz: Size
909896
{
910897
probe_loop!(probe < self.indices.len(), {
911898
let pos = &mut self.indices[probe];
912-
if let Some((i, hash_proxy)) = pos.resolve::<Sz>() {
913-
let entry_hash = hash_proxy.get_short_hash(&self.entries, i);
914-
// if existing element probed less than us, swap
915-
let their_dist = probe_distance(self.mask, entry_hash.into_hash(), probe);
916-
if their_dist < dist {
917-
swap(&mut old_pos, pos);
918-
dist = their_dist;
919-
}
920-
} else {
899+
if pos.is_none() {
921900
*pos = old_pos;
922901
break;
902+
} else {
903+
old_pos = replace(pos, old_pos);
923904
}
924-
dist += 1;
925905
});
926906
}
927907

0 commit comments

Comments
 (0)