From d9f40f59c9f14bef7f87c11da7494f7dcf1457b4 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 10 Jun 2024 22:22:03 +0200 Subject: Started cube.h refactor --- src/cube.c | 2 ++ src/cube.h | 81 +++++++++++++++++++++++++++++++++++---------------- src/cube_generic.h | 7 +++++ src/cube_public.h | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/solve_generic.h | 19 ------------ 5 files changed, 149 insertions(+), 43 deletions(-) create mode 100644 src/cube_public.h (limited to 'src') diff --git a/src/cube.c b/src/cube.c index fce0683..97f7844 100644 --- a/src/cube.c +++ b/src/cube.c @@ -47,3 +47,5 @@ #include "moves.h" #include "solve_h48.h" #include "solve_generic.h" + +#include "cube_public.h" diff --git a/src/cube.h b/src/cube.h index d30c1d4..b5f4211 100644 --- a/src/cube.h +++ b/src/cube.h @@ -1,32 +1,65 @@ -/* TODO: change cube to const char [static 22], or const char * */ -/* TODO: return error code */ -/* TODO: implement all of this, or move the implementation to cube_public.h */ -/* TODO: document in a separate file, possibly leave here 1-line comments */ - -cube_t compose(cube_t cube, cube_t perm); -cube_t inverse(cube_t cube); -cube_t solvedcube(void); -cube_t applymoves(cube_t, const char *); -cube_t applytrans(cube_t, const char *); -cube_t readcube(const char *format, const char *buf); -void writecube(const char *format, cube_t cube, char *buf); -int64_t solve( - cube_t cube, +/* +All the functions below return 0 in case of success and a positive number +in case of error. See (TODO: documentation) for details. +*/ + +int nissy_compose( + const char cube[static 22], + const char permutation[static 22], + char result[static 22] +); + +int nissy_inverse( + const char cube[static 22], + char result[static 22] +); + +int nissy_applyoves( + const char cube[static 22], + const char *moves, + char result[static 22] +); + +int nissy_applytrans( + const char cube[static 22], + const char *transformation, + char result[static 22] +); + +int nissy_frommoves( + const char *moves, + char result[static 22] +); + +int nissy_readcube( + const char *format, + const char *cube_string, + char result[static 22] +); + +int nissy_writecube( + const char *format, + const char cube[static 22], + char *result +); + +int nissy_gendata( + const char *solver, + const char *options, + void *generated_data, + int64_t *generated_bytes +); + +int nissy_solve( + const char cube[static 22], const char *solver, const char *options, const char *nisstype, int8_t minmoves, int8_t maxmoves, - int64_t maxsols, + int64_t maxsolutions, int8_t optimal, - const void *data, - char *solutions -); -void multisolve( - int n, - cube_t *cube, - const char *solver, const void *data, - char *sols + char *solutions, + int *n_solutions ); -int64_t gendata(const char *solver, const char *options, void *data); diff --git a/src/cube_generic.h b/src/cube_generic.h index 6b675ee..7ff9992 100644 --- a/src/cube_generic.h +++ b/src/cube_generic.h @@ -8,6 +8,7 @@ _static bool issolved(cube_t); _static bool iserror(cube_t); _static cube_t applymoves(cube_t, const char *); _static cube_t applytrans(cube_t, const char *); +_static cube_t frommoves(const char *); _static int permsign(uint8_t *, int); _static cube_t move(cube_t, uint8_t); @@ -164,6 +165,12 @@ applymoves_error: return zero; } +_static cube_t +frommoves(const char *buf) +{ + return applymoves(solved, buf); +} + _static cube_t applytrans(cube_t cube, const char *buf) { diff --git a/src/cube_public.h b/src/cube_public.h new file mode 100644 index 0000000..7f7b81c --- /dev/null +++ b/src/cube_public.h @@ -0,0 +1,83 @@ +int check(cube_t, char [static 22]); + +int +check(cube_t cube, char result[static 22]) +{ + if (!isconsistent(cube)) { + writecube("B32", zero, result); + return 2; + } + + return issolvable(cube) ? 0 : 1; +} + +int +nissy_compose( + const char cube[static 22], + const char permutation[static 22], + char result[static 22] +) +{ + cube_t c, p, res; + + c = readcube("B32", cube); + p = readcube("B32", permutation); + + res = compose(c, p); + + return check(res, result); +} + +int +nissy_inverse( + const char cube[static 22], + char result[static 22] +); + +int nissy_applymoves( + const char cube[static 22], + const char *moves, + char result[static 22] +); + +int nissy_applytrans( + const char cube[static 22], + const char *transformation, + char result[static 22] +); + +int nissy_frommoves( + const char *moves, + char result[static 22] +); + +int nissy_readcube( + const char *format, + const char *cube_string, + char result[static 22] +); + +int nissy_writecube( + const char *format, + const char cube[static 22], + char *result +); + +int64_t nissy_gendata( + const char *solver, + const char *options, + void *data +); + +int64_t nissy_solve( + const char cube[static 22], + const char *solver, + const char *options, + const char *nisstype, + int8_t minmoves, + int8_t maxmoves, + int64_t maxsolutions, + int8_t optimal, + const void *data, + char *solutions +); diff --git a/src/solve_generic.h b/src/solve_generic.h index b5d1e2d..c80f3ef 100644 --- a/src/solve_generic.h +++ b/src/solve_generic.h @@ -57,25 +57,6 @@ solve( return -1; } -void -multisolve( - int n, - cube_t *cube, - const char *solver, - const void *data, - char *sols -) -{ - char *s; - int i; - - s = sols; - for (i = 0; i < n; i++) { - solve(cube[i], solver, "", "normal", 0, -1, 1, 0, NULL, s); - while (s++); - } -} - _static void solve_generic_appendsolution(dfsarg_generic_t *arg) { -- cgit v1.3