tokyodoves 1.0.2

A library of an efficient board of Tokyo Doves and associated toolkits
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
//! Bit-mask utility [`BitMask`] and viewer to it [`MaskViewer`].
//!
//! This module consists of [`BitMask`] and [`MaskViewer`].
//! More explanations are needed.
use array_macro::array;
use strum_macros::EnumIter;

use crate::error;
use crate::prelude::macros;

const MASKS: [BitMask; 64] = {
    let mask0 = BitMask::new();
    array![n => mask0.rotate_left_all(n as u32); 64]
};

/// Constant masks used by many kinds of bit-wise manupulation.
#[derive(Debug, Clone)]
pub struct BitMask {
    /// Represents 4x4 main region.
    ///
    /// # Examples
    ///
    /// ```text
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    pub core: u64,
    /// Outside of the union of main region and 1-bit margin.
    ///
    /// # Examples
    ///
    /// ```text
    /// 1 1 1 1 1 1 1 1
    /// 1 0 0 0 0 0 0 1
    /// 1 0 0 0 0 0 0 1
    /// 1 0 0 0 0 0 0 1
    /// 1 0 0 0 0 0 0 1
    /// 1 0 0 0 0 0 0 1
    /// 1 0 0 0 0 0 0 1
    /// 1 1 1 1 1 1 1 1
    /// ```
    pub outfield: u64,
    /// Masks used to examine the direction exerted by a bit on the margin.
    ///
    /// For details, see the implementation of
    /// the method [`target_bit_to_direction`](`BitMask::target_bit_to_direction`).
    ///
    /// # Examples
    ///
    /// ```text
    /// [0] W + NE + N + NW
    /// 0 0 0 0 0 0 0 0
    /// 0 1 1 1 1 1 1 0
    /// 0 1 0 0 0 0 0 0
    /// 0 1 0 0 0 0 0 0
    /// 0 1 0 0 0 0 0 0
    /// 0 1 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [1] SW + E + N + NW
    /// 0 0 0 0 0 0 0 0
    /// 0 1 1 1 1 1 0 0
    /// 0 0 0 0 0 0 1 0
    /// 0 0 0 0 0 0 1 0
    /// 0 0 0 0 0 0 1 0
    /// 0 0 0 0 0 0 1 0
    /// 0 1 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [2] S + E + NE + NW
    /// 0 0 0 0 0 0 0 0
    /// 0 1 0 0 0 0 1 0
    /// 0 0 0 0 0 0 1 0
    /// 0 0 0 0 0 0 1 0
    /// 0 0 0 0 0 0 1 0
    /// 0 0 0 0 0 0 1 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    pieces: [u64; 3],
    /// Masks used to determine if there are walls.
    ///
    /// For details, see the implementation of the method [`BitMask::calc_wall_bits`].
    ///
    /// # Examples
    ///
    /// ```text
    /// [0]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [1]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 1 0 0
    /// 0 0 0 0 0 1 0 0
    /// 0 0 0 0 0 1 0 0
    /// 0 0 0 0 0 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [2]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 0 0 0 0 0
    /// 0 0 1 0 0 0 0 0
    /// 0 0 1 0 0 0 0 0
    /// 0 0 1 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [3]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    edges: [u64; 4], // n, e, w, s
    /// Masks used to create walls.
    ///
    /// For details, see the implementation of the method [`calc_wall_bits`](`BitMask::calc_wall_bits`).
    ///
    /// # Examples
    ///
    /// ```text
    /// [0]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [1]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 1 0 0 0 0 1 0
    /// 0 1 0 0 0 0 1 0
    /// 0 1 0 0 0 0 1 0
    /// 0 1 0 0 0 0 1 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    walls: [u64; 2], // ns, ew
    /// # Examples
    ///
    /// ```text
    /// [0]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 1
    /// 1 1 1 0 0 0 0 1
    /// 1 1 1 0 0 0 0 1
    /// 1 1 1 0 0 0 0 1
    /// 1 1 1 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [1]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 1 1 1 1 0 0 0 0
    /// 1 1 1 1 0 0 0 0
    /// 1 1 1 1 0 0 0 0
    /// 1 1 1 1 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [2]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 1 1 1 1 0 0 0
    /// 0 1 1 1 1 0 0 0
    /// 0 1 1 1 1 0 0 0
    /// 0 1 1 1 1 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    for_hmin: [u64; 3],
    /// # Examples
    ///
    /// ```text
    /// [0]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 1 1 1
    /// 1 0 0 0 0 1 1 1
    /// 1 0 0 0 0 1 1 1
    /// 1 0 0 0 0 1 1 1
    /// 1 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [1]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 1 1 1 1
    /// 0 0 0 0 1 1 1 1
    /// 0 0 0 0 1 1 1 1
    /// 0 0 0 0 1 1 1 1
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [2]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 1 1 1 1 0
    /// 0 0 0 1 1 1 1 0
    /// 0 0 0 1 1 1 1 0
    /// 0 0 0 1 1 1 1 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    for_hmax: [u64; 3],
    /// # Examples
    ///
    /// ```text
    /// [0]
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    ///
    /// [1]
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [2]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    for_vmin: [u64; 3],
    /// # Examples
    ///
    /// ```text
    /// [0]
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    ///
    /// [1]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    ///
    /// [2]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    for_vmax: [u64; 3],
    /// # Examples
    ///
    /// ```text
    /// [0]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [1]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 1 1 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [2]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 1 0 0 0 0
    /// 0 0 1 1 0 0 0 0
    /// 0 0 1 1 0 0 0 0
    /// 0 0 1 1 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    ///
    /// [3]
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 1 0 1 0 0 0
    /// 0 0 1 0 1 0 0 0
    /// 0 0 1 0 1 0 0 0
    /// 0 0 1 0 1 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// 0 0 0 0 0 0 0 0
    /// ```
    for_idx: [u64; 4],
}

impl BitMask {
    const fn new() -> Self {
        let core = 0x000000000f0f0f0f_u64;
        let outfield = 0x607fffe060606060_u64;
        let pieces = [
            0x0000001f90101010_u64,
            0x9000001f00808080_u64,
            0x8f00001080808080_u64,
        ];
        let edges = [
            0x000000000f000000_u64, // n
            0x0000000001010101_u64, // e
            0x0000000008080808_u64, // w
            0x000000000000000f_u64, // s
        ];
        let walls = [
            0x0f00000f00000000_u64, // ns
            0x8000000010909090_u64, // ew
        ];

        let for_hmin = array![n => core.rotate_left(3 - n as u32); 3];
        let for_hmax = array![n => core.rotate_right(3 - n as u32); 3];
        let for_vmin = array![n => core.rotate_left(8 * (3 - n) as u32); 3];
        let for_vmax = array![n => core.rotate_right(8 * (3 - n) as u32); 3];

        let for_idx = [
            0x0f0f0000_u64,
            0x0f000f00_u64,
            0x0c0c0c0c_u64,
            0x0a0a0a0a_u64,
        ];

        Self {
            core,
            outfield,
            pieces,
            edges,
            walls,
            for_hmin,
            for_hmax,
            for_vmin,
            for_vmax,
            for_idx,
        }
    }

    /// Returns [`BitMask`] created by applying [`u64::rotate_left`] to all bit-masks of `self`.
    const fn rotate_left_all(&self, n: u32) -> Self {
        const fn _make_rotated<const N: usize>(base: &[u64; N], n: u32) -> [u64; N] {
            let mut ret = [0; N];
            macros::for_loop!(let mut i = 0; i < N; i += 1 => {
                ret[i] = base[i].rotate_left(n);
            });
            ret
        }

        macro_rules! define_rotated {
            ($($field:ident),*) => {
                $(let $field = _make_rotated(&self.$field, n);)*
            };
        }

        let core = self.core.rotate_left(n);
        let outfield = self.outfield.rotate_left(n);
        define_rotated!(pieces, edges, walls, for_hmin, for_hmax, for_vmin, for_vmax, for_idx);

        Self {
            core,
            outfield,
            pieces,
            edges,
            walls,
            for_hmin,
            for_hmax,
            for_vmin,
            for_vmax,
            for_idx,
        }
    }

    /// Returns `bit`'s index value, which is assigned to each square
    /// in 4x4 main region.
    ///
    /// Index values are as below:
    /// ```text
    /// +----+----+----+----+
    /// | 15 | 14 | 13 | 12 |
    /// +----+----+----+----+
    /// | 11 | 10 |  9 |  8 |
    /// +----+----+----+----+
    /// |  7 |  6 |  5 |  4 |
    /// +----+----+----+----+
    /// |  3 |  2 |  1 |  0 |
    /// +----+----+----+----+
    /// ```
    /// If `bit` is out of main region, it returns `None`.
    pub fn field_idx(&self, bit: u64) -> Option<usize> {
        if bit & self.core != bit {
            return None;
        }
        let mut idx = 0_usize;
        for mask in self.for_idx {
            idx <<= 1;
            if bit & mask == bit {
                idx += 1;
            }
        }
        Some(idx)
    }

    /// Infer the direction on which the [`MaskViewer`] has to shift
    /// based on the bit of next position. If viewer should not shift,
    /// it returns `Ok(None)`.
    ///
    /// The structure of elements of `pieces` of [`BitMask`] is as follows:
    /// ```text
    /// [0] W + NE + N + NW
    /// [1] SW + E + N + NW
    /// [2] S + E + NE + NW
    /// ```
    /// The information that the specified `bit` intersects with which element
    /// determines which direction `bit` belongs to.
    /// Specifically, the association below works:
    /// ```text
    ///         [0][1][2]
    /// SE -> 0b 0  0  0
    /// S  -> 0b 0  0  1
    /// SW -> 0b 0  1  0
    /// E  -> 0b 0  1  1
    /// W  -> 0b 1  0  0
    /// NE -> 0b 1  0  1
    /// N  -> 0b 1  1  0
    /// NW -> 0b 1  1  1
    /// ```
    fn target_bit_to_direction(
        &self,
        target_bit: u64,
    ) -> Result<Option<Direction>, error::DirectionError> {
        if target_bit & self.core == target_bit {
            return Ok(None);
        }
        if target_bit & self.outfield == target_bit {
            return Err(error::DirectionError::BitOutOfField(target_bit));
        }

        let mut idx = 0_usize;
        for piece in self.pieces {
            idx <<= 1;
            if target_bit & piece == target_bit {
                idx += 1;
            }
        }

        let direction = Direction::try_from(idx)?;
        Ok(Some(direction))
    }

    /// Calculates bits of walls based on `bits`.
    pub fn calc_wall_bits(&self, bits: u64) -> u64 {
        let mut wall_bits = 0;

        // H-wall
        if (bits & self.edges[0] != 0) && (bits & self.edges[3] != 0) {
            wall_bits |= self.walls[0];
        }

        // V-wall
        if (bits & self.edges[1] != 0) && (bits & self.edges[2] != 0) {
            wall_bits |= self.walls[1];
        }

        wall_bits
    }

    /// Calculates the minimum rectangle that contains `bits`.
    ///
    /// See the documents given to [`super::Board::minimum_rectangle`].
    pub fn minimum_rectangle(&self, bits: u64) -> Rectangle {
        macro_rules! calc_edge {
            ($field:ident, $default:expr, $i:ident, $eval:expr) => {{
                let mut val = $default;
                for ($i, m) in self.$field.into_iter().enumerate() {
                    if bits & m == bits {
                        val = $eval;
                        break;
                    }
                }
                val
            }};
        }

        let hmin = calc_edge!(for_hmin, 0, i, 3 - i);
        let hmax = calc_edge!(for_hmax, 3, i, i);
        let vmin = calc_edge!(for_vmin, 0, i, 3 - i);
        let vmax = calc_edge!(for_vmax, 3, i, i);

        Rectangle {
            hmin,
            hmax,
            vmin,
            vmax,
        }
    }
}

/// A rectangle with edges of [`usize`] coordinates.
///
/// A rectangle is created by
/// the [`minimum_rectangle`](`crate::Board::minimum_rectangle`) method,
/// which calculates the minimum rectangle that contains
/// all doves on the board.
/// The fields of this struct stand for coordinates of edges.
///
/// # Examples
/// ```text
/// [Case 1]
///     0   1   2   3
///   +---+---+---+---+
/// 0 | b |   | T |   |
///   +---+---+---+---+
/// 1 |   | B | a |   |
///   +---+---+---+---+
/// 2 |   |   |   |   |
///   +---+---+---+---+
/// 3 |   |   |   |   |
///   +---+---+---+---+
/// -> hmin=0, hmax=2, vmin=0, vmax=1
///
/// [Case 2]
///     0   1   2   3
///   +---+---+---+---+
/// 0 |   |   |   |   |
///   +---+---+---+---+
/// 1 |   | b |   |   |
///   +---+---+---+---+
/// 2 | a |   | H | B |
///   +---+---+---+---+
/// 3 |   |   |   |   |
///   +---+---+---+---+
/// -> hmin=0, hmax=3, vmin=1, vmax=2
///
/// [Case 3]
///     0   1   2   3
///   +---+---+---+---+
/// 0 |   |   |   |   |
///   +---+---+---+---+
/// 1 |   |   |   |   |
///   +---+---+---+---+
/// 2 |   |   |   |   |
///   +---+---+---+---+
/// 3 | h | B | b | Y |
///   +---+---+---+---+
/// -> hmin=0, hmax=3, vmin=3, vmax=3
/// ```
/// ```rust
/// use std::str::FromStr;
/// use tokyodoves::{BoardBuilder, Rectangle};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let boards = vec![
///     BoardBuilder::from_str("b T; Ba")?.build()?,
///     BoardBuilder::from_str("; b;a HB")?.build()?,
///     BoardBuilder::from_str(";;;hBbY")?.build()?,
/// ];
/// assert_eq!(
///     boards[0].minimum_rectangle(),
///     Rectangle { hmin: 0, hmax: 2, vmin: 0, vmax: 1 }
/// );
/// assert_eq!(
///     boards[1].minimum_rectangle(),
///     Rectangle { hmin: 0, hmax: 3, vmin: 1, vmax: 2 }
/// );
/// assert_eq!(
///     boards[2].minimum_rectangle(),
///     Rectangle { hmin: 0, hmax: 3, vmin: 3, vmax: 3 }
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rectangle {
    /// The minimum coordinate in horizontal direction
    pub hmin: usize,
    /// The maximum coordinate in horizontal direction
    pub hmax: usize,
    /// The minimum coordinate in vertical direction
    pub vmin: usize,
    /// The maximum coordinate in vertical direction
    pub vmax: usize,
}

/// A struct to represent the lengths of edges of [`Rectangle`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RectangleSize {
    /// The length of the vertical edge
    pub vsize: usize,
    /// The length of the horizontal edge
    pub hsize: usize,
}

impl Rectangle {
    /// Calculates the lengths of horizontal and vertical edges.
    pub fn size(&self) -> RectangleSize {
        let hsize = self.hmax - self.hmin + 1;
        let vsize = self.vmax - self.vmin + 1;
        RectangleSize { hsize, vsize }
    }
}

#[derive(Debug, Clone, Copy, EnumIter)]
pub(crate) enum Direction {
    /// South-East
    SE,
    /// South
    S,
    /// South-West
    SW,
    /// East
    E,
    /// West
    W,
    /// North-East
    NE,
    /// North
    N,
    /// North-West
    NW,
}

impl TryFrom<usize> for Direction {
    type Error = error::DirectionError;

    fn try_from(value: usize) -> Result<Self, Self::Error> {
        use Direction::*;
        let direction = match value {
            0 => SE,
            1 => S,
            2 => SW,
            3 => E,
            4 => W,
            5 => NE,
            6 => N,
            7 => NW,
            _ => return Err(error::DirectionError::IndexOutOfRange(value)),
        };
        Ok(direction)
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub struct MaskViewer {
    status: usize,
}

impl MaskViewer {
    pub fn new() -> Self {
        Self { status: 0 }
    }

    pub fn view_mask(&self) -> &BitMask {
        // safety is guaranteed because self.status can be changed only via do_shift
        unsafe { MASKS.get_unchecked(self.status) }
    }

    pub(crate) fn view_next_mask(&self, d: Direction) -> &BitMask {
        // safety is guaranteed because the turned value of Self::shift_status ranges from 0 to 63
        let next_status = Self::shift_status(self.status, d);
        unsafe { MASKS.get_unchecked(next_status) }
    }

    /// Get a (reference to) [`BitMask`] that contains `bit`
    /// without changing internal index (differently from [`shift_toward`](`Self::shift_toward`)).
    pub(crate) fn view_mask_at(&self, bit: u64) -> Result<&BitMask, error::DirectionError> {
        match self.view_mask().target_bit_to_direction(bit)? {
            Some(d) => Ok(self.view_next_mask(d)),
            None => Ok(self.view_mask()),
        }
    }

    /// Changes the reference to constant [`BitMask`] so that
    /// the `core` of new [`BitMask`] contains the specified `bit`.
    pub(crate) fn shift_toward(&mut self, bit: u64) -> Result<(), error::DirectionError> {
        if let Some(d) = self.view_mask().target_bit_to_direction(bit)? {
            self.do_shift(d);
        }
        Ok(())
    }

    fn do_shift(&mut self, d: Direction) {
        self.status = Self::shift_status(self.status, d);
    }

    fn shift_status(status: usize, d: Direction) -> usize {
        use Direction::*;
        let x = match d {
            SE => 55,
            S => 56,
            SW => 57,
            E => 63,
            W => 1,
            NE => 7,
            N => 8,
            NW => 9,
        };
        (status + x) % 64
    }
}