Skip to content

Commit

Permalink
chore: simplify public interface management
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Sep 8, 2024
1 parent 1dbd3a0 commit fd3e61c
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 252 deletions.
156 changes: 0 additions & 156 deletions games/src/interface/bitboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,159 +183,3 @@ where
Self::FIRST_RANK << (<Self::Square as SquareType>::File::N * rank.into() as usize)
}
}

/// bitboard_type generates a new BitBoard with the given type as its base
/// representation. The base type must implement num_traits::int::PrimInt so
/// that the BitBoardType trait can be implemented.
///
/// # Examples
///
/// ```
/// bitboard_type! {
/// BitBoardTypeName: u64 {
/// Square = OurSquareType;
/// Empty = OurEmptyBitBoard;
/// Universe = OurUniverseBitBoard;
/// FirstFile = OurFirstFileBitBoard;
/// FirstRank = OurFirstRankBitBoard;
/// }
/// }
/// ```
macro_rules! bitboard_type {
($name:tt : $typ:tt {
Square = $sq:tt;
Empty = $empty:expr;
Universe = $universe:expr;
FirstFile = $first_file:expr;
FirstRank = $first_rank:expr;
} ) => {
#[derive(
Copy,
Clone,
PartialEq,
Eq,
num_derive::FromPrimitive,
derive_more::BitOr,
derive_more::BitAnd,
derive_more::BitXor,
derive_more::Shl,
derive_more::Shr,
derive_more::BitAndAssign,
derive_more::BitOrAssign,
derive_more::BitXorAssign,
derive_more::ShlAssign,
derive_more::ShrAssign,
derive_more::SubAssign,
)]
pub struct $name(pub $typ);

impl $crate::interface::BitBoardType for $name {
type Base = $typ;
type Square = $sq;

const EMPTY: Self = $empty;
const UNIVERSE: Self = $universe;
const FIRST_FILE: Self = $first_file;
const FIRST_RANK: Self = $first_rank;
}

impl Iterator for $name {
type Item = $sq;

/// next pops the next Square from the BitBoard and returns it.
fn next(&mut self) -> Option<Self::Item> {
$crate::interface::BitBoardType::pop_lsb(self)
}
}

impl std::ops::Sub<usize> for $name {
type Output = Self;

fn sub(self, rhs: usize) -> Self::Output {
Self(self.0 - rhs as u64)
}
}

impl From<$typ> for $name {
fn from(num: $typ) -> Self {
Self(num)
}
}

impl From<$name> for $typ {
fn from(value: $name) -> Self {
value.0
}
}

impl From<$sq> for $name {
fn from(square: $sq) -> Self {
Self(1 << square as u64)
}
}

impl std::ops::Not for $name {
type Output = Self;

/// Returns the complementary BitBoard of `self`.
fn not(self) -> Self::Output {
// ! will set the unused bits so remove them with an &.
Self(!self.0) & <Self as $crate::interface::BitBoardType>::UNIVERSE
}
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl std::ops::Sub for $name {
type Output = Self;

/// Returns the difference of `self` and `rhs` as a new BitBoard.
fn sub(self, rhs: Self) -> Self::Output {
self & !rhs
}
}

#[allow(clippy::suspicious_arithmetic_impl)]
impl std::ops::BitOr<$sq> for $name {
type Output = Self;

/// Returns the union of `self` and `rhs` as a new BitBoard.
fn bitor(self, rhs: $sq) -> Self::Output {
self | Self::from(rhs)
}
}

impl std::ops::Sub<$sq> for $name {
type Output = Self;

/// Returns the BitBoard obtained on removing `rhs` from `self`.
fn sub(self, rhs: $sq) -> Self::Output {
self & !Self::from(rhs)
}
}

// Display a bitboard as ASCII art with 0s and 1s.
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut string_rep = String::from("");
for rank in <<$sq as $crate::interface::SquareType>::Rank as strum::IntoEnumIterator>::iter().rev() {
for file in <<$sq as $crate::interface::SquareType>::File as strum::IntoEnumIterator>::iter() {
let square = <$sq as $crate::interface::SquareType>::new(file, rank);
string_rep += if $crate::interface::BitBoardType::contains(*self, square) { "1 " } else { "0 " };
}

string_rep += "\n";
}

write!(f, "{string_rep}")
}
}

impl std::fmt::Debug for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self)
}
}
};
}

pub(crate) use bitboard_type;
Loading

0 comments on commit fd3e61c

Please sign in to comment.