bitboard: add test for compounding directions

This commit is contained in:
Simon Gardling 2025-03-17 13:42:13 -04:00
parent c0eac2b175
commit 680a2d084c
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -8,7 +8,7 @@ use static_assertions::const_assert;
pub type BitBoardInner = u64;
#[derive(Copy, Clone, PartialEq, Eq, Allocative)]
#[derive(Copy, Clone, PartialEq, Eq, Allocative, Debug)]
pub struct BitBoard(BitBoardInner);
// BitBoard should be big enough to fit all points on the board
@ -24,14 +24,14 @@ impl BitBoard {
self.get_by_index(coord.0)
}
pub const fn set(&mut self, coord: CoordPair, value: bool) {
self.set_by_index(coord.0, value);
}
const fn get_by_index(&self, index: CoordPairInner) -> bool {
((self.0 >> index) & 0b1) != 0b0
}
pub const fn set(&mut self, coord: CoordPair, value: bool) {
self.set_by_index(coord.0, value);
}
const fn set_by_index(&mut self, index: CoordPairInner, value: bool) {
// PERF! branchless setting of bit (~+3% perf bump)
self.0 &= !(0b1 << index); // clear bit
@ -133,4 +133,15 @@ mod test {
b.set((2, 4).into(), false);
assert!(!b.get((2, 4).into()));
}
#[test]
fn dir_multi() {
let mut base = BitBoard::from_coord(CoordPair::from_axes(4, 4));
base = base.southeast(1);
base = base.southeast(1);
assert_eq!(
base,
BitBoard::from_coord(CoordPair::from_axes(4, 4)).southeast(2)
);
}
}