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