rs_poker 3.0.0

A library to help with any Rust code dealing with poker. This includes card values, suits, hands, hand ranks, 5 card hand strength calculation, 7 card hand strength calulcation, and monte carlo game simulation helpers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};

use super::{Card, FlatDeck};
use std::fmt::Debug;

use rand::Rng;
#[cfg(feature = "serde")]
use serde::ser::SerializeSeq;

/// This struct is a bitset for cards
/// Each card is represented by a bit in a 64 bit integer
///
/// The bit is set if the card present
/// The bit is unset if the card not in the set
///
/// It implements the BitOr, BitAnd, and BitXor traits
/// It implements the Display trait
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct CardBitSet {
    // The bitset
    cards: u64,
}

const FIFTY_TWO_ONES: u64 = (1 << 52) - 1;

impl CardBitSet {
    /// Create a new empty bitset
    ///
    /// ```
    /// use rs_poker::core::CardBitSet;
    /// let cards = CardBitSet::new();
    /// assert!(cards.is_empty());
    /// ```
    pub fn new() -> Self {
        Self { cards: 0 }
    }

    /// This does what it says on the tin it insertes a card into the bitset
    ///
    /// ```
    /// use rs_poker::core::{Card, CardBitSet, Deck, Suit, Value};
    /// let mut cards = CardBitSet::new();
    ///
    /// cards.insert(Card::new(Value::Six, Suit::Club));
    /// cards.insert(Card::new(Value::King, Suit::Club));
    /// cards.insert(Card::new(Value::Ace, Suit::Club));
    /// assert_eq!(3, cards.count());
    /// ```
    pub fn insert(&mut self, card: Card) {
        self.cards |= 1 << u8::from(card);
    }

    /// Remove a card from the bitset
    ///
    /// ```
    /// use rs_poker::core::{Card, CardBitSet, Deck, Suit, Value};
    /// let mut cards = CardBitSet::new();
    /// cards.insert(Card::from(17));
    ///
    /// // We're using the u8 but it's got a value as well
    /// assert_eq!(Card::new(Value::Six, Suit::Club), Card::from(17));
    ///
    /// // The card is in the bitset
    /// assert!(cards.contains(Card::new(Value::Six, Suit::Club)));
    /// // We can remove the card
    /// cards.remove(Card::new(Value::Six, Suit::Club));
    ///
    /// // show that the card is no longer in the bitset
    /// assert!(!cards.contains(Card::from(17)));
    /// ```
    pub fn remove(&mut self, card: Card) {
        self.cards &= !(1 << u8::from(card));
    }

    /// Is the card in the bitset ?
    ///
    /// ```
    /// use rs_poker::core::{Card, CardBitSet, Deck, Suit, Value};
    ///
    /// let mut cards = CardBitSet::new();
    /// cards.insert(Card::from(17));
    ///
    /// assert!(cards.contains(Card::new(Value::Six, Suit::Club)));
    /// ```
    pub fn contains(&self, card: Card) -> bool {
        (self.cards & (1 << u8::from(card))) != 0
    }

    /// Is the bitset empty ?
    ///
    /// ```
    /// use rs_poker::core::{Card, CardBitSet};
    ///
    /// let mut cards = CardBitSet::new();
    /// assert!(cards.is_empty());
    ///
    /// cards.insert(Card::from(17));
    /// assert!(!cards.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.cards == 0
    }

    /// How many cards are in the bitset ?
    ///
    /// ```
    /// use rs_poker::core::{Card, CardBitSet};
    /// let mut cards = CardBitSet::new();
    ///
    /// assert_eq!(0, cards.count());
    /// for card in 0..13 {
    ///    cards.insert(Card::from(card));
    ///    assert_eq!(card as usize + 1, cards.count());
    /// }
    /// assert_eq!(13, cards.count());
    pub fn count(&self) -> usize {
        self.cards.count_ones() as usize
    }

    pub fn clear(&mut self) {
        self.cards = 0;
    }

    /// Sample one card from the bitset
    ///
    /// Returns `None` if the bitset is empty
    ///
    ///
    /// # Examples
    ///
    /// Sample will give a random card from the bitset
    ///
    /// ```
    /// use rand::rng;
    /// use rs_poker::core::{Card, CardBitSet, Deck};
    ///
    /// let mut rng = rng();
    /// let cards = CardBitSet::default();
    /// let card = cards.sample_one(&mut rng);
    ///
    /// assert!(card.is_some());
    /// assert!(cards.contains(card.unwrap()));
    /// ```
    ///
    /// ```
    /// use rand::rng;
    /// use rs_poker::core::{Card, CardBitSet};
    ///
    /// let mut rng = rng();
    /// let cards = CardBitSet::new();
    /// assert!(cards.sample_one(&mut rng).is_none());
    /// ```
    pub fn sample_one<R: Rng>(&self, rng: &mut R) -> Option<Card> {
        if self.is_empty() {
            return None;
        }

        let max = 64 - self.cards.leading_zeros();
        let min = self.cards.trailing_zeros();

        let mut idx = rng.random_range(min..=max);
        while (self.cards & (1 << idx)) == 0 {
            // While it's faster to just decrement/incrment the index, we need to ensure
            // that this doesn't bias towards lower/higher values
            idx = rng.random_range(min..=max);
        }
        Some(Card::from(idx as u8))
    }
}

impl Default for CardBitSet {
    /// Create a new bitset with all the cards in it
    /// ```
    /// use rs_poker::core::CardBitSet;
    ///
    /// let cards = CardBitSet::default();
    ///
    /// assert_eq!(52, cards.count());
    /// assert!(!cards.is_empty());
    /// ```
    fn default() -> Self {
        Self {
            cards: FIFTY_TWO_ONES,
        }
    }
}

// Trait for converting a CardBitSet into a FlatDeck
// Create the vec for storage and then return the flatdeck
impl From<CardBitSet> for FlatDeck {
    fn from(value: CardBitSet) -> Self {
        value.into_iter().collect::<Vec<Card>>().into()
    }
}

impl Debug for CardBitSet {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_set().entries(*self).finish()
    }
}

impl BitOr<CardBitSet> for CardBitSet {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self {
            cards: self.cards | rhs.cards,
        }
    }
}

impl BitOr<Card> for CardBitSet {
    type Output = Self;

    fn bitor(self, rhs: Card) -> Self::Output {
        Self {
            cards: self.cards | (1 << u8::from(rhs)),
        }
    }
}

impl BitOrAssign<CardBitSet> for CardBitSet {
    fn bitor_assign(&mut self, rhs: Self) {
        self.cards |= rhs.cards;
    }
}

impl BitOrAssign<Card> for CardBitSet {
    fn bitor_assign(&mut self, rhs: Card) {
        self.cards |= 1 << u8::from(rhs);
    }
}

impl BitXor for CardBitSet {
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self::Output {
        Self {
            cards: self.cards ^ rhs.cards,
        }
    }
}

impl BitXor<Card> for CardBitSet {
    type Output = Self;

    fn bitxor(self, rhs: Card) -> Self::Output {
        Self {
            cards: self.cards ^ (1 << u8::from(rhs)),
        }
    }
}

impl BitXorAssign<Card> for CardBitSet {
    fn bitxor_assign(&mut self, rhs: Card) {
        self.cards ^= 1 << u8::from(rhs);
    }
}

impl BitXorAssign<CardBitSet> for CardBitSet {
    fn bitxor_assign(&mut self, rhs: Self) {
        self.cards ^= rhs.cards;
    }
}

impl BitAnd for CardBitSet {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        Self {
            cards: self.cards & rhs.cards,
        }
    }
}

impl BitAndAssign for CardBitSet {
    fn bitand_assign(&mut self, rhs: Self) {
        self.cards &= rhs.cards;
    }
}

impl Not for CardBitSet {
    type Output = Self;

    fn not(self) -> Self::Output {
        Self {
            cards: !self.cards & FIFTY_TWO_ONES, // Ensure we only keep the first 52 bits
        }
    }
}

/// The iterator for the CardBitSet
/// It iterates over the cards in the bitset
pub struct CardBitSetIter(u64);

impl IntoIterator for CardBitSet {
    type Item = Card;
    type IntoIter = CardBitSetIter;

    fn into_iter(self) -> Self::IntoIter {
        CardBitSetIter(self.cards)
    }
}

impl Iterator for CardBitSetIter {
    type Item = Card;

    fn next(&mut self) -> Option<Self::Item> {
        if self.0 == 0 {
            return None;
        }

        let card = self.0.trailing_zeros();
        self.0 &= !(1 << card);

        Some(Card::from(card as u8))
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for CardBitSet {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.count()))?;
        for card in (*self).into_iter() {
            seq.serialize_element(&card)?;
        }
        seq.end()
    }
}

#[cfg(feature = "serde")]
struct CardBitSetVisitor;

#[cfg(feature = "serde")]
impl<'de> serde::de::Visitor<'de> for CardBitSetVisitor {
    type Value = CardBitSet;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("a sequence of cards")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        let mut deck = CardBitSet::new();
        while let Some(card) = seq.next_element()? {
            deck.insert(card);
        }
        Ok(deck)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for CardBitSet {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_seq(CardBitSetVisitor)
    }
}

#[cfg(test)]
mod tests {
    use core::panic;
    use std::collections::HashSet;

    use crate::core::Deck;

    use super::*;

    #[test]
    fn test_empty() {
        let cards = CardBitSet::new();
        assert!(cards.is_empty());
    }

    #[test]
    fn test_insert_all() {
        let mut all_cards = CardBitSet::new();
        for card in Deck::default() {
            let mut single_card = CardBitSet::new();

            single_card.insert(card);
            all_cards |= single_card;

            assert!(single_card.contains(card));
        }

        assert_eq!(all_cards.count(), 52);

        for card in Deck::default() {
            assert!(all_cards.contains(card));
        }
    }

    #[test]
    fn test_xor_is_remove() {
        let mut all_cards = CardBitSet::new();
        for card in Deck::default() {
            all_cards |= card;
        }

        for card in Deck::default() {
            let xor_masked_set: CardBitSet = all_cards ^ card;
            assert!(!xor_masked_set.contains(card));

            let mut removed_set = all_cards;
            removed_set.remove(card);

            assert_eq!(removed_set, xor_masked_set);
        }
        assert_eq!(52, all_cards.count());
    }

    #[test]
    fn test_is_empty() {
        let empty = CardBitSet::new();
        assert!(empty.is_empty());
    }

    #[test]
    fn test_not_empty() {
        let mut cards = CardBitSet::new();

        cards.insert(Card::from(17));
        assert!(!cards.is_empty());
    }

    #[test]
    fn test_add_cards_iter() {
        let mut hash_set: HashSet<Card> = HashSet::new();
        let mut bit_set = CardBitSet::new();

        let deck = FlatDeck::from(Deck::default());

        for card in deck.sample(13) {
            hash_set.insert(card);
            bit_set.insert(card);
        }

        assert_eq!(hash_set.len(), bit_set.count());
        for card in hash_set.clone() {
            assert!(bit_set.contains(card));
        }

        for card in bit_set {
            assert!(hash_set.contains(&card));
        }
    }

    #[test]
    fn test_default_contains() {
        let mut bitset_cards = CardBitSet::default();
        assert_eq!(52, bitset_cards.count());

        for card in Deck::default() {
            assert!(bitset_cards.contains(card));
            bitset_cards.remove(card);
        }

        assert_eq!(0, bitset_cards.count());
        assert!(bitset_cards.is_empty());
    }

    #[test]
    fn test_formatting_cards() {
        let mut cards = CardBitSet::new();
        cards.insert(Card::new(crate::core::Value::Ace, crate::core::Suit::Club));
        cards.insert(Card::new(
            crate::core::Value::King,
            crate::core::Suit::Diamond,
        ));
        cards.insert(Card::new(
            crate::core::Value::Three,
            crate::core::Suit::Heart,
        ));

        assert_eq!(format!("{:?}", cards), "{Card(Ac), Card(3h), Card(Kd)}");
    }

    #[test]
    fn test_bit_and() {
        let mut cards = CardBitSet::new();
        cards.insert(Card::new(crate::core::Value::Ace, crate::core::Suit::Club));
        cards.insert(Card::new(
            crate::core::Value::King,
            crate::core::Suit::Diamond,
        ));

        let mut cards2 = CardBitSet::new();
        cards2.insert(Card::new(
            crate::core::Value::Three,
            crate::core::Suit::Heart,
        ));
        cards2.insert(Card::new(
            crate::core::Value::King,
            crate::core::Suit::Diamond,
        ));

        let and = cards & cards2;
        assert_eq!(and.count(), 1);

        assert!(and.contains(Card::new(
            crate::core::Value::King,
            crate::core::Suit::Diamond,
        )));
        assert!(!and.contains(Card::new(crate::core::Value::Ace, crate::core::Suit::Club,)));
    }

    #[test]
    fn test_bit_and_assign() {
        let mut cards = CardBitSet::new();
        cards.insert(Card::new(crate::core::Value::Ace, crate::core::Suit::Club));
        cards.insert(Card::new(
            crate::core::Value::King,
            crate::core::Suit::Diamond,
        ));

        let mut cards2 = CardBitSet::new();
        cards2.insert(Card::new(
            crate::core::Value::Three,
            crate::core::Suit::Heart,
        ));
        cards2.insert(Card::new(
            crate::core::Value::King,
            crate::core::Suit::Diamond,
        ));

        cards &= cards2;

        assert_eq!(cards.count(), 1);

        // The shared card
        assert!(cards.contains(Card::new(
            crate::core::Value::King,
            crate::core::Suit::Diamond,
        )));

        // None of the non-shared are there.
        assert!(!cards.contains(Card::new(crate::core::Value::Ace, crate::core::Suit::Club,)));
        assert!(!cards.contains(Card::new(
            crate::core::Value::Three,
            crate::core::Suit::Heart,
        )));
    }

    #[test]
    fn test_pick_one() {
        let mut rng = rand::rng();
        let mut cards = CardBitSet::new();

        cards.insert(Card::new(crate::core::Value::Ace, crate::core::Suit::Club));

        let card = cards.sample_one(&mut rng);
        assert!(card.is_some(), "Card should be present");
        assert_eq!(
            card.unwrap(),
            Card::new(crate::core::Value::Ace, crate::core::Suit::Club)
        );
    }

    #[test]
    fn test_pick_one_all() {
        let mut rng = rand::rng();
        let mut cards = CardBitSet::default();

        let mut picked: HashSet<Card> = HashSet::new();

        for _i in 0..10 {
            let card = cards.sample_one(&mut rng);
            if let Some(c) = card {
                cards.remove(c);

                assert!(
                    !picked.contains(&c),
                    "Card already picked: {:?} picked = {:?}",
                    c,
                    picked
                );
                picked.insert(c);
            } else {
                panic!("No more cards to pick");
            }
        }
        assert_eq!(cards.count(), 42); // 52 - 10 = 42
    }

    #[test]
    fn test_can_pick_one_for_all() {
        let mut rng = rand::rng();
        let mut cards_one = CardBitSet::default();
        let mut cards_two = CardBitSet::default();

        let mut picked_one = Vec::new();
        let mut picked_two = Vec::new();

        while cards_one.count() > 0 && cards_two.count() > 0 {
            if let Some(card_one) = cards_one.sample_one(&mut rng) {
                picked_one.push(card_one);
                cards_one.remove(card_one);
            }

            if let Some(card_two) = cards_two.sample_one(&mut rng) {
                picked_two.push(card_two);
                cards_two.remove(card_two);
            }
        }

        assert!(cards_one.is_empty(), "Cards one should be empty");
        assert!(cards_two.is_empty(), "Cards two should be empty");

        assert_eq!(picked_one.len(), 52);
        assert_eq!(picked_two.len(), 52);

        assert_ne!(picked_one, picked_two, "Picked cards should be different");

        // Check that all picked cards are unique
        let unique_one: HashSet<_> = picked_one.iter().cloned().collect();
        let unique_two: HashSet<_> = picked_two.iter().cloned().collect();

        assert_eq!(
            unique_one.len(),
            picked_one.len(),
            "Picked cards one should be unique"
        );
        assert_eq!(
            unique_two.len(),
            picked_two.len(),
            "Picked cards two should be unique"
        );
    }
}