diff options
Diffstat (limited to '')
| -rw-r--r-- | src/core/cube.h | 4 | ||||
| -rw-r--r-- | src/core/io_moves.h | 9 | ||||
| -rw-r--r-- | src/core/moves.h | 7 | ||||
| -rw-r--r-- | src/nissy.c | 115 | ||||
| -rw-r--r-- | src/nissy.h | 130 | ||||
| -rw-r--r-- | src/solvers/h48/solve.h | 10 | ||||
| -rw-r--r-- | src/solvers/h48/solve_multithread.h | 10 |
7 files changed, 110 insertions, 175 deletions
diff --git a/src/core/cube.h b/src/core/cube.h index a3c3e14..49d868c 100644 --- a/src/core/cube.h +++ b/src/core/cube.h | |||
| @@ -4,7 +4,7 @@ STATIC bool isconsistent(cube_t); | |||
| 4 | STATIC bool issolvable(cube_t); | 4 | STATIC bool issolvable(cube_t); |
| 5 | STATIC bool issolved(cube_t); | 5 | STATIC bool issolved(cube_t); |
| 6 | STATIC bool iserror(cube_t); | 6 | STATIC bool iserror(cube_t); |
| 7 | STATIC void getcube_fix(int64_t *, int64_t *, int64_t *, int64_t *); | 7 | STATIC void getcube_fix(long long *, long long *, long long *, long long *); |
| 8 | 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); |
| 9 | 9 | ||
| 10 | /* This is used only in tests, use SOLVED_CUBE directly everywhere else */ | 10 | /* This is used only in tests, use SOLVED_CUBE directly everywhere else */ |
| @@ -128,7 +128,7 @@ iserror(cube_t cube) | |||
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | STATIC void | 130 | STATIC void |
| 131 | getcube_fix(int64_t *ep, int64_t *eo, int64_t *cp, int64_t *co) | 131 | getcube_fix(long long *ep, long long *eo, long long *cp, long long *co) |
| 132 | { | 132 | { |
| 133 | uint8_t e[12], c[8], coarr[8]; | 133 | uint8_t e[12], c[8], coarr[8]; |
| 134 | 134 | ||
diff --git a/src/core/io_moves.h b/src/core/io_moves.h index be43568..806887b 100644 --- a/src/core/io_moves.h +++ b/src/core/io_moves.h | |||
| @@ -43,6 +43,7 @@ writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) | |||
| 43 | { | 43 | { |
| 44 | int i; | 44 | int i; |
| 45 | uint64_t len; | 45 | uint64_t len; |
| 46 | int64_t written; | ||
| 46 | const char *s; | 47 | const char *s; |
| 47 | char *b; | 48 | char *b; |
| 48 | 49 | ||
| @@ -51,16 +52,16 @@ writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) | |||
| 51 | return NISSY_ERROR_BUFFER_SIZE; | 52 | return NISSY_ERROR_BUFFER_SIZE; |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | for (i = 0, b = buf; i < n; i++, b++, buf_size--) { | 55 | for (i = 0, b = buf, written = 0; i < n; i++, b++, written++) { |
| 55 | s = movestr[m[i]]; | 56 | s = movestr[m[i]]; |
| 56 | len = strlen(s); | 57 | len = strlen(s); |
| 57 | if (len >= buf_size) { | 58 | if (len + written >= buf_size) { |
| 58 | LOG("Error: the given buffer is too small for " | 59 | LOG("Error: the given buffer is too small for " |
| 59 | "writing the given moves.\n"); | 60 | "writing the given moves.\n"); |
| 60 | goto writemoves_error; | 61 | goto writemoves_error; |
| 61 | } | 62 | } |
| 62 | memcpy(b, s, len); | 63 | memcpy(b, s, len); |
| 63 | buf_size -= len; | 64 | written += len; |
| 64 | b += len; | 65 | b += len; |
| 65 | *b = ' '; | 66 | *b = ' '; |
| 66 | } | 67 | } |
| @@ -69,7 +70,7 @@ writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) | |||
| 69 | b--; /* Remove last space */ | 70 | b--; /* Remove last space */ |
| 70 | *b = '\0'; | 71 | *b = '\0'; |
| 71 | 72 | ||
| 72 | return buf_size; | 73 | return written; |
| 73 | 74 | ||
| 74 | writemoves_error: | 75 | writemoves_error: |
| 75 | *buf = '\0'; | 76 | *buf = '\0'; |
diff --git a/src/core/moves.h b/src/core/moves.h index 28abc7f..d07bc19 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -15,7 +15,6 @@ STATIC void invertmoves(uint8_t *, uint8_t, uint8_t *); | |||
| 15 | 15 | ||
| 16 | STATIC int readmoves(const char *, int, uint8_t *); | 16 | STATIC int readmoves(const char *, int, uint8_t *); |
| 17 | STATIC cube_t applymoves(cube_t, const char *); | 17 | STATIC cube_t applymoves(cube_t, const char *); |
| 18 | STATIC cube_t frommoves(const char *); | ||
| 19 | 18 | ||
| 20 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ | 19 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ |
| 21 | RET_ERROR, ARG_ACTION) \ | 20 | RET_ERROR, ARG_ACTION) \ |
| @@ -246,9 +245,3 @@ applymoves(cube_t cube, const char *buf) | |||
| 246 | 245 | ||
| 247 | return cube; | 246 | return cube; |
| 248 | } | 247 | } |
| 249 | |||
| 250 | STATIC cube_t | ||
| 251 | frommoves(const char *buf) | ||
| 252 | { | ||
| 253 | return applymoves(SOLVED_CUBE, buf); | ||
| 254 | } | ||
diff --git a/src/nissy.c b/src/nissy.c index 6b083e0..fe2747b 100644 --- a/src/nissy.c +++ b/src/nissy.c | |||
| @@ -12,7 +12,7 @@ | |||
| 12 | #include "solvers/solvers.h" | 12 | #include "solvers/solvers.h" |
| 13 | 13 | ||
| 14 | int parse_h48_solver(const char *, uint8_t [static 1], uint8_t [static 1]); | 14 | int parse_h48_solver(const char *, uint8_t [static 1], uint8_t [static 1]); |
| 15 | STATIC int64_t write_result(cube_t, char [static NISSY_SIZE_B32]); | 15 | STATIC long long write_result(cube_t, char [static NISSY_SIZE_B32]); |
| 16 | STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN], | 16 | STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN], |
| 17 | const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t); | 17 | const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t); |
| 18 | STATIC bool checkdata(const char *, const tableinfo_t *); | 18 | STATIC bool checkdata(const char *, const tableinfo_t *); |
| @@ -20,7 +20,7 @@ STATIC bool checkdata(const char *, const tableinfo_t *); | |||
| 20 | #define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F } | 20 | #define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F } |
| 21 | struct { | 21 | struct { |
| 22 | char *option; | 22 | char *option; |
| 23 | void (*fix)(int64_t *, int64_t *, int64_t *, int64_t *); | 23 | void (*fix)(long long *, long long *, long long *, long long *); |
| 24 | } getcube_options[] = { | 24 | } getcube_options[] = { |
| 25 | GETCUBE_OPTIONS("fix", getcube_fix), | 25 | GETCUBE_OPTIONS("fix", getcube_fix), |
| 26 | GETCUBE_OPTIONS(NULL, NULL) | 26 | GETCUBE_OPTIONS(NULL, NULL) |
| @@ -105,7 +105,7 @@ distribution_equal( | |||
| 105 | return wrong == 0; | 105 | return wrong == 0; |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | STATIC int64_t | 108 | STATIC long long |
| 109 | write_result(cube_t cube, char result[static NISSY_SIZE_B32]) | 109 | write_result(cube_t cube, char result[static NISSY_SIZE_B32]) |
| 110 | { | 110 | { |
| 111 | writecube("B32", cube, NISSY_SIZE_B32, result); | 111 | writecube("B32", cube, NISSY_SIZE_B32, result); |
| @@ -118,7 +118,7 @@ write_result(cube_t cube, char result[static NISSY_SIZE_B32]) | |||
| 118 | return NISSY_OK; | 118 | return NISSY_OK; |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | int64_t | 121 | long long |
| 122 | nissy_compose( | 122 | nissy_compose( |
| 123 | const char cube[static NISSY_SIZE_B32], | 123 | const char cube[static NISSY_SIZE_B32], |
| 124 | const char permutation[static NISSY_SIZE_B32], | 124 | const char permutation[static NISSY_SIZE_B32], |
| @@ -126,7 +126,7 @@ nissy_compose( | |||
| 126 | ) | 126 | ) |
| 127 | { | 127 | { |
| 128 | cube_t c, p, res; | 128 | cube_t c, p, res; |
| 129 | int64_t err; | 129 | long long err; |
| 130 | 130 | ||
| 131 | c = readcube("B32", cube); | 131 | c = readcube("B32", cube); |
| 132 | 132 | ||
| @@ -159,14 +159,14 @@ nissy_compose_error: | |||
| 159 | return err; | 159 | return err; |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | int64_t | 162 | long long |
| 163 | nissy_inverse( | 163 | nissy_inverse( |
| 164 | const char cube[static NISSY_SIZE_B32], | 164 | const char cube[static NISSY_SIZE_B32], |
| 165 | char result[static NISSY_SIZE_B32] | 165 | char result[static NISSY_SIZE_B32] |
| 166 | ) | 166 | ) |
| 167 | { | 167 | { |
| 168 | cube_t c, res; | 168 | cube_t c, res; |
| 169 | int64_t err; | 169 | long long err; |
| 170 | 170 | ||
| 171 | c = readcube("B32", cube); | 171 | c = readcube("B32", cube); |
| 172 | 172 | ||
| @@ -191,7 +191,7 @@ nissy_inverse_error: | |||
| 191 | return err; | 191 | return err; |
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | int64_t | 194 | long long |
| 195 | nissy_applymoves( | 195 | nissy_applymoves( |
| 196 | const char cube[static NISSY_SIZE_B32], | 196 | const char cube[static NISSY_SIZE_B32], |
| 197 | const char *moves, | 197 | const char *moves, |
| @@ -199,7 +199,7 @@ nissy_applymoves( | |||
| 199 | ) | 199 | ) |
| 200 | { | 200 | { |
| 201 | cube_t c, res; | 201 | cube_t c, res; |
| 202 | int64_t err; | 202 | long long err; |
| 203 | 203 | ||
| 204 | if (moves == NULL) { | 204 | if (moves == NULL) { |
| 205 | LOG("Error: 'moves' argument is NULL\n"); | 205 | LOG("Error: 'moves' argument is NULL\n"); |
| @@ -230,7 +230,7 @@ nissy_applymoves_error: | |||
| 230 | return err; | 230 | return err; |
| 231 | } | 231 | } |
| 232 | 232 | ||
| 233 | int64_t | 233 | long long |
| 234 | nissy_applytrans( | 234 | nissy_applytrans( |
| 235 | const char cube[static NISSY_SIZE_B32], | 235 | const char cube[static NISSY_SIZE_B32], |
| 236 | const char transformation[static NISSY_SIZE_TRANSFORMATION], | 236 | const char transformation[static NISSY_SIZE_TRANSFORMATION], |
| @@ -238,7 +238,7 @@ nissy_applytrans( | |||
| 238 | ) | 238 | ) |
| 239 | { | 239 | { |
| 240 | cube_t c, res; | 240 | cube_t c, res; |
| 241 | int64_t err; | 241 | long long err; |
| 242 | 242 | ||
| 243 | c = readcube("B32", cube); | 243 | c = readcube("B32", cube); |
| 244 | 244 | ||
| @@ -263,47 +263,17 @@ nissy_applytrans_error: | |||
| 263 | return err; | 263 | return err; |
| 264 | } | 264 | } |
| 265 | 265 | ||
| 266 | int64_t | 266 | long long |
| 267 | nissy_frommoves( | ||
| 268 | const char *moves, | ||
| 269 | char result[static NISSY_SIZE_B32] | ||
| 270 | ) | ||
| 271 | { | ||
| 272 | cube_t res; | ||
| 273 | int64_t err; | ||
| 274 | |||
| 275 | if (moves == NULL) { | ||
| 276 | LOG("Error: 'moves' argument is NULL\n"); | ||
| 277 | err = NISSY_ERROR_NULL_POINTER; | ||
| 278 | goto nissy_frommoves_error; | ||
| 279 | } | ||
| 280 | |||
| 281 | res = applymoves(SOLVED_CUBE, moves); | ||
| 282 | |||
| 283 | if (!isconsistent(res)) { | ||
| 284 | /* Assume we got a reasonable error message from applymoves */ | ||
| 285 | err = NISSY_ERROR_INVALID_MOVES; | ||
| 286 | goto nissy_frommoves_error; | ||
| 287 | } | ||
| 288 | |||
| 289 | return write_result(res, result); | ||
| 290 | |||
| 291 | nissy_frommoves_error: | ||
| 292 | writecube("B32", ZERO_CUBE, NISSY_SIZE_B32, result); | ||
| 293 | return err; | ||
| 294 | } | ||
| 295 | |||
| 296 | int64_t | ||
| 297 | nissy_convert( | 267 | nissy_convert( |
| 298 | const char *format_in, | 268 | const char *format_in, |
| 299 | const char *format_out, | 269 | const char *format_out, |
| 300 | const char *cube_string, | 270 | const char *cube_string, |
| 301 | uint64_t result_size, | 271 | unsigned result_size, |
| 302 | char result[result_size] | 272 | char result[result_size] |
| 303 | ) | 273 | ) |
| 304 | { | 274 | { |
| 305 | cube_t c; | 275 | cube_t c; |
| 306 | int64_t err; | 276 | long long err; |
| 307 | 277 | ||
| 308 | if (format_in == NULL) { | 278 | if (format_in == NULL) { |
| 309 | LOG("Error: 'format_in' argument is NULL\n"); | 279 | LOG("Error: 'format_in' argument is NULL\n"); |
| @@ -337,12 +307,12 @@ nissy_convert_error: | |||
| 337 | return err; | 307 | return err; |
| 338 | } | 308 | } |
| 339 | 309 | ||
| 340 | int64_t | 310 | long long |
| 341 | nissy_getcube( | 311 | nissy_getcube( |
| 342 | int64_t ep, | 312 | long long ep, |
| 343 | int64_t eo, | 313 | long long eo, |
| 344 | int64_t cp, | 314 | long long cp, |
| 345 | int64_t co, | 315 | long long co, |
| 346 | const char *options, | 316 | const char *options, |
| 347 | char result[static NISSY_SIZE_B32] | 317 | char result[static NISSY_SIZE_B32] |
| 348 | ) | 318 | ) |
| @@ -371,7 +341,7 @@ nissy_getcube( | |||
| 371 | return write_result(c, result); | 341 | return write_result(c, result); |
| 372 | } | 342 | } |
| 373 | 343 | ||
| 374 | int64_t | 344 | long long |
| 375 | nissy_datasize( | 345 | nissy_datasize( |
| 376 | const char *solver | 346 | const char *solver |
| 377 | ) | 347 | ) |
| @@ -385,7 +355,7 @@ nissy_datasize( | |||
| 385 | return nissy_gendata(solver, 0, NULL); | 355 | return nissy_gendata(solver, 0, NULL); |
| 386 | } | 356 | } |
| 387 | 357 | ||
| 388 | int64_t | 358 | long long |
| 389 | nissy_datainfo( | 359 | nissy_datainfo( |
| 390 | uint64_t data_size, | 360 | uint64_t data_size, |
| 391 | const char data[data_size], | 361 | const char data[data_size], |
| @@ -394,7 +364,7 @@ nissy_datainfo( | |||
| 394 | { | 364 | { |
| 395 | uint8_t i; | 365 | uint8_t i; |
| 396 | tableinfo_t info; | 366 | tableinfo_t info; |
| 397 | int64_t ret; | 367 | long long ret; |
| 398 | 368 | ||
| 399 | ret = readtableinfo(data_size, data, &info); | 369 | ret = readtableinfo(data_size, data, &info); |
| 400 | if (ret != 0) | 370 | if (ret != 0) |
| @@ -434,10 +404,10 @@ nissy_datainfo( | |||
| 434 | return NISSY_OK; | 404 | return NISSY_OK; |
| 435 | } | 405 | } |
| 436 | 406 | ||
| 437 | int64_t | 407 | long long |
| 438 | nissy_gendata( | 408 | nissy_gendata( |
| 439 | const char *solver, | 409 | const char *solver, |
| 440 | uint64_t data_size, | 410 | unsigned long long data_size, |
| 441 | char data[data_size] | 411 | char data[data_size] |
| 442 | ) | 412 | ) |
| 443 | { | 413 | { |
| @@ -463,9 +433,9 @@ nissy_gendata( | |||
| 463 | } | 433 | } |
| 464 | } | 434 | } |
| 465 | 435 | ||
| 466 | int64_t | 436 | long long |
| 467 | nissy_checkdata( | 437 | nissy_checkdata( |
| 468 | uint64_t data_size, | 438 | unsigned long long data_size, |
| 469 | const char data[data_size] | 439 | const char data[data_size] |
| 470 | ) | 440 | ) |
| 471 | { | 441 | { |
| @@ -488,18 +458,18 @@ nissy_checkdata( | |||
| 488 | return NISSY_OK; | 458 | return NISSY_OK; |
| 489 | } | 459 | } |
| 490 | 460 | ||
| 491 | int64_t | 461 | long long |
| 492 | nissy_solve( | 462 | nissy_solve( |
| 493 | const char cube[static NISSY_SIZE_B32], | 463 | const char cube[static NISSY_SIZE_B32], |
| 494 | const char *solver, | 464 | const char *solver, |
| 495 | uint8_t nissflag, | 465 | unsigned nissflag, |
| 496 | int8_t minmoves, | 466 | unsigned minmoves, |
| 497 | int8_t maxmoves, | 467 | unsigned maxmoves, |
| 498 | int64_t maxsols, | 468 | unsigned maxsols, |
| 499 | int8_t optimal, | 469 | int optimal, |
| 500 | uint64_t data_size, | 470 | unsigned long long data_size, |
| 501 | const char data[data_size], | 471 | const char data[data_size], |
| 502 | uint64_t sols_size, | 472 | unsigned sols_size, |
| 503 | char sols[sols_size] | 473 | char sols[sols_size] |
| 504 | ) | 474 | ) |
| 505 | { | 475 | { |
| @@ -524,21 +494,6 @@ nissy_solve( | |||
| 524 | return NISSY_ERROR_UNSOLVABLE_CUBE; | 494 | return NISSY_ERROR_UNSOLVABLE_CUBE; |
| 525 | } | 495 | } |
| 526 | 496 | ||
| 527 | if (minmoves < 0) { | ||
| 528 | LOG("solve: 'minmoves' is negative, setting it to 0\n"); | ||
| 529 | minmoves = 0; | ||
| 530 | } | ||
| 531 | |||
| 532 | if (maxmoves < 0) { | ||
| 533 | LOG("solve: 'maxmoves' is negative, setting it to 20\n"); | ||
| 534 | maxmoves = 20; | ||
| 535 | } | ||
| 536 | |||
| 537 | if (maxsols < 0) { | ||
| 538 | LOG("solve: 'maxsols' is negative, stopping\n"); | ||
| 539 | return NISSY_ERROR_OPTIONS; | ||
| 540 | } | ||
| 541 | |||
| 542 | if (maxsols == 0) { | 497 | if (maxsols == 0) { |
| 543 | LOG("solve: 'maxsols' is 0, returning no solution\n"); | 498 | LOG("solve: 'maxsols' is 0, returning no solution\n"); |
| 544 | return 0; | 499 | return 0; |
| @@ -565,7 +520,7 @@ nissy_solve( | |||
| 565 | } | 520 | } |
| 566 | } | 521 | } |
| 567 | 522 | ||
| 568 | int64_t | 523 | long long |
| 569 | nissy_setlogger( | 524 | nissy_setlogger( |
| 570 | void (*log)(const char *, ...) | 525 | void (*log)(const char *, ...) |
| 571 | ) | 526 | ) |
diff --git a/src/nissy.h b/src/nissy.h index 6d78778..8f4f363 100644 --- a/src/nissy.h +++ b/src/nissy.h | |||
| @@ -1,13 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | This is libnissy (temporarily also known as h48), a Rubik's cube library. | 2 | This is libnissy (temporarily also known as h48), a Rubik's cube library. |
| 3 | 3 | ||
| 4 | If you include this file, you should also use the following includes: | ||
| 5 | |||
| 6 | #include <inttypes.h> | ||
| 7 | #include <stdarg.h> | ||
| 8 | #include <stdbool.h> | ||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | All the functions return 0 or a positive integer in case of success and | 4 | All the functions return 0 or a positive integer in case of success and |
| 12 | a negative integer in case of error, unless otherwise specified. See at | 5 | a negative integer in case of error, unless otherwise specified. See at |
| 13 | the bottom of this file for the list of error codes and their meaning. | 6 | the bottom of this file for the list of error codes and their meaning. |
| @@ -24,20 +17,29 @@ A transformation must be given in the format | |||
| 24 | for example 'rotation UF' or 'mirrored BL'. | 17 | for example 'rotation UF' or 'mirrored BL'. |
| 25 | */ | 18 | */ |
| 26 | 19 | ||
| 20 | |||
| 21 | /* Constants *****************************************************************/ | ||
| 22 | |||
| 27 | /* Some constants for size for I/O buffers */ | 23 | /* Some constants for size for I/O buffers */ |
| 28 | #define NISSY_SIZE_B32 UINT64_C(22) | 24 | #define NISSY_SIZE_B32 22U |
| 29 | #define NISSY_SIZE_H48 UINT64_C(88) | 25 | #define NISSY_SIZE_H48 88U |
| 30 | #define NISSY_SIZE_TRANSFORMATION UINT64_C(12) | 26 | #define NISSY_SIZE_TRANSFORMATION 12U |
| 31 | 27 | ||
| 32 | /* Flags for NISS options */ | 28 | /* Flags for NISS options */ |
| 33 | #define NISSY_NISSFLAG_NORMAL UINT8_C(1) | 29 | #define NISSY_NISSFLAG_NORMAL 1U |
| 34 | #define NISSY_NISSFLAG_INVERSE UINT8_C(2) | 30 | #define NISSY_NISSFLAG_INVERSE 2U |
| 35 | #define NISSY_NISSFLAG_MIXED UINT8_C(4) | 31 | #define NISSY_NISSFLAG_MIXED 4U |
| 36 | #define NISSY_NISSFLAG_LINEAR \ | 32 | #define NISSY_NISSFLAG_LINEAR \ |
| 37 | (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_INVERSE) | 33 | (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_INVERSE) |
| 38 | #define NISSY_NISSFLAG_ALL \ | 34 | #define NISSY_NISSFLAG_ALL \ |
| 39 | (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_INVERSE | NISSY_NISSFLAG_MIXED) | 35 | (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_INVERSE | NISSY_NISSFLAG_MIXED) |
| 40 | 36 | ||
| 37 | /* The solved cube in B32 format */ | ||
| 38 | #define NISSY_SOLVED_CUBE "ABCDEFGH=ABCDEFGHIJKL" | ||
| 39 | |||
| 40 | |||
| 41 | /* Library functions *********************************************************/ | ||
| 42 | |||
| 41 | /* | 43 | /* |
| 42 | Apply the secod argument as a permutation on the first argument. | 44 | Apply the secod argument as a permutation on the first argument. |
| 43 | 45 | ||
| @@ -56,7 +58,7 @@ Return values: | |||
| 56 | NISSY_ERROR_INVALID_CUBE - At least one of the given cubes is invalid. | 58 | NISSY_ERROR_INVALID_CUBE - At least one of the given cubes is invalid. |
| 57 | NISSY_ERROR_UNKNOWN - An unknown error occurred. | 59 | NISSY_ERROR_UNKNOWN - An unknown error occurred. |
| 58 | */ | 60 | */ |
| 59 | int64_t nissy_compose( | 61 | long long nissy_compose( |
| 60 | const char cube[static NISSY_SIZE_B32], | 62 | const char cube[static NISSY_SIZE_B32], |
| 61 | const char permutation[static NISSY_SIZE_B32], | 63 | const char permutation[static NISSY_SIZE_B32], |
| 62 | char result[static NISSY_SIZE_B32] | 64 | char result[static NISSY_SIZE_B32] |
| @@ -77,7 +79,7 @@ Return values: | |||
| 77 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. | 79 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. |
| 78 | NISSY_ERROR_UNKNOWN - An unknown error occurred. | 80 | NISSY_ERROR_UNKNOWN - An unknown error occurred. |
| 79 | */ | 81 | */ |
| 80 | int64_t nissy_inverse( | 82 | long long nissy_inverse( |
| 81 | const char cube[static NISSY_SIZE_B32], | 83 | const char cube[static NISSY_SIZE_B32], |
| 82 | char result[static NISSY_SIZE_B32] | 84 | char result[static NISSY_SIZE_B32] |
| 83 | ); | 85 | ); |
| @@ -99,7 +101,7 @@ Return values: | |||
| 99 | NISSY_ERROR_INVALID_MOVES - The given moves are invalid. | 101 | NISSY_ERROR_INVALID_MOVES - The given moves are invalid. |
| 100 | NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL. | 102 | NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL. |
| 101 | */ | 103 | */ |
| 102 | int64_t nissy_applymoves( | 104 | long long nissy_applymoves( |
| 103 | const char cube[static NISSY_SIZE_B32], | 105 | const char cube[static NISSY_SIZE_B32], |
| 104 | const char *moves, | 106 | const char *moves, |
| 105 | char result[static NISSY_SIZE_B32] | 107 | char result[static NISSY_SIZE_B32] |
| @@ -120,33 +122,13 @@ Return values: | |||
| 120 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. | 122 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. |
| 121 | NISSY_ERROR_INVALID_TRANS - The given transformation is invalid. | 123 | NISSY_ERROR_INVALID_TRANS - The given transformation is invalid. |
| 122 | */ | 124 | */ |
| 123 | int64_t nissy_applytrans( | 125 | long long nissy_applytrans( |
| 124 | const char cube[static NISSY_SIZE_B32], | 126 | const char cube[static NISSY_SIZE_B32], |
| 125 | const char transformation[static NISSY_SIZE_TRANSFORMATION], | 127 | const char transformation[static NISSY_SIZE_TRANSFORMATION], |
| 126 | char result[static NISSY_SIZE_B32] | 128 | char result[static NISSY_SIZE_B32] |
| 127 | ); | 129 | ); |
| 128 | 130 | ||
| 129 | /* | 131 | /* |
| 130 | Apply the given moves to the solved cube. | ||
| 131 | |||
| 132 | Parameters: | ||
| 133 | moves - The moves to be applied to the solved cube. Must be a | ||
| 134 | NULL-terminated string. | ||
| 135 | result - Return parameter for the resulting cube, in B32 format. | ||
| 136 | |||
| 137 | Return values: | ||
| 138 | NISSY_OK - The moves were performed succesfully. | ||
| 139 | NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is | ||
| 140 | probably due to an unknown internal error. | ||
| 141 | NISSY_ERROR_INVALID_MOVES - The given moves are invalid. | ||
| 142 | NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL. | ||
| 143 | */ | ||
| 144 | int64_t nissy_frommoves( | ||
| 145 | const char *moves, | ||
| 146 | char result[static NISSY_SIZE_B32] | ||
| 147 | ); | ||
| 148 | |||
| 149 | /* | ||
| 150 | Convert the given cube between the two given formats. | 132 | Convert the given cube between the two given formats. |
| 151 | 133 | ||
| 152 | Parameters: | 134 | Parameters: |
| @@ -165,11 +147,11 @@ Return values: | |||
| 165 | NISSY_ERROR_NULL_POINTER - At least one of 'format_in', 'format_out' or | 147 | NISSY_ERROR_NULL_POINTER - At least one of 'format_in', 'format_out' or |
| 166 | 'cube_string' arguments is NULL. | 148 | 'cube_string' arguments is NULL. |
| 167 | */ | 149 | */ |
| 168 | int64_t nissy_convert( | 150 | long long nissy_convert( |
| 169 | const char *format_in, | 151 | const char *format_in, |
| 170 | const char *format_out, | 152 | const char *format_out, |
| 171 | const char *cube_string, | 153 | const char *cube_string, |
| 172 | uint64_t result_size, | 154 | unsigned result_size, |
| 173 | char result[result_size] | 155 | char result[result_size] |
| 174 | ); | 156 | ); |
| 175 | 157 | ||
| @@ -192,11 +174,11 @@ Return values: | |||
| 192 | NISSY_WARNING_UNSOLVABLE - The resulting cube is unsolvable. | 174 | NISSY_WARNING_UNSOLVABLE - The resulting cube is unsolvable. |
| 193 | NISSY_ERROR_OPTIONS - One or more of the given parameters is invalid. | 175 | NISSY_ERROR_OPTIONS - One or more of the given parameters is invalid. |
| 194 | */ | 176 | */ |
| 195 | int64_t nissy_getcube( | 177 | long long nissy_getcube( |
| 196 | int64_t ep, | 178 | long long ep, |
| 197 | int64_t eo, | 179 | long long eo, |
| 198 | int64_t cp, | 180 | long long cp, |
| 199 | int64_t co, | 181 | long long co, |
| 200 | const char *options, | 182 | const char *options, |
| 201 | char result[static NISSY_SIZE_B32] | 183 | char result[static NISSY_SIZE_B32] |
| 202 | ); | 184 | ); |
| @@ -214,7 +196,7 @@ Return values: | |||
| 214 | NISSY_ERROR_UNKNOWN - An unknown error occurred. | 196 | NISSY_ERROR_UNKNOWN - An unknown error occurred. |
| 215 | Any value >= 0 - The size of the data, in bytes. | 197 | Any value >= 0 - The size of the data, in bytes. |
| 216 | */ | 198 | */ |
| 217 | int64_t nissy_datasize( | 199 | long long nissy_datasize( |
| 218 | const char *solver | 200 | const char *solver |
| 219 | ); | 201 | ); |
| 220 | 202 | ||
| @@ -233,9 +215,9 @@ Return values: | |||
| 233 | NISSY_ERROR_UNKNOWN - An error occurred while generating the data. | 215 | NISSY_ERROR_UNKNOWN - An error occurred while generating the data. |
| 234 | Any value >= 0 - The size of the data, in bytes. | 216 | Any value >= 0 - The size of the data, in bytes. |
| 235 | */ | 217 | */ |
| 236 | int64_t nissy_gendata( | 218 | long long nissy_gendata( |
| 237 | const char *solver, | 219 | const char *solver, |
| 238 | uint64_t data_size, | 220 | unsigned long long data_size, |
| 239 | char data[data_size] | 221 | char data[data_size] |
| 240 | ); | 222 | ); |
| 241 | 223 | ||
| @@ -250,8 +232,8 @@ Return values: | |||
| 250 | NISSY_OK - The data is valid. | 232 | NISSY_OK - The data is valid. |
| 251 | NISSY_ERROR_DATA - The data is invalid. | 233 | NISSY_ERROR_DATA - The data is invalid. |
| 252 | */ | 234 | */ |
| 253 | int64_t nissy_checkdata( | 235 | long long nissy_checkdata( |
| 254 | uint64_t data_size, | 236 | unsigned long long data_size, |
| 255 | const char data[data_size] | 237 | const char data[data_size] |
| 256 | ); | 238 | ); |
| 257 | 239 | ||
| @@ -284,17 +266,17 @@ Return values: | |||
| 284 | NISSY_ERROR_NULL_POINTER - The 'solver' argument is null. | 266 | NISSY_ERROR_NULL_POINTER - The 'solver' argument is null. |
| 285 | Any value >= 0 - The number of solutions found. | 267 | Any value >= 0 - The number of solutions found. |
| 286 | */ | 268 | */ |
| 287 | int64_t nissy_solve( | 269 | long long nissy_solve( |
| 288 | const char cube[static NISSY_SIZE_B32], | 270 | const char cube[static NISSY_SIZE_B32], |
| 289 | const char *solver, | 271 | const char *solver, |
| 290 | uint8_t nissflag, | 272 | unsigned nissflag, |
| 291 | int8_t minmoves, | 273 | unsigned minmoves, |
| 292 | int8_t maxmoves, | 274 | unsigned maxmoves, |
| 293 | int64_t maxsolutions, | 275 | unsigned maxsolutions, |
| 294 | int8_t optimal, | 276 | int optimal, |
| 295 | uint64_t data_size, | 277 | unsigned long long data_size, |
| 296 | const char data[data_size], | 278 | const char data[data_size], |
| 297 | uint64_t sols_size, | 279 | unsigned sols_size, |
| 298 | char sols[sols_size] | 280 | char sols[sols_size] |
| 299 | ); | 281 | ); |
| 300 | 282 | ||
| @@ -308,18 +290,18 @@ Return values: | |||
| 308 | NISSY_OK - Logger set succesfully. No warning or error is goind to be given | 290 | NISSY_OK - Logger set succesfully. No warning or error is goind to be given |
| 309 | if the logger is NULL or invalid. | 291 | if the logger is NULL or invalid. |
| 310 | */ | 292 | */ |
| 311 | int64_t nissy_setlogger( | 293 | long long nissy_setlogger( |
| 312 | void (*logger_function)(const char *, ...) | 294 | void (*logger_function)(const char *, ...) |
| 313 | ); | 295 | ); |
| 314 | 296 | ||
| 315 | 297 | ||
| 316 | /* Error codes */ | 298 | /* Error codes ***************************************************************/ |
| 317 | 299 | ||
| 318 | /* | 300 | /* |
| 319 | The value NISSY_OK denotes a success. If returned by solve, it means | 301 | The value NISSY_OK denotes a success. If returned by solve, it means |
| 320 | that no solution has been found. | 302 | that no solution has been found. |
| 321 | */ | 303 | */ |
| 322 | #define NISSY_OK INT64_C(0) | 304 | #define NISSY_OK 0LL |
| 323 | 305 | ||
| 324 | /* | 306 | /* |
| 325 | The value NISSY_WARNING_UNSOLVABLE is a warning. It means that the | 307 | The value NISSY_WARNING_UNSOLVABLE is a warning. It means that the |
| @@ -327,44 +309,44 @@ operation was completed succesfully, but the resulting cube is in an | |||
| 327 | unsolvable state. This could be intended, for example if the user has | 309 | unsolvable state. This could be intended, for example if the user has |
| 328 | provided an unsolvable cube as input. | 310 | provided an unsolvable cube as input. |
| 329 | */ | 311 | */ |
| 330 | #define NISSY_WARNING_UNSOLVABLE INT64_C(-1) | 312 | #define NISSY_WARNING_UNSOLVABLE -1LL |
| 331 | 313 | ||
| 332 | /* | 314 | /* |
| 333 | The value NISSY_ERROR_INVALID_CUBE means that the provided cube is | 315 | The value NISSY_ERROR_INVALID_CUBE means that the provided cube is |
| 334 | invalid. It could be written in an unknown format, or in a format | 316 | invalid. It could be written in an unknown format, or in a format |
| 335 | different from what specified, or simply ill-formed. | 317 | different from what specified, or simply ill-formed. |
| 336 | */ | 318 | */ |
| 337 | #define NISSY_ERROR_INVALID_CUBE INT64_C(-10) | 319 | #define NISSY_ERROR_INVALID_CUBE -10LL |
| 338 | 320 | ||
| 339 | /* | 321 | /* |
| 340 | The value NISSY_ERROR_UNSOLVABLE_CUBE means that the provided cube is | 322 | The value NISSY_ERROR_UNSOLVABLE_CUBE means that the provided cube is |
| 341 | in an unsolvable state. | 323 | in an unsolvable state. |
| 342 | */ | 324 | */ |
| 343 | #define NISSY_ERROR_UNSOLVABLE_CUBE INT64_C(-11) | 325 | #define NISSY_ERROR_UNSOLVABLE_CUBE -11LL |
| 344 | 326 | ||
| 345 | /* | 327 | /* |
| 346 | The value NISSY_ERROR_INVALID_MOVES means that the given moves are | 328 | The value NISSY_ERROR_INVALID_MOVES means that the given moves are |
| 347 | invalid. | 329 | invalid. |
| 348 | */ | 330 | */ |
| 349 | #define NISSY_ERROR_INVALID_MOVES INT64_C(-20) | 331 | #define NISSY_ERROR_INVALID_MOVES -20LL |
| 350 | 332 | ||
| 351 | /* | 333 | /* |
| 352 | The value NISSY_ERROR_INVALID_TRANS means that the given transformation | 334 | The value NISSY_ERROR_INVALID_TRANS means that the given transformation |
| 353 | is invalid. | 335 | is invalid. |
| 354 | */ | 336 | */ |
| 355 | #define NISSY_ERROR_INVALID_TRANS INT64_C(-30) | 337 | #define NISSY_ERROR_INVALID_TRANS -30LL |
| 356 | 338 | ||
| 357 | /* | 339 | /* |
| 358 | The value NISSY_ERROR_INVALID_FORMAT means that the given format is | 340 | The value NISSY_ERROR_INVALID_FORMAT means that the given format is |
| 359 | not known. | 341 | not known. |
| 360 | */ | 342 | */ |
| 361 | #define NISSY_ERROR_INVALID_FORMAT INT64_C(-40) | 343 | #define NISSY_ERROR_INVALID_FORMAT -40LL |
| 362 | 344 | ||
| 363 | /* | 345 | /* |
| 364 | The value NISSY_ERROR_INVALID_SOLVER means that the given solver is | 346 | The value NISSY_ERROR_INVALID_SOLVER means that the given solver is |
| 365 | not known. | 347 | not known. |
| 366 | */ | 348 | */ |
| 367 | #define NISSY_ERROR_INVALID_SOLVER INT64_C(-50) | 349 | #define NISSY_ERROR_INVALID_SOLVER -50LL |
| 368 | 350 | ||
| 369 | /* | 351 | /* |
| 370 | The value NISSY_ERROR_NULL_POINTER means that one of the provided pointer | 352 | The value NISSY_ERROR_NULL_POINTER means that one of the provided pointer |
| @@ -372,32 +354,32 @@ arguments is NULL. For example, it may be returned by solve when called | |||
| 372 | with a solver that requires some pre-computed data, but the provided | 354 | with a solver that requires some pre-computed data, but the provided |
| 373 | data is NULL. | 355 | data is NULL. |
| 374 | */ | 356 | */ |
| 375 | #define NISSY_ERROR_NULL_POINTER INT64_C(-60) | 357 | #define NISSY_ERROR_NULL_POINTER -60LL |
| 376 | 358 | ||
| 377 | /* | 359 | /* |
| 378 | The value NISSY_ERROR_BUFFER_SIZE means that one of the buffers provided | 360 | The value NISSY_ERROR_BUFFER_SIZE means that one of the buffers provided |
| 379 | is too small. For example, it could be too small to hold the result or | 361 | is too small. For example, it could be too small to hold the result or |
| 380 | too small to hold the data generated by gendata. | 362 | too small to hold the data generated by gendata. |
| 381 | */ | 363 | */ |
| 382 | #define NISSY_ERROR_BUFFER_SIZE INT64_C(-61) | 364 | #define NISSY_ERROR_BUFFER_SIZE -61LL |
| 383 | 365 | ||
| 384 | /* | 366 | /* |
| 385 | The value NISSY_ERROR_DATA means that the provided data is invalid. For | 367 | The value NISSY_ERROR_DATA means that the provided data is invalid. For |
| 386 | example, it may be returned by solve when called with incompatible solver | 368 | example, it may be returned by solve when called with incompatible solver |
| 387 | and data arguments. | 369 | and data arguments. |
| 388 | */ | 370 | */ |
| 389 | #define NISSY_ERROR_DATA INT64_C(-70) | 371 | #define NISSY_ERROR_DATA -70LL |
| 390 | 372 | ||
| 391 | /* | 373 | /* |
| 392 | The value NISSY_ERROR_OPTIONS means that one or more of the given options | 374 | The value NISSY_ERROR_OPTIONS means that one or more of the given options |
| 393 | are invalid. For example, it may be returned by solve when called with | 375 | are invalid. For example, it may be returned by solve when called with |
| 394 | a negative maximum number of solutions. | 376 | a negative maximum number of solutions. |
| 395 | */ | 377 | */ |
| 396 | #define NISSY_ERROR_OPTIONS INT64_C(-80) | 378 | #define NISSY_ERROR_OPTIONS -80LL |
| 397 | 379 | ||
| 398 | /* | 380 | /* |
| 399 | The value NISSY_ERROR_UNKNOWN denotes an unexpected error. It probably | 381 | The value NISSY_ERROR_UNKNOWN denotes an unexpected error. It probably |
| 400 | means that there some bug in this library. If you can, report any error | 382 | means that there some bug in this library. If you can, report any error |
| 401 | of this kind to sebastiano@tronto.net. Thanks! | 383 | of this kind to sebastiano@tronto.net. Thanks! |
| 402 | */ | 384 | */ |
| 403 | #define NISSY_ERROR_UNKNOWN INT64_C(-999) | 385 | #define NISSY_ERROR_UNKNOWN -999LL |
diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h index bcc8b33..36b6875 100644 --- a/src/solvers/h48/solve.h +++ b/src/solvers/h48/solve.h | |||
| @@ -65,12 +65,13 @@ solve_h48_appendsolution(dfsarg_solveh48_t *arg) | |||
| 65 | 65 | ||
| 66 | if (strl < 0) | 66 | if (strl < 0) |
| 67 | goto solve_h48_appendsolution_error; | 67 | goto solve_h48_appendsolution_error; |
| 68 | *arg->nextsol += strl; | 68 | *arg->nextsol += strl-1; |
| 69 | arg->solutions_size -= strl; | 69 | arg->solutions_size -= strl-1; |
| 70 | 70 | ||
| 71 | if (arg->npremoves) { | 71 | if (arg->npremoves) { |
| 72 | **arg->nextsol = ' '; | 72 | **arg->nextsol = ' '; |
| 73 | (*arg->nextsol)++; | 73 | (*arg->nextsol)++; |
| 74 | arg->solutions_size--; | ||
| 74 | 75 | ||
| 75 | invertmoves(arg->premoves, arg->npremoves, invertedpremoves); | 76 | invertmoves(arg->premoves, arg->npremoves, invertedpremoves); |
| 76 | strl = writemoves(invertedpremoves, | 77 | strl = writemoves(invertedpremoves, |
| @@ -78,13 +79,14 @@ solve_h48_appendsolution(dfsarg_solveh48_t *arg) | |||
| 78 | 79 | ||
| 79 | if (strl < 0) | 80 | if (strl < 0) |
| 80 | goto solve_h48_appendsolution_error; | 81 | goto solve_h48_appendsolution_error; |
| 81 | *arg->nextsol += strl; | 82 | *arg->nextsol += strl-1; |
| 82 | arg->solutions_size -= strl; | 83 | arg->solutions_size -= strl-1; |
| 83 | } | 84 | } |
| 84 | LOG("Solution found: %s\n", solution); | 85 | LOG("Solution found: %s\n", solution); |
| 85 | 86 | ||
| 86 | **arg->nextsol = '\n'; | 87 | **arg->nextsol = '\n'; |
| 87 | (*arg->nextsol)++; | 88 | (*arg->nextsol)++; |
| 89 | arg->solutions_size--; | ||
| 88 | (*arg->nsols)++; | 90 | (*arg->nsols)++; |
| 89 | 91 | ||
| 90 | solve_h48_appendsolution_error: | 92 | solve_h48_appendsolution_error: |
diff --git a/src/solvers/h48/solve_multithread.h b/src/solvers/h48/solve_multithread.h index 8e00404..d8c81af 100644 --- a/src/solvers/h48/solve_multithread.h +++ b/src/solvers/h48/solve_multithread.h | |||
| @@ -36,13 +36,14 @@ solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq) | |||
| 36 | 36 | ||
| 37 | if (strl < 0) | 37 | if (strl < 0) |
| 38 | goto solve_h48_appendsolution_thread_error; | 38 | goto solve_h48_appendsolution_thread_error; |
| 39 | *arg->nextsol += strl; | 39 | *arg->nextsol += strl-1; |
| 40 | arg->solutions_size -= strl; | 40 | arg->solutions_size -= strl-1; |
| 41 | 41 | ||
| 42 | if (arg->npremoves) | 42 | if (arg->npremoves) |
| 43 | { | 43 | { |
| 44 | **arg->nextsol = ' '; | 44 | **arg->nextsol = ' '; |
| 45 | (*arg->nextsol)++; | 45 | (*arg->nextsol)++; |
| 46 | arg->solutions_size--; | ||
| 46 | 47 | ||
| 47 | invertmoves(arg->premoves, arg->npremoves, invertedpremoves); | 48 | invertmoves(arg->premoves, arg->npremoves, invertedpremoves); |
| 48 | strl = writemoves(invertedpremoves, | 49 | strl = writemoves(invertedpremoves, |
| @@ -50,13 +51,14 @@ solve_h48_appendsolution_thread(dfsarg_solveh48_t *arg, task_queue_t *tq) | |||
| 50 | 51 | ||
| 51 | if (strl < 0) | 52 | if (strl < 0) |
| 52 | goto solve_h48_appendsolution_thread_error; | 53 | goto solve_h48_appendsolution_thread_error; |
| 53 | *arg->nextsol += strl; | 54 | *arg->nextsol += strl-1; |
| 54 | arg->solutions_size -= strl; | 55 | arg->solutions_size -= strl-1; |
| 55 | } | 56 | } |
| 56 | LOG("Solution found: %s\n", solution); | 57 | LOG("Solution found: %s\n", solution); |
| 57 | 58 | ||
| 58 | **arg->nextsol = '\n'; | 59 | **arg->nextsol = '\n'; |
| 59 | (*arg->nextsol)++; | 60 | (*arg->nextsol)++; |
| 61 | arg->solutions_size--; | ||
| 60 | (*arg->nsols)++; | 62 | (*arg->nsols)++; |
| 61 | 63 | ||
| 62 | solve_h48_appendsolution_thread_error: | 64 | solve_h48_appendsolution_thread_error: |
