From 01ae7be70de7678e84eed69417c15c231a8d22c8 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Wed, 12 Feb 2025 10:14:48 -0500 Subject: [PATCH] bit board improvements --- src/bitboard.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/bitboard.rs b/src/bitboard.rs index afd0d32..6dd9330 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -10,19 +10,31 @@ impl BitBoard { BitBoard(0) } + const fn get_index(row: usize, col: usize) -> usize { + row * BOARD_SIZE + col + } + pub const fn get(&self, row: usize, col: usize) -> bool { - ((self.0 >> (row * BOARD_SIZE + col)) & 1) != 0 + ((self.0 >> Self::get_index(row, col)) & 1) != 0 } pub const fn set(&mut self, row: usize, col: usize, value: bool) { - let index = row * BOARD_SIZE + col; + let index = Self::get_index(row, col); if value { - self.0 |= 1 << index; // Set the bit at (row, col) to 1 + self.set_bit(index); // Set the bit at (row, col) to 1 } else { - self.0 &= !(1 << index); // Clear the bit at (row, col) + self.clear_bit(index); // Clear the bit at (row, col) } } + const fn clear_bit(&mut self, index: usize) { + self.0 &= !(1 << index) + } + + const fn set_bit(&mut self, index: usize) { + self.0 |= 1 << index + } + pub const fn count(&self) -> u32 { self.0.count_ones() }