aboutsummaryrefslogtreecommitdiff
path: root/old/2021-11-10-beforeremovingchecker/alg.c
diff options
context:
space:
mode:
Diffstat (limited to 'old/2021-11-10-beforeremovingchecker/alg.c')
-rw-r--r--old/2021-11-10-beforeremovingchecker/alg.c366
1 files changed, 0 insertions, 366 deletions
diff --git a/old/2021-11-10-beforeremovingchecker/alg.c b/old/2021-11-10-beforeremovingchecker/alg.c
deleted file mode 100644
index 06c2d7b..0000000
--- a/old/2021-11-10-beforeremovingchecker/alg.c
+++ /dev/null
@@ -1,366 +0,0 @@
1#include "alg.h"
2
3/* Local functions ***********************************************************/
4
5static void free_alglistnode(AlgListNode *aln);
6static void realloc_alg(Alg *alg, int n);
7
8/* Movesets ******************************************************************/
9
10bool
11moveset_HTM(Move m)
12{
13 return m >= U && m <= B3;
14}
15
16bool
17moveset_URF(Move m)
18{
19 Move b = base_move(m);
20
21 return b == U || b == R || b == F;
22}
23
24bool
25moveset_eofb(Move m)
26{
27 Move b = base_move(m);
28
29 return b == U || b == D || b == R || b == L ||
30 ((b == F || b == B) && m == b+1);
31}
32
33bool
34moveset_drud(Move m)
35{
36 Move b = base_move(m);
37
38 return b == U || b == D ||
39 ((b == R || b == L || b == F || b == B) && m == b + 1);
40}
41
42bool
43moveset_htr(Move m)
44{
45 Move b = base_move(m);
46
47 return moveset_HTM(m) && m == b + 1;
48}
49
50
51/* Functions *****************************************************************/
52
53void
54append_alg(AlgList *l, Alg *alg)
55{
56 AlgListNode *node = malloc(sizeof(AlgListNode));
57 int i;
58
59 node->alg = new_alg("");
60 for (i = 0; i < alg->len; i++)
61 append_move(node->alg, alg->move[i], alg->inv[i]);
62 node->next = NULL;
63
64 if (++l->len == 1)
65 l->first = node;
66 else
67 l->last->next = node;
68 l->last = node;
69}
70
71void
72append_move(Alg *alg, Move m, bool inverse)
73{
74 if (alg->len == alg->allocated)
75 realloc_alg(alg, 2*alg->len);
76
77 alg->move[alg->len] = m;
78 alg->inv [alg->len] = inverse;
79 alg->len++;
80}
81
82Move
83base_move(Move m)
84{
85 if (m == NULLMOVE)
86 return NULLMOVE;
87 else
88 return m - (m-1)%3;
89}
90
91void
92compose_alg(Alg *alg1, Alg *alg2)
93{
94 int i;
95
96 for (i = 0; i < alg2->len; i++)
97 append_move(alg1, alg2->move[i], alg2->inv[i]);
98}
99
100void
101free_alg(Alg *alg)
102{
103 free(alg->move);
104 free(alg->inv);
105 free(alg);
106}
107
108void
109free_alglist(AlgList *l)
110{
111 AlgListNode *aux, *i = l->first;
112
113 while (i != NULL) {
114 aux = i->next;
115 free_alglistnode(i);
116 i = aux;
117 }
118 free(l);
119}
120
121static void
122free_alglistnode(AlgListNode *aln)
123{
124 free_alg(aln->alg);
125 free(aln);
126}
127
128Alg *
129inverse_alg(Alg *alg)
130{
131 Alg *ret = new_alg("");
132 int i;
133
134 for (i = alg->len-1; i >= 0; i--)
135 append_move(ret, inverse_move(alg->move[i]), alg->inv[i]);
136
137 return ret;
138}
139
140Move
141inverse_move(Move m)
142{
143 return m == NULLMOVE ? NULLMOVE : m + 2 - 2*((m-1) % 3);
144}
145
146char *
147move_string(Move m)
148{
149 static char move_string_aux[NMOVES][7] = {
150 [NULLMOVE] = "-",
151 [U] = "U", [U2] = "U2", [U3] = "U\'",
152 [D] = "D", [D2] = "D2", [D3] = "D\'",
153 [R] = "R", [R2] = "R2", [R3] = "R\'",
154 [L] = "L", [L2] = "L2", [L3] = "L\'",
155 [F] = "F", [F2] = "F2", [F3] = "F\'",
156 [B] = "B", [B2] = "B2", [B3] = "B\'",
157 [Uw] = "Uw", [Uw2] = "Uw2", [Uw3] = "Uw\'",
158 [Dw] = "Dw", [Dw2] = "Dw2", [Dw3] = "Dw\'",
159 [Rw] = "Rw", [Rw2] = "Rw2", [Rw3] = "Rw\'",
160 [Lw] = "Lw", [Lw2] = "Lw2", [Lw3] = "Lw\'",
161 [Fw] = "Fw", [Fw2] = "Fw2", [Fw3] = "Fw\'",
162 [Bw] = "Bw", [Bw2] = "Bw2", [Bw3] = "Bw\'",
163 [M] = "M", [M2] = "M2", [M3] = "M\'",
164 [E] = "E", [E2] = "E2", [E3] = "E\'",
165 [S] = "S", [S2] = "S2", [S3] = "S\'",
166 [x] = "x", [x2] = "x2", [x3] = "x\'",
167 [y] = "y", [y2] = "y2", [y3] = "y\'",
168 [z] = "z", [z2] = "z2", [z3] = "z\'",
169 };
170
171 return move_string_aux[m];
172}
173
174void
175movelist_to_position(Move *movelist, int *position)
176{
177 Move m;
178
179 for (m = 0; m < NMOVES && movelist[m] != NULLMOVE; m++)
180 position[movelist[m]] = m;
181}
182
183void
184moveset_to_list(Moveset ms, Move *r)
185{
186 int n = 0;
187 Move i;
188
189 if (ms == NULL) {
190 fprintf(stderr, "Error: no moveset given\n");
191 return;
192 }
193
194 for (i = U; i < NMOVES; i++)
195 if (ms(i))
196 r[n++] = i;
197
198 r[n] = NULLMOVE;
199}
200
201Alg *
202new_alg(char *str)
203{
204 Alg *alg = malloc(sizeof(Alg));
205 int i;
206 bool niss = false, move_read;
207 Move j, m;
208
209 alg->move = malloc(30 * sizeof(Move));
210 alg->inv = malloc(30 * sizeof(bool));
211 alg->allocated = 30;
212 alg->len = 0;
213
214 for (i = 0; str[i]; i++) {
215 if (str[i] == ' ' || str[i] == '\t' || str[i] == '\n')
216 continue;
217
218 if (str[i] == '(' && niss) {
219 fprintf(stderr, "Error reading moves: nested ( )\n");
220 return alg;
221 }
222
223 if (str[i] == ')' && !niss) {
224 fprintf(stderr, "Error reading moves: unmatched )\n");
225 return alg;
226 }
227
228 if (str[i] == '(' || str[i] == ')') {
229 niss = !niss;
230 continue;
231 }
232
233 move_read = false;
234 for (j = 0; j < NMOVES; j++) {
235 if (str[i] == move_string(j)[0] ||
236 (str[i] >= 'a' && str[i] <= 'z' &&
237 str[i] == move_string(j)[0]-('A'-'a') && j<=B)) {
238 m = j;
239 if (str[i] >= 'a' && str[i] <= 'z' && j<=B) {
240 m += Uw - U;
241 }
242 if (m <= B && str[i+1]=='w') {
243 m += Uw - U;
244 i++;
245 }
246 if (str[i+1]=='2') {
247 m += 1;
248 i++;
249 } else if (str[i+1] == '\'' ||
250 str[i+1] == '3' ||
251 str[i+1] == '`' ) {
252 m += 2;
253 i++;
254 } else if ((int)str[i+1] == -62 &&
255 (int)str[i+2] == -76) {
256 /* Weird apostrophe */
257 m += 2;
258 i += 2;
259 } else if ((int)str[i+1] == -30 &&
260 (int)str[i+2] == -128 &&
261 (int)str[i+3] == -103) {
262 /* MacOS apostrophe */
263 m += 2;
264 i += 3;
265 }
266 append_move(alg, m, niss);
267 move_read = true;
268 break;
269 }
270 }
271
272 if (!move_read) {
273 alg = new_alg("");
274 return alg;
275 }
276 }
277
278 return alg;
279}
280
281AlgList *
282new_alglist()
283{
284 AlgList *ret = malloc(sizeof(AlgList));
285
286 ret->len = 0;
287 ret->first = NULL;
288 ret->last = NULL;
289
290 return ret;
291}
292
293Alg *
294on_inverse(Alg *alg)
295{
296 Alg *ret = new_alg("");
297 int i;
298
299 for (i = 0; i < alg->len; i++)
300 append_move(ret, alg->move[i], !alg->inv[i]);
301
302 return ret;
303}
304
305void
306print_alg(Alg *alg, bool l)
307{
308 /* TODO: make it possible to print to stdout or to string */
309 /* Maybe just return a string */
310 char fill[4];
311 int i;
312 bool niss = false;
313
314 for (i = 0; i < alg->len; i++) {
315 if (!niss && alg->inv[i])
316 strcpy(fill, i == 0 ? "(" : " (");
317 if (niss && !alg->inv[i])
318 strcpy(fill, ") ");
319 if (niss == alg->inv[i])
320 strcpy(fill, i == 0 ? "" : " ");
321
322 printf("%s%s", fill, move_string(alg->move[i]));
323 niss = alg->inv[i];
324 }
325
326 if (niss)
327 printf(")");
328 if (l)
329 printf(" (%d)", alg->len);
330
331 printf("\n");
332}
333
334void
335print_alglist(AlgList *al, bool l)
336{
337 AlgListNode *i;
338
339 for (i = al->first; i != NULL; i = i->next)
340 print_alg(i->alg, l);
341}
342
343static void
344realloc_alg(Alg *alg, int n)
345{
346 if (alg == NULL) {
347 fprintf(stderr, "Error: trying to reallocate NULL alg.\n");
348 return;
349 }
350
351 if (n < alg->len) {
352 fprintf(stderr, "Error: alg too long for reallocation ");
353 fprintf(stderr, "(%d vs %d)\n", alg->len, n);
354 return;
355 }
356
357 if (n > 1000000) {
358 fprintf(stderr, "Warning: very long alg,");
359 fprintf(stderr, "something might go wrong.\n");
360 }
361
362 alg->move = realloc(alg->move, n * sizeof(int));
363 alg->inv = realloc(alg->inv, n * sizeof(int));
364 alg->allocated = n;
365}
366

Generated with cgit - Back to sebastiano.tronto.net