This commit is contained in:
Simon Gardling 2025-02-22 14:08:39 -05:00
parent 8e22768987
commit 2305a8065f
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -314,21 +314,23 @@ impl FutureMoves {
let mut index_map = vec![None; self.arena.len()];
let new_start: Vec<(usize, (usize, Move))> = retain
let new_start: Vec<(usize, usize, Move)> = retain
.into_iter()
.enumerate() // old_idx
.zip(self.arena.drain(..))
.flat_map(|((old_idx, keep), node)| keep.then_some((old_idx, node))) // filter out unrelated nodes
.filter(|&((_, keep), _)| keep) // filter out un-related nodes
.map(|((old_idx, _), node)| (old_idx, node))
.enumerate() // new_idx
.map(|(a, (b, c))| (a, b, c))
.collect();
for &(new_idx, (old_idx, _)) in &new_start {
for &(new_idx, old_idx, _) in &new_start {
index_map[old_idx] = Some(new_idx);
}
self.arena = new_start
.into_iter()
.map(|(_, (_, mut node))| {
.map(|(_, _, mut node)| {
if let Some(parent) = node.parent.as_mut() {
if let Some(new_parent) = index_map[*parent] {
*parent = new_parent;
@ -339,7 +341,10 @@ impl FutureMoves {
}
for c in node.children.as_mut_slice() {
debug_assert!(index_map.get(*c).unwrap().is_some());
debug_assert!(
index_map.get(*c).unwrap().is_some(),
"index_map should contain the child's index"
);
*c = unsafe { index_map.get_unchecked(*c).unwrap_unchecked() };
}
@ -426,18 +431,15 @@ mod tests {
#[test]
fn expand_layer_test() {
let mut futm = FutureMoves::new(Piece::Black, 1, 1);
futm.arena.push(Move {
i: 0,
j: 0,
board: Board::new().starting_pos(),
winner: Winner::None,
parent: None,
children: Vec::new(),
value: 0,
self_value: 0,
color: Piece::Black,
lazy_children: false,
});
futm.arena.push(Move::new(
0,
0,
Board::new().starting_pos(),
Piece::Black,
false,
Piece::Black,
None,
));
futm.update_root_idx_raw(0);