1
0
Fork 0

update bitboards

This commit is contained in:
Paul-Nicolas Madelaine 2025-11-26 23:03:23 +01:00
parent 85c7c8eb52
commit ecb82b3120

View file

@ -2,24 +2,24 @@
use crate::board::*; use crate::board::*;
use core::iter::ExactSizeIterator;
use core::iter::FusedIterator;
/// A set of squares. /// A set of squares.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Bitboard(pub u64); pub struct Bitboard(pub u64);
impl Bitboard { impl Bitboard {
/// Makes a new, empty `Bitboard`.
#[inline] #[inline]
pub const fn new() -> Self { pub const fn new() -> Self {
Self(0) Self(0)
} }
/// Returns `true` if the bitboard contains the given square.
#[inline] #[inline]
pub const fn is_empty(&self) -> bool { pub const fn contains(&self, square: Square) -> bool {
self.0 == 0 self.0 & (1 << square as u8) != 0
} }
/// Returns the first square in the bitboard, if any.
#[inline] #[inline]
pub const fn first(&self) -> Option<Square> { pub const fn first(&self) -> Option<Square> {
let mask = self.0; let mask = self.0;
@ -29,19 +29,19 @@ impl Bitboard {
} }
} }
/// Returns the last square in the bitboard, if any.
#[inline] #[inline]
pub const fn last(&self) -> Option<Square> { pub const fn last(&self) -> Option<Square> {
let mask = self.0; let mask = self.0;
match mask { match mask {
0 => None, 0 => None,
_ => Some(unsafe { _ => Some(unsafe { Square::new_unchecked(63 ^ mask.leading_zeros() as u8) }),
Square::new_unchecked(63_u8.unchecked_sub(mask.leading_zeros() as u8))
}),
} }
} }
/// Removes the first square from the bitboard and returns it, if any.
#[inline] #[inline]
pub const fn pop(&mut self) -> Option<Square> { pub const fn pop_first(&mut self) -> Option<Square> {
let mask = &mut self.0; let mask = &mut self.0;
let square = match mask { let square = match mask {
0 => None, 0 => None,
@ -51,21 +51,32 @@ impl Bitboard {
square square
} }
/// Adds a square to the bitboard.
#[inline] #[inline]
pub const fn insert(&mut self, square: Square) { pub const fn insert(&mut self, square: Square) {
self.0 |= 1 << square as u8; self.0 |= 1 << square as u8;
} }
#[inline] /// Removes a square from the bitboard.
pub const fn contains(&self, square: Square) -> bool {
self.0 & (1 << square as u8) != 0
}
#[inline] #[inline]
pub const fn remove(&mut self, square: Square) { pub const fn remove(&mut self, square: Square) {
self.0 &= !(1 << square as u8); self.0 &= !(1 << square as u8);
} }
/// Returns the number of squares in the bitboard.
#[inline]
pub const fn len(&self) -> usize {
self.0.count_ones() as usize
}
/// Returns `true` if the bitboard contains no squares.
#[inline]
pub const fn is_empty(&self) -> bool {
self.0 == 0
}
/// Returns the mirror image of the bitboard. This is the set of the mirror images of all the
/// squares in the bitboard.
#[inline] #[inline]
pub const fn mirror(self) -> Bitboard { pub const fn mirror(self) -> Bitboard {
let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes(); let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes();
@ -138,7 +149,7 @@ impl Iterator for Bitboard {
type Item = Square; type Item = Square;
#[inline] #[inline]
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.pop() self.pop_first()
} }
#[inline] #[inline]
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
@ -174,11 +185,11 @@ impl Iterator for Bitboard {
acc acc
} }
} }
impl FusedIterator for Bitboard {} impl core::iter::FusedIterator for Bitboard {}
impl ExactSizeIterator for Bitboard { impl core::iter::ExactSizeIterator for Bitboard {
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
self.0.count_ones() as usize self.len()
} }
} }