nissy-nx

A Rubik's cube optimal solver
git clone https://git.tronto.net/nissy-nx
Download | Log | Files | Refs | README | LICENSE

cubetypes.h (8452B)


      1 #ifndef CUBETYPES_H
      2 #define CUBETYPES_H
      3 
      4 #include <stdbool.h>
      5 #include <inttypes.h>
      6 #include <pthread.h>
      7 
      8 #define NMOVES               55 /* Actually 54, but one is NULLMOVE */
      9 #define NTRANS               48
     10 #define NROTATIONS           24
     11 #define entry_group_t        uint8_t /* For pruning tables */
     12 
     13 #define MAX_N_COORD          6
     14 
     15 /* Enums *********************************************************************/
     16 
     17 typedef enum
     18 center
     19 {
     20 	U_center, D_center,
     21 	R_center, L_center,
     22 	F_center, B_center
     23 } Center;
     24 
     25 typedef enum
     26 corner
     27 {
     28 	UFR, UFL, UBL, UBR,
     29 	DFR, DFL, DBL, DBR
     30 } Corner;
     31 
     32 typedef enum
     33 coordtype
     34 {
     35 	COMP_COORD, SYM_COORD, SYMCOMP_COORD
     36 } CoordType;
     37 
     38 typedef enum
     39 edge
     40 {
     41 	UF, UL, UB, UR,
     42 	DF, DL, DB, DR,
     43 	FR, FL, BL, BR
     44 } Edge;
     45 
     46 typedef enum
     47 move
     48 {
     49 	NULLMOVE,
     50 	U, U2, U3, D, D2, D3,
     51 	R, R2, R3, L, L2, L3,
     52 	F, F2, F3, B, B2, B3,
     53 	Uw, Uw2, Uw3, Dw, Dw2, Dw3,
     54 	Rw, Rw2, Rw3, Lw, Lw2, Lw3,
     55 	Fw, Fw2, Fw3, Bw, Bw2, Bw3,
     56 	M, M2, M3,
     57 	S, S2, S3,
     58 	E, E2, E3,
     59 	x, x2, x3,
     60 	y, y2, y3,
     61 	z, z2, z3,
     62 } Move;
     63 
     64 typedef enum
     65 trans
     66 {
     67 	uf, ur, ub, ul,
     68 	df, dr, db, dl,
     69 	rf, rd, rb, ru,
     70 	lf, ld, lb, lu,
     71 	fu, fr, fd, fl,
     72 	bu, br, bd, bl,
     73 	uf_mirror, ur_mirror, ub_mirror, ul_mirror,
     74 	df_mirror, dr_mirror, db_mirror, dl_mirror,
     75 	rf_mirror, rd_mirror, rb_mirror, ru_mirror,
     76 	lf_mirror, ld_mirror, lb_mirror, lu_mirror,
     77 	fu_mirror, fr_mirror, fd_mirror, fl_mirror,
     78 	bu_mirror, br_mirror, bd_mirror, bl_mirror,
     79 } Trans;
     80 
     81 
     82 /* Typedefs ******************************************************************/
     83 
     84 typedef struct alg                Alg;
     85 typedef struct alglist            AlgList;
     86 typedef struct alglistnode        AlgListNode;
     87 typedef struct choicestep         ChoiceStep;
     88 typedef struct command            Command;
     89 typedef struct commandargs        CommandArgs;
     90 typedef struct coordinate         Coordinate;
     91 typedef struct cube               Cube;
     92 /*typedef struct dfsarg             DfsArg;*/
     93 typedef struct fstcube            FstCube;
     94 typedef struct indexer            Indexer;
     95 typedef struct movable            Movable;
     96 typedef struct moveset            Moveset;
     97 typedef struct prunedata          PruneData;
     98 typedef struct solveoptions       SolveOptions;
     99 typedef struct step               Step;
    100 typedef struct symdata            SymData;
    101 typedef struct threaddatasolve    ThreadDataSolve;
    102 typedef struct threaddatagenpt    ThreadDataGenpt;
    103 typedef struct transgroup         TransGroup;
    104 
    105 typedef bool                 (*Checker)          (Cube *);
    106 typedef bool                 (*CubeTester)       (Cube *, Alg *);
    107 /*typedef bool                 (*DfsMover)         (DfsArg *);*/
    108 typedef void                 (*DfsExtraCopier)   (void *, void *);
    109 typedef Alg *                (*Validator)        (Alg *);
    110 typedef void                 (*Exec)             (CommandArgs *);
    111 typedef CommandArgs *        (*ArgParser)        (int, char **);
    112 typedef bool                 (*Tester)           (void);
    113 typedef int                  (*TransFinder)      (uint64_t, Trans *);
    114 
    115 
    116 /* Structs *******************************************************************/
    117 
    118 struct
    119 alg
    120 {
    121 	Move *                    move;
    122 	bool *                    inv;
    123 	int                       len;
    124 	int                       allocated;
    125 	Move *                    move_normal;
    126 	int                       len_normal;
    127 	Move *                    move_inverse;
    128 	int                       len_inverse;
    129 };
    130 
    131 struct
    132 alglist
    133 {
    134 	AlgListNode *             first;
    135 	AlgListNode *             last;
    136 	int                       len;
    137 };
    138 
    139 struct
    140 alglistnode
    141 {
    142 	Alg *                     alg;
    143 	AlgListNode *             next;
    144 };
    145 
    146 struct
    147 choicestep
    148 {
    149 	char *                    shortname;
    150 	char *                    name;
    151 	Step *                    step[99];
    152 	Trans                     t[99];
    153 	char *                    ready_msg;
    154 };
    155 
    156 struct
    157 command
    158 {
    159 	char *                    name;
    160 	char *                    usage;
    161 	char *                    description;
    162 	ArgParser                 parse_args;
    163 	Exec                      exec;
    164 };
    165 
    166 struct
    167 commandargs
    168 {
    169 	bool                      success;
    170 	Alg *                     scramble;
    171 	SolveOptions *            opts;
    172 	ChoiceStep *              cs;
    173 	Command *                 command; /* For help */
    174 	int                       n;
    175 	char                      scrtype[20];
    176 	bool                      scrstdin;
    177 	bool                      header;
    178 };
    179 
    180 struct
    181 coordinate
    182 {
    183 	char *                    name;
    184 	CoordType                 type;
    185 	bool                      generated;
    186 	Indexer *                 i[99];
    187 	uint64_t                  max;
    188 	uint64_t *                mtable[NMOVES];
    189 	uint64_t *                ttable[NTRANS];
    190 	TransGroup *              tgrp;
    191 	Coordinate *              base[2];
    192 	uint64_t *                symclass;
    193 	uint64_t *                symrep;
    194 	Trans *                   transtorep;
    195 	Trans *                   ttrep_move[NMOVES];
    196 	uint64_t *                selfsim;
    197 };
    198 
    199 struct
    200 cube
    201 {
    202 	int                       ep[12];
    203 	int                       eo[12];
    204 	int                       cp[8];
    205 	int                       co[8];
    206 	int                       xp[6];
    207 };
    208 
    209 /*
    210 struct
    211 movable
    212 {
    213 	uint64_t                  val;
    214 	Trans                     t;
    215 };
    216 */
    217 
    218 /*
    219 struct
    220 dfsarg
    221 {
    222 	Cube *                    cube;
    223 	Movable                   ind[MAX_N_COORD];
    224 	Trans                     t;
    225 	Step *                    s;
    226 	SolveOptions *            opts;
    227 	int                       d;
    228 	int                       bound;
    229 	bool                      niss;
    230 	AlgList *                 sols;
    231 	pthread_mutex_t *         sols_mutex;
    232 	Alg *                     current_alg;
    233 	void *                    extra;
    234 };
    235 */
    236 
    237 /*
    238 struct
    239 dfsarg
    240 {
    241 	void *                    cube_data;
    242 	SolveOptions *            opts;
    243 	int                       d;
    244 	int                       bound;
    245 	bool                      niss;
    246 	AlgList *                 sols;
    247 	Alg *                     current_alg;
    248 	Solver *                  solver;
    249 	Threader *                threader;
    250 };
    251 */
    252 
    253 struct
    254 fstcube
    255 {
    256 	uint16_t                  uf_eofb;
    257 	uint16_t                  uf_eposepe;
    258 	uint16_t                  uf_coud;
    259 	uint16_t                  uf_cp;
    260 	uint16_t                  fr_eofb;
    261 	uint16_t                  fr_eposepe;
    262 	uint16_t                  fr_coud;
    263 	uint16_t                  rd_eofb;
    264 	uint16_t                  rd_eposepe;
    265 	uint16_t                  rd_coud;
    266 };
    267 
    268 struct
    269 indexer
    270 {
    271 	int                       n;
    272 	uint64_t                  (*index)(Cube *);
    273 	void                      (*to_cube)(uint64_t, Cube *);
    274 };
    275 
    276 struct
    277 moveset
    278 {
    279 	char *                    name;
    280 	bool                      (*allowed)(Move);
    281 	bool                      (*can_append)(Alg *, Move, bool);
    282 	bool                      (*cancel_niss)(Alg *);
    283 	Move                      sorted_moves[NMOVES+1];
    284 };
    285 
    286 struct
    287 prunedata
    288 {
    289 	entry_group_t *           ptable;
    290 	uint64_t                  n;
    291 	Coordinate *              coord;
    292 	Moveset *                 moveset;
    293 	uint64_t                  count[16];
    294 	bool                      compact;
    295 	int                       base;
    296 };
    297 
    298 struct
    299 solveoptions
    300 {
    301 	int                       min_moves;
    302 	int                       max_moves;
    303 	int                       max_solutions;
    304 	int                       nthreads;
    305 	int                       optimal;
    306 	bool                      can_niss;
    307 	bool                      verbose;
    308 	bool                      all;
    309 	bool                      print_number;
    310 	bool                      count_only;
    311 };
    312 
    313 struct
    314 step
    315 {
    316 	Checker                   ready;
    317 	bool                      final;
    318 	Moveset *                 moveset;
    319 	int                       n_coord;
    320 	Coordinate *              coord[MAX_N_COORD];
    321 	Trans                     coord_trans[MAX_N_COORD];
    322 	PruneData *               pd[MAX_N_COORD];
    323 	bool                      pd_compact[MAX_N_COORD];
    324 	Validator                 is_valid;
    325 	/*DfsMover                  custom_move_checkstop;*/
    326 	DfsExtraCopier            copy_extra;
    327 };
    328 
    329 /*
    330 struct
    331 threaddatasolve
    332 {
    333 	DfsArg                    arg;
    334 	int                       thid;
    335 	AlgList *                 start;
    336 	AlgListNode **            node;
    337 	pthread_mutex_t *         start_mutex;
    338 };
    339 */
    340 
    341 struct
    342 threaddatagenpt
    343 {
    344 	int                       thid;
    345 	int                       nthreads;
    346 	PruneData *               pd;
    347 	int                       d;
    348 	int                       nchunks;
    349 	pthread_mutex_t **        mutex;
    350 	pthread_mutex_t *         upmutex;
    351 };
    352 
    353 struct
    354 transgroup
    355 {
    356 	int                       n;
    357 	Trans                     t[NTRANS];
    358 };
    359 
    360 #endif