diff options
Diffstat (limited to 'src/coord.c')
| -rw-r--r-- | src/coord.c | 560 |
1 files changed, 241 insertions, 319 deletions
diff --git a/src/coord.c b/src/coord.c index 3bbda22..abfa1b9 100644 --- a/src/coord.c +++ b/src/coord.c | |||
| @@ -1,20 +1,5 @@ | |||
| 1 | #include "coord.h" | 1 | #include "coord.h" |
| 2 | 2 | ||
| 3 | static Cube antindex_eofb(uint64_t ind); | ||
| 4 | static Cube antindex_eofbepos(uint64_t ind); | ||
| 5 | static Cube antindex_epud(uint64_t ind); | ||
| 6 | static Cube antindex_coud(uint64_t ind); | ||
| 7 | static Cube antindex_corners(uint64_t ind); | ||
| 8 | static Cube antindex_cp(uint64_t ind); | ||
| 9 | static Cube antindex_cphtr(uint64_t); | ||
| 10 | static Cube antindex_cornershtr(uint64_t ind); | ||
| 11 | static Cube antindex_cornershtrfin(uint64_t ind); | ||
| 12 | static Cube antindex_drud(uint64_t ind); | ||
| 13 | static Cube antindex_drud_eofb(uint64_t ind); | ||
| 14 | static Cube antindex_htr_drud(uint64_t ind); | ||
| 15 | static Cube antindex_htrfin(uint64_t ind); | ||
| 16 | static Cube antindex_cpud_separate(uint64_t ind); | ||
| 17 | |||
| 18 | static uint64_t index_eofb(Cube cube); | 3 | static uint64_t index_eofb(Cube cube); |
| 19 | static uint64_t index_eofbepos(Cube cube); | 4 | static uint64_t index_eofbepos(Cube cube); |
| 20 | static uint64_t index_epud(Cube cube); | 5 | static uint64_t index_epud(Cube cube); |
| @@ -30,10 +15,27 @@ static uint64_t index_htr_drud(Cube cube); | |||
| 30 | static uint64_t index_htrfin(Cube cube); | 15 | static uint64_t index_htrfin(Cube cube); |
| 31 | static uint64_t index_cpud_separate(Cube cube); | 16 | static uint64_t index_cpud_separate(Cube cube); |
| 32 | 17 | ||
| 18 | static uint64_t move_eofb(Move m, uint64_t ind); | ||
| 19 | static uint64_t move_eofbepos(Move m, uint64_t ind); | ||
| 20 | static uint64_t move_epud(Move m, uint64_t ind); | ||
| 21 | static uint64_t move_coud(Move m, uint64_t ind); | ||
| 22 | static uint64_t move_corners(Move m, uint64_t ind); | ||
| 23 | static uint64_t move_cp(Move m, uint64_t ind); | ||
| 24 | static uint64_t move_cphtr(Move m, uint64_t ind); | ||
| 25 | static uint64_t move_cornershtr(Move m, uint64_t ind); | ||
| 26 | static uint64_t move_cornershtrfin(Move m, uint64_t ind); | ||
| 27 | static uint64_t move_drud(Move m, uint64_t ind); | ||
| 28 | static uint64_t move_drud_eofb(Move m, uint64_t ind); | ||
| 29 | static uint64_t move_htr_drud(Move m, uint64_t ind); | ||
| 30 | static uint64_t move_htrfin(Move m, uint64_t ind); | ||
| 31 | static uint64_t move_cpud_separate(Move m, uint64_t ind); | ||
| 32 | |||
| 33 | static void init_cphtr_cosets(); | 33 | static void init_cphtr_cosets(); |
| 34 | static void init_cphtr_left_cosets_bfs(int i, int c); | 34 | static void init_cphtr_left_cosets_bfs(int i, int c); |
| 35 | static void init_cphtr_right_cosets_color(int i, int c); | 35 | static void init_cphtr_right_cosets_color(int i, int c); |
| 36 | static void init_cpud_separate(); | ||
| 36 | static void init_cornershtrfin(); | 37 | static void init_cornershtrfin(); |
| 38 | static void init_htr_eposs(); | ||
| 37 | 39 | ||
| 38 | 40 | ||
| 39 | /* All sorts of useful costants and tables **********************************/ | 41 | /* All sorts of useful costants and tables **********************************/ |
| @@ -41,497 +43,408 @@ static void init_cornershtrfin(); | |||
| 41 | static int cphtr_left_cosets[FACTORIAL8]; | 43 | static int cphtr_left_cosets[FACTORIAL8]; |
| 42 | static int cphtr_right_cosets[FACTORIAL8]; | 44 | static int cphtr_right_cosets[FACTORIAL8]; |
| 43 | static int cphtr_right_rep[BINOM8ON4*6]; | 45 | static int cphtr_right_rep[BINOM8ON4*6]; |
| 46 | int cpud_separate_ind[FACTORIAL8]; | ||
| 47 | int cpud_separate_ant[BINOM8ON4]; | ||
| 44 | static int cornershtrfin_ind[FACTORIAL8]; | 48 | static int cornershtrfin_ind[FACTORIAL8]; |
| 45 | static int cornershtrfin_ant[24*24/6]; | 49 | int cornershtrfin_ant[24*24/6]; |
| 50 | static int htr_eposs_ind[BINOM12ON4]; | ||
| 51 | static int htr_eposs_ant[BINOM8ON4]; | ||
| 46 | 52 | ||
| 47 | /* Coordinates and their implementation **************************************/ | 53 | /* Coordinates and their implementation **************************************/ |
| 48 | 54 | ||
| 49 | Coordinate | 55 | Coordinate |
| 50 | coord_eofb = { | 56 | coord_eofb = { |
| 51 | .index = index_eofb, | 57 | .index = index_eofb, |
| 52 | .cube = antindex_eofb, | ||
| 53 | .max = POW2TO11, | 58 | .max = POW2TO11, |
| 59 | .move = move_eofb, | ||
| 54 | }; | 60 | }; |
| 55 | 61 | ||
| 56 | Coordinate | 62 | Coordinate |
| 57 | coord_eofbepos = { | 63 | coord_eofbepos = { |
| 58 | .index = index_eofbepos, | 64 | .index = index_eofbepos, |
| 59 | .cube = antindex_eofbepos, | ||
| 60 | .max = POW2TO11 * BINOM12ON4, | 65 | .max = POW2TO11 * BINOM12ON4, |
| 66 | .move = move_eofbepos, | ||
| 61 | }; | 67 | }; |
| 62 | 68 | ||
| 63 | Coordinate | 69 | Coordinate |
| 64 | coord_coud = { | 70 | coord_coud = { |
| 65 | .index = index_coud, | 71 | .index = index_coud, |
| 66 | .cube = antindex_coud, | ||
| 67 | .max = POW3TO7, | 72 | .max = POW3TO7, |
| 73 | .move = move_coud, | ||
| 68 | }; | 74 | }; |
| 69 | 75 | ||
| 70 | Coordinate | 76 | Coordinate |
| 71 | coord_corners = { | 77 | coord_corners = { |
| 72 | .index = index_corners, | 78 | .index = index_corners, |
| 73 | .cube = antindex_corners, | ||
| 74 | .max = POW3TO7 * FACTORIAL8, | 79 | .max = POW3TO7 * FACTORIAL8, |
| 80 | .move = move_corners, | ||
| 75 | }; | 81 | }; |
| 76 | 82 | ||
| 77 | Coordinate | 83 | Coordinate |
| 78 | coord_cp = { | 84 | coord_cp = { |
| 79 | .index = index_cp, | 85 | .index = index_cp, |
| 80 | .cube = antindex_cp, | ||
| 81 | .max = FACTORIAL8, | 86 | .max = FACTORIAL8, |
| 87 | .move = move_cp, | ||
| 82 | }; | 88 | }; |
| 83 | 89 | ||
| 84 | Coordinate | 90 | Coordinate |
| 85 | coord_cphtr = { | 91 | coord_cphtr = { |
| 86 | .index = index_cphtr, | 92 | .index = index_cphtr, |
| 87 | .cube = antindex_cphtr, | ||
| 88 | .max = BINOM8ON4 * 6, | 93 | .max = BINOM8ON4 * 6, |
| 94 | .move = move_cphtr, | ||
| 89 | }; | 95 | }; |
| 90 | 96 | ||
| 91 | Coordinate | 97 | Coordinate |
| 92 | coord_cornershtr = { | 98 | coord_cornershtr = { |
| 93 | .index = index_cornershtr, | 99 | .index = index_cornershtr, |
| 94 | .cube = antindex_cornershtr, | ||
| 95 | .max = POW3TO7 * BINOM8ON4 * 6, | 100 | .max = POW3TO7 * BINOM8ON4 * 6, |
| 101 | .move = move_cornershtr, | ||
| 96 | }; | 102 | }; |
| 97 | 103 | ||
| 98 | Coordinate | 104 | Coordinate |
| 99 | coord_cornershtrfin = { | 105 | coord_cornershtrfin = { |
| 100 | .index = index_cornershtrfin, | 106 | .index = index_cornershtrfin, |
| 101 | .cube = antindex_cornershtrfin, | ||
| 102 | .max = 24*24/6, | 107 | .max = 24*24/6, |
| 108 | .move = move_cornershtrfin, | ||
| 103 | }; | 109 | }; |
| 104 | 110 | ||
| 105 | Coordinate | 111 | Coordinate |
| 106 | coord_epud = { | 112 | coord_epud = { |
| 107 | .index = index_epud, | 113 | .index = index_epud, |
| 108 | .cube = antindex_epud, | ||
| 109 | .max = FACTORIAL8, | 114 | .max = FACTORIAL8, |
| 115 | .move = move_epud, | ||
| 110 | }; | 116 | }; |
| 111 | 117 | ||
| 112 | Coordinate | 118 | Coordinate |
| 113 | coord_drud = { | 119 | coord_drud = { |
| 114 | .index = index_drud, | 120 | .index = index_drud, |
| 115 | .cube = antindex_drud, | ||
| 116 | .max = POW2TO11 * POW3TO7 * BINOM12ON4, | 121 | .max = POW2TO11 * POW3TO7 * BINOM12ON4, |
| 122 | .move = move_drud, | ||
| 117 | }; | 123 | }; |
| 118 | 124 | ||
| 119 | Coordinate | 125 | Coordinate |
| 120 | coord_htr_drud = { | 126 | coord_htr_drud = { |
| 121 | .index = index_htr_drud, | 127 | .index = index_htr_drud, |
| 122 | .cube = antindex_htr_drud, | ||
| 123 | .max = BINOM8ON4 * 6 * BINOM8ON4, | 128 | .max = BINOM8ON4 * 6 * BINOM8ON4, |
| 129 | .move = move_htr_drud, | ||
| 124 | }; | 130 | }; |
| 125 | 131 | ||
| 126 | Coordinate | 132 | Coordinate |
| 127 | coord_htrfin = { | 133 | coord_htrfin = { |
| 128 | .index = index_htrfin, | 134 | .index = index_htrfin, |
| 129 | .cube = antindex_htrfin, | ||
| 130 | .max = 24 * 24 * 24 *24 * 24 / 6, /* should be /12 but it's ok */ | 135 | .max = 24 * 24 * 24 *24 * 24 / 6, /* should be /12 but it's ok */ |
| 136 | .move = move_htrfin, | ||
| 131 | }; | 137 | }; |
| 132 | 138 | ||
| 133 | Coordinate | 139 | Coordinate |
| 134 | coord_drud_eofb = { | 140 | coord_drud_eofb = { |
| 135 | .index = index_drud_eofb, | 141 | .index = index_drud_eofb, |
| 136 | .cube = antindex_drud_eofb, | ||
| 137 | .max = POW3TO7 * BINOM12ON4, | 142 | .max = POW3TO7 * BINOM12ON4, |
| 143 | .move = move_drud_eofb, | ||
| 138 | }; | 144 | }; |
| 139 | 145 | ||
| 140 | Coordinate | 146 | Coordinate |
| 141 | coord_cpud_separate = { | 147 | coord_cpud_separate = { |
| 142 | .index = index_cpud_separate, | 148 | .index = index_cpud_separate, |
| 143 | .cube = antindex_cpud_separate, | ||
| 144 | .max = BINOM8ON4, | 149 | .max = BINOM8ON4, |
| 150 | .move = move_cpud_separate, | ||
| 145 | }; | 151 | }; |
| 146 | 152 | ||
| 147 | /* Antindexers ***************************************************************/ | 153 | /* Indexers ******************************************************************/ |
| 148 | 154 | ||
| 149 | static Cube | 155 | static uint64_t |
| 150 | antindex_eofb(uint64_t ind) | 156 | index_eofb(Cube cube) |
| 151 | { | 157 | { |
| 152 | /* The returned cube is consistent */ | 158 | return cube.eofb; |
| 153 | Cube ret = {0}; | ||
| 154 | |||
| 155 | ret.eofb = ind; | ||
| 156 | ret.eorl = ind; | ||
| 157 | ret.eoud = ind; | ||
| 158 | |||
| 159 | return ret; | ||
| 160 | } | 159 | } |
| 161 | 160 | ||
| 162 | static Cube | 161 | static uint64_t |
| 163 | antindex_eofbepos(uint64_t ind) | 162 | index_eofbepos(Cube cube) |
| 164 | { | 163 | { |
| 165 | /* The returned cube is NOT consistent: eoud can be wrong */ | 164 | return (cube.epose / FACTORIAL4) * POW2TO11 + cube.eofb; |
| 166 | Cube ret = {0}; | 165 | } |
| 167 | |||
| 168 | /* We need eorl for sym16 coordinate */ | ||
| 169 | static int initialized = false; | ||
| 170 | static uint64_t eorl_a[POW2TO11][BINOM12ON4]; | ||
| 171 | static int eo_aux[12], ep_aux[12]; | ||
| 172 | unsigned int i, j, k; | ||
| 173 | |||
| 174 | if (!initialized) { | ||
| 175 | for (i = 0; i < POW2TO11; i++) { | ||
| 176 | for (j = 0; j < BINOM12ON4; j++) { | ||
| 177 | int_to_sum_zero_array(i, 2, 12, eo_aux); | ||
| 178 | index_to_subset(j, 12, 4, ep_aux); | ||
| 179 | for (k = 0; k < 12; k++) | ||
| 180 | if ((ep_aux[k] && k < FR) || | ||
| 181 | (!ep_aux[k] && k >= FR)) | ||
| 182 | eo_aux[k] = 1 - eo_aux[k]; | ||
| 183 | eorl_a[i][j] = digit_array_to_int(eo_aux,11,2); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | 166 | ||
| 187 | initialized = true; | 167 | static uint64_t |
| 188 | } | 168 | index_epud(Cube cube) |
| 169 | { | ||
| 170 | uint64_t ret; | ||
| 171 | CubeArray *arr = new_cubearray(cube, pf_ep); | ||
| 189 | 172 | ||
| 190 | ret.eofb = ind % POW2TO11; | 173 | ret = perm_to_index(arr->ep, 8); |
| 191 | ret.epose = (ind / POW2TO11) * 24; | 174 | free_cubearray(arr, pf_ep); |
| 192 | ret.eorl = eorl_a[ret.eofb][ret.epose/24]; | ||
| 193 | 175 | ||
| 194 | return ret; | 176 | return ret; |
| 195 | } | 177 | } |
| 196 | 178 | ||
| 197 | static Cube | 179 | static uint64_t |
| 198 | antindex_epud(uint64_t ind) | 180 | index_coud(Cube cube) |
| 199 | { | 181 | { |
| 200 | /* The returned cube is consistent */ | 182 | return cube.coud; |
| 201 | static bool initialized = false; | ||
| 202 | static Cube epud_aux[FACTORIAL8]; | ||
| 203 | int a[12]; | ||
| 204 | uint64_t ui; | ||
| 205 | CubeArray arr; | ||
| 206 | |||
| 207 | if (!initialized) { | ||
| 208 | a[FR] = FR; | ||
| 209 | a[FL] = FL; | ||
| 210 | a[BL] = BL; | ||
| 211 | a[BR] = BR; | ||
| 212 | for (ui = 0; ui < FACTORIAL8; ui++) { | ||
| 213 | index_to_perm(ui, 8, a); | ||
| 214 | arr.ep = a; | ||
| 215 | epud_aux[ui] = arrays_to_cube(&arr, pf_ep); | ||
| 216 | } | ||
| 217 | |||
| 218 | initialized = true; | ||
| 219 | } | ||
| 220 | |||
| 221 | return epud_aux[ind]; | ||
| 222 | } | 183 | } |
| 223 | 184 | ||
| 224 | static Cube | 185 | static uint64_t |
| 225 | antindex_coud(uint64_t ind) | 186 | index_corners(Cube cube) |
| 226 | { | 187 | { |
| 227 | /* The returned cube is consistent */ | 188 | return cube.coud * FACTORIAL8 + cube.cp; |
| 228 | Cube ret = {0}; | ||
| 229 | |||
| 230 | ret.coud = ind; | ||
| 231 | ret.corl = ind; | ||
| 232 | ret.cofb = ind; | ||
| 233 | |||
| 234 | return ret; | ||
| 235 | } | 189 | } |
| 236 | 190 | ||
| 237 | static Cube | 191 | static uint64_t |
| 238 | antindex_corners(uint64_t ind) | 192 | index_cp(Cube cube) |
| 239 | { | 193 | { |
| 240 | /* The returned cube is NOT consistent: corl and cofb can be wrong */ | 194 | return cube.cp; |
| 241 | /* TODO: remember to make this consistent if I use this for symcoord */ | ||
| 242 | Cube ret = {0}; | ||
| 243 | |||
| 244 | ret.coud = ind / FACTORIAL8; | ||
| 245 | ret.cp = ind % FACTORIAL8; | ||
| 246 | |||
| 247 | return ret; | ||
| 248 | } | 195 | } |
| 249 | 196 | ||
| 250 | static Cube | 197 | static uint64_t |
| 251 | antindex_cp(uint64_t ind) | 198 | index_cphtr(Cube cube) |
| 252 | { | 199 | { |
| 253 | /* The returned cube is NOT consistent: co can be wrong in all axes */ | 200 | return cphtr_right_cosets[cube.cp]; |
| 254 | Cube ret = {0}; | 201 | } |
| 255 | 202 | ||
| 256 | ret.cp = ind; | 203 | static uint64_t |
| 204 | index_cornershtr(Cube cube) | ||
| 205 | { | ||
| 206 | return cube.coud * BINOM8ON4 * 6 + index_cphtr(cube); | ||
| 207 | } | ||
| 257 | 208 | ||
| 258 | return ret; | 209 | static uint64_t |
| 210 | index_cornershtrfin(Cube cube) | ||
| 211 | { | ||
| 212 | return cornershtrfin_ind[cube.cp]; | ||
| 259 | } | 213 | } |
| 260 | 214 | ||
| 261 | static Cube | 215 | static uint64_t |
| 262 | antindex_cphtr(uint64_t ind) | 216 | index_drud(Cube cube) |
| 263 | { | 217 | { |
| 264 | /* The returned cube is NOT consistent: co can be wrong in all axes */ | 218 | uint64_t a, b, c; |
| 265 | Cube ret = {0}; | ||
| 266 | 219 | ||
| 267 | ret.cp = cphtr_right_rep[ind]; | 220 | a = cube.eofb; |
| 221 | b = cube.coud; | ||
| 222 | c = cube.epose / FACTORIAL4; | ||
| 268 | 223 | ||
| 269 | return ret; | 224 | b *= POW2TO11; |
| 225 | c *= POW2TO11 * POW3TO7; | ||
| 226 | |||
| 227 | return a + b + c; | ||
| 270 | } | 228 | } |
| 271 | 229 | ||
| 272 | static Cube | 230 | static uint64_t |
| 273 | antindex_cornershtr(uint64_t ind) | 231 | index_drud_eofb(Cube cube) |
| 274 | { | 232 | { |
| 275 | /* The returned cube is NOT consistent: corl and cofb can be wrong */ | 233 | return index_drud(cube) / POW2TO11; |
| 276 | Cube ret = antindex_cphtr(ind % (BINOM8ON4 * 6)); | ||
| 277 | |||
| 278 | ret.coud = ind / (BINOM8ON4 * 6); | ||
| 279 | |||
| 280 | return ret; | ||
| 281 | } | 234 | } |
| 282 | 235 | ||
| 283 | static Cube | 236 | static uint64_t |
| 284 | antindex_cornershtrfin(uint64_t ind) | 237 | index_htr_drud(Cube cube) |
| 285 | { | 238 | { |
| 286 | /* The returned cube is consistent */ | 239 | uint64_t a, b; |
| 287 | Cube ret = {0}; | ||
| 288 | 240 | ||
| 289 | ret.cp = cornershtrfin_ant[ind]; | 241 | a = index_cphtr(cube); |
| 242 | b = htr_eposs_ind[cube.eposs/24]; | ||
| 290 | 243 | ||
| 291 | return ret; | 244 | return a * BINOM8ON4 + b; |
| 292 | } | 245 | } |
| 293 | 246 | ||
| 294 | static Cube | 247 | static uint64_t |
| 295 | antindex_drud(uint64_t ind) | 248 | index_htrfin(Cube cube) |
| 296 | { | 249 | { |
| 297 | /* The returned cube is NOT consistent in the same way as eofbepos */ | 250 | uint64_t epe, eps, epm, cp, ep; |
| 298 | /* (see above). It works with sym16 coordinates */ | ||
| 299 | uint64_t epos, eofb; | ||
| 300 | Cube ret = {0}; | ||
| 301 | |||
| 302 | eofb = ind % POW2TO11; | ||
| 303 | epos = ind / (POW2TO11 * POW3TO7); | ||
| 304 | ret = antindex_eofbepos(eofb + POW2TO11 * epos); | ||
| 305 | 251 | ||
| 306 | ret.coud = (ind / POW2TO11) % POW3TO7; | 252 | epe = cube.epose % 24; |
| 307 | ret.corl = ret.coud; | 253 | eps = cube.eposs % 24; |
| 308 | ret.cofb = ret.coud; | 254 | epm = cube.eposm % 24; |
| 255 | ep = (epe * 24 + eps) *24 + epm; | ||
| 256 | cp = index_cornershtrfin(cube); | ||
| 309 | 257 | ||
| 310 | return ret; | 258 | return cp * 24 * 24 * 24 + ep; |
| 311 | } | 259 | } |
| 312 | 260 | ||
| 313 | static Cube | 261 | static uint64_t |
| 314 | antindex_drud_eofb(uint64_t ind) | 262 | index_cpud_separate(Cube cube) |
| 315 | { | 263 | { |
| 316 | /* The returned cube is NOT consistent (see antindex_drud) */ | 264 | return cpud_separate_ind[cube.cp]; |
| 317 | return antindex_drud(ind * POW2TO11); | ||
| 318 | } | 265 | } |
| 319 | 266 | ||
| 320 | static Cube | 267 | /* Coordinate movers *********************************************************/ |
| 321 | antindex_htr_drud(uint64_t ind) | ||
| 322 | { | ||
| 323 | /* The returned cube is NOT consistent: corl and cofb can be wrong */ | ||
| 324 | /* (see cphtr) and eposm can be wrong too (not epose because dr). */ | ||
| 325 | Cube ret = {0}; | ||
| 326 | static bool initialized = false; | ||
| 327 | static int aux[BINOM8ON4], ep[12], ep2[12]; | ||
| 328 | static int eps_solved[4] = {UL, UR, DL, DR}; | ||
| 329 | unsigned int i, j, k; | ||
| 330 | 268 | ||
| 331 | if (!initialized) { | 269 | static uint64_t |
| 332 | for (i = 0; i < BINOM8ON4; i++) { | 270 | move_eofb(Move m, uint64_t ind) |
| 333 | for (j = 0; j < 12; j++) | 271 | { |
| 334 | ep[j] = ep2[j] = 0; | 272 | return eofb_mtable[m][ind]; |
| 335 | index_to_subset(i, 8, 4, ep); | ||
| 336 | for (j = 0, k = 0; j < 8; j++) | ||
| 337 | ep2[j] = ep[j/2+4*(j%2)] ? eps_solved[k++] : 0; | ||
| 338 | aux[i] = array_ep_to_epos(ep2, eps_solved); | ||
| 339 | } | ||
| 340 | |||
| 341 | initialized = true; | ||
| 342 | } | ||
| 343 | |||
| 344 | ret = antindex_cphtr(ind / BINOM8ON4); | ||
| 345 | ret.epose = 0; | ||
| 346 | ret.eposs = aux[ind % BINOM8ON4]; | ||
| 347 | |||
| 348 | return ret; | ||
| 349 | } | 273 | } |
| 350 | 274 | ||
| 351 | static Cube | 275 | static uint64_t |
| 352 | antindex_htrfin(uint64_t ind) | 276 | move_eofbepos(Move m, uint64_t ind) |
| 353 | { | 277 | { |
| 354 | /* The returned cube is consistent */ | 278 | uint64_t a, b; |
| 355 | Cube ret = {0}; | ||
| 356 | |||
| 357 | ret = antindex_cornershtrfin(ind/(24*24*24)); | ||
| 358 | 279 | ||
| 359 | ret.eposm = ind % 24; | 280 | a = epose_mtable[m][(ind / POW2TO11)*24]; |
| 360 | ind /= 24; | 281 | b = eofb_mtable[m][ind % POW2TO11]; |
| 361 | ret.eposs = ind % 24; | ||
| 362 | ind /= 24; | ||
| 363 | ret.epose = ind % 24; | ||
| 364 | 282 | ||
| 365 | return ret; | 283 | return a/24 + b; |
| 366 | } | 284 | } |
| 367 | 285 | ||
| 368 | static Cube | 286 | static uint64_t |
| 369 | antindex_cpud_separate(uint64_t ind) | 287 | move_epud(Move m, uint64_t ind) |
| 370 | { | 288 | { |
| 371 | /* Not consistent because of side corner orientations and cp */ | 289 | /* TODO: save to file? */ |
| 372 | unsigned int ui; | ||
| 373 | int i, co[8], cp[8]; | ||
| 374 | Corner u, d; | ||
| 375 | |||
| 376 | static Cube aux[BINOM8ON4]; | ||
| 377 | static bool initialized = false; | 290 | static bool initialized = false; |
| 291 | static int a[12] = { [8] = 8, [9] = 9, [10] = 10, [11] = 11 }; | ||
| 292 | static int shortlist[NMOVES] = { | ||
| 293 | [U] = 0, [U2] = 1, [U3] = 2, [D] = 3, [D2] = 4, [D3] = 5, | ||
| 294 | [R2] = 6, [L2] = 7, [F2] = 8, [B2] = 9 | ||
| 295 | }; | ||
| 296 | static uint64_t aux[10][FACTORIAL8]; | ||
| 297 | uint64_t ui; | ||
| 298 | int j; | ||
| 299 | Move mj; | ||
| 300 | Cube c; | ||
| 301 | CubeArray *arr, *auxarr; | ||
| 302 | |||
| 303 | if (!moveset_drud.allowed(m)) { | ||
| 304 | fprintf(stderr, "Move not allowed for epud\n" | ||
| 305 | "This is a bug, please report\n"); | ||
| 306 | return coord_epud.max; | ||
| 307 | } | ||
| 378 | 308 | ||
| 379 | if (!initialized) { | 309 | if (!initialized) { |
| 380 | for (ui = 0; ui < BINOM8ON4; ui++) { | 310 | auxarr = malloc(sizeof(CubeArray)); |
| 381 | index_to_subset(ui, 8, 4, co); | 311 | auxarr->ep = a; |
| 382 | for (i = 0, u = UFR, d = DFR; i < 8; i++) | 312 | for (ui = 0; ui < coord_epud.max; ui++) { |
| 383 | cp[i] = co[i] ? d++ : u++; | 313 | index_to_perm(ui, 8, a); |
| 384 | aux[ui] = (Cube){.cp = perm_to_index(cp, 8)}; | 314 | c = arrays_to_cube(auxarr, pf_ep); |
| 315 | for (j = 0; moveset_drud.sorted_moves[j] != NULLMOVE; | ||
| 316 | j++) { | ||
| 317 | mj = moveset_drud.sorted_moves[j]; | ||
| 318 | arr = new_cubearray(apply_move(mj, c), pf_ep); | ||
| 319 | aux[shortlist[mj]][ui] = | ||
| 320 | perm_to_index(arr->ep, 8); | ||
| 321 | free_cubearray(arr, pf_ep); | ||
| 322 | } | ||
| 385 | } | 323 | } |
| 324 | free(auxarr); | ||
| 386 | 325 | ||
| 387 | initialized = true; | 326 | initialized = true; |
| 388 | } | 327 | } |
| 389 | 328 | ||
| 390 | return aux[ind]; | 329 | return aux[shortlist[m]][ind]; |
| 391 | } | ||
| 392 | |||
| 393 | /* Indexers ******************************************************************/ | ||
| 394 | |||
| 395 | static uint64_t | ||
| 396 | index_eofb(Cube cube) | ||
| 397 | { | ||
| 398 | return cube.eofb; | ||
| 399 | } | 330 | } |
| 400 | 331 | ||
| 401 | static uint64_t | 332 | static uint64_t |
| 402 | index_eofbepos(Cube cube) | 333 | move_coud(Move m, uint64_t ind) |
| 403 | { | 334 | { |
| 404 | return (cube.epose / FACTORIAL4) * POW2TO11 + cube.eofb; | 335 | return coud_mtable[m][ind]; |
| 405 | } | 336 | } |
| 406 | 337 | ||
| 407 | static uint64_t | 338 | static uint64_t |
| 408 | index_epud(Cube cube) | 339 | move_corners(Move m, uint64_t ind) |
| 409 | { | 340 | { |
| 410 | uint64_t ret; | 341 | uint64_t a, b; |
| 411 | CubeArray *arr = new_cubearray(cube, pf_ep); | ||
| 412 | 342 | ||
| 413 | ret = perm_to_index(arr->ep, 8); | 343 | a = coud_mtable[m][ind / FACTORIAL8]; |
| 414 | free_cubearray(arr, pf_ep); | 344 | b = cp_mtable[m][ind % FACTORIAL8]; |
| 415 | 345 | ||
| 416 | return ret; | 346 | return a * FACTORIAL8 + b; |
| 417 | } | 347 | } |
| 418 | 348 | ||
| 419 | static uint64_t | 349 | static uint64_t |
| 420 | index_coud(Cube cube) | 350 | move_cp(Move m, uint64_t ind) |
| 421 | { | 351 | { |
| 422 | return cube.coud; | 352 | return cp_mtable[m][ind]; |
| 423 | } | 353 | } |
| 424 | 354 | ||
| 425 | static uint64_t | 355 | static uint64_t |
| 426 | index_corners(Cube cube) | 356 | move_cphtr(Move m, uint64_t ind) |
| 427 | { | 357 | { |
| 428 | return cube.coud * FACTORIAL8 + cube.cp; | 358 | static bool initialized = false; |
| 429 | } | 359 | static uint64_t aux[NMOVES][BINOM8ON4*6]; |
| 360 | uint64_t ui; | ||
| 361 | Move j; | ||
| 430 | 362 | ||
| 431 | static uint64_t | 363 | if (!initialized) { |
| 432 | index_cp(Cube cube) | 364 | for (ui = 0; ui < BINOM8ON4*6; ui++) |
| 433 | { | 365 | for (j = U; j < NMOVES; j++) |
| 434 | return cube.cp; | 366 | aux[j][ui] = |
| 435 | } | 367 | cp_mtable[j][cphtr_right_rep[ind]]; |
| 436 | 368 | ||
| 437 | static uint64_t | 369 | initialized = true; |
| 438 | index_cphtr(Cube cube) | 370 | } |
| 439 | { | 371 | |
| 440 | return cphtr_right_cosets[cube.cp]; | 372 | return aux[m][ind]; |
| 441 | } | 373 | } |
| 442 | 374 | ||
| 443 | static uint64_t | 375 | static uint64_t |
| 444 | index_cornershtr(Cube cube) | 376 | move_cornershtr(Move m, uint64_t ind) |
| 445 | { | 377 | { |
| 446 | return cube.coud * BINOM8ON4 * 6 + index_cphtr(cube); | 378 | uint64_t a, b; |
| 379 | |||
| 380 | a = coud_mtable[m][ind/(BINOM8ON4 * 6)]; | ||
| 381 | b = move_cphtr(m, ind % (BINOM8ON4 * 6)); | ||
| 382 | |||
| 383 | return a * BINOM8ON4 * 6 + b; | ||
| 447 | } | 384 | } |
| 448 | 385 | ||
| 449 | static uint64_t | 386 | static uint64_t |
| 450 | index_cornershtrfin(Cube cube) | 387 | move_cornershtrfin(Move m, uint64_t ind) |
| 451 | { | 388 | { |
| 452 | return cornershtrfin_ind[cube.cp]; | 389 | int a; |
| 390 | |||
| 391 | a = cp_mtable[m][cornershtrfin_ant[ind]]; | ||
| 392 | |||
| 393 | return cornershtrfin_ind[a]; | ||
| 453 | } | 394 | } |
| 454 | 395 | ||
| 455 | static uint64_t | 396 | static uint64_t |
| 456 | index_drud(Cube cube) | 397 | move_drud(Move m, uint64_t ind) |
| 457 | { | 398 | { |
| 458 | uint64_t a, b, c; | 399 | uint64_t a, b, c; |
| 459 | 400 | ||
| 460 | a = cube.eofb; | 401 | a = eofb_mtable[m][ind % POW2TO11]; |
| 461 | b = cube.coud; | 402 | b = coud_mtable[m][(ind / POW2TO11) % POW3TO7]; |
| 462 | c = cube.epose / FACTORIAL4; | 403 | c = epose_mtable[m][ind / (POW2TO11 * POW3TO7)]; |
| 463 | |||
| 464 | b *= POW2TO11; | ||
| 465 | c *= POW2TO11 * POW3TO7; | ||
| 466 | 404 | ||
| 467 | return a + b + c; | 405 | return a + (b + c * POW3TO7) * POW2TO11; |
| 468 | } | 406 | } |
| 469 | 407 | ||
| 470 | static uint64_t | 408 | static uint64_t |
| 471 | index_drud_eofb(Cube cube) | 409 | move_drud_eofb(Move m, uint64_t ind) |
| 472 | { | 410 | { |
| 473 | return index_drud(cube) / POW2TO11; | 411 | uint64_t a, b; |
| 412 | |||
| 413 | a = coud_mtable[m][ind % POW3TO7]; | ||
| 414 | b = epose_mtable[m][ind / POW3TO7]; | ||
| 415 | |||
| 416 | return a + b * POW3TO7; | ||
| 474 | } | 417 | } |
| 475 | 418 | ||
| 476 | static uint64_t | 419 | static uint64_t |
| 477 | index_htr_drud(Cube cube) | 420 | move_htr_drud(Move m, uint64_t ind) |
| 478 | { | 421 | { |
| 479 | static bool initialized = false; | 422 | uint64_t a, b; |
| 480 | static int aux[BINOM12ON4], ep[12], ep2[12]; | ||
| 481 | static int eps_solved[4] = {UL, UR, DL, DR}; | ||
| 482 | unsigned int i, j; | ||
| 483 | 423 | ||
| 484 | if (!initialized) { | 424 | a = move_cphtr(m, ind/BINOM8ON4); |
| 485 | for (i = 0; i < BINOM12ON4; i++) { | 425 | b = eposs_mtable[m][htr_eposs_ant[ind%BINOM8ON4]]; |
| 486 | for (j = 0; j < 12; j++) | ||
| 487 | ep[j] = ep2[j] = 0; | ||
| 488 | epos_to_partial_ep(i*24, ep, eps_solved); | ||
| 489 | for (j = 0; j < 8; j++) | ||
| 490 | ep2[j/2 + 4*(j%2)] = ep[j] ? 1 : 0; | ||
| 491 | aux[i] = subset_to_index(ep2, 8, 4); | ||
| 492 | } | ||
| 493 | 426 | ||
| 494 | initialized = true; | 427 | return a*BINOM8ON4 + htr_eposs_ind[b/24]; |
| 495 | } | ||
| 496 | |||
| 497 | return index_cphtr(cube) * BINOM8ON4 + aux[cube.eposs/24]; | ||
| 498 | } | 428 | } |
| 499 | 429 | ||
| 500 | static uint64_t | 430 | static uint64_t |
| 501 | index_htrfin(Cube cube) | 431 | move_htrfin(Move m, uint64_t ind) |
| 502 | { | 432 | { |
| 503 | uint64_t epe, eps, epm, cp, ep; | 433 | uint64_t a, b, bm, bs, be; |
| 504 | 434 | ||
| 505 | epe = cube.epose % 24; | 435 | a = move_cornershtrfin(m, ind % (24*24*24)); |
| 506 | eps = cube.eposs % 24; | 436 | bm = eposm_mtable[m][ind%24] % 24; |
| 507 | epm = cube.eposm % 24; | 437 | bs = eposs_mtable[m][(ind/24)%24] % 24; |
| 508 | ep = (epe * 24 + eps) *24 + epm; | 438 | be = epose_mtable[m][ind/(24*24)] % 24; |
| 509 | cp = index_cornershtrfin(cube); | 439 | b = (be * 24 + bs) * 24 + bm; |
| 510 | 440 | ||
| 511 | return cp * 24 * 24 * 24 + ep; | 441 | return a * (24*24*24) + b; |
| 512 | } | 442 | } |
| 513 | 443 | ||
| 514 | static uint64_t | 444 | static uint64_t |
| 515 | index_cpud_separate(Cube cube) | 445 | move_cpud_separate(Move m, uint64_t ind) |
| 516 | { | 446 | { |
| 517 | unsigned int ui; | 447 | return cpud_separate_ind[cp_mtable[m][cpud_separate_ant[ind]]]; |
| 518 | int i, co[8]; | ||
| 519 | |||
| 520 | static int aux[FACTORIAL8]; | ||
| 521 | static bool initialized = false; | ||
| 522 | |||
| 523 | if (!initialized) { | ||
| 524 | for (ui = 0; ui < FACTORIAL8; ui++) { | ||
| 525 | for (i = 0; i < 8; i++) | ||
| 526 | co[i] = what_corner_at((Cube){.cp=ui},i)>UBR ? | ||
| 527 | 1 : 0; | ||
| 528 | aux[ui] = subset_to_index(co, 8, 4); | ||
| 529 | } | ||
| 530 | |||
| 531 | initialized = true; | ||
| 532 | } | ||
| 533 | |||
| 534 | return aux[cube.cp]; | ||
| 535 | } | 448 | } |
| 536 | 449 | ||
| 537 | /* Init functions implementation *********************************************/ | 450 | /* Init functions implementation *********************************************/ |
| @@ -618,6 +531,20 @@ init_cphtr_right_cosets_color(int i, int d) | |||
| 618 | } | 531 | } |
| 619 | 532 | ||
| 620 | static void | 533 | static void |
| 534 | init_cpud_separate() | ||
| 535 | { | ||
| 536 | unsigned int ui; | ||
| 537 | int i, co[8]; | ||
| 538 | |||
| 539 | for (ui = 0; ui < FACTORIAL8; ui++) { | ||
| 540 | for (i = 0; i < 8; i++) | ||
| 541 | co[i] = what_corner_at((Cube){.cp=ui},i)>UBR ? 1 : 0; | ||
| 542 | cpud_separate_ind[ui] = subset_to_index(co, 8, 4); | ||
| 543 | cpud_separate_ant[cpud_separate_ind[ui]] = ui; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | |||
| 547 | static void | ||
| 621 | init_cornershtrfin() | 548 | init_cornershtrfin() |
| 622 | { | 549 | { |
| 623 | unsigned int i, j; | 550 | unsigned int i, j; |
| @@ -649,28 +576,21 @@ init_cornershtrfin() | |||
| 649 | } | 576 | } |
| 650 | 577 | ||
| 651 | void | 578 | void |
| 652 | test_coord(Coordinate *coord) | 579 | init_htr_eposs() |
| 653 | { | 580 | { |
| 654 | bool passed; | 581 | int ep[12], ep2[12]; |
| 655 | uint64_t ui, failcount; | 582 | int eps_solved[4] = {UL, UR, DL, DR}; |
| 656 | 583 | unsigned int i, j; | |
| 657 | if (!(passed = (coord->index((Cube){0}) == 0))) { | ||
| 658 | printf("Failed: coordinate of solved cube is " | ||
| 659 | "%" PRIu64 "\n", coord->index((Cube){0})); | ||
| 660 | } | ||
| 661 | 584 | ||
| 662 | printf("Testing %" PRIu64 " coordinates\n", coord->max); | 585 | for (i = 0; i < BINOM12ON4; i++) { |
| 663 | for (failcount = 0, ui = 0; ui < coord->max; ui++) { | 586 | for (j = 0; j < 12; j++) |
| 664 | if (!(passed = (coord->index(coord->cube(ui)) == ui))) { | 587 | ep[j] = ep2[j] = 0; |
| 665 | printf("Failed at %" PRIu64 "\n", ui); | 588 | epos_to_partial_ep(i*24, ep, eps_solved); |
| 666 | failcount++; | 589 | for (j = 0; j < 8; j++) |
| 667 | } | 590 | ep2[j/2 + 4*(j%2)] = ep[j] ? 1 : 0; |
| 591 | htr_eposs_ind[i] = subset_to_index(ep2, 8, 4); | ||
| 592 | htr_eposs_ant[htr_eposs_ind[i]] = i*24; | ||
| 668 | } | 593 | } |
| 669 | |||
| 670 | if (passed) | ||
| 671 | printf("Ok\n"); | ||
| 672 | else | ||
| 673 | printf("Test failed in %" PRIu64 " cases\n", failcount); | ||
| 674 | } | 594 | } |
| 675 | 595 | ||
| 676 | void | 596 | void |
| @@ -685,5 +605,7 @@ init_coord() | |||
| 685 | 605 | ||
| 686 | init_cphtr_cosets(); | 606 | init_cphtr_cosets(); |
| 687 | init_cornershtrfin(); | 607 | init_cornershtrfin(); |
| 608 | init_htr_eposs(); | ||
| 609 | init_cpud_separate(); | ||
| 688 | } | 610 | } |
| 689 | 611 | ||
