diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-02-10 23:30:20 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-02-10 23:30:20 +0100 |
| commit | 3bfd0bb403d62bd942d1912d085ea59b156062fd (patch) | |
| tree | 913ddcf693d89009fa033e245b997e27211d00ef /src | |
| parent | 374c33d1412f8f3a35dd3eb874dcaef82d60fff4 (diff) | |
| download | nissy-3bfd0bb403d62bd942d1912d085ea59b156062fd.tar.gz nissy-3bfd0bb403d62bd942d1912d085ea59b156062fd.zip | |
Added (just a few) tests for alg and added fields to alg struct
Diffstat (limited to '')
| -rw-r--r-- | src/alg.c | 107 | ||||
| -rw-r--r-- | src/alg.h | 10 | ||||
| -rw-r--r-- | src/cubetypes.h | 4 | ||||
| -rw-r--r-- | src/solve.c | 24 |
4 files changed, 77 insertions, 68 deletions
| @@ -84,30 +84,42 @@ append_move(Alg *alg, Move m, bool inverse) | |||
| 84 | alg->move[alg->len] = m; | 84 | alg->move[alg->len] = m; |
| 85 | alg->inv [alg->len] = inverse; | 85 | alg->inv [alg->len] = inverse; |
| 86 | alg->len++; | 86 | alg->len++; |
| 87 | |||
| 88 | if (inverse) | ||
| 89 | alg->move_inverse[alg->len_inverse++] = m; | ||
| 90 | else | ||
| 91 | alg->move_normal[alg->len_normal++] = m; | ||
| 87 | } | 92 | } |
| 88 | 93 | ||
| 89 | static int | 94 | static int |
| 90 | axis(Move m) | 95 | axis(Move m) |
| 91 | { | 96 | { |
| 92 | if (m == NULLMOVE) | 97 | static int aux[] = { |
| 93 | return 0; | 98 | [NULLMOVE] = 0, |
| 94 | 99 | ||
| 95 | if (m >= U && m <= B3) | 100 | [U] = 1, [U2] = 1, [U3] = 1, |
| 96 | return (m-1)/6 + 1; | 101 | [D] = 1, [D2] = 1, [D3] = 1, |
| 97 | 102 | [Uw] = 1, [Uw2] = 1, [Uw3] = 1, | |
| 98 | if (m >= Uw && m <= Bw3) | 103 | [Dw] = 1, [Dw2] = 1, [Dw3] = 1, |
| 99 | return (m-1)/6 - 2; | 104 | [E] = 1, [E2] = 1, [E3] = 1, |
| 105 | [y] = 1, [y2] = 1, [y3] = 1, | ||
| 100 | 106 | ||
| 101 | if (base_move(m) == E || base_move(m) == y) | 107 | [R] = 2, [R2] = 2, [R3] = 2, |
| 102 | return 1; | 108 | [L] = 2, [L2] = 2, [L3] = 2, |
| 109 | [Rw] = 2, [Rw2] = 2, [Rw3] = 2, | ||
| 110 | [Lw] = 2, [Lw2] = 2, [Lw3] = 2, | ||
| 111 | [M] = 2, [M2] = 2, [M3] = 2, | ||
| 112 | [x] = 2, [x2] = 2, [x3] = 2, | ||
| 103 | 113 | ||
| 104 | if (base_move(m) == M || base_move(m) == x) | 114 | [F] = 3, [F2] = 3, [F3] = 3, |
| 105 | return 2; | 115 | [B] = 3, [B2] = 3, [B3] = 3, |
| 106 | 116 | [Fw] = 3, [Fw2] = 3, [Fw3] = 3, | |
| 107 | if (base_move(m) == S || base_move(m) == z) | 117 | [Bw] = 3, [Bw2] = 3, [Bw3] = 3, |
| 108 | return 3; | 118 | [S] = 3, [S2] = 3, [S3] = 3, |
| 119 | [z] = 3, [z2] = 3, [z3] = 3, | ||
| 120 | }; | ||
| 109 | 121 | ||
| 110 | return -1; | 122 | return aux[m]; |
| 111 | } | 123 | } |
| 112 | 124 | ||
| 113 | Move | 125 | Move |
| @@ -137,7 +149,7 @@ compose_alg(Alg *alg1, Alg *alg2) | |||
| 137 | void | 149 | void |
| 138 | copy_alg(Alg *src, Alg *dst) | 150 | copy_alg(Alg *src, Alg *dst) |
| 139 | { | 151 | { |
| 140 | dst->len = 0; /* Overwrites */ | 152 | dst->len = dst->len_normal = dst->len_inverse = 0; |
| 141 | compose_alg(dst, src); | 153 | compose_alg(dst, src); |
| 142 | } | 154 | } |
| 143 | 155 | ||
| @@ -169,16 +181,6 @@ free_alglistnode(AlgListNode *aln) | |||
| 169 | free(aln); | 181 | free(aln); |
| 170 | } | 182 | } |
| 171 | 183 | ||
| 172 | void | ||
| 173 | inplace(Alg * (*f)(Alg *), Alg *alg) | ||
| 174 | { | ||
| 175 | Alg *aux; | ||
| 176 | |||
| 177 | aux = f(alg); | ||
| 178 | copy_alg(aux, alg); | ||
| 179 | free(aux); | ||
| 180 | } | ||
| 181 | |||
| 182 | Alg * | 184 | Alg * |
| 183 | inverse_alg(Alg *alg) | 185 | inverse_alg(Alg *alg) |
| 184 | { | 186 | { |
| @@ -234,10 +236,14 @@ new_alg(char *str) | |||
| 234 | Move j, m; | 236 | Move j, m; |
| 235 | 237 | ||
| 236 | alg = malloc(sizeof(Alg)); | 238 | alg = malloc(sizeof(Alg)); |
| 237 | alg->move = malloc(30 * sizeof(Move)); | 239 | alg->allocated = 30; |
| 238 | alg->inv = malloc(30 * sizeof(bool)); | 240 | alg->move = malloc(alg->allocated * sizeof(Move)); |
| 239 | alg->allocated = 30; | 241 | alg->inv = malloc(alg->allocated * sizeof(bool)); |
| 240 | alg->len = 0; | 242 | alg->move_normal = malloc(alg->allocated * sizeof(Move)); |
| 243 | alg->move_inverse = malloc(alg->allocated * sizeof(Move)); | ||
| 244 | alg->len = 0; | ||
| 245 | alg->len_normal = 0; | ||
| 246 | alg->len_inverse = 0; | ||
| 241 | 247 | ||
| 242 | niss = false; | 248 | niss = false; |
| 243 | for (i = 0; str[i]; i++) { | 249 | for (i = 0; str[i]; i++) { |
| @@ -246,13 +252,13 @@ new_alg(char *str) | |||
| 246 | 252 | ||
| 247 | if (str[i] == '(' && niss) { | 253 | if (str[i] == '(' && niss) { |
| 248 | fprintf(stderr, "Error reading moves: nested ( )\n"); | 254 | fprintf(stderr, "Error reading moves: nested ( )\n"); |
| 249 | alg->len = 0; | 255 | alg->len = alg->len_normal = alg->len_inverse = 0; |
| 250 | return alg; | 256 | return alg; |
| 251 | } | 257 | } |
| 252 | 258 | ||
| 253 | if (str[i] == ')' && !niss) { | 259 | if (str[i] == ')' && !niss) { |
| 254 | fprintf(stderr, "Error reading moves: unmatched )\n"); | 260 | fprintf(stderr, "Error reading moves: unmatched )\n"); |
| 255 | alg->len = 0; | 261 | alg->len = alg->len_normal = alg->len_inverse = 0; |
| 256 | return alg; | 262 | return alg; |
| 257 | } | 263 | } |
| 258 | 264 | ||
| @@ -319,7 +325,7 @@ new_alg(char *str) | |||
| 319 | 325 | ||
| 320 | if (niss) { | 326 | if (niss) { |
| 321 | fprintf(stderr, "Error reading moves: unmatched (\n"); | 327 | fprintf(stderr, "Error reading moves: unmatched (\n"); |
| 322 | alg->len = 0; | 328 | alg->len = alg->len_normal = alg->len_inverse = 0; |
| 323 | } | 329 | } |
| 324 | 330 | ||
| 325 | return alg; | 331 | return alg; |
| @@ -349,6 +355,19 @@ on_inverse(Alg *alg) | |||
| 349 | return ret; | 355 | return ret; |
| 350 | } | 356 | } |
| 351 | 357 | ||
| 358 | bool | ||
| 359 | possible_next(Move m, Moveset *ms, Move l0, Move l1) | ||
| 360 | { | ||
| 361 | bool allowed, order; | ||
| 362 | uint64_t mbit; | ||
| 363 | |||
| 364 | mbit = ((uint64_t)1) << m; | ||
| 365 | allowed = mbit & ms->mask[l1][l0]; | ||
| 366 | order = !commute(l0, m) || l0 < m; | ||
| 367 | |||
| 368 | return allowed && order; | ||
| 369 | } | ||
| 370 | |||
| 352 | void | 371 | void |
| 353 | print_alg(Alg *alg, bool l) | 372 | print_alg(Alg *alg, bool l) |
| 354 | { | 373 | { |
| @@ -404,8 +423,10 @@ realloc_alg(Alg *alg, int n) | |||
| 404 | fprintf(stderr, "something might go wrong.\n"); | 423 | fprintf(stderr, "something might go wrong.\n"); |
| 405 | } | 424 | } |
| 406 | 425 | ||
| 407 | alg->move = realloc(alg->move, n * sizeof(int)); | 426 | alg->move = realloc(alg->move, n * sizeof(int)); |
| 408 | alg->inv = realloc(alg->inv, n * sizeof(int)); | 427 | alg->inv = realloc(alg->inv, n * sizeof(int)); |
| 428 | alg->move_normal = realloc(alg->move_normal, n * sizeof(int)); | ||
| 429 | alg->move_inverse = realloc(alg->move_inverse, n * sizeof(int)); | ||
| 409 | alg->allocated = n; | 430 | alg->allocated = n; |
| 410 | } | 431 | } |
| 411 | 432 | ||
| @@ -455,14 +476,12 @@ unniss(Alg *alg) | |||
| 455 | 476 | ||
| 456 | ret = new_alg(""); | 477 | ret = new_alg(""); |
| 457 | 478 | ||
| 458 | for (i = 0; i < alg->len; i++) | 479 | for (i = 0; i < alg->len_normal; i++) |
| 459 | if (!alg->inv[i]) | 480 | append_move(ret, alg->move_normal[i], false); |
| 460 | append_move(ret, alg->move[i], false); | 481 | |
| 461 | 482 | for (i = 0; i < alg->len_inverse; i++) | |
| 462 | for (i = alg->len-1; i >= 0; i--) | 483 | append_move(ret, inverse_move(alg->move_inverse[i]), false); |
| 463 | if (alg->inv[i]) | 484 | |
| 464 | append_move(ret, inverse_move(alg->move[i]), false); | ||
| 465 | |||
| 466 | return ret; | 485 | return ret; |
| 467 | } | 486 | } |
| 468 | 487 | ||
| @@ -483,7 +502,7 @@ init_moveset(Moveset *ms) | |||
| 483 | for (l1 = 0; l1 < NMOVES; l1++) { | 502 | for (l1 = 0; l1 < NMOVES; l1++) { |
| 484 | for (l2 = 0; l2 < NMOVES; l2++) { | 503 | for (l2 = 0; l2 < NMOVES; l2++) { |
| 485 | ms->mask[l2][l1] = 0; | 504 | ms->mask[l2][l1] = 0; |
| 486 | for (l=0; ms->sorted_moves[l]!=NULLMOVE; l++) { | 505 | for (l = 0; ms->sorted_moves[l] != NULLMOVE; l++) { |
| 487 | m = ms->sorted_moves[l]; | 506 | m = ms->sorted_moves[l]; |
| 488 | if (ms->allowed_next(l2, l1, m)) | 507 | if (ms->allowed_next(l2, l1, m)) |
| 489 | ms->mask[l2][l1] |= (one<<m); | 508 | ms->mask[l2][l1] |= (one<<m); |
| @@ -15,6 +15,11 @@ bool allowed_eofb(Move m); | |||
| 15 | bool allowed_drud(Move m); | 15 | bool allowed_drud(Move m); |
| 16 | bool allowed_htr(Move m); | 16 | bool allowed_htr(Move m); |
| 17 | bool allowed_next_all(Move l2, Move l1, Move m); | 17 | bool allowed_next_all(Move l2, Move l1, Move m); |
| 18 | |||
| 19 | void moveset_to_list(Moveset ms, Move *lst); | ||
| 20 | void init_moveset(Moveset *ms); | ||
| 21 | bool possible_next(Move m, Moveset *ms, Move l0, Move l1); | ||
| 22 | |||
| 18 | void append_alg(AlgList *l, Alg *alg); | 23 | void append_alg(AlgList *l, Alg *alg); |
| 19 | void append_move(Alg *alg, Move m, bool inverse); | 24 | void append_move(Alg *alg, Move m, bool inverse); |
| 20 | Move base_move(Move m); | 25 | Move base_move(Move m); |
| @@ -23,12 +28,9 @@ bool commute(Move m1, Move m2); | |||
| 23 | void copy_alg(Alg *src, Alg *dst); | 28 | void copy_alg(Alg *src, Alg *dst); |
| 24 | void free_alg(Alg *alg); | 29 | void free_alg(Alg *alg); |
| 25 | void free_alglist(AlgList *l); | 30 | void free_alglist(AlgList *l); |
| 26 | void inplace(Alg * (*f)(Alg *), Alg *alg); | ||
| 27 | Alg * inverse_alg(Alg *alg); | 31 | Alg * inverse_alg(Alg *alg); |
| 28 | Move inverse_move(Move m); | 32 | Move inverse_move(Move m); |
| 29 | char * move_string(Move m); | 33 | char * move_string(Move m); |
| 30 | void movelist_to_position(Move *ml, int *pos); | ||
| 31 | void moveset_to_list(Moveset ms, Move *lst); | ||
| 32 | Alg * new_alg(char *str); | 34 | Alg * new_alg(char *str); |
| 33 | AlgList * new_alglist(); | 35 | AlgList * new_alglist(); |
| 34 | Alg * on_inverse(Alg *alg); | 36 | Alg * on_inverse(Alg *alg); |
| @@ -38,8 +40,6 @@ void swapmove(Move *m1, Move *m2); | |||
| 38 | char * trans_string(Trans t); /* Here because similar to move_string, move? */ | 40 | char * trans_string(Trans t); /* Here because similar to move_string, move? */ |
| 39 | Alg * unniss(Alg *alg); | 41 | Alg * unniss(Alg *alg); |
| 40 | 42 | ||
| 41 | void init_moveset(Moveset *ms); | ||
| 42 | |||
| 43 | /* Movesets ******************************************************************/ | 43 | /* Movesets ******************************************************************/ |
| 44 | 44 | ||
| 45 | #ifndef ALG_C | 45 | #ifndef ALG_C |
diff --git a/src/cubetypes.h b/src/cubetypes.h index 565dddb..8d38904 100644 --- a/src/cubetypes.h +++ b/src/cubetypes.h | |||
| @@ -122,6 +122,10 @@ alg | |||
| 122 | bool * inv; | 122 | bool * inv; |
| 123 | int len; | 123 | int len; |
| 124 | int allocated; | 124 | int allocated; |
| 125 | Move * move_normal; | ||
| 126 | int len_normal; | ||
| 127 | Move * move_inverse; | ||
| 128 | int len_inverse; | ||
| 125 | }; | 129 | }; |
| 126 | 130 | ||
| 127 | struct | 131 | struct |
diff --git a/src/solve.c b/src/solve.c index df15cca..d5a8f3d 100644 --- a/src/solve.c +++ b/src/solve.c | |||
| @@ -4,7 +4,6 @@ | |||
| 4 | 4 | ||
| 5 | /* Local functions ***********************************************************/ | 5 | /* Local functions ***********************************************************/ |
| 6 | 6 | ||
| 7 | static bool allowed_next(Move move, Step *s, Move l0, Move l1); | ||
| 8 | static bool cancel_niss(DfsArg *arg); | 7 | static bool cancel_niss(DfsArg *arg); |
| 9 | static void copy_dfsarg(DfsArg *src, DfsArg *dst); | 8 | static void copy_dfsarg(DfsArg *src, DfsArg *dst); |
| 10 | static void dfs(DfsArg *arg); | 9 | static void dfs(DfsArg *arg); |
| @@ -19,19 +18,6 @@ static bool solvestop(int d, int op, SolveOptions *opts, AlgList *sols); | |||
| 19 | /* Local functions ***********************************************************/ | 18 | /* Local functions ***********************************************************/ |
| 20 | 19 | ||
| 21 | static bool | 20 | static bool |
| 22 | allowed_next(Move m, Step *s, Move l0, Move l1) | ||
| 23 | { | ||
| 24 | bool allowed, order; | ||
| 25 | uint64_t mbit; | ||
| 26 | |||
| 27 | mbit = ((uint64_t)1) << m; | ||
| 28 | allowed = mbit & s->moveset->mask[l1][l0]; | ||
| 29 | order = !commute(l0, m) || l0 < m; | ||
| 30 | |||
| 31 | return allowed && order; | ||
| 32 | } | ||
| 33 | |||
| 34 | static bool | ||
| 35 | cancel_niss(DfsArg *arg) | 21 | cancel_niss(DfsArg *arg) |
| 36 | { | 22 | { |
| 37 | Moveset *ms; | 23 | Moveset *ms; |
| @@ -82,16 +68,14 @@ copy_dfsarg(DfsArg *src, DfsArg *dst) | |||
| 82 | dst->ind[i].t = src->ind[i].t; | 68 | dst->ind[i].t = src->ind[i].t; |
| 83 | } | 69 | } |
| 84 | 70 | ||
| 85 | /* | ||
| 86 | src->s->copy_extra(src, dst); | 71 | src->s->copy_extra(src, dst); |
| 87 | */ | ||
| 88 | } | 72 | } |
| 89 | 73 | ||
| 90 | static void | 74 | static void |
| 91 | dfs(DfsArg *arg) | 75 | dfs(DfsArg *arg) |
| 92 | { | 76 | { |
| 93 | int i; | 77 | int i; |
| 94 | Move m; | 78 | Move m, l0, l1; |
| 95 | DfsArg newarg; | 79 | DfsArg newarg; |
| 96 | 80 | ||
| 97 | if (dfs_move_checkstop(arg)) | 81 | if (dfs_move_checkstop(arg)) |
| @@ -105,9 +89,11 @@ dfs(DfsArg *arg) | |||
| 105 | 89 | ||
| 106 | for (i = 0; arg->s->moveset->sorted_moves[i] != NULLMOVE; i++) { | 90 | for (i = 0; arg->s->moveset->sorted_moves[i] != NULLMOVE; i++) { |
| 107 | m = arg->s->moveset->sorted_moves[i]; | 91 | m = arg->s->moveset->sorted_moves[i]; |
| 108 | if (allowed_next(m, arg->s, arg->last[0], arg->last[1])) { | 92 | l0 = arg->last[0]; |
| 93 | l1 = arg->last[1]; | ||
| 94 | if (possible_next(m, arg->s->moveset, l0, l1)) { | ||
| 109 | copy_dfsarg(arg, &newarg); | 95 | copy_dfsarg(arg, &newarg); |
| 110 | newarg.last[1] = arg->last[0]; | 96 | newarg.last[1] = l0; |
| 111 | newarg.last[0] = m; | 97 | newarg.last[0] = m; |
| 112 | append_move(arg->current_alg, m, newarg.niss); | 98 | append_move(arg->current_alg, m, newarg.niss); |
| 113 | dfs(&newarg); | 99 | dfs(&newarg); |
