Files
pokerfreak/src/read/census.rs
T

77 lines
2.1 KiB
Rust

use crate::card::Card;
/// Rank and suit tallies of hero's hole cards and the board.
/// Rank masks use the pip value (2..=14) as bit index, so an ace is bit 14.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Census {
hole: [u8; 15],
board: [u8; 15],
pub hole_mask: u16,
pub board_mask: u16,
/// Rank mask per suit (index = `Suit as usize`).
pub hole_suit: [u16; 4],
pub board_suit: [u16; 4],
pub board_len: usize,
}
/// The bit of one pip value (0 for anything outside the mask width).
pub fn bit(pip: u8) -> u16 {
1u16.checked_shl(pip as u32).unwrap_or(0)
}
/// Pip of the highest set bit, 0 for an empty mask.
pub fn top_pip(mask: u16) -> u8 {
if mask == 0 {
0
} else {
15 - mask.leading_zeros() as u8
}
}
impl Census {
pub fn of(hole: &[Card], board: &[Card]) -> Census {
let mut census = Census {
hole: [0; 15],
board: [0; 15],
hole_mask: 0,
board_mask: 0,
hole_suit: [0; 4],
board_suit: [0; 4],
board_len: board.len(),
};
for card in hole {
let (pip, suit) = (card.rank.value(), card.suit as usize);
census.hole[pip as usize] += 1;
census.hole_mask |= bit(pip);
census.hole_suit[suit] |= bit(pip);
}
for card in board {
let (pip, suit) = (card.rank.value(), card.suit as usize);
census.board[pip as usize] += 1;
census.board_mask |= bit(pip);
census.board_suit[suit] |= bit(pip);
}
census
}
pub fn hole_count(&self, pip: u8) -> u8 {
self.hole.get(pip as usize).copied().unwrap_or(0)
}
pub fn board_count(&self, pip: u8) -> u8 {
self.board.get(pip as usize).copied().unwrap_or(0)
}
pub fn total_count(&self, pip: u8) -> u8 {
self.hole_count(pip) + self.board_count(pip)
}
pub fn hole_suited(&self, suit: usize) -> u32 {
self.hole_suit.get(suit).map_or(0, |m| m.count_ones())
}
pub fn board_suited(&self, suit: usize) -> u32 {
self.board_suit.get(suit).map_or(0, |m| m.count_ones())
}
}