bowling 0.2.0

A generic, typestate-driven bowling game engine
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
//! Pin-deck representation as a compact bitset.
//!
//! [`PinSet`] stores up to 16 pins in a `u16` bitmask. Pin indices are
//! zero-based: pin 0 is the head pin (or the first pin in a generic layout).

use std::{fmt, ops};

// ---------------------------------------------------------------------------
// PinSet
// ---------------------------------------------------------------------------

/// A set of pins represented as a `u16` bitmask (up to 16 pins).
///
/// Bit *i* is `1` when pin *i* is present in the set. The type is `Copy` and
/// all operations are `const`-friendly where possible.
///
/// # Construction
///
/// ```
/// # use bowling::pins::PinSet;
/// // Infallible, const-evaluable constructors:
/// let ten = PinSet::full::<10>();          // first 10 pins
/// let specific = PinSet::of([3, 5, 6, 9]); // pins by index
/// let span = PinSet::range(5, 10);         // pins 5..10
///
/// // Collect from an iterator:
/// let collected: PinSet = (0..5).collect();
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct PinSet(u16);

impl PinSet {
    /// The empty set (no pins).
    pub const EMPTY: Self = Self(0);

    /// Creates a full pin set with the first `N` pins standing.
    ///
    /// This is an infallible, `const`-evaluable constructor. The pin count is
    /// checked at compile time when used in a const context; exceeding 16
    /// triggers a compile-time error.
    ///
    /// ```
    /// # use bowling::pins::PinSet;
    /// let ten = PinSet::full::<10>();
    /// assert_eq!(ten.count(), 10);
    /// ```
    #[inline]
    pub const fn full<const N: u8>() -> Self {
        assert!(N <= 16, "pin count exceeds maximum of 16");
        if N == 16 {
            Self(u16::MAX)
        } else {
            Self((1u16 << N) - 1)
        }
    }

    /// Creates a `PinSet` containing exactly the given pin indices.
    ///
    /// ```
    /// # use bowling::pins::PinSet;
    /// let ps = PinSet::of([0, 1, 2]);
    /// assert_eq!(ps.count(), 3);
    /// assert!(ps.contains(0) && ps.contains(1) && ps.contains(2));
    /// ```
    #[inline]
    pub const fn of<const N: usize>(pins: [u8; N]) -> Self {
        let mut bits = 0u16;
        let mut i = 0;
        while i < N {
            bits |= 1 << pins[i];
            i += 1;
        }
        Self(bits)
    }

    /// Creates a `PinSet` with pin indices in the half-open range
    /// `[start, end)`.
    ///
    /// ```
    /// # use bowling::pins::PinSet;
    /// let ps = PinSet::range(3, 7);
    /// assert_eq!(ps, PinSet::of([3, 4, 5, 6]));
    /// ```
    #[inline]
    pub const fn range(start: u8, end: u8) -> Self {
        assert!(end <= 16, "end exceeds maximum of 16");
        assert!(start <= end, "start must not exceed end");
        if start == end {
            return Self::EMPTY;
        }
        // bits [start, end): mask with `end` bits set, minus mask with `start` bits set.
        let hi = if end == 16 {
            u16::MAX
        } else {
            (1u16 << end) - 1
        };
        let lo = if start == 16 {
            u16::MAX
        } else {
            (1u16 << start) - 1
        };
        Self(hi & !lo)
    }

    /// Creates a `PinSet` from a raw bitmask. No validation; caller must
    /// ensure the mask only uses bits 0..`pin_count`.
    #[inline]
    pub const fn from_raw(bits: u16) -> Self {
        Self(bits)
    }

    /// Returns the underlying bitmask.
    #[inline]
    pub const fn to_raw(self) -> u16 {
        self.0
    }

    /// Returns the number of pins in the set.
    #[inline]
    #[expect(
        clippy::cast_possible_truncation,
        reason = "u16 has at most 16 bits set"
    )]
    pub const fn count(self) -> u8 {
        self.0.count_ones() as u8
    }

    /// Returns `true` if the set contains no pins.
    #[inline]
    pub const fn is_empty(self) -> bool {
        self.0 == 0
    }

    /// Returns `true` if pin `idx` is in the set.
    #[inline]
    pub const fn contains(self, idx: u8) -> bool {
        (self.0 >> idx) & 1 == 1
    }

    /// Returns `true` if `self` and `other` share no common pins.
    #[inline]
    pub const fn is_disjoint(self, other: Self) -> bool {
        (self.0 & other.0) == 0
    }

    /// Returns `true` if every pin in `self` is also in `other`.
    #[inline]
    pub const fn is_subset_of(self, other: Self) -> bool {
        other.contains_all(self)
    }

    /// Adds a single pin to the set.
    #[inline]
    pub const fn insert(self, idx: u8) -> Self {
        Self(self.0 | (1 << idx))
    }

    /// Removes a single pin from the set.
    #[inline]
    pub const fn remove(self, idx: u8) -> Self {
        Self(self.0 & !(1 << idx))
    }

    /// Returns `true` if `other` is a subset of `self` (all bits in `other`
    /// are also set in `self`).
    #[inline]
    pub const fn contains_all(self, other: Self) -> bool {
        (self.0 & other.0) == other.0
    }

    /// Set difference: pins in `self` that are **not** in `other`.
    ///
    /// Also available via the `-` operator.
    #[inline]
    pub const fn difference(self, other: Self) -> Self {
        Self(self.0 & !other.0)
    }

    /// Set intersection: pins in both `self` and `other`.
    ///
    /// Also available via the `&` operator.
    #[inline]
    pub const fn intersection(self, other: Self) -> Self {
        Self(self.0 & other.0)
    }

    /// Set union: pins in either `self` or `other`.
    ///
    /// Also available via the `|` operator.
    #[inline]
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    /// Complement within a given pin count: the pins in `0..N` that
    /// are **not** in `self`.
    ///
    /// ```
    /// # use bowling::pins::PinSet;
    /// let standing = PinSet::of([0, 1, 2]);
    /// let knocked = standing.complement_within::<10>();
    /// assert_eq!(knocked, PinSet::range(3, 10));
    /// ```
    #[inline]
    pub const fn complement_within<const N: u8>(self) -> Self {
        Self::full::<N>().difference(self)
    }

    /// Iterate over the pin indices present in the set.
    pub fn iter(self) -> PinSetIter {
        PinSetIter(self.0)
    }
}

// ---------------------------------------------------------------------------
// Operator trait impls
// ---------------------------------------------------------------------------

/// Set union: `a | b` returns pins in either set.
impl ops::BitOr for PinSet {
    type Output = Self;

    #[inline]
    fn bitor(self, rhs: Self) -> Self {
        self.union(rhs)
    }
}

/// Set union assignment: `a |= b`.
impl ops::BitOrAssign for PinSet {
    #[inline]
    fn bitor_assign(&mut self, rhs: Self) {
        *self = *self | rhs;
    }
}

/// Set intersection: `a & b` returns pins in both sets.
impl ops::BitAnd for PinSet {
    type Output = Self;

    #[inline]
    fn bitand(self, rhs: Self) -> Self {
        self.intersection(rhs)
    }
}

/// Set intersection assignment: `a &= b`.
impl ops::BitAndAssign for PinSet {
    #[inline]
    fn bitand_assign(&mut self, rhs: Self) {
        *self = *self & rhs;
    }
}

/// Set difference: `a - b` returns pins in `a` that are not in `b`.
impl ops::Sub for PinSet {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self {
        self.difference(rhs)
    }
}

/// Set difference assignment: `a -= b`.
impl ops::SubAssign for PinSet {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

/// Bitwise complement: `!a` flips all 16 bits.
///
/// Note: this flips all 16 bits regardless of pin count. For a
/// complement within a specific pin count, use
/// [`complement_within`](PinSet::complement_within).
impl ops::Not for PinSet {
    type Output = Self;

    #[inline]
    fn not(self) -> Self {
        Self(!self.0)
    }
}

// ---------------------------------------------------------------------------
// FromIterator / Extend
// ---------------------------------------------------------------------------

/// Collect pin indices into a `PinSet`.
///
/// ```
/// # use bowling::pins::PinSet;
/// let ps: PinSet = [0u8, 2, 4].into_iter().collect();
/// assert_eq!(ps, PinSet::of([0, 2, 4]));
/// ```
impl FromIterator<u8> for PinSet {
    fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self {
        let mut bits = 0u16;
        for idx in iter {
            bits |= 1 << idx;
        }
        Self(bits)
    }
}

/// Extend a `PinSet` with additional pin indices.
impl Extend<u8> for PinSet {
    fn extend<I: IntoIterator<Item = u8>>(&mut self, iter: I) {
        for idx in iter {
            self.0 |= 1 << idx;
        }
    }
}

impl fmt::Debug for PinSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "PinSet({:#06x}, count={})", self.0, self.count())
    }
}

impl fmt::Display for PinSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        let mut first = true;
        for idx in *self {
            if !first {
                write!(f, ", ")?;
            }
            write!(f, "{idx}")?;
            first = false;
        }
        write!(f, "}}")
    }
}

impl IntoIterator for PinSet {
    type Item = u8;
    type IntoIter = PinSetIter;

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

// ---------------------------------------------------------------------------
// PinSetIter
// ---------------------------------------------------------------------------

/// Iterator over pin indices in a [`PinSet`].
pub struct PinSetIter(u16);

impl Iterator for PinSetIter {
    type Item = u8;

    fn next(&mut self) -> Option<u8> {
        if self.0 == 0 {
            None
        } else {
            #[expect(
                clippy::cast_possible_truncation,
                reason = "u16 trailing zeros fits in u8"
            )]
            let idx = self.0.trailing_zeros() as u8;
            self.0 &= self.0 - 1; // clear lowest set bit
            Some(idx)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let c = self.0.count_ones() as usize;
        (c, Some(c))
    }
}

impl ExactSizeIterator for PinSetIter {}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn full_ten_pins() {
        let ps = PinSet::full::<10>();
        assert_eq!(ps.count(), 10);
        assert!(ps.contains(0));
        assert!(ps.contains(9));
        assert!(!ps.contains(10));
    }

    #[test]
    fn empty_set() {
        let ps = PinSet::EMPTY;
        assert!(ps.is_empty());
        assert_eq!(ps.count(), 0);
    }

    #[test]
    fn subset_operations() {
        let full = PinSet::full::<10>();
        let knocked = PinSet::of([0, 1]);
        assert!(full.contains_all(knocked));
        let remaining = full - knocked;
        assert_eq!(remaining.count(), 8);
        assert!(!remaining.contains(0));
        assert!(!remaining.contains(1));
        assert!(remaining.contains(2));
    }

    #[test]
    fn iter_collects_correctly() {
        let ps = PinSet::of([1, 3]);
        let indices: Vec<u8> = ps.iter().collect();
        assert_eq!(indices, vec![1, 3]);
    }

    #[test]
    fn full_sixteen_pins() {
        let ps = PinSet::full::<16>();
        assert_eq!(ps.count(), 16);
    }

    #[test]
    fn of_creates_correct_set() {
        let ps = PinSet::of([3, 5, 6, 9]);
        assert_eq!(ps.count(), 4);
        assert!(ps.contains(3));
        assert!(ps.contains(5));
        assert!(ps.contains(6));
        assert!(ps.contains(9));
        assert!(!ps.contains(0));
    }

    #[test]
    fn range_creates_correct_span() {
        let ps = PinSet::range(3, 7);
        assert_eq!(ps, PinSet::of([3, 4, 5, 6]));
        assert_eq!(ps.count(), 4);
    }

    #[test]
    fn range_empty_when_equal() {
        assert_eq!(PinSet::range(5, 5), PinSet::EMPTY);
    }

    #[test]
    fn from_iterator_collects() {
        let ps: PinSet = vec![0u8, 2, 4].into_iter().collect();
        assert_eq!(ps, PinSet::of([0, 2, 4]));
    }

    #[test]
    fn operators_match_named_methods() {
        let a = PinSet::of([0, 1, 2, 3]);
        let b = PinSet::of([2, 3, 4, 5]);
        assert_eq!(a | b, a.union(b));
        assert_eq!(a & b, a.intersection(b));
        assert_eq!(a - b, a.difference(b));
    }

    #[test]
    fn is_disjoint_works() {
        let a = PinSet::of([0, 1]);
        let b = PinSet::of([2, 3]);
        assert!(a.is_disjoint(b));
        assert!(!a.is_disjoint(PinSet::of([1, 2])));
    }

    #[test]
    fn is_subset_of_works() {
        let small = PinSet::of([1, 2]);
        let big = PinSet::full::<10>();
        assert!(small.is_subset_of(big));
        assert!(!big.is_subset_of(small));
    }

    #[test]
    fn complement_within_works() {
        let standing = PinSet::of([0, 1, 2]);
        assert_eq!(standing.complement_within::<10>(), PinSet::range(3, 10));
    }

    #[test]
    fn extend_adds_pins() {
        let mut ps = PinSet::of([0, 1]);
        ps.extend([2u8, 3]);
        assert_eq!(ps, PinSet::of([0, 1, 2, 3]));
    }

    #[test]
    fn assign_operators() {
        let mut ps = PinSet::of([0, 1, 2]);
        ps |= PinSet::of([3, 4]);
        assert_eq!(ps, PinSet::range(0, 5));

        ps &= PinSet::of([1, 2, 3]);
        assert_eq!(ps, PinSet::of([1, 2, 3]));

        ps -= PinSet::of([3]);
        assert_eq!(ps, PinSet::of([1, 2]));
    }
}