ducy 0.6.0

A performant analysis library for poker.
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
use std::fmt::Display;
use std::ops::{BitOr, BitOrAssign, Sub, SubAssign};
use std::str::FromStr;

use crate::error::DucyError;
use crate::ranking::standard_hand_ranker::RankOrder;

/// Weighted hand ranges for equity calculations.
pub mod range;

/// Represents the rank of a playing card (Two through Ace).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Rank {
    /// 2
    Two,
    /// 3
    Three,
    /// 4
    Four,
    /// 5
    Five,
    /// 6
    Six,
    /// 7
    Seven,
    /// 8
    Eight,
    /// 9
    Nine,
    /// 10
    Ten,
    /// Jack
    Jack,
    /// Queen
    Queen,
    /// King
    King,
    /// Ace
    Ace,
}

impl Rank {
    /// Every rank, Two through Ace.
    pub const ALL: [Rank; 13] = [
        Rank::Two,
        Rank::Three,
        Rank::Four,
        Rank::Five,
        Rank::Six,
        Rank::Seven,
        Rank::Eight,
        Rank::Nine,
        Rank::Ten,
        Rank::Jack,
        Rank::Queen,
        Rank::King,
        Rank::Ace,
    ];

    /// Iterates over every rank, Two through Ace.
    pub fn iter() -> std::array::IntoIter<Rank, 13> {
        Self::ALL.into_iter()
    }

    /// Tries to create a `Rank` from a character representation.
    /// Returns `Ok(Rank)` if successful, or `Err(String)` if the character is invalid.
    /// Characters representing ranks are: '2'-'9', 'T' (Ten), 'J' (Jack), 'Q' (Queen), 'K' (King), 'A' (Ace).
    pub fn try_from_char(val: &char) -> Result<Self, DucyError> {
        match val.to_ascii_lowercase() {
            'a' => Ok(Rank::Ace),
            '2' => Ok(Rank::Two),
            '3' => Ok(Rank::Three),
            '4' => Ok(Rank::Four),
            '5' => Ok(Rank::Five),
            '6' => Ok(Rank::Six),
            '7' => Ok(Rank::Seven),
            '8' => Ok(Rank::Eight),
            '9' => Ok(Rank::Nine),
            't' => Ok(Rank::Ten),
            'j' => Ok(Rank::Jack),
            'q' => Ok(Rank::Queen),
            'k' => Ok(Rank::King),
            _ => Err(DucyError::InvalidRank),
        }
    }
}

impl Display for Rank {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Rank::Ace => write!(f, "A"),
            Rank::Two => write!(f, "2"),
            Rank::Three => write!(f, "3"),
            Rank::Four => write!(f, "4"),
            Rank::Five => write!(f, "5"),
            Rank::Six => write!(f, "6"),
            Rank::Seven => write!(f, "7"),
            Rank::Eight => write!(f, "8"),
            Rank::Nine => write!(f, "9"),
            Rank::Ten => write!(f, "T"),
            Rank::Jack => write!(f, "J"),
            Rank::Queen => write!(f, "Q"),
            Rank::King => write!(f, "K"),
        }
    }
}

/// Represents the suit of a playing card (Clubs, Diamonds, Hearts, Spades).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Suit {
    /// Clubs (c)
    Clubs,
    /// Diamonds (d)
    Diamonds,
    /// Hearts (h)
    Hearts,
    /// Spades (s)
    Spades,
}

impl Suit {
    /// Every suit, in declaration order.
    pub const ALL: [Suit; 4] = [Suit::Clubs, Suit::Diamonds, Suit::Hearts, Suit::Spades];

    /// Iterates over every suit, in declaration order.
    pub fn iter() -> std::array::IntoIter<Suit, 4> {
        Self::ALL.into_iter()
    }

    /// Tries to create a `Suit` from a character representation.
    /// Returns `Ok(Suit)` if successful, or `Err(String)` if the character is invalid.
    /// Characters representing suits are: 'c' (Clubs), 'd' (Diamonds), 'h' (Hearts), 's' (Spades).
    pub fn try_from_char(val: &char) -> Result<Self, DucyError> {
        match val.to_ascii_lowercase() {
            'c' => Ok(Suit::Clubs),
            'd' => Ok(Suit::Diamonds),
            'h' => Ok(Suit::Hearts),
            's' => Ok(Suit::Spades),
            _ => Err(DucyError::InvalidSuit),
        }
    }
}

impl Display for Suit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Suit::Hearts => write!(f, "h"),
            Suit::Diamonds => write!(f, "d"),
            Suit::Clubs => write!(f, "c"),
            Suit::Spades => write!(f, "s"),
        }
    }
}

/// Represents a playing card with a rank and suit.
/// Provides methods to create, parse, and display cards.
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(into = "String", try_from = "String"))]
pub struct Card {
    val: Deck,
    suit: Suit,
    rank: Rank,
}

impl Card {
    /// Creates a new `Card` with the specified rank and suit.
    ///
    /// # Arguments
    ///
    /// * `rank` - The rank of the card.
    /// * `suit` - The suit of the card.
    ///
    /// # Returns
    ///
    /// A `Card` instance with the specified rank and suit.
    pub fn new(rank: Rank, suit: Suit) -> Card {
        Card {
            val: Deck::get_card(&rank, &suit),
            rank,
            suit,
        }
    }

    /// Creates a `Card` instance from a `Deck` value, rank, and suit without performing any checks.
    ///
    /// # Arguments
    ///
    /// * `val` - The `Deck` value representing the card.
    /// * `rank` - The rank of the card.
    /// * `suit` - The suit of the card.
    ///
    /// # Returns
    ///
    /// A `Card` instance with the specified `Deck` value, rank, and suit.
    fn from_deck_unchecked(val: Deck, rank: Rank, suit: Suit) -> Self {
        Self { val, rank, suit }
    }

    /// Returns the `Deck` value associated with this card.
    ///
    /// # Returns
    ///
    /// The `Deck` value representing this card.
    pub fn get_deck(&self) -> Deck {
        self.val
    }

    /// Parses a card from a two-character string like `"Ah"` (Ace of Hearts).
    pub fn parse(val: &str) -> Result<Self, DucyError> {
        let trimmed = val.trim();
        if trimmed.len() < 2 {
            return Err(DucyError::InvalidCard);
        }
        let mut chars = val.chars();
        let rank: char = chars.next().ok_or(DucyError::InvalidCard)?;
        let suit = chars.next().ok_or(DucyError::InvalidCard)?;
        Ok(Self::new(
            Rank::try_from_char(&rank)?,
            Suit::try_from_char(&suit)?,
        ))
    }
    /// Returns an iterator over all possible `Card` instances.
    ///
    /// # Returns
    ///
    /// An iterator that yields every possible card in a standard deck.
    pub fn values() -> impl Iterator<Item = Card> {
        CardIterator::new()
    }
}

impl Display for Card {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}{}", self.rank(), self.suit())
    }
}

impl FromStr for Card {
    type Err = DucyError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl Card {
    /// Returns the rank of the card.
    pub fn rank(&self) -> Rank {
        self.rank
    }

    /// Returns the suit of the card.
    pub fn suit(&self) -> Suit {
        self.suit
    }
}

impl From<Card> for String {
    fn from(card: Card) -> Self {
        card.to_string()
    }
}

impl TryFrom<String> for Card {
    type Error = DucyError;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        Card::parse(&s)
    }
}

/// Represents a deck of playing cards.
/// Able to hold any subset of playing cards, up to one of each card.
/// Provides methods to create, parse, and display decks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(into = "String", try_from = "String"))]
pub struct Deck {
    cards: u64,
}

impl Display for Deck {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let card_iterator = DeckCardIterator::new(*self, true);
        let cards_str: Vec<String> = card_iterator.map(|x| format!("{}", x)).collect();
        let cards_as_str = cards_str.join(" ");
        write!(f, "{}", cards_as_str)
    }
}

impl FromStr for Deck {
    type Err = DucyError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl From<Deck> for String {
    fn from(deck: Deck) -> Self {
        deck.to_string()
    }
}

impl TryFrom<String> for Deck {
    type Error = DucyError;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        Deck::parse(&s)
    }
}

const SINGLE_SUIT_BITFIELD: u64 = 0b11111111111111;
const SINGLE_SUIT_HIGH_ACE_BITFIELD: u64 = 0b11111111111110;
const SINGLE_SUIT_LOW_ACE_BITFIELD: u64 = 0b01111111111111;
const ALL_CARDS_BITFIELD: u64 = SINGLE_SUIT_BITFIELD
    | SINGLE_SUIT_BITFIELD << 16
    | SINGLE_SUIT_BITFIELD << 32
    | SINGLE_SUIT_BITFIELD << 48;
const ALL_CARDS_NO_LOW_ACES_BITFIELD: u64 = SINGLE_SUIT_HIGH_ACE_BITFIELD
    | SINGLE_SUIT_HIGH_ACE_BITFIELD << 16
    | SINGLE_SUIT_HIGH_ACE_BITFIELD << 32
    | SINGLE_SUIT_HIGH_ACE_BITFIELD << 48;
const SINGLE_RANK_BITFIELD: u64 = 0b00010000000000000;
const SINGLE_RANK_FILTER: u64 = SINGLE_RANK_BITFIELD
    | SINGLE_RANK_BITFIELD << 16
    | SINGLE_RANK_BITFIELD << 32
    | SINGLE_RANK_BITFIELD << 48;

const RANKS: [Rank; 14] = [
    Rank::Ace,
    Rank::Two,
    Rank::Three,
    Rank::Four,
    Rank::Five,
    Rank::Six,
    Rank::Seven,
    Rank::Eight,
    Rank::Nine,
    Rank::Ten,
    Rank::Jack,
    Rank::Queen,
    Rank::King,
    Rank::Ace,
];

const RANK_BITS: [u64; 13] = [
    0b1 << 1,
    0b1 << 2,
    0b1 << 3,
    0b1 << 4,
    0b1 << 5,
    0b1 << 6,
    0b1 << 7,
    0b1 << 8,
    0b1 << 9,
    0b1 << 10,
    0b1 << 11,
    0b1 << 12,
    0b1 << 13 | 0b1,
];

const SUITS: [Suit; 4] = [Suit::Clubs, Suit::Diamonds, Suit::Hearts, Suit::Spades];

impl Deck {
    /// Creates an empty deck with no cards.
    pub fn empty() -> Self {
        Self { cards: 0 }
    }

    /// Creates a deck containing all possible cards.
    pub fn all_cards() -> Self {
        Self {
            cards: ALL_CARDS_BITFIELD,
        }
    }

    /// Parses a string representation of a deck and returns a `Deck` instance if successful.
    /// # Arguments
    ///
    /// * `val` - A string representation of the deck, with cards separated by spaces.
    ///
    /// # Returns
    ///
    /// A `Result` containing the `Deck` instance if parsing is successful, or an error message if parsing fails.
    pub fn parse(val: &str) -> Result<Self, DucyError> {
        let owned_cards: Vec<Card> = val
            .split(' ')
            .map(Card::parse)
            .collect::<Result<Vec<Card>, DucyError>>()?;

        let mut empty = Self::empty();
        empty.insert_cards(owned_cards.iter());
        Ok(empty)
    }

    /// Creates a `Deck` containing a single specified card.
    ///
    /// # Arguments
    ///
    /// * `rank` - The rank of the card.
    /// * `suit` - The suit of the card.
    ///
    /// # Returns
    ///
    /// A `Deck` instance containing only the specified card.
    pub fn get_card(rank: &Rank, suit: &Suit) -> Self {
        Self {
            cards: Self::get_bits_for_card(rank, suit),
        }
    }

    /// Checks if the deck contains the specified card.
    pub fn has_card(&self, card: &Card) -> bool {
        card.get_deck().cards & self.cards > 0
    }

    /// Checks if the deck contains the specified cards.
    pub fn has_cards(&self, deck: &Deck) -> bool {
        deck.cards & self.cards == deck.cards
    }

    /// Inserts multiple cards into the deck.
    ///
    /// # Arguments
    ///
    /// * `cards` - An iterator over the cards to be inserted into the deck.
    pub fn insert_cards<'a>(&mut self, cards: impl Iterator<Item = &'a Card>) {
        for card in cards {
            self.cards |= card.get_deck().cards
        }
    }

    /// Removes multiple cards from the deck.
    ///
    /// # Arguments
    ///
    /// * `cards` - An iterator over the cards to be removed from the deck.
    ///
    /// # Returns
    ///
    /// None. The deck is modified in place.
    pub fn remove_cards(&mut self, cards: impl Iterator<Item = Card>) {
        for card in cards {
            self.cards &= !card.get_deck().cards;
        }
    }

    /// Removes the nth card from the deck with checking bounds.
    /// # Arguments
    ///
    /// * `index` - The index of the card to be removed.
    ///
    /// # Returns
    ///
    /// A `Result` containing the removed `Card` if successful, or an error message if the index is out of bounds.
    fn try_remove_nth_card(&mut self, index: usize) -> Result<Card, DucyError> {
        let num_cards = self.num_cards();
        if index >= num_cards as usize {
            return Err(DucyError::NotEnoughCards);
        }
        Ok(self.remove_nth_card_unchecked(index))
    }

    /// Returns an iterator over the cards in the deck.
    ///
    /// # Arguments
    ///
    /// * `rank_first` - If true, the iterator will prioritize ranks over suits.
    pub fn iter(self, rank_first: bool) -> impl Iterator<Item = Card> {
        DeckCardIterator::new(self, rank_first)
    }

    /// Returns the nth card from the deck if it exists.
    ///
    /// # Arguments
    ///
    /// * `index` - The index of the card to retrieve.
    ///
    /// # Returns
    ///
    /// An `Option` containing the `Card` if it exists, or `None` if the index is out of bounds.
    ///
    pub fn try_get_nth_card(&self, index: usize) -> Option<Card> {
        let num_cards = self.num_cards();
        if index >= num_cards as usize {
            None
        } else {
            Some(self.get_nth_card_unchecked(index))
        }
    }

    /// Attempts to remove a specified number of random cards from the deck.
    ///
    /// # Arguments
    ///
    /// * `number_to_remove` - The number of random cards to remove from the deck.
    ///
    /// # Returns
    ///
    /// A `Result` containing a vector of the removed `Card`s if successful, or an error message if there are not enough cards in the deck.
    pub fn try_remove_random_cards(
        &mut self,
        number_to_remove: u32,
    ) -> Result<Vec<Card>, DucyError> {
        let mut num_cards = self.num_cards();
        let mut cards = vec![];

        if number_to_remove > num_cards {
            return Err(DucyError::NotEnoughCards);
        }
        for _ in 0..number_to_remove {
            let index = rand::random_range(0..num_cards);
            cards.push(self.try_remove_nth_card(index as usize)?);
            num_cards -= 1;
        }
        Ok(cards)
    }

    /// Returns `true` if the deck is empty, `false` otherwise.
    pub fn is_empty(&self) -> bool {
        self.cards == 0
    }

    /// Returns the number of cards currently in the deck.
    pub fn num_cards(&self) -> u32 {
        let all_ranks = Self::get_without_low_aces(self.cards);
        u64::count_ones(all_ranks)
    }

    /// Returns `true` if all cards in the deck belong to a single suit.
    pub fn is_single_suit(&self) -> bool {
        self.single_suit_index().is_some()
    }

    /// Returns the suit index (0-3) if all cards share one suit, or `None`.
    pub fn single_suit_index(&self) -> Option<usize> {
        let no_low = Self::get_without_low_aces(self.cards);
        if no_low == 0 {
            return None;
        }
        for i in 0..4 {
            let mask = SINGLE_SUIT_HIGH_ACE_BITFIELD << (16 * i);
            if no_low & mask == no_low {
                return Some(i);
            }
        }
        None
    }

    /// Returns `true` if all cards belong to the suit at the given index (0-3).
    pub fn all_in_suit_index(&self, suit_index: usize) -> bool {
        let no_low = Self::get_without_low_aces(self.cards);
        if no_low == 0 {
            return false;
        }
        let mask = SINGLE_SUIT_HIGH_ACE_BITFIELD << (16 * suit_index);
        no_low & mask == no_low
    }

    /// Returns `true` if any rank appears more than once.
    pub fn has_rank_pair(&self) -> bool {
        self.get_combined_ranks().num_unique_ranks() < self.num_cards()
    }

    /// Returns an iterator over for each suit in the deck that returns a set of which ranks are present in each suit.  
    pub fn get_single_suit_ranks(&self) -> impl Iterator<Item = (RankSet, Suit)> {
        SingleSuitRankIterator {
            deck: *self,
            suit_index: 0,
        }
    }

    /// Returns which ranks are present for the specified suit index.
    /// Used to obtain a combined view of all the ranks in the deck, regardless of suit.
    pub fn get_combined_ranks(&self) -> RankSet {
        RankSet {
            ranks: self.get_combined_ranks_bits(),
        }
    }

    /// Returns the count of each rank in the deck as a `RankCount` object.
    pub fn get_rank_count(&self) -> RankCount {
        RankCount {
            rank_counts: self.get_rank_counts(),
        }
    }

    /// Returns an iterator over all possible combinations of the specified number of cards from the deck.
    /// # Arguments
    ///
    /// * `num_cards` - The number of cards to include in each combination.
    ///
    /// # Returns
    ///
    /// An iterator over all possible combinations of the specified number of cards from the deck.
    pub fn enumerate_combinations(self, num_cards: usize) -> impl Iterator<Item = Deck> {
        DeckIterator::new(self, num_cards)
    }

    fn get_single_suit_card_ranks(&self, i: usize) -> u64 {
        self.cards >> (16 * i) & SINGLE_SUIT_BITFIELD
    }

    fn get_combined_ranks_bits(&self) -> u64 {
        // combine the deck for all suits
        let mut ranks_combined = 0;
        for i in 0..4 {
            ranks_combined |= self.get_single_suit_card_ranks(i);
        }
        ranks_combined
    }

    fn get_nth_card_unchecked(&self, index: usize) -> Card {
        self.iter(true)
            .nth(index)
            .expect("index out of bounds in get_nth_card_unchecked")
    }

    fn get_rank_counts(&self) -> [u32; 13] {
        let mut counts = [0; 13];
        (0..13).for_each(|i| {
            let filtered = self.cards & (SINGLE_RANK_FILTER >> i);
            counts[i] = filtered.count_ones();
        });
        counts
    }

    fn get_without_low_aces(cards: u64) -> u64 {
        cards & ALL_CARDS_NO_LOW_ACES_BITFIELD
    }

    fn remove_nth_card_unchecked(&mut self, index: usize) -> Card {
        let card = self.get_nth_card_unchecked(index);
        self.remove_cards([card].into_iter());
        card
    }

    fn get_bits_for_card(rank: &Rank, suit: &Suit) -> u64 {
        let rank_bits: u64 = match rank {
            Rank::Ace => 1 << 13 | 1,
            Rank::Two => 1 << 1,
            Rank::Three => 1 << 2,
            Rank::Four => 1 << 3,
            Rank::Five => 1 << 4,
            Rank::Six => 1 << 5,
            Rank::Seven => 1 << 6,
            Rank::Eight => 1 << 7,
            Rank::Nine => 1 << 8,
            Rank::Ten => 1 << 9,
            Rank::Jack => 1 << 10,
            Rank::Queen => 1 << 11,
            Rank::King => 1 << 12,
        };
        let suit_shift = match suit {
            Suit::Clubs => 0,
            Suit::Diamonds => 16,
            Suit::Hearts => 32,
            Suit::Spades => 48,
        };
        rank_bits << suit_shift
    }
}

struct DeckCardIterator {
    suit_index: usize,
    rank_index: usize,
    rank_first: bool,
    deck: Deck,
}

impl DeckCardIterator {
    fn new(deck: Deck, rank_first: bool) -> Self {
        Self {
            suit_index: 0,
            rank_index: 0,
            rank_first,
            deck,
        }
    }
}

impl Iterator for DeckCardIterator {
    type Item = Card;

    fn next(&mut self) -> Option<Self::Item> {
        if self.suit_index >= 4 || self.rank_index >= 13 {
            None
        } else {
            let bit_to_check = RANK_BITS[12 - self.rank_index] << (16 * self.suit_index);
            let card = if self.deck.cards & bit_to_check == bit_to_check {
                Some(Card::from_deck_unchecked(
                    Deck {
                        cards: bit_to_check,
                    },
                    RANKS[13 - self.rank_index],
                    SUITS[self.suit_index],
                ))
            } else {
                None
            };

            if self.rank_first {
                self.suit_index = (self.suit_index + 1) % 4;
                if self.suit_index == 0 {
                    self.rank_index += 1;
                }
            } else {
                self.rank_index = (self.rank_index + 1) % 13;
                if self.rank_index == 0 {
                    self.suit_index += 1;
                }
            }

            if card.is_some() { card } else { self.next() }
        }
    }
}

struct SingleSuitRankIterator {
    deck: Deck,
    suit_index: usize,
}

impl Iterator for SingleSuitRankIterator {
    type Item = (RankSet, Suit);

    fn next(&mut self) -> Option<Self::Item> {
        if self.suit_index == 4 {
            None
        } else {
            let ranks = self.deck.get_single_suit_card_ranks(self.suit_index);
            let suit = SUITS[self.suit_index];
            self.suit_index += 1;
            Some((RankSet { ranks }, suit))
        }
    }
}

/// A bitfield representing which ranks are present in a suit or combined view.
pub struct RankSet {
    ranks: u64,
}

impl RankSet {
    /// Returns the number of unique ranks present.
    pub fn num_unique_ranks(&self) -> u32 {
        (self.ranks & SINGLE_SUIT_LOW_ACE_BITFIELD).count_ones()
    }

    /// Returns the highest five ranks present, if there are at least five unique ranks.
    pub fn get_highest_five(&self, rank_order: &RankOrder) -> Option<[Rank; 5]> {
        let all_ranks = self.ranks
            & match rank_order {
                RankOrder::AceIsHigh => SINGLE_SUIT_HIGH_ACE_BITFIELD,
                RankOrder::AceIsLow => SINGLE_SUIT_LOW_ACE_BITFIELD,
            };

        if u64::count_ones(all_ranks) < 5 {
            None
        } else {
            let mut result = [Rank::Two; 5];
            let mut found = 0;
            for i in 1..14 {
                let target = 0b1 << (14 - i);
                if target & all_ranks == target {
                    result[found] = RANKS[14 - i];
                    found += 1;
                    if found == 5 {
                        break;
                    }
                }
            }
            Some(result)
        }
    }

    /// Checks if the rank set matches a specific bit pattern.
    ///
    /// `required_on_bits` specifies which bits must be set.
    /// `field_length` specifies the length of the bit field to check.
    /// Returns the highest rank that matches the pattern, if any.
    pub fn matches_pattern(&self, required_on_bits: u64, field_length: usize) -> Option<Rank> {
        for i in 0..(15 - field_length) {
            let must_match = required_on_bits << (14 - field_length - i);
            if must_match & self.ranks == must_match {
                return Some(RANKS[13 - i]);
            }
        }
        None
    }
}

/// Counts of each rank present in a deck.
pub struct RankCount {
    rank_counts: [u32; 13],
}

impl RankCount {
    /// Returns the highest rank that appears at least `target_count` times, excluding specified ranks.
    pub fn find_highest_with_n(
        &self,
        ranks_to_exclude: &[Rank],
        target_count: u32,
    ) -> Option<Rank> {
        for (index, val) in self.rank_counts.iter().enumerate() {
            if ranks_to_exclude.contains(&RANKS[13 - index]) {
                continue;
            } else if *val >= target_count {
                return Some(RANKS[13 - index]);
            }
        }
        None
    }
}

impl BitOr for Deck {
    type Output = Deck;

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

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

impl BitOrAssign<Card> for Deck {
    fn bitor_assign(&mut self, rhs: Card) {
        self.cards |= rhs.val.cards
    }
}

impl Sub for Deck {
    type Output = Deck;

    fn sub(self, rhs: Self) -> Self::Output {
        let cards = self.cards & !(rhs.cards);
        Deck { cards }
    }
}

impl Sub for &Deck {
    type Output = Deck;

    fn sub(self, rhs: Self) -> Self::Output {
        let cards = self.cards & !(rhs.cards);
        Deck { cards }
    }
}

impl SubAssign for Deck {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}
impl<'a> SubAssign<&'a Deck> for Deck {
    fn sub_assign(&mut self, rhs: &'a Deck) {
        self.cards &= !rhs.cards;
    }
}

impl From<u64> for Deck {
    fn from(value: u64) -> Self {
        Deck { cards: value }
    }
}

impl From<Deck> for u64 {
    fn from(val: Deck) -> Self {
        val.cards
    }
}

struct CardIterator {
    inner: DeckCardIterator,
}

impl CardIterator {
    pub fn new() -> Self {
        Self {
            inner: DeckCardIterator::new(Deck::all_cards(), true),
        }
    }
}

impl Iterator for CardIterator {
    type Item = Card;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

struct DeckIterator {
    card_bits: [u64; 52],
    indices: [usize; 5],
    n: usize,
    k: usize,
    done: bool,
}

impl DeckIterator {
    fn new(deck: Deck, size: usize) -> Self {
        debug_assert!(size <= 5);
        let mut card_bits = [0u64; 52];
        let mut n = 0;
        for suit_idx in 0..4usize {
            let shift = suit_idx * 16;
            for rank_bits in &RANK_BITS {
                let bits = rank_bits << shift;
                if deck.cards & bits == bits {
                    card_bits[n] = bits;
                    n += 1;
                }
            }
        }
        let done = size == 0 || size > n;
        let mut indices = [0usize; 5];
        for (i, idx) in indices.iter_mut().enumerate().take(size.min(5)) {
            *idx = i;
        }
        Self {
            card_bits,
            indices,
            n,
            k: size,
            done,
        }
    }
}

impl Iterator for DeckIterator {
    type Item = Deck;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }
        let mut bits = 0u64;
        for idx in 0..self.k {
            bits |= self.card_bits[self.indices[idx]];
        }
        let mut i = self.k;
        while i > 0 {
            i -= 1;
            if self.indices[i] != i + self.n - self.k {
                break;
            }
            if i == 0 {
                self.done = true;
                return Some(Deck::from(bits));
            }
        }
        self.indices[i] += 1;
        for j in (i + 1)..self.k {
            self.indices[j] = self.indices[j - 1] + 1;
        }
        Some(Deck::from(bits))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::test_util::deck_from_cards;

    #[test]
    pub fn test_has_card() {
        let mut empty = Deck::empty();
        let mut full = Deck::all_cards();

        for card in Card::values() {
            assert!(!empty.has_card(&card));
            assert!(full.has_card(&card))
        }

        let two_clubs = Card::new(Rank::Two, Suit::Clubs);
        let three_clubs = Card::new(Rank::Three, Suit::Clubs);

        empty.insert_cards([two_clubs].iter());
        full.remove_cards([three_clubs].into_iter());
        assert!(empty.has_card(&two_clubs));
        assert!(!empty.has_card(&three_clubs));
        assert!(!full.has_card(&three_clubs));
        assert!(full.has_card(&two_clubs));

        let card = empty.try_remove_nth_card(0).unwrap();
        assert_eq!(card, two_clubs);
        assert!(!empty.has_card(&two_clubs));

        println!("{}", full);
        let card = full.try_remove_nth_card(3).unwrap();
        assert_eq!(card, Card::parse("As").unwrap());
        assert_eq!(full.num_cards(), 50);
        let card = full.try_remove_nth_card(10).unwrap();
        assert_eq!(card, Card::parse("Qs").unwrap());
        assert_eq!(full.num_cards(), 49);
    }

    #[test]
    pub fn test_deck_display() {
        let deck = Deck::all_cards();
        println!("{}", deck);
    }

    #[test]
    fn every_rank_and_suit_once_in_order() {
        let ranks: Vec<String> = Rank::iter().map(|r| r.to_string()).collect();
        assert_eq!(ranks.concat(), "23456789TJQKA");
        let suits: Vec<String> = Suit::iter().map(|s| s.to_string()).collect();
        assert_eq!(suits.concat(), "cdhs");
    }

    #[test]
    pub fn test_iterator() {
        let deck = deck_from_cards("As Ks Qs");

        let mut subdeck_1 = deck_from_cards("As Ks");
        let mut subdeck_2 = deck_from_cards("As Qs");
        let mut subdeck_3 = deck_from_cards("Ks Qs");

        for subdeck in deck.enumerate_combinations(2) {
            assert_eq!(subdeck.num_cards(), 2);
            if subdeck_1 == subdeck {
                subdeck_1.try_remove_nth_card(0).unwrap();
                subdeck_1.try_remove_nth_card(0).unwrap();
            } else if subdeck_2 == subdeck {
                subdeck_2.try_remove_nth_card(0).unwrap();
                subdeck_2.try_remove_nth_card(0).unwrap();
            } else if subdeck_3 == subdeck {
                subdeck_3.try_remove_nth_card(0).unwrap();
                subdeck_3.try_remove_nth_card(0).unwrap();
            } else {
                unreachable!();
            }
        }
    }

    #[test]
    pub fn create_card_works() {
        let card = Card::new(Rank::Ace, Suit::Spades);
        assert_eq!(Rank::Ace, card.rank());
        assert_eq!(Suit::Spades, card.suit());
    }

    #[test]
    pub fn test_display_for_card() {
        let card = Card::new(Rank::Ace, Suit::Spades);
        assert_eq!(format!("{card}"), "As");
    }

    #[test]
    pub fn test_from_str() -> Result<(), DucyError> {
        for i in 0..52 {
            let card = Deck::all_cards().try_get_nth_card(i).unwrap();
            let display: String = format!("{}", card);
            let other_card = Card::parse(&display)?;
            assert_eq!(other_card, card);
        }

        assert_eq!(Card::parse("As")?, Card::new(Rank::Ace, Suit::Spades));

        Ok(())
    }

    #[test]
    pub fn test_card_fromstr_trait() {
        let card: Card = "As".parse().unwrap();
        assert_eq!(card, Card::new(Rank::Ace, Suit::Spades));
        assert!("xx".parse::<Card>().is_err());
    }

    #[test]
    pub fn test_deck_fromstr_trait() {
        let deck: Deck = "As Kh".parse().unwrap();
        assert_eq!(deck, Deck::parse("As Kh").unwrap());
        assert_eq!(deck.num_cards(), 2);
    }

    #[test]
    pub fn test_parse_invalid_rank() {
        assert_eq!(
            Rank::try_from_char(&'x'),
            Err(crate::error::DucyError::InvalidRank)
        );
        assert_eq!(
            Rank::try_from_char(&'1'),
            Err(crate::error::DucyError::InvalidRank)
        );
    }

    #[test]
    pub fn test_parse_invalid_suit() {
        assert_eq!(
            Suit::try_from_char(&'x'),
            Err(crate::error::DucyError::InvalidSuit)
        );
        assert_eq!(
            Suit::try_from_char(&'a'),
            Err(crate::error::DucyError::InvalidSuit)
        );
    }

    #[test]
    pub fn test_parse_invalid_card() {
        assert!(Card::parse("").is_err());
        assert!(Card::parse("A").is_err());
        assert!(Card::parse("Xc").is_err());
        assert!(Card::parse("Ax").is_err());
    }

    #[test]
    pub fn test_parse_invalid_deck() {
        assert!(Deck::parse("As Xc").is_err());
        assert!(Deck::parse("not_cards").is_err());
    }

    #[test]
    pub fn test_deck_num_cards() {
        assert_eq!(Deck::empty().num_cards(), 0);
        assert_eq!(Deck::all_cards().num_cards(), 52);
        assert_eq!(deck_from_cards("As Ks Qs").num_cards(), 3);
    }

    #[test]
    pub fn test_deck_is_empty() {
        assert!(Deck::empty().is_empty());
        assert!(!Deck::all_cards().is_empty());
    }

    #[test]
    pub fn test_deck_subtraction() {
        let full = Deck::all_cards();
        let aces = deck_from_cards("As Ah Ad Ac");
        let remaining = full - aces;
        assert_eq!(remaining.num_cards(), 48);
        assert!(!remaining.has_cards(&aces));
    }

    #[test]
    pub fn test_deck_bitor() {
        let d1 = deck_from_cards("As Ks");
        let d2 = deck_from_cards("Qs Js");
        let combined = d1 | d2;
        assert_eq!(combined.num_cards(), 4);
        assert!(combined.has_cards(&d1));
        assert!(combined.has_cards(&d2));
    }

    #[test]
    pub fn test_try_remove_nth_card_out_of_bounds() {
        let mut deck = deck_from_cards("As Ks");
        assert!(deck.try_remove_nth_card(5).is_err());
    }

    #[test]
    pub fn test_try_remove_random_cards() {
        let mut deck = Deck::all_cards();
        let removed = deck.try_remove_random_cards(5).unwrap();
        assert_eq!(removed.len(), 5);
        assert_eq!(deck.num_cards(), 47);
        for card in &removed {
            assert!(!deck.has_card(card));
        }
    }

    #[test]
    pub fn test_try_remove_random_cards_too_many() {
        let mut deck = deck_from_cards("As Ks");
        assert!(deck.try_remove_random_cards(3).is_err());
    }

    #[test]
    pub fn test_remove_cards_does_not_add() {
        let mut deck = deck_from_cards("As Ks");
        let three_clubs = Card::new(Rank::Three, Suit::Clubs);
        assert!(!deck.has_card(&three_clubs));
        deck.remove_cards([three_clubs].into_iter());
        assert!(!deck.has_card(&three_clubs));
        assert_eq!(deck.num_cards(), 2);
    }

    #[test]
    pub fn test_try_get_nth_card_at_boundary() {
        let deck = deck_from_cards("As Ks");
        assert!(deck.try_get_nth_card(0).is_some());
        assert!(deck.try_get_nth_card(1).is_some());
        assert!(deck.try_get_nth_card(2).is_none());
    }

    #[cfg(feature = "serde")]
    #[test]
    pub fn test_serde_card_roundtrip() {
        let card = Card::new(Rank::Ace, Suit::Spades);
        let json = serde_json::to_string(&card).unwrap();
        assert_eq!(json, "\"As\"");
        let deserialized: Card = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, card);
    }

    #[cfg(feature = "serde")]
    #[test]
    pub fn test_serde_deck_roundtrip() {
        let deck = deck_from_cards("As Kh Qd");
        let json = serde_json::to_string(&deck).unwrap();
        let deserialized: Deck = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, deck);
    }

    #[cfg(feature = "serde")]
    #[test]
    pub fn test_serde_rank_roundtrip() {
        let rank = Rank::Ace;
        let json = serde_json::to_string(&rank).unwrap();
        let deserialized: Rank = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, rank);
    }
}