h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit bfb7b1ab9d2c85a256c42c006060ffe7c2652638
parent 45eddc73d44f59d82fcf93e2213bb972c6b7c86a
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Wed, 12 Mar 2025 16:40:07 +0100

Revert API change

Diffstat:
MTODO_COORDINATES | 6+++++-
Mpython/nissy_module.c | 14++++++--------
Mshell/shell.c | 20+++-----------------
Msrc/nissy.c | 4+---
Msrc/nissy.h | 4----
Msrc/solvers/coord/common.h | 17+++++++++++++++++
Msrc/solvers/coord/gendata.h | 2+-
Msrc/solvers/coord/solve.h | 16+++++++---------
8 files changed, 40 insertions(+), 43 deletions(-)

diff --git a/TODO_COORDINATES b/TODO_COORDINATES @@ -1,14 +1,18 @@ - coord solver + - return to fully qualified solver name + x undo chnages to API + - add a parameter to gendata to get a shortname (filename) for the table - there is an error (try R B2 as scramble, EO on RL) - debug - fix - h48 solve dispatch: move "h7k2" part to "options"? - it makes sense, but breaks the rule "solver == table" + - if so, adjust tools that use nissy_solve() - add new parameter to solve(): options (char *) - fix all usages x tests x shell - - tools (after deciding if refactoring h48 solve dispatch) + x tools x python bindings - document what to do when changing interface? - where do I document which solvers take which parameters? diff --git a/python/nissy_module.c b/python/nissy_module.c @@ -315,7 +315,6 @@ PyDoc_STRVAR(solve_doc, "Parameters:\n" " - cube: a cube in B32 format\n" " - solver: the solver to use\n" -" - options: extra options for the chosen solver\n" " - minmoves: the minimum number of moves to use\n" " - maxmoves: the maximum number of moves to use\n" " - maxsolution: the maximum number of solutions to return\n" @@ -332,21 +331,20 @@ solve(PyObject *self, PyObject *args) long long result; unsigned nissflag, minmoves, maxmoves, maxsolutions; int optimal, i, j, k, threads; - const char *cube, *solver, *options; + const char *cube, *solver; char solutions[MAX_SOLUTIONS_SIZE]; long long stats[NISSY_SIZE_SOLVE_STATS]; PyByteArrayObject *data; PyObject *list, *item; - if (!PyArg_ParseTuple(args, "sssIIIIiiY", &cube, &solver, &options, - &nissflag, &minmoves, &maxmoves, &maxsolutions, &optimal, - &threads, &data)) + if (!PyArg_ParseTuple(args, "ssIIIIiiY", &cube, &solver, &nissflag, + &minmoves, &maxmoves, &maxsolutions, &optimal, &threads, &data)) return NULL; Py_BEGIN_ALLOW_THREADS - result = nissy_solve(cube, solver, options, nissflag, minmoves, - maxmoves, maxsolutions, optimal, threads, data->ob_alloc, - data->ob_bytes, MAX_SOLUTIONS_SIZE, solutions, stats); + result = nissy_solve(cube, solver, nissflag, minmoves, maxmoves, + maxsolutions, optimal, threads, data->ob_alloc, data->ob_bytes, + MAX_SOLUTIONS_SIZE, solutions, stats); Py_END_ALLOW_THREADS if(!check_error(result)) { diff --git a/shell/shell.c b/shell/shell.c @@ -17,7 +17,6 @@ #define FLAG_PERM "-perm" #define FLAG_COMMAND "-command" #define FLAG_STR_CUBE "-cubestr" -#define FLAG_SOLVE_OPTS "-options" #define FLAG_FORMAT "-format" #define FLAG_FORMAT_IN "-fin" #define FLAG_FORMAT_OUT "-fout" @@ -45,7 +44,6 @@ typedef struct { char cube_perm[22]; char *str_command; char *str_cube; - char *str_options; char *str_format; char *str_format_in; char *str_format_out; @@ -81,7 +79,6 @@ static bool set_cube(int, char **, args_t *); static bool set_cube_perm(int, char **, args_t *); static bool set_str_command(int, char **, args_t *); static bool set_str_cube(int, char **, args_t *); -static bool set_str_options(int, char **, args_t *); static bool set_str_format(int, char **, args_t *); static bool set_str_format_in(int, char **, args_t *); static bool set_str_format_out(int, char **, args_t *); @@ -108,7 +105,6 @@ struct { OPTION(FLAG_PERM, 1, set_cube_perm), OPTION(FLAG_COMMAND, 1, set_str_command), OPTION(FLAG_STR_CUBE, 1, set_str_cube), - OPTION(FLAG_SOLVE_OPTS, 1, set_str_options), OPTION(FLAG_FORMAT, 1, set_str_format), OPTION(FLAG_FORMAT_IN, 1, set_str_format_in), OPTION(FLAG_FORMAT_OUT, 1, set_str_format_out), @@ -201,7 +197,6 @@ struct { "solve", "solve " FLAG_SOLVER " SOLVER" "[" FLAG_MINMOVES " n] [" FLAG_MAXMOVES " N] " - "[" FLAG_SOLVE_OPTS " options] " FLAG_CUBE " CUBE" FLAG_THREADS " T", "Solve the given CUBE using SOLVER, " @@ -213,7 +208,6 @@ struct { "solve_scramble", "solve_scramble " FLAG_SOLVER " SOLVER" "[" FLAG_MINMOVES " n] [" FLAG_MAXMOVES " N] " - "[" FLAG_SOLVE_OPTS " options] " FLAG_MOVES " MOVES", "Solve the given SCRAMBLE using SOLVER, " "using at least n and at most N moves. " @@ -485,9 +479,9 @@ solve_exec(args_t *args) } ret = nissy_solve( - args->cube, args->str_solver, args->str_options, nissflag, - args->minmoves, args->maxmoves, args->maxsolutions, args->optimal, - args->threads, size, buf, SOLUTIONS_BUFFER_SIZE, solutions, stats); + args->cube, args->str_solver, nissflag, args->minmoves, + args->maxmoves, args->maxsolutions, args->optimal, args->threads, + size, buf, SOLUTIONS_BUFFER_SIZE, solutions, stats); free(buf); @@ -665,14 +659,6 @@ set_str_cube(int argc, char **argv, args_t *args) } static bool -set_str_options(int argc, char **argv, args_t *args) -{ - args->str_options = argv[0]; - - return true; -} - -static bool set_str_format(int argc, char **argv, args_t *args) { args->str_format = argv[0]; diff --git a/src/nissy.c b/src/nissy.c @@ -503,7 +503,6 @@ long long nissy_solve( const char cube[static NISSY_SIZE_B32], const char *solver, - const char *options, unsigned nissflag, unsigned minmoves, unsigned maxmoves, @@ -569,11 +568,10 @@ nissy_solve( parse_ret = parse_h48_solver(solver, &h, &k); if (parse_ret != NISSY_OK) return parse_ret; -/* TODO give warning if options is not NULL or empty? */ return solve_h48(c, minmoves, maxmoves, maxsols, opt, t, data_size, data, sols_size, sols, stats); } else if (!strncmp(solver, "coord_", 6)) { - return solve_coord_dispatch(c, solver + 6, options, nissflag, + return solve_coord_dispatch(c, solver + 6, nissflag, minmoves, maxmoves, maxsols, opt, t, data_size, data, sols_size, sols); } else { diff --git a/src/nissy.h b/src/nissy.h @@ -257,9 +257,6 @@ Solve the given cube using the given solver and options. Parameters: cube - The cube to solver, in B32 format. solver - The name of the solver. - options - Extra options for the solver. Some solver require additional - parameters, some do not. For example, a coordinate solver - may require an "axis" (UD, FB, RL) as a parameter. nissflag - The flags for NISS (linear, inverse, mixed, or combinations). minmoves - The minimum number of moves for a solution. maxmoves - The maximum number of moves for a solution. @@ -293,7 +290,6 @@ long long nissy_solve( const char cube[static NISSY_SIZE_B32], const char *solver, - const char *options, unsigned nissflag, unsigned minmoves, unsigned maxmoves, diff --git a/src/solvers/coord/common.h b/src/solvers/coord/common.h @@ -6,6 +6,7 @@ coord_t *all_coordinates[] = { STATIC void append_coord_name(const coord_t *, char *); STATIC coord_t *parse_coord(const char *, int); STATIC uint8_t parse_axis(const char *, int); +STATIC void parse_coord_and_axis(const char *, int, coord_t **, uint8_t *); STATIC void append_coord_name(const coord_t *coord, char *str) @@ -44,3 +45,19 @@ parse_axis(const char *axis, int n) return UINT8_ERROR; } + +STATIC void +parse_coord_and_axis(const char *str, int n, coord_t **coord, uint8_t *axis) +{ + int i; + + for (i = 0; i < n; i++) + if (str[i] == '_') + break; + + if (coord != NULL) + *coord = parse_coord(str, i); + + if (axis != NULL) + *axis = i == n ? UINT8_ERROR : parse_axis(str+i+1, n-i-1); +} diff --git a/src/solvers/coord/gendata.h b/src/solvers/coord/gendata.h @@ -9,7 +9,7 @@ gendata_coord_dispatch(const char *coordstr, void *buf) { coord_t *coord; - coord = parse_coord(coordstr, strlen(coordstr)); + parse_coord_and_axis(coordstr, strlen(coordstr), &coord, NULL); if (coord == NULL) { LOG("Could not parse coordinate '%s'\n", coord); diff --git a/src/solvers/coord/solve.h b/src/solvers/coord/solve.h @@ -20,9 +20,8 @@ typedef struct { STATIC int64_t solve_coord(cube_t, coord_t *, uint8_t, uint8_t, uint8_t, uint8_t, uint64_t, int, int, uint64_t, const void *, uint64_t, char *); -STATIC int64_t solve_coord_dispatch(cube_t, const char *, const char *, - uint8_t, uint8_t, uint8_t, uint64_t, int, int, uint64_t, const void *, - uint64_t, char *); +STATIC int64_t solve_coord_dispatch(cube_t, const char *, uint8_t, uint8_t, + uint8_t, uint64_t, int, int, uint64_t, const void *, uint64_t, char *); STATIC bool solve_coord_appendchar(char *, uint64_t, uint64_t *, char); STATIC int64_t solve_coord_appendsolution(dfsarg_solve_coord_t *); STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t *); @@ -123,8 +122,7 @@ solve_coord_dfs(dfsarg_solve_coord_t *arg) STATIC int64_t solve_coord_dispatch( cube_t cube, - const char *coordstr, - const char *options, + const char *coord_and_axis, uint8_t nissflag, uint8_t minmoves, uint8_t maxmoves, @@ -140,16 +138,16 @@ solve_coord_dispatch( coord_t *coord; uint8_t axis; - coord = parse_coord(coordstr, strlen(coordstr)); - axis = parse_axis(options, strlen(options)); + parse_coord_and_axis( + coord_and_axis, strlen(coord_and_axis), &coord, &axis); if (coord == NULL) { - LOG("Could not parse coordinate '%s'\n", coordstr); + LOG("Could not parse coordinate from '%s'\n", coord_and_axis); return NISSY_ERROR_INVALID_SOLVER; } if (axis == UINT8_ERROR) { - LOG("Could not parse axis from options '%s'\n", options); + LOG("Could not parse axis from '%s'\n", coord_and_axis); return NISSY_ERROR_INVALID_SOLVER; }