56 lines
1.2 KiB
Rust
56 lines
1.2 KiB
Rust
use crate::board::{BOARD_AREA, BOARD_SIZE};
|
|
use bitvec::prelude::*;
|
|
use static_assertions::const_assert;
|
|
|
|
// BitBoard should be big enough to fit all points on the board
|
|
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= BOARD_AREA);
|
|
|
|
/// Backing Type of BitBoard
|
|
type BBBaseType = u64;
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
pub struct BitBoard(BitArr!(for BOARD_AREA, in BBBaseType, Lsb0));
|
|
|
|
impl Default for BitBoard {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl BitBoard {
|
|
pub const fn new() -> Self {
|
|
Self(bitarr!(BBBaseType, Lsb0; 0; BOARD_AREA))
|
|
}
|
|
|
|
const fn get_index(row: usize, col: usize) -> usize {
|
|
row * BOARD_SIZE + col
|
|
}
|
|
|
|
pub fn get(&self, row: usize, col: usize) -> bool {
|
|
self.0[Self::get_index(row, col)]
|
|
}
|
|
|
|
pub fn set(&mut self, row: usize, col: usize, value: bool) {
|
|
self.0.set(Self::get_index(row, col), value);
|
|
}
|
|
|
|
pub fn count(&self) -> usize {
|
|
self.0.count_ones()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn set_and_get() {
|
|
let mut b = BitBoard::new();
|
|
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));
|
|
}
|
|
}
|