From 04dfbb23aad91246fb9077896b2405b912c7df1b Mon Sep 17 00:00:00 2001 From: Paul-Nicolas Madelaine Date: Sun, 19 Oct 2025 15:13:51 +0200 Subject: [PATCH] make bitboards public --- src/bitboard.rs | 35 +++++++++++++++++++++++------------ src/board.rs | 20 ++++++++++---------- src/lib.rs | 2 +- src/setup.rs | 29 +++++++++++++++-------------- 4 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/bitboard.rs b/src/bitboard.rs index 746e225..41c3467 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -1,22 +1,28 @@ +//! Sets of squares. + use crate::board::*; use std::iter::ExactSizeIterator; use std::iter::FusedIterator; +/// A set of squares. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Bitboard(pub(crate) u64); impl Bitboard { + /// Returns an empty bitboard. #[inline] pub fn new() -> Self { Self(0) } + /// Returns `true` if the bitboard is empty. #[inline] pub fn is_empty(&self) -> bool { self.0 == 0 } + /// Returns the square in the bitboard with the smallest index. #[inline] pub fn first(&self) -> Option { let mask = self.0; @@ -26,6 +32,8 @@ impl Bitboard { } } + /// Removes the square in the bitboard with the smallest index and returns it, or `None` if the + /// bitboard is empty. #[inline] pub fn pop(&mut self) -> Option { let Self(ref mut mask) = self; @@ -37,13 +45,27 @@ impl Bitboard { square } + /// Inserts a square in the bitboard. #[inline] pub fn insert(&mut self, square: Square) { self.0 |= 1 << square as u8; } + /// Returns `true` if the bitboard contains the given square. #[inline] - pub fn trans(&self, direction: Direction) -> Self { + pub fn contains(&self, square: Square) -> bool { + self.0 & (1 << square as u8) != 0 + } + + /// Returns the mirror of the bitboard (see [`Setup::mirror`]). + #[inline] + pub fn mirror(self) -> Bitboard { + let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes(); + Self(u64::from_le_bytes([h, g, f, e, d, c, b, a])) + } + + #[inline] + pub(crate) fn trans(&self, direction: Direction) -> Self { match direction { Direction::North => Self(self.0 << 8), Direction::NorthEast => Self(self.0 << 9) & !File::A.bitboard(), @@ -55,17 +77,6 @@ impl Bitboard { Direction::NorthWest => Self(self.0 << 7) & !File::H.bitboard(), } } - - #[inline] - pub fn contains(&self, square: Square) -> bool { - self.0 & (1 << square as u8) != 0 - } - - #[inline] - pub fn mirror(self) -> Bitboard { - let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes(); - Self(u64::from_le_bytes([h, g, f, e, d, c, b, a])) - } } impl std::ops::BitOr for Bitboard { diff --git a/src/board.rs b/src/board.rs index a24ffa8..8367fe2 100644 --- a/src/board.rs +++ b/src/board.rs @@ -3,9 +3,9 @@ use crate::bitboard::*; macro_rules! container { - ($a:ident, $b:ident, $n:literal) => { + ($v: vis, $a:ident, $b:ident, $n:literal) => { #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub(crate) struct $b(pub(crate) [T; $n]); + $v struct $b(pub(crate) [T; $n]); #[allow(unused)] impl $b { #[inline] @@ -35,7 +35,7 @@ pub enum Color { Black, } -container!(Color, ByColor, 2); +container!(pub, Color, ByColor, 2); impl Color { #[inline] @@ -93,7 +93,7 @@ pub enum File { H, } -container!(File, ByFile, 8); +container!(pub(crate), File, ByFile, 8); impl File { #[inline] @@ -173,7 +173,7 @@ pub enum Rank { Eighth, } -container!(Rank, ByRank, 8); +container!(pub(crate), Rank, ByRank, 8); impl Rank { #[inline] @@ -259,7 +259,7 @@ pub enum Square{ A8, B8, C8, D8, E8, F8, G8, H8, } -container!(Square, BySquare, 64); +container!(pub, Square, BySquare, 64); impl Square { #[inline] @@ -310,7 +310,7 @@ impl Square { } #[inline] - pub(crate) fn bitboard(self) -> Bitboard { + pub fn bitboard(self) -> Bitboard { Bitboard(1 << self as u8) } @@ -525,7 +525,7 @@ impl Role { } #[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) struct ByRole(pub(crate) [T; 6]); +pub struct ByRole(pub(crate) [T; 6]); #[allow(unused)] impl ByRole { #[inline] @@ -597,7 +597,7 @@ pub(crate) enum Direction { West, } -container!(Direction, ByDirection, 8); +container!(pub(crate), Direction, ByDirection, 8); impl Direction { #[inline] @@ -639,7 +639,7 @@ pub enum CastlingSide { Long, } -container!(CastlingSide, ByCastlingSide, 2); +container!(pub(crate), CastlingSide, ByCastlingSide, 2); impl CastlingSide { #[inline] diff --git a/src/lib.rs b/src/lib.rs index 6c36c79..df4f342 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,10 +73,10 @@ //! - etc. pub(crate) mod array_vec; -pub(crate) mod bitboard; pub(crate) mod magics; pub(crate) mod rays; +pub mod bitboard; pub mod board; pub mod lookup; pub mod position; diff --git a/src/setup.rs b/src/setup.rs index bd3497f..294d218 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -261,7 +261,7 @@ impl Setup { let d = InitialisedLookup::init(); let blockers = self.p_b_q | self.n_b_k | self.r_q_k; - let pieces = self.bitboards(); + let pieces = self.pieces(); if Color::all() .into_iter() @@ -379,20 +379,9 @@ impl Setup { } } + /// Returns the bitboard of each kind of piece. #[inline] - pub(crate) fn get_role(&self, square: Square) -> Option { - let mask = square.bitboard(); - let bit0 = (self.p_b_q & mask).0 >> square as u8; - let bit1 = (self.n_b_k & mask).0 >> square as u8; - let bit2 = (self.r_q_k & mask).0 >> square as u8; - match bit0 | bit1 << 1 | bit2 << 2 { - 0 => None, - i => Some(unsafe { Role::transmute(i as u8) }), - } - } - - #[inline] - pub(crate) fn bitboards(&self) -> ByColor> { + pub fn pieces(&self) -> ByColor> { let Self { w, p_b_q, @@ -423,6 +412,18 @@ impl Setup { }) }) } + + #[inline] + pub(crate) fn get_role(&self, square: Square) -> Option { + let mask = square.bitboard(); + let bit0 = (self.p_b_q & mask).0 >> square as u8; + let bit1 = (self.n_b_k & mask).0 >> square as u8; + let bit2 = (self.r_q_k & mask).0 >> square as u8; + match bit0 | bit1 << 1 | bit2 << 2 { + 0 => None, + i => Some(unsafe { Role::transmute(i as u8) }), + } + } } impl std::fmt::Debug for Setup {