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
#[cfg(test)]
mod test;

use crate::Strain;
use core::fmt;
use core::num::{NonZeroU8, Wrapping};
use core::ops::{Add, AddAssign, BitAnd, BitOr, BitXor, Index, IndexMut, Not, Sub, SubAssign};
use rand::prelude::SliceRandom as _;
use thiserror::Error;

/// A suit of playing cards
///
/// Suits are convertible to [`Strain`]s since suits form a subset of strains.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Suit {
    /// ♣, convertible to [`Strain::Clubs`]
    Clubs,
    /// ♦, convertible to [`Strain::Diamonds`]
    Diamonds,
    /// ♥, convertible to [`Strain::Hearts`]
    Hearts,
    /// ♠, convertible to [`Strain::Spades`]
    Spades,
}

impl Suit {
    /// Suits in the ascending order, the order in this crate
    pub const ASC: [Self; 4] = [Self::Clubs, Self::Diamonds, Self::Hearts, Self::Spades];

    /// Suits in the descending order, the order in [`dds_bridge_sys`]
    pub const DESC: [Self; 4] = [Self::Spades, Self::Hearts, Self::Diamonds, Self::Clubs];
}

impl From<Suit> for Strain {
    fn from(suit: Suit) -> Self {
        match suit {
            Suit::Clubs => Self::Clubs,
            Suit::Diamonds => Self::Diamonds,
            Suit::Hearts => Self::Hearts,
            Suit::Spades => Self::Spades,
        }
    }
}

/// Error raised when converting [`Strain::Notrump`] to a suit
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq)]
#[error("Notrump is not a suit")]
pub struct SuitFromNotrumpError;

impl TryFrom<Strain> for Suit {
    type Error = SuitFromNotrumpError;

    fn try_from(strain: Strain) -> Result<Self, Self::Error> {
        match strain {
            Strain::Clubs => Ok(Self::Clubs),
            Strain::Diamonds => Ok(Self::Diamonds),
            Strain::Hearts => Ok(Self::Hearts),
            Strain::Spades => Ok(Self::Spades),
            Strain::Notrump => Err(SuitFromNotrumpError),
        }
    }
}

/// Position at the table
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Seat {
    /// Dealer of Board 1, partner of [`Seat::South`]
    North,
    /// Dealer of Board 2, partner of [`Seat::West`]
    East,
    /// Dealer of Board 3, partner of [`Seat::North`]
    South,
    /// Dealer of Board 4, partner of [`Seat::East`]
    West,
}

impl Seat {
    /// Seats in the order of dealing
    pub const ALL: [Self; 4] = [Self::North, Self::East, Self::South, Self::West];
}

impl Add<Wrapping<u8>> for Seat {
    type Output = Self;

    fn add(self, rhs: Wrapping<u8>) -> Self {
        // SAFETY: this is just modular arithmetics on a 4-element enum
        unsafe { core::mem::transmute((Wrapping(self as u8) + rhs).0 & 3) }
    }
}

impl Add<Seat> for Wrapping<u8> {
    type Output = Seat;

    fn add(self, rhs: Seat) -> Seat {
        rhs + self
    }
}

impl AddAssign<Wrapping<u8>> for Seat {
    fn add_assign(&mut self, rhs: Wrapping<u8>) {
        *self = *self + rhs;
    }
}

impl Sub<Wrapping<u8>> for Seat {
    type Output = Self;

    fn sub(self, rhs: Wrapping<u8>) -> Self {
        // SAFETY: this is just modular arithmetics on a 4-element enum
        unsafe { core::mem::transmute((Wrapping(self as u8) - rhs).0 & 3) }
    }
}

impl SubAssign<Wrapping<u8>> for Seat {
    fn sub_assign(&mut self, rhs: Wrapping<u8>) {
        *self = *self - rhs;
    }
}

impl Sub<Self> for Seat {
    type Output = Wrapping<u8>;

    fn sub(self, rhs: Self) -> Wrapping<u8> {
        (Wrapping(self as u8) - Wrapping(rhs as u8)) & Wrapping(3)
    }
}

impl From<Seat> for char {
    fn from(seat: Seat) -> Self {
        match seat {
            Seat::North => 'N',
            Seat::East => 'E',
            Seat::South => 'S',
            Seat::West => 'W',
        }
    }
}

bitflags::bitflags! {
    /// A set of seats
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct SeatFlags: u8 {
        /// The set containing [`Seat::North`]
        const NORTH = 0b0001;
        /// The set containing [`Seat::East`]
        const EAST = 0b0010;
        /// The set containing [`Seat::South`]
        const SOUTH = 0b0100;
        /// The set containing [`Seat::West`]
        const WEST = 0b1000;
    }
}

impl SeatFlags {
    /// The empty set
    pub const EMPTY: Self = Self::empty();

    /// The set containing all seats
    pub const ALL: Self = Self::all();

    /// The set containing [`Seat::North`] and [`Seat::South`]
    pub const NS: Self = Self::NORTH.union(Self::SOUTH);

    /// The set containing [`Seat::East`] and [`Seat::West`]
    pub const EW: Self = Self::EAST.union(Self::WEST);
}

impl From<Seat> for SeatFlags {
    fn from(seat: Seat) -> Self {
        match seat {
            Seat::North => Self::NORTH,
            Seat::East => Self::EAST,
            Seat::South => Self::SOUTH,
            Seat::West => Self::WEST,
        }
    }
}

/// A playing card
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Card(NonZeroU8);

impl Card {
    /// Create a card from suit and rank
    ///
    /// The rank is a number from 2 to 14.  J, Q, K, A are encoded as 11, 12,
    /// 13, 14 respectively.
    ///
    /// # Panics
    /// Panics if the rank is not in the range 2..=14.
    #[must_use]
    pub const fn new(suit: Suit, rank: u8) -> Self {
        assert!(rank >= 2 && rank <= 14);
        // SAFETY: rank is guaranteed to be non-zero
        Self(unsafe { NonZeroU8::new_unchecked(rank << 2 | suit as u8) })
    }

    /// The suit of the card
    #[must_use]
    pub const fn suit(self) -> Suit {
        // SAFETY: suit is guaranteed to be valid, in (0..=3)
        unsafe { core::mem::transmute(self.0.get() & 3) }
    }

    /// The rank of the card
    ///
    /// The rank is a number from 2 to 14.  J, Q, K, A are denoted as 11, 12,
    /// 13, 14 respectively.
    #[must_use]
    pub const fn rank(self) -> u8 {
        self.0.get() >> 2
    }
}

/// A bitset whose size is known at compile time
pub trait SmallSet<T>: Copy + Eq + BitAnd + BitOr + BitXor + Not + Sub {
    /// The empty set
    const EMPTY: Self;

    /// The set containing all possible values
    const ALL: Self;

    /// The number of elements in the set
    #[must_use]
    fn len(self) -> usize;

    /// Whether the set is empty
    #[must_use]
    fn is_empty(self) -> bool {
        self == Self::EMPTY
    }

    /// Whether the set contains a value
    fn contains(self, value: T) -> bool;

    /// Insert a value into the set
    fn insert(&mut self, value: T) -> bool;

    /// Remove a value from the set
    fn remove(&mut self, value: T) -> bool;

    /// Toggle a value in the set
    fn toggle(&mut self, value: T) -> bool;

    /// Iterate over the values in the set
    fn iter(self) -> impl Iterator<Item = T>;
}

/// A set of cards of the same suit
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Holding(u16);

#[derive(Debug, Clone, PartialEq, Eq)]
struct HoldingIter {
    rest: u16,
    cursor: u8,
}

impl Iterator for HoldingIter {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        if self.rest == 0 {
            return None;
        }
        // 1. Trailing zeros are in the range of 0..=15, which fits in `u8``
        // 2. Trailing zeros cannot be 15 since the bitset is from a `Holding`
        #[allow(clippy::cast_possible_truncation)]
        let step = self.rest.trailing_zeros() as u8 + 1;
        self.rest >>= step;
        self.cursor += step;
        Some(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 SmallSet<u8> for Holding {
    const EMPTY: Self = Self(0);
    const ALL: Self = Self(0x7FFC);

    fn len(self) -> usize {
        self.0.count_ones() as usize
    }

    fn contains(self, rank: u8) -> bool {
        self.0 & 1 << rank != 0
    }

    fn insert(&mut self, rank: u8) -> bool {
        let insertion = 1 << rank & Self::ALL.0;
        let inserted = insertion & !self.0 != 0;
        self.0 |= insertion;
        inserted
    }

    fn remove(&mut self, rank: u8) -> bool {
        let removed = self.contains(rank);
        self.0 &= !(1 << rank);
        removed
    }

    fn toggle(&mut self, rank: u8) -> bool {
        self.0 ^= 1 << rank & Self::ALL.0;
        self.contains(rank)
    }

    fn iter(self) -> impl Iterator<Item = u8> {
        HoldingIter {
            rest: self.0,
            cursor: 0,
        }
    }
}

impl Holding {
    /// As a bitset of ranks
    #[must_use]
    pub const fn to_bits(self) -> u16 {
        self.0
    }

    /// Create a holding from a bitset of ranks
    #[must_use]
    pub const fn from_bits(bits: u16) -> Self {
        Self(bits & Self::ALL.0)
    }
}

impl BitAnd for Holding {
    type Output = Self;

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

impl BitOr for Holding {
    type Output = Self;

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

impl BitXor for Holding {
    type Output = Self;

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

impl Not for Holding {
    type Output = Self;

    fn not(self) -> Self {
        Self::ALL ^ self
    }
}

impl Sub for Holding {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        self & !rhs
    }
}

/// Show cards in descending order
///
/// 1. The ten is shown as `T` for PBN compatibility.
/// 2. This implementation ignores formatting flags for simplicity and speed.
///    If you want to pad or align the output, use [`fmt::Formatter::pad`].
impl fmt::Display for Holding {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for rank in (2..15).rev() {
            if self.contains(rank) {
                use fmt::Write as _;
                f.write_char(b"23456789TJQKA"[rank as usize - 2] as char)?;
            }
        }
        Ok(())
    }
}

/// A hand of playing cards
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Hand(pub [Holding; 4]);

impl Index<Suit> for Hand {
    type Output = Holding;

    fn index(&self, suit: Suit) -> &Holding {
        &self.0[suit as usize]
    }
}

impl IndexMut<Suit> for Hand {
    fn index_mut(&mut self, suit: Suit) -> &mut Holding {
        &mut self.0[suit as usize]
    }
}

impl Hand {
    /// As a bitset of cards
    #[must_use]
    pub const fn to_bits(self) -> u64 {
        // SAFETY: every combination of 64 bits is a valid `u64`
        unsafe { core::mem::transmute(self.0) }
    }

    /// Create a hand from a bitset of cards
    ///
    /// This function removes invalid cards.
    #[must_use]
    pub const fn from_bits(bits: u64) -> Self {
        // SAFETY: just filtered out invalid cards
        unsafe { Self::from_bits_unchecked(bits & Self::ALL.to_bits()) }
    }

    /// Create a hand from a bitset of cards without checking
    ///
    /// # Safety
    /// The bitset must not contain invalid cards.
    #[must_use]
    pub const unsafe fn from_bits_unchecked(bits: u64) -> Self {
        core::mem::transmute(bits)
    }
}

impl SmallSet<Card> for Hand {
    const EMPTY: Self = Self([Holding::EMPTY; 4]);
    const ALL: Self = Self([Holding::ALL; 4]);

    fn len(self) -> usize {
        self.to_bits().count_ones() as usize
    }

    fn contains(self, card: Card) -> bool {
        self[card.suit()].contains(card.rank())
    }

    fn insert(&mut self, card: Card) -> bool {
        self[card.suit()].insert(card.rank())
    }

    fn remove(&mut self, card: Card) -> bool {
        self[card.suit()].remove(card.rank())
    }

    fn toggle(&mut self, card: Card) -> bool {
        self[card.suit()].toggle(card.rank())
    }

    fn iter(self) -> impl Iterator<Item = Card> {
        Suit::ASC
            .into_iter()
            .flat_map(move |suit| self[suit].iter().map(move |rank| Card::new(suit, rank)))
    }
}

/// PBN-compatible display of a hand
///
/// This implementation ignores formatting flags for simplicity and speed.
impl fmt::Display for Hand {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}.{}.{}.{}",
            self[Suit::Spades],
            self[Suit::Hearts],
            self[Suit::Diamonds],
            self[Suit::Clubs]
        )
    }
}

impl BitAnd for Hand {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self {
        // SAFETY: safe when both operands are valid
        unsafe { Self::from_bits_unchecked(self.to_bits() & rhs.to_bits()) }
    }
}

impl BitOr for Hand {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self {
        // SAFETY: safe when both operands are valid
        unsafe { Self::from_bits_unchecked(self.to_bits() | rhs.to_bits()) }
    }
}

impl BitXor for Hand {
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self {
        // SAFETY: safe when both operands are valid
        unsafe { Self::from_bits_unchecked(self.to_bits() ^ rhs.to_bits()) }
    }
}

impl Not for Hand {
    type Output = Self;

    fn not(self) -> Self {
        Self::ALL ^ self
    }
}

impl Sub for Hand {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        self & !rhs
    }
}

/// A deal of four hands
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Deal(pub [Hand; 4]);

impl Index<Seat> for Deal {
    type Output = Hand;

    fn index(&self, seat: Seat) -> &Hand {
        &self.0[seat as usize]
    }
}

impl IndexMut<Seat> for Deal {
    fn index_mut(&mut self, seat: Seat) -> &mut Hand {
        &mut self.0[seat as usize]
    }
}

struct DealDisplay {
    deal: Deal,
    seat: Seat,
}

impl fmt::Display for DealDisplay {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}:{} {} {} {}",
            char::from(self.seat),
            self.deal[self.seat],
            self.deal[self.seat + Wrapping(1)],
            self.deal[self.seat + Wrapping(2)],
            self.deal[self.seat + Wrapping(3)],
        )
    }
}

impl Deal {
    /// Create a deal from a shuffled standard 52-card deck
    pub fn new(rng: &mut (impl rand::Rng + ?Sized)) -> Self {
        let mut deck: [_; 52] = core::array::from_fn(|i| {
            // Safe because `i` is always in the range of 0..52
            #[allow(clippy::cast_possible_truncation)]
            let i = i as u8;
            let suit: Suit = unsafe { core::mem::transmute(i & 3) };
            Card::new(suit, (i >> 2) + 2)
        });
        deck.shuffle(rng);

        deck.into_iter()
            .enumerate()
            .fold(Self::default(), |mut deal, (i, card)| {
                // SAFETY: `i & 3` is always in the range of 0..=3
                #[allow(clippy::cast_possible_truncation)]
                let seat: Seat = unsafe { core::mem::transmute((i & 3) as u8) };
                deal[seat].insert(card);
                deal
            })
    }

    /// PBN-compatible display from a seat's perspective
    #[must_use]
    pub fn display(self, seat: Seat) -> impl fmt::Display {
        DealDisplay { deal: self, seat }
    }

    /// Shuffle existing hands while preserving the numbers of cards
    #[must_use]
    pub fn shuffled(self, rng: &mut (impl rand::Rng + ?Sized), seats: SeatFlags) -> Self {
        let mut deal = Self::default();
        let mut deck = Vec::with_capacity(52);
        let mut lengths = [0; 4];

        for seat in Seat::ALL {
            if seats.contains(seat.into()) {
                deck.extend(self[seat].iter());
                lengths[seat as usize] = self[seat].len();
            } else {
                deal[seat] = self[seat];
            }
        }

        deck.shuffle(rng);
        let mut seat = Seat::North;

        loop {
            let Some(card) = deck.pop() else { break };

            while lengths[seat as usize] == 0 {
                seat += Wrapping(1);
            }

            lengths[seat as usize] -= 1;
            deal[seat].insert(card);
        }
        deal
    }
}