board: optimize propegate_from calls (+4.7% perf)

This commit is contained in:
Simon Gardling 2025-03-11 16:16:39 -04:00
parent 72dc7b9a48
commit f84706ac40
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -225,11 +225,6 @@ impl Board {
get_board!(self, Piece::Black).set(coord, !is_white); get_board!(self, Piece::Black).set(coord, !is_white);
} }
const fn delete(&mut self, coord: CoordPair) {
get_board!(self, Piece::White).set(coord, false);
get_board!(self, Piece::Black).set(coord, false);
}
/// Return a modified [`Board`] with the piece placed at a position /// Return a modified [`Board`] with the piece placed at a position
/// Returns None if the move was invalid /// Returns None if the move was invalid
pub fn what_if(&self, coord: CoordPair, piece: Piece) -> Result<Self, &'static str> { pub fn what_if(&self, coord: CoordPair, piece: Piece) -> Result<Self, &'static str> {
@ -252,22 +247,17 @@ impl Board {
return Err("position is occupied"); return Err("position is occupied");
} }
if self.propegate_from(coord, piece) {
self.place_unchecked(coord, piece); self.place_unchecked(coord, piece);
if !self.propegate_from(coord) {
self.delete(coord);
Err("move would not propegate")
} else {
Ok(()) Ok(())
} else {
Err("move would not propegate")
} }
} }
/// Propegate the board and captures starting from a specific position /// Propegate the board and captures starting from a specific position
/// returns true if flips occurred /// returns true if flips occurred
fn propegate_from(&mut self, coord: CoordPair) -> bool { fn propegate_from(&mut self, coord: CoordPair, starting_color: Piece) -> bool {
let Some(starting_color) = self.get(coord) else {
return false;
};
let flip_mask = self.propegate_from_dry(coord, starting_color); let flip_mask = self.propegate_from_dry(coord, starting_color);
let did_flip = !flip_mask.is_empty(); let did_flip = !flip_mask.is_empty();