diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-11-10 14:44:48 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-11-10 14:44:48 +0100 |
| commit | fb9ae9e41eaf01b3651395fdd450ac1a4743e592 (patch) | |
| tree | 8fb29e9b62d40d6407f082f55a7164775154bf78 /cube.h | |
| parent | 04c3ee1f5acac47650be8d0ffbf90e238df0d12b (diff) | |
| download | nissy-core-fb9ae9e41eaf01b3651395fdd450ac1a4743e592.tar.gz nissy-core-fb9ae9e41eaf01b3651395fdd450ac1a4743e592.zip | |
Big changes to the interface
Diffstat (limited to 'cube.h')
| -rw-r--r-- | cube.h | 161 |
1 files changed, 52 insertions, 109 deletions
| @@ -17,6 +17,7 @@ corners is with respect to U/D. | |||
| 17 | The permutation of the center pieces is not stored. This means that the | 17 | The permutation of the center pieces is not stored. This means that the |
| 18 | cube is assumed to be in a fixed orientation. | 18 | cube is assumed to be in a fixed orientation. |
| 19 | 19 | ||
| 20 | TODO: define EO and CO better, explain how to use them | ||
| 20 | TODO: encode centers? | 21 | TODO: encode centers? |
| 21 | 22 | ||
| 22 | The exact cube type structure depends on your system's configuration. If | 23 | The exact cube type structure depends on your system's configuration. If |
| @@ -24,22 +25,22 @@ you operate on the cube only via the functions provided below, you don't | |||
| 24 | need to worry about this. | 25 | need to worry about this. |
| 25 | ******************************************************************************/ | 26 | ******************************************************************************/ |
| 26 | 27 | ||
| 27 | #ifdef CUBE_AVX2 | ||
| 28 | typedef __m256i cube_t; | ||
| 29 | #else | ||
| 30 | typedef struct { | 28 | typedef struct { |
| 31 | uint8_t c[8]; /* Corners */ | 29 | uint8_t corner[8]; |
| 32 | uint8_t e[12]; /* Edges */ | 30 | uint8_t edge[12]; |
| 33 | } cube_t; | 31 | } cube_t; |
| 34 | #endif | ||
| 35 | 32 | ||
| 36 | /* Returns a copy of the solved cube */ | 33 | /* Returns a copy of the solved cube */ |
| 37 | cube_t solvedcube(void); | 34 | cube_t solvedcube(void); |
| 38 | 35 | ||
| 39 | /* Basic checks on the cube */ | 36 | /* Basic checks on the cube */ |
| 37 | bool isconsistent(cube_t); | ||
| 40 | bool issolvable(cube_t); | 38 | bool issolvable(cube_t); |
| 41 | bool equal(cube_t, cube_t); | ||
| 42 | bool issolved(cube_t); | 39 | bool issolved(cube_t); |
| 40 | bool equal(cube_t, cube_t); | ||
| 41 | |||
| 42 | /* All functions can return an error value, use iserror() to check this */ | ||
| 43 | bool iserror(cube_t); | ||
| 43 | 44 | ||
| 44 | /* Apply the second cube on the first as a move sequence */ | 45 | /* Apply the second cube on the first as a move sequence */ |
| 45 | cube_t compose(cube_t, cube_t); | 46 | cube_t compose(cube_t, cube_t); |
| @@ -47,134 +48,75 @@ cube_t compose(cube_t, cube_t); | |||
| 47 | /* Invert the cube */ | 48 | /* Invert the cube */ |
| 48 | cube_t inverse(cube_t); | 49 | cube_t inverse(cube_t); |
| 49 | 50 | ||
| 50 | /* All functions can return an error value, use iserror() to check this */ | 51 | /* Check if a cube represent a valid state (possibly unsolvable) */ |
| 51 | bool iserror(cube_t); | ||
| 52 | |||
| 53 | /****************************************************************************** | ||
| 54 | Moves and transformations | ||
| 55 | |||
| 56 | Moves and transformations are represented each as an (unsigned) 8 bit integer. | ||
| 57 | 52 | ||
| 58 | Moves are numbered as follows: | 53 | /* TODO comment on these and the format for moves and trans */ |
| 59 | U=0 U2=1 U'=2 D=3 D2=4 D'=5 | 54 | /* For trans, only one trans is supported */ |
| 60 | R=6 R2=7 R'=8 L=9 L2=10 L'=11 | 55 | cube_t applymoves(cube_t, char *); |
| 61 | F=12 F2=13 F'=14 B=15 B2=16 B'=17 | 56 | cube_t applytrans(cube_t, char *); |
| 62 | 57 | ||
| 63 | TODO: NISS | 58 | /****************************************************************************** |
| 59 | Read / write utilities | ||
| 64 | 60 | ||
| 65 | TODO: Extend the moveset? | 61 | Reading and writing is not done directly via stdin / stdout, but via an |
| 62 | array of char (called buf in the prototypes below). | ||
| 66 | 63 | ||
| 67 | Transformations can be either simple rotations or a rotation composed | 64 | Multiple representations of the cube as text are supported: |
| 68 | with a mirroring. A composed rotation + mirror is obtained by applying | ||
| 69 | the corresponding rotation to the solved cube mirrored along the M plane. | ||
| 70 | 65 | ||
| 71 | For example, to apply the transformation RBm (mirrored RB) to a cube C: | 66 | - H48: a human-readable format. |
| 72 | 1. Apply a mirror along the M plane to the solved cube | 67 | Each edge is represented by two letters denoting the sides it |
| 73 | 2. Rotate the mirrored cube with z' y2 | 68 | belongs to and one number denoting its orientation (0 oriented, 1 |
| 74 | 3. Apply the cube C to the transformed solved cube | 69 | mis-oriented). Similarly, each corner is represented by three letters and |
| 75 | 4. Apply the transformations of step 1a and 1b in reverse | 70 | a number (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). |
| 76 | 71 | ||
| 77 | See cube.c for a full list of transformations. | 72 | The solved cube looks like this: |
| 78 | ******************************************************************************/ | ||
| 79 | 73 | ||
| 80 | typedef uint8_t move_t; | 74 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 |
| 81 | typedef uint8_t trans_t; | 75 | UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 |
| 82 | 76 | ||
| 83 | /* Apply a move or a transformation on the cube */ | 77 | The cube after the moves R'U'F looks like this: |
| 84 | cube_t move(cube_t, move_t); | ||
| 85 | cube_t transform(cube_t, trans_t); | ||
| 86 | 78 | ||
| 87 | /****************************************************************************** | 79 | FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 |
| 88 | Read / write utilities | 80 | UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 |
| 89 | 81 | ||
| 90 | Reading and writing is not done directly via stdin / stdout, but via an | 82 | Whitespace (including newlines) between pieces is ignored when reading the |
| 91 | array of char (called buf in the prototypes below). | 83 | cube. A single whitespace character is added between pieces when writing. |
| 92 | 84 | ||
| 93 | Multiple representations of the cube as text are supported, although | 85 | - SRC: format used to generate code for internal use. |
| 94 | not all of them are supported for both reading and writing. See below | 86 | In cube.c, a type called cube_array_t is defined and used for basic, |
| 95 | for details. More formats may be supported in the future. | 87 | non-performance-critical methods. If OUT is the output in SRC format, |
| 88 | the following line can be used to declare a new cube object: | ||
| 96 | 89 | ||
| 97 | Moves are read using the standard notation. Each move (U, D, R, L, F, | 90 | cube_array_t cube = OUT |
| 98 | B) can be followed by a modifier (1, 2, 3, '). Whitespace (spaces, tabs, | 91 | |
| 99 | newlines) are ignored. Parantheses and other notation is not supported. | 92 | - AVX: analogue to SRC, but for the AVX2 internal representation of the cube. |
| 100 | TODO: parantheses for NISS | ||
| 101 | 93 | ||
| 102 | For how transformations are read or written, see cube.c. | 94 | Not all formats are supported for both reading and writing. More formats |
| 95 | may be supported in the future. | ||
| 103 | ******************************************************************************/ | 96 | ******************************************************************************/ |
| 104 | 97 | ||
| 105 | /* The different formats for reading or writing the cube */ | ||
| 106 | typedef enum { | ||
| 107 | H48, /* H48 is a human-readable format. | ||
| 108 | * | ||
| 109 | * Each edge is represented by two letters denoting the sides it | ||
| 110 | * belongs to and one number denoting its orientation (0 oriented, | ||
| 111 | * 1 mis-oriented). Similarly, each corner is represented by three | ||
| 112 | * letters and a number (0 oriented, 1 twisted clockwise, 2 | ||
| 113 | * twisted counter-clockwise). | ||
| 114 | * | ||
| 115 | * The solved cube looks like this: | ||
| 116 | * | ||
| 117 | * UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 | ||
| 118 | * UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
| 119 | * | ||
| 120 | * The cube after the moves R'U'F looks like this: | ||
| 121 | * | ||
| 122 | * FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 | ||
| 123 | * UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 | ||
| 124 | * | ||
| 125 | * Whitespace (including newlines) between pieces is ignored when | ||
| 126 | * reading the cube. A single whitespace character is added | ||
| 127 | * between pieces when writing. | ||
| 128 | */ | ||
| 129 | SRC, /* The SRC format can be used to generate code for internal use. | ||
| 130 | * | ||
| 131 | * In cube.c, a type called cube_array_t is defined and used for | ||
| 132 | * basic, non-performance-critical methods. If OUT is the output | ||
| 133 | * in SRC format, the following line can be used to declare a new | ||
| 134 | * cube object: | ||
| 135 | * | ||
| 136 | * cube_array_t cube = OUT | ||
| 137 | */ | ||
| 138 | AVX, /* The AVX format is analogous to SRC, but for the AVX2 internal | ||
| 139 | * representation of the cube. | ||
| 140 | */ | ||
| 141 | } format_t; | ||
| 142 | |||
| 143 | /* Reads a cube from buf in the specified format, and return it. | 98 | /* Reads a cube from buf in the specified format, and return it. |
| 144 | * Supported formats: H48. | 99 | * Supported formats: "H48". |
| 145 | */ | 100 | */ |
| 146 | cube_t readcube(format_t format, char *buf); | 101 | cube_t readcube(char *format, char *buf); |
| 147 | 102 | ||
| 148 | /* Write the given cube to buf in the specified format. | 103 | /* Write the given cube to buf in the specified format. |
| 149 | * Supported formats: H48, SRC, AVX. | 104 | * Supported formats: "H48", "SRC", "AVX". |
| 150 | */ | 105 | */ |
| 151 | void writecube(format_t format, cube_t cube, char *buf); | 106 | void writecube(char *format, cube_t cube, char *buf); |
| 152 | |||
| 153 | /* Utilities for reading and writing moves */ | ||
| 154 | int readmoves(char *buf, move_t *moves); | ||
| 155 | void writemoves(move_t *moves, int n, char *buf); | ||
| 156 | trans_t readtrans(char *buf); | ||
| 157 | void writetrans(trans_t trans, char *buf); | ||
| 158 | 107 | ||
| 159 | /****************************************************************************** | 108 | /****************************************************************************** |
| 160 | Coordinates | 109 | Coordinates |
| 161 | 110 | ||
| 162 | The coordinate functions compute one aspect of the cube (for example, | 111 | TODO description |
| 163 | the edge orientation) and they return it as an integer. They are used | ||
| 164 | for example to build pruning tables for various solving methods. | ||
| 165 | ******************************************************************************/ | 112 | ******************************************************************************/ |
| 166 | 113 | ||
| 167 | int16_t coord_eo(cube_t); /* Edge orientation */ | 114 | int64_t coord_eo(cube_t); |
| 168 | 115 | ||
| 169 | /****************************************************************************** | 116 | /****************************************************************************** |
| 170 | Solvers | 117 | Solvers |
| 171 | 118 | ||
| 172 | All solvers work at fixed depth, i.e. they will only find solutions of the | 119 | The solutions are returned as a newline-separated list of characters. |
| 173 | specified length. Iterating over the possible lengths, if desired, is left as | ||
| 174 | an implementation detail for the user of this library. | ||
| 175 | |||
| 176 | The solutions are returned as a list of moves, which can then be converted to | ||
| 177 | a string using writemoves(). | ||
| 178 | 120 | ||
| 179 | Unless specified otherwise, all the solutions are not trivially simplifiable. | 121 | Unless specified otherwise, all the solutions are not trivially simplifiable. |
| 180 | This means that sequences like U U2 or R L R will not appear in any solution. | 122 | This means that sequences like U U2 or R L R will not appear in any solution. |
| @@ -188,6 +130,7 @@ TODO NISS / INVERSE / LINEAR as a mask? | |||
| 188 | 130 | ||
| 189 | All solvers take at least the following parameters, satisfying the conditions | 131 | All solvers take at least the following parameters, satisfying the conditions |
| 190 | in square brackets: | 132 | in square brackets: |
| 133 | TODO more! | ||
| 191 | - cube_t cube [issolvable(cube)]: The cube to solve. | 134 | - cube_t cube [issolvable(cube)]: The cube to solve. |
| 192 | - uint8_t depth [depth <= 20]: The lenght of the solution. | 135 | - uint8_t depth [depth <= 20]: The lenght of the solution. |
| 193 | - int maxsols: The maximum number of solutions to find. The solver | 136 | - int maxsols: The maximum number of solutions to find. The solver |
| @@ -201,12 +144,12 @@ in square brackets: | |||
| 201 | Some solvers take other parameters. See below for details. | 144 | Some solvers take other parameters. See below for details. |
| 202 | ******************************************************************************/ | 145 | ******************************************************************************/ |
| 203 | 146 | ||
| 147 | /* TODO | ||
| 204 | int solve_generic( | 148 | int solve_generic( |
| 205 | cube_t cube, | 149 | cube_t cube, |
| 206 | uint8_t depth, | 150 | uint8_t depth, |
| 207 | int maxsols, | 151 | int maxsols, |
| 208 | move_t *ret | 152 | uint8_t *ret, // TODO change to char |
| 209 | int (*estimate)(cube_t), | 153 | int (*estimate)(cube_t) |
| 210 | ); | 154 | ); |
| 211 | 155 | */ | |
| 212 | int solve_light(cube_t, int | ||
