From 9a41a379a8a1f33b6a9d28f1d5ff3c1d0826e1f5 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 23 Jan 2023 05:02:30 +0300 Subject: refactor: use Square as type everywhere --- src/bitboard.rs | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'src/bitboard.rs') diff --git a/src/bitboard.rs b/src/bitboard.rs index b05803c..d7a4fdb 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -1,3 +1,5 @@ +use crate::square::Square; + /// Finite set of up to 64 bits representing chess board squares pub type Bitboard = u64; @@ -28,7 +30,7 @@ pub fn pop_count(bb: Bitboard) -> u8 { } /// Return Bitboard with only Least Single Bit -pub fn ls1b(bb: Bitboard) -> u64 { +pub fn ls1b(bb: Bitboard) -> Bitboard { if bb == 0 { return 0 } @@ -39,18 +41,20 @@ pub fn ls1b(bb: Bitboard) -> u64 { /// /// WARNING: Only works for SINGLE Bitboards /// Useful for calculating bit-index of LS1B -pub fn bitscan(bb: Bitboard) -> u8 { +pub fn bitscan(bb: Bitboard) -> Square { debug_assert!(pop_count(bb) == 1, "Bitscan only works for SINGLE Bitboards!"); - pop_count(bb - 1) + Square::from(pop_count(bb - 1)) } -pub fn bitscan_and_reset(bb: &mut Bitboard) -> u8 { +/// Finds and removes ls1b in the bitboard, returning it's index +pub fn bitscan_and_reset(bb: &mut Bitboard) -> Square { let ls1b_bitboard = ls1b(*bb); *bb ^= ls1b_bitboard; bitscan(ls1b_bitboard) } -pub fn serialize_bitboard(bb: Bitboard) -> Vec { +/// Convert bitboard into the list of squares +pub fn serialize_bitboard(bb: Bitboard) -> Vec { let mut serialized = Vec::with_capacity(64); let mut bitboard = bb; while bitboard > 0 { @@ -77,10 +81,10 @@ mod tests { #[test] fn test_bitscan() { - assert_eq!(bitscan(4), 2); - assert_eq!(bitscan(16), 4); - assert_eq!(bitscan(64), 6); - assert_eq!(bitscan(128), 7); + assert_eq!(bitscan(4), Square::from(2)); + assert_eq!(bitscan(16), Square::from(4)); + assert_eq!(bitscan(64), Square::from(6)); + assert_eq!(bitscan(128), Square::from(7)); } #[test] @@ -89,21 +93,13 @@ mod tests { bitscan(5); } - #[test] - fn test_bitscan_and_reset() { - assert_eq!(bitscan(4), 2); - assert_eq!(bitscan(16), 4); - assert_eq!(bitscan(64), 6); - assert_eq!(bitscan(128), 7); - } - #[test] fn test_serialize_bitboard() { let bb = 1 << 4 | 1 << 15 | 1 << 60; let serialized = serialize_bitboard(bb); - assert_eq!(serialized[0], 4); - assert_eq!(serialized[1], 15); - assert_eq!(serialized[2], 60); + assert_eq!(serialized[0], Square::from(4)); + assert_eq!(serialized[1], Square::from(15)); + assert_eq!(serialized[2], Square::from(60)); } } -- cgit v1.2.3