[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.
This commit is contained in:
parent
cad040e454
commit
1b63f56042
2 changed files with 34 additions and 31 deletions
|
@ -74,7 +74,7 @@ pub(super) fn library() -> &'static MoveLibrary {
|
||||||
|
|
||||||
macro_rules! library_getter {
|
macro_rules! library_getter {
|
||||||
($name:ident) => {
|
($name:ident) => {
|
||||||
pub(super) fn $name(&self, sq: Square) -> BitBoard {
|
pub(super) const fn $name(&self, sq: Square) -> BitBoard {
|
||||||
self.$name[sq as usize]
|
self.$name[sq as usize]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -83,29 +83,29 @@ macro_rules! library_getter {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(super) struct MoveLibrary {
|
pub(super) struct MoveLibrary {
|
||||||
// Rays
|
// Rays
|
||||||
rays: [[BitBoard; 8]; Square::NUM],
|
rays: [[BitBoard; Direction::NUM]; Square::NUM],
|
||||||
|
|
||||||
// Piecewise move tables
|
// Piecewise move tables
|
||||||
pawn_attacks: [[BitBoard; 64]; 2],
|
pawn_attacks: [[BitBoard; Square::NUM]; Color::NUM],
|
||||||
pawn_pushes: [[BitBoard; 64]; 2],
|
pawn_pushes: [[BitBoard; Square::NUM]; Color::NUM],
|
||||||
knight_moves: [BitBoard; 64],
|
knight_moves: [BitBoard; Square::NUM],
|
||||||
bishop_moves: [BitBoard; 64],
|
bishop_moves: [BitBoard; Square::NUM],
|
||||||
rook_moves: [BitBoard; 64],
|
rook_moves: [BitBoard; Square::NUM],
|
||||||
queen_moves: [BitBoard; 64],
|
queen_moves: [BitBoard; Square::NUM],
|
||||||
king_moves: [BitBoard; 64],
|
king_moves: [BitBoard; Square::NUM],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MoveLibrary {
|
impl MoveLibrary {
|
||||||
const fn new() -> MoveLibrary {
|
const fn new() -> MoveLibrary {
|
||||||
MoveLibrary {
|
MoveLibrary {
|
||||||
rays: [[BitBoard::empty(); 8]; Square::NUM],
|
rays: [[BitBoard::empty(); Direction::NUM]; Square::NUM],
|
||||||
pawn_attacks: [[BitBoard::empty(); 64]; 2],
|
pawn_attacks: [[BitBoard::empty(); Square::NUM]; Color::NUM],
|
||||||
pawn_pushes: [[BitBoard::empty(); 64]; 2],
|
pawn_pushes: [[BitBoard::empty(); Square::NUM]; Color::NUM],
|
||||||
knight_moves: [BitBoard::empty(); 64],
|
knight_moves: [BitBoard::empty(); Square::NUM],
|
||||||
bishop_moves: [BitBoard::empty(); 64],
|
bishop_moves: [BitBoard::empty(); Square::NUM],
|
||||||
rook_moves: [BitBoard::empty(); 64],
|
rook_moves: [BitBoard::empty(); Square::NUM],
|
||||||
queen_moves: [BitBoard::empty(); 64],
|
queen_moves: [BitBoard::empty(); Square::NUM],
|
||||||
king_moves: [BitBoard::empty(); 64],
|
king_moves: [BitBoard::empty(); Square::NUM],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,23 +125,23 @@ impl MoveLibrary {
|
||||||
fn init_orthogonal_rays(&mut self, sq: Square) {
|
fn init_orthogonal_rays(&mut self, sq: Square) {
|
||||||
let sq_bb: BitBoard = sq.into();
|
let sq_bb: BitBoard = sq.into();
|
||||||
let rays = &mut self.rays[sq as usize];
|
let rays = &mut self.rays[sq as usize];
|
||||||
rays[Direction::North as usize] = Self::generate_ray(sq_bb, BitBoard::shift_north_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::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::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::West as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_west_one);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_diagonal_rays(&mut self, sq: Square) {
|
fn init_diagonal_rays(&mut self, sq: Square) {
|
||||||
let sq_bb: BitBoard = sq.into();
|
let sq_bb: BitBoard = sq.into();
|
||||||
let rays = &mut self.rays[sq as usize];
|
let rays = &mut self.rays[sq as usize];
|
||||||
rays[Direction::NorthEast 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] =
|
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] =
|
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] =
|
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) {
|
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 ray = BitBoard::empty();
|
||||||
|
|
||||||
let mut iter = shift(&sq);
|
let mut iter = shift(&sq);
|
||||||
|
@ -238,15 +237,15 @@ impl MoveLibrary {
|
||||||
ray
|
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]
|
&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]
|
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]
|
self.pawn_attacks[color as usize][sq as usize]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,11 @@ pub enum Color {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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<Item = &'static Color> {
|
pub fn iter() -> impl Iterator<Item = &'static Color> {
|
||||||
Color::ALL.iter()
|
Color::ALL.iter()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue