diff --git a/src/board/bitboard/iterator.rs b/src/board/bitboard/iterator.rs index 7c01a9a..f5711ee 100644 --- a/src/board/bitboard/iterator.rs +++ b/src/board/bitboard/iterator.rs @@ -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 { - 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) { - let size = self.0.count_ones() as usize; + let size = self.0.count() as usize; (size, Some(size)) } diff --git a/src/board/bitboard/mod.rs b/src/board/bitboard/mod.rs index 9059235..3cb9d7b 100644 --- a/src/board/bitboard/mod.rs +++ b/src/board/bitboard/mod.rs @@ -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::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 for Bitboard { type Error = IntoSquareError; fn try_into(self) -> Result { - 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() {