diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 03:12:54 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-01-23 03:12:54 +0300 |
commit | be1a88383619900d30d2e63ad91e041ec10392ae (patch) | |
tree | ed45d26f7c69beb736895f1e0047b416c504e057 | |
parent | 393a3e57d0323e4403ad73192cecc1bc6e471435 (diff) | |
download | chessnost-be1a88383619900d30d2e63ad91e041ec10392ae.tar.gz |
feat: implement bitboard serialization
-rw-r--r-- | src/bitboard.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/bitboard.rs b/src/bitboard.rs index 6312dd8..b05803c 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -44,6 +44,21 @@ pub fn bitscan(bb: Bitboard) -> u8 { pop_count(bb - 1) } +pub fn bitscan_and_reset(bb: &mut Bitboard) -> u8 { + let ls1b_bitboard = ls1b(*bb); + *bb ^= ls1b_bitboard; + bitscan(ls1b_bitboard) +} + +pub fn serialize_bitboard(bb: Bitboard) -> Vec<u8> { + let mut serialized = Vec::with_capacity(64); + let mut bitboard = bb; + while bitboard > 0 { + serialized.push(bitscan_and_reset(&mut bitboard)); + } + serialized +} + #[cfg(test)] mod tests { use super::*; @@ -73,5 +88,22 @@ mod tests { fn test_bitscan_with_non_single_bb() { 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); + } } |