use crate::Suit;
use core::fmt::{self, Write as _};
use core::iter::FusedIterator;
use core::num::NonZero;
use core::ops;
use core::str::FromStr;
use thiserror::Error;
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[error("{0} is not a valid rank (1..=13)")]
pub struct InvalidRank(pub u8);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(try_from = "u8", into = "u8")
)]
#[repr(transparent)]
pub struct Rank(NonZero<u8>);
impl Rank {
pub const A: Self = Self(NonZero::new(1).unwrap());
pub const T: Self = Self(NonZero::new(10).unwrap());
pub const J: Self = Self(NonZero::new(11).unwrap());
pub const Q: Self = Self(NonZero::new(12).unwrap());
pub const K: Self = Self(NonZero::new(13).unwrap());
#[must_use]
#[inline]
pub const fn new(rank: u8) -> Self {
match Self::try_new(rank) {
Ok(r) => r,
Err(_) => panic!("rank must be in 1..=13"),
}
}
#[inline]
pub const fn try_new(rank: u8) -> Result<Self, InvalidRank> {
match NonZero::new(rank) {
Some(nonzero) if rank <= 13 => Ok(Self(nonzero)),
_ => Err(InvalidRank(rank)),
}
}
#[must_use]
#[inline]
pub const fn get(self) -> u8 {
self.0.get()
}
#[must_use]
#[inline]
pub const fn letter(self) -> char {
b"A23456789TJQK"[self.get() as usize - 1] as char
}
#[must_use]
#[inline]
pub const fn deadwood(self) -> u8 {
if self.get() > 10 { 10 } else { self.get() }
}
}
impl From<Rank> for u8 {
#[inline]
fn from(rank: Rank) -> Self {
rank.get()
}
}
impl TryFrom<u8> for Rank {
type Error = InvalidRank;
#[inline]
fn try_from(rank: u8) -> Result<Self, InvalidRank> {
Self::try_new(rank)
}
}
impl fmt::Display for Rank {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_char(self.letter())
}
}
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
#[error("Invalid rank: expected A, 2-10, T, J, Q, K")]
pub struct ParseRankError;
impl FromStr for Rank {
type Err = ParseRankError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_uppercase().as_str() {
"A" => Ok(Self::A),
"K" => Ok(Self::K),
"Q" => Ok(Self::Q),
"J" => Ok(Self::J),
"T" | "10" => Ok(Self::T),
"9" => Ok(Self::new(9)),
"8" => Ok(Self::new(8)),
"7" => Ok(Self::new(7)),
"6" => Ok(Self::new(6)),
"5" => Ok(Self::new(5)),
"4" => Ok(Self::new(4)),
"3" => Ok(Self::new(3)),
"2" => Ok(Self::new(2)),
_ => Err(ParseRankError),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct Card {
pub suit: Suit,
pub rank: Rank,
}
impl fmt::Display for Card {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}{}", self.rank, self.suit)
}
}
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseCardError {
#[error("Invalid suit in card: expected one of C, D, H, S, ♣, ♦, ♥, ♠, ♧, ♢, ♡, ♤")]
Suit,
#[error("Invalid rank in card: expected A, 2-10, T, J, Q, K")]
Rank,
}
impl FromStr for Card {
type Err = ParseCardError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let build = |rank: &str, suit: &str| -> Result<Self, ParseCardError> {
Ok(Self {
suit: suit.parse().map_err(|_| ParseCardError::Suit)?,
rank: rank.parse().map_err(|_| ParseCardError::Rank)?,
})
};
let lead = if s.starts_with("10") { 2 } else { 1 };
if let Some((rank, suit)) = s.split_at_checked(lead)
&& let Ok(card) = build(rank, suit)
{
return Ok(card);
}
let trail = if s.ends_with("10") {
2
} else {
s.chars().next_back().map_or(0, char::len_utf8)
};
let (suit, rank) = s.split_at(s.len().saturating_sub(trail));
build(rank, suit)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
#[repr(transparent)]
pub struct Holding(u16);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HoldingIter {
rest: u16,
cursor: u8,
}
impl Iterator for HoldingIter {
type Item = Rank;
fn next(&mut self) -> Option<Self::Item> {
if self.rest == 0 {
return None;
}
let step = self.rest.trailing_zeros() as u8 + 1;
self.rest >>= step;
self.cursor += step;
Some(Rank(unsafe { NonZero::new_unchecked(self.cursor - 1) }))
}
fn size_hint(&self) -> (usize, Option<usize>) {
let count = self.rest.count_ones() as usize;
(count, Some(count))
}
fn count(self) -> usize {
self.rest.count_ones() as usize
}
}
impl DoubleEndedIterator for HoldingIter {
fn next_back(&mut self) -> Option<Self::Item> {
if self.rest == 0 {
return None;
}
let pos = 15 - self.rest.leading_zeros() as u8;
self.rest &= !(1u16 << pos);
Some(Rank(unsafe { NonZero::new_unchecked(self.cursor + pos) }))
}
}
impl ExactSizeIterator for HoldingIter {
fn len(&self) -> usize {
self.rest.count_ones() as usize
}
}
impl FusedIterator for HoldingIter {}
impl Holding {
pub const EMPTY: Self = Self(0);
pub const ALL: Self = Self(0x3FFE);
#[must_use]
#[inline]
pub const fn len(self) -> usize {
self.0.count_ones() as usize
}
#[must_use]
#[inline]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[must_use]
#[inline]
pub const fn contains(self, rank: Rank) -> bool {
self.0 & 1 << rank.get() != 0
}
#[inline]
pub const fn insert(&mut self, rank: Rank) -> bool {
let insertion = 1 << rank.get() & Self::ALL.0;
let inserted = insertion & !self.0 != 0;
self.0 |= insertion;
inserted
}
#[inline]
pub const fn remove(&mut self, rank: Rank) -> bool {
let removed = self.contains(rank);
self.0 &= !(1 << rank.get());
removed
}
#[inline]
pub const fn toggle(&mut self, rank: Rank) -> bool {
self.0 ^= 1 << rank.get() & Self::ALL.0;
self.contains(rank)
}
#[inline]
pub const fn set(&mut self, rank: Rank, condition: bool) {
let flag = 1 << rank.get();
let mask = (condition as u16).wrapping_neg();
self.0 = (self.0 & !flag) | (mask & flag);
}
#[inline]
#[must_use]
pub const fn iter(self) -> HoldingIter {
HoldingIter {
rest: self.0 & Self::ALL.0,
cursor: 0,
}
}
#[must_use]
#[inline]
pub const fn to_bits(self) -> u16 {
self.0
}
#[must_use]
#[inline]
pub const fn from_bits_retain(bits: u16) -> Self {
Self(bits)
}
#[must_use]
#[inline]
pub const fn contains_unknown_bits(self) -> bool {
self.0 & Self::ALL.0 != self.0
}
#[must_use]
#[inline]
pub const fn from_bits(bits: u16) -> Option<Self> {
if bits & Self::ALL.0 == bits {
Some(Self(bits))
} else {
None
}
}
#[must_use]
#[inline]
pub const fn from_bits_truncate(bits: u16) -> Self {
Self(bits & Self::ALL.0)
}
#[must_use]
#[inline]
pub const fn from_rank(rank: Rank) -> Self {
Self(1 << rank.get())
}
}
impl IntoIterator for Holding {
type Item = Rank;
type IntoIter = HoldingIter;
fn into_iter(self) -> HoldingIter {
self.iter()
}
}
impl ops::BitAnd for Holding {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
Self(self.0 & rhs.0)
}
}
impl ops::BitOr for Holding {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl ops::BitXor for Holding {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self {
Self(self.0 ^ rhs.0)
}
}
impl ops::Not for Holding {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self::from_bits_truncate(!self.0)
}
}
impl ops::Sub for Holding {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Self(self.0 & !rhs.0)
}
}
impl ops::BitAndAssign for Holding {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs;
}
}
impl ops::BitOrAssign for Holding {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}
impl ops::BitXorAssign for Holding {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
*self = *self ^ rhs;
}
}
impl ops::SubAssign for Holding {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl fmt::Display for Holding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for rank in 1u8..14 {
if self.0 & 1 << rank != 0 {
f.write_char(b"A23456789TJQK"[rank as usize - 1] as char)?;
}
}
Ok(())
}
}
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ParseHoldingError {
#[error("Ranks are not all valid or in ascending order")]
InvalidRanks,
#[error("The same rank appears more than once")]
RepeatedRank,
#[error("A suit contains more than 13 cards")]
TooManyCards,
}
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ParseHandError {
#[error(transparent)]
Holding(#[from] ParseHoldingError),
#[error("The hand does not contain 4 suits")]
NotFourSuits,
}
impl FromStr for Holding {
type Err = ParseHoldingError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > 14 {
return Err(ParseHoldingError::TooManyCards);
}
let bytes = s.as_bytes();
let mut i = 0;
let mut prev_rank: u8 = 0;
let mut holding = Self::EMPTY;
while i < bytes.len() {
let c = bytes[i].to_ascii_uppercase();
let rank: u8 = match c {
b'A' => 1,
b'K' => 13,
b'Q' => 12,
b'J' => 11,
b'T' => 10,
b'1' => {
if bytes.get(i + 1) != Some(&b'0') {
return Err(ParseHoldingError::InvalidRanks);
}
i += 1;
10
}
b'2'..=b'9' => c - b'0',
_ => return Err(ParseHoldingError::InvalidRanks),
};
if rank == prev_rank {
return Err(ParseHoldingError::RepeatedRank);
}
if rank < prev_rank {
return Err(ParseHoldingError::InvalidRanks);
}
prev_rank = rank;
holding.insert(Rank(unsafe { NonZero::new_unchecked(rank) }));
i += 1;
}
Ok(holding)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct Hand([Holding; 4]);
const _: () = assert!(size_of::<Hand>() == 8);
const _: () = assert!(Hand::ALL.to_bits() == 0x3FFE_3FFE_3FFE_3FFE);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandIter {
suits: [HoldingIter; 4],
fwd: u8,
bwd: u8,
}
impl Iterator for HandIter {
type Item = Card;
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.fwd > 3 {
return None;
}
let suit = Suit::ASC[self.fwd as usize];
if let Some(rank) = self.suits[self.fwd as usize].next() {
return Some(Card { suit, rank });
}
if self.fwd == self.bwd {
self.fwd = 4;
return None;
}
self.fwd += 1;
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let count = self.len();
(count, Some(count))
}
fn count(self) -> usize {
self.len()
}
}
impl DoubleEndedIterator for HandIter {
fn next_back(&mut self) -> Option<Self::Item> {
loop {
if self.fwd > 3 {
return None;
}
let suit = Suit::ASC[self.bwd as usize];
if let Some(rank) = self.suits[self.bwd as usize].next_back() {
return Some(Card { suit, rank });
}
if self.fwd == self.bwd {
self.fwd = 4;
return None;
}
self.bwd -= 1;
}
}
}
impl ExactSizeIterator for HandIter {
fn len(&self) -> usize {
if self.fwd > 3 {
return 0;
}
(self.fwd as usize..=self.bwd as usize)
.map(|i| self.suits[i].len())
.sum()
}
}
impl FusedIterator for HandIter {}
impl ops::Index<Suit> for Hand {
type Output = Holding;
#[inline]
fn index(&self, suit: Suit) -> &Holding {
&self.0[suit as usize]
}
}
impl ops::IndexMut<Suit> for Hand {
#[inline]
fn index_mut(&mut self, suit: Suit) -> &mut Holding {
&mut self.0[suit as usize]
}
}
impl Hand {
#[must_use]
#[inline]
pub const fn to_bits(self) -> u64 {
self.0[0].to_bits() as u64
| (self.0[1].to_bits() as u64) << 16
| (self.0[2].to_bits() as u64) << 32
| (self.0[3].to_bits() as u64) << 48
}
#[must_use]
#[inline]
pub const fn from_bits_retain(bits: u64) -> Self {
Self([
Holding::from_bits_retain(bits as u16),
Holding::from_bits_retain((bits >> 16) as u16),
Holding::from_bits_retain((bits >> 32) as u16),
Holding::from_bits_retain((bits >> 48) as u16),
])
}
#[must_use]
#[inline]
pub const fn contains_unknown_bits(self) -> bool {
self.to_bits() & Self::ALL.to_bits() != self.to_bits()
}
#[must_use]
#[inline]
pub const fn from_bits(bits: u64) -> Option<Self> {
if bits & Self::ALL.to_bits() == bits {
Some(Self::from_bits_retain(bits))
} else {
None
}
}
#[must_use]
#[inline]
pub const fn from_bits_truncate(bits: u64) -> Self {
Self::from_bits_retain(bits & Self::ALL.to_bits())
}
#[must_use]
#[inline]
pub const fn new(clubs: Holding, diamonds: Holding, hearts: Holding, spades: Holding) -> Self {
Self([clubs, diamonds, hearts, spades])
}
#[must_use]
#[inline]
pub const fn from_card(card: Card) -> Self {
let mut hand = Self::EMPTY;
hand.0[card.suit as usize] = Holding::from_rank(card.rank);
hand
}
pub const EMPTY: Self = Self([Holding::EMPTY; 4]);
pub const ALL: Self = Self([Holding::ALL; 4]);
#[must_use]
#[inline]
pub const fn len(self) -> usize {
self.to_bits().count_ones() as usize
}
#[must_use]
#[inline]
pub const fn is_empty(self) -> bool {
self.to_bits() == 0
}
#[must_use]
#[inline]
pub fn contains(self, card: Card) -> bool {
self[card.suit].contains(card.rank)
}
#[inline]
pub fn insert(&mut self, card: Card) -> bool {
self[card.suit].insert(card.rank)
}
#[inline]
pub fn remove(&mut self, card: Card) -> bool {
self[card.suit].remove(card.rank)
}
#[inline]
pub fn toggle(&mut self, card: Card) -> bool {
self[card.suit].toggle(card.rank)
}
#[inline]
pub fn set(&mut self, card: Card, condition: bool) {
self[card.suit].set(card.rank, condition);
}
#[inline]
#[must_use]
pub const fn iter(self) -> HandIter {
HandIter {
suits: [
self.0[0].iter(),
self.0[1].iter(),
self.0[2].iter(),
self.0[3].iter(),
],
fwd: 0,
bwd: 3,
}
}
}
impl IntoIterator for Hand {
type Item = Card;
type IntoIter = HandIter;
#[inline]
fn into_iter(self) -> HandIter {
self.iter()
}
}
impl FromIterator<Card> for Hand {
fn from_iter<I: IntoIterator<Item = Card>>(iter: I) -> Self {
iter.into_iter().fold(Self::EMPTY, |mut hand, card| {
hand.insert(card);
hand
})
}
}
impl From<Card> for Hand {
#[inline]
fn from(card: Card) -> Self {
Self::from_card(card)
}
}
impl fmt::Display for Hand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_empty() {
return f.write_char('-');
}
self[Suit::Clubs].fmt(f)?;
f.write_char('.')?;
self[Suit::Diamonds].fmt(f)?;
f.write_char('.')?;
self[Suit::Hearts].fmt(f)?;
f.write_char('.')?;
self[Suit::Spades].fmt(f)
}
}
impl FromStr for Hand {
type Err = ParseHandError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > 52 + 4 + 3 {
return Err(ParseHoldingError::TooManyCards.into());
}
if s == "-" {
return Ok(Self::EMPTY);
}
let holdings: Result<Vec<_>, _> = s.split('.').map(Holding::from_str).collect();
Ok(Self(
holdings?
.try_into()
.map_err(|_| ParseHandError::NotFourSuits)?,
))
}
}
impl ops::BitAnd for Hand {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
Self::from_bits_retain(self.to_bits() & rhs.to_bits())
}
}
impl ops::BitOr for Hand {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self::from_bits_retain(self.to_bits() | rhs.to_bits())
}
}
impl ops::BitXor for Hand {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self {
Self::from_bits_retain(self.to_bits() ^ rhs.to_bits())
}
}
impl ops::Not for Hand {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self::from_bits_truncate(!self.to_bits())
}
}
impl ops::Sub for Hand {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Self::from_bits_retain(self.to_bits() & !rhs.to_bits())
}
}
impl ops::BitAndAssign for Hand {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
*self = *self & rhs;
}
}
impl ops::BitOrAssign for Hand {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}
impl ops::BitXorAssign for Hand {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
*self = *self ^ rhs;
}
}
impl ops::SubAssign for Hand {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rank_letters_and_deadwood() {
let letters: Vec<char> = (1..=13).map(|r| Rank::new(r).letter()).collect();
assert_eq!(letters.iter().collect::<String>(), "A23456789TJQK");
let values: Vec<u8> = (1..=13).map(|r| Rank::new(r).deadwood()).collect();
assert_eq!(values, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]);
assert!(Rank::A < Rank::new(2));
assert!(Rank::T < Rank::J);
assert_eq!(Rank::try_new(0), Err(InvalidRank(0)));
assert_eq!(Rank::try_new(14), Err(InvalidRank(14)));
}
#[test]
fn holding_bit_ops() {
let mut holding = Holding::EMPTY;
assert!(holding.insert(Rank::A));
assert!(!holding.insert(Rank::A));
assert!(holding.insert(Rank::K));
assert_eq!(holding.len(), 2);
assert!(holding.contains(Rank::A));
assert!(!holding.contains(Rank::Q));
assert!(holding.remove(Rank::A));
assert!(!holding.remove(Rank::A));
assert!(holding.toggle(Rank::Q));
assert!(!holding.toggle(Rank::Q));
holding.set(Rank::T, true);
holding.set(Rank::K, false);
assert_eq!(holding.iter().collect::<Vec<_>>(), [Rank::T]);
assert_eq!(!Holding::EMPTY, Holding::ALL);
assert_eq!(Holding::ALL.len(), 13);
assert_eq!(Holding::ALL - Holding::ALL, Holding::EMPTY);
}
#[test]
fn holding_iteration_is_ascending() {
let holding: Holding = "A23TK".parse().unwrap();
let ranks: Vec<u8> = holding.iter().map(Rank::get).collect();
assert_eq!(ranks, [1, 2, 3, 10, 13]);
let reversed: Vec<u8> = holding.iter().rev().map(Rank::get).collect();
assert_eq!(reversed, [13, 10, 3, 2, 1]);
let mut iter = holding.iter();
assert_eq!(iter.len(), 5);
assert_eq!(iter.next().map(Rank::get), Some(1));
assert_eq!(iter.next_back().map(Rank::get), Some(13));
assert_eq!(iter.len(), 3);
assert_eq!(iter.next().map(Rank::get), Some(2));
assert_eq!(iter.next_back().map(Rank::get), Some(10));
assert_eq!(iter.next().map(Rank::get), Some(3));
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
}
#[test]
fn invalid_bits_are_not_iterated() {
let holding = Holding::from_bits_retain(0x8001);
assert!(holding.contains_unknown_bits());
assert_eq!(holding.iter().count(), 0);
assert_eq!(Holding::from_bits(0x8001), None);
assert_eq!(Holding::from_bits_truncate(0x8001), Holding::EMPTY);
}
#[test]
fn hand_layout() {
let mut hand = Hand::EMPTY;
assert!(hand.insert(Card {
suit: Suit::Clubs,
rank: Rank::A,
}));
assert_eq!(hand.to_bits(), 2);
assert!(hand.insert(Card {
suit: Suit::Spades,
rank: Rank::K,
}));
assert_eq!(hand.to_bits(), 2 | 1 << 61);
assert_eq!(Hand::from_bits_retain(hand.to_bits()), hand);
assert_eq!(hand.len(), 2);
}
#[test]
fn hand_iteration_is_ascending() {
let hand: Hand = "A23.456.789.T".parse().unwrap();
assert_eq!(hand.len(), 10);
let cards: Vec<Card> = hand.iter().collect();
assert_eq!(cards.first().map(ToString::to_string), Some("A♣".into()));
assert_eq!(cards.last().map(ToString::to_string), Some("T♠".into()));
assert!(cards.windows(2).all(|w| {
let (a, b) = (w[0], w[1]);
a.suit < b.suit || (a.suit == b.suit && a.rank < b.rank)
}));
assert_eq!(hand.iter().rev().count(), 10);
assert_eq!(Hand::from_iter(cards), hand);
}
}