From 61e7a4e8d1cbaaf45e5263627bedc24ac7e16d23 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 17 Jul 2022 20:39:47 +0200 Subject: [PATCH] Add 'Bitboard::count' --- src/board/bitboard/mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/board/bitboard/mod.rs b/src/board/bitboard/mod.rs index 524cf33..d2c8921 100644 --- a/src/board/bitboard/mod.rs +++ b/src/board/bitboard/mod.rs @@ -37,6 +37,12 @@ impl Bitboard { Bitboard(0b00000000_11111111_00000000_00000000_00000000_00000000_00000000_00000000), Bitboard(0b11111111_00000000_00000000_00000000_00000000_00000000_00000000_00000000), ]; + + /// Count the number of pieces in the [Bitboard]. + #[inline(always)] + pub fn count(self) -> u32 { + self.0.count_ones() + } } impl Default for Bitboard { @@ -170,6 +176,13 @@ mod test { use super::*; use crate::board::square::*; + #[test] + fn count() { + assert_eq!(Bitboard::EMPTY.count(), 0); + assert_eq!(Bitboard::FILES[0].count(), 8); + assert_eq!(Bitboard::ALL.count(), 64); + } + #[test] fn iter() { assert_eq!(Bitboard::EMPTY.into_iter().collect::>(), Vec::new());