typing improvements
This commit is contained in:
parent
52e7ed7386
commit
f64aa16143
@ -12,7 +12,7 @@ pub type BitBoardInner = u64;
|
|||||||
pub struct BitBoard(BitBoardInner);
|
pub struct BitBoard(BitBoardInner);
|
||||||
|
|
||||||
// BitBoard should be big enough to fit all points on the board
|
// BitBoard should be big enough to fit all points on the board
|
||||||
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::BOARD_AREA as usize);
|
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::BOARD_AREA.0 as usize);
|
||||||
|
|
||||||
impl BitBoard {
|
impl BitBoard {
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
@ -38,7 +38,6 @@ impl BitBoard {
|
|||||||
self.0 |= (value as BitBoardInner) << index; // set bit (if needed)
|
self.0 |= (value as BitBoardInner) << index; // set bit (if needed)
|
||||||
}
|
}
|
||||||
|
|
||||||
// works on both `bitvec` and native (const on native)
|
|
||||||
pub const fn count(&self) -> usize {
|
pub const fn count(&self) -> usize {
|
||||||
self.0.count_ones() as usize
|
self.0.count_ones() as usize
|
||||||
}
|
}
|
||||||
@ -94,7 +93,7 @@ impl BitBoard {
|
|||||||
const fn col_mask(col: CoordAxis) -> Self {
|
const fn col_mask(col: CoordAxis) -> Self {
|
||||||
let mut mask = 0;
|
let mut mask = 0;
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < Board::BOARD_AREA {
|
while i < Board::BOARD_AREA.0 {
|
||||||
mask |= 1 << (i + col);
|
mask |= 1 << (i + col);
|
||||||
i += Board::BOARD_SIZE;
|
i += Board::BOARD_SIZE;
|
||||||
}
|
}
|
||||||
@ -154,7 +153,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn set_and_get() {
|
fn set_and_get() {
|
||||||
let mut b = BitBoard::new();
|
let mut b = BitBoard::new();
|
||||||
for c in 0..Board::BOARD_AREA {
|
for c in 0..Board::BOARD_AREA.0 {
|
||||||
assert!(
|
assert!(
|
||||||
!b.get(CoordPair(c)),
|
!b.get(CoordPair(c)),
|
||||||
"A just-initalized BitBoard should be completely empty"
|
"A just-initalized BitBoard should be completely empty"
|
||||||
|
|||||||
@ -6,13 +6,13 @@ use std::{cmp::Ordering, fmt};
|
|||||||
/// Map of all points on the board against some type T
|
/// Map of all points on the board against some type T
|
||||||
/// Used to index like so: example[i][j]
|
/// Used to index like so: example[i][j]
|
||||||
/// with each coordinate
|
/// with each coordinate
|
||||||
pub struct PosMap<T: Default>(ArrayVec<T, { Board::BOARD_AREA as usize }>);
|
pub struct PosMap<T: Default>(ArrayVec<T, { Board::BOARD_AREA.0 as usize }>);
|
||||||
|
|
||||||
impl<T: Default> PosMap<T> {
|
impl<T: Default> PosMap<T> {
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self(ArrayVec::from_iter(
|
Self(ArrayVec::from_iter(
|
||||||
(0..Board::BOARD_AREA).map(|_| Default::default()),
|
(0..Board::BOARD_AREA.0).map(|_| Default::default()),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +117,7 @@ impl Board {
|
|||||||
pub const BOARD_SIZE: CoordAxis = 8;
|
pub const BOARD_SIZE: CoordAxis = 8;
|
||||||
|
|
||||||
/// Area of the board
|
/// Area of the board
|
||||||
pub const BOARD_AREA: CoordAxis = Self::BOARD_SIZE.pow(2);
|
pub const BOARD_AREA: CoordPair = CoordPair(CoordPair(Self::BOARD_SIZE).0.pow(2));
|
||||||
|
|
||||||
/// Create a new empty board
|
/// Create a new empty board
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
@ -156,7 +156,7 @@ impl Board {
|
|||||||
|
|
||||||
/// Provides an iterator of all possible positions on the board
|
/// Provides an iterator of all possible positions on the board
|
||||||
pub fn all_positions() -> impl Iterator<Item = CoordPair> {
|
pub fn all_positions() -> impl Iterator<Item = CoordPair> {
|
||||||
(0..Self::BOARD_SIZE).flat_map(|i| (0..Self::BOARD_SIZE).map(move |j| (i, j).into()))
|
(0..Self::BOARD_AREA.0).map(CoordPair)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator of all possible moves a `color` can make
|
/// Returns an iterator of all possible moves a `color` can make
|
||||||
@ -248,12 +248,16 @@ impl Board {
|
|||||||
let count = flip_mask.count();
|
let count = flip_mask.count();
|
||||||
|
|
||||||
// Apply the flips
|
// Apply the flips
|
||||||
*self.board_mut(starting_color) |= flip_mask;
|
self.apply_flip_mask(starting_color, flip_mask);
|
||||||
*self.board_mut(starting_color.flip()) &= !flip_mask;
|
|
||||||
|
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn apply_flip_mask(&mut self, color: Piece, flip_mask: BitBoard) {
|
||||||
|
*self.board_mut(color) |= flip_mask;
|
||||||
|
*self.board_mut(color.flip()) &= !flip_mask;
|
||||||
|
}
|
||||||
|
|
||||||
/// Propegate piece captures originating from (i, j)
|
/// Propegate piece captures originating from (i, j)
|
||||||
/// DO NOT USE THIS ALONE, this should be called as a part of
|
/// DO NOT USE THIS ALONE, this should be called as a part of
|
||||||
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
||||||
|
|||||||
@ -9,7 +9,7 @@ pub type CoordAxis = u8;
|
|||||||
/// using u8 for this results in a ~3-6% perf increase
|
/// using u8 for this results in a ~3-6% perf increase
|
||||||
pub type CoordPairInner = u8;
|
pub type CoordPairInner = u8;
|
||||||
|
|
||||||
const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA as usize);
|
const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA.0 as usize);
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
|
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
|
||||||
pub struct CoordPair(pub CoordPairInner);
|
pub struct CoordPair(pub CoordPairInner);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user