X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/blobdiff_plain/66e41ccd173874b3309e4c24be64c7b6b2ac6298..1d494bb187b70135df721c13306d7f26fdf33f50:/pypol/_islhelper.c diff --git a/pypol/_islhelper.c b/pypol/_islhelper.c new file mode 100644 index 0000000..f8c03e3 --- /dev/null +++ b/pypol/_islhelper.c @@ -0,0 +1,148 @@ +#include + +#include +#include + + +struct pointer_list { + int cursor; + PyObject *pointers; +}; + +typedef struct pointer_list pointer_list; + + +static int pointer_list_append_constraint(isl_constraint *c, void *user) { + pointer_list *list; + PyObject *value; + + list = (pointer_list *) user; + value = PyLong_FromVoidPtr(c); + if (value == NULL) { + return -1; + } + PyList_SET_ITEM(list->pointers, list->cursor++, value); + return 0; +} + +static PyObject * isl_basic_set_constraints(PyObject *self, PyObject* args) { + long ptr; + isl_basic_set *bset; + int n; + PyObject *pointers; + pointer_list *list; + + if (!PyArg_ParseTuple(args, "l", &ptr)) { + return NULL; + } + bset = (isl_basic_set *) ptr; + n = isl_basic_set_n_constraint(bset); + if (n == -1) { + PyErr_SetString(PyExc_RuntimeError, + "an error occurred in isl_basic_set_n_constraint"); + return NULL; + } + pointers = PyList_New(n); + if (pointers == NULL) { + return NULL; + } + list = malloc(sizeof(pointer_list)); + if (list == NULL) { + Py_DECREF(pointers); + return PyErr_NoMemory(); + } + list->cursor = 0; + list->pointers = pointers; + n = isl_basic_set_foreach_constraint(bset, pointer_list_append_constraint, + list); + free(list); + if (n == -1) { + PyErr_SetString(PyExc_RuntimeError, + "an error occurred in isl_basic_set_foreach_constraint"); + Py_DECREF(pointers); + return NULL; + } + return pointers; +} + + +static int pointer_list_append_basic_set(isl_basic_set *bset, void *user) { + pointer_list *list; + PyObject *value; + + list = (pointer_list *) user; + value = PyLong_FromVoidPtr(bset); + if (value == NULL) { + return -1; + } + PyList_SET_ITEM(list->pointers, list->cursor++, value); + return 0; +} + +static PyObject * isl_set_basic_sets(PyObject *self, PyObject *args) { + long ptr; + isl_set *set; + int n; + PyObject *pointers; + pointer_list *list; + + if (!PyArg_ParseTuple(args, "l", &ptr)) { + return NULL; + } + set = (isl_set *) ptr; + n = isl_set_n_basic_set(set); + if (n == -1) { + PyErr_SetString(PyExc_RuntimeError, + "an error occurred in isl_set_n_basic_set"); + return NULL; + } + pointers = PyList_New(n); + if (pointers == NULL) { + return NULL; + } + list = malloc(sizeof(pointer_list)); + if (list == NULL) { + Py_DECREF(pointers); + return PyErr_NoMemory(); + } + list->cursor = 0; + list->pointers = pointers; + n = isl_set_foreach_basic_set(set, pointer_list_append_basic_set, list); + free(list); + if (n == -1) { + PyErr_SetString(PyExc_RuntimeError, + "an error occurred in isl_set_foreach_basic_set"); + Py_DECREF(pointers); + return NULL; + } + return pointers; +} + + +static PyMethodDef _islhelper_methods[] = { + {"isl_basic_set_constraints", isl_basic_set_constraints, METH_VARARGS, NULL}, + {"isl_set_basic_sets", isl_set_basic_sets, METH_VARARGS, NULL}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef _islhelpermodule = { + PyModuleDef_HEAD_INIT, + "_islhelper", + NULL, + 0, + _islhelper_methods +}; + +PyMODINIT_FUNC PyInit__islhelper(void) { + PyObject *m; + m = PyModule_Create(&_islhelpermodule); + if (m == NULL) { + return NULL; + } + + if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) { + return NULL; + } + + return m; +}