aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-10-11 19:24:15 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-10-11 22:10:42 +0200
commitc4f64cf2556c0f8597e1fbc19d7c664b8da94612 (patch)
tree67cf5324d089dc8bafa255a5580556ba44361b0d
parent4668156da5915c8d7a7b40edaf6617b004547bd7 (diff)
downloadnissy-core-c4f64cf2556c0f8597e1fbc19d7c664b8da94612.tar.gz
nissy-core-c4f64cf2556c0f8597e1fbc19d7c664b8da94612.zip
Added buffer sizes in API args
-rw-r--r--shell.c15
-rw-r--r--src/nissy.c99
-rw-r--r--src/nissy.h99
-rw-r--r--tools/000_gendata/gendata.c6
-rw-r--r--tools/001_derive_h48/derive_h48.c3
-rw-r--r--tools/100_checkdata/checkdata.c8
-rw-r--r--tools/200_stats_tables_h48/stats_tables_h48.c12
-rw-r--r--tools/300_solve_small/solve_small.c24
-rw-r--r--tools/tool.h7
9 files changed, 180 insertions, 93 deletions
diff --git a/shell.c b/shell.c
index 9db8a92..306ad78 100644
--- a/shell.c
+++ b/shell.c
@@ -9,9 +9,9 @@
9 9
10#include "src/nissy.h" 10#include "src/nissy.h"
11 11
12#define PRINTCUBE_BUFFER_SIZE 1024 /* Should be enough */ 12#define PRINTCUBE_BUFFER_SIZE UINT64_C(1024)
13#define SOLUTIONS_BUFFER_SIZE 500000 /* Should be enough */ 13#define SOLUTIONS_BUFFER_SIZE UINT64_C(500000)
14#define MAX_PATH_LENGTH 10000 /* Should be enough */ 14#define MAX_PATH_LENGTH UINT64_C(10000)
15 15
16#define FLAG_CUBE "-cube" 16#define FLAG_CUBE "-cube"
17#define FLAG_PERM "-perm" 17#define FLAG_PERM "-perm"
@@ -308,8 +308,8 @@ convert_exec(args_t *args)
308 char result[PRINTCUBE_BUFFER_SIZE]; 308 char result[PRINTCUBE_BUFFER_SIZE];
309 int64_t ret; 309 int64_t ret;
310 310
311 ret = nissy_convert( 311 ret = nissy_convert(args->str_format_in, args->str_format_out,
312 args->str_format_in, args->str_format_out, args->str_cube, result); 312 args->str_cube, PRINTCUBE_BUFFER_SIZE, result);
313 if (ret == NISSY_OK || ret == NISSY_WARNING_UNSOLVABLE) 313 if (ret == NISSY_OK || ret == NISSY_WARNING_UNSOLVABLE)
314 printf("%s\n", result); 314 printf("%s\n", result);
315 315
@@ -382,7 +382,7 @@ gendata_exec(args_t *args)
382 382
383 buf = malloc(size); 383 buf = malloc(size);
384 384
385 ret = nissy_gendata(args->str_solver, buf); 385 ret = nissy_gendata(args->str_solver, size, buf);
386 if (ret < 0) { 386 if (ret < 0) {
387 fprintf(stderr, "Unknown error in generating data\n"); 387 fprintf(stderr, "Unknown error in generating data\n");
388 fclose(file); 388 fclose(file);
@@ -473,7 +473,8 @@ solve_exec(args_t *args)
473 473
474 ret = nissy_solve( 474 ret = nissy_solve(
475 args->cube, args->str_solver, nissflag, args->minmoves, 475 args->cube, args->str_solver, nissflag, args->minmoves,
476 args->maxmoves, args->maxsolutions, args->optimal, buf, solutions); 476 args->maxmoves, args->maxsolutions, args->optimal,
477 size, buf, SOLUTIONS_BUFFER_SIZE, solutions);
477 478
478 free(buf); 479 free(buf);
479 480
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;
diff --git a/src/nissy.h b/src/nissy.h
index e037e3b..d4c2760 100644
--- a/src/nissy.h
+++ b/src/nissy.h
@@ -98,7 +98,7 @@ Apply the given sequence of moves on the given cube.
98 98
99Parameters: 99Parameters:
100 cube - The cube to move, in B32 format. 100 cube - The cube to move, in B32 format.
101 moves - The moves to apply to the cube. 101 moves - The moves to apply to the cube. Must be a NULL-terminated string.
102 result - The return parameter for the resulting cube, in B32 format. 102 result - The return parameter for the resulting cube, in B32 format.
103 103
104Return values: 104Return values:
@@ -108,6 +108,7 @@ Return values:
108 or due to an unknown internal error. 108 or due to an unknown internal error.
109 NISSY_ERROR_INVALID_CUBE - The given cube is invalid. 109 NISSY_ERROR_INVALID_CUBE - The given cube is invalid.
110 NISSY_ERROR_INVALID_MOVES - The given moves are invalid. 110 NISSY_ERROR_INVALID_MOVES - The given moves are invalid.
111 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL.
111*/ 112*/
112int64_t nissy_applymoves( 113int64_t nissy_applymoves(
113 const char cube[static 22], 114 const char cube[static 22],
@@ -132,7 +133,7 @@ Return values:
132*/ 133*/
133int64_t nissy_applytrans( 134int64_t nissy_applytrans(
134 const char cube[static 22], 135 const char cube[static 22],
135 const char *transformation, 136 const char transformation[static 12],
136 char result[static 22] 137 char result[static 22]
137); 138);
138 139
@@ -140,7 +141,8 @@ int64_t nissy_applytrans(
140Apply the given moves to the solved cube. 141Apply the given moves to the solved cube.
141 142
142Parameters: 143Parameters:
143 moves - The moves to be applied to the solved cube. 144 moves - The moves to be applied to the solved cube. Must be a
145 NULL-terminated string.
144 result - Return parameter for the resulting cube, in B32 format. 146 result - Return parameter for the resulting cube, in B32 format.
145 147
146Return values: 148Return values:
@@ -148,6 +150,7 @@ Return values:
148 NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is 150 NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is
149 probably due to an unknown internal error. 151 probably due to an unknown internal error.
150 NISSY_ERROR_INVALID_MOVES - The given moves are invalid. 152 NISSY_ERROR_INVALID_MOVES - The given moves are invalid.
153 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL.
151*/ 154*/
152int64_t nissy_frommoves( 155int64_t nissy_frommoves(
153 const char *moves, 156 const char *moves,
@@ -158,23 +161,26 @@ int64_t nissy_frommoves(
158Convert the given cube between the two given formats. 161Convert the given cube between the two given formats.
159 162
160Parameters: 163Parameters:
161 format_in - The input format. 164 format_in - The input format.
162 format_out - The output format. 165 format_out - The output format.
163 string - The cube, in format_in format. 166 cube_string - The cube, in format_in format.
164 result - Return parameter for the cube in format_out format. Must be 167 retult_size - The allocated size of the result array.
165 large enough to contains the cube in this format. 168 result - Return parameter for the cube in format_out format.
166 169
167Return values: 170Return values:
168 NISSY_OK - The conversion was performed succesfully. 171 NISSY_OK - The conversion was performed succesfully.
169 NISSY_ERROR_INVALID_CUBE - The given cube is invalid. 172 NISSY_ERROR_INVALID_CUBE - The given cube is invalid.
170 NISSY_ERROR_INVALID_FORMAT - At least one of the given formats is invalid. 173 NISSY_ERROR_INVALID_FORMAT - At least one of the given formats is invalid.
171 NISSY_ERROR_UNKNOWN - An unknown error occurred. 174 NISSY_ERROR_UNKNOWN - An unknown error occurred.
175 NISSY_ERROR_NULL_POINTER - At least one of 'format_in', 'format_out' or
176 'cube_string' arguments is NULL.
172*/ 177*/
173int64_t nissy_convert( 178int64_t nissy_convert(
174 const char *format_in, 179 const char *format_in,
175 const char *format_out, 180 const char *format_out,
176 const char *cube, 181 const char *cube_string,
177 char *result 182 uint64_t result_size,
183 char result[result_size]
178); 184);
179 185
180/* 186/*
@@ -214,6 +220,7 @@ Parameters:
214 220
215Return values: 221Return values:
216 NISSY_ERROR_INVALID_SOLVER - The given solver is not known. 222 NISSY_ERROR_INVALID_SOLVER - The given solver is not known.
223 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null.
217 NISSY_ERROR_UNKNOWN - An unknown error occurred. 224 NISSY_ERROR_UNKNOWN - An unknown error occurred.
218 Any value >= 0 - The size of the data, in bytes. 225 Any value >= 0 - The size of the data, in bytes.
219*/ 226*/
@@ -225,55 +232,75 @@ int64_t nissy_datasize(
225Compute the data for the given solver and store it in generated_data. 232Compute the data for the given solver and store it in generated_data.
226 233
227Parameters: 234Parameters:
228 solver - The name of the solver. 235 solver - The name of the solver.
229 data - The return parameter for the generated data. Must be large enoguh 236 data_size - The size of the data buffer. It is advised to use nissy_datasize
230 to contain the whole data. It is advised to use nissy_datasize to 237 to check how much memory is needed.
231 check how much memory is needed. 238 data - The return parameter for the generated data.
232 239
233Return values: 240Return values:
234 NISSY_ERROR_INVALID_SOLVER - The given solver is not known. 241 NISSY_ERROR_INVALID_SOLVER - The given solver is not known.
242 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null.
235 NISSY_ERROR_UNKNOWN - An error occurred while generating the data. 243 NISSY_ERROR_UNKNOWN - An error occurred while generating the data.
236 Any value >= 0 - The size of the data, in bytes. 244 Any value >= 0 - The size of the data, in bytes.
237*/ 245*/
238int64_t nissy_gendata( 246int64_t nissy_gendata(
239 const char *solver, 247 const char *solver,
240 void *data 248 uint64_t data_size,
249 char data[data_size]
241); 250);
242 251
243/* 252/*
244Print information on a data table via the provided callback writer. 253Print information on a data table via the provided callback writer.
245 254
246Parameters: 255Parameters:
247 data - The data 256 data_size - The size of the data buffer.
248 write - A callback writer with the same signature as printf(3). 257 data - The data to be read.
258 write - A callback writer with the same signature as printf(3).
249 259
250Return values: 260Return values:
251 NISSY_OK - The data is correct. 261 NISSY_OK - The data is correct.
252 NISSY_ERROR_DATA - The data contains errors. 262 NISSY_ERROR_DATA - The data contains errors.
253*/ 263*/
254int64_t nissy_datainfo( 264int64_t nissy_datainfo(
255 const void *data, 265 uint64_t data_size,
266 const char data[data_size],
256 void (*write)(const char *, ...) 267 void (*write)(const char *, ...)
257); 268);
258 269
259/* 270/*
271Check that the data is a valid data table for a solver.
272
273Parameters:
274 data_size - The size of the data buffer.
275 data - The data for the solver. Can be computed with gendata.
276
277Return values:
278 NISSY_OK - The data is valid.
279 NISSY_ERROR_DATA - The data is invalid.
280*/
281int64_t nissy_checkdata(
282 uint64_t data_size,
283 const char data[data_size]
284);
285
286/*
260Solve the given cube using the given solver and options. 287Solve the given cube using the given solver and options.
261 288
262Parameters: 289Parameters:
263 cube - The cube to solver, in B32 format. 290 cube - The cube to solver, in B32 format.
264 solver - The name of the solver. 291 solver - The name of the solver.
265 nissflag - The flags for NISS (linear, inverse, mixed, or combinations). 292 nissflag - The flags for NISS (linear, inverse, mixed, or combinations).
266 minmoves - The minimum number of moves for a solution. 293 minmoves - The minimum number of moves for a solution.
267 maxmoves - The maximum number of moves for a solution. 294 maxmoves - The maximum number of moves for a solution.
268 maxsolutions - The maximum number of solutions. 295 maxsols - The maximum number of solutions.
269 optimal - If set to a non-negative value, the maximum number of moves 296 optimal - If set to a non-negative value, the maximum number of moves
270 above the optimal solution length. 297 above the optimal solution length.
271 data - The data for the solver. Can be computed with gendata. 298 data_size - The size of the data buffer.
272 solutions - The return parameter for the solutions. Must be large enough 299 data - The data for the solver. Can be computed with gendata.
273 to store all found solutions. The solutions are separated by 300 sols_size - The size of the solutions buffer.
274 a '\n' (newline) and a '\0' (NULL character) terminates the 301 sols - The return parameter for the solutions. The solutions are
275 list. 302 separated by a '\n' (newline) and a '\0' (NULL character)
276 TODO: replace with callback writer. 303 terminates the list.
277 304
278Return values: 305Return values:
279 NISSY_OK - Cube solved succesfully. 306 NISSY_OK - Cube solved succesfully.
@@ -281,8 +308,8 @@ Return values:
281 NISSY_ERROR_UNSOLVABLE_CUBE - The given cube is valid, but not solvable with 308 NISSY_ERROR_UNSOLVABLE_CUBE - The given cube is valid, but not solvable with
282 the given solver. 309 the given solver.
283 NISSY_ERROR_OPTIONS - One or more of the given options are invalid. 310 NISSY_ERROR_OPTIONS - One or more of the given options are invalid.
284 NISSY_ERROR_NULL_POINTER - One of the provided pointers is null.
285 NISSY_ERROR_INVALID_SOLVER - The given solver is not known. 311 NISSY_ERROR_INVALID_SOLVER - The given solver is not known.
312 NISSY_ERROR_NULL_POINTER - The 'solver' argument is null.
286 Any value >= 0 - The number of solutions found. 313 Any value >= 0 - The number of solutions found.
287*/ 314*/
288int64_t nissy_solve( 315int64_t nissy_solve(
@@ -293,8 +320,10 @@ int64_t nissy_solve(
293 int8_t maxmoves, 320 int8_t maxmoves,
294 int64_t maxsolutions, 321 int64_t maxsolutions,
295 int8_t optimal, 322 int8_t optimal,
296 const void *data, 323 uint64_t data_size,
297 char *solutions 324 const char data[data_size],
325 uint64_t sols_size,
326 char sols[sols_size]
298); 327);
299 328
300/* 329/*
diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c
index eec9f76..0d647f9 100644
--- a/tools/000_gendata/gendata.c
+++ b/tools/000_gendata/gendata.c
@@ -17,8 +17,8 @@ run(void) {
17 case -2: 17 case -2:
18 goto gendata_run_finish; 18 goto gendata_run_finish;
19 default: 19 default:
20 nissy_datainfo(buf, write_stdout); 20 nissy_datainfo(size, buf, write_stdout);
21 consistent = nissy_checkdata(solver, buf) == 0; 21 consistent = nissy_checkdata(size, buf) == 0;
22 expected = check_distribution(solver, buf); 22 expected = check_distribution(solver, buf);
23 if (consistent && expected) { 23 if (consistent && expected) {
24 printf("\n"); 24 printf("\n");
@@ -42,7 +42,7 @@ int main(int argc, char **argv) {
42 uint8_t h, k; 42 uint8_t h, k;
43 43
44 if (argc < 2) { 44 if (argc < 2) {
45 fprintf(stderr, "Error: not enough arguments. " 45 printf("Error: not enough arguments. "
46 "A solver must be given.\n"); 46 "A solver must be given.\n");
47 return 1; 47 return 1;
48 } 48 }
diff --git a/tools/001_derive_h48/derive_h48.c b/tools/001_derive_h48/derive_h48.c
index c5f28c8..c964882 100644
--- a/tools/001_derive_h48/derive_h48.c
+++ b/tools/001_derive_h48/derive_h48.c
@@ -19,8 +19,7 @@ void run(void) {
19 19
20int main(int argc, char **argv) { 20int main(int argc, char **argv) {
21 if (argc < 5) { 21 if (argc < 5) {
22 fprintf(stderr, 22 printf("Error: not enough arguments. Required:\n"
23 "Error: not enough arguments. Required:\n"
24 "1. Solver name for large table\n" 23 "1. Solver name for large table\n"
25 "2. Solver name for derived table\n" 24 "2. Solver name for derived table\n"
26 "3. Filename containing large table\n" 25 "3. Filename containing large table\n"
diff --git a/tools/100_checkdata/checkdata.c b/tools/100_checkdata/checkdata.c
index 695a4dd..2005f17 100644
--- a/tools/100_checkdata/checkdata.c
+++ b/tools/100_checkdata/checkdata.c
@@ -12,19 +12,19 @@ run(void) {
12 size = nissy_datasize(solver); 12 size = nissy_datasize(solver);
13 13
14 if (size <= 0) { 14 if (size <= 0) {
15 fprintf(stderr, "Error in datasize\n"); 15 printf("Error in datasize\n");
16 return; 16 return;
17 } 17 }
18 18
19 if ((f = fopen(filename, "rb")) == NULL) { 19 if ((f = fopen(filename, "rb")) == NULL) {
20 fprintf(stderr, "Error reading file %s\n", filename); 20 printf("Error reading file %s\n", filename);
21 return; 21 return;
22 } 22 }
23 23
24 buf = malloc(size); 24 buf = malloc(size);
25 fread(buf, size, 1, f); 25 fread(buf, size, 1, f);
26 fclose(f); 26 fclose(f);
27 result = nissy_checkdata(solver, buf); 27 result = nissy_checkdata(size, buf);
28 free(buf); 28 free(buf);
29 29
30 printf("checkdata %s\n", result == 0 ? "succeeded" : "failed"); 30 printf("checkdata %s\n", result == 0 ? "succeeded" : "failed");
@@ -34,7 +34,7 @@ run(void) {
34 34
35int main(int argc, char **argv) { 35int main(int argc, char **argv) {
36 if (argc < 3) { 36 if (argc < 3) {
37 fprintf(stderr, "Error: not enough arguments. " 37 printf("Error: not enough arguments. "
38 "A solver and a file name must be given.\n"); 38 "A solver and a file name must be given.\n");
39 return 1; 39 return 1;
40 } 40 }
diff --git a/tools/200_stats_tables_h48/stats_tables_h48.c b/tools/200_stats_tables_h48/stats_tables_h48.c
index a9a2a1e..11df137 100644
--- a/tools/200_stats_tables_h48/stats_tables_h48.c
+++ b/tools/200_stats_tables_h48/stats_tables_h48.c
@@ -6,6 +6,7 @@
6#define LOG_EVERY (NCUBES_PER_THREAD / 10) 6#define LOG_EVERY (NCUBES_PER_THREAD / 10)
7 7
8int NCUBES_PER_THREAD; 8int NCUBES_PER_THREAD;
9int64_t size = 0;
9const char *solver = "h48stats"; 10const char *solver = "h48stats";
10const char *filename = "tables/h48h0k4"; 11const char *filename = "tables/h48h0k4";
11char *buf; 12char *buf;
@@ -40,11 +41,12 @@ run_thread(void *arg)
40 cp = rand64(); 41 cp = rand64();
41 co = rand64(); 42 co = rand64();
42 nissy_getcube(ep, eo, cp, co, "fix", cube); 43 nissy_getcube(ep, eo, cp, co, "fix", cube);
43 nissy_solve(cube, "h48stats", "", 0, MAXMOVES, 1, -1, buf, s); 44 nissy_solve(cube, "h48stats", NISSY_NISSFLAG_NORMAL,
45 0, MAXMOVES, 1, -1, size, buf, 12, s);
44 for (j = 0; j < 12; j++) 46 for (j = 0; j < 12; j++)
45 a->v[j][(int)s[j]]++; 47 a->v[j][(int)s[j]]++;
46 if ((i+1) % LOG_EVERY == 0) 48 if ((i+1) % LOG_EVERY == 0)
47 fprintf(stderr, "[thread %d] %d cubes solved...\n", 49 printf("[thread %d] %d cubes solved...\n",
48 a->thread_id, i+1); 50 a->thread_id, i+1);
49 } 51 }
50 52
@@ -57,6 +59,8 @@ void run(void) {
57 pthread_t thread[THREADS]; 59 pthread_t thread[THREADS];
58 thread_arg_t arg[THREADS]; 60 thread_arg_t arg[THREADS];
59 61
62 size = nissy_datasize(solver);
63
60 for (i = 0; i < THREADS; i++) { 64 for (i = 0; i < THREADS; i++) {
61 arg[i] = (thread_arg_t) { 65 arg[i] = (thread_arg_t) {
62 .thread_id = i, 66 .thread_id = i,
@@ -88,7 +92,7 @@ int main(int argc, char **argv) {
88 nissy_setlogger(log_stderr); 92 nissy_setlogger(log_stderr);
89 93
90 if (argc < 2) { 94 if (argc < 2) {
91 fprintf(stderr, "Error: not enough arguments. " 95 printf("Error: not enough arguments. "
92 "Number of cubes per thread must be provided.\n"); 96 "Number of cubes per thread must be provided.\n");
93 return 1; 97 return 1;
94 } 98 }
@@ -96,7 +100,7 @@ int main(int argc, char **argv) {
96 NCUBES_PER_THREAD = atoi(argv[1]); 100 NCUBES_PER_THREAD = atoi(argv[1]);
97 101
98 if (NCUBES_PER_THREAD < 1 || NCUBES_PER_THREAD > 1000000) { 102 if (NCUBES_PER_THREAD < 1 || NCUBES_PER_THREAD > 1000000) {
99 fprintf(stderr, "Invalid number of cubes: must be > 0 and " 103 printf("Invalid number of cubes: must be > 0 and "
100 "< 1000000, but %d was given.\n", NCUBES_PER_THREAD); 104 "< 1000000, but %d was given.\n", NCUBES_PER_THREAD);
101 return 1; 105 return 1;
102 } 106 }
diff --git a/tools/300_solve_small/solve_small.c b/tools/300_solve_small/solve_small.c
index ade949a..5400436 100644
--- a/tools/300_solve_small/solve_small.c
+++ b/tools/300_solve_small/solve_small.c
@@ -1,6 +1,9 @@
1#include "../tool.h" 1#include "../tool.h"
2 2
3#define SOL_BUFFER_LEN 1000
4
3const char *solver = "h48h0k4"; 5const char *solver = "h48h0k4";
6int64_t size = 0;
4char *buf; 7char *buf;
5 8
6char *scrambles[] = { 9char *scrambles[] = {
@@ -14,25 +17,22 @@ char *scrambles[] = {
14void run(void) { 17void run(void) {
15 int i; 18 int i;
16 int64_t n; 19 int64_t n;
17 char sol[100], cube[22]; 20 char sol[SOL_BUFFER_LEN], cube[22];
18 21
19 printf("Solved the following scrambles:\n\n"); 22 printf("Solved the following scrambles:\n\n");
20 for (i = 0; scrambles[i] != NULL; i++) { 23 for (i = 0; scrambles[i] != NULL; i++) {
21 printf("%d. %s\n", i+1, scrambles[i]); 24 printf("%d. %s\n", i+1, scrambles[i]);
22 fprintf(stderr, "Solving scramble %s\n", scrambles[i]); 25 printf("Solving scramble %s\n", scrambles[i]);
23 if (nissy_frommoves(scrambles[i], cube) == -1) { 26 if (nissy_frommoves(scrambles[i], cube) == -1) {
24 fprintf(stderr, "Invalid scramble\n"); 27 printf("Invalid scramble\n");
25 printf("Invalid\n");
26 continue; 28 continue;
27 } 29 }
28 n = nissy_solve( 30 n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
29 cube, solver, "", 0, 20, 1, -1, buf, sol); 31 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol);
30 if (n == 0) { 32 if (n == 0)
31 printf("No solution\n"); 33 printf("No solution found\n");
32 fprintf(stderr, "No solution found\n"); 34 else
33 } else {
34 printf("Solutions:\n%s\n", sol); 35 printf("Solutions:\n%s\n", sol);
35 }
36 } 36 }
37} 37}
38 38
@@ -46,6 +46,8 @@ int main(void) {
46 if (getdata(solver, &buf, filename) != 0) 46 if (getdata(solver, &buf, filename) != 0)
47 return 1; 47 return 1;
48 48
49 size = nissy_datasize(solver);
50
49 timerun(run); 51 timerun(run);
50 52
51 free(buf); 53 free(buf);
diff --git a/tools/tool.h b/tools/tool.h
index 02ff0f2..2fc0224 100644
--- a/tools/tool.h
+++ b/tools/tool.h
@@ -96,7 +96,7 @@ generatetable(const char *solver, char **buf)
96 } 96 }
97 97
98 *buf = malloc(size); 98 *buf = malloc(size);
99 gensize = nissy_gendata(solver, *buf); 99 gensize = nissy_gendata(solver, size, *buf);
100 100
101 if (gensize != size) { 101 if (gensize != size) {
102 printf("Error generating table"); 102 printf("Error generating table");
@@ -212,7 +212,7 @@ gendata_run(
212 case -2: 212 case -2:
213 goto gendata_run_finish; 213 goto gendata_run_finish;
214 default: 214 default:
215 nissy_datainfo(buf, write_stdout); 215 nissy_datainfo(size, buf, write_stdout);
216 printf("\n"); 216 printf("\n");
217 printf("Succesfully generated %" PRId64 " bytes. " 217 printf("Succesfully generated %" PRId64 " bytes. "
218 "See above for details on the tables.\n", size); 218 "See above for details on the tables.\n", size);
@@ -237,6 +237,7 @@ derivedata_run(
237 int64_t size; 237 int64_t size;
238 char *buf; 238 char *buf;
239 239
240 buf = NULL;
240 size = derivetable(solver_large, solver_small, filename_large, &buf); 241 size = derivetable(solver_large, solver_small, filename_large, &buf);
241 switch (size) { 242 switch (size) {
242 case -1: 243 case -1:
@@ -244,7 +245,7 @@ derivedata_run(
244 case -2: 245 case -2:
245 goto derivedata_run_finish; 246 goto derivedata_run_finish;
246 default: 247 default:
247 nissy_datainfo(buf, write_stdout); 248 nissy_datainfo(size, buf, write_stdout);
248 printf("\n"); 249 printf("\n");
249 printf("Succesfully generated %" PRId64 " bytes. " 250 printf("Succesfully generated %" PRId64 " bytes. "
250 "See above for details on the tables.\n", size); 251 "See above for details on the tables.\n", size);

Generated with cgit - Back to sebastiano.tronto.net