diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/cube.h | 66 | ||||
| -rw-r--r-- | src/core/moves.h | 88 | ||||
| -rw-r--r-- | src/core/transform.h | 18 | ||||
| -rw-r--r-- | src/core/transform_with_switch.h | 18 |
4 files changed, 79 insertions, 111 deletions
diff --git a/src/core/cube.h b/src/core/cube.h index 4b28103..df5161a 100644 --- a/src/core/cube.h +++ b/src/core/cube.h | |||
| @@ -1,25 +1,12 @@ | |||
| 1 | #define _move(M, c) compose(c, _move_cube_ ## M) | ||
| 2 | #define _premove(M, c) compose(_move_cube_ ## M, c) | ||
| 3 | |||
| 4 | _static cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); | 1 | _static cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); |
| 5 | _static cube_t solvedcube(void); | 2 | _static cube_t solvedcube(void); |
| 6 | _static bool isconsistent(cube_t); | 3 | _static bool isconsistent(cube_t); |
| 7 | _static bool issolvable(cube_t); | 4 | _static bool issolvable(cube_t); |
| 8 | _static bool issolved(cube_t); | 5 | _static bool issolved(cube_t); |
| 9 | _static bool iserror(cube_t); | 6 | _static bool iserror(cube_t); |
| 10 | _static cube_t applymoves(cube_t, const char *); | ||
| 11 | _static cube_t applytrans(cube_t, const char *); | ||
| 12 | _static cube_t frommoves(const char *); | ||
| 13 | _static void getcube_fix(int64_t *, int64_t *, int64_t *, int64_t *); | 7 | _static void getcube_fix(int64_t *, int64_t *, int64_t *, int64_t *); |
| 14 | _static cube_t getcube(int64_t, int64_t, int64_t, int64_t); | 8 | _static cube_t getcube(int64_t, int64_t, int64_t, int64_t); |
| 15 | 9 | ||
| 16 | _static cube_t transform_edges(cube_t, uint8_t); | ||
| 17 | _static cube_t transform_corners(cube_t, uint8_t); | ||
| 18 | _static cube_t transform(cube_t, uint8_t); | ||
| 19 | |||
| 20 | /* declared in moves.h */ | ||
| 21 | _static cube_t move(cube_t, uint8_t); | ||
| 22 | |||
| 23 | _static cube_t | 10 | _static cube_t |
| 24 | cubefromarray(uint8_t c[static 8], uint8_t e[static 12]) | 11 | cubefromarray(uint8_t c[static 8], uint8_t e[static 12]) |
| 25 | { | 12 | { |
| @@ -149,41 +136,6 @@ iserror(cube_t cube) | |||
| 149 | return equal(cube, zero); | 136 | return equal(cube, zero); |
| 150 | } | 137 | } |
| 151 | 138 | ||
| 152 | _static cube_t | ||
| 153 | applymoves(cube_t cube, const char *buf) | ||
| 154 | { | ||
| 155 | uint8_t r, m; | ||
| 156 | const char *b; | ||
| 157 | |||
| 158 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 159 | "move error: inconsistent cube\n"); | ||
| 160 | |||
| 161 | for (b = buf; *b != '\0'; b++) { | ||
| 162 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 163 | b++; | ||
| 164 | if (*b == '\0') | ||
| 165 | goto applymoves_finish; | ||
| 166 | if ((r = readmove(*b)) == _error) | ||
| 167 | goto applymoves_error; | ||
| 168 | if ((m = readmodifier(*(b+1))) != 0) | ||
| 169 | b++; | ||
| 170 | cube = move(cube, r + m); | ||
| 171 | } | ||
| 172 | |||
| 173 | applymoves_finish: | ||
| 174 | return cube; | ||
| 175 | |||
| 176 | applymoves_error: | ||
| 177 | LOG("applymoves error\n"); | ||
| 178 | return zero; | ||
| 179 | } | ||
| 180 | |||
| 181 | _static cube_t | ||
| 182 | frommoves(const char *buf) | ||
| 183 | { | ||
| 184 | return applymoves(solved, buf); | ||
| 185 | } | ||
| 186 | |||
| 187 | _static void | 139 | _static void |
| 188 | getcube_fix(int64_t *ep, int64_t *eo, int64_t *cp, int64_t *co) | 140 | getcube_fix(int64_t *ep, int64_t *eo, int64_t *cp, int64_t *co) |
| 189 | { | 141 | { |
| @@ -227,21 +179,3 @@ getcube(int64_t ep, int64_t eo, int64_t cp, int64_t co) | |||
| 227 | 179 | ||
| 228 | return cubefromarray(carr, earr); | 180 | return cubefromarray(carr, earr); |
| 229 | } | 181 | } |
| 230 | |||
| 231 | _static cube_t | ||
| 232 | applytrans(cube_t cube, const char *buf) | ||
| 233 | { | ||
| 234 | uint8_t t; | ||
| 235 | |||
| 236 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 237 | "transformation error: inconsistent cube\n"); | ||
| 238 | |||
| 239 | t = readtrans(buf); | ||
| 240 | |||
| 241 | return transform(cube, t); | ||
| 242 | } | ||
| 243 | |||
| 244 | /* | ||
| 245 | TODO transform is now relegated to a separated file because it is too long. | ||
| 246 | It would be nice to make it shorter without loosing performance. | ||
| 247 | */ | ||
diff --git a/src/core/moves.h b/src/core/moves.h index 8f9c8f5..71ef470 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -1,13 +1,7 @@ | |||
| 1 | /* probably these can be placed in constants file */ | 1 | #define _move(M, c) compose(c, _move_cube_ ## M) |
| 2 | #define NORMAL 0x00 | 2 | #define _premove(M, c) compose(_move_cube_ ## M, c) |
| 3 | #define INVERSE 0x01 | ||
| 4 | #define INVERSEBRANCH 0x03 | ||
| 5 | #define NORMALBRANCH 0x02 | ||
| 6 | #define ALLMOVES 0x3FFFF | ||
| 7 | #define NOHALFTURNS 0x2DB6D | ||
| 8 | 3 | ||
| 9 | _static_inline bool allowednextmove(uint8_t *, uint8_t); | 4 | _static_inline bool allowednextmove(uint8_t *, uint8_t); |
| 10 | _static uint32_t allowednextmoveH48(uint8_t *, uint8_t, uint32_t); | ||
| 11 | 5 | ||
| 12 | _static_inline uint8_t inverse_trans(uint8_t); | 6 | _static_inline uint8_t inverse_trans(uint8_t); |
| 13 | _static_inline uint8_t movebase(uint8_t); | 7 | _static_inline uint8_t movebase(uint8_t); |
| @@ -17,7 +11,10 @@ _static_inline uint32_t disable_moves(uint32_t, uint8_t); | |||
| 17 | _static cube_t move(cube_t, uint8_t); | 11 | _static cube_t move(cube_t, uint8_t); |
| 18 | _static cube_t premove(cube_t, uint8_t); | 12 | _static cube_t premove(cube_t, uint8_t); |
| 19 | _static uint8_t inverse_move(uint8_t); | 13 | _static uint8_t inverse_move(uint8_t); |
| 20 | _static uint8_t* invertpremoves(uint8_t *, uint8_t); | 14 | _static void invertmoves(uint8_t *, uint8_t, uint8_t *); |
| 15 | |||
| 16 | _static cube_t applymoves(cube_t, const char *); | ||
| 17 | _static cube_t frommoves(const char *); | ||
| 21 | 18 | ||
| 22 | _static bool | 19 | _static bool |
| 23 | allowednextmove(uint8_t *moves, uint8_t n) | 20 | allowednextmove(uint8_t *moves, uint8_t n) |
| @@ -50,34 +47,6 @@ disable_moves(uint32_t current_result, uint8_t base_index) | |||
| 50 | return current_result & ~(7 << base_index); | 47 | return current_result & ~(7 << base_index); |
| 51 | } | 48 | } |
| 52 | 49 | ||
| 53 | _static uint32_t | ||
| 54 | allowednextmoveH48(uint8_t *moves, uint8_t n, uint32_t h48branch) | ||
| 55 | { | ||
| 56 | uint32_t result = ALLMOVES; | ||
| 57 | if (h48branch & NORMALBRANCH) | ||
| 58 | result &= NOHALFTURNS; | ||
| 59 | if (n < 1) | ||
| 60 | return result; | ||
| 61 | |||
| 62 | uint8_t base1 = movebase(moves[n-1]); | ||
| 63 | uint8_t axis1 = moveaxis(moves[n-1]); | ||
| 64 | |||
| 65 | result = disable_moves(result, base1 * 3); | ||
| 66 | if (base1 % 2) | ||
| 67 | result = disable_moves(result, (base1 - 1) * 3); | ||
| 68 | |||
| 69 | if (n == 1) | ||
| 70 | return result; | ||
| 71 | |||
| 72 | uint8_t base2 = movebase(moves[n-2]); | ||
| 73 | uint8_t axis2 = moveaxis(moves[n-2]); | ||
| 74 | |||
| 75 | if(axis1 == axis2) | ||
| 76 | result = disable_moves(result, base2 * 3); | ||
| 77 | |||
| 78 | return result; | ||
| 79 | } | ||
| 80 | |||
| 81 | _static_inline uint8_t | 50 | _static_inline uint8_t |
| 82 | inverse_trans(uint8_t t) | 51 | inverse_trans(uint8_t t) |
| 83 | { | 52 | { |
| @@ -194,17 +163,46 @@ inverse_move(uint8_t m) | |||
| 194 | return m - 2 * (m % 3) + 2; | 163 | return m - 2 * (m % 3) + 2; |
| 195 | } | 164 | } |
| 196 | 165 | ||
| 197 | _static uint8_t* | 166 | _static void |
| 198 | invertpremoves(uint8_t *moves, uint8_t nmoves) | 167 | invertmoves(uint8_t *moves, uint8_t nmoves, uint8_t *ret) |
| 199 | { | 168 | { |
| 200 | uint8_t i; | 169 | uint8_t i; |
| 201 | uint8_t *ret = malloc(nmoves * sizeof(uint8_t)); | ||
| 202 | 170 | ||
| 203 | for (i = 0; i < nmoves; i++) | 171 | for (i = 0; i < nmoves; i++) |
| 204 | ret[i] = inverse_move(moves[i]); | 172 | ret[i] = inverse_move(moves[nmoves - i - 1]); |
| 173 | } | ||
| 205 | 174 | ||
| 206 | // invert elements in the array | 175 | _static cube_t |
| 207 | for (i = 0; i < nmoves / 2; i++) | 176 | applymoves(cube_t cube, const char *buf) |
| 208 | _swap(ret[i], ret[nmoves - i - 1]); | 177 | { |
| 209 | return ret; | 178 | uint8_t r, m; |
| 179 | const char *b; | ||
| 180 | |||
| 181 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 182 | "move error: inconsistent cube\n"); | ||
| 183 | |||
| 184 | for (b = buf; *b != '\0'; b++) { | ||
| 185 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 186 | b++; | ||
| 187 | if (*b == '\0') | ||
| 188 | goto applymoves_finish; | ||
| 189 | if ((r = readmove(*b)) == _error) | ||
| 190 | goto applymoves_error; | ||
| 191 | if ((m = readmodifier(*(b+1))) != 0) | ||
| 192 | b++; | ||
| 193 | cube = move(cube, r + m); | ||
| 194 | } | ||
| 195 | |||
| 196 | applymoves_finish: | ||
| 197 | return cube; | ||
| 198 | |||
| 199 | applymoves_error: | ||
| 200 | LOG("applymoves error\n"); | ||
| 201 | return zero; | ||
| 202 | } | ||
| 203 | |||
| 204 | _static cube_t | ||
| 205 | frommoves(const char *buf) | ||
| 206 | { | ||
| 207 | return applymoves(solved, buf); | ||
| 210 | } | 208 | } |
diff --git a/src/core/transform.h b/src/core/transform.h index 358b051..fa7ce59 100644 --- a/src/core/transform.h +++ b/src/core/transform.h | |||
| @@ -19,6 +19,11 @@ | |||
| 19 | invertco(compose(compose(_trans_cube_ ## T, c), \ | 19 | invertco(compose(compose(_trans_cube_ ## T, c), \ |
| 20 | _trans_cube_ ## T ## _inverse)) | 20 | _trans_cube_ ## T ## _inverse)) |
| 21 | 21 | ||
| 22 | _static cube_t transform_edges(cube_t, uint8_t); | ||
| 23 | _static cube_t transform_corners(cube_t, uint8_t); | ||
| 24 | _static cube_t transform(cube_t, uint8_t); | ||
| 25 | _static cube_t applytrans(cube_t, const char *); | ||
| 26 | |||
| 22 | static cube_t cube_trans_table[48] = { | 27 | static cube_t cube_trans_table[48] = { |
| 23 | [_trans_UFr] = _trans_cube_UFr, | 28 | [_trans_UFr] = _trans_cube_UFr, |
| 24 | [_trans_UFm] = _trans_cube_UFm, | 29 | [_trans_UFm] = _trans_cube_UFm, |
| @@ -174,3 +179,16 @@ transform(cube_t c, uint8_t t) | |||
| 174 | 179 | ||
| 175 | return t < 24 ? ret : invertco(ret); | 180 | return t < 24 ? ret : invertco(ret); |
| 176 | } | 181 | } |
| 182 | |||
| 183 | _static cube_t | ||
| 184 | applytrans(cube_t cube, const char *buf) | ||
| 185 | { | ||
| 186 | uint8_t t; | ||
| 187 | |||
| 188 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 189 | "transformation error: inconsistent cube\n"); | ||
| 190 | |||
| 191 | t = readtrans(buf); | ||
| 192 | |||
| 193 | return transform(cube, t); | ||
| 194 | } | ||
diff --git a/src/core/transform_with_switch.h b/src/core/transform_with_switch.h index d3a108c..429e656 100644 --- a/src/core/transform_with_switch.h +++ b/src/core/transform_with_switch.h | |||
| @@ -17,6 +17,11 @@ | |||
| 17 | invertco(compose(compose(_trans_cube_ ## T, c), \ | 17 | invertco(compose(compose(_trans_cube_ ## T, c), \ |
| 18 | _trans_cube_ ## T ## _inverse)) | 18 | _trans_cube_ ## T ## _inverse)) |
| 19 | 19 | ||
| 20 | _static cube_t transform_edges(cube_t, uint8_t); | ||
| 21 | _static cube_t transform_corners(cube_t, uint8_t); | ||
| 22 | _static cube_t transform(cube_t, uint8_t); | ||
| 23 | _static cube_t applytrans(cube_t, const char *); | ||
| 24 | |||
| 20 | _static cube_t | 25 | _static cube_t |
| 21 | transform_edges(cube_t c, uint8_t t) | 26 | transform_edges(cube_t c, uint8_t t) |
| 22 | { | 27 | { |
| @@ -334,3 +339,16 @@ transform(cube_t c, uint8_t t) | |||
| 334 | return zero; | 339 | return zero; |
| 335 | } | 340 | } |
| 336 | } | 341 | } |
| 342 | |||
| 343 | _static cube_t | ||
| 344 | applytrans(cube_t cube, const char *buf) | ||
| 345 | { | ||
| 346 | uint8_t t; | ||
| 347 | |||
| 348 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 349 | "transformation error: inconsistent cube\n"); | ||
| 350 | |||
| 351 | t = readtrans(buf); | ||
| 352 | |||
| 353 | return transform(cube, t); | ||
| 354 | } | ||
