Struct shakmaty::bitboard::Bitboard

source ·
pub struct Bitboard(pub u64);
Expand description

A set of squares represented by a 64 bit integer mask.

§Examples

use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
// . 1 1 1 1 . . .
// . 1 . . . 1 . .
// . 1 . . . 1 . .
// . 1 . . 1 . . .
// . 1 1 1 . . . .
// . 1 . 1 . . . .
// . 1 . . 1 . . .
// . 1 . . . 1 . .

Tuple Fields§

§0: u64

Implementations§

source§

impl Bitboard

source

pub const fn from_square(sq: Square) -> Bitboard

A bitboard with a single square.

source

pub const fn from_rank(rank: Rank) -> Bitboard

Returns the bitboard containing all squares of the given rank.

source

pub const fn from_file(file: File) -> Bitboard

Returns the bitboard containing all squares of the given file.

source

pub const fn shift(self, offset: i32) -> Bitboard

Silently overflowing bitwise shift with a signed offset, << for positive values and >> for negative values.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.shift(-8), Bitboard(0x001e_2222_120e_0a12));
// . . . . . . . .
// . 1 1 1 1 . . .
// . 1 . . . 1 . .
// . 1 . . . 1 . .
// . 1 . . 1 . . .
// . 1 1 1 . . . .
// . 1 . 1 . . . .
// . 1 . . 1 . . .

assert_eq!(bitboard.shift(64), Bitboard(0));
assert_eq!(bitboard.shift(i32::MIN), Bitboard(0));
source

pub const fn any(self) -> bool

Tests if self is non-empty.

§Examples
use shakmaty::Bitboard;

assert!(Bitboard::BACKRANKS.any());
source

pub const fn is_empty(self) -> bool

Tests if self is empty.

§Examples
use shakmaty::Bitboard;

assert!(Bitboard::EMPTY.is_empty());
source

pub const fn contains(self, sq: Square) -> bool

Tests if self contains the given square.

source

pub fn add<T: Into<Bitboard>>(&mut self, squares: T)

Adds squares.

source

pub fn toggle<T: Into<Bitboard>>(&mut self, squares: T)

Toggles squares.

source

pub fn discard<T: Into<Bitboard>>(&mut self, squares: T)

Discards squares.

source

pub fn remove(&mut self, sq: Square) -> bool

Removes a square from the bitboard.

Returns true if the square was in the set. Use Bitboard::discard() if you do not care about the return value.

§Examples
use shakmaty::{Bitboard, Square};

let mut bitboard = Bitboard::FULL;
assert_eq!(bitboard.remove(Square::E4), true);
assert_eq!(bitboard.remove(Square::E4), false);
source

pub fn set(&mut self, square: Square, v: bool)

Conditionally adds or discards square.

source

pub fn clear(&mut self)

Clears all squares.

source

pub const fn intersect(self, squares: Bitboard) -> Bitboard

Returns the intersection of self and squares. Equivalent to bitwise &.

source

pub fn with<T: Into<Bitboard>>(self, squares: T) -> Bitboard

Returns the union of self and squares. Equivalent to bitwise |.

source

pub const fn with_const(self, squares: Bitboard) -> Bitboard

Same as the with method, but usable in const contexts.

source

pub fn without<T: Into<Bitboard>>(self, squares: T) -> Bitboard

Returns self without squares (set difference).

source

pub const fn without_const(self, squares: Bitboard) -> Bitboard

Same as the without method, but usable in const contexts.

source

pub fn toggled<T: Into<Bitboard>>(self, squares: T) -> Bitboard

Returns all squares that are in self or squares but not in both (symmetric set difference). Equivalent to bitwise ^.

source

pub const fn toggled_const(self, squares: Bitboard) -> Bitboard

Same as the toggled method, but usable in const contexts.

source

pub fn is_disjoint<T: Into<Bitboard>>(self, other: T) -> bool

Tests if self and other are disjoint.

source

pub const fn is_disjoint_const(self, other: Bitboard) -> bool

Same as the is_disjoint method, but usable in const contexts.

source

pub fn is_subset<T: Into<Bitboard>>(self, other: T) -> bool

Tests if self is a subset of other.

§Examples
use shakmaty::Bitboard;

assert!(Bitboard::DARK_SQUARES.is_subset(Bitboard::FULL));
source

pub const fn is_subset_const(self, other: Bitboard) -> bool

Same as the is_subset method, but usable in const contexts.

source

pub fn is_superset<T: Into<Bitboard>>(self, other: T) -> bool

Tests if self is a superset of other.

§Examples
use shakmaty::Bitboard;

assert!(Bitboard::FULL.is_superset(Bitboard::LIGHT_SQUARES));
source

pub const fn is_superset_const(self, other: Bitboard) -> bool

Same as the is_superset method, but usable in const contexts.

source

pub fn pop_front(&mut self) -> Option<Square>

Removes and returns the first square, if any.

source

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

Returns the first square, if any.

source

pub fn discard_first(&mut self)

Discards the first square.

source

pub const fn without_first(self) -> Bitboard

Returns self without the first square.

source

pub const fn isolate_first(self) -> Bitboard

Returns the bitboard with only the first square of self.

source

pub fn pop_back(&mut self) -> Option<Square>

Removes and returns the last square, if any.

source

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

Returns the last square.

source

pub fn discard_last(&mut self)

Discards the last square.

source

pub fn without_last(self) -> Bitboard

Returns self without the last square.

source

pub fn isolate_last(self) -> Bitboard

Returns the bitboard with only the last square of self.

source

pub const fn count(self) -> usize

Returns the number of squares in self.

§Examples
use shakmaty::Bitboard;

assert_eq!(Bitboard::CORNERS.count(), 4);
source

pub const fn more_than_one(self) -> bool

Tests if there is more than one square in self.

source

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

Gets the only square in the set, if there is exactly one.

§Examples
use shakmaty::{Bitboard, Square, Rank};

assert_eq!(Bitboard::from(Square::H5).single_square(), Some(Square::H5));
assert_eq!(Bitboard::from(Rank::First).single_square(), None);
source

pub const fn carry_rippler(self) -> CarryRippler

An iterator over the subsets of this bitboard.

§Examples
use shakmaty::Bitboard;

for subset in Bitboard::CENTER.carry_rippler() {
    assert!(subset.is_subset(Bitboard::CENTER));
}
source

pub const fn flip_vertical(self) -> Bitboard

Mirror the bitboard vertically.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.flip_vertical(), Bitboard(0x2212_0a0e_1222_221e));
// . 1 . . . 1 . .
// . 1 . . 1 . . .
// . 1 . 1 . . . .
// . 1 1 1 . . . .
// . 1 . . 1 . . .
// . 1 . . . 1 . .
// . 1 . . . 1 . .
// . 1 1 1 1 . . .
source

pub const fn flip_horizontal(self) -> Bitboard

Mirror the bitboard horizontally.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.flip_horizontal(), Bitboard(0x7844_4448_7050_4844));
// . . . 1 1 1 1 .
// . . 1 . . . 1 .
// . . 1 . . . 1 .
// . . . 1 . . 1 .
// . . . . 1 1 1 .
// . . . . 1 . 1 .
// . . . 1 . . 1 .
// . . 1 . . . 1 .
source

pub const fn flip_diagonal(self) -> Bitboard

Mirror the bitboard at the a1-h8 diagonal.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.flip_diagonal(), Bitboard(0x0000_6192_8c88_ff00));
// . . . . . . . .
// . . . . . . . .
// 1 . . . . 1 1 .
// . 1 . . 1 . . 1
// . . 1 1 . . . 1
// . . . 1 . . . 1
// 1 1 1 1 1 1 1 1
// . . . . . . . .
source

pub const fn flip_anti_diagonal(self) -> Bitboard

Mirror the bitboard at the h1-a8 diagonal.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.flip_anti_diagonal(), Bitboard(0x00ff_1131_4986_0000));
// . . . . . . . .
// 1 1 1 1 1 1 1 1
// 1 . . . 1 . . .
// 1 . . . 1 1 . .
// 1 . . 1 . . 1 .
// . 1 1 . . . . 1
// . . . . . . . .
// . . . . . . . .
source

pub const fn rotate_90(self) -> Bitboard

Rotate the bitboard 90 degrees clockwise.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.rotate_90(), Bitboard(0x00ff_888c_9261_0000));
// . . . . . . . .
// 1 1 1 1 1 1 1 1
// . . . 1 . . . 1
// . . 1 1 . . . 1
// . 1 . . 1 . . 1
// 1 . . . . 1 1 .
// . . . . . . . .
// . . . . . . . .
source

pub const fn rotate_180(self) -> Bitboard

Rotate the bitboard 180 degrees.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.rotate_180(), Bitboard(0x4448_5070_4844_4478));
// . . 1 . . . 1 .
// . . . 1 . . 1 .
// . . . . 1 . 1 .
// . . . . 1 1 1 .
// . . . 1 . . 1 .
// . . 1 . . . 1 .
// . . 1 . . . 1 .
// . . . 1 1 1 1 .
source

pub const fn rotate_270(self) -> Bitboard

Rotate the bitboard 270 degrees clockwise.

§Examples
use shakmaty::Bitboard;

let bitboard = Bitboard(0x1e22_2212_0e0a_1222);
assert_eq!(bitboard.rotate_270(), Bitboard(0x0000_8649_3111_ff00));
// . . . . . . . .
// . . . . . . . .
// . 1 1 . . . . 1
// 1 . . 1 . . 1 .
// 1 . . . 1 1 . .
// 1 . . . 1 . . .
// 1 1 1 1 1 1 1 1
// . . . . . . . .
source

pub const EMPTY: Bitboard = _

An empty bitboard.

// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
source

pub const FULL: Bitboard = _

A bitboard containing all squares.

// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
source

pub const DARK_SQUARES: Bitboard = _

All dark squares.

// . 1 . 1 . 1 . 1
// 1 . 1 . 1 . 1 .
// . 1 . 1 . 1 . 1
// 1 . 1 . 1 . 1 .
// . 1 . 1 . 1 . 1
// 1 . 1 . 1 . 1 .
// . 1 . 1 . 1 . 1
// 1 . 1 . 1 . 1 .
source

pub const LIGHT_SQUARES: Bitboard = _

All light squares.

// 1 . 1 . 1 . 1 .
// . 1 . 1 . 1 . 1
// 1 . 1 . 1 . 1 .
// . 1 . 1 . 1 . 1
// 1 . 1 . 1 . 1 .
// . 1 . 1 . 1 . 1
// 1 . 1 . 1 . 1 .
// . 1 . 1 . 1 . 1
source

pub const CORNERS: Bitboard = _

The four corner squares.

// 1 . . . . . . 1
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// 1 . . . . . . 1
source

pub const BACKRANKS: Bitboard = _

The backranks.

// 1 1 1 1 1 1 1 1
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// 1 1 1 1 1 1 1 1
source

pub const CENTER: Bitboard = _

The four center squares.

// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . 1 1 . . .
// . . . 1 1 . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
source

pub const NORTH: Bitboard = _

The northern half of the board.

// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
source

pub const SOUTH: Bitboard = _

The southern half of the board.

// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// . . . . . . . .
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
// 1 1 1 1 1 1 1 1
source

pub const WEST: Bitboard = _

The western half of the board.

// 1 1 1 1 . . . .
// 1 1 1 1 . . . .
// 1 1 1 1 . . . .
// 1 1 1 1 . . . .
// 1 1 1 1 . . . .
// 1 1 1 1 . . . .
// 1 1 1 1 . . . .
// 1 1 1 1 . . . .
source

pub const EAST: Bitboard = _

The eastern half of the board.

// . . . . 1 1 1 1
// . . . . 1 1 1 1
// . . . . 1 1 1 1
// . . . . 1 1 1 1
// . . . . 1 1 1 1
// . . . . 1 1 1 1
// . . . . 1 1 1 1
// . . . . 1 1 1 1

Trait Implementations§

source§

impl Binary for Bitboard

source§

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

Formats the value using the given formatter.
source§

impl<T> BitAnd<T> for Bitboard
where T: Into<Bitboard>,

§

type Output = Bitboard

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: T) -> Bitboard

Performs the & operation. Read more
source§

impl<T> BitAndAssign<T> for Bitboard
where T: Into<Bitboard>,

source§

fn bitand_assign(&mut self, rhs: T)

Performs the &= operation. Read more
source§

impl<T> BitOr<T> for Bitboard
where T: Into<Bitboard>,

§

type Output = Bitboard

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: T) -> Bitboard

Performs the | operation. Read more
source§

impl<T> BitOrAssign<T> for Bitboard
where T: Into<Bitboard>,

source§

fn bitor_assign(&mut self, rhs: T)

Performs the |= operation. Read more
source§

impl<T> BitXor<T> for Bitboard
where T: Into<Bitboard>,

§

type Output = Bitboard

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: T) -> Bitboard

Performs the ^ operation. Read more
source§

impl<T> BitXorAssign<T> for Bitboard
where T: Into<Bitboard>,

source§

fn bitxor_assign(&mut self, rhs: T)

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 Extend<Square> for Bitboard

source§

fn extend<T: IntoIterator<Item = Square>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl From<Bitboard> for u64

source§

fn from(bb: Bitboard) -> u64

Converts to this type from the input type.
source§

impl From<File> for Bitboard

source§

fn from(file: File) -> Bitboard

Converts to this type from the input type.
source§

impl From<Rank> for Bitboard

source§

fn from(rank: Rank) -> Bitboard

Converts to this type from the input type.
source§

impl From<Square> for Bitboard

source§

fn from(sq: Square) -> Bitboard

Converts to this type from the input type.
source§

impl From<u64> for Bitboard

source§

fn from(bb: u64) -> Bitboard

Converts to this type from the input type.
source§

impl FromIterator<Square> for Bitboard

source§

fn from_iter<T>(iter: T) -> Bitboard
where T: IntoIterator<Item = Square>,

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 = IntoIter

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

fn into_iter(self) -> IntoIter

Creates an iterator from a value. Read more
source§

impl LowerHex for Bitboard

source§

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

Formats the value using the given formatter.
source§

impl Not for Bitboard

§

type Output = Bitboard

The resulting type after applying the ! operator.
source§

fn not(self) -> Bitboard

Performs the unary ! operation. Read more
source§

impl Octal for Bitboard

source§

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

Formats the value using the given formatter.
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 UpperHex for Bitboard

source§

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

Formats the value using the given formatter.
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> ToOwned for T
where T: Clone,

§

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

§

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.