diff options
Diffstat (limited to '')
| -rw-r--r-- | src/nissy.h | 229 |
1 files changed, 189 insertions, 40 deletions
diff --git a/src/nissy.h b/src/nissy.h index c78fb50..c019998 100644 --- a/src/nissy.h +++ b/src/nissy.h | |||
| @@ -8,10 +8,9 @@ If you include this file, you should also use the following includes: | |||
| 8 | #include <stdbool> | 8 | #include <stdbool> |
| 9 | #include <string> | 9 | #include <string> |
| 10 | 10 | ||
| 11 | All the functions below return 0 in case of success and a positive number | 11 | All the functions return 0 or a positive integer in case of success and |
| 12 | in case of error, unless otherwise specified. Errors are checked in code | 12 | a negative integer in case of error, unless otherwise specified. |
| 13 | order: for example if error code 1 is returned then it could that also | 13 | You can see the list of error codes below, or use nissy_explainerror(). |
| 14 | an error with code 2 or higher occurred. | ||
| 15 | 14 | ||
| 16 | Arguments of type char [static 22] denote a cube in B32 format. | 15 | Arguments of type char [static 22] denote a cube in B32 format. |
| 17 | Other available formats are H48 and SRC. See README.md for more info on | 16 | Other available formats are H48 and SRC. See README.md for more info on |
| @@ -25,14 +24,48 @@ A transformation must be given in the format | |||
| 25 | for example 'rotation UF' or 'mirrored BL'. | 24 | for example 'rotation UF' or 'mirrored BL'. |
| 26 | */ | 25 | */ |
| 27 | 26 | ||
| 27 | /* Error codes */ | ||
| 28 | #define NISSY_OK INT64_C(0) | ||
| 29 | #define NISSY_WARNING_UNSOLVABLE INT64_C(-1) | ||
| 30 | #define NISSY_WARNING_NULL_CALLBACK INT64_C(-2) | ||
| 31 | #define NISSY_ERROR_INVALID_CUBE INT64_C(-10) | ||
| 32 | #define NISSY_ERROR_UNSOLVABLE_CUBE INT64_C(-11) | ||
| 33 | #define NISSY_ERROR_INVALID_MOVES INT64_C(-20) | ||
| 34 | #define NISSY_ERROR_INVALID_TRANS INT64_C(-30) | ||
| 35 | #define NISSY_ERROR_INVALID_FORMAT INT64_C(-40) | ||
| 36 | #define NISSY_ERROR_INVALID_SOLVER INT64_C(-50) | ||
| 37 | #define NISSY_ERROR_NULL_POINTER INT64_C(-60) | ||
| 38 | #define NISSY_ERROR_DATA INT64_C(-70) | ||
| 39 | #define NISSY_ERROR_OPTIONS INT64_C(-80) | ||
| 40 | #define NISSY_ERROR_INVALID_CODE INT64_C(-90) | ||
| 41 | #define NISSY_ERROR_UNKNOWN INT64_C(-999) | ||
| 42 | |||
| 43 | /* Flags for NISS options */ | ||
| 44 | #define NISSY_NISSFLAG_NORMAL UINT8_C(1) | ||
| 45 | #define NISSY_NISSFLAG_INVERSE UINT8_C(2) | ||
| 46 | #define NISSY_NISSFLAG_MIXED UINT8_C(4) | ||
| 47 | #define NISSY_NISSFLAG_LINEAR \ | ||
| 48 | (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_INVERSE) | ||
| 49 | #define NISSY_NISSFLAG_ALL \ | ||
| 50 | (NISSY_NISSFLAG_NORMAL | NISSY_NISSFLAG_INVERSE | NISSY_NISSFLAG_MIXED) | ||
| 51 | |||
| 28 | /* | 52 | /* |
| 29 | Apply the secod argument as a permutation on the first argument. | 53 | Apply the secod argument as a permutation on the first argument. |
| 30 | 54 | ||
| 55 | Parameters: | ||
| 56 | cube - The first cube, in B32 format. | ||
| 57 | permutation - The second cube, in B32 format. This cube is treated as a | ||
| 58 | permutation and "applied" to the first cube. | ||
| 59 | result - The return parameter for the resulting cube, in B32 format. | ||
| 60 | |||
| 31 | Return values: | 61 | Return values: |
| 32 | 0 Valid result | 62 | NISSY_OK - The cubes were composed succesfully. |
| 33 | 1 The given cube is invalid | 63 | NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is |
| 34 | 2 The given permutation is invalid | 64 | either because at least on of the given cubes |
| 35 | 9 The resulting cube is not solvable | 65 | was not solvable, or due to an unknown internal |
| 66 | error. | ||
| 67 | NISSY_ERROR_INVALID_CUBE - At least one of the given cubes is invalid. | ||
| 68 | NISSY_ERROR_UNKNOWN - An unknown error occurred. | ||
| 36 | */ | 69 | */ |
| 37 | int64_t nissy_compose( | 70 | int64_t nissy_compose( |
| 38 | const char cube[static 22], | 71 | const char cube[static 22], |
| @@ -43,10 +76,17 @@ int64_t nissy_compose( | |||
| 43 | /* | 76 | /* |
| 44 | Compute the inverse of the given cube. | 77 | Compute the inverse of the given cube. |
| 45 | 78 | ||
| 79 | Parameters: | ||
| 80 | cube - The cube to be inverted, in B32 format. | ||
| 81 | result - The return parameter for the resulting cube, in B32 format. | ||
| 82 | |||
| 46 | Return values: | 83 | Return values: |
| 47 | 0 Valid result | 84 | NISSY_OK - The cube was inverted succesfully. |
| 48 | 1 The given cube is invalid | 85 | NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is |
| 49 | 9 The resulting cube is not solvable | 86 | either because the given cube was not solvable, |
| 87 | or due to an unknown internal error. | ||
| 88 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. | ||
| 89 | NISSY_ERROR_UNKNOWN - An unknown error occurred. | ||
| 50 | */ | 90 | */ |
| 51 | int64_t nissy_inverse( | 91 | int64_t nissy_inverse( |
| 52 | const char cube[static 22], | 92 | const char cube[static 22], |
| @@ -56,11 +96,18 @@ int64_t nissy_inverse( | |||
| 56 | /* | 96 | /* |
| 57 | Apply the given sequence of moves on the given cube. | 97 | Apply the given sequence of moves on the given cube. |
| 58 | 98 | ||
| 99 | Parameters: | ||
| 100 | cube - The cube to move, in B32 format. | ||
| 101 | moves - The moves to apply to the cube. | ||
| 102 | result - The return parameter for the resulting cube, in B32 format. | ||
| 103 | |||
| 59 | Return values: | 104 | Return values: |
| 60 | 0 Valid result | 105 | NISSY_OK - The moves were applied succesfully. |
| 61 | 1 The given cube is invalid | 106 | NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is |
| 62 | 8 The given moves are invalid | 107 | either because the given cube was not solvable, |
| 63 | 9 The resulting cube is not solvable | 108 | or due to an unknown internal error. |
| 109 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. | ||
| 110 | NISSY_ERROR_INVALID_MOVES - The given moves are invalid. | ||
| 64 | */ | 111 | */ |
| 65 | int64_t nissy_applymoves( | 112 | int64_t nissy_applymoves( |
| 66 | const char cube[static 22], | 113 | const char cube[static 22], |
| @@ -71,11 +118,17 @@ int64_t nissy_applymoves( | |||
| 71 | /* | 118 | /* |
| 72 | Apply the single given transformation to the given cube. | 119 | Apply the single given transformation to the given cube. |
| 73 | 120 | ||
| 121 | Parameters: | ||
| 122 | cube - The cube to be transformed, in B32 format. | ||
| 123 | transformation - The transformation in (rotation|mirrored) xy format. | ||
| 124 | result - The return parameter for the resulting cube, in B32 format. | ||
| 125 | |||
| 74 | Return values: | 126 | Return values: |
| 75 | 0 Valid result | 127 | NISSY_OK - The transformation was performed succesfully. |
| 76 | 1 The given cube is invalid | 128 | NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is |
| 77 | 8 The given transformation is invalid | 129 | probably due to an unknown internal error. |
| 78 | 9 The resulting cube is not solvable | 130 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. |
| 131 | NISSY_ERROR_INVALID_TRANS - The given transformation is invalid. | ||
| 79 | */ | 132 | */ |
| 80 | int64_t nissy_applytrans( | 133 | int64_t nissy_applytrans( |
| 81 | const char cube[static 22], | 134 | const char cube[static 22], |
| @@ -86,9 +139,15 @@ int64_t nissy_applytrans( | |||
| 86 | /* | 139 | /* |
| 87 | Apply the given moves to the solved cube. | 140 | Apply the given moves to the solved cube. |
| 88 | 141 | ||
| 142 | Parameters: | ||
| 143 | moves - The moves to be applied to the solved cube. | ||
| 144 | result - Return parameter for the resulting cube, in B32 format. | ||
| 145 | |||
| 89 | Return values: | 146 | Return values: |
| 90 | 0 Valid result | 147 | NISSY_OK - The moves were performed succesfully. |
| 91 | 1 The given moves are invalid | 148 | NISSY_WARNING_UNSOLVABLE - The resulting cube is not solvable. This is |
| 149 | probably due to an unknown internal error. | ||
| 150 | NISSY_ERROR_INVALID_MOVES - The given moves are invalid. | ||
| 92 | */ | 151 | */ |
| 93 | int64_t nissy_frommoves( | 152 | int64_t nissy_frommoves( |
| 94 | const char *moves, | 153 | const char *moves, |
| @@ -98,20 +157,45 @@ int64_t nissy_frommoves( | |||
| 98 | /* | 157 | /* |
| 99 | Convert the given cube between the two given formats. | 158 | Convert the given cube between the two given formats. |
| 100 | 159 | ||
| 160 | Parameters: | ||
| 161 | format_in - The input format. | ||
| 162 | format_out - The output format. | ||
| 163 | string - The cube, in format_in format. | ||
| 164 | result - Return parameter for the cube in format_out format. Must be | ||
| 165 | large enough to contains the cube in this format. | ||
| 166 | |||
| 101 | Return values: | 167 | Return values: |
| 102 | 0 Valid result | 168 | NISSY_OK - The conversion was performed succesfully. |
| 103 | 1 The given cube or format_in is invalid | 169 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. |
| 104 | 2 The resulting cube or format_out is invalid | 170 | NISSY_ERROR_INVALID_FORMAT - At least one of the given formats is invalid. |
| 105 | 3 The resulting cube is inconsistent | 171 | NISSY_ERROR_UNKNOWN - An unknown error occurred. |
| 106 | */ | 172 | */ |
| 107 | int64_t nissy_convert( | 173 | int64_t nissy_convert( |
| 108 | const char *format_in, | 174 | const char *format_in, |
| 109 | const char *format_out, | 175 | const char *format_out, |
| 110 | const char *cube_string, | 176 | const char *cube, |
| 111 | char *result | 177 | char *result |
| 112 | ); | 178 | ); |
| 113 | 179 | ||
| 114 | /* Get the cube with the given ep, eo, cp and co values. */ | 180 | /* |
| 181 | Get the cube with the given ep, eo, cp and co values. The values must be in the | ||
| 182 | ranges specified below, but if the option "fix" is given any values outside its | ||
| 183 | range will be adjusted before using it. The option "fix" also fixes parity and | ||
| 184 | orientation issues, resulting always in a solvable cube. | ||
| 185 | |||
| 186 | Parameters: | ||
| 187 | ep - The edge permutation, 0 <= ep <= 479001600 (12!) | ||
| 188 | eo - The edge orientation, 0 <= eo <= 2047 (2^11) | ||
| 189 | cp - The corner permutation, 0 <= cp <= 40320 (8!) | ||
| 190 | co - The corner orientation, 0 <= co <= 2187 (3^7) | ||
| 191 | options - Other options. | ||
| 192 | result - The return parameter for the resulting cube, in B32 format. | ||
| 193 | |||
| 194 | Return values: | ||
| 195 | NISSY_OK - The cube was generated succesfully. | ||
| 196 | NISSY_WARNING_UNSOLVABLE - The resulting cube is unsolvable. | ||
| 197 | NISSY_ERROR_OPTIONS - One or more of the given parameters is invalid. | ||
| 198 | */ | ||
| 115 | int64_t nissy_getcube( | 199 | int64_t nissy_getcube( |
| 116 | int64_t ep, | 200 | int64_t ep, |
| 117 | int64_t eo, | 201 | int64_t eo, |
| @@ -125,9 +209,13 @@ int64_t nissy_getcube( | |||
| 125 | Compute the size of the data generated by nissy_gendata, when called with | 209 | Compute the size of the data generated by nissy_gendata, when called with |
| 126 | the same parameters, or -1 in case of error. | 210 | the same parameters, or -1 in case of error. |
| 127 | 211 | ||
| 212 | Parameters: | ||
| 213 | solver - The name of the solver. | ||
| 214 | |||
| 128 | Return values: | 215 | Return values: |
| 129 | -1 Error | 216 | NISSY_ERROR_INVALID_SOLVER - The given solver is not known. |
| 130 | >=0 The size of the table, in bytes | 217 | NISSY_ERROR_UNKNOWN - An unknown error occurred. |
| 218 | Any value >= 0 - The size of the data, in bytes. | ||
| 131 | */ | 219 | */ |
| 132 | int64_t nissy_datasize( | 220 | int64_t nissy_datasize( |
| 133 | const char *solver | 221 | const char *solver |
| @@ -136,38 +224,71 @@ int64_t nissy_datasize( | |||
| 136 | /* | 224 | /* |
| 137 | Compute the data for the given solver and store it in generated_data. | 225 | Compute the data for the given solver and store it in generated_data. |
| 138 | 226 | ||
| 227 | Parameters: | ||
| 228 | solver - The name of the solver. | ||
| 229 | data - The return parameter for the generated data. Must be large enoguh | ||
| 230 | to contain the whole data. It is advised to use nissy_datasize to | ||
| 231 | check how much memory is needed. | ||
| 232 | |||
| 139 | Return values: | 233 | Return values: |
| 140 | -1 Error | 234 | NISSY_ERROR_INVALID_SOLVER - The given solver is not known. |
| 141 | >=0 The size of the table, in bytes | 235 | NISSY_ERROR_UNKNOWN - An error occurred while generating the data. |
| 236 | Any value >= 0 - The size of the data, in bytes. | ||
| 142 | */ | 237 | */ |
| 143 | int64_t nissy_gendata( | 238 | int64_t nissy_gendata( |
| 144 | const char *solver, | 239 | const char *solver, |
| 145 | void *generated_data | 240 | void *data |
| 146 | ); | 241 | ); |
| 147 | 242 | ||
| 148 | /* | 243 | /* |
| 149 | Print information on a data table via the provided callback writer. | 244 | Print information on a data table via the provided callback writer. |
| 150 | 245 | ||
| 246 | Parameters: | ||
| 247 | data - The data | ||
| 248 | write - A callback writer with the same signature as printf(3). | ||
| 249 | |||
| 151 | Return values: | 250 | Return values: |
| 152 | 0 No error | 251 | NISSY_OK - The data is correct. |
| 153 | 1 The given data could not be read correctly | 252 | NISSY_ERROR_DATA - The data contains errors. |
| 154 | */ | 253 | */ |
| 155 | int64_t nissy_datainfo( | 254 | int64_t nissy_datainfo( |
| 156 | const void *table, | 255 | const void *data, |
| 157 | void (*write)(const char *, ...) | 256 | void (*write)(const char *, ...) |
| 158 | ); | 257 | ); |
| 159 | 258 | ||
| 160 | /* | 259 | /* |
| 161 | Solve the given cube using the given solver and options | 260 | Solve the given cube using the given solver and options. |
| 261 | |||
| 262 | Parameters: | ||
| 263 | cube - The cube to solver, in B32 format. | ||
| 264 | solver - The name of the solver. | ||
| 265 | nissflag - The flags for NISS (linear, inverse, mixed, or combinations). | ||
| 266 | minmoves - The minimum number of moves for a solution. | ||
| 267 | maxmoves - The maximum number of moves for a solution. | ||
| 268 | maxsolutions - The maximum number of solutions. | ||
| 269 | optimal - If set to a non-negative value, the maximum number of moves | ||
| 270 | above the optimal solution length. | ||
| 271 | data - The data for the solver. Can be computed with gendata. | ||
| 272 | solutions - The return parameter for the solutions. Must be large enough | ||
| 273 | to store all found solutions. The solutions are separated by | ||
| 274 | a '\n' (newline) and a '\0' (NULL character) terminates the | ||
| 275 | list. | ||
| 276 | TODO: replace with callback writer. | ||
| 162 | 277 | ||
| 163 | Return values: | 278 | Return values: |
| 164 | -1 Error | 279 | NISSY_OK - Cube solved succesfully. |
| 165 | >=0 The number of solutions found | 280 | NISSY_ERROR_INVALID_CUBE - The given cube is invalid. |
| 281 | NISSY_ERROR_UNSOLVABLE_CUBE - The given cube is valid, but not solvable with | ||
| 282 | the given solver. | ||
| 283 | 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. | ||
| 286 | Any value >= 0 - The number of solutions found. | ||
| 166 | */ | 287 | */ |
| 167 | int64_t nissy_solve( | 288 | int64_t nissy_solve( |
| 168 | const char cube[static 22], | 289 | const char cube[static 22], |
| 169 | const char *solver, | 290 | const char *solver, |
| 170 | const char *nisstype, /* TODO: remove, use flags */ | 291 | uint8_t nissflag, |
| 171 | int8_t minmoves, | 292 | int8_t minmoves, |
| 172 | int8_t maxmoves, | 293 | int8_t maxmoves, |
| 173 | int64_t maxsolutions, | 294 | int64_t maxsolutions, |
| @@ -178,5 +299,33 @@ int64_t nissy_solve( | |||
| 178 | 299 | ||
| 179 | /* | 300 | /* |
| 180 | Set a global logger function used by this library. | 301 | Set a global logger function used by this library. |
| 302 | |||
| 303 | Parameters: | ||
| 304 | write - A callback writer with the same signature as printf(3). | ||
| 305 | |||
| 306 | Return values: | ||
| 307 | NISSY_OK - Logger set succesfully. | ||
| 308 | NISSY_WARNING_NULL_CALLBACK - The provided callback writer is NULL. | ||
| 181 | */ | 309 | */ |
| 182 | void nissy_setlogger(void (*logger_function)(const char *, ...)); | 310 | int64_t nissy_setlogger( |
| 311 | void (*logger_function)(const char *, ...) | ||
| 312 | ); | ||
| 313 | |||
| 314 | /* | ||
| 315 | Print an explanation of the given error code via the provided callback writer. | ||
| 316 | |||
| 317 | Parameters: | ||
| 318 | error_code - The error code to be explained. It can be any value returned | ||
| 319 | by a function in this library, not necessarily an error. | ||
| 320 | write - A callback writer with the same signature as printf(3). | ||
| 321 | Must be non-NULL. | ||
| 322 | |||
| 323 | Return values: | ||
| 324 | NISSY_OK - The error code is known. | ||
| 325 | NISSY_WARNING_NULL_CALLBACK - The provided callback writer is NULL. | ||
| 326 | NISSY_ERROR_INVALID_CODE - The error code is unknown. | ||
| 327 | */ | ||
| 328 | int64_t nissy_explainerror( | ||
| 329 | int64_t error_code, | ||
| 330 | void (*write)(const char *, ...) | ||
| 331 | ); | ||
