pub struct Bitboard(/* private fields */);
Expand description
Bitboard is a wrapper around a u64 integer, where each bit represents some or none on its corresponding chess board square. It is used to encode a set of some arbitrary homogenous data for an entire chess board.
Implementations§
Source§impl Bitboard
Bitboard Constants
impl Bitboard
Bitboard Constants
pub const EMPTY: Bitboard
pub const BLACK_SQUARES: Bitboard
pub const WHITE_SQUARES: Bitboard
pub const RANK_1: Bitboard
pub const RANK_2: Bitboard
pub const RANK_3: Bitboard
pub const RANK_4: Bitboard
pub const RANK_5: Bitboard
pub const RANK_6: Bitboard
pub const RANK_7: Bitboard
pub const RANK_8: Bitboard
pub const FILE_A: Bitboard
pub const FILE_B: Bitboard
pub const FILE_C: Bitboard
pub const FILE_D: Bitboard
pub const FILE_E: Bitboard
pub const FILE_F: Bitboard
pub const FILE_G: Bitboard
pub const FILE_H: Bitboard
pub const NOT_FILE_A: Bitboard
pub const NOT_FILE_H: Bitboard
pub const KINGSIDE_BETWEEN: Bitboard
pub const QUEENSIDE_BETWEEN: Bitboard
pub const KINGSIDE_PASS: Bitboard
pub const QUEENSIDE_PASS: Bitboard
Source§impl Bitboard
impl Bitboard
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if there are no squares in self, false otherwise.
Sourcepub const fn count_squares(&self) -> u32
pub const fn count_squares(&self) -> u32
Returns number of squares present. Equivalent to number of bits in binary representation that are ‘1’. When compiled for targets with BMI1 popcnt instruction, should resolve to a single instruction.
Sourcepub fn has_square<I: SquareIndexable>(&self, idx: I) -> bool
pub fn has_square<I: SquareIndexable>(&self, idx: I) -> bool
Returns true if index is populated.
Sourcepub fn set_square<I: SquareIndexable>(&mut self, idx: I)
pub fn set_square<I: SquareIndexable>(&mut self, idx: I)
Sets bit index to 1.
Sourcepub fn clear_square<I: SquareIndexable>(&mut self, idx: I)
pub fn clear_square<I: SquareIndexable>(&mut self, idx: I)
Sets bit index to 0.
Sourcepub fn toggle_square<I: SquareIndexable>(&mut self, idx: I)
pub fn toggle_square<I: SquareIndexable>(&mut self, idx: I)
Toggles bit index. 0 -> 1, 1 -> 0.
Sourcepub fn clear_square_and_above<I: SquareIndexable>(&mut self, idx: I)
pub fn clear_square_and_above<I: SquareIndexable>(&mut self, idx: I)
Clears squares including and above target square. TODO: There is a BMI2 instruction BZHI to zero high hits starting at position, however the instruction is not used with &mut self, only self. Figure out how to get get compiler to use BZHI.
Sourcepub fn clear_square_and_below<I: SquareIndexable>(&mut self, idx: I)
pub fn clear_square_and_below<I: SquareIndexable>(&mut self, idx: I)
Clears squares including and below target square. TODO: Find a BMI instruction, if applicable. Maybe BLSMSK.
Sourcepub fn clear_lowest_square(&mut self)
pub fn clear_lowest_square(&mut self)
Clears the lowest square from self. If there are no squares, does nothing. When compiled for targets that support BMI1 BLSR (reset lowest set bit), should resolve to a single instruction and a move.
Sourcepub fn get_lowest_square(&self) -> Option<Square>
pub fn get_lowest_square(&self) -> Option<Square>
Returns the lowest square that exists in bitboard, or None if bitboard has no squares.
Sourcepub const fn contains(&self, other: &Bitboard) -> bool
pub const fn contains(&self, other: &Bitboard) -> bool
Returns true if other is a subset of self. If all squares of other are in self, then other is a subset of self.
Sourcepub const fn has_any(&self, other: &Bitboard) -> bool
pub const fn has_any(&self, other: &Bitboard) -> bool
Returns true if self has any squares that are in other. In other words, if there is any overlap, return true.
Sourcepub const fn to_north(&self) -> Self
pub const fn to_north(&self) -> Self
Returns new Bitboard with all squares shifted 1 square north (ex: D4 -> D5).
Sourcepub const fn to_south(&self) -> Self
pub const fn to_south(&self) -> Self
Returns new Bitboard with all squares shifted 1 square south (ex: D4 -> D3).
Sourcepub const fn to_east(&self) -> Self
pub const fn to_east(&self) -> Self
Returns new Bitboard with all squares shifted 1 square east (ex: D4 -> E4). To prevent wrapping of bit to other rank, bits are removed on FILE_A.
Sourcepub const fn to_west(&self) -> Self
pub const fn to_west(&self) -> Self
Returns new Bitboard with all squares shifted 1 square west (ex: D4 -> C4). To prevent wrapping of bit to other rank, bits are removed on FILE_H.
Sourcepub const fn to_north_east(&self) -> Self
pub const fn to_north_east(&self) -> Self
Returns new Bitboard with all squares shifted 1 square north east (ex: D4 -> E5). To prevent wrapping of bit to other rank, bits are removed on FILE_A.
Sourcepub const fn to_north_west(&self) -> Self
pub const fn to_north_west(&self) -> Self
Returns new Bitboard with all squares shifted 1 square north west (ex: D4 -> C5). To prevent wrapping of bit to other rank, bits are removed on FILE_H.
Sourcepub const fn to_south_east(&self) -> Self
pub const fn to_south_east(&self) -> Self
Returns new Bitboard with all squares shifted 1 square south east (ex: D4 -> E3). To prevent wrapping of bit to other rank, bits are removed on FILE_A.
Sourcepub const fn to_south_west(&self) -> Self
pub const fn to_south_west(&self) -> Self
Returns new Bitboard with all squares shifted 1 square south west (ex: D4 -> C3). To prevent wrapping of bit to other rank, bits are removed on FILE_H.
Sourcepub fn squares(&self) -> Vec<Square>
pub fn squares(&self) -> Vec<Square>
Returns a vector of all the Squares represented in the Bitboard.
§Examples
let squares = vec![Square::A1, Square::D7];
let mut board = Bitboard::EMPTY;
squares.iter().for_each(|square| board.set_square(*square));
assert_eq!(board.squares(), squares);
§Algorithm
For each ‘1’ bit in Bitboard:
- Count trailing zeros. This is equal to the Square index, and is of 0-63.
- Get shift index by shifting by square index.
- Use shift index to remove bit from Bitboard.
- Convert square index to a Square and add to list.
Trait Implementations§
Source§impl BitAndAssign for Bitboard
impl BitAndAssign for Bitboard
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&=
operation. Read moreSource§impl BitOrAssign<&Bitboard> for Bitboard
impl BitOrAssign<&Bitboard> for Bitboard
Source§fn bitor_assign(&mut self, rhs: &Bitboard)
fn bitor_assign(&mut self, rhs: &Bitboard)
|=
operation. Read moreSource§impl BitOrAssign for Bitboard
impl BitOrAssign for Bitboard
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read moreSource§impl<I: SquareIndexable> From<I> for Bitboard
impl<I: SquareIndexable> From<I> for Bitboard
Source§impl IntoIterator for Bitboard
Allow the squares of a Bitboard to be iterated directly and cheaply.
impl IntoIterator for Bitboard
Allow the squares of a Bitboard to be iterated directly and cheaply.