refocus_tree: use hashmap for index_map

This commit is contained in:
Simon Gardling 2025-04-03 12:14:46 -04:00
parent f4007c2d42
commit 82b679095c
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -528,7 +528,11 @@ impl FutureMoves {
stack.extend(self.arena[idx].children.iter());
}
let mut index_map = vec![None; self.arena.len()];
let mut index_map: HashMap<
usize,
usize,
BuildHasherDefault<nohash_hasher::NoHashHasher<usize>>,
> = HashMap::with_capacity_and_hasher(self.arena.len(), BuildHasherDefault::default());
let (indexes, moves): (Vec<(usize, usize)>, Vec<Move>) = retain
.into_iter()
@ -541,7 +545,7 @@ impl FutureMoves {
.unzip();
for (new_idx, old_idx) in indexes {
index_map[old_idx] = Some(new_idx);
index_map.insert(old_idx, new_idx);
}
self.arena = moves
@ -549,13 +553,12 @@ impl FutureMoves {
.map(|mut node| {
node.parent = node
.parent
.and_then(|parent| index_map.get(parent))
.and_then(|&x| x);
.and_then(|parent| index_map.get(&parent).copied());
for c in node.children.as_mut_slice() {
*c = index_map
.get(*c)
.and_then(|&x| x)
.get(c)
.copied()
.expect("index_map should contain the child's index");
}
@ -563,7 +566,7 @@ impl FutureMoves {
})
.collect();
self.current_root = index_map[root];
self.current_root = index_map.get(&root).copied();
}
}