This commit is contained in:
Eryn Wells 2025-05-08 17:37:51 -07:00
parent d5cdf273c8
commit 091cc99cb3
42 changed files with 805 additions and 1662 deletions

View file

@ -144,7 +144,7 @@ impl BitBoard {
self.0 != 0
}
/// Returns `true` if this [BitBoard] has the bit corresponding to `square` set.
/// Returns `true` if this [`BitBoard`] has the bit corresponding to `square` set.
///
/// ## Examples
///
@ -179,7 +179,7 @@ impl BitBoard {
self.0.count_ones()
}
/// Set a square in this [BitBoard] by toggling the corresponding bit to 1.
/// Set a square in this [`BitBoard`] by toggling the corresponding bit to 1.
/// This always succeeds, even if the bit was already set.
///
/// ## Examples
@ -237,21 +237,20 @@ impl BitBoard {
&self,
direction: &IterationDirection,
) -> Box<dyn Iterator<Item = Square>> {
#[allow(clippy::cast_possible_truncation)]
fn index_to_square(index: usize) -> Square {
unsafe { Square::from_index_unchecked(index as u8) }
}
match direction {
IterationDirection::Leading => {
Box::new(LeadingBitScanner::new(self.0).map(index_to_square))
}
IterationDirection::Trailing => {
Box::new(TrailingBitScanner::new(self.0).map(index_to_square))
}
IterationDirection::Leading => Box::new(self.occupied_squares_leading()),
IterationDirection::Trailing => Box::new(self.occupied_squares_trailing()),
}
}
pub fn occupied_squares_leading(&self) -> LeadingBitScanner {
LeadingBitScanner::new(self.0)
}
pub fn occupied_squares_trailing(&self) -> TrailingBitScanner {
TrailingBitScanner::new(self.0)
}
#[must_use]
pub fn first_occupied_square(&self, direction: &IterationDirection) -> Option<Square> {
match direction {
@ -264,7 +263,7 @@ impl BitBoard {
/// board, starting at the leading (most-significant) end of the board. If
/// the board is empty, returns `None`.
#[must_use]
fn first_occupied_square_leading(self) -> Option<Square> {
pub fn first_occupied_square_leading(self) -> Option<Square> {
let leading_zeros = self._leading_zeros();
if leading_zeros < SQUARES_NUM {
unsafe {
@ -281,7 +280,7 @@ impl BitBoard {
/// board, starting at the trailing (least-significant) end of the board.
/// If the board is empty, returns `None`.
#[must_use]
fn first_occupied_square_trailing(self) -> Option<Square> {
pub fn first_occupied_square_trailing(self) -> Option<Square> {
let trailing_zeros = self._trailing_zeros();
if trailing_zeros < SQUARES_NUM {
@ -496,12 +495,9 @@ mod tests {
let bb = BitBoard(0b01010100);
let expected_squares = [Square::G1, Square::E1, Square::C1];
for (a, b) in bb
.occupied_squares(&IterationDirection::Leading)
.zip(expected_squares.iter().copied())
{
assert_eq!(a, b);
}
bb.occupied_squares(&IterationDirection::Leading)
.zip(expected_squares)
.for_each(|(a, b)| assert_eq!(a, b));
}
#[test]
@ -512,12 +508,9 @@ mod tests {
let expected_squares = [Square::H8, Square::F6, Square::C5, Square::E2, Square::D1];
for (a, b) in bb
.occupied_squares(&IterationDirection::Leading)
.zip(expected_squares.iter().cloned())
{
assert_eq!(a, b);
}
bb.occupied_squares(&IterationDirection::Leading)
.zip(expected_squares)
.for_each(|(a, b)| assert_eq!(a, b));
}
#[test]