From 1b63f56042cff25273b33cb8a091e155781ce504 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 25 Apr 2024 07:56:53 -0700 Subject: [PATCH] [bitboard, core] Make library getters const; parameterize Library attributes - Add chessfriend_core::Color::NUM - All the library getters can be const. - Use the constants from the core library to define the length of the slices in the Library struct. --- bitboard/src/library.rs | 59 ++++++++++++++++++++--------------------- core/src/colors.rs | 6 ++++- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/bitboard/src/library.rs b/bitboard/src/library.rs index c92388a..7a45906 100644 --- a/bitboard/src/library.rs +++ b/bitboard/src/library.rs @@ -74,7 +74,7 @@ pub(super) fn library() -> &'static MoveLibrary { macro_rules! library_getter { ($name:ident) => { - pub(super) fn $name(&self, sq: Square) -> BitBoard { + pub(super) const fn $name(&self, sq: Square) -> BitBoard { self.$name[sq as usize] } }; @@ -83,29 +83,29 @@ macro_rules! library_getter { #[derive(Debug)] pub(super) struct MoveLibrary { // Rays - rays: [[BitBoard; 8]; Square::NUM], + rays: [[BitBoard; Direction::NUM]; Square::NUM], // Piecewise move tables - pawn_attacks: [[BitBoard; 64]; 2], - pawn_pushes: [[BitBoard; 64]; 2], - knight_moves: [BitBoard; 64], - bishop_moves: [BitBoard; 64], - rook_moves: [BitBoard; 64], - queen_moves: [BitBoard; 64], - king_moves: [BitBoard; 64], + pawn_attacks: [[BitBoard; Square::NUM]; Color::NUM], + pawn_pushes: [[BitBoard; Square::NUM]; Color::NUM], + knight_moves: [BitBoard; Square::NUM], + bishop_moves: [BitBoard; Square::NUM], + rook_moves: [BitBoard; Square::NUM], + queen_moves: [BitBoard; Square::NUM], + king_moves: [BitBoard; Square::NUM], } impl MoveLibrary { const fn new() -> MoveLibrary { MoveLibrary { - rays: [[BitBoard::empty(); 8]; Square::NUM], - pawn_attacks: [[BitBoard::empty(); 64]; 2], - pawn_pushes: [[BitBoard::empty(); 64]; 2], - knight_moves: [BitBoard::empty(); 64], - bishop_moves: [BitBoard::empty(); 64], - rook_moves: [BitBoard::empty(); 64], - queen_moves: [BitBoard::empty(); 64], - king_moves: [BitBoard::empty(); 64], + rays: [[BitBoard::empty(); Direction::NUM]; Square::NUM], + pawn_attacks: [[BitBoard::empty(); Square::NUM]; Color::NUM], + pawn_pushes: [[BitBoard::empty(); Square::NUM]; Color::NUM], + knight_moves: [BitBoard::empty(); Square::NUM], + bishop_moves: [BitBoard::empty(); Square::NUM], + rook_moves: [BitBoard::empty(); Square::NUM], + queen_moves: [BitBoard::empty(); Square::NUM], + king_moves: [BitBoard::empty(); Square::NUM], } } @@ -125,23 +125,23 @@ impl MoveLibrary { fn init_orthogonal_rays(&mut self, sq: Square) { let sq_bb: BitBoard = sq.into(); let rays = &mut self.rays[sq as usize]; - rays[Direction::North as usize] = Self::generate_ray(sq_bb, BitBoard::shift_north_one); - rays[Direction::South as usize] = Self::generate_ray(sq_bb, BitBoard::shift_south_one); - rays[Direction::East as usize] = Self::generate_ray(sq_bb, BitBoard::shift_east_one); - rays[Direction::West as usize] = Self::generate_ray(sq_bb, BitBoard::shift_west_one); + rays[Direction::North as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_north_one); + rays[Direction::South as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_south_one); + rays[Direction::East as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_east_one); + rays[Direction::West as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_west_one); } fn init_diagonal_rays(&mut self, sq: Square) { let sq_bb: BitBoard = sq.into(); let rays = &mut self.rays[sq as usize]; rays[Direction::NorthEast as usize] = - Self::generate_ray(sq_bb, BitBoard::shift_north_east_one); + Self::_generate_ray(sq_bb, BitBoard::shift_north_east_one); rays[Direction::NorthWest as usize] = - Self::generate_ray(sq_bb, BitBoard::shift_north_west_one); + Self::_generate_ray(sq_bb, BitBoard::shift_north_west_one); rays[Direction::SouthWest as usize] = - Self::generate_ray(sq_bb, BitBoard::shift_south_west_one); + Self::_generate_ray(sq_bb, BitBoard::shift_south_west_one); rays[Direction::SouthEast as usize] = - Self::generate_ray(sq_bb, BitBoard::shift_south_east_one); + Self::_generate_ray(sq_bb, BitBoard::shift_south_east_one); } fn init_king_moves(&mut self, idx: usize) { @@ -225,8 +225,7 @@ impl MoveLibrary { }; } - #[inline] - fn generate_ray(sq: BitBoard, shift: fn(&BitBoard) -> BitBoard) -> BitBoard { + fn _generate_ray(sq: BitBoard, shift: fn(&BitBoard) -> BitBoard) -> BitBoard { let mut ray = BitBoard::empty(); let mut iter = shift(&sq); @@ -238,15 +237,15 @@ impl MoveLibrary { ray } - pub(super) fn ray(&self, sq: Square, dir: Direction) -> &BitBoard { + pub(super) const fn ray(&self, sq: Square, dir: Direction) -> &BitBoard { &self.rays[sq as usize][dir as usize] } - pub(super) fn pawn_pushes(&self, sq: Square, color: Color) -> BitBoard { + pub(super) const fn pawn_pushes(&self, sq: Square, color: Color) -> BitBoard { self.pawn_pushes[color as usize][sq as usize] } - pub(super) fn pawn_attacks(&self, sq: Square, color: Color) -> BitBoard { + pub(super) const fn pawn_attacks(&self, sq: Square, color: Color) -> BitBoard { self.pawn_attacks[color as usize][sq as usize] } diff --git a/core/src/colors.rs b/core/src/colors.rs index 62d7603..b60531b 100644 --- a/core/src/colors.rs +++ b/core/src/colors.rs @@ -11,7 +11,11 @@ pub enum Color { } impl Color { - pub const ALL: [Color; 2] = [Color::White, Color::Black]; + /// Number of colors + pub const NUM: usize = 2; + + /// Slice of all possible colors + pub const ALL: [Color; Color::NUM] = [Color::White, Color::Black]; pub fn iter() -> impl Iterator { Color::ALL.iter()