optimize FutureMoves::prune_unrelated
This commit is contained in:
parent
0bd8dec65d
commit
dd783f0608
@ -3,6 +3,10 @@ name = "othello"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[profile.release]
|
||||
# for profiling
|
||||
debug = true
|
||||
|
||||
[dependencies]
|
||||
num = "0.4"
|
||||
rand = "0.9"
|
||||
|
||||
@ -223,16 +223,19 @@ impl FutureMoves {
|
||||
stack.extend(self.arena[idx].children.iter().copied());
|
||||
}
|
||||
|
||||
let mut new_arena = Vec::new();
|
||||
let mut new_arena = Vec::with_capacity(retain.iter().filter(|x| **x).count());
|
||||
let mut index_map = vec![None; self.arena.len()];
|
||||
|
||||
for (old_idx, _) in retain.iter().enumerate().rev().filter(|(_, a)| **a) {
|
||||
index_map[old_idx] = Some(new_arena.len());
|
||||
for (old_idx, keep) in retain.iter().enumerate().rev() {
|
||||
let mut node = self.arena.remove(old_idx);
|
||||
if *keep {
|
||||
index_map[old_idx] = Some(new_arena.len());
|
||||
|
||||
node.parent = node.parent.and_then(|p| index_map[p]);
|
||||
node.children = node.children.iter().filter_map(|&c| index_map[c]).collect();
|
||||
new_arena.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
self.arena = new_arena;
|
||||
self.current_root = index_map[root];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user