diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 05:02:30 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 05:10:48 +0300 |
commit | 9a41a379a8a1f33b6a9d28f1d5ff3c1d0826e1f5 (patch) | |
tree | 58a7ec5f6bdbc701436c011e82a5b6817483bb1c /src/bitboard.rs | |
parent | c61c22208de60465db7f9cf58cdfa5d204826daf (diff) | |
download | chessnost-9a41a379a8a1f33b6a9d28f1d5ff3c1d0826e1f5.tar.gz |
refactor: use Square as type everywhere
Diffstat (limited to 'src/bitboard.rs')
-rw-r--r-- | src/bitboard.rs | 36 |
1 files changed, 16 insertions, 20 deletions
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<u8> { +/// Convert bitboard into the list of squares +pub fn serialize_bitboard(bb: Bitboard) -> Vec<Square> { 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] @@ -90,20 +94,12 @@ mod tests { } #[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)); } } |