aboutsummaryrefslogtreecommitdiff
path: root/python/nissy_module.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--python/nissy_module.c74
1 files changed, 55 insertions, 19 deletions
diff --git a/python/nissy_module.c b/python/nissy_module.c
index a86b23f..88109d3 100644
--- a/python/nissy_module.c
+++ b/python/nissy_module.c
@@ -44,6 +44,29 @@ string_result(long long err, const char *result)
44} 44}
45 45
46static PyObject * 46static PyObject *
47stringlist_result(long long err, char *result)
48{
49 int i, j, k;
50 PyObject *list, *item;
51
52 if(!check_error(err)) {
53 return NULL;
54 } else {
55 list = PyList_New(err);
56 for (i = 0, j = 0, k = 0; result[i] != 0; i++) {
57 if (result[i] != '\n')
58 continue;
59 result[i] = 0;
60 item = PyUnicode_FromString(&result[k]);
61 PyList_SetItem(list, j, item);
62 j++;
63 k = i+1;
64 }
65 return list;
66 }
67}
68
69static PyObject *
47string_result_free(long long err, char *result) 70string_result_free(long long err, char *result)
48{ 71{
49 PyObject *ret; 72 PyObject *ret;
@@ -287,12 +310,11 @@ solve(PyObject *self, PyObject *args)
287{ 310{
288 long long result; 311 long long result;
289 unsigned nissflag, minmoves, maxmoves, maxsolutions; 312 unsigned nissflag, minmoves, maxmoves, maxsolutions;
290 int optimal, i, j, k, threads; 313 int optimal, threads;
291 const char *cube, *solver; 314 const char *cube, *solver;
292 char solutions[MAX_SOLUTIONS_SIZE]; 315 char solutions[MAX_SOLUTIONS_SIZE];
293 long long stats[NISSY_SIZE_SOLVE_STATS]; 316 long long stats[NISSY_SIZE_SOLVE_STATS];
294 PyByteArrayObject *data; 317 PyByteArrayObject *data;
295 PyObject *list, *item;
296 318
297 if (!PyArg_ParseTuple(args, "ssIIIIIIY", &cube, &solver, &nissflag, 319 if (!PyArg_ParseTuple(args, "ssIIIIIIY", &cube, &solver, &nissflag,
298 &minmoves, &maxmoves, &maxsolutions, &optimal, &threads, &data)) 320 &minmoves, &maxmoves, &maxsolutions, &optimal, &threads, &data))
@@ -305,21 +327,7 @@ solve(PyObject *self, PyObject *args)
305 stats, NULL, NULL); 327 stats, NULL, NULL);
306 Py_END_ALLOW_THREADS 328 Py_END_ALLOW_THREADS
307 329
308 if(!check_error(result)) { 330 return stringlist_result(result, solutions);
309 return NULL;
310 } else {
311 list = PyList_New(result);
312 for (i = 0, j = 0, k = 0; solutions[i] != 0; i++) {
313 if (solutions[i] != '\n')
314 continue;
315 solutions[i] = 0;
316 item = PyUnicode_FromString(&solutions[k]);
317 PyList_SetItem(list, j, item);
318 j++;
319 k = i+1;
320 }
321 return list;
322 }
323} 331}
324 332
325PyDoc_STRVAR(countmoves_doc, 333PyDoc_STRVAR(countmoves_doc,
@@ -368,8 +376,9 @@ comparemoves(PyObject *self, PyObject *args)
368 if (!PyArg_ParseTuple(args, "ss", &m1, &m2)) 376 if (!PyArg_ParseTuple(args, "ss", &m1, &m2))
369 return NULL; 377 return NULL;
370 378
371 if ((cmp = nissy_comparemoves(m1, m2)) < 0) 379 cmp = nissy_comparemoves(m1, m2);
372 return long_result(cmp); 380 if (!check_error(cmp))
381 return NULL;
373 382
374 switch (cmp) { 383 switch (cmp) {
375 case NISSY_COMPARE_MOVES_EQUAL: 384 case NISSY_COMPARE_MOVES_EQUAL:
@@ -381,6 +390,32 @@ comparemoves(PyObject *self, PyObject *args)
381 } 390 }
382} 391}
383 392
393PyDoc_STRVAR(variations_doc,
394"variations(moves, variation)\n"
395"--\n\n"
396"Find variations of a given move sequence\n"
397"\n"
398"Parameters:\n"
399" - moves: the moves\n"
400" - variation: the variation to apply, such as 'unniss' or 'lastqt'\n"
401"\n"
402"Returns: a list of move sequences, the variation of the given moves.\n"
403);
404PyObject *
405variations(PyObject *self, PyObject *args)
406{
407 long long err;
408 const char *m, *v;
409 char result[MAX_SOLUTIONS_SIZE];
410
411 if (!PyArg_ParseTuple(args, "ss", &m, &v))
412 return NULL;
413
414 err = nissy_variations(m, v, MAX_SOLUTIONS_SIZE, result);
415
416 return stringlist_result(err, result);
417}
418
384static PyMethodDef nissy_methods[] = { 419static PyMethodDef nissy_methods[] = {
385 { "inverse", inverse, METH_VARARGS, inverse_doc }, 420 { "inverse", inverse, METH_VARARGS, inverse_doc },
386 { "applymoves", applymoves, METH_VARARGS, applymoves_doc }, 421 { "applymoves", applymoves, METH_VARARGS, applymoves_doc },
@@ -392,6 +427,7 @@ static PyMethodDef nissy_methods[] = {
392 { "solve", solve, METH_VARARGS, solve_doc }, 427 { "solve", solve, METH_VARARGS, solve_doc },
393 { "countmoves", countmoves, METH_VARARGS, countmoves_doc }, 428 { "countmoves", countmoves, METH_VARARGS, countmoves_doc },
394 { "comparemoves", comparemoves, METH_VARARGS, comparemoves_doc }, 429 { "comparemoves", comparemoves, METH_VARARGS, comparemoves_doc },
430 { "variations", variations, METH_VARARGS, variations_doc },
395 { NULL, NULL, 0, NULL } 431 { NULL, NULL, 0, NULL }
396}; 432};
397 433

Generated with cgit - Back to sebastiano.tronto.net