Add '*Assign' operators to 'Bitboard'

This commit is contained in:
Bruno BELANYI 2022-07-25 17:34:11 +02:00
parent 02768b6d96
commit 072a1ea13c
3 changed files with 85 additions and 5 deletions

View file

@ -115,6 +115,22 @@ impl std::ops::Shr<usize> for Bitboard {
}
}
/// Treat bitboard as a set of squares, shift each square's index left by the amount given.
impl std::ops::ShlAssign<usize> for Bitboard {
#[inline(always)]
fn shl_assign(&mut self, rhs: usize) {
*self = *self << rhs;
}
}
/// Treat bitboard as a set of squares, shift each square's index right by the amount given.
impl std::ops::ShrAssign<usize> for Bitboard {
#[inline(always)]
fn shr_assign(&mut self, rhs: usize) {
*self = *self >> rhs;
}
}
/// Treat bitboard as a set of squares, and invert the set.
impl std::ops::Not for Bitboard {
type Output = Bitboard;
@ -145,6 +161,22 @@ impl std::ops::BitOr<Square> for Bitboard {
}
}
/// Treat each bitboard as a set of squares, keep squares that are in either sets.
impl std::ops::BitOrAssign<Bitboard> for Bitboard {
#[inline(always)]
fn bitor_assign(&mut self, rhs: Bitboard) {
*self = *self | rhs;
}
}
/// Treat the [Square] as a singleton bitboard, and apply the operator.
impl std::ops::BitOrAssign<Square> for Bitboard {
#[inline(always)]
fn bitor_assign(&mut self, rhs: Square) {
*self = *self | rhs;
}
}
/// Treat each bitboard as a set of squares, keep squares that are in both sets.
impl std::ops::BitAnd<Bitboard> for Bitboard {
type Output = Bitboard;
@ -165,6 +197,22 @@ impl std::ops::BitAnd<Square> for Bitboard {
}
}
/// Treat each bitboard as a set of squares, keep squares that are in both sets.
impl std::ops::BitAndAssign<Bitboard> for Bitboard {
#[inline(always)]
fn bitand_assign(&mut self, rhs: Bitboard) {
*self = *self & rhs;
}
}
/// Treat the [Square] as a singleton bitboard, and apply the operator.
impl std::ops::BitAndAssign<Square> for Bitboard {
#[inline(always)]
fn bitand_assign(&mut self, rhs: Square) {
*self = *self & rhs;
}
}
/// Treat each bitboard as a set of squares, keep squares that are in exactly one of either set.
impl std::ops::BitXor<Bitboard> for Bitboard {
type Output = Bitboard;
@ -185,6 +233,22 @@ impl std::ops::BitXor<Square> for Bitboard {
}
}
/// Treat each bitboard as a set of squares, keep squares that are in exactly one of either set.
impl std::ops::BitXorAssign<Bitboard> for Bitboard {
#[inline(always)]
fn bitxor_assign(&mut self, rhs: Bitboard) {
*self = *self ^ rhs;
}
}
/// Treat the [Square] as a singleton bitboard, and apply the operator.
impl std::ops::BitXorAssign<Square> for Bitboard {
#[inline(always)]
fn bitxor_assign(&mut self, rhs: Square) {
*self = *self ^ rhs;
}
}
/// Treat each bitboard as a set of squares, and substract one set from another.
impl std::ops::Sub<Bitboard> for Bitboard {
type Output = Bitboard;
@ -205,6 +269,22 @@ impl std::ops::Sub<Square> for Bitboard {
}
}
/// Treat each bitboard as a set of squares, and substract one set from another.
impl std::ops::SubAssign<Bitboard> for Bitboard {
#[inline(always)]
fn sub_assign(&mut self, rhs: Bitboard) {
*self = *self - rhs;
}
}
/// Treat the [Square] as a singleton bitboard, and apply the operator.
impl std::ops::SubAssign<Square> for Bitboard {
#[inline(always)]
fn sub_assign(&mut self, rhs: Square) {
*self = *self - rhs;
}
}
#[cfg(test)]
mod test {
use std::collections::HashSet;

View file

@ -139,7 +139,7 @@ impl Direction {
while !board.is_empty() {
board = self.move_board(board);
res = res | board;
res |= board;
if !(board & blockers).is_empty() {
break;
}

View file

@ -20,16 +20,16 @@ pub fn generate_rook_mask(square: Square) -> Bitboard {
let mask = {
let mut mask = Bitboard::EMPTY;
if square.file() != File::A {
mask = mask | File::A.into_bitboard()
mask |= File::A.into_bitboard()
};
if square.file() != File::H {
mask = mask | File::H.into_bitboard()
mask |= File::H.into_bitboard()
};
if square.rank() != Rank::First {
mask = mask | Rank::First.into_bitboard()
mask |= Rank::First.into_bitboard()
};
if square.rank() != Rank::Eighth {
mask = mask | Rank::Eighth.into_bitboard()
mask |= Rank::Eighth.into_bitboard()
};
mask
};