Repository navigation
Expand file tree
/
Copy pathLifeAPI.hpp
More file actions
1493 lines (1224 loc) · 44.3 KB
/
Copy pathLifeAPI.hpp
File metadata and controls
1493 lines (1224 loc) · 44.3 KB
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <array>
#include <bit>
#include <random>
#include <vector>
#define XXH_INLINE_ALL 1
#include "xxHash/xxhash.h"
#include "Bits.hpp"
const int N = 64;
constexpr unsigned torus_wrap(int x) {
return x & (N - 1); // Valid for negative x
}
namespace PRNG {
static std::random_device rd;
static std::mt19937_64 e2(rd());
static std::uniform_int_distribution<uint64_t>
dist(std::llround(std::pow(2, 61)), std::llround(std::pow(2, 62)));
} // namespace PRNG
enum struct SymmetryTransform : uint32_t;
enum struct StaticSymmetry : uint32_t;
// See LifeTarget.hpp
struct LifeTarget;
// See LifeStrip.hpp
struct StripIndex;
struct LifeStateStrip;
struct LifeStateStripProxy;
struct LifeStateStripConstProxy;
enum class InitializedTag { UNINITIALIZED };
struct __attribute__((aligned(64))) LifeState {
uint64_t state[N];
////////////////////////////////
// Constructors
////////////////////////////////
constexpr LifeState() : state{0} {}
explicit constexpr LifeState(__attribute__((unused)) InitializedTag) {}
LifeState(const LifeState &) = default;
LifeState &operator=(const LifeState &) = default;
LifeState(LifeState&& other) noexcept = default;
LifeState& operator=(LifeState&& other) noexcept = default;
// Avoid accidentally coercing from a bool
LifeState &operator=(bool) = delete;
static constexpr LifeState Cell(std::pair<int, int> cell) {
LifeState result;
result.Set(cell.first, cell.second);
return result;
}
static LifeState RandomState() {
LifeState result;
for (unsigned i = 0; i < N; i++)
result[i] = PRNG::dist(PRNG::e2);
return result;
}
// State is parity of (x + y), so (0, 0) is OFF
static LifeState Checkerboard() {
// mvrnote: TODO, just ConstantParse it
LifeState checkerboard;
for (int i = 0; i < N; i++) {
if (i % 2 == 0)
checkerboard.state[i] = 0xAAAAAAAAAAAAAAAAULL;
else
checkerboard.state[i] = std::rotl(0xAAAAAAAAAAAAAAAAULL, 1);
}
return checkerboard;
}
static constexpr LifeState SolidRect(int x, int y, int w, int h) {
uint64_t column;
if (h < 64)
column = std::rotl(((uint64_t)1 << h) - 1, y);
else
column = ~0ULL;
unsigned start, end;
if (w < N) {
start = torus_wrap(x);
end = torus_wrap(x + w);
} else {
start = 0;
end = N;
}
LifeState result;
if (end > start) {
for (unsigned int i = start; i < end; i++)
result[i] = column;
} else {
for (unsigned int i = 0; i < end; i++)
result[i] = column;
for (unsigned int i = start; i < N; i++)
result[i] = column;
}
return result;
}
static constexpr LifeState SolidRectXY(int x1, int y1, int x2, int y2) {
return SolidRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
}
static constexpr LifeState NZOIAround(std::pair<int, int> cell, unsigned distance) {
unsigned size = 2 * distance + 1;
return LifeState::SolidRect(cell.first - distance, cell.second - distance,
size, size);
}
static constexpr LifeState CellZOI(std::pair<int, int> cell) {
return LifeState::NZOIAround(cell, 1);
}
////////////////////////////////
// Getting and Setting
////////////////////////////////
void Set(unsigned x, unsigned y) { state[x] |= (1ULL << y); }
void Erase(unsigned x, unsigned y) { state[x] &= ~(1ULL << y); }
void Set(unsigned x, unsigned y, bool val) { if(val) Set(x, y); else Erase(x, y); }
bool Get(unsigned x, unsigned y) const { return (state[x] & (1ULL << y)) != 0; }
void SetSafe(int x, int y, bool val) { Set(torus_wrap(x), torus_wrap(y), val); }
bool GetSafe(int x, int y) const { return Get(torus_wrap(x), torus_wrap(y)); }
void Set(std::pair<int, int> cell) { Set(cell.first, cell.second); };
void Erase(std::pair<int, int> cell) { Erase(cell.first, cell.second); };
void Set(std::pair<int, int> cell, bool val) { Set(cell.first, cell.second, val); };
bool Get(std::pair<int, int> cell) const { return Get(cell.first, cell.second); };
void SetSafe(std::pair<int, int> cell, bool val) { SetSafe(cell.first, cell.second, val); };
bool GetSafe(std::pair<int, int> cell) const { return GetSafe(cell.first, cell.second); };
constexpr uint64_t &operator[](const unsigned i) { return state[i]; }
constexpr uint64_t operator[](const unsigned i) const { return state[i]; }
template <unsigned width>
std::array<uint64_t, width> GetStrip(unsigned column) const {
std::array<uint64_t, width> result;
const unsigned offset = (width - 1) / 2; // 0, 0, 1, 1, 2, 2
if (offset <= column && column + width - 1 - offset < N) {
for (unsigned i = 0; i < width; i++) {
const unsigned c = column + i - offset;
result[i] = state[c];
}
} else {
for (unsigned i = 0; i < width; i++) {
const unsigned c = torus_wrap(column + i + N - offset);
result[i] = state[c];
}
}
return result;
}
template <unsigned width>
void SetStrip(unsigned column, std::array<uint64_t, width> value) {
const unsigned offset = (width - 1) / 2; // 0, 0, 1, 1, 2, 2
for (unsigned i = 0; i < width; i++) {
const unsigned c = torus_wrap(column + i + N - offset);
state[c] = value[i];
}
}
inline LifeStateStripProxy operator[](const StripIndex column);
inline const LifeStateStripConstProxy operator[](const StripIndex column) const;
template <unsigned radius> uint64_t GetPatch(std::pair<int, int> cell) const {
auto [x, y] = cell;
unsigned diameter = 2 * radius + 1;
uint64_t result = 0;
for (unsigned i = 0; i < diameter; i++) {
const unsigned c = torus_wrap(x + i + N - radius);
uint64_t bits = std::rotr(state[c], y - radius) & ((1ULL << diameter) - 1);
result |= std::rotl(bits, i * diameter);
}
return result;
}
template <unsigned radius>
void SetPatch(std::pair<int, int> cell, uint64_t value) {
auto [x, y] = cell;
unsigned diameter = 2 * radius + 1;
for (unsigned i = 0; i < diameter; i++) {
const unsigned c = torus_wrap(x + i + N - radius);
uint64_t bits = std::rotr(value, i * diameter) & ((1ULL << diameter) - 1);
state[c] &= ~std::rotl((1ULL << diameter) - 1, y - radius);
state[c] |= std::rotl(bits, y - radius);
}
}
////////////////////////////////
// Operators
////////////////////////////////
bool operator==(const LifeState &b) const {
uint64_t diffs = 0;
for (unsigned i = 0; i < N; i++)
diffs |= state[i] ^ b[i];
return diffs == 0;
}
bool operator!=(const LifeState &b) const { return !(*this == b); }
LifeState operator~() const {
LifeState result(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
result[i] = ~state[i];
}
return result;
}
LifeState operator&(const LifeState &other) const {
LifeState result(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
result[i] = state[i] & other[i];
}
return result;
}
LifeState &operator&=(const LifeState &other) {
for (unsigned i = 0; i < N; i++) {
state[i] = state[i] & other[i];
}
return *this;
}
LifeState operator|(const LifeState &other) const {
LifeState result(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
result[i] = state[i] | other[i];
}
return result;
}
LifeState &operator|=(const LifeState &other) {
for (unsigned i = 0; i < N; i++) {
state[i] = state[i] | other[i];
}
return *this;
}
LifeState operator^(const LifeState &other) const {
LifeState result(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
result[i] = state[i] ^ other[i];
}
return result;
}
LifeState &operator^=(const LifeState &other) {
for (unsigned i = 0; i < N; i++) {
state[i] = state[i] ^ other[i];
}
return *this;
}
////////////////////////////////
// Queries
////////////////////////////////
bool IsEmpty() const {
uint64_t all = 0;
for (unsigned i = 0; i < N; i++) {
all |= state[i];
}
return all == 0;
}
unsigned GetPop() const {
unsigned pop = 0;
for (unsigned i = 0; i < N; i++) {
pop += std::popcount(state[i]);
}
return pop;
}
// Get an ON cell as fast as possible. I make no guarantees about which cell it will be
std::pair<int, int> FirstOn() const {
for (int x = N - 4; x >= 0; x -= 4) {
if ((state[x] | state[x + 1] | state[x + 2] | state[x + 3]) == 0ULL) {
continue;
}
if (state[x] != 0ULL) {
return std::make_pair(x, std::countr_zero(state[x]));
} else if (state[x + 1] != 0ULL) {
return std::make_pair(x + 1, std::countr_zero(state[x + 1]));
} else if (state[x + 2] != 0ULL) {
return std::make_pair(x + 2, std::countr_zero(state[x + 2]));
} else {
return std::make_pair(x + 3, std::countr_zero(state[x + 3]));
}
}
return std::make_pair(-1, -1);
}
// std::pair<int, int> FirstOnWrapped() const {
// unsigned foundq = N;
// for (unsigned x = 0; x < N/2; x += 4) {
// if ((state[x] | state[x + 1] | state[x + 2] | state[x + 3]) != 0ULL) {
// foundq = x;
// }
// }
// if (foundq == N) {
// for (unsigned x = N/2; x < N; x += 4) {
// if ((state[x] | state[x + 1] | state[x + 2] | state[x + 3]) != 0ULL) {
// foundq = x;
// }
// }
// }
// if (foundq == N) {
// return std::make_pair(-1, -1);
// }
// if (state[foundq] != 0ULL) {
// return std::make_pair(foundq, (std::countr_zero(std::rotr(state[foundq], 32)) + 32) % 64);
// } else if (state[foundq + 1] != 0ULL) {
// return std::make_pair(foundq + 1, (std::countr_zero(std::rotr(state[foundq + 1], 32)) + 32) % 64);
// } else if (state[foundq + 2] != 0ULL) {
// return std::make_pair(foundq + 2, (std::countr_zero(std::rotr(state[foundq + 2], 32)) + 32) % 64);
// } else if (state[foundq + 3] != 0ULL) {
// return std::make_pair(foundq + 3, (std::countr_zero(std::rotr(state[foundq + 3], 32)) + 32) % 64);
// } else {
// return std::make_pair(-1, -1);
// }
// }
inline std::vector<std::pair<int, int>> OnCells() const;
LifeState FirstCell() const { return LifeState::Cell(FirstOn()); }
std::pair<int, int> FindSetNeighbour(std::pair<int, int> cell) const {
// This could obviously be done faster by extracting the result
// directly from the columns, but this is probably good enough for now
const std::array<std::pair<int, int>, 9> directions = {std::make_pair(0, 0), {-1, 0}, {1, 0}, {0,1}, {0, -1}, {-1,-1}, {-1,1}, {1, -1}, {1, 1}};
for (auto d : directions) {
int x = torus_wrap(cell.first + d.first);
int y = torus_wrap(cell.second + d.second);
if (Get(x, y))
return std::make_pair(x, y);
}
return std::make_pair(-1, -1);
}
uint64_t GetHash() const { return XXH3_64bits(state, N * sizeof(uint64_t)); }
inline uint64_t GetOctoHash() const;
inline bool AreDisjoint(const LifeState &pat) const {
uint64_t differences = 0;
#pragma clang loop vectorize(enable)
for (unsigned i = 0; i < N; i++) {
uint64_t difference = (~state[i] & pat[i]) ^ (pat[i]);
differences |= difference;
}
return differences == 0;
}
inline bool Contains(const LifeState &pat) const {
uint64_t differences = 0;
#pragma clang loop vectorize(enable)
for (unsigned i = 0; i < N; i++) {
uint64_t difference = (state[i] & pat[i]) ^ (pat[i]);
differences |= difference;
}
return differences == 0;
}
bool Contains(const LifeState &pat, int targetDx, int targetDy) const {
int dy = torus_wrap(targetDy);
for (unsigned i = 0; i < N; i++) {
int curX = torus_wrap(i + targetDx);
if ((std::rotr(state[curX], dy) & pat[i]) != (pat[i]))
return false;
}
return true;
}
bool AreDisjoint(const LifeState &pat, int targetDx, int targetDy) const {
int dy = torus_wrap(targetDy);
for (unsigned i = 0; i < N; i++) {
int curX = torus_wrap(i + targetDx);
if (((~std::rotr(state[curX], dy)) & pat[i]) != pat[i])
return false;
}
return true;
}
inline bool Contains(const LifeTarget &target, int dx, int dy) const;
inline bool Contains(const LifeTarget &target) const;
LifeState MatchLive(const LifeState &live) const {
LifeState invThis = ~*this;
return ~live.Mirrored().Convolve(invThis);
}
LifeState MatchLiveAndDead(const LifeState &live, const LifeState &dead) const {
LifeState invThis = ~*this;
LifeState liveMatches = ~live.Mirrored().Convolve(invThis);
if (liveMatches.IsEmpty())
return LifeState();
return liveMatches & ~Convolve(dead.Mirrored());
}
LifeState MatchesLiveAndDeadSym(const LifeState &live,
const LifeState &dead) const;
LifeState Match(const LifeState &live) const {
return MatchLiveAndDead(live, live.GetBoundary());
}
LifeState Match(const LifeTarget &target) const;
std::array<int, 4> XYBounds() const {
int leftMargin;
int rightMargin;
if constexpr (N == 64) {
uint64_t popCols = PopulatedColumns();
popCols = std::rotr(popCols, 32);
leftMargin = std::countr_zero(popCols);
rightMargin = std::countl_zero(popCols);
}
if constexpr (N == 32) {
uint32_t popCols = PopulatedColumns();
popCols = std::rotr(popCols, 16);
leftMargin = std::countr_zero(popCols);
rightMargin = std::countl_zero(popCols);
}
uint64_t orOfCols = 0;
for (unsigned i = 0; i < N; ++i)
orOfCols |= state[i];
if (orOfCols == 0ULL) {
return std::array<int, 4>({-1, -1, -1, -1});
}
orOfCols = std::rotr(orOfCols, 32);
int topMargin = std::countr_zero(orOfCols);
int bottomMargin = std::countl_zero(orOfCols);
if constexpr (N == 64) {
return std::array<int, 4>({leftMargin - 32, topMargin - 32,
31 - rightMargin, 31 - bottomMargin});
}
if constexpr (N == 32) {
return std::array<int, 4>({leftMargin - 16, topMargin - 32,
15 - rightMargin, 31 - bottomMargin});
}
}
uint64_t PopulatedColumns() const {
uint64_t result = 0;
for (unsigned i = 0; i < N; i++)
if (state[i] != 0)
result |= 1ULL << i;
return result;
}
std::pair<int, int> WidthHeight() const {
uint64_t orOfCols = 0;
for (unsigned i = 0; i < N; ++i)
orOfCols |= state[i];
if (orOfCols == 0ULL) // empty grid.
return std::make_pair(0, 0);
uint64_t cols = PopulatedColumns();
unsigned width;
if constexpr (N == 64) {
width = populated_width_uint64_t(cols);
}
if constexpr (N == 32) {
width = populated_width_uint32_t((uint32_t)cols);
}
unsigned height = populated_width_uint64_t(orOfCols);
return {width, height};
}
////////////////////////////////
// ZOI
////////////////////////////////
LifeState ZOI() const {
LifeState temp(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
uint64_t col = state[i];
temp[i] = col | std::rotl(col, 1) | std::rotr(col, 1);
}
LifeState boundary(InitializedTag::UNINITIALIZED);
boundary[0] = temp[N - 1] | temp[0] | temp[1];
for (int i = 1; i < N - 1; i++)
boundary[i] = temp[i - 1] | temp[i] | temp[i + 1];
boundary[N - 1] = temp[N - 2] | temp[N - 1] | temp[0];
return boundary;
}
LifeState GetBoundary() const { return ZOI() & ~*this; }
// Convolve with 3o$obo$3o!
LifeState ZOIHollow() const {
LifeState temp(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
uint64_t col = state[i];
temp[i] = col | std::rotl(col, 1) | std::rotr(col, 1);
}
LifeState tempmid(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
uint64_t col = state[i];
tempmid[i] = std::rotl(col, 1) | std::rotr(col, 1);
}
LifeState boundary(InitializedTag::UNINITIALIZED);
boundary[0] = temp[N - 1] | tempmid[0] | temp[1];
for (int i = 1; i < N - 1; i++)
boundary[i] = temp[i - 1] | tempmid[i] | temp[i + 1];
boundary[N - 1] = temp[N - 2] | tempmid[N - 1] | temp[0];
return boundary;
}
LifeState BigZOI() const {
LifeState b(InitializedTag::UNINITIALIZED);
b[0] = state[0] | std::rotl(state[0], 1) | std::rotr(state[0], 1) |
state[N - 1] | state[0 + 1];
for (unsigned i = 1; i < N - 1; i++) {
b[i] = state[i] | std::rotl(state[i], 1) | std::rotr(state[i], 1) |
state[i - 1] | state[i + 1];
}
b[N - 1] = state[N - 1] | std::rotl(state[N - 1], 1) |
std::rotr(state[N - 1], 1) | state[N - 1 - 1] | state[0];
LifeState c(InitializedTag::UNINITIALIZED);
c[0] = b[0] | b[N - 1] | b[0 + 1];
for (unsigned i = 1; i < N - 1; i++) {
c[i] = b[i] | b[i - 1] | b[i + 1];
}
c[N - 1] = b[N - 1] | b[N - 1 - 1] | b[0];
LifeState zoi(InitializedTag::UNINITIALIZED);
zoi[0] = c[0] | std::rotl(c[0], 1) | std::rotr(c[0], 1);
for (unsigned i = 1; i < N - 1; i++) {
zoi[i] = c[i] | std::rotl(c[i], 1) | std::rotr(c[i], 1);
}
zoi[N - 1] = c[N - 1] | std::rotl(c[N - 1], 1) | std::rotr(c[N - 1], 1);
return zoi;
}
uint64_t ZOIColumn(int i) const {
uint64_t col = state[torus_wrap(i - 1)] | state[i] | state[torus_wrap(i + 1)];
return std::rotl(col, 1) | col | std::rotr(col, 1);
}
/* static LifeState ColumnZOI(int i, uint64_t col) {
LifeState result;
col = col | std::rotl(col, 1) | std::rotr(col, 1);
result[(i - 1 + N) % N] = col;
result[i] = col;
result[(i + 1) % N] = col;
return result;
} */
LifeState NZOI(unsigned distance) const {
return Convolve(LifeState::NZOIAround({0, 0}, distance));
}
LifeState BufferAround(std::pair<int, int> size) const {
auto bounds = XYBounds();
if (bounds[0] == -1 &&
bounds[1] == -1 &&
bounds[2] == -1 &&
bounds[3] == -1)
return ~LifeState();
int width = bounds[2] - bounds[0] + 1;
int height = bounds[3] - bounds[1] + 1;
int remainingwidth = size.first - width;
int remainingheight = size.second - height;
if (remainingwidth < 0 || remainingheight < 0)
return LifeState();
else
return LifeState::SolidRectXY(bounds[0] - remainingwidth,
bounds[1] - remainingheight,
bounds[2] + remainingwidth,
bounds[3] + remainingheight);
}
LifeState MooreZOI() const {
LifeState temp(InitializedTag::UNINITIALIZED);
LifeState boundary(InitializedTag::UNINITIALIZED);
for (unsigned i = 0; i < N; i++) {
uint64_t col = state[i];
temp[i] = col | std::rotl(col, 1) | std::rotr(col, 1);
}
boundary[0] = state[N - 1] | temp[0] | state[1];
for (unsigned i = 1; i < N - 1; i++)
boundary[i] = state[i - 1] | temp[i] | state[i + 1];
boundary[N - 1] = state[N - 2] | temp[N - 1] | state[0];
return boundary;
}
inline LifeState Convolve(const LifeState &other) const;
LifeState ComponentContaining(const LifeState &seed, const LifeState &corona) const {
LifeState result;
LifeState tocheck = seed;
while (!tocheck.IsEmpty()) {
LifeState neighbours = tocheck.Convolve(corona) & *this;
tocheck = neighbours & ~result;
result |= neighbours;
}
return result;
}
std::vector<LifeState> Components(const LifeState &corona) const {
std::vector<LifeState> result;
LifeState remaining = *this;
while (!remaining.IsEmpty()) {
LifeState component = remaining.ComponentContaining(remaining.FirstCell(), corona);
result.push_back(component);
remaining &= ~component;
}
return result;
}
std::vector<LifeState> StillComponents() const {
LifeState out1, out2, outMore;
InteractionCounts(out1, out2, outMore);
const LifeState stillzoi = LifeState::ConstantParse("2bo$b3o$5o$b3o$2bo!", -2, -2);
auto cs = (*this | outMore).Components(stillzoi);
for(auto &c : cs){
c &= *this;
}
return cs;
}
LifeState StillComponentContaining(const LifeState &seed) const {
LifeState out1, out2, outMore;
InteractionCounts(out1, out2, outMore);
const LifeState stillzoi = LifeState::ConstantParse("2bo$b3o$5o$b3o$2bo!", -2, -2);
auto c = (*this | outMore).ComponentContaining(seed, stillzoi);
return c & *this;
}
private:
bool IsPseudoStillLifeHelper(const std::vector<LifeState>& components, bool topLevel) const {
if (components.empty()) return true;
if (!topLevel) {
LifeState combined;
for (auto &c : components) {
combined |= c;
}
if (combined == combined.Stepped())
return true;
}
// Try all non-empty proper subsets (2^n - 2 possibilities)
int n = components.size();
for (int mask = 1; mask < (1 << n) - 1; mask++) {
LifeState group1, group2;
std::vector<LifeState> group1Components, group2Components;
// Split components based on mask
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
group1 |= components[i];
group1Components.push_back(components[i]);
} else {
group2 |= components[i];
group2Components.push_back(components[i]);
}
}
if (IsPseudoStillLifeHelper(group1Components, false) &&
IsPseudoStillLifeHelper(group2Components, false)) {
return true;
}
}
return false;
}
public:
bool IsPseudoStillLife() const {
auto components = Components(LifeState::ConstantParse("3o$3o$3o!", -1, -1));
return IsPseudoStillLifeHelper(components, true);
}
////////////////////////////////
// Transforms
////////////////////////////////
void Move(int x, int y) {
uint64_t temp[N];
x = torus_wrap(x);
const int shift = static_cast<int>(static_cast<unsigned>(y) & 63u);
const unsigned split = N - x;
if (x == 0 && shift == 0)
return;
if (x == 0) {
for (unsigned i = 0; i < N; i++) {
state[i] = std::rotl(state[i], shift);
}
return;
}
if (shift == 0) {
for (unsigned i = 0; i < split; i++) {
temp[i + x] = state[i];
}
for (unsigned i = split; i < N; i++) {
temp[i - split] = state[i];
}
for (unsigned i = 0; i < N; i++) {
state[i] = temp[i];
}
return;
}
for (unsigned i = 0; i < split; i++) {
temp[i + x] = std::rotl(state[i], shift);
}
for (unsigned i = split; i < N; i++) {
temp[i - split] = std::rotl(state[i], shift);
}
for (unsigned i = 0; i < N; i++) {
state[i] = temp[i];
}
}
void Move(std::pair<int, int> vec) { Move(vec.first, vec.second); }
constexpr LifeState Moved(int x, int y) const {
x = torus_wrap(x);
const int shift = static_cast<int>(static_cast<unsigned>(y) & 63u);
const unsigned split = N - x;
if (x == 0 && shift == 0)
return *this;
LifeState result(InitializedTag::UNINITIALIZED);
if (x == 0) {
for (unsigned i = 0; i < N; i++) {
result[i] = std::rotl(state[i], shift);
}
return result;
}
if (shift == 0) {
for (unsigned i = 0; i < split; i++) {
result[i + x] = state[i];
}
for (unsigned i = split; i < N; i++) {
result[i - split] = state[i];
}
return result;
}
for (unsigned i = 0; i < split; i++) {
result[i + x] = std::rotl(state[i], shift);
}
for (unsigned i = split; i < N; i++) {
result[i - split] = std::rotl(state[i], shift);
}
return result;
}
// constexpr LifeState Moved(int x, int y) const {
// LifeState result(InitializedTag::UNINITIALIZED);
// if (x < 0)
// x += N;
// if (y < 0)
// y += 64;
// for (unsigned i = 0; i < N; i++) {
// int newi = (i + x) % N;
// result[newi] = std::rotl(state[i], y);
// }
// return result;
// }
constexpr LifeState Moved(std::pair<int, int> vec) const {
return Moved(vec.first, vec.second);
}
void AlignWith(const LifeState &other) {
auto offset = Match(other).FirstOn();
Move(-offset.first, -offset.second);
}
void Reverse(unsigned idxS, unsigned idxE) {
for (unsigned i = 0; idxS + 2 * i < idxE; i++) {
int l = idxS + i;
int r = idxE - i;
uint64_t temp = state[l];
state[l] = state[r];
state[r] = temp;
}
}
void FlipY() { // even reflection across y-axis, ie (0,0) maps to (-1, 0)
Reverse(0, N - 1);
}
void BitReverse() {
for (unsigned i = 0; i < N; i++) {
state[i] = __builtin_bitreverse64(state[i]);
}
}
// even reflection across x-axis, ie (0,0) maps to (0, -1)
void FlipX() { BitReverse(); }
void Transpose(bool whichDiagonal) {
int j, k;
uint64_t m, t;
for (j = N / 2, m = (~0ULL) >> (N / 2); j; j >>= 1, m ^= m << j) {
for (k = 0; k < N; k = ((k | j) + 1) & ~j) {
if (whichDiagonal) {
t = (state[k] ^ (state[k | j] >> j)) & m;
state[k] ^= t;
state[k | j] ^= (t << j);
} else {
t = (state[k] >> j ^ (state[k | j])) & m;
state[k] ^= (t << j);
state[k | j] ^= t;
}
}
}
}
void Transpose() { Transpose(true); }
inline void Transform(SymmetryTransform transf);
LifeState Mirrored() const {
LifeState result = *this;
result.FlipX();
result.FlipY();
result.Move(1, 1);
return result;
}
LifeState Transformed(SymmetryTransform transf) const {
LifeState result = *this;
result.Transform(transf);
return result;
}
void Transform(int dx, int dy, SymmetryTransform transf) {
Move(dx, dy);
Transform(transf);
}
inline std::vector<LifeState> SymmetryOrbit() const;
inline std::vector<SymmetryTransform> SymmetryOrbitRepresentatives() const;
LifeState Halve() const;
LifeState HalveX() const;
LifeState HalveY() const;
LifeState Skew() const;
LifeState InvSkew() const;
////////////////////////////////
// Stepping and Counting
////////////////////////////////
static inline void HalfAdd(uint64_t &out0, uint64_t &out1, const uint64_t ina, const uint64_t inb) {
out0 = ina ^ inb;
out1 = ina & inb;
}
static inline void FullAdd(uint64_t &out0, uint64_t &out1, const uint64_t ina, const uint64_t inb, const uint64_t inc) {
uint64_t halftotal = ina ^ inb;
out0 = halftotal ^ inc;
uint64_t halfcarry1 = ina & inb;
uint64_t halfcarry2 = inc & halftotal;
out1 = halfcarry1 | halfcarry2;
}
// From Page 5 of
// https://www.gathering4gardner.org/g4g13gift/math/RokickiTomas-GiftExchange-LifeAlgorithms-G4G13.pdf
uint64_t inline Rokicki(const uint64_t &a, const uint64_t &bU0,
const uint64_t &bU1, const uint64_t &bB0,
const uint64_t &bB1) {
uint64_t aw = std::rotl(a, 1);
uint64_t ae = std::rotr(a, 1);
uint64_t s0 = aw ^ ae;
uint64_t s1 = aw & ae;
uint64_t ts0 = bB0 ^ bU0;
uint64_t ts1 = (bB0 & bU0) | (ts0 & s0);
return (bB1 ^ bU1 ^ ts1 ^ s1) & ((bB1 | bU1) ^ (ts1 | s1)) &
((ts0 ^ s0) | a);
}
static inline void HalfAdd(LifeState &outbit, LifeState &outcarry,
const LifeState &ina, const LifeState &inb) {
outbit = ina ^ inb;
outcarry = ina & inb;
}
static inline void FullAdd(LifeState &outbit, LifeState &outcarry,
const LifeState &ina, const LifeState &inb,
const LifeState &inc) {
LifeState halftotal = ina ^ inb;
outbit = halftotal ^ inc;
LifeState halfcarry1 = ina & inb;
LifeState halfcarry2 = inc & halftotal;
outcarry = halfcarry1 | halfcarry2;
}
inline void Step();
// mvrnote: This could be done without the copy
inline LifeState Stepped() const {
LifeState copy = *this;
copy.Step();
return copy;
}
inline void StepAlt();
void Step(unsigned numIters) {
for (unsigned i = 0; i < numIters; i++) {
Step();
}
}
inline LifeState Stepped(unsigned numIters) const {
LifeState copy = *this;
copy.Step(numIters);
return copy;
}