solve.h (9388B)
1 typedef struct { 2 cube_t cube; 3 cube_t inverse; 4 uint8_t target_depth; 5 solution_moves_t *solution_moves; 6 solution_settings_t *solution_settings; 7 solution_list_t *solution_list; 8 uint8_t nissflag; 9 bool lastisnormal; 10 coord_t *coord; 11 const unsigned char *coord_data; 12 const unsigned char *ptable; 13 } dfsarg_solve_coord_t; 14 15 STATIC int64_t solve_coord(oriented_cube_t, coord_t [static 1], uint8_t, 16 uint8_t, uint8_t, uint8_t, uint64_t, uint8_t, uint8_t, uint64_t, 17 const unsigned char *, size_t, char *, int (*)(void *), void *); 18 STATIC long long solve_coord_dispatch(oriented_cube_t, const char *, unsigned, 19 unsigned, unsigned, unsigned, unsigned, unsigned, unsigned long long, 20 const unsigned char *, unsigned, char *, 21 long long [static NISSY_SIZE_SOLVE_STATS], int (*)(void *), void *); 22 STATIC bool coord_solution_admissible(const dfsarg_solve_coord_t [static 1]); 23 STATIC bool solve_coord_dfs_stop(const dfsarg_solve_coord_t [static 1]); 24 STATIC bool coord_continue_onnormal(const dfsarg_solve_coord_t [static 1]); 25 STATIC bool coord_continue_oninverse(const dfsarg_solve_coord_t [static 1]); 26 STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t [static 1]); 27 28 STATIC bool 29 coord_solution_admissible(const dfsarg_solve_coord_t arg[static 1]) 30 { 31 uint8_t n; 32 33 n = arg->solution_moves->nmoves + arg->solution_moves->npremoves; 34 if (arg->target_depth != n) 35 return false; 36 37 return arg->coord->is_admissible == NULL || 38 arg->coord->is_admissible(arg->solution_moves); 39 } 40 41 STATIC bool 42 solve_coord_dfs_stop(const dfsarg_solve_coord_t arg[static 1]) 43 { 44 bool hasnissed; 45 uint8_t n, pval; 46 uint64_t coord; 47 const cube_t *c; 48 49 n = arg->solution_moves->nmoves + arg->solution_moves->npremoves; 50 if (n >= arg->target_depth) 51 return true; 52 53 hasnissed = arg->solution_moves->nmoves > 0 && 54 arg->solution_moves->npremoves > 0; 55 if (!hasnissed && (arg->nissflag & NISSY_NISSFLAG_MIXED)) 56 return false; 57 58 c = arg->lastisnormal ? &arg->cube : &arg->inverse; 59 60 coord = arg->coord->coord(*c, arg->coord_data); 61 pval = get_coord_pval(arg->coord, arg->ptable, coord); 62 63 return n + pval > arg->target_depth; 64 } 65 66 STATIC bool 67 coord_continue_onnormal(const dfsarg_solve_coord_t arg[static 1]) 68 { 69 uint8_t f, nn, ni, th; 70 71 f = arg->nissflag; 72 nn = arg->solution_moves->nmoves; 73 ni = arg->solution_moves->npremoves; 74 th = DIV_ROUND_UP(arg->target_depth, 2); 75 76 if (nn + ni == 0) 77 return f & (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_MIXED); 78 79 if (arg->lastisnormal) 80 return (f & NISSY_NISSFLAG_NORMAL) || 81 ((f & NISSY_NISSFLAG_MIXED) && (ni > 0 || nn <= th)); 82 83 return (f & NISSY_NISSFLAG_MIXED) && nn == 0 && ni < th && 84 coord_can_switch(arg->coord, arg->coord_data, 85 ni, arg->solution_moves->premoves); 86 } 87 88 STATIC bool 89 coord_continue_oninverse(const dfsarg_solve_coord_t arg[static 1]) 90 { 91 uint8_t f, nn, ni, th; 92 93 f = arg->nissflag; 94 nn = arg->solution_moves->nmoves; 95 ni = arg->solution_moves->npremoves; 96 th = DIV_ROUND_UP(arg->target_depth, 2); 97 98 if (nn + ni == 0) 99 return f & (NISSY_NISSFLAG_INVERSE | NISSY_NISSFLAG_MIXED); 100 101 if (!arg->lastisnormal) 102 return (f & NISSY_NISSFLAG_INVERSE) || 103 ((f & NISSY_NISSFLAG_MIXED) && (nn > 0 || ni < th)); 104 105 return (f & NISSY_NISSFLAG_MIXED) && ni == 0 && nn <= th && 106 coord_can_switch(arg->coord, arg->coord_data, 107 nn, arg->solution_moves->moves); 108 } 109 110 STATIC int64_t 111 solve_coord_dfs(dfsarg_solve_coord_t arg[static 1]) 112 { 113 bool lastbackup; 114 uint8_t m, l, nnbackup, nibackup; 115 uint32_t mm; 116 uint64_t coord; 117 int64_t n, ret; 118 cube_t backup_cube, backup_inverse; 119 120 coord = arg->coord->coord(arg->cube, arg->coord_data); 121 if (coord == 0) { 122 if (!coord_solution_admissible(arg)) 123 return 0; 124 return appendsolution(arg->solution_moves, 125 arg->solution_settings, arg->solution_list); 126 } 127 128 if (solve_coord_dfs_stop(arg)) 129 return 0; 130 131 backup_cube = arg->cube; 132 backup_inverse = arg->inverse; 133 lastbackup = arg->lastisnormal; 134 nnbackup = arg->solution_moves->nmoves; 135 nibackup = arg->solution_moves->npremoves; 136 137 ret = 0; 138 if (coord_continue_onnormal(arg)) { 139 l = arg->solution_moves->nmoves; 140 mm = arg->coord->moves_mask; 141 if (l != 0) { 142 m = arg->solution_moves->moves[l-1]; 143 mm &= allowedmask[movebase(m)]; 144 } 145 arg->solution_moves->nmoves++; 146 arg->lastisnormal = true; 147 148 for (m = 0; m < NMOVES; m++) { 149 if (!(mm & (UINT32_C(1) << (uint32_t)m))) 150 continue; 151 152 arg->solution_moves->moves[l] = m; 153 arg->cube = move(backup_cube, m); 154 arg->inverse = premove(backup_inverse, m); 155 n = solve_coord_dfs(arg); 156 if (n < 0) 157 return n; 158 ret += n; 159 arg->solution_moves->npremoves = nibackup; 160 } 161 162 arg->solution_moves->nmoves--; 163 } 164 165 arg->lastisnormal = lastbackup; 166 167 if (coord_continue_oninverse(arg)) { 168 l = arg->solution_moves->npremoves; 169 mm = arg->coord->moves_mask; 170 if (l != 0) { 171 m = arg->solution_moves->premoves[l-1]; 172 mm &= allowedmask[movebase(m)]; 173 } 174 arg->solution_moves->npremoves++; 175 arg->lastisnormal = false; 176 177 for (m = 0; m < NMOVES; m++) { 178 if (!(mm & (UINT32_C(1) << (uint32_t)m))) 179 continue; 180 181 arg->solution_moves->premoves[l] = m; 182 arg->inverse = move(backup_inverse, m); 183 arg->cube = premove(backup_cube, m); 184 n = solve_coord_dfs(arg); 185 if (n < 0) 186 return n; 187 ret += n; 188 arg->solution_moves->nmoves = nnbackup; 189 } 190 191 arg->solution_moves->npremoves--; 192 } 193 194 arg->cube = backup_cube; 195 arg->inverse = backup_inverse; 196 arg->lastisnormal = lastbackup; 197 198 return ret; 199 } 200 201 STATIC long long 202 solve_coord_dispatch( 203 oriented_cube_t oc, 204 const char *coord_and_axis, 205 unsigned nissflag, 206 unsigned minmoves, 207 unsigned maxmoves, 208 unsigned maxsolutions, 209 unsigned optimal, 210 unsigned threads, 211 unsigned long long data_size, 212 const unsigned char *data, 213 unsigned solutions_size, 214 char *sols, 215 long long stats[static NISSY_SIZE_SOLVE_STATS], 216 int (*poll_status)(void *), 217 void *poll_status_data 218 ) 219 { 220 coord_t *coord; 221 uint8_t axis; 222 223 parse_coord_and_axis(coord_and_axis, &coord, &axis); 224 225 if (coord == NULL) { 226 LOG("Error: could not parse coordinate from '%s'\n", 227 coord_and_axis); 228 return NISSY_ERROR_INVALID_SOLVER; 229 } 230 231 if (axis == UINT8_ERROR) { 232 LOG("Error: could not parse axis from '%s'\n", coord_and_axis); 233 return NISSY_ERROR_INVALID_SOLVER; 234 } 235 236 return solve_coord(oc, coord, axis, nissflag, minmoves, maxmoves, 237 maxsolutions, optimal, threads, data_size, data, 238 solutions_size, sols, poll_status, poll_status_data); 239 } 240 241 STATIC int64_t 242 solve_coord( 243 oriented_cube_t oc, 244 coord_t coord [static 1], 245 uint8_t axis, 246 uint8_t nissflag, 247 uint8_t minmoves, 248 uint8_t maxmoves, 249 uint64_t maxsolutions, 250 uint8_t optimal, 251 uint8_t threads, 252 uint64_t data_size, 253 const unsigned char *data, 254 size_t solutions_size, 255 char *sols, 256 int (*poll_status)(void *), 257 void *poll_status_data 258 ) 259 { 260 int8_t d; 261 uint8_t t; 262 int64_t ndepth; 263 cube_t c; 264 const unsigned char *coord_data; 265 const unsigned char *ptable; 266 dfsarg_solve_coord_t arg; 267 tableinfo_t info; 268 solution_moves_t solution_moves; 269 solution_settings_t solution_settings; 270 solution_list_t solution_list; 271 272 t = coord->axistrans[axis]; 273 c = transform(oc.cube, t); 274 275 if (!coord->is_solvable(c)) 276 goto solve_coord_error_unsolvable; 277 278 if (!solution_list_init(&solution_list, solutions_size, sols)) 279 goto solve_coord_error_buffer; 280 281 if (readtableinfo(data_size, data, &info) != NISSY_OK) 282 goto solve_coord_error_data; 283 284 if (info.type == TABLETYPE_PRUNING) { 285 /* Only the pruning table */ 286 coord_data = NULL; 287 ptable = data + INFOSIZE; 288 } else { 289 /* Coordinate has extra data */ 290 coord_data = data + INFOSIZE; 291 ptable = data + info.next + INFOSIZE; 292 } 293 294 solution_moves_reset(&solution_moves); 295 296 solution_settings = (solution_settings_t) { 297 .tmask = TM_SINGLE(inverse_trans(t)), 298 .unniss = false, 299 .maxmoves = maxmoves, 300 .maxsolutions = maxsolutions, 301 .optimal = optimal, 302 .orientation = oc.orientation, 303 }; 304 305 arg = (dfsarg_solve_coord_t) { 306 .cube = c, 307 .inverse = inverse(c), 308 .coord = coord, 309 .coord_data = coord_data, 310 .ptable = ptable, 311 .solution_moves = &solution_moves, 312 .solution_settings = &solution_settings, 313 .solution_list = &solution_list, 314 .nissflag = nissflag, 315 316 /* 317 Since no move has been done yet, this field should be 318 neither true nor false; using its value now is logically 319 undefined behavior. 320 TODO: find a more elegant solution 321 */ 322 .lastisnormal = true, 323 }; 324 325 if (coord->coord(c, coord_data) == 0) { 326 if (minmoves == 0 && !appendsolution(&solution_moves, 327 &solution_settings, &solution_list)) 328 goto solve_coord_error_buffer; 329 goto solve_coord_done; 330 } 331 332 for ( 333 d = MAX(minmoves, 1); 334 !solutions_done(&solution_list, &solution_settings, d); 335 d++ 336 ) { 337 if (d >= 12) 338 LOG("[%s solve] Found %" PRIu64 " solutions, " 339 "searching at depth %" PRId8 "\n", 340 coord->name, solution_list.nsols, d); 341 342 arg.target_depth = d; 343 solution_moves_reset(arg.solution_moves); 344 ndepth = solve_coord_dfs(&arg); 345 346 if (ndepth < 0) 347 return ndepth; 348 349 solution_list.nsols += (uint64_t)ndepth; 350 } 351 352 solve_coord_done: 353 return (int64_t)solution_list.nsols; 354 355 solve_coord_error_data: 356 LOG("[%s solve] Error reading table\n", coord->name); 357 return NISSY_ERROR_DATA; 358 359 solve_coord_error_buffer: 360 LOG("[%s solve] Error appending solution to buffer: size too small\n", 361 coord->name); 362 return NISSY_ERROR_BUFFER_SIZE; 363 364 solve_coord_error_unsolvable: 365 LOG("[%s solve] Error: cube not ready\n", coord->name); 366 return NISSY_ERROR_UNSOLVABLE_CUBE; 367 }