Compare commits

...

3 commits

Author SHA1 Message Date
Bruno BELANYI 4960286557 Simplify 'BitboardIterator'
All checks were successful
ci/woodpecker/push/check Pipeline was successful
2024-04-14 16:11:35 +01:00
Bruno BELANYI 524e3b2c76 Simplify 'TryInto<Square>' for 'Bitboard' 2024-04-14 16:11:35 +01:00
Bruno BELANYI ed38c6c12d Add 'Bitboard::any_square' 2024-04-14 16:11:35 +01:00
2 changed files with 35 additions and 16 deletions

View file

@ -2,11 +2,11 @@
/// [Bitboard].
use crate::board::Bitboard;
pub struct BitboardIterator(u64);
pub struct BitboardIterator(Bitboard);
impl BitboardIterator {
pub fn new(board: Bitboard) -> Self {
Self(board.0)
Self(board)
}
}
@ -14,17 +14,15 @@ impl Iterator for BitboardIterator {
type Item = crate::board::Square;
fn next(&mut self) -> Option<Self::Item> {
if self.0 == 0 {
None
} else {
let lsb = self.0.trailing_zeros() as usize;
self.0 ^= 1 << lsb;
Some(crate::board::Square::from_index(lsb))
}
let res = self.0.any_square();
if let Some(square) = res {
self.0 ^= square;
};
res
}
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.0.count_ones() as usize;
let size = self.0.count() as usize;
(size, Some(size))
}

View file

@ -75,6 +75,12 @@ impl Bitboard {
(self.0 & (self.0.wrapping_sub(1))) != 0
}
/// Return a [Square] from the board, or `None` if it is empty.
#[inline(always)]
pub fn any_square(self) -> Option<Square> {
Square::try_from_index(self.0.trailing_zeros() as usize)
}
/// Iterate over the power-set of a given [Bitboard], yielding each possible sub-set of
/// [Square] that belong to the [Bitboard]. In other words, generate all set of [Square] that
/// contain all, some, or none of the [Square] that are in the given [Bitboard].
@ -110,12 +116,10 @@ impl TryInto<Square> for Bitboard {
type Error = IntoSquareError;
fn try_into(self) -> Result<Square, Self::Error> {
let index = match self.count() {
1 => self.0.trailing_zeros() as usize,
0 => return Err(IntoSquareError::EmptyBoard),
_ => return Err(IntoSquareError::TooManySquares),
};
Ok(Square::from_index(index))
if self.has_more_than_one() {
return Err(IntoSquareError::TooManySquares);
}
self.any_square().ok_or(IntoSquareError::EmptyBoard)
}
}
@ -479,6 +483,23 @@ mod test {
);
}
#[test]
fn any_square() {
for square in Square::iter() {
assert_eq!(square.into_bitboard().any_square(), Some(square));
}
}
#[test]
fn any_square_empty() {
assert!(Bitboard::EMPTY.any_square().is_none());
}
#[test]
fn any_square_full_board() {
assert!(Bitboard::ALL.any_square().is_some());
}
#[test]
fn into_square() {
for square in Square::iter() {