2022-07-15 21:53:08 +02:00
|
|
|
/// An [Iterator](std::iter::Iterator) of [Square](crate::board::Square) contained in a
|
|
|
|
/// [Bitboard](crate::board::Bitboard).
|
|
|
|
pub struct BitboardIterator(pub(crate) u64);
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 18:21:14 +02:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
let size = self.0.count_ones() as usize;
|
|
|
|
|
|
|
|
(size, Some(size))
|
|
|
|
}
|
2022-07-15 21:53:08 +02:00
|
|
|
}
|
2022-07-18 18:21:14 +02:00
|
|
|
|
|
|
|
impl ExactSizeIterator for BitboardIterator {}
|
|
|
|
|
|
|
|
impl std::iter::FusedIterator for BitboardIterator {}
|