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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
|
#include "coord.h"
static Cube admissible_eos_from_eofbepos(Cube cube);
static Cube antindex_eofb(uint64_t ind);
static Cube antindex_eofbepos(uint64_t ind);
static Cube antindex_epud(uint64_t ind);
static Cube antindex_coud(uint64_t ind);
static Cube antindex_corners(uint64_t ind);
static Cube antindex_cp(uint64_t ind);
static Cube antindex_cornershtr(uint64_t ind);
static Cube antindex_drud(uint64_t ind);
static Cube antindex_coud_sym16(uint64_t ind);
static Cube antindex_cp_sym16(uint64_t ind);
static Cube antindex_eofbepos_sym16(uint64_t ind);
static Cube antindex_drud_sym16(uint64_t ind);
static Cube antindex_drud_eofb(uint64_t ind);
static Cube antindex_drudfin_noE_sym16(uint64_t ind);
static Cube antindex_htrfin(uint64_t ind);
static Cube antindex_khuge(uint64_t ind);
static int epos_dependent_pos(int pos1, int pos2);
static void gensym(SymData *sd);
static uint64_t index_eofb(Cube cube);
static uint64_t index_eofbepos(Cube cube);
static uint64_t index_epud(Cube cube);
static uint64_t index_coud(Cube cube);
static uint64_t index_corners(Cube cube);
static uint64_t index_cp(Cube cube);
static uint64_t index_cornershtr(Cube cube);
static uint64_t index_drud(Cube cube);
static uint64_t index_coud_sym16(Cube cube);
static uint64_t index_cp_sym16(Cube cube);
static uint64_t index_eofbepos_sym16(Cube cube);
static uint64_t index_drud_sym16(Cube cube);
static uint64_t index_drud_eofb(Cube cube);
static uint64_t index_drudfin_noE_sym16(Cube cube);
static uint64_t index_htrfin(Cube cube);
static uint64_t index_khuge(Cube cube);
static void init_cphtr_cosets();
static void init_cphtr_left_cosets_bfs(int i, int c);
static void init_cphtr_right_cosets_color(int i, int c);
static void init_symdata();
static bool read_symdata_file(SymData *sd);
static bool write_symdata_file(SymData *sd);
/* All sorts of useful costants and tables **********************************/
/* TODO: Can I move inside functions that use them?
Maybe I need to pass them as argument to some
secondary function */
static int cphtr_left_cosets[FACTORIAL8];
static int cphtr_right_cosets[FACTORIAL8];
static int cphtr_right_rep[BINOM8ON4*6];
/* Symmetry data for some coordinates ****************************************/
static Trans
trans_group_trivial[1] = { uf };
static Trans
trans_group_udfix[16] = {
uf, ur, ub, ul,
df, dr, db, dl,
uf_mirror, ur_mirror, ub_mirror, ul_mirror,
df_mirror, dr_mirror, db_mirror, dl_mirror,
};
SymData
sd_coud_16 = {
.filename = "sd_coud_16",
.coord = &coord_coud,
.sym_coord = &coord_coud_sym16,
.ntrans = 16,
.trans = trans_group_udfix
};
SymData
sd_cp_16 = {
.filename = "sd_cp_16",
.coord = &coord_cp,
.sym_coord = &coord_cp_sym16,
.ntrans = 16,
.trans = trans_group_udfix
};
SymData
sd_eofbepos_16 = {
.filename = "sd_eofbepos_16",
.coord = &coord_eofbepos,
.sym_coord = &coord_eofbepos_sym16,
.ntrans = 16,
.trans = trans_group_udfix
};
static int n_all_symdata = 3;
static SymData * all_sd[3] = {
&sd_coud_16,
&sd_cp_16,
&sd_eofbepos_16,
};
/* Coordinates and their implementation **************************************/
Coordinate
coord_eofb = {
.index = index_eofb,
.cube = antindex_eofb,
.check = check_eofb,
.max = POW2TO11,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_eofbepos = {
.index = index_eofbepos,
.cube = antindex_eofbepos,
.check = check_eofbepos,
.max = POW2TO11 * BINOM12ON4,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_coud = {
.index = index_coud,
.cube = antindex_coud,
.check = check_coud,
.max = POW3TO7,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_corners = {
.index = index_corners,
.cube = antindex_corners,
.check = check_corners,
.max = POW3TO7 * FACTORIAL8,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_cp = {
.index = index_cp,
.cube = antindex_cp,
.check = check_cp,
.max = FACTORIAL8,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_cornershtr = {
.index = index_cornershtr,
.cube = antindex_cornershtr,
.check = check_cornershtr,
.max = POW3TO7 * BINOM8ON4 * 6,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_drud = {
.index = index_drud,
.cube = antindex_drud,
.check = check_drud,
.max = POW2TO11 * POW3TO7 * BINOM12ON4,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_htrfin = {
.index = index_htrfin,
.cube = antindex_htrfin,
.check = check_htrfin,
.max = 24 * 24 * 24 *24 * 24,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_drud_eofb = {
.index = index_drud_eofb,
.cube = antindex_drud_eofb,
.check = check_drud,
.max = POW3TO7 * BINOM12ON4,
.ntrans = 1,
.trans = trans_group_trivial,
};
Coordinate
coord_eofbepos_sym16 = {
.index = index_eofbepos_sym16,
.cube = antindex_eofbepos_sym16,
.check = check_eofbepos,
.ntrans = 16,
.trans = trans_group_udfix,
};
Coordinate
coord_coud_sym16 = {
.index = index_coud_sym16,
.cube = antindex_coud_sym16,
.check = check_coud,
.ntrans = 16,
.trans = trans_group_udfix,
};
Coordinate
coord_cp_sym16 = {
.index = index_cp_sym16,
.cube = antindex_cp_sym16,
.check = check_cp,
.ntrans = 16,
.trans = trans_group_udfix,
};
Coordinate
coord_drud_sym16 = {
.index = index_drud_sym16,
.cube = antindex_drud_sym16,
.check = check_drud,
.max = POW3TO7 * 64430,
.ntrans = 16,
.trans = trans_group_udfix,
};
Coordinate
coord_drudfin_noE_sym16 = {
.index = index_drudfin_noE_sym16,
.cube = antindex_drudfin_noE_sym16,
.check = check_drudfin_noE,
.max = FACTORIAL8 * 2768,
.ntrans = 16,
.trans = trans_group_udfix,
};
Coordinate
coord_khuge = {
.index = index_khuge,
.cube = antindex_khuge,
.check = check_khuge,
.max = POW3TO7 * FACTORIAL4 * 64430,
.ntrans = 16,
.trans = trans_group_udfix,
};
/* Functions *****************************************************************/
static Cube
admissible_eos_from_eofbepos(Cube cube)
{
Edge e;
Cube ret;
CubeArray *arr = new_cubearray(cube, pf_all);
memcpy(arr->eorl, arr->eofb, 12 * sizeof(int));
memcpy(arr->eoud, arr->eofb, 12 * sizeof(int));
for (e = 0; e < 12; e++) {
if ((edge_slice(e) != 0 && edge_slice(arr->ep[e]) == 0) ||
(edge_slice(e) == 0 && edge_slice(arr->ep[e]) != 0))
arr->eorl[e] = 1 - arr->eorl[e];
if ((edge_slice(e) != 2 && edge_slice(arr->ep[e]) == 2) ||
(edge_slice(e) == 2 && edge_slice(arr->ep[e]) != 2))
arr->eoud[e] = 1 - arr->eoud[e];
}
ret = arrays_to_cube(arr, pf_all);
free_cubearray(arr, pf_all);
return ret;
}
static Cube
antindex_eofb(uint64_t ind)
{
return (Cube){ .eofb = ind, .eorl = ind, .eoud = ind };
}
static Cube
antindex_eofbepos(uint64_t ind)
{
static bool initialized = false;
static Cube admissible_ee_aux[POW2TO11*BINOM12ON4];
static Cube c1;
static int k;
static uint64_t ui;
if (!initialized) {
for (ui = 0; ui < POW2TO11*BINOM12ON4; ui++) {
k = (ui / POW2TO11) * 24;
c1 = admissible_ep((Cube){ .epose = k }, pf_e);
c1.eofb = ui % POW2TO11;
c1 = admissible_eos_from_eofbepos(c1);
admissible_ee_aux[ui] = c1;
}
initialized = true;
}
return admissible_ee_aux[ind];
}
static Cube
antindex_epud(uint64_t ind)
{
static bool initialized = false;
static Cube epud_aux[FACTORIAL8];
int a[12];
uint64_t ui;
CubeArray arr;
if (!initialized) {
a[FR] = FR;
a[FL] = FL;
a[BL] = BL;
a[BR] = BR;
for (ui = 0; ui < FACTORIAL8; ui++) {
index_to_perm(ui, 8, a);
arr.ep = a;
epud_aux[ui] = arrays_to_cube(&arr, pf_ep);
}
initialized = true;
}
return epud_aux[ind];
}
static Cube
antindex_coud(uint64_t ind)
{
return (Cube){ .coud = ind, .corl = ind, .cofb = ind };
}
static Cube
antindex_corners(uint64_t ind)
{
Cube c = {0};
c.coud = ind / FACTORIAL8;
c.cp = ind % FACTORIAL8;
return c;
}
static Cube
antindex_cp(uint64_t ind)
{
Cube c = {0};
c.cp = ind;
return c;
}
static Cube
antindex_cornershtr(uint64_t ind)
{
Cube c = anti_cphtr(ind % (BINOM8ON4 * 6));
c.coud = ind / (BINOM8ON4 * 6);
return c;
}
/* TODO: admissible eos and cos */
/* DONE: temporary fix, make it better */
/* Or maybe it's ok like this? */
static Cube
antindex_drud(uint64_t ind)
{
uint64_t epos, eofb;
Cube c;
eofb = ind % POW2TO11;
epos = ind / (POW2TO11 * POW3TO7);
c = antindex_eofbepos(eofb + POW2TO11 * epos);
c.coud = (ind / POW2TO11) % POW3TO7;
c.corl = c.coud;
c.cofb = c.coud;
return c;
}
static Cube
antindex_drud_eofb(uint64_t ind)
{
return antindex_drud(ind * POW2TO11);
}
static Cube
antindex_coud_sym16(uint64_t ind)
{
return sd_coud_16.rep[ind];
}
static Cube
antindex_cp_sym16(uint64_t ind)
{
return sd_cp_16.rep[ind];
}
static Cube
antindex_eofbepos_sym16(uint64_t ind)
{
return sd_eofbepos_16.rep[ind];
}
static Cube
antindex_drud_sym16(uint64_t ind)
{
Cube c;
c = sd_eofbepos_16.rep[ind/POW3TO7];
c.coud = ind % POW3TO7;
c.cofb = c.coud;
c.corl = c.coud;
return c;
}
static Cube
antindex_drudfin_noE_sym16(uint64_t ind)
{
Cube c1, c2;
c1 = antindex_epud(ind % FACTORIAL8);
c2 = sd_cp_16.rep[ind/FACTORIAL8];
c1.cp = c2.cp;
return c1;
}
static Cube
antindex_htrfin(uint64_t ind)
{
Cube ret = {0};
uint64_t cp1, cp2;
static bool initialized = false;
static int i, j, k, c[8], c1[4], c2[4], cp[24][24];
static int c1solved[4] = {UFR, UBL, DFL, DBR};
static int c2solved[4] = {UFL, UBR, DFR, DBL};
if (!initialized) {
for (i = 0; i < 24; i++) {
for (j = 0; j < 24; j++) {
index_to_perm(i, 4, c1);
index_to_perm(j, 4, c2);
for (k = 0; k < 8; k++)
if (k == UFR || k == UBL ||
k == DFL || k == DBR)
c[k] = c1[c1solved[k/2]];
else
c[k] = c2[c2solved[k/2]];
cp[i][j] = perm_to_index(c, 8);
}
}
initialized = true;
}
cp2 = ind % 24;
ind /= 24;
cp1 = ind % 24;
ret.cp = cp[cp1][cp2];
ind /= 24;
ret.eposm = ind % 24;
ind /= 24;
ret.eposs = ind % 24;
ret.epose = ind / 24;
return ret;
}
static Cube
antindex_khuge(uint64_t ind)
{
Cube c;
c = sd_eofbepos_16.rep[ind/(FACTORIAL4*POW3TO7)];
c.epose = ((c.epose / 24) * 24) + ((ind/POW3TO7) % 24);
c.coud = ind % POW3TO7;
return c;
}
bool
check_centers(Cube cube)
{
return cube.cpos == 0;
}
bool
check_corners(Cube cube)
{
return cube.cp == 0 && cube.coud == 0;
}
bool
check_cp(Cube cube)
{
return cube.cp == 0;
}
bool
check_cornershtr(Cube cube)
{
return cube.coud == 0 && cphtr(cube) == 0; /* TODO: use array cphtrcosets*/
}
bool
check_coud(Cube cube)
{
return cube.coud == 0;
}
bool
check_drud(Cube cube)
{
return cube.eofb == 0 && cube.eorl == 0 && cube.coud == 0;
}
bool
check_htr(Cube cube)
{
return check_cornershtr(cube) &&
cube.eofb == 0 && cube.eorl == 0 && cube.eoud == 0;
}
bool
check_htrfin(Cube cube)
{
return cube.cp == 0 &&
cube.epose == 0 && cube.eposs == 0 && cube.eposm == 0;
}
bool
check_drudfin_noE(Cube cube)
{
return cube.eposs == 0 && cube.eposm == 0 && cube.cp == 0;
}
bool
check_eofb(Cube cube)
{
return cube.eofb == 0;
}
bool
check_eofbepos(Cube cube)
{
return cube.eofb == 0 && cube.epose / 24 == 0;
}
bool
check_epose(Cube cube)
{
return cube.epose == 0;
}
bool
check_ep(Cube cube)
{
return cube.epose == 0 && cube.eposs == 0 && cube.eposm == 0;
}
bool
check_khuge(Cube cube)
{
return check_drud(cube) && cube.epose % 24 == 0;
}
bool
check_nothing(Cube cube)
{
return is_admissible(cube); /*TODO: maybe change?*/
}
static int
epos_dependent_pos(int poss, int pose)
{
static int epe_solved[4] = {FR, FL, BL, BR};
static int eps_solved[4] = {UL, UR, DL, DR};
int ep[12] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int ep8[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int i, j;
epos_to_partial_ep(poss*FACTORIAL4, ep, eps_solved);
epos_to_partial_ep(pose*FACTORIAL4, ep, epe_solved);
for (i = 0, j = 0; i < 12; i++)
if (edge_slice(ep[i]) != 0)
ep8[j++] = (edge_slice(ep[i]) == 1) ? 1 : 0;
swap(&ep8[1], &ep8[4]);
swap(&ep8[3], &ep8[6]);
return subset_to_index(ep8, 8, 4);
}
static void
gensym(SymData *sd)
{
uint64_t i, in, nreps = 0;
int j;
Cube c, d;
if (sd->generated)
return;
sd->class = malloc(sd->coord->max * sizeof(uint64_t));
sd->rep = malloc(sd->coord->max * sizeof(Cube));
sd->transtorep = malloc(sd->coord->max * sizeof(Trans));
if (read_symdata_file(sd)) {
sd->generated = true;
return;
}
fprintf(stderr, "Cannot load %s, generating it\n", sd->filename);
for (i = 0; i < sd->coord->max; i++)
sd->class[i] = sd->coord->max + 1;
for (i = 0; i < sd->coord->max; i++) {
if (sd->class[i] == sd->coord->max + 1) {
c = sd->coord->cube(i);
sd->rep[nreps] = c;
for (j = 0; j < sd->ntrans; j++) {
d = apply_trans(sd->trans[j], c);
in = sd->coord->index(d);
if (sd->class[in] == sd->coord->max + 1) {
sd->class[in] = nreps;
sd->transtorep[in] =
inverse_trans(sd->trans[j]);
}
}
nreps++;
}
}
sd->sym_coord->max = nreps;
sd->rep = realloc(sd->rep, nreps * sizeof(Cube));
sd->generated = true;
fprintf(stderr, "Found %lu classes\n", nreps);
if (!write_symdata_file(sd))
fprintf(stderr, "Error writing SymData file\n");
return;
}
static uint64_t
index_eofb(Cube cube)
{
return cube.eofb;
}
static uint64_t
index_eofbepos(Cube cube)
{
return (cube.epose / FACTORIAL4) * POW2TO11 + cube.eofb;
}
static uint64_t
index_epud(Cube cube)
{
uint64_t ret;
CubeArray *arr = new_cubearray(cube, pf_ep);
ret = perm_to_index(arr->ep, 8);
free_cubearray(arr, pf_ep);
return ret;
}
static uint64_t
index_coud(Cube cube)
{
return cube.coud;
}
static uint64_t
index_corners(Cube cube)
{
return cube.coud * FACTORIAL8 + cube.cp;
}
static uint64_t
index_cp(Cube cube)
{
return cube.cp;
}
static uint64_t
index_cornershtr(Cube cube)
{
return cube.coud * BINOM8ON4 * 6 + cphtr(cube);
}
static uint64_t
index_drud(Cube cube)
{
uint64_t a, b, c;
a = cube.eofb;
b = cube.coud;
c = cube.epose / FACTORIAL4;
b *= POW2TO11;
c *= POW2TO11 * POW3TO7;
return a + b + c;
}
static uint64_t
index_drud_eofb(Cube cube)
{
return index_drud(cube) / POW2TO11;
}
static uint64_t
index_coud_sym16(Cube cube)
{
return sd_coud_16.class[index_coud(cube)];
}
static uint64_t
index_cp_sym16(Cube cube)
{
return sd_cp_16.class[index_cp(cube)];
}
static uint64_t
index_drud_sym16(Cube cube)
{
Trans t;
Cube c;
t = sd_eofbepos_16.transtorep[index_eofbepos(cube)];
c = apply_trans(t, cube);
return index_eofbepos_sym16(c) * POW3TO7 + c.coud;
}
static uint64_t
index_drudfin_noE_sym16(Cube cube)
{
Trans t;
Cube c;
t = sd_cp_16.transtorep[index_cp(cube)];
c = apply_trans(t, cube);
return index_cp_sym16(c) * FACTORIAL8 + index_epud(c);
}
static uint64_t
index_htrfin(Cube cube)
{
uint64_t epe, eps, epm, cp, ep;
static bool initialized = false;
static uint64_t cp1[FACTORIAL8], cp2[FACTORIAL8];
static unsigned int i;
static int j, n1, n2, c[8], c1[4], c2[4];
if (!initialized) {
for (i = 0; i < FACTORIAL8; i++) {
index_to_perm(i, 8, c);
n1 = 0;
n2 = 0;
for (j = 0; j < 8; j++)
if (c[j] == UFR || c[j] == UBL ||
c[j] == DFL || c[j] == DBR)
c1[n1++] = c[j] / 2;
else
c2[n2++] = c[j] / 2;
cp1[i] = perm_to_index(c1, 4);
cp2[i] = perm_to_index(c2, 4);
}
initialized = true;
}
epe = cube.epose % 24;
eps = cube.eposs % 24;
epm = cube.eposm % 24;
cp = cp1[cube.cp] * 24 + cp2[cube.cp];
ep = (epe * 24 + eps) *24 + epm;
return ep * 24 * 24 + cp;
}
static uint64_t
index_eofbepos_sym16(Cube cube)
{
return sd_eofbepos_16.class[index_eofbepos(cube)];
}
static uint64_t
index_khuge(Cube cube)
{
Trans t;
Cube c;
uint64_t a;
t = sd_eofbepos_16.transtorep[index_eofbepos(cube)];
c = apply_trans(t, cube);
a = (index_eofbepos_sym16(c) * 24) + (c.epose % 24);
return a * POW3TO7 + c.coud;
}
static bool
read_symdata_file(SymData *sd)
{
init_env();
FILE *f;
char fname[strlen(tabledir)+100];
uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max;
bool r = true;
strcpy(fname, tabledir);
strcat(fname, "/");
strcat(fname, sd->filename);
if ((f = fopen(fname, "rb")) == NULL)
return false;
r = r && fread(&sd->sym_coord->max, sizeof(uint64_t), 1, f) == 1;
r = r && fread(sd->rep, sizeof(Cube), *sn, f) == *sn;
r = r && fread(sd->class, sizeof(uint64_t), n, f) == n;
r = r && fread(sd->transtorep, sizeof(Trans), n, f) == n;
fclose(f);
return r;
}
static bool
write_symdata_file(SymData *sd)
{
init_env();
FILE *f;
char fname[strlen(tabledir)+100];
uint64_t n = sd->coord->max, *sn = &sd->sym_coord->max;
bool r = true;
strcpy(fname, tabledir);
strcat(fname, "/");
strcat(fname, sd->filename);
if ((f = fopen(fname, "wb")) == NULL)
return false;
r = r && fwrite(&sd->sym_coord->max, sizeof(uint64_t), 1, f) == 1;
r = r && fwrite(sd->rep, sizeof(Cube), *sn, f) == *sn;
r = r && fwrite(sd->class, sizeof(uint64_t), n, f) == n;
r = r && fwrite(sd->transtorep, sizeof(Trans), n, f) == n;
fclose(f);
return r;
}
/* Init functions implementation *********************************************/
/*
* There is certainly a bette way to do this, but for now I just use
* a "graph coloring" algorithm to compute the left cosets, and I compose
* with every possible cp to get the right cosets (it is possible that I am
* mixing up left and right).
*
* For doing it better "Mathematically", we need 3 things:
* - Checking that cp separates the orbits (UFR,UBL,DFL,DBR) and the other
* This is easy and it is done in the commented function cphtr_cp().
* - Check that there is no ep/cp parity
* - Check that we are not in the "3c" case; this is the part I don't
* know how to do.
*/
static void
init_cphtr_cosets()
{
unsigned int i;
int c = 0, d = 0;
for (i = 0; i < FACTORIAL8; i++) {
cphtr_left_cosets[i] = -1;
cphtr_right_cosets[i] = -1;
}
/* First we compute left cosets with a bfs */
for (i = 0; i < FACTORIAL8; i++)
if (cphtr_left_cosets[i] == -1)
init_cphtr_left_cosets_bfs(i, c++);
/* Then we compute right cosets using compose() */
for (i = 0; i < FACTORIAL8; i++)
if (cphtr_right_cosets[i] == -1)
init_cphtr_right_cosets_color(i, d++);
}
static void
init_cphtr_left_cosets_bfs(int i, int c)
{
int j, jj, k, next[FACTORIAL8], next2[FACTORIAL8], n, n2;
Move moves[6] = {U2, D2, R2, L2, F2, B2};
n = 1;
next[0] = i;
cphtr_left_cosets[i] = c;
while (n != 0) {
for (j = 0, n2 = 0; j < n; j++) {
for (k = 0; k < 6; k++) {
/*jj = cp_mtable[moves[k]][next[j]];*/
/* TODO fix formatting */
jj = apply_move(moves[k], (Cube){.cp=next[j]}).cp;
if (cphtr_left_cosets[jj] == -1) {
cphtr_left_cosets[jj] = c;
next2[n2++] = jj;
}
}
}
for (j = 0; j < n2; j++)
next[j] = next2[j];
n = n2;
}
}
static void
init_cphtr_right_cosets_color(int i, int d)
{
int cp;
unsigned int j;
cphtr_right_rep[d] = i;
for (j = 0; j < FACTORIAL8; j++) {
if (cphtr_left_cosets[j] == 0) {
/* TODO: use antindexer, it's nicer */
cp = compose((Cube){.cp = i}, (Cube){.cp = j}).cp;
cphtr_right_cosets[cp] = d;
}
}
}
static void
init_symdata()
{
int i;
for (i = 0; i < n_all_symdata; i++)
gensym(all_sd[i]);
}
/*TODO maybe move the next two */
uint64_t
cphtr(Cube cube)
{
return cphtr_right_cosets[cube.cp];
}
Cube
anti_cphtr(uint64_t ind)
{
return (Cube) { .cp = cphtr_right_rep[ind] };
}
uint64_t
epos_dependent(Cube c)
{
static int initialized = false;
static int aux[BINOM12ON4][BINOM12ON4];
static uint64_t ui, uj;
if (!initialized) {
for (ui = 0; ui < BINOM12ON4; ui++)
for (uj = 0; uj < BINOM12ON4; uj++)
aux[ui][uj] = epos_dependent_pos(ui, uj);
initialized = true;
}
return aux[c.eposs/FACTORIAL4][c.epose/FACTORIAL4];
}
void
init_coord()
{
static bool initialized = false;
if (initialized)
return;
initialized = true;
init_cphtr_cosets();
init_symdata();
}
|