abstract away more coordinates

This commit is contained in:
2025-02-27 21:00:04 -05:00
parent 956386fb66
commit 6ae1726010
12 changed files with 251 additions and 204 deletions

View File

@@ -1,6 +1,6 @@
use super::{
board::{Board, Coord},
misc::get_index,
board::Board,
coords::{CoordAxis, CoordPair},
};
use const_fn::const_fn;
use static_assertions::const_assert;
@@ -43,34 +43,34 @@ impl BitBoard {
}
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get(&self, row: Coord, col: Coord) -> bool {
self.get_by_index(get_index(row, col))
pub const fn get(&self, coord: CoordPair) -> bool {
self.get_by_index(coord.0)
}
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn set(&mut self, row: Coord, col: Coord, value: bool) {
self.set_by_index(get_index(row, col), value);
pub const fn set(&mut self, coord: CoordPair, value: bool) {
self.set_by_index(coord.0, value);
}
#[cfg(not(feature = "bitvec"))]
const fn get_by_index(&self, index: Coord) -> bool {
const fn get_by_index(&self, index: CoordAxis) -> bool {
((self.0 >> index) & 0b1) != 0b0
}
#[cfg(not(feature = "bitvec"))]
const fn set_by_index(&mut self, index: Coord, value: bool) {
const fn set_by_index(&mut self, index: CoordAxis, value: bool) {
// PERF! branchless setting of bit (~+3% perf bump)
self.0 &= !(0b1 << index); // clear bit
self.0 |= (value as BitBoardInner) << index; // set bit (if needed)
}
#[cfg(feature = "bitvec")]
pub fn get_by_index(&self, index: Coord) -> bool {
pub fn get_by_index(&self, index: CoordAxis) -> bool {
self.0[index as usize]
}
#[cfg(feature = "bitvec")]
pub fn set_by_index(&mut self, index: Coord, value: bool) {
pub fn set_by_index(&mut self, index: CoordAxis, value: bool) {
self.0.set(index as usize, value);
}
@@ -91,15 +91,15 @@ mod test {
for i in 0..Board::BOARD_SIZE {
for j in 0..Board::BOARD_SIZE {
assert!(
!b.get(i, j),
!b.get((i, j).into()),
"A just-initalized BitBoard should be completely empty"
)
}
}
assert!(!b.get(2, 4));
b.set(2, 4, true);
assert!(b.get(2, 4));
b.set(2, 4, false);
assert!(!b.get(2, 4));
assert!(!b.get((2, 4).into()));
b.set((2, 4).into(), true);
assert!(b.get((2, 4).into()));
b.set((2, 4).into(), false);
assert!(!b.get((2, 4).into()));
}
}