openpql_prelude/card/
card_iter.rs

1use super::{Card, Card64, CardCount};
2
3/// Iterator over cards in a Card64 bitset.
4///
5/// Iterates through all cards set in a Card64 bitset.
6/// Produces cards grouped by rank across suits: 2s2h2c2d, then 3s3h3c3d, etc.
7#[derive(Debug, Clone)]
8pub struct CardIter {
9    c64: Card64,
10    idx: CardCount, // need this for column-major iter
11}
12
13impl CardIter {
14    pub(crate) const fn new(c64: Card64) -> Self {
15        Self { c64, idx: 0 }
16    }
17}
18
19impl Iterator for CardIter {
20    type Item = Card;
21
22    fn next(&mut self) -> Option<Self::Item> {
23        while self.idx < Card::N_CARDS {
24            let c = Card::all::<false>()[self.idx as usize];
25            self.idx += 1;
26
27            if self.c64.contains_card(c) {
28                return Some(c);
29            }
30        }
31
32        None
33    }
34
35    fn size_hint(&self) -> (usize, Option<usize>) {
36        let count = self.c64.count() as usize;
37        (count, Some(count))
38    }
39}
40
41impl ExactSizeIterator for CardIter {}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::*;
47
48    #[test]
49    fn test_order() {
50        let iter = CardIter::new(Card64::all::<false>());
51        let cards: Vec<_> = iter.collect();
52
53        assert_eq!(cards, Card::all::<false>());
54    }
55}