diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index 5c05b3f..7d51d50 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -343,12 +343,9 @@ impl FutureMoves { } node.children.retain_mut(|c| { - if let Some(new_c) = index_map[*c] { - *c = new_c; - true - } else { - panic!("node is not kept in move"); - } + debug_assert!(index_map.get(*c).unwrap().is_some()); + *c = unsafe { index_map.get_unchecked(*c).unwrap_unchecked() }; + true }); node @@ -358,3 +355,75 @@ impl FutureMoves { self.current_root = index_map[root]; } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn prune_tree_test() { + let mut futm = FutureMoves::new(Piece::Black, 0, 0); + futm.arena.push(Move { + i: 0, + j: 0, + board: Board::new(), + winner: Winner::None, + parent: None, + children: vec![1, 3, 4], + value: 0, + self_value: 0, + color: Piece::Black, + lazy_children: false, + }); + + futm.current_root = Some(0); + + // child 1 + futm.arena.push(Move::new( + 0, + 0, + Board::new(), + Piece::White, + false, + Piece::Black, + Some(0), + )); + + // dummy + futm.arena.push(Move::new( + 1234, + 1234, + Board::new(), + Piece::White, + false, + Piece::Black, + None, + )); + + futm.arena.push(Move::new( + 0, + 0, + Board::new(), + Piece::White, + false, + Piece::Black, + Some(0), + )); + futm.arena.push(Move::new( + 0, + 0, + Board::new(), + Piece::White, + false, + Piece::Black, + Some(0), + )); + assert_eq!(futm.arena_len(), 5); + + futm.refocus_tree(); + assert_eq!(futm.arena_len(), 4, "{:?}", futm.arena); + assert_eq!(futm.arena[0].children.len(), 3); + + assert_ne!(futm.arena[2].i, 1234, "dummy value still exists"); + } +}