1
0
Fork 0

make bitboards public

This commit is contained in:
Paul-Nicolas Madelaine 2025-10-19 15:13:51 +02:00
parent 5f1bc22797
commit 04dfbb23aa
4 changed files with 49 additions and 37 deletions

View file

@ -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<Square> {
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<Square> {
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 {