bitperm 0.1.0

Bit permutations and bit-packed polycube/grid structures in Rust
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
use std::fmt;
// use std::ops::*;
use std::collections::HashSet;
use std::iter::FromIterator;

use arrayvec::*;
use derive_more::*;

use flowscad::*;

use crate::bitlib::*;

// use itertools::Itertools;

// -----------------------------------------------------------------
// 4x4x4 cube space represented by the 64 bits in a u64
// -----------------------------------------------------------------
// Position at (x,y,z) = x + 4*y + 16*z
// Rotations will happen from the center of the cube
//
// The operators >> and << implement unbounded_shr() and unbounded_shl(),
// so they may be used safely.

#[derive(
    Copy,
    Clone,
    Eq,
    PartialEq,
    Hash,
    PartialOrd,
    Ord,
    BitAnd,
    BitAndAssign,
    BitOr,
    BitOrAssign,
    BitXor,
    BitXorAssign,
)]
pub struct BitCube4(pub u64);

/// Let BitCube4 use >> operator in the safe manner of unbounded_shr()
impl core::ops::Shr<u32> for BitCube4 {
    type Output = Self;

    fn shr(self, shift: u32) -> Self {
        Self(self.unbounded_shr(shift))
    }
}

/// Let BitCube4 use << operator in the safe manner of unbounded_shl()
impl core::ops::Shl<u32> for BitCube4 {
    type Output = Self;

    fn shl(self, shift: u32) -> Self {
        Self(self.unbounded_shl(shift))
    }
}

/// Define BitAnd with u64 for BitCube4
impl core::ops::BitAnd<u64> for BitCube4 {
    type Output = Self;

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

/// Define BitOr with u64 for BitCube4
impl core::ops::BitOr<u64> for BitCube4 {
    type Output = Self;

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

impl core::ops::Deref for BitCube4 {
    type Target = u64;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

pub struct BitCube4Rotations(pub ArrayVec<BitCube4, 24>);

impl From<u64> for BitCube4 {
    fn from(x: u64) -> BitCube4 {
        BitCube4(x)
    }
}
/*
/// Embedding a BitCube3 into a BitCube4 requires moving the three 3x3
/// planes into the first three 4x4 planes, while adding a bit to extend
/// the row length from 3 to 4.
impl From<BitCube3> for BitCube4 {
    fn from(bc3: BitCube3) -> BitCube4 {
        let mut x = bc3.0 as u64;
        // Shift the plane start index from 0,9,18 to 0,16,32
        x = (x & 0o777) ^ ((x & 0o777000) << 7) ^ ((x & 0o777000000) << 14);
        // Shift the row start index from 0,3,6 to 0,4,8
        x = (x & 0x700070007) ^ ((x & 0x3800380038) << 1) ^ ((x & 0x01c001c001c0) << 2);
        BitCube4(x)
    }
}
*/

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

impl From<BitCube4> for D3 {
    fn from(val: BitCube4) -> Self {
        let block = D3::cube(1.0);
        (0..64)
            .filter(|ii| (val.0 >> ii) & 1 == 1)
            .map(|ii| v3(ii & 0x3, (ii >> 2) & 0x3, ii >> 4))
            .map(|xyz| block.clone().translate(xyz))
            .union()
            .translate(v3(-2, -2, -2))
            .scale(10)
            .color(ColorEnum::Red)
    }
}

impl BitCube4 {
    /// Count the number of cubes (ones) in the BitCube
    pub fn count_cubes(self) -> u32 {
        self.0.count_ones()
    }

    /// Produce all rotations of a BitCube
    /// Prefer a gray code path through all rotations
    pub fn rotate_all_set(self) -> HashSet<BitCube4> {
        let mut vec = Vec::new();
        let mut x = self;
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_y();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d(); // One wasted move. There is probably a gray code path that is shorter.
        x = x.rotate_y();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        HashSet::from_iter(vec.iter().cloned())
    }

    /// Produce all rotations of a BitCube
    /// Prefer a gray code path through all rotations
    pub fn rotate_all_vec(self) -> ArrayVec<BitCube4, 24> {
        let mut vec = ArrayVec::<BitCube4, 24>::new();
        let mut x = self;
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_y();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        x = x.rotate_d(); // One wasted move. There is probably a gray code path that is shorter.
        x = x.rotate_y();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z();
            vec.push(x);
        }
        vec.sort_unstable();
        let symmetries = vec.partition_dedup().0.len(); // Move duplicates to the end.
        vec.truncate(symmetries);
        vec
    }

    /// Produce all rotations of a BitCube object translated towards origin.
    /// Prefer a gray code path through all rotations
    pub fn origin_rotate_all(self) -> ArrayVec<BitCube4, 24> {
        let mut vec = ArrayVec::<BitCube4, 24>::new();
        let mut x = self.shift_to_origin();
        vec.push(x);
        for _ in 0..3 {
            for _ in 0..3 {
                x = x.rotate_z().shift_to_origin();
                vec.push(x);
            }
            x = x.rotate_d().shift_to_origin();
            vec.push(x);
        }
        for _ in 0..3 {
            x = x.rotate_z().shift_to_origin();
            vec.push(x);
        }
        x = x.rotate_y().shift_to_origin();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z().shift_to_origin();
            vec.push(x);
        }
        x = x.rotate_d(); // One wasted move. There is probably a gray code path that is shorter.
        x = x.rotate_y().shift_to_origin();
        vec.push(x);
        for _ in 0..3 {
            x = x.rotate_z().shift_to_origin();
            vec.push(x);
        }
        vec.sort_unstable();
        let symmetries = vec.partition_dedup().0.len(); // Move duplicates to the end.
        vec.truncate(symmetries);
        vec
    }

    // 1100
    // 0100
    // 1000
    // 0000
    /// 2x2x2 Example: 01 23 | 45 67 => 45 01 | 67 23
    pub fn rotate_x(self) -> Self {
        let mut cube = self.0;
        // Swap 2x2 blocks front <-> back
        swap_mask_shift_u64(&mut cube, 0x00ff_00ff_00ff_00ff_u64, 8);
        // Swap diagonal 2x2 blocks
        swap_mask_shift_u64(&mut cube, 0x0000_0000_00ff_00ff_u64, 40);
        // Within 2x2 blocks swap front <-> back
        swap_mask_shift_u64(&mut cube, 0x0f0f_0f0f_0f0f_0f0f_u64, 4);
        // Within 2x2 blocks swap diagonal entries
        swap_mask_shift_u64(&mut cube, 0x0000_0f0f_0000_0f0f_u64, 20);
        BitCube4(cube)
    }

    /// 2x2x2 Example: 01 23 | 45 67 => 15 37 | 04 26
    /// 2x2x2 Example: 01 23 | 45 67 => 45 67 | 01 23
    pub fn rotate_y(self) -> Self {
        let mut cube = self.0;
        // Swap 2x2 blocks up <-> down
        swap_mask_shift_u64(&mut cube, 0x0000_0000_ffff_ffff_u64, 32);
        // Swap diagonal 2x2 blocks
        swap_mask_shift_u64(&mut cube, 0x0000_0000_3333_3333_u64, 34);
        // Within 2x2 blocks swap up <-> down
        swap_mask_shift_u64(&mut cube, 0x0000_ffff_0000_ffff_u64, 16);
        // Within 2x2 blocks swap diagonal entries
        swap_mask_shift_u64(&mut cube, 0x0000_5555_0000_5555_u64, 17);
        BitCube4(cube)
    }

    /// 2x2x2 Example: 01 23 | 45 67 => 20 31 | 64 75
    /// The z-rotation is the easiest to understand since the rotation happens in the xy-plane and
    /// is copied in the other dimension.
    /// 23 32 31
    /// 01 10 20
    pub fn rotate_z(self) -> Self {
        let mut cube = self.0;
        // Swap 2x2 squares left <-> right
        swap_mask_shift_u64(&mut cube, 0x3333_3333_3333_3333_u64, 2);
        // Swap diagonal 2x2 squares
        swap_mask_shift_u64(&mut cube, 0x0033_0033_0033_0033_u64, 10);
        // Within 2x2 squares swap left <-> right columns
        swap_mask_shift_u64(&mut cube, 0x5555_5555_5555_5555_u64, 1);
        // Within 2x2 squares swap diagonal entries
        swap_mask_shift_u64(&mut cube, 0x0505_0505_0505_0505_u64, 5);
        BitCube4(cube)
    }

    /// Rotate 120 degrees about the diagonal through the origin and center of the cube.
    /// 2x2x2 Example: 01 23 | 45 67 => 04 15 | 26 37
    /// Use two position involutions: 2 <-> 4 and 3 <-> 5, then 1 <-> 2 and 5 <-> 6.
    /// For the 4-cube, rotate the position of all the 2-cubes,
    /// then rotate all the 2-cubes in place.
    pub fn rotate_d(self) -> Self {
        let mut cube = self.0;
        // Swap sub-cubes 2 <-> 4 and 3 <-> 5
        swap_mask_shift_u64(&mut cube, 0x0000_0000_ff00_ff00_u64, 24);
        // Swap sub-cubes 1 <-> 2 and 5 <-> 6
        swap_mask_shift_u64(&mut cube, 0x00cc_00cc_00cc_00cc_u64, 6);
        // Swap 2 <-> 4 and 3 <-> 5 in each sub-cube
        swap_mask_shift_u64(&mut cube, 0x0000_f0f0_0000_f0f0_u64, 12);
        // Swap 1 <-> 2 and 5 <-> 6 in each sub-cube
        swap_mask_shift_u64(&mut cube, 0x0a0a_0a0a_0a0a_0a0a_u64, 3);
        BitCube4(cube)
    }

    pub fn shift_x(self, shift: i8) -> Self {
        match shift {
            0 => self,
            1 => (self << 1) & 0xeeee_eeee_eeee_eeee_u64,
            2 => Self((self.0.unbounded_shl(2)) & 0xdddd_dddd_dddd_dddd_u64),
            3 => Self((self.0.unbounded_shl(3)) & 0x8888_8888_8888_8888_u64),
            -1 => Self((self.0.unbounded_shr(1)) & 0x7777_7777_7777_7777_u64),
            -2 => Self((self.0.unbounded_shr(2)) & 0x3333_3333_3333_3333_u64),
            -3 => Self((self.0.unbounded_shr(3)) & 0x1111_1111_1111_1111_u64),
            _ => Self(0),
        }
    }

    /// Return none if the x-shift would move part of the polycube past an edge.
    pub fn bounded_shift_x(self, shift: i8) -> Option<Self> {
        let shifted = self.shift_x(shift);
        if self.count_cubes() == shifted.count_cubes() {
            Some(shifted)
        } else {
            None
        }
    }

    pub fn shift_y(self, shift: i8) -> Self {
        match shift {
            0 => self,
            1 => Self((self.0 << 4) & 0xfff0_fff0_fff0_fff0_u64),
            2 => Self((self.0 << 8) & 0xff00_ff00_ff00_ff00_u64),
            3 => Self((self.0 << 12) & 0xf000_f000_f000_f000_u64),
            -1 => Self((self.0 >> 4) & 0x0fff_0fff_0fff_0fff_u64),
            -2 => Self((self.0 >> 8) & 0x00ff_00ff_00ff_00ff_u64),
            -3 => Self((self.0 >> 12) & 0x000f_000f_000f_000f_u64),
            _ => Self(0),
        }
    }

    /// Return none if the y-shift would move part of the polycube past an edge.
    pub fn bounded_shift_y(self, shift: i8) -> Option<Self> {
        let shifted = self.shift_y(shift);
        if self.count_cubes() == shifted.count_cubes() {
            Some(shifted)
        } else {
            None
        }
    }

    pub fn shift_z(self, shift: i8) -> Self {
        match shift {
            0 => self,
            1 => Self((self.0 << 16) & 0xffff_ffff_ffff_0000_u64),
            2 => Self((self.0 << 32) & 0xffff_ffff_0000_0000_u64),
            3 => Self((self.0 << 48) & 0xffff_0000_0000_0000_u64),
            -1 => Self((self.0 >> 16) & 0x0000_ffff_ffff_ffff_u64),
            -2 => Self((self.0 >> 32) & 0x0000_0000_ffff_ffff_u64),
            -3 => Self((self.0 >> 48) & 0x0000_0000_0000_ffff_u64),
            _ => Self(0),
        }
    }

    /// Return none if the z-shift would move part of the polycube past an edge.
    pub fn bounded_shift_z(self, shift: i8) -> Option<Self> {
        let shifted = self.shift_z(shift);
        if self.count_cubes() == shifted.count_cubes() {
            Some(shifted)
        } else {
            None
        }
    }

    /// Given a piece in the 4-cube, shift it towards the origin so that it touches the x, y, and z
    /// planes
    pub fn shift_to_origin(self) -> Self {
        let mut shape = self.0;
        let z_shift = (shape.trailing_zeros() / 16) * 16;
        shape = shape.unbounded_shr(z_shift);
        let xy_proj = shape | shape.unbounded_shr(32);
        let xy_proj = xy_proj | xy_proj.unbounded_shr(16);
        let y_shift = (xy_proj.trailing_zeros() / 4) * 4;
        shape = shape.unbounded_shr(y_shift);
        let x_shift = xy_proj | xy_proj.unbounded_shr(8);
        let x_shift = x_shift | x_shift.unbounded_shr(4);
        shape = shape.unbounded_shr(x_shift.trailing_zeros());
        Self(shape)
    }

    pub fn overlap(self, other: Self) -> bool {
        self.0 & other.0 != 0
    }
}

/*
impl BitOr for BitCube4 {
    type Output = Self;

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

impl BitOrAssign for BitCube4 {
    // rhs is the "right-hand side" of the expression `a |= b`
    fn bitor_assign(&mut self, rhs: Self) {
        *self = Self(self.0 | rhs.0)
    }
}


impl BitAnd for BitCube4 {
    type Output = Self;

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

impl BitAndAssign for BitCube4 {
    // rhs is the "right-hand side" of the expression `a &= b`
    fn bitand_assign(&mut self, rhs: Self) {
        *self = Self(self.0 & rhs.0)
    }
}

impl BitAnd<u64> for BitCube4 {
    type Output = Self;

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

// impl fmt::Debug for BitCube4 {
// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// write!(f, "BitCube4({:#018x})\n{:}", self.0, self)
// }
// }

impl fmt::Debug for BitCube4 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "BitCube4({:#018x})", self.0)
    }
}

impl fmt::Display for BitCube4 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            (0..4)
                .map(|x| {
                    let x = self.0 >> (4 * (3 - x));
                    (0..4)
                        .map(|y| format!("{:04b}", (x >> (16 * y)) & 0xf))
                        .map(|s| s.chars().rev().collect())
                        .collect::<Vec<String>>()
                        .join(" ")
                })
                .collect::<Vec<String>>()
                .join("\n")
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::bitcube3::BitCube3;

    #[test]
    fn test_debug() {
        assert_eq!(
            format!("{:?}", BitCube4::from(ORDER)),
            "BitCube4(0xfedcba9876543210)"
        );
    }

    #[test]
    fn test_display() {
        assert_eq!(
            format!("{:}", BitCube4::from(ORDER)),
            "1100 1110 1101 1111\n0100 0110 0101 0111\n1000 1010 1001 1011\n0000 0010 0001 0011"
        );
    }

    #[test]
    fn test_bitor() {
        assert_eq!(
            BitCube4::from(FULL) | BitCube4::from(FULL),
            BitCube4::from(FULL)
        );
        assert_eq!(
            BitCube4::from(ALL) | BitCube4::from(ALL),
            BitCube4::from(ALL)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_X)
                | BitCube4::from(BC4_CENTER_Y)
                | BitCube4::from(BC4_CENTER_Z),
            BitCube4::from(BC4_CENTER_ALL)
        );
    }

    #[test]
    fn test_shift_x() {
        assert_eq!(
            BitCube4::from(FULL).shift_x(1),
            BitCube4(0xeeee_eeee_eeee_eeee_u64)
        );
    }

    #[test]
    fn test_shift_y() {
        assert_eq!(
            BitCube4::from(FULL).shift_y(1),
            BitCube4(0xfff0_fff0_fff0_fff0_u64)
        );
    }

    #[test]
    fn test_shift_z() {
        assert_eq!(
            BitCube4::from(FULL).shift_z(1),
            BitCube4(0xffff_ffff_ffff_0000_u64)
        );
    }

    #[test]
    fn test_bounded_shift_x() {
        assert_eq!(BitCube4::from(FULL).bounded_shift_x(1), None);
        assert_eq!(BitCube4::from(BC4_CENTER_X).bounded_shift_x(1), None);
        assert_eq!(
            BitCube4::from(BC4_CENTER_Y).bounded_shift_x(1),
            Some(BitCube4(0x0000cccccccc0000))
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Z).bounded_shift_x(1),
            Some(BitCube4(0x0cc00cc00cc00cc0))
        );
    }

    #[test]
    fn test_bounded_shift_y() {
        assert_eq!(BitCube4::from(FULL).bounded_shift_y(1), None);
        assert_eq!(
            BitCube4::from(BC4_CENTER_X).bounded_shift_y(1),
            Some(BitCube4(0x0000ff00ff000000))
        );
        assert_eq!(BitCube4::from(BC4_CENTER_Y).bounded_shift_y(1), None);
        assert_eq!(
            BitCube4::from(BC4_CENTER_Z).bounded_shift_y(1),
            Some(BitCube4(0x6600660066006600))
        );
    }

    #[test]
    fn test_bounded_shift_z() {
        assert_eq!(BitCube4::from(FULL).bounded_shift_z(1), None);
        assert_eq!(
            BitCube4::from(BC4_CENTER_X).bounded_shift_z(1),
            Some(BitCube4(0x0ff00ff000000000))
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Y).bounded_shift_z(1),
            Some(BitCube4(0x6666666600000000))
        );
        assert_eq!(BitCube4::from(BC4_CENTER_Z).bounded_shift_z(1), None);
    }

    #[test]
    fn test_shift_to_origin() {
        assert_eq!(BitCube4::from(FULL).shift_to_origin(), BitCube4::from(FULL));
        assert_eq!(
            (BitCube4::from(BC4_CENTER_X) | BitCube4::from(BC4_CENTER_Y)).shift_to_origin(),
            BitCube4(0x0000_0000_6ff6_6ff6)
        );
    }

    #[test]
    fn test_rotate_x() {
        assert_eq!(BitCube4::from(FULL).rotate_x(), BitCube4::from(FULL));
        assert_eq!(
            BitCube4::from(BC4_CENTER_X).rotate_x(),
            BitCube4::from(BC4_CENTER_X)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Y).rotate_x(),
            BitCube4::from(BC4_CENTER_Z)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Z).rotate_x(),
            BitCube4::from(BC4_CENTER_Y)
        );
        assert_eq!(BitCube4(0xf).rotate_x(), BitCube4(0xf000));
        assert_eq!(BitCube4(0xf000).rotate_x(), BitCube4(0xf000000000000000));
    }

    #[test]
    fn test_rotate_y() {
        assert_eq!(BitCube4::from(FULL).rotate_y(), BitCube4::from(FULL));
        assert_eq!(
            BitCube4::from(UPPER_RIGHT_2X4X2).rotate_y(),
            BitCube4::from(LOWER_RIGHT_2X4X2)
        );
        assert_eq!(
            BitCube4::from(LOWER_RIGHT_2X4X2).rotate_y(),
            BitCube4::from(LOWER_LEFT_2X4X2)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_X).rotate_y(),
            BitCube4::from(BC4_CENTER_Z)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Y).rotate_y(),
            BitCube4::from(BC4_CENTER_Y)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Z).rotate_y(),
            BitCube4::from(BC4_CENTER_X)
        );
        assert_eq!(BitCube4(0xf).rotate_y(), BitCube4(0x0001000100010001));
    }

    #[test]
    fn test_rotate_z() {
        assert_eq!(BitCube4::from(FULL).rotate_z(), BitCube4::from(FULL));
        assert_eq!(
            BitCube4::from(BC4_CENTER_X).rotate_z(),
            BitCube4::from(BC4_CENTER_Y)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Y).rotate_z(),
            BitCube4::from(BC4_CENTER_X)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Z).rotate_z(),
            BitCube4::from(BC4_CENTER_Z)
        );
        assert_eq!(BitCube4(0xf).rotate_z(), BitCube4(0x8888));
    }

    #[test]
    fn test_rotate_d() {
        assert_eq!(BitCube4::from(FULL).rotate_d(), BitCube4::from(FULL));
        assert_eq!(
            BitCube4::from(SUBCUBE_0).rotate_d(),
            BitCube4::from(SUBCUBE_0)
        );
        assert_eq!(
            BitCube4::from(SUBCUBE_1).rotate_d(),
            BitCube4::from(SUBCUBE_2)
        );
        assert_eq!(
            BitCube4::from(SUBCUBE_2).rotate_d(),
            BitCube4::from(SUBCUBE_4)
        );
        assert_eq!(
            BitCube4::from(SUBCUBE_4).rotate_d(),
            BitCube4::from(SUBCUBE_1)
        );
        assert_eq!(
            BitCube4::from(SUBCUBE_3).rotate_d(),
            BitCube4::from(SUBCUBE_6)
        );
        assert_eq!(
            BitCube4::from(SUBCUBE_6).rotate_d(),
            BitCube4::from(SUBCUBE_5)
        );
        assert_eq!(
            BitCube4::from(SUBCUBE_5).rotate_d(),
            BitCube4::from(SUBCUBE_3)
        );
        assert_eq!(
            BitCube4::from(SUBCUBE_7).rotate_d(),
            BitCube4::from(SUBCUBE_7)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_X).rotate_d(),
            BitCube4::from(BC4_CENTER_Y)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Y).rotate_d(),
            BitCube4::from(BC4_CENTER_Z)
        );
        assert_eq!(
            BitCube4::from(BC4_CENTER_Z).rotate_d(),
            BitCube4::from(BC4_CENTER_X)
        );
        assert_eq!(BitCube4(0x1011f).rotate_d(), BitCube4(0x0000000100011113));
    }

    #[test]
    fn test_rotation_order_axes() {
        let shapes = [
            BitCube4::from(FULL),
            BitCube4::from(BC4_CENTER_X),
            BitCube4::from(BC4_CENTER_Y),
            BitCube4::from(BC4_CENTER_Z),
            BitCube4(0x3),
        ];

        for &shape in &shapes {
            assert_eq!(shape.rotate_x().rotate_x().rotate_x().rotate_x(), shape);
            assert_eq!(shape.rotate_y().rotate_y().rotate_y().rotate_y(), shape);
            assert_eq!(shape.rotate_z().rotate_z().rotate_z().rotate_z(), shape);
        }
    }

    #[test]
    fn test_rotation_order_diagonal() {
        let shapes = [
            BitCube4::from(FULL),
            BitCube4::from(BC4_CENTER_X),
            BitCube4::from(BC4_CENTER_Y),
            BitCube4::from(BC4_CENTER_Z),
            BitCube4::from(SUBCUBE_0),
            BitCube4(0x3),
        ];

        for &shape in &shapes {
            let r1 = shape.rotate_d();
            let r2 = r1.rotate_d();
            let r3 = r2.rotate_d();
            assert_eq!(r3, shape);
        }
    }

    #[test]
    fn test_overlap() {
        assert!(BitCube4::from(BC4_CENTER_X).overlap(BitCube4::from(BC4_CENTER_Y)));
    }

    #[test]
    fn test_from_bitperm3() {
        assert_eq!(
            BitCube4::from(BitCube3(0o777777777)),
            BitCube4(0x77707770777)
        );
        assert_eq!(
            BitCube4::from(BitCube3(0o700000000)),
            BitCube4(0x70000000000)
        );
        assert_eq!(BitCube4::from(BitCube3(0o76543210)), BitCube4(0x7605430210));
    }

    #[test]
    fn test_rotate_all_set() {
        assert_eq!(
            BitCube4::rotate_all_set(BitCube4::from(BC4_CENTER_ALL)),
            HashSet::from([BitCube4::from(BC4_CENTER_ALL)])
        );
        assert_eq!(
            BitCube4::rotate_all_set(BitCube4::from(BC4_CENTER_X)),
            HashSet::from([
                BitCube4::from(BC4_CENTER_X),
                BitCube4::from(BC4_CENTER_Y),
                BitCube4::from(BC4_CENTER_Z)
            ])
        );
        assert_eq!(BitCube4::rotate_all_set(BitCube4::from(SUBCUBE_0)).len(), 8);
        assert_eq!(
            BitCube4::rotate_all_set(BitCube4::from(SUBCUBE_0)),
            HashSet::from([
                BitCube4::from(SUBCUBE_0),
                BitCube4::from(SUBCUBE_1),
                BitCube4::from(SUBCUBE_2),
                BitCube4::from(SUBCUBE_3),
                BitCube4::from(SUBCUBE_4),
                BitCube4::from(SUBCUBE_5),
                BitCube4::from(SUBCUBE_6),
                BitCube4::from(SUBCUBE_7)
            ])
        );
        assert_eq!(BitCube4::rotate_all_set(BitCube4(0x3)).len(), 24);
    }

    #[test]
    fn test_rotate_all_vec() {
        assert_eq!(
            BitCube4::rotate_all_vec(BitCube4::from(BC4_CENTER_ALL)).as_slice(),
            &[BitCube4::from(BC4_CENTER_ALL)]
        );
        assert_eq!(
            BitCube4::rotate_all_vec(BitCube4::from(BC4_CENTER_X)).as_slice(),
            &[
                BitCube4::from(BC4_CENTER_X),
                BitCube4::from(BC4_CENTER_Y),
                BitCube4::from(BC4_CENTER_Z)
            ]
        );
        assert_eq!(BitCube4::rotate_all_vec(BitCube4::from(SUBCUBE_0)).len(), 8);
        assert_eq!(
            BitCube4::rotate_all_vec(BitCube4::from(SUBCUBE_0)).as_slice(),
            &[
                BitCube4(0x0000000000330033),
                BitCube4(0x0000000000cc00cc),
                BitCube4(0x0000000033003300),
                BitCube4(0x00000000cc00cc00),
                BitCube4(0x0033003300000000),
                BitCube4(0x00cc00cc00000000),
                BitCube4(0x3300330000000000),
                BitCube4(0xcc00cc0000000000)
            ]
        );
        assert_eq!(
            BitCube4::rotate_all_vec(BitCube4::from(SUBCUBE_0)).as_slice(),
            &[
                BitCube4::from(SUBCUBE_0),
                BitCube4::from(SUBCUBE_1),
                BitCube4::from(SUBCUBE_2),
                BitCube4::from(SUBCUBE_3),
                BitCube4::from(SUBCUBE_4),
                BitCube4::from(SUBCUBE_5),
                BitCube4::from(SUBCUBE_6),
                BitCube4::from(SUBCUBE_7)
            ]
        );
        assert_eq!(BitCube4::rotate_all_vec(BitCube4(0x3)).len(), 24);
    }

    #[test]
    fn test_origin_rotate_all() {
        assert_eq!(
            BitCube4::origin_rotate_all(BitCube4::from(BC4_CENTER_ALL)).as_slice(),
            &[BitCube4::from(BC4_CENTER_ALL)]
        );
        assert_eq!(
            BitCube4::origin_rotate_all(BitCube4::from(BC4_CENTER_X)).as_slice(),
            &[
                BitCube4(0x0000000000ff00ff),
                BitCube4(0x0000000033333333),
                BitCube4(0x0033003300330033)
            ]
        );
        assert_eq!(
            BitCube4::origin_rotate_all(BitCube4::from(SUBCUBE_0)).len(),
            1
        );
        assert_eq!(
            BitCube4::origin_rotate_all(BitCube4::from(SUBCUBE_0)).as_slice(),
            &[BitCube4::from(SUBCUBE_0)]
        );
        assert_eq!(BitCube4::origin_rotate_all(BitCube4(0x3)).len(), 3);
        assert_eq!(BitCube4::origin_rotate_all(BitCube4(0x1011f)).len(), 24);
    }
}