Skip to content

Commit

Permalink
chore: stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Jan 24, 2025
1 parent 555037a commit 82e7bb6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
9 changes: 5 additions & 4 deletions games/src/interface/bitboard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::{
CartesianFile, CartesianRank, CartesianSquare, RepresentableType, SetType,
derive_set, CartesianFile, CartesianRank, CartesianSquare,
RepresentableType, SetType,
};

#[derive(
Expand All @@ -24,9 +25,9 @@ pub struct CartesianSquareSet<const F: u8, const R: u8>(u64);

impl<const F: u8, const R: u8> CartesianSquareSet<F, R> {
/// The BitBoard containing Squares in the first File.
const FIRST_FILE: Self = Self(0);
const FIRST_FILE: Self = derive_set!(@first_file u64 F R);
/// The BitBoard containing Squares in the first Rank.
const FIRST_RANK: Self = Self(0);
const FIRST_RANK: Self = derive_set!(@first_rank u64 F R);

/// north returns a new Self with all the squares shifted to the north.
#[must_use]
Expand Down Expand Up @@ -68,7 +69,7 @@ impl<const F: u8, const R: u8> CartesianSquareSet<F, R> {
impl<const F: u8, const R: u8> SetType<u64, CartesianSquare<F, R>, u8>
for CartesianSquareSet<F, R>
{
const EMPTY: Self = Self(0);
const EMPTY: Self = derive_set!(@universe_bb u64 F R);
const UNIVERSE: Self = Self(0);
}

Expand Down
40 changes: 22 additions & 18 deletions games/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,7 @@ pub(crate) use set_type;

// derive_set resolves into some of the fundamental sets for a SetType/BitBoard.
macro_rules! derive_set {
// @universe resolves into the universal set.
(@universe $base:tt $elem:tt) => {{
(@universe $base:tt $elem:path) => {{
use $crate::interface::RepresentableType;
Self(
// (1 << Square::N) - 1
Expand All @@ -607,37 +606,42 @@ macro_rules! derive_set {
)
}};

// @universe resolves into the universal set.
(@universe_bb $base:ident $F:ident $R:ident) => {{
Self(
// (1 << Square::N) - 1
match (1 as $base).checked_shl($F as u32 * $R as u32) {
Some(universe) => universe.wrapping_sub(1),
None => (-1i8) as $base,
},
)
}};

// @first_rank resolves into the set containing the Squares in the first Rank.
(@first_rank $bb:tt) => {{
use $crate::interface::{RepresentableType, BitBoardType, SquareType};
(@first_rank $base:ident $F:ident $R:ident) => {{
Self(
// (1 << File::N) - 1
match (1 as <$bb as BitBoardType>::Base)
.checked_shl(<<<$bb as BitBoardType>::Square as SquareType>::File as RepresentableType<u8>>::N as u32) {
Some(universe) => universe.wrapping_sub(1),
None => 1,
}
match (1 as $base).checked_shl($F as u32) {
Some(universe) => universe.wrapping_sub(1),
None => 1,
},
)
}};

// @first_file resolves into the set containing the Squares in the first File.
(@first_file $bb:tt) => {{
use $crate::interface::{RepresentableType, BitBoardType, SquareType};

(@first_file $base:ident $F:ident $R:ident) => {{
let mut i = 0u32;
let mut file = 0 as <$bb as BitBoardType>::Base;
let file_n = <<
<$bb as BitBoardType>::Square as SquareType
>::File as RepresentableType<u8>>::N as u32;
let mut file = 0 as $base;
let file_n = $F as u32;

loop {
// i < file_n
if i >= file_n {
break
break;
}

// file |= 1 << (File::N * i)
file |= match (1 as <$bb as BitBoardType>::Base).checked_shl(file_n * i) {
file |= match (1 as $base).checked_shl(file_n * i) {
Some(file) => file,
None => 0,
};
Expand Down
8 changes: 4 additions & 4 deletions games/src/interface/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ impl<const C: u8> Iterator for CartesianFile<C> {
impl<const C: u8> DoubleEndedIterator for CartesianFile<C> {
fn next_back(&mut self) -> Option<Self::Item> {
let idx = (*self).into();
self.0 = idx - 1;
if idx < C {
if idx > 0 {
self.0 = idx - 1;
Some(Self(idx))
} else {
None
Expand Down Expand Up @@ -347,8 +347,8 @@ impl<const C: u8> Iterator for CartesianRank<C> {
impl<const C: u8> DoubleEndedIterator for CartesianRank<C> {
fn next_back(&mut self) -> Option<Self::Item> {
let idx = (*self).into();
self.0 = idx - 1;
if idx < C {
if idx > 0 {
self.0 = idx - 1;
Some(Self(idx))
} else {
None
Expand Down

0 comments on commit 82e7bb6

Please sign in to comment.