aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/cube.h66
-rw-r--r--src/core/moves.h88
-rw-r--r--src/core/transform.h18
-rw-r--r--src/core/transform_with_switch.h18
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
24cubefromarray(uint8_t c[static 8], uint8_t e[static 12]) 11cubefromarray(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
153applymoves(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
173applymoves_finish:
174 return cube;
175
176applymoves_error:
177 LOG("applymoves error\n");
178 return zero;
179}
180
181_static cube_t
182frommoves(const char *buf)
183{
184 return applymoves(solved, buf);
185}
186
187_static void 139_static void
188getcube_fix(int64_t *ep, int64_t *eo, int64_t *cp, int64_t *co) 140getcube_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
232applytrans(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/*
245TODO transform is now relegated to a separated file because it is too long.
246It 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
23allowednextmove(uint8_t *moves, uint8_t n) 20allowednextmove(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
54allowednextmoveH48(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
82inverse_trans(uint8_t t) 51inverse_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
198invertpremoves(uint8_t *moves, uint8_t nmoves) 167invertmoves(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++) 176applymoves(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
196applymoves_finish:
197 return cube;
198
199applymoves_error:
200 LOG("applymoves error\n");
201 return zero;
202}
203
204_static cube_t
205frommoves(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
22static cube_t cube_trans_table[48] = { 27static 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
184applytrans(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
21transform_edges(cube_t c, uint8_t t) 26transform_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
344applytrans(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}

Generated with cgit - Back to sebastiano.tronto.net