[BitBoard] Clean up the API; implement some traits
Clean up the BitBoard API by renaming methods with simpler names. run-help Redo Redo the implementation of a couple methods to be more succinct.
This commit is contained in:
parent
480a009e63
commit
daf5c86792
1 changed files with 20 additions and 17 deletions
|
@ -25,7 +25,7 @@ impl BitBoard {
|
|||
|
||||
#[must_use]
|
||||
pub const fn empty() -> BitBoard {
|
||||
BitBoard(0)
|
||||
Self::EMPTY
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
@ -79,8 +79,8 @@ impl BitBoard {
|
|||
|
||||
impl BitBoard {
|
||||
#[must_use]
|
||||
pub const fn as_bits(&self) -> &u64 {
|
||||
&self.0
|
||||
pub const fn as_bits(&self) -> u64 {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Returns `true` if the [`BitBoard`] has no bits set.
|
||||
|
@ -113,7 +113,7 @@ impl BitBoard {
|
|||
}
|
||||
|
||||
/// Returns `true` if this [`BitBoard`] has the bit corresponding to `square` set.
|
||||
pub fn is_set(self, square: Square) -> bool {
|
||||
pub fn contains(self, square: Square) -> bool {
|
||||
let square_bitboard: BitBoard = square.into();
|
||||
!(self & square_bitboard).is_empty()
|
||||
}
|
||||
|
@ -132,14 +132,14 @@ impl BitBoard {
|
|||
self.0.count_ones()
|
||||
}
|
||||
|
||||
pub fn set_square(&mut self, sq: Square) {
|
||||
let sq_bb: BitBoard = sq.into();
|
||||
*self |= sq_bb
|
||||
pub fn set(&mut self, square: Square) {
|
||||
let square_bitboard: BitBoard = square.into();
|
||||
self.0 |= square_bitboard.0
|
||||
}
|
||||
|
||||
pub fn clear_square(&mut self, sq: Square) {
|
||||
let sq_bb: BitBoard = sq.into();
|
||||
*self &= !sq_bb
|
||||
pub fn clear(&mut self, square: Square) {
|
||||
let square_bitboard: BitBoard = square.into();
|
||||
self.0 &= !square_bitboard.0
|
||||
}
|
||||
|
||||
/// Returns `true` if this BitBoard represents a single square.
|
||||
|
@ -202,6 +202,12 @@ impl Default for BitBoard {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<BitBoard> for u64 {
|
||||
fn from(value: BitBoard) -> Self {
|
||||
value.as_bits()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<File> for BitBoard {
|
||||
fn from(value: File) -> Self {
|
||||
library::FILES[*value.as_index() as usize]
|
||||
|
@ -228,13 +234,10 @@ impl From<Square> for BitBoard {
|
|||
|
||||
impl FromIterator<Square> for BitBoard {
|
||||
fn from_iter<T: IntoIterator<Item = Square>>(iter: T) -> Self {
|
||||
let mut builder = BitBoardBuilder::empty();
|
||||
|
||||
for sq in iter {
|
||||
builder = builder.square(sq)
|
||||
}
|
||||
|
||||
builder.build()
|
||||
iter.into_iter().fold(BitBoard::EMPTY, |mut acc, sq| {
|
||||
acc.set(sq);
|
||||
acc
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue