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 | |
| 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')
| -rw-r--r-- | src/solvers/coord/common.h | 25 | ||||
| -rw-r--r-- | src/solvers/coord/gendata.h | 4 | ||||
| -rw-r--r-- | src/solvers/coord/solve.h | 61 | ||||
| -rw-r--r-- | src/solvers/coord/types_macros.h | 2 | ||||
| -rw-r--r-- | src/solvers/h48/gendata_h48.h | 4 | ||||
| -rw-r--r-- | src/solvers/h48/gendata_types_macros.h | 1 | ||||
| -rw-r--r-- | src/solvers/h48/solve.h | 48 | ||||
| -rw-r--r-- | src/solvers/solutions.h | 14 | ||||
| -rw-r--r-- | src/solvers/solvers.h | 1 |
9 files changed, 77 insertions, 83 deletions
diff --git a/src/solvers/coord/common.h b/src/solvers/coord/common.h index d570b6d..8553658 100644 --- a/src/solvers/coord/common.h +++ b/src/solvers/coord/common.h | |||
| @@ -4,9 +4,9 @@ coord_t *all_coordinates[] = { | |||
| 4 | }; | 4 | }; |
| 5 | 5 | ||
| 6 | STATIC void append_coord_name(const coord_t *, char *); | 6 | STATIC void append_coord_name(const coord_t *, char *); |
| 7 | STATIC coord_t *parse_coord(const char *, int); | 7 | STATIC coord_t *parse_coord(size_t n, const char [n]); |
| 8 | STATIC uint8_t parse_axis(const char *, int); | 8 | STATIC uint8_t parse_axis(size_t n, const char [n]); |
| 9 | STATIC void parse_coord_and_axis(const char *, int, coord_t **, uint8_t *); | 9 | STATIC void parse_coord_and_axis(size_t n, const char [n], coord_t **, uint8_t *); |
| 10 | STATIC int64_t dataid_coord(const char *, char [static NISSY_DATAID_SIZE]); | 10 | STATIC int64_t dataid_coord(const char *, char [static NISSY_DATAID_SIZE]); |
| 11 | 11 | ||
| 12 | STATIC void | 12 | STATIC void |
| @@ -22,7 +22,7 @@ append_coord_name(const coord_t *coord, char *str) | |||
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | STATIC coord_t * | 24 | STATIC coord_t * |
| 25 | parse_coord(const char *coord, int n) | 25 | parse_coord(size_t n, const char coord[n]) |
| 26 | { | 26 | { |
| 27 | int i; | 27 | int i; |
| 28 | 28 | ||
| @@ -34,7 +34,7 @@ parse_coord(const char *coord, int n) | |||
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | STATIC uint8_t | 36 | STATIC uint8_t |
| 37 | parse_axis(const char *axis, int n) | 37 | parse_axis(size_t n, const char axis[n]) |
| 38 | { | 38 | { |
| 39 | if (!strncmp(axis, "UD", n) || !strncmp(axis, "DU", n)) { | 39 | if (!strncmp(axis, "UD", n) || !strncmp(axis, "DU", n)) { |
| 40 | return AXIS_UD; | 40 | return AXIS_UD; |
| @@ -48,19 +48,24 @@ parse_axis(const char *axis, int n) | |||
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | STATIC void | 50 | STATIC void |
| 51 | parse_coord_and_axis(const char *str, int n, coord_t **coord, uint8_t *axis) | 51 | parse_coord_and_axis( |
| 52 | size_t n, | ||
| 53 | const char str[n], | ||
| 54 | coord_t **coord, | ||
| 55 | uint8_t *axis | ||
| 56 | ) | ||
| 52 | { | 57 | { |
| 53 | int i; | 58 | size_t i; |
| 54 | 59 | ||
| 55 | for (i = 0; i < n; i++) | 60 | for (i = 0; i < n; i++) |
| 56 | if (str[i] == '_') | 61 | if (str[i] == '_') |
| 57 | break; | 62 | break; |
| 58 | 63 | ||
| 59 | if (coord != NULL) | 64 | if (coord != NULL) |
| 60 | *coord = parse_coord(str, i); | 65 | *coord = parse_coord(i, str); |
| 61 | 66 | ||
| 62 | if (axis != NULL) | 67 | if (axis != NULL) |
| 63 | *axis = i == n ? UINT8_ERROR : parse_axis(str+i+1, n-i-1); | 68 | *axis = i == n ? UINT8_ERROR : parse_axis(n-i-1, str+i+1); |
| 64 | } | 69 | } |
| 65 | 70 | ||
| 66 | STATIC int64_t | 71 | STATIC int64_t |
| @@ -68,7 +73,7 @@ dataid_coord(const char *ca, char dataid[static NISSY_DATAID_SIZE]) | |||
| 68 | { | 73 | { |
| 69 | coord_t *c; | 74 | coord_t *c; |
| 70 | 75 | ||
| 71 | parse_coord_and_axis(ca, strlen(ca), &c, NULL); | 76 | parse_coord_and_axis(strlen(ca), ca, &c, NULL); |
| 72 | 77 | ||
| 73 | if (c == NULL) { | 78 | if (c == NULL) { |
| 74 | LOG("dataid_coord: cannot parse coordinate from '%s'\n", ca); | 79 | LOG("dataid_coord: cannot parse coordinate from '%s'\n", ca); |
diff --git a/src/solvers/coord/gendata.h b/src/solvers/coord/gendata.h index 93ffc65..5819608 100644 --- a/src/solvers/coord/gendata.h +++ b/src/solvers/coord/gendata.h | |||
| @@ -11,7 +11,7 @@ gendata_coord_dispatch(const char *coordstr, void *buf) | |||
| 11 | { | 11 | { |
| 12 | coord_t *coord; | 12 | coord_t *coord; |
| 13 | 13 | ||
| 14 | parse_coord_and_axis(coordstr, strlen(coordstr), &coord, NULL); | 14 | parse_coord_and_axis(strlen(coordstr), coordstr, &coord, NULL); |
| 15 | 15 | ||
| 16 | if (coord == NULL) { | 16 | if (coord == NULL) { |
| 17 | LOG("Could not parse coordinate '%s'\n", coord); | 17 | LOG("Could not parse coordinate '%s'\n", coord); |
| @@ -137,7 +137,7 @@ getdistribution_coord( | |||
| 137 | 137 | ||
| 138 | memset(distr, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); | 138 | memset(distr, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); |
| 139 | 139 | ||
| 140 | if((c = parse_coord(coord, strlen(coord))) == NULL) | 140 | if((c = parse_coord(strlen(coord), coord)) == NULL) |
| 141 | return; | 141 | return; |
| 142 | 142 | ||
| 143 | for (i = 0; i < c->max; i++) { | 143 | for (i = 0; i < c->max; i++) { |
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; |
diff --git a/src/solvers/coord/types_macros.h b/src/solvers/coord/types_macros.h index 2309efc..f2d436c 100644 --- a/src/solvers/coord/types_macros.h +++ b/src/solvers/coord/types_macros.h | |||
| @@ -11,5 +11,5 @@ typedef struct { | |||
| 11 | uint32_t moves_mask; | 11 | uint32_t moves_mask; |
| 12 | uint64_t trans_mask; | 12 | uint64_t trans_mask; |
| 13 | uint8_t axistrans[3]; | 13 | uint8_t axistrans[3]; |
| 14 | bool (*is_admissible)(int n, uint8_t [n]); | 14 | bool (*is_admissible)(size_t n, uint8_t [n]); |
| 15 | } coord_t; | 15 | } coord_t; |
diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h index 55db1be..2d9a9f7 100644 --- a/src/solvers/h48/gendata_h48.h +++ b/src/solvers/h48/gendata_h48.h | |||
| @@ -533,7 +533,7 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t *arg) | |||
| 533 | /* Depth d+3 */ | 533 | /* Depth d+3 */ |
| 534 | for (m[2] = 0; m[2] < 18; m[2]++) { | 534 | for (m[2] = 0; m[2] < 18; m[2]++) { |
| 535 | markarg.depth = d+3; | 535 | markarg.depth = d+3; |
| 536 | if (!allowednextmove(m, 3)) { | 536 | if (!allowednextmove(3, m)) { |
| 537 | m[2] += 2; | 537 | m[2] += 2; |
| 538 | continue; | 538 | continue; |
| 539 | } | 539 | } |
| @@ -548,7 +548,7 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t *arg) | |||
| 548 | /* Depth d+4 */ | 548 | /* Depth d+4 */ |
| 549 | for (m[3] = 0; m[3] < 18; m[3]++) { | 549 | for (m[3] = 0; m[3] < 18; m[3]++) { |
| 550 | markarg.depth = d+4; | 550 | markarg.depth = d+4; |
| 551 | if (!allowednextmove(m, 4)) { | 551 | if (!allowednextmove(4, m)) { |
| 552 | m[3] += 2; | 552 | m[3] += 2; |
| 553 | continue; | 553 | continue; |
| 554 | } | 554 | } |
diff --git a/src/solvers/h48/gendata_types_macros.h b/src/solvers/h48/gendata_types_macros.h index a2026b2..ec6576a 100644 --- a/src/solvers/h48/gendata_types_macros.h +++ b/src/solvers/h48/gendata_types_macros.h | |||
| @@ -28,7 +28,6 @@ | |||
| 28 | #define H48_SHIFT(i, k) ((uint8_t)(k) * (uint8_t)((i) % H48_COEFF(k))) | 28 | #define H48_SHIFT(i, k) ((uint8_t)(k) * (uint8_t)((i) % H48_COEFF(k))) |
| 29 | #define H48_MASK(i, k) ((UINT8_BIT(k) - UINT8_C(1)) << H48_SHIFT(i, k)) | 29 | #define H48_MASK(i, k) ((UINT8_BIT(k) - UINT8_C(1)) << H48_SHIFT(i, k)) |
| 30 | 30 | ||
| 31 | #define MAXLEN 20 | ||
| 32 | #define CHUNKS COCSEP_CLASSES | 31 | #define CHUNKS COCSEP_CLASSES |
| 33 | 32 | ||
| 34 | /* | 33 | /* |
diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h index 182beee..f7565d5 100644 --- a/src/solvers/h48/solve.h +++ b/src/solvers/h48/solve.h | |||
| @@ -32,8 +32,8 @@ typedef struct { | |||
| 32 | const uint8_t *h48data; | 32 | const uint8_t *h48data; |
| 33 | const uint8_t *h48data_fallback_h0k4; | 33 | const uint8_t *h48data_fallback_h0k4; |
| 34 | const void *h48data_fallback_eoesep; | 34 | const void *h48data_fallback_eoesep; |
| 35 | uint64_t solutions_size; | 35 | size_t solutions_size; |
| 36 | uint64_t *solutions_used; | 36 | size_t *solutions_used; |
| 37 | char **solutions; | 37 | char **solutions; |
| 38 | uint32_t movemask_normal; | 38 | uint32_t movemask_normal; |
| 39 | uint32_t movemask_inverse; | 39 | uint32_t movemask_inverse; |
| @@ -58,7 +58,6 @@ typedef struct { | |||
| 58 | 58 | ||
| 59 | STATIC int64_t solve_h48_appendsolution(dfsarg_solve_h48_t *); | 59 | STATIC int64_t solve_h48_appendsolution(dfsarg_solve_h48_t *); |
| 60 | STATIC int64_t solve_h48_appendallsym(dfsarg_solve_h48_t *); | 60 | STATIC int64_t solve_h48_appendallsym(dfsarg_solve_h48_t *); |
| 61 | STATIC bool solve_h48_appendchar(dfsarg_solve_h48_t *, char); | ||
| 62 | STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t *); | 61 | STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t *); |
| 63 | STATIC int64_t solve_h48_maketasks( | 62 | STATIC int64_t solve_h48_maketasks( |
| 64 | dfsarg_solve_h48_t *, dfsarg_solve_h48_maketasks_t *, | 63 | dfsarg_solve_h48_t *, dfsarg_solve_h48_maketasks_t *, |
| @@ -66,7 +65,7 @@ STATIC int64_t solve_h48_maketasks( | |||
| 66 | STATIC void *solve_h48_runthread(void *); | 65 | STATIC void *solve_h48_runthread(void *); |
| 67 | STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t *); | 66 | STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t *); |
| 68 | STATIC int64_t solve_h48(cube_t, int8_t, int8_t, uint64_t, int8_t, int8_t, | 67 | STATIC int64_t solve_h48(cube_t, int8_t, int8_t, uint64_t, int8_t, int8_t, |
| 69 | uint64_t, const void *, uint64_t, char *, | 68 | uint64_t, const void *, size_t, char *, |
| 70 | long long [static NISSY_SIZE_SOLVE_STATS]); | 69 | long long [static NISSY_SIZE_SOLVE_STATS]); |
| 71 | 70 | ||
| 72 | STATIC int64_t | 71 | STATIC int64_t |
| @@ -76,15 +75,15 @@ solve_h48_appendsolution(dfsarg_solve_h48_t *arg) | |||
| 76 | arg->nmoves + arg->npremoves > *arg->shortest_sol + arg->optimal) | 75 | arg->nmoves + arg->npremoves > *arg->shortest_sol + arg->optimal) |
| 77 | return 0; | 76 | return 0; |
| 78 | 77 | ||
| 79 | invertmoves(arg->premoves, arg->npremoves, arg->moves + arg->nmoves); | 78 | invertmoves(arg->npremoves, arg->premoves, arg->moves + arg->nmoves); |
| 80 | 79 | ||
| 81 | /* Sort parallel moves for consistency */ | 80 | /* Sort parallel moves for consistency */ |
| 82 | sortparallel(arg->moves, arg->nmoves + arg->npremoves); | 81 | sortparallel(arg->nmoves + arg->npremoves, arg->moves); |
| 83 | 82 | ||
| 84 | /* Do not append the solution in case premoves cancel with normal */ | 83 | /* Do not append the solution in case premoves cancel with normal */ |
| 85 | if (arg->npremoves > 0 && !allowednextmove(arg->moves, arg->nmoves+1)) | 84 | if (arg->npremoves > 0 && !allowednextmove(arg->nmoves+1, arg->moves)) |
| 86 | return 0; | 85 | return 0; |
| 87 | if (arg->npremoves > 1 && !allowednextmove(arg->moves, arg->nmoves+2)) | 86 | if (arg->npremoves > 1 && !allowednextmove(arg->nmoves+2, arg->moves)) |
| 88 | return 0; | 87 | return 0; |
| 89 | 88 | ||
| 90 | return solve_h48_appendallsym(arg); | 89 | return solve_h48_appendallsym(arg); |
| @@ -109,7 +108,7 @@ solve_h48_appendallsym(dfsarg_solve_h48_t *arg) | |||
| 109 | all[j][i] = transform_move(arg->moves[i], t); | 108 | all[j][i] = transform_move(arg->moves[i], t); |
| 110 | 109 | ||
| 111 | /* Sort parallel moves for consistency */ | 110 | /* Sort parallel moves for consistency */ |
| 112 | sortparallel(all[j], n); | 111 | sortparallel(n, all[j]); |
| 113 | 112 | ||
| 114 | /* Check for duplicate solutions */ | 113 | /* Check for duplicate solutions */ |
| 115 | for (k = 0; k < j; k++) { | 114 | for (k = 0; k < j; k++) { |
| @@ -133,7 +132,7 @@ solve_h48_appendallsym(dfsarg_solve_h48_t *arg) | |||
| 133 | for (k = 0; k < j && *arg->nsols < arg->maxsolutions; k++) { | 132 | for (k = 0; k < j && *arg->nsols < arg->maxsolutions; k++) { |
| 134 | l = arg->solutions_size - *arg->solutions_used; | 133 | l = arg->solutions_size - *arg->solutions_used; |
| 135 | m = *arg->solutions + *arg->solutions_used; | 134 | m = *arg->solutions + *arg->solutions_used; |
| 136 | strl = writemoves(all[k], n, l, m); | 135 | strl = writemoves(n, all[k], l, m); |
| 137 | if (strl < 0) | 136 | if (strl < 0) |
| 138 | goto solve_h48_appendallsym_error; | 137 | goto solve_h48_appendallsym_error; |
| 139 | 138 | ||
| @@ -141,7 +140,8 @@ solve_h48_appendallsym(dfsarg_solve_h48_t *arg) | |||
| 141 | 140 | ||
| 142 | *arg->solutions_used += MAX(0, strl-1); | 141 | *arg->solutions_used += MAX(0, strl-1); |
| 143 | 142 | ||
| 144 | if (!solve_h48_appendchar(arg, '\n')) | 143 | if (!appendchar(arg->solutions_size, |
| 144 | *arg->solutions, arg->solutions_used, '\n')) | ||
| 145 | goto solve_h48_appendallsym_error; | 145 | goto solve_h48_appendallsym_error; |
| 146 | 146 | ||
| 147 | (*arg->nsols)++; | 147 | (*arg->nsols)++; |
| @@ -156,18 +156,6 @@ solve_h48_appendallsym_error: | |||
| 156 | return NISSY_ERROR_BUFFER_SIZE; | 156 | return NISSY_ERROR_BUFFER_SIZE; |
| 157 | } | 157 | } |
| 158 | 158 | ||
| 159 | STATIC bool | ||
| 160 | solve_h48_appendchar(dfsarg_solve_h48_t *arg, char c) | ||
| 161 | { | ||
| 162 | if (arg->solutions_size <= *arg->solutions_used) | ||
| 163 | return false; | ||
| 164 | |||
| 165 | *(*arg->solutions + *arg->solutions_used) = c; | ||
| 166 | (*arg->solutions_used)++; | ||
| 167 | |||
| 168 | return true; | ||
| 169 | } | ||
| 170 | |||
| 171 | STATIC_INLINE bool | 159 | STATIC_INLINE bool |
| 172 | solve_h48_stop(dfsarg_solve_h48_t *arg) | 160 | solve_h48_stop(dfsarg_solve_h48_t *arg) |
| 173 | { | 161 | { |
| @@ -283,9 +271,9 @@ solve_h48_dfs(dfsarg_solve_h48_t *arg) | |||
| 283 | ulbi = arg->use_lb_inverse; | 271 | ulbi = arg->use_lb_inverse; |
| 284 | 272 | ||
| 285 | ret = 0; | 273 | ret = 0; |
| 286 | mm_normal = allowednextmove_mask(arg->moves, arg->nmoves) & | 274 | mm_normal = allowednextmove_mask(arg->nmoves, arg->moves) & |
| 287 | arg->movemask_normal; | 275 | arg->movemask_normal; |
| 288 | mm_inverse = allowednextmove_mask(arg->premoves, arg->npremoves) & | 276 | mm_inverse = allowednextmove_mask(arg->npremoves, arg->premoves) & |
| 289 | arg->movemask_inverse; | 277 | arg->movemask_inverse; |
| 290 | if (popcount_u32(mm_normal) <= popcount_u32(mm_inverse)) { | 278 | if (popcount_u32(mm_normal) <= popcount_u32(mm_inverse)) { |
| 291 | arg->nmoves++; | 279 | arg->nmoves++; |
| @@ -397,7 +385,7 @@ solve_h48_maketasks( | |||
| 397 | return NISSY_OK; | 385 | return NISSY_OK; |
| 398 | } | 386 | } |
| 399 | 387 | ||
| 400 | mm = allowednextmove_mask(maketasks_arg->moves, maketasks_arg->nmoves); | 388 | mm = allowednextmove_mask(maketasks_arg->nmoves, maketasks_arg->moves); |
| 401 | 389 | ||
| 402 | maketasks_arg->nmoves++; | 390 | maketasks_arg->nmoves++; |
| 403 | backup_cube = maketasks_arg->cube; | 391 | backup_cube = maketasks_arg->cube; |
| @@ -435,7 +423,7 @@ solve_h48( | |||
| 435 | int8_t threads, | 423 | int8_t threads, |
| 436 | uint64_t data_size, | 424 | uint64_t data_size, |
| 437 | const void *data, | 425 | const void *data, |
| 438 | uint64_t solutions_size, | 426 | size_t solutions_size, |
| 439 | char *solutions, | 427 | char *solutions, |
| 440 | long long stats[static NISSY_SIZE_SOLVE_STATS] | 428 | long long stats[static NISSY_SIZE_SOLVE_STATS] |
| 441 | ) | 429 | ) |
| @@ -447,7 +435,8 @@ solve_h48( | |||
| 447 | solve_h48_task_t tasks[STARTING_CUBES]; | 435 | solve_h48_task_t tasks[STARTING_CUBES]; |
| 448 | dfsarg_solve_h48_maketasks_t maketasks_arg; | 436 | dfsarg_solve_h48_maketasks_t maketasks_arg; |
| 449 | long double fallback_rate, lookups_per_node; | 437 | long double fallback_rate, lookups_per_node; |
| 450 | uint64_t solutions_used, symmask, offset; | 438 | uint64_t symmask, offset; |
| 439 | size_t solutions_used; | ||
| 451 | int64_t nodes_visited, table_lookups, table_fallbacks; | 440 | int64_t nodes_visited, table_lookups, table_fallbacks; |
| 452 | tableinfo_t info, fbinfo, fbinfo2; | 441 | tableinfo_t info, fbinfo, fbinfo2; |
| 453 | const uint32_t *cocsepdata; | 442 | const uint32_t *cocsepdata; |
| @@ -561,7 +550,8 @@ solve_h48( | |||
| 561 | } | 550 | } |
| 562 | 551 | ||
| 563 | solve_h48_done: | 552 | solve_h48_done: |
| 564 | if (!solve_h48_appendchar(&arg[0], '\0')) | 553 | if (!appendchar(arg[0].solutions_size, *arg[0].solutions, |
| 554 | arg[0].solutions_used, '\0')) | ||
| 565 | goto solve_h48_error_solutions_buffer; | 555 | goto solve_h48_error_solutions_buffer; |
| 566 | 556 | ||
| 567 | nodes_visited = table_lookups = table_fallbacks = 0; | 557 | nodes_visited = table_lookups = table_fallbacks = 0; |
diff --git a/src/solvers/solutions.h b/src/solvers/solutions.h new file mode 100644 index 0000000..1396210 --- /dev/null +++ b/src/solvers/solutions.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | #define MAXLEN 20 | ||
| 2 | |||
| 3 | STATIC bool appendchar(size_t n, char [n], size_t *, char); | ||
| 4 | |||
| 5 | STATIC bool | ||
| 6 | appendchar(size_t n, char s[n], size_t *used, char c) | ||
| 7 | { | ||
| 8 | if (n <= *used) | ||
| 9 | return false; | ||
| 10 | |||
| 11 | s[(*used)++] = c; | ||
| 12 | |||
| 13 | return true; | ||
| 14 | } | ||
diff --git a/src/solvers/solvers.h b/src/solvers/solvers.h index 7ef1746..42184b9 100644 --- a/src/solvers/solvers.h +++ b/src/solvers/solvers.h | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | #include "solutions.h" | ||
| 1 | #include "tables.h" | 2 | #include "tables.h" |
| 2 | #include "h48/h48.h" | 3 | #include "h48/h48.h" |
| 3 | #include "coord/coord.h" | 4 | #include "coord/coord.h" |
