Split linear.py and add domains
[linpy.git] / pypol / _islhelper.c
diff --git a/pypol/_islhelper.c b/pypol/_islhelper.c
new file mode 100644 (file)
index 0000000..f8c03e3
--- /dev/null
@@ -0,0 +1,148 @@
+#include <Python.h>
+
+#include <isl/constraint.h>
+#include <isl/set.h>
+
+
+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;
+}