aboutsummaryrefslogtreecommitdiff
path: root/src/nissy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nissy.c')
-rw-r--r--src/nissy.c99
1 files changed, 75 insertions, 24 deletions
diff --git a/src/nissy.c b/src/nissy.c
index d37256a..f8f5d09 100644
--- a/src/nissy.c
+++ b/src/nissy.c
@@ -16,7 +16,7 @@ int parse_h48_solver(const char *, uint8_t [static 1], uint8_t [static 1]);
16STATIC int64_t write_result(cube_t, char [static 22]); 16STATIC int64_t write_result(cube_t, char [static 22]);
17STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN], 17STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN],
18 const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t); 18 const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t);
19STATIC bool checkdata(const void *, const tableinfo_t *); 19STATIC bool checkdata(const char *, const tableinfo_t *);
20 20
21#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F } 21#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F }
22struct { 22struct {
@@ -67,7 +67,7 @@ parse_h48_solver_error:
67} 67}
68 68
69STATIC bool 69STATIC bool
70checkdata(const void *buf, const tableinfo_t *info) 70checkdata(const char *buf, const tableinfo_t *info)
71{ 71{
72 uint64_t distr[INFO_DISTRIBUTION_LEN]; 72 uint64_t distr[INFO_DISTRIBUTION_LEN];
73 73
@@ -202,6 +202,12 @@ nissy_applymoves(
202 cube_t c, res; 202 cube_t c, res;
203 int64_t err; 203 int64_t err;
204 204
205 if (moves == NULL) {
206 LOG("Error: 'moves' argument is NULL\n");
207 err = NISSY_ERROR_NULL_POINTER;
208 goto nissy_applymoves_error;
209 }
210
205 c = readcube("B32", cube); 211 c = readcube("B32", cube);
206 212
207 if (!isconsistent(c)) { 213 if (!isconsistent(c)) {
@@ -228,7 +234,7 @@ nissy_applymoves_error:
228int64_t 234int64_t
229nissy_applytrans( 235nissy_applytrans(
230 const char cube[static 22], 236 const char cube[static 22],
231 const char *transformation, 237 const char transformation[static 12],
232 char result[static 22] 238 char result[static 22]
233) 239)
234{ 240{
@@ -267,6 +273,12 @@ nissy_frommoves(
267 cube_t res; 273 cube_t res;
268 int64_t err; 274 int64_t err;
269 275
276 if (moves == NULL) {
277 LOG("Error: 'moves' argument is NULL\n");
278 err = NISSY_ERROR_NULL_POINTER;
279 goto nissy_frommoves_error;
280 }
281
270 res = applymoves(SOLVED_CUBE, moves); 282 res = applymoves(SOLVED_CUBE, moves);
271 283
272 if (!isconsistent(res)) { 284 if (!isconsistent(res)) {
@@ -287,13 +299,32 @@ nissy_convert(
287 const char *format_in, 299 const char *format_in,
288 const char *format_out, 300 const char *format_out,
289 const char *cube_string, 301 const char *cube_string,
290 char *result 302 size_t result_size,
303 char result[result_size]
291) 304)
292{ 305{
293 cube_t c; 306 cube_t c;
294 int ret; 307 int ret;
295 int64_t err; 308 int64_t err;
296 309
310 if (format_in == NULL) {
311 LOG("Error: 'format_in' argument is NULL\n");
312 err = NISSY_ERROR_NULL_POINTER;
313 goto nissy_convert_error;
314 }
315
316 if (format_out == NULL) {
317 LOG("Error: 'format_out' argument is NULL\n");
318 err = NISSY_ERROR_NULL_POINTER;
319 goto nissy_convert_error;
320 }
321
322 if (cube_string == NULL) {
323 LOG("Error: 'cube_string' argument is NULL\n");
324 err = NISSY_ERROR_NULL_POINTER;
325 goto nissy_convert_error;
326 }
327
297 c = readcube(format_in, cube_string); 328 c = readcube(format_in, cube_string);
298 329
299 if (iserror(c)) { 330 if (iserror(c)) {
@@ -334,6 +365,11 @@ nissy_getcube(
334 int i; 365 int i;
335 cube_t c; 366 cube_t c;
336 367
368 if (options == NULL) {
369 LOG("Error: 'options' argument is NULL\n");
370 return NISSY_ERROR_NULL_POINTER;
371 }
372
337 for (i = 0; getcube_options[i].option != NULL; i++) 373 for (i = 0; getcube_options[i].option != NULL; i++)
338 if (!strcmp(options, getcube_options[i].option)) 374 if (!strcmp(options, getcube_options[i].option))
339 getcube_options[i].fix(&ep, &eo, &cp, &co); 375 getcube_options[i].fix(&ep, &eo, &cp, &co);
@@ -355,13 +391,19 @@ nissy_datasize(
355 const char *solver 391 const char *solver
356) 392)
357{ 393{
394 if (solver == NULL) {
395 LOG("Error: 'solver' argument is NULL\n");
396 return NISSY_ERROR_NULL_POINTER;
397 }
398
358 /* gendata() handles a NULL *data as a "dryrun" request */ 399 /* gendata() handles a NULL *data as a "dryrun" request */
359 return nissy_gendata(solver, NULL); 400 return nissy_gendata(solver, 0, NULL);
360} 401}
361 402
362int64_t 403int64_t
363nissy_datainfo( 404nissy_datainfo(
364 const void *data, 405 size_t data_size,
406 const char data[data_size],
365 void (*write)(const char *, ...) 407 void (*write)(const char *, ...)
366) 408)
367{ 409{
@@ -396,7 +438,8 @@ nissy_datainfo(
396 } 438 }
397 439
398 if (info.next != 0) 440 if (info.next != 0)
399 return nissy_datainfo((char *)data + info.next, write); 441 return nissy_datainfo(
442 data_size - info.next, (char *)data + info.next, write);
400 443
401 write("\n---------\n"); 444 write("\n---------\n");
402 445
@@ -406,12 +449,18 @@ nissy_datainfo(
406int64_t 449int64_t
407nissy_gendata( 450nissy_gendata(
408 const char *solver, 451 const char *solver,
409 void *data 452 size_t data_size,
453 char data[data_size]
410) 454)
411{ 455{
412 int p; 456 int p;
413 gendata_h48_arg_t arg; 457 gendata_h48_arg_t arg;
414 458
459 if (solver == NULL) {
460 LOG("Error: 'solver' argument is NULL\n");
461 return NISSY_ERROR_NULL_POINTER;
462 }
463
415 arg.buf = data; 464 arg.buf = data;
416 if (!strncmp(solver, "h48", 3)) { 465 if (!strncmp(solver, "h48", 3)) {
417 p = parse_h48_solver(solver, &arg.h, &arg.k); 466 p = parse_h48_solver(solver, &arg.h, &arg.k);
@@ -427,8 +476,8 @@ nissy_gendata(
427 476
428int64_t 477int64_t
429nissy_checkdata( 478nissy_checkdata(
430 const char *solver, 479 uint64_t data_size,
431 const void *data 480 const char data[data_size]
432) 481)
433{ 482{
434 char *buf; 483 char *buf;
@@ -454,16 +503,23 @@ nissy_solve(
454 uint8_t nissflag, 503 uint8_t nissflag,
455 int8_t minmoves, 504 int8_t minmoves,
456 int8_t maxmoves, 505 int8_t maxmoves,
457 int64_t maxsolutions, 506 int64_t maxsols,
458 int8_t optimal, 507 int8_t optimal,
459 const void *data, 508 size_t data_size,
460 char *solutions 509 const char data[data_size],
510 size_t sols_size,
511 char sols[sols_size]
461) 512)
462{ 513{
463 cube_t c; 514 cube_t c;
464 int p; 515 int p;
465 uint8_t h, k; 516 uint8_t h, k;
466 517
518 if (solver == NULL) {
519 LOG("Error: 'solver' argument is NULL\n");
520 return NISSY_ERROR_NULL_POINTER;
521 }
522
467 c = readcube_B32(cube); 523 c = readcube_B32(cube);
468 524
469 if (!isconsistent(c)) { 525 if (!isconsistent(c)) {
@@ -486,24 +542,19 @@ nissy_solve(
486 maxmoves = 20; 542 maxmoves = 20;
487 } 543 }
488 544
489 if (maxsolutions < 0) { 545 if (maxsols < 0) {
490 LOG("solve: 'maxsols' is negative, stopping\n"); 546 LOG("solve: 'maxsols' is negative, stopping\n");
491 return NISSY_ERROR_OPTIONS; 547 return NISSY_ERROR_OPTIONS;
492 } 548 }
493 549
494 if (maxsolutions == 0) { 550 if (maxsols == 0) {
495 LOG("solve: 'maxsols' is 0, returning no solution\n"); 551 LOG("solve: 'maxsols' is 0, returning no solution\n");
496 return 0; 552 return 0;
497 } 553 }
498 554
499 if (solutions == NULL) {
500 LOG("solve: return parameter 'solutions' is NULL, stopping\n");
501 return NISSY_ERROR_NULL_POINTER;
502 }
503
504 if (!strncmp(solver, "h48", 3)) { 555 if (!strncmp(solver, "h48", 3)) {
505 if (!strcmp(solver, "h48stats")) 556 if (!strcmp(solver, "h48stats"))
506 return solve_h48stats(c, maxmoves, data, solutions); 557 return solve_h48stats(c, maxmoves, data, sols);
507 558
508 p = parse_h48_solver(solver, &h, &k); 559 p = parse_h48_solver(solver, &h, &k);
509 if (p != 0) { 560 if (p != 0) {
@@ -512,13 +563,13 @@ nissy_solve(
512 } else { 563 } else {
513 return THREADS > 1 ? 564 return THREADS > 1 ?
514 solve_h48_multithread(c, minmoves, 565 solve_h48_multithread(c, minmoves,
515 maxmoves, maxsolutions, data, solutions) : 566 maxmoves, maxsols, data, sols) :
516 solve_h48(c, minmoves, 567 solve_h48(c, minmoves,
517 maxmoves, maxsolutions, data, solutions); 568 maxmoves, maxsols, data, sols);
518 } 569 }
519 } else if (!strcmp(solver, "simple")) { 570 } else if (!strcmp(solver, "simple")) {
520 return solve_simple( 571 return solve_simple(
521 c, minmoves, maxmoves, maxsolutions, optimal, solutions); 572 c, minmoves, maxmoves, maxsols, optimal, sols);
522 } else { 573 } else {
523 LOG("solve: unknown solver '%s'\n", solver); 574 LOG("solve: unknown solver '%s'\n", solver);
524 return NISSY_ERROR_INVALID_SOLVER; 575 return NISSY_ERROR_INVALID_SOLVER;

Generated with cgit - Back to sebastiano.tronto.net