aboutsummaryrefslogtreecommitdiff
path: root/src/solvers/coord/common.h
blob: 95c0e6c738b60ee408a5f8872e4232446ad8cbcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
STATIC uint64_t coord_coord_generic(
    const coord_t [static 1], cube_t, const void *);
STATIC cube_t coord_cube_generic(
    const coord_t [static 1], uint64_t, const void *);
STATIC bool coord_isnasty_generic(
    const coord_t [static 1], uint64_t, const void *);
STATIC size_t coord_gendata_generic(const coord_t [static 1], void *);

STATIC void append_coord_name(const coord_t [static 1], char *);
STATIC bool solution_lastqt_cw(const solution_moves_t [static 1]);
STATIC bool coord_can_switch(
    const coord_t [static 1], const void *, size_t n, const uint8_t [n]);

STATIC uint64_t
coord_coord_generic(const coord_t coord[static 1], cube_t c, const void *data)
{
	const char *datanoinfo;
	const uint32_t *data32;
	uint32_t d;
	cube_t tr;

	datanoinfo = (const char *)data + INFOSIZE;
	data32 = (const uint32_t *)datanoinfo;
	d = data32[coord->sym.coord(c)];
	tr = transform(c, COORD_TTREP(d));

	return COORD_CLASS(d) * coord->sym.max2 + coord->sym.coord2(tr);
}

STATIC cube_t
coord_cube_generic(const coord_t coord[static 1], uint64_t i, const void *data)
{
	const char *datanoinfo;
	const uint32_t *rep32;
	cube_t c;

	datanoinfo = (const char *)data + INFOSIZE;
	rep32 = (const uint32_t *)(datanoinfo + 4 * (size_t)coord->sym.max);
	c = coord->sym.cube(rep32[i / coord->sym.max2]);

	return coord->sym.merge(c, coord->sym.cube2(i % coord->sym.max2));
}

STATIC bool
coord_isnasty_generic(
	const coord_t coord[static 1],
	uint64_t i,
	const void *data
)
{
	const char *datanoinfo;
	const uint32_t *classttrep, *rep32;
	uint32_t r;

	datanoinfo = (const char *)data + INFOSIZE;
	classttrep = (const uint32_t *)datanoinfo;
	rep32 = (const uint32_t *)(datanoinfo + 4 * (size_t)coord->sym.max);
	r = rep32[i / coord->sym.max2];

	return COORD_ISNASTY(classttrep[r]);
}

STATIC size_t
coord_gendata_generic(
	const coord_t coord[static 1],
	void *data
)
{
	uint64_t i, j, n, t, nasty;
	char *datanoinfo;
	uint32_t *classttrep, *rep;
	size_t coord_datasize;
	cube_t c;
	tableinfo_t info;

	coord_datasize = INFOSIZE + 4*(coord->sym.classes + coord->sym.max);

	if (data == NULL)
		return coord_datasize;

	datanoinfo = (char *)data + INFOSIZE;
	classttrep = (uint32_t *)datanoinfo;
	rep = classttrep + coord->sym.max;
	memset(data, 0xFF, coord_datasize);

	info = (tableinfo_t) {
		.solver = "coord data for ",
		.type = TABLETYPE_SPECIAL,
		.infosize = INFOSIZE,
		.fullsize = coord_datasize,
		.hash = 0,
		.entries = coord->sym.classes + coord->sym.max,
		.classes = coord->sym.classes,
		.bits = 32,
		.base = 0,
		.maxvalue = 0,
		.next = 0
	};
	append_coord_name(coord, info.solver);

	for (i = 0, n = 0; i < coord->sym.max; i++) {
		if (classttrep[i] != 0xFFFFFFFF)
			continue;

		c = coord->sym.cube(i);
		for (t = 1, nasty = 0; t < NTRANS && !nasty; t++) {
			if (!((UINT64_C(1) << t) & coord->trans_mask))
				continue;

			nasty = i == coord->sym.coord(transform(c, t));
		}

		for (t = 0; t < NTRANS; t++) {
			if (!((UINT64_C(1) << t) & coord->trans_mask))
				continue;

			j = coord->sym.coord(transform(c, t));
			classttrep[j] =
			    (n << COORD_CLASS_SHIFT) |
			    (nasty << COORD_ISNASTY_SHIFT) |
			    (inverse_trans(t) << COORD_TTREP_SHIFT);
		}
		rep[n++] = i;
	}

	writetableinfo(&info, coord_datasize, data);

	DBG_ASSERT(n == coord->sym.classes, 0,
	    "%s coordinate data: computed %" PRIu64 " classes, "
	    "expected %" PRIu64 "\n", coord->name, n, coord->sym.classes);

	return coord_datasize;
}

STATIC void
append_coord_name(const coord_t coord[static 1], char *str)
{
	int i, j;

	i = 0;
	j = strlen(str);
	while (coord->name[i]) str[j++] = coord->name[i++];

	str[j] = '\0';
}

STATIC bool
solution_lastqt_cw(const solution_moves_t s[static 1])
{
	return are_lastmoves_singlecw(s->nmoves, s->moves) &&
	    are_lastmoves_singlecw(s->npremoves, s->premoves);
}

STATIC bool
coord_can_switch(
	const coord_t coord[static 1],
	const void *data,
	size_t n,
	const uint8_t moves[n]
)
{
	/*
	This function checks that the last move (or two moves, if parallel)
	have a non-trivial effect on the coordinate of the solved cube. This
	works in general for all coordinates that have been used so far, but
	in more general cases that have not been considered yet it may fail.
	*/

	uint64_t i;

	if (n == 0)
		return true;

	i = coord->coord(move(SOLVED_CUBE, moves[n-1]), data);
	if (i == 0)
		return false;

	if (n == 1 || !parallel(moves[n-1], moves[n-2]))
		return true;

	i = coord->coord(move(SOLVED_CUBE, moves[n-1]), data);
	return i != 0;
}

Generated with cgit - Back to sebastiano.tronto.net