aboutsummaryrefslogtreecommitdiff
path: root/python/nissy_module.c
blob: 43f5b02cefaf7374c5d81e01ac0b32d250b0e8b4 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
This version of the python module includes an attempt to use the callback
functions for pausing / stopping / resuming a solve. It probably cannot
work until PEP 703 (freethreading python) is implemented.
*/

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdbool.h>

#include "../src/nissy.h"

#define MAX_SOLUTIONS_SIZE 250000

PyObject *poll_status_callback = NULL;

static bool
check_error(long long err)
{
	char err_string[255];

	/* A positive value always denotes a success */
	if (err > 0)
		return true;

	switch (err) {
	case NISSY_OK: /* Fallthrough */
	case NISSY_WARNING_UNSOLVABLE:
		return true;
	case NISSY_ERROR_INVALID_CUBE:
	case NISSY_ERROR_UNSOLVABLE_CUBE: /* Fallthrough */
	case NISSY_ERROR_INVALID_MOVES:
	case NISSY_ERROR_INVALID_TRANS:
	case NISSY_ERROR_INVALID_SOLVER:
	case NISSY_ERROR_NULL_POINTER:
	case NISSY_ERROR_BUFFER_SIZE:
	case NISSY_ERROR_DATA:
	case NISSY_ERROR_OPTIONS:
	case NISSY_ERROR_UNKNOWN:
	default:
		sprintf(err_string, "Error from libnissy (%lld)", err);
		PyErr_SetString(PyExc_Exception, err_string);
		return false;
	}

}

static PyObject *
string_result(long long err, const char *result)
{
	return check_error(err) ? PyUnicode_FromString(result) : NULL;
}

static PyObject *
stringlist_result(long long err, char *result)
{
	int i, j, k;
	PyObject *list, *item;

	if(!check_error(err)) {
		return NULL;
	} else {
		list = PyList_New(err);
		for (i = 0, j = 0, k = 0; result[i] != 0; i++) {
			if (result[i] != '\n')
				continue;
			result[i] = 0;
			item = PyUnicode_FromString(&result[k]);
			PyList_SetItem(list, j, item);
			j++;
			k = i+1;
		}
		return list;
	}
}

static PyObject *
string_result_free(long long err, char *result)
{
	PyObject *ret;

	ret = PyUnicode_FromString(result);
	free(result);

	return check_error(err) ? ret : NULL;
}

static PyObject *
long_result(long long result)
{
	check_error(result);
	return PyLong_FromLong(result);
}

static int
callback_wrapper(void *arg)
{
	int r;
	PyObject *result;
	PyThreadState **thread;

	thread = (PyThreadState **)arg;

printf("About to restore thread\n");
PyEval_RestoreThread(*thread);
printf("Restoring\n");

	if (poll_status_callback != NULL && poll_status_callback != Py_None &&
	    PyCallable_Check(poll_status_callback) == 1) {
		result = PyObject_CallNoArgs(poll_status_callback);
		if (result == NULL) {
			r = 0;
			goto callback_wrapper_end;
		}
		r = PyLong_AsInt(result);
		r = r >= 0 && r <= 2 ? r : 0;
	} else {
		r = NISSY_STATUS_RUN;
	}

callback_wrapper_end:
printf("Saving\n");
*thread = PyEval_SaveThread();
printf("Saved\n");
return r;
}

PyDoc_STRVAR(inverse_doc,
"inverse(cube)\n"
"--\n\n"
"Invert 'cube'.\n"
"\n"
"Parameters:\n"
"  - cube: a cube in\n"
"\n"
"Returns: the inverse cube\n"
);
static PyObject *
inverse(PyObject *self, PyObject *args)
{
	long long err;
	const char *cube;
	char result[NISSY_SIZE_CUBE];

	if (!PyArg_ParseTuple(args, "s", &cube))
		return NULL;

	err = nissy_inverse(cube, result);
	return string_result(err, result);
}

PyDoc_STRVAR(applymoves_doc,
"applymoves(cube, moves)\n"
"--\n\n"
"Apply 'moves' to 'cube'.\n"
"\n"
"Parameters:\n"
"  - cube: a cube in\n"
"  - moves: the moves to apply on the cube\n"
"\n"
"Returns: the resulting cube\n"
);
static PyObject *
applymoves(PyObject *self, PyObject *args)
{
	long long err;
	const char *cube, *moves;
	char result[NISSY_SIZE_CUBE];

	if (!PyArg_ParseTuple(args, "ss", &cube, &moves))
		return NULL;

	err = nissy_applymoves(cube, moves, result);
	return string_result(err, result);
}

PyDoc_STRVAR(applytrans_doc,
"applytrans(cube, transformation)\n"
"--\n\n"
"Apply 'transformation' to 'cube'.\n"
"\n"
"Parameters:\n"
"  - cube: a cube\n"
"  - transformation: the transformation to apply on the cube, formatted as\n"
"    (rotation|mirrored) (2 letters)\n"
"    for example 'mirrored ur' or 'rotation lf'\n"
"\n"
"Returns: the resulting cube\n"
);
static PyObject *
applytrans(PyObject *self, PyObject *args)
{
	long long err;
	const char *cube, *trans;
	char result[NISSY_SIZE_CUBE];

	if (!PyArg_ParseTuple(args, "ss", &cube, &trans))
		return NULL;

	err = nissy_applytrans(cube, trans, result);
	return string_result(err, result);
}

PyDoc_STRVAR(getcube_doc,
"getcube(ep, eo, cp, co, orientation, options)\n"
"--\n\n"
"Constructs the cube from the given coordinates and options\n"
"\n"
"Parameters:\n"
"  - ep: the edge permutation coordinate\n"
"  - eo: the edge orientation coordinate\n"
"  - cp: the corner permutation coordinate\n"
"  - co: the corner orientation coordinate\n"
"  - orientation: the orientation of the cube\n"
"  - options: a string, for example \"fix\"\n"
"\n"
"Returns: the cube constructed from the given coordinates\n"
);
static PyObject *
getcube(PyObject *self, PyObject *args)
{
	long long ep, eo, cp, co, or, err;
	const char *options;
	char result[NISSY_SIZE_CUBE];

	if (!PyArg_ParseTuple(
	    args, "LLLLLs", &ep, &eo, &cp, &co, &or, &options))
		return NULL;

	err = nissy_getcube(ep, eo, cp, co, or, options, result);
	return string_result(err, result);
}

PyDoc_STRVAR(solverinfo_doc,
"solverinfo(solver)\n"
"--\n\n"
"Returns the size and the short name of the data for the given solver\n"
"\n"
"Parameters:\n"
"  - solver: the name of the solver\n"
"\n"
"Returns: a pair containing the size and the short name "
"of the data for the solver, in bytes\n"
);
static PyObject *
solverinfo(PyObject *self, PyObject *args)
{
	long long result;
	const char *solver;
	char buf[NISSY_SIZE_DATAID];
	PyObject *py_result, *py_buf;

	if (!PyArg_ParseTuple(args, "s", &solver))
		return NULL;

	result = nissy_solverinfo(solver, buf);
	
	py_result = PyLong_FromLong(result);
	py_buf = PyUnicode_FromString(buf);
	return PyTuple_Pack(2, py_result, py_buf);
}

PyDoc_STRVAR(gendata_doc,
"gendata(solver)\n"
"--\n\n"
"Generates the data (pruning table) for the given solver\n"
"\n"
"Parameters:\n"
"  - solver: the name of the solver\n"
"\n"
"Returns: a bytearray containing the data for the solver\n"
);
static PyObject *
gendata(PyObject *self, PyObject *args)
{
	long long size, err;
	const char *solver;
	char dataid[NISSY_SIZE_DATAID];
	unsigned char *buf;

	if (!PyArg_ParseTuple(args, "s", &solver))
		return NULL;

	size = nissy_solverinfo(solver, dataid);
	if (!check_error(size))
		return NULL;

	buf = PyMem_Malloc(size);

	Py_BEGIN_ALLOW_THREADS
	err = nissy_gendata(solver, size, buf);
	Py_END_ALLOW_THREADS

	if (check_error(err))
		return PyByteArray_FromStringAndSize((char *)buf, size);
	else
		return NULL;
}

PyDoc_STRVAR(checkdata_doc,
"checkdata(data)\n"
"--\n\n"
"Checks if the data (pruning table) given is valid or not\n"
"\n"
"Parameters:\n"
"  - solver: the name of the solver\n"
"  - data: a bytearray containing the data for a solver\n"
"\n"
"Returns: true if the data is valid, false otherwise\n"
);
PyObject *
checkdata(PyObject *self, PyObject *args)
{
	const char *solver;
	long long result;
	PyByteArrayObject *data;

	if (!PyArg_ParseTuple(args, "sY", &solver, &data))
		return NULL;

	result = nissy_checkdata(
	    solver, data->ob_alloc, (unsigned char *)data->ob_bytes);

	if (check_error(result))
		return Py_True;
	else
		return Py_False;
}

PyDoc_STRVAR(solve_doc,
"solve(cube, solver, nissflag, minmoves, maxmoves, maxsolutions,"
" optimal, threads, data, callback)\n"
"--\n\n"
"Solves the given 'cube' with the given 'solver' and other parameters."
"See the documentation for libnissy (in nissy.h) for details.\n"
"\n"
"Parameters:\n"
"  - cube: a cube\n"
"  - solver: the solver to use\n"
"  - minmoves: the minimum number of moves to use\n"
"  - maxmoves: the maximum number of moves to use\n"
"  - maxsolution: the maximum number of solutions to return\n"
"  - optimal: the largest number of moves from the shortest solution\n"
"  - threads: the number of threads to use (0 for default)\n"
"  - data: a bytearray containing the data for the solver\n"
"  - callback: a function that returns 0 (run), 1 (stop) or 2 (pause).\n"
"              Polled by the solver to determine if the user requested the\n"
"              to pause or stop the solve. Not used by all solvers.\n"
"\n"
"Returns: a list with the solutions found\n"
);
PyObject *
solve(PyObject *self, PyObject *args)
{
	long long result;
	unsigned nissflag, minmoves, maxmoves, maxsolutions;
	int optimal, threads;
	const char *cube, *solver;
	char solutions[MAX_SOLUTIONS_SIZE];
	long long stats[NISSY_SIZE_SOLVE_STATS];
	PyByteArrayObject *data;
	PyObject *callback;

	if (!PyArg_ParseTuple(args, "ssIIIIIIYO", &cube, &solver, &nissflag,
	     &minmoves, &maxmoves, &maxsolutions, &optimal, &threads, &data,
	     &callback))
		return NULL;

if (Py_TYPE(callback) == NULL)
printf("Type of callback is NULL!\n");
PyObject_Print(callback, stdout, 0);
printf("Checking callback here:\n");
PyObject *rrr = PyObject_CallNoArgs(callback);
printf("result: %d\n", PyLong_AsInt(rrr));

poll_status_callback = callback;

	Py_BEGIN_ALLOW_THREADS

#ifdef Py_GIL_DISABLED
	result = nissy_solve(cube, solver, nissflag, minmoves, maxmoves,
	    maxsolutions, optimal, threads, data->ob_alloc,
	    (unsigned char *)data->ob_bytes, MAX_SOLUTIONS_SIZE, solutions,
	    stats, callback_wrapper, &_save);
#else
	if (callback != NULL && callback != Py_None)
		printf("Warning: pause / stop / resume not available on "
		    "versions of Python with GIL enabled.\n");
	result = nissy_solve(cube, solver, nissflag, minmoves, maxmoves,
	    maxsolutions, optimal, threads, data->ob_alloc,
	    (unsigned char *)data->ob_bytes, MAX_SOLUTIONS_SIZE, solutions,
	    stats, NULL, NULL);
#endif
	Py_END_ALLOW_THREADS

	return stringlist_result(result, solutions);
}

PyDoc_STRVAR(countmoves_doc,
"countmoves(moves)\n"
"--\n\n"
"Count the moves\n"
"\n"
"Parameters:\n"
"  - moves: the moves to be counted\n"
"\n"
"Returns: the number of moves in HTM metric\n"
);
PyObject *
countmoves(PyObject *self, PyObject *args)
{
	long long count;
	const char *moves;

	if (!PyArg_ParseTuple(args, "s", &moves))
		return NULL;

	count = nissy_countmoves(moves);
	return long_result(count);
}

PyDoc_STRVAR(comparemoves_doc,
"comparemoves(moves1, moves2)\n"
"--\n\n"
"Compare the two move sequences\n"
"\n"
"Parameters:\n"
"  - moves1: the first sequence of moves\n"
"  - moves2: the second sequence of moves\n"
"\n"
"Returns: a string describing how the two moves sequences compare. "
"This can be one of:\n"
"\"EQUAL\"      - The two sequences are equal up to swapping parallel moves\n"
"\"DIFFERENT\"  - The two sequences are different\n"
);
PyObject *
comparemoves(PyObject *self, PyObject *args)
{
	long long cmp;
	const char *m1, *m2;

	if (!PyArg_ParseTuple(args, "ss", &m1, &m2))
		return NULL;

	cmp = nissy_comparemoves(m1, m2);
	if (!check_error(cmp))
		return NULL;

	switch (cmp) {
	case NISSY_COMPARE_MOVES_EQUAL:
		return string_result(cmp, "EQUAL");
	case NISSY_COMPARE_MOVES_DIFFERENT:
		return string_result(cmp, "DIFFERENT");
	default:
		return long_result(cmp);
	}
}

PyDoc_STRVAR(variations_doc,
"variations(moves, variation)\n"
"--\n\n"
"Find variations of a given move sequence\n"
"\n"
"Parameters:\n"
"  - moves: the moves\n"
"  - variation: the variation to apply, such as 'unniss' or 'lastqt'\n"
"\n"
"Returns: a list of move sequences, the variation of the given moves.\n"
);
PyObject *
variations(PyObject *self, PyObject *args)
{
	long long err;
	const char *m, *v;
	char result[MAX_SOLUTIONS_SIZE];

	if (!PyArg_ParseTuple(args, "ss", &m, &v))
		return NULL;

	err = nissy_variations(m, v, MAX_SOLUTIONS_SIZE, result);

	return stringlist_result(err, result);
}

static PyMethodDef nissy_methods[] = {
	{ "inverse", inverse, METH_VARARGS, inverse_doc },
	{ "applymoves", applymoves, METH_VARARGS, applymoves_doc },
	{ "applytrans", applytrans, METH_VARARGS, applytrans_doc },
	{ "getcube", getcube, METH_VARARGS, getcube_doc },
	{ "solverinfo", solverinfo, METH_VARARGS, solverinfo_doc },
	{ "gendata", gendata, METH_VARARGS, gendata_doc },
	{ "checkdata", checkdata, METH_VARARGS, checkdata_doc },
	{ "solve", solve, METH_VARARGS, solve_doc },
	{ "countmoves", countmoves, METH_VARARGS, countmoves_doc },
	{ "comparemoves", comparemoves, METH_VARARGS, comparemoves_doc },
	{ "variations", variations, METH_VARARGS, variations_doc },
	{ NULL, NULL, 0, NULL }
};

static struct PyModuleDef nissy = {
	.m_base = PyModuleDef_HEAD_INIT,
	.m_name = "nissy",
	.m_doc = "python module for libnissy",
	.m_size = -1,
	.m_methods = nissy_methods,
	.m_slots = NULL,
	.m_traverse = NULL,
	.m_clear = NULL,
	.m_free = NULL
};

static void
log_stdout(const char *str, void *unused)
{
	fprintf(stderr, "%s", str);
}

PyMODINIT_FUNC PyInit_nissy(void) {
	PyObject *module;

	nissy_setlogger(log_stdout, NULL);
	module = PyModule_Create(&nissy);
#ifdef Py_GIL_DISABLED
	PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED);
#endif

	PyModule_AddStringConstant(module, "solved_cube", NISSY_SOLVED_CUBE);
	PyModule_AddIntConstant(module, "nissflag_normal", NISSY_NISSFLAG_NORMAL);
	PyModule_AddIntConstant(module, "nissflag_inverse", NISSY_NISSFLAG_INVERSE);
	PyModule_AddIntConstant(module, "nissflag_mixed", NISSY_NISSFLAG_MIXED);
	PyModule_AddIntConstant(module, "nissflag_linear", NISSY_NISSFLAG_LINEAR);
	PyModule_AddIntConstant(module, "nissflag_all", NISSY_NISSFLAG_ALL);
	PyModule_AddIntConstant(module, "status_run", NISSY_STATUS_RUN);
	PyModule_AddIntConstant(module, "status_stop", NISSY_STATUS_STOP);
	PyModule_AddIntConstant(module, "status_pause", NISSY_STATUS_PAUSE);

	return module;
}

Generated with cgit - Back to sebastiano.tronto.net