pub struct BitBoard(pub u64);
Expand description

A bitboard. A bitboard is an ordered set of squares.

Operators are overloaded to work as set operations accordingly:

let a1 = Square::A1.bitboard();
let b1 = Square::B1.bitboard();
let c1 = Square::C1.bitboard();
let x = a1 | b1;
let y = a1 | c1;
// Union
assert_eq!(x | y, a1 | b1 | c1);
// Intersection
assert_eq!(x & y, a1);
// Symmetric difference
assert_eq!(x ^ y, b1 | c1);
// Difference
assert_eq!(x - y, b1);
// Complement
assert_eq!(!x, BitBoard::FULL - x);

Tuple Fields§

§0: u64

The backing u64. A square is present in the set if the bit at 1 << square as u8 is set.

Implementations§

source§

impl BitBoard

source

pub const EMPTY: Self = _

An empty bitboard.

§Examples
assert_eq!(BitBoard::EMPTY, bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
});
source

pub const FULL: Self = _

A bitboard with every square.

§Examples
assert_eq!(BitBoard::FULL, bitboard! {
    X X X X X X X X
    X X X X X X X X
    X X X X X X X X
    X X X X X X X X
    X X X X X X X X
    X X X X X X X X
    X X X X X X X X
    X X X X X X X X
});
source

pub const EDGES: Self = Self::__EDGES

The edges on the board.

§Examples
assert_eq!(BitBoard::EDGES, bitboard! {
    X X X X X X X X
    X . . . . . . X
    X . . . . . . X
    X . . . . . . X
    X . . . . . . X
    X . . . . . . X
    X . . . . . . X
    X X X X X X X X
});
source

pub const CORNERS: Self = Self::__CORNERS

The corners of the board.

§Examples
assert_eq!(BitBoard::CORNERS, bitboard! {
    X . . . . . . X
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    X . . . . . . X
});
source

pub const DARK_SQUARES: Self = Self::__DARK_SQUARES

The dark squares on the board.

§Examples
assert_eq!(BitBoard::DARK_SQUARES, bitboard! {
    . X . X . X . X
    X . X . X . X .
    . X . X . X . X
    X . X . X . X .
    . X . X . X . X
    X . X . X . X .
    . X . X . X . X
    X . X . X . X .
});
source

pub const LIGHT_SQUARES: Self = Self::__LIGHT_SQUARES

The light squares on the board.

§Examples
assert_eq!(BitBoard::LIGHT_SQUARES, bitboard! {
    X . X . X . X .
    . X . X . X . X
    X . X . X . X .
    . X . X . X . X
    X . X . X . X .
    . X . X . X . X
    X . X . X . X .
    . X . X . X . X
});
source

pub const fn flip_ranks(self) -> Self

Flip the bitboard’s ranks.

§Examples
let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
assert_eq!(bb.flip_ranks(), bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X . X . . .
    . . X X X X . .
    . . X . X X . .
    . . X X X . . .
    . . . . . . . .
    . . . . . . . .
});
source

pub const fn flip_files(self) -> Self

Flip the bitboard’s files.

§Examples
let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
assert_eq!(bb.flip_files(), bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . . X X X . .
    . . X X . X . .
    . . X X X X . .
    . . . X . X . .
    . . . . . . . .
    . . . . . . . .
});
source

pub const fn len(self) -> u32

Count the number of squares in the bitboard.

§Examples
assert_eq!(BitBoard::EMPTY.len(), 0);
let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
assert_eq!(bb.len(), 12);
source

pub const fn has(self, square: Square) -> bool

Check if a Square is set.

§Examples
let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
assert!(bb.has(Square::C3));
assert!(!bb.has(Square::B2));
source

pub const fn is_disjoint(self, other: BitBoard) -> bool

Check if a bitboard contains no squares in common with another.

§Examples
let bb_a = bitboard! {
    X X X . . . . .
    X . X X . . . .
    X X X X . . . .
    X . X . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
};
let bb_b = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . . . . .
    . . . . X X X .
    . . . . X . X X
    . . . . X X X X
    . . . . X . X .
};
assert!(bb_a.is_disjoint(bb_b));
source

pub const fn is_subset(self, other: BitBoard) -> bool

Check if a bitboard is a subset of another.

§Examples
let bb = bitboard! {
    . . . . . . . .
    . X X X X X . .
    . X X X X X X .
    . X X . X X X .
    . X X X X X X .
    . X X X X X . .
    . X X . X X . .
    . . . . . . . .
};
let subset = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
assert!(subset.is_subset(bb));
source

pub const fn is_superset(self, other: BitBoard) -> bool

Check if a bitboard is a superset of another.

§Examples
let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
let superset = bitboard! {
    . . . . . . . .
    . X X X X X . .
    . X X X X X X .
    . X X . X X X .
    . X X X X X X .
    . X X X X X . .
    . X X . X X . .
    . . . . . . . .
};
assert!(superset.is_superset(bb));
source

pub const fn is_empty(self) -> bool

Checks if the BitBoard is empty.

§Examples
assert!(BitBoard::EMPTY.is_empty());
let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
assert!(!bb.is_empty());
source

pub const fn next_square(self) -> Option<Square>

Grabs the first square if the bitboard is not empty.

§Examples
assert!(BitBoard::EMPTY.next_square().is_none());
let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
assert_eq!(bb.next_square(), Some(Square::C3));
source

pub fn iter(self) -> BitBoardIter

Iterate the squares in the bitboard, ordered by square.

§Examples
let bb = BitBoard::FULL;
let squares = &Square::ALL;
for (s1, &s2) in bb.iter().zip(squares) {
    assert_eq!(s1, s2);
}
source

pub fn iter_subsets(self) -> BitBoardSubsetIter

Iterate all subsets of a bitboard. Subsets are produced in lexicographic order; Each subset is greater than the last.

let bb = bitboard! {
    . . . . . . . .
    . . . . . . . .
    . . X X X . . .
    . . X . X X . .
    . . X X X X . .
    . . X . X . . .
    . . . . . . . .
    . . . . . . . .
};
for subset in bb.iter_subsets() {
    assert!(subset.is_subset(bb));
}

Trait Implementations§

source§

impl BitAnd for BitBoard

§

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 for BitBoard

§

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 for BitBoard

source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
source§

impl BitXor for BitBoard

§

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 BitXorAssign for BitBoard

source§

fn bitxor_assign(&mut self, rhs: Self)

Performs the ^= operation. Read more
source§

impl Clone for BitBoard

source§

fn clone(&self) -> BitBoard

Returns a copy 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 Default for BitBoard

source§

fn default() -> BitBoard

Returns the “default value” for a type. Read more
source§

impl From<File> for BitBoard

source§

fn from(value: File) -> Self

Converts to this type from the input type.
source§

impl From<Rank> for BitBoard

source§

fn from(value: Rank) -> Self

Converts to this type from the input type.
source§

impl From<Square> for BitBoard

source§

fn from(value: Square) -> Self

Converts to this type from the input type.
source§

impl FromIterator<Square> for BitBoard

source§

fn from_iter<T: IntoIterator<Item = Square>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl Hash for BitBoard

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl IntoIterator for BitBoard

§

type Item = Square

The type of the elements being iterated over.
§

type IntoIter = BitBoardIter

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

§

type Output = BitBoard

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl Ord for BitBoard

source§

fn cmp(&self, other: &BitBoard) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for BitBoard

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for BitBoard

source§

fn partial_cmp(&self, other: &BitBoard) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub for BitBoard

§

type Output = BitBoard

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl SubAssign for BitBoard

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.