Skip to content

Commit 0e4a3a2

Browse files
Make reverse actually use O(1) space
1 parent 25b0a21 commit 0e4a3a2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/map.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,9 +1760,22 @@ impl<K, V> OrderMapCore<K, V> {
17601760
}
17611761

17621762
fn reverse(&mut self) {
1763-
let side_index = self.save_hash_index();
17641763
self.entries.reverse();
1765-
self.restore_hash_index(side_index);
1764+
1765+
// No need to save hash indices, can easily calculate what they should
1766+
// be, given that this is an in-place reversal.
1767+
dispatch_32_vs_64!(self => apply_new_index(&mut self.indices, self.entries.len()));
1768+
1769+
fn apply_new_index<Sz>(indices: &mut [Pos], len: usize)
1770+
where
1771+
Sz: Size,
1772+
{
1773+
for pos in indices {
1774+
if let Some((i, _)) = pos.resolve::<Sz>() {
1775+
pos.set_pos::<Sz>(len - i);
1776+
}
1777+
}
1778+
}
17661779
}
17671780

17681781
fn save_hash_index(&mut self) -> Vec<usize> {

0 commit comments

Comments
 (0)