Struct cozy_chess::BitBoard
source · 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
impl BitBoard
sourcepub const EMPTY: BitBoard = _
pub const EMPTY: BitBoard = _
An empty bitboard.
§Examples
assert_eq!(BitBoard::EMPTY, bitboard! {
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
});
sourcepub const FULL: BitBoard = _
pub const FULL: BitBoard = _
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
});
sourcepub const EDGES: BitBoard = Self::__EDGES
pub const EDGES: BitBoard = 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
});
sourcepub const CORNERS: BitBoard = Self::__CORNERS
pub const CORNERS: BitBoard = Self::__CORNERS
The corners of the board.
§Examples
assert_eq!(BitBoard::CORNERS, bitboard! {
X . . . . . . X
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
X . . . . . . X
});
sourcepub const DARK_SQUARES: BitBoard = Self::__DARK_SQUARES
pub const DARK_SQUARES: BitBoard = 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 .
});
sourcepub const LIGHT_SQUARES: BitBoard = Self::__LIGHT_SQUARES
pub const LIGHT_SQUARES: BitBoard = 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
});
sourcepub const fn flip_ranks(self) -> BitBoard
pub const fn flip_ranks(self) -> BitBoard
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 . . .
. . . . . . . .
. . . . . . . .
});
sourcepub const fn flip_files(self) -> BitBoard
pub const fn flip_files(self) -> BitBoard
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 . .
. . . . . . . .
. . . . . . . .
});
sourcepub const fn len(self) -> u32
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);
sourcepub const fn is_disjoint(self, other: BitBoard) -> bool
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));
sourcepub const fn is_subset(self, other: BitBoard) -> bool
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));
sourcepub const fn is_superset(self, other: BitBoard) -> bool
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));
sourcepub const fn next_square(self) -> Option<Square>
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));
sourcepub fn iter(self) -> BitBoardIter ⓘ
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);
}
sourcepub fn iter_subsets(self) -> BitBoardSubsetIter ⓘ
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 BitAndAssign for BitBoard
impl BitAndAssign for BitBoard
source§fn bitand_assign(&mut self, rhs: BitBoard)
fn bitand_assign(&mut self, rhs: BitBoard)
&=
operation. Read moresource§impl BitOrAssign for BitBoard
impl BitOrAssign for BitBoard
source§fn bitor_assign(&mut self, rhs: BitBoard)
fn bitor_assign(&mut self, rhs: BitBoard)
|=
operation. Read moresource§impl BitXorAssign for BitBoard
impl BitXorAssign for BitBoard
source§fn bitxor_assign(&mut self, rhs: BitBoard)
fn bitxor_assign(&mut self, rhs: BitBoard)
^=
operation. Read moresource§impl FromIterator<Square> for BitBoard
impl FromIterator<Square> for BitBoard
source§impl IntoIterator for BitBoard
impl IntoIterator for BitBoard
source§impl Ord for BitBoard
impl Ord for BitBoard
source§impl PartialEq for BitBoard
impl PartialEq for BitBoard
source§impl PartialOrd for BitBoard
impl PartialOrd for BitBoard
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign for BitBoard
impl SubAssign for BitBoard
source§fn sub_assign(&mut self, rhs: BitBoard)
fn sub_assign(&mut self, rhs: BitBoard)
-=
operation. Read more