diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-22 06:43:11 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-22 18:45:47 +0100 |
| commit | ce3f1cc0ef9f46d70ab5387b1458e9098b40711d (patch) | |
| tree | 4949745670b829f2211381bf14b8440dcc6ca7b1 /src/solvers/coord/solve.h | |
| parent | 0550a16c1cce868bbc3f3b5ad59f80e35cf2a6cd (diff) | |
| download | nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.tar.gz nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.zip | |
Some safety with move arrays, small refactor appendchar
Diffstat (limited to 'src/solvers/coord/solve.h')
| -rw-r--r-- | src/solvers/coord/solve.h | 61 |
1 files changed, 23 insertions, 38 deletions
diff --git a/src/solvers/coord/solve.h b/src/solvers/coord/solve.h index 6903d1e..fa160f8 100644 --- a/src/solvers/coord/solve.h +++ b/src/solvers/coord/solve.h | |||
| @@ -1,10 +1,8 @@ | |||
| 1 | #define MAXLEN_COORDSOL 20 | ||
| 2 | |||
| 3 | typedef struct { | 1 | typedef struct { |
| 4 | cube_t cube; | 2 | cube_t cube; |
| 5 | uint8_t depth; | 3 | uint8_t depth; |
| 6 | uint8_t nmoves; | 4 | uint8_t nmoves; |
| 7 | uint8_t moves[MAXLEN_COORDSOL]; | 5 | uint8_t moves[MAXLEN]; |
| 8 | coord_t *coord; | 6 | coord_t *coord; |
| 9 | const void *coord_data; | 7 | const void *coord_data; |
| 10 | const uint8_t *ptable; | 8 | const uint8_t *ptable; |
| @@ -13,23 +11,22 @@ typedef struct { | |||
| 13 | int64_t maxsolutions; | 11 | int64_t maxsolutions; |
| 14 | int optimal; | 12 | int optimal; |
| 15 | uint8_t *shortest_sol; | 13 | uint8_t *shortest_sol; |
| 16 | uint64_t solutions_size; | 14 | size_t solutions_size; |
| 17 | uint64_t *solutions_used; | 15 | size_t *solutions_used; |
| 18 | char **solutions; | 16 | char **solutions; |
| 19 | } dfsarg_solve_coord_t; | 17 | } dfsarg_solve_coord_t; |
| 20 | 18 | ||
| 21 | STATIC int64_t solve_coord(cube_t, coord_t *, uint8_t, uint8_t, uint8_t, | 19 | STATIC int64_t solve_coord(cube_t, coord_t *, uint8_t, uint8_t, uint8_t, |
| 22 | uint8_t, uint64_t, int, int, uint64_t, const void *, uint64_t, char *); | 20 | uint8_t, uint64_t, int, int, uint64_t, const void *, size_t, char *); |
| 23 | STATIC int64_t solve_coord_dispatch(cube_t, const char *, uint8_t, uint8_t, | 21 | STATIC int64_t solve_coord_dispatch(cube_t, const char *, uint8_t, uint8_t, |
| 24 | uint8_t, uint64_t, int, int, uint64_t, const void *, uint64_t, char *); | 22 | uint8_t, uint64_t, int, int, uint64_t, const void *, size_t, char *); |
| 25 | STATIC bool solve_coord_appendchar(char *, uint64_t, uint64_t *, char); | ||
| 26 | STATIC int64_t solve_coord_appendsolution(dfsarg_solve_coord_t *); | 23 | STATIC int64_t solve_coord_appendsolution(dfsarg_solve_coord_t *); |
| 27 | STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t *); | 24 | STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t *); |
| 28 | 25 | ||
| 29 | STATIC int64_t | 26 | STATIC int64_t |
| 30 | solve_coord_appendsolution(dfsarg_solve_coord_t *arg) | 27 | solve_coord_appendsolution(dfsarg_solve_coord_t *arg) |
| 31 | { | 28 | { |
| 32 | uint8_t i, t, tmoves[MAXLEN_COORDSOL]; | 29 | uint8_t i, t, tmoves[MAXLEN]; |
| 33 | int64_t strl; | 30 | int64_t strl; |
| 34 | uint64_t l; | 31 | uint64_t l; |
| 35 | char *m; | 32 | char *m; |
| @@ -44,18 +41,18 @@ solve_coord_appendsolution(dfsarg_solve_coord_t *arg) | |||
| 44 | for (i = 0; i < arg->nmoves; i++) | 41 | for (i = 0; i < arg->nmoves; i++) |
| 45 | tmoves[i] = transform_move(arg->moves[i], t); | 42 | tmoves[i] = transform_move(arg->moves[i], t); |
| 46 | 43 | ||
| 47 | sortparallel(tmoves, arg->nmoves); | 44 | sortparallel(arg->nmoves, tmoves); |
| 48 | 45 | ||
| 49 | l = arg->solutions_size - *arg->solutions_used; | 46 | l = arg->solutions_size - *arg->solutions_used; |
| 50 | m = *arg->solutions + *arg->solutions_used; | 47 | m = *arg->solutions + *arg->solutions_used; |
| 51 | strl = writemoves(tmoves, arg->nmoves, l, m); | 48 | strl = writemoves(arg->nmoves, tmoves, l, m); |
| 52 | if (strl < 0) | 49 | if (strl < 0) |
| 53 | goto solve_coord_appendsolution_error; | 50 | goto solve_coord_appendsolution_error; |
| 54 | 51 | ||
| 55 | *arg->solutions_used += MAX(0, strl-1); | 52 | *arg->solutions_used += MAX(0, strl-1); |
| 56 | 53 | ||
| 57 | if (!solve_coord_appendchar( | 54 | if (!appendchar( |
| 58 | *arg->solutions, arg->solutions_size, arg->solutions_used, '\n')) | 55 | arg->solutions_size, *arg->solutions, arg->solutions_used, '\n')) |
| 59 | goto solve_coord_appendsolution_error; | 56 | goto solve_coord_appendsolution_error; |
| 60 | 57 | ||
| 61 | (*arg->nsols)++; | 58 | (*arg->nsols)++; |
| @@ -68,18 +65,6 @@ solve_coord_appendsolution_error: | |||
| 68 | return NISSY_ERROR_BUFFER_SIZE; | 65 | return NISSY_ERROR_BUFFER_SIZE; |
| 69 | } | 66 | } |
| 70 | 67 | ||
| 71 | STATIC bool | ||
| 72 | solve_coord_appendchar(char *s, uint64_t s_size, uint64_t *s_used, char c) | ||
| 73 | { | ||
| 74 | if (s_size == *s_used) | ||
| 75 | return false; | ||
| 76 | |||
| 77 | s[*s_used] = c; | ||
| 78 | (*s_used)++; | ||
| 79 | |||
| 80 | return true; | ||
| 81 | } | ||
| 82 | |||
| 83 | STATIC int64_t | 68 | STATIC int64_t |
| 84 | solve_coord_dfs(dfsarg_solve_coord_t *arg) | 69 | solve_coord_dfs(dfsarg_solve_coord_t *arg) |
| 85 | { | 70 | { |
| @@ -104,7 +89,7 @@ solve_coord_dfs(dfsarg_solve_coord_t *arg) | |||
| 104 | backup_cube = arg->cube; | 89 | backup_cube = arg->cube; |
| 105 | 90 | ||
| 106 | ret = 0; | 91 | ret = 0; |
| 107 | mm = allowednextmove_mask(arg->moves, arg->nmoves); | 92 | mm = allowednextmove_mask(arg->nmoves, arg->moves); |
| 108 | arg->nmoves++; | 93 | arg->nmoves++; |
| 109 | for (m = 0; m < 18; m++) { | 94 | for (m = 0; m < 18; m++) { |
| 110 | if (!(mm & (1 << m))) | 95 | if (!(mm & (1 << m))) |
| @@ -135,7 +120,7 @@ solve_coord_dispatch( | |||
| 135 | int threads, | 120 | int threads, |
| 136 | uint64_t data_size, | 121 | uint64_t data_size, |
| 137 | const void *data, | 122 | const void *data, |
| 138 | uint64_t sols_size, | 123 | size_t solutions_size, |
| 139 | char *sols | 124 | char *sols |
| 140 | ) | 125 | ) |
| 141 | { | 126 | { |
| @@ -143,7 +128,7 @@ solve_coord_dispatch( | |||
| 143 | uint8_t axis; | 128 | uint8_t axis; |
| 144 | 129 | ||
| 145 | parse_coord_and_axis( | 130 | parse_coord_and_axis( |
| 146 | coord_and_axis, strlen(coord_and_axis), &coord, &axis); | 131 | strlen(coord_and_axis), coord_and_axis, &coord, &axis); |
| 147 | 132 | ||
| 148 | if (coord == NULL) { | 133 | if (coord == NULL) { |
| 149 | LOG("Could not parse coordinate from '%s'\n", coord_and_axis); | 134 | LOG("Could not parse coordinate from '%s'\n", coord_and_axis); |
| @@ -156,7 +141,8 @@ solve_coord_dispatch( | |||
| 156 | } | 141 | } |
| 157 | 142 | ||
| 158 | return solve_coord(cube, coord, axis, nissflag, minmoves, maxmoves, | 143 | return solve_coord(cube, coord, axis, nissflag, minmoves, maxmoves, |
| 159 | maxsolutions, optimal, threads, data_size, data, sols_size, sols); | 144 | maxsolutions, optimal, threads, data_size, data, |
| 145 | solutions_size, sols); | ||
| 160 | } | 146 | } |
| 161 | 147 | ||
| 162 | STATIC int64_t | 148 | STATIC int64_t |
| @@ -172,14 +158,14 @@ solve_coord( | |||
| 172 | int threads, | 158 | int threads, |
| 173 | uint64_t data_size, | 159 | uint64_t data_size, |
| 174 | const void *data, | 160 | const void *data, |
| 175 | uint64_t sols_size, | 161 | size_t solutions_size, |
| 176 | char *sols | 162 | char *sols |
| 177 | ) | 163 | ) |
| 178 | { | 164 | { |
| 179 | int8_t d; | 165 | int8_t d; |
| 180 | uint8_t t, shortest_sol; | 166 | uint8_t t, shortest_sol; |
| 181 | int64_t nsols, ndepth; | 167 | int64_t nsols, ndepth; |
| 182 | uint64_t sols_used; | 168 | size_t solutions_used; |
| 183 | cube_t c; | 169 | cube_t c; |
| 184 | const void *coord_data; | 170 | const void *coord_data; |
| 185 | const uint8_t *ptable; | 171 | const uint8_t *ptable; |
| @@ -200,8 +186,8 @@ solve_coord( | |||
| 200 | } | 186 | } |
| 201 | 187 | ||
| 202 | nsols = 0; | 188 | nsols = 0; |
| 203 | sols_used = 0; | 189 | solutions_used = 0; |
| 204 | shortest_sol = MAXLEN_COORDSOL + 1; | 190 | shortest_sol = MAXLEN + 1; |
| 205 | t = coord->axistrans[axis]; | 191 | t = coord->axistrans[axis]; |
| 206 | c = transform(cube, t); | 192 | c = transform(cube, t); |
| 207 | 193 | ||
| @@ -215,16 +201,15 @@ solve_coord( | |||
| 215 | .maxsolutions = (int64_t)maxsolutions, | 201 | .maxsolutions = (int64_t)maxsolutions, |
| 216 | .optimal = optimal, | 202 | .optimal = optimal, |
| 217 | .shortest_sol = &shortest_sol, | 203 | .shortest_sol = &shortest_sol, |
| 218 | .solutions_size = sols_size, | 204 | .solutions_size = solutions_size, |
| 219 | .solutions_used = &sols_used, | 205 | .solutions_used = &solutions_used, |
| 220 | .solutions = &sols, | 206 | .solutions = &sols, |
| 221 | }; | 207 | }; |
| 222 | 208 | ||
| 223 | if (coord->coord(c, coord_data) == 0) { | 209 | if (coord->coord(c, coord_data) == 0) { |
| 224 | if (minmoves == 0) { | 210 | if (minmoves == 0) { |
| 225 | nsols = 1; | 211 | nsols = 1; |
| 226 | if (!solve_coord_appendchar( | 212 | if (!appendchar(solutions_size, sols, &solutions_used, '\n')) |
| 227 | sols, sols_size, &sols_used, '\n')) | ||
| 228 | goto solve_coord_error_buffer; | 213 | goto solve_coord_error_buffer; |
| 229 | } | 214 | } |
| 230 | goto solve_coord_done; | 215 | goto solve_coord_done; |
| @@ -254,7 +239,7 @@ solve_coord( | |||
| 254 | } | 239 | } |
| 255 | 240 | ||
| 256 | solve_coord_done: | 241 | solve_coord_done: |
| 257 | if (!solve_coord_appendchar(sols, sols_size, &sols_used, '\0')) | 242 | if (!appendchar(solutions_size, sols, &solutions_used, '\0')) |
| 258 | goto solve_coord_error_buffer; | 243 | goto solve_coord_error_buffer; |
| 259 | 244 | ||
| 260 | return nsols; | 245 | return nsols; |
