Struct Bitboard

Source
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

Source§

impl Bitboard

Source

pub const fn bits(&self) -> u64

Get copy of internal u64 representation of this Bitboard.

Source

pub const fn is_empty(&self) -> bool

Returns true if there are no squares in self, false otherwise.

Source

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.

Source

pub fn has_square<I: SquareIndexable>(&self, idx: I) -> bool

Returns true if index is populated.

Source

pub fn set_square<I: SquareIndexable>(&mut self, idx: I)

Sets bit index to 1.

Source

pub fn clear_square<I: SquareIndexable>(&mut self, idx: I)

Sets bit index to 0.

Source

pub fn toggle_square<I: SquareIndexable>(&mut self, idx: I)

Toggles bit index. 0 -> 1, 1 -> 0.

Source

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.

Source

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.

Source

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.

Source

pub fn get_lowest_square(&self) -> Option<Square>

Returns the lowest square that exists in bitboard, or None if bitboard has no squares.

Source

pub fn remove(&mut self, other: &Bitboard)

Remove all squares in other from self.

Source

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.

Source

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.

Source

pub const fn to_north(&self) -> Self

Returns new Bitboard with all squares shifted 1 square north (ex: D4 -> D5).

Source

pub const fn to_south(&self) -> Self

Returns new Bitboard with all squares shifted 1 square south (ex: D4 -> D3).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 BitAnd<Bitboard> for &Bitboard

Source§

type Output = Bitboard

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Bitboard) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAnd for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAndAssign for Bitboard

Source§

fn bitand_assign(&mut self, rhs: Self)

Performs the &= operation. Read more
Source§

impl BitOr<&Bitboard> for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &Bitboard) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr<Bitboard> for &Bitboard

Source§

type Output = Bitboard

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Bitboard) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOr for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOrAssign<&Bitboard> for Bitboard

Source§

fn bitor_assign(&mut self, rhs: &Bitboard)

Performs the |= operation. Read more
Source§

impl BitOrAssign for Bitboard

Source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
Source§

impl BitXor for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl Clone for Bitboard

Source§

fn clone(&self) -> Bitboard

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Bitboard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I: SquareIndexable> From<&[I]> for Bitboard

Source§

fn from(square_index_slice: &[I]) -> Self

Converts to this type from the input type.
Source§

impl From<File> for Bitboard

Source§

fn from(file: File) -> Self

Converts to this type from the input type.
Source§

impl<I: SquareIndexable> From<I> for Bitboard

Source§

fn from(square_index: I) -> Self

Converts to this type from the input type.
Source§

impl From<Rank> for Bitboard

Source§

fn from(rank: Rank) -> Self

Converts to this type from the input type.
Source§

impl IntoIterator for Bitboard

Allow the squares of a Bitboard to be iterated directly and cheaply.

Source§

type Item = Square

The type of the elements being iterated over.
Source§

type IntoIter = BitboardSquareIterator

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Not for &Bitboard

Source§

type Output = Bitboard

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl Not for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl PartialEq for Bitboard

Source§

fn eq(&self, other: &Bitboard) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Bitboard

Source§

impl Eq for Bitboard

Source§

impl StructuralPartialEq for Bitboard

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V