Compare commits
3 commits
8be7105ac5
...
4960286557
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | 4960286557 | ||
Bruno BELANYI | 524e3b2c76 | ||
Bruno BELANYI | ed38c6c12d |
|
@ -2,11 +2,11 @@
|
||||||
/// [Bitboard].
|
/// [Bitboard].
|
||||||
use crate::board::Bitboard;
|
use crate::board::Bitboard;
|
||||||
|
|
||||||
pub struct BitboardIterator(u64);
|
pub struct BitboardIterator(Bitboard);
|
||||||
|
|
||||||
impl BitboardIterator {
|
impl BitboardIterator {
|
||||||
pub fn new(board: Bitboard) -> Self {
|
pub fn new(board: Bitboard) -> Self {
|
||||||
Self(board.0)
|
Self(board)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,17 +14,15 @@ impl Iterator for BitboardIterator {
|
||||||
type Item = crate::board::Square;
|
type Item = crate::board::Square;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if self.0 == 0 {
|
let res = self.0.any_square();
|
||||||
None
|
if let Some(square) = res {
|
||||||
} else {
|
self.0 ^= square;
|
||||||
let lsb = self.0.trailing_zeros() as usize;
|
};
|
||||||
self.0 ^= 1 << lsb;
|
res
|
||||||
Some(crate::board::Square::from_index(lsb))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
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))
|
(size, Some(size))
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,6 +75,12 @@ impl Bitboard {
|
||||||
(self.0 & (self.0.wrapping_sub(1))) != 0
|
(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
|
/// 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
|
/// [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].
|
/// 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;
|
type Error = IntoSquareError;
|
||||||
|
|
||||||
fn try_into(self) -> Result<Square, Self::Error> {
|
fn try_into(self) -> Result<Square, Self::Error> {
|
||||||
let index = match self.count() {
|
if self.has_more_than_one() {
|
||||||
1 => self.0.trailing_zeros() as usize,
|
return Err(IntoSquareError::TooManySquares);
|
||||||
0 => return Err(IntoSquareError::EmptyBoard),
|
}
|
||||||
_ => return Err(IntoSquareError::TooManySquares),
|
self.any_square().ok_or(IntoSquareError::EmptyBoard)
|
||||||
};
|
|
||||||
Ok(Square::from_index(index))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn into_square() {
|
fn into_square() {
|
||||||
for square in Square::iter() {
|
for square in Square::iter() {
|
||||||
|
|
Loading…
Reference in a new issue