Simplify Expression.__mul__(), Expression.__truediv__()
[linpy.git] / pypol / _islhelper.c
index f8c03e3..7751125 100644 (file)
 
 #include <isl/constraint.h>
 #include <isl/set.h>
+#include <isl/vertices.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) {
+static inline int _append_pointer_to_list(void *pointer, void *user) {
+    PyObject *list, *item;
+    list = (PyObject *) user;
+    item = PyLong_FromVoidPtr(pointer);
+    if (item == NULL) {
         return -1;
     }
-    PyList_SET_ITEM(list->pointers, list->cursor++, value);
-    return 0;
+    return PyList_Append(list, item);
 }
 
-static PyObject * isl_basic_set_constraints(PyObject *self, PyObject* args) {
-    long ptr;
-    isl_basic_set *bset;
-    int n;
-    PyObject *pointers;
-    pointer_list *list;
+static int _append_constraint_to_list(isl_constraint *constraint, void *user) {
+    return _append_pointer_to_list((void *) constraint, user);
+}
 
-    if (!PyArg_ParseTuple(args, "l", &ptr)) {
+static PyObject * isl_basic_set_constraints(PyObject *self, PyObject *args) {
+    long pointer;
+    isl_basic_set *bset;
+    PyObject *list;
+    if (!PyArg_ParseTuple(args, "l", &pointer)) {
         return NULL;
     }
-    bset = (isl_basic_set *) ptr;
-    n = isl_basic_set_n_constraint(bset);
-    if (n == -1) {
+    bset = (isl_basic_set *) pointer;
+    list = PyList_New(0);
+    if (list == NULL) {
+        return NULL;
+    }
+    if (isl_basic_set_foreach_constraint(bset, _append_constraint_to_list, list) == -1) {
         PyErr_SetString(PyExc_RuntimeError,
-            "an error occurred in isl_basic_set_n_constraint");
+            "an error occurred in isl_basic_set_foreach_constraint");
+        Py_DECREF(list);
         return NULL;
     }
-    pointers = PyList_New(n);
-    if (pointers == NULL) {
+    return list;
+}
+
+static int _append_basic_set_to_list(isl_basic_set *bset, void *user) {
+    return _append_pointer_to_list((void *) bset, user);
+}
+
+static PyObject * isl_set_basic_sets(PyObject *self, PyObject *args) {
+    long pointer;
+    isl_set *set;
+    PyObject *list;
+    if (!PyArg_ParseTuple(args, "l", &pointer)) {
         return NULL;
     }
-    list = malloc(sizeof(pointer_list));
+    set = (isl_set *) pointer;
+    list = PyList_New(0);
     if (list == NULL) {
-        Py_DECREF(pointers);
-        return PyErr_NoMemory();
+        return NULL;
     }
-    list->cursor = 0;
-    list->pointers = pointers;
-    n = isl_basic_set_foreach_constraint(bset, pointer_list_append_constraint,
-        list);
-    free(list);
-    if (n == -1) {
+    if (isl_set_foreach_basic_set(set, _append_basic_set_to_list, list) == -1) {
         PyErr_SetString(PyExc_RuntimeError,
-            "an error occurred in isl_basic_set_foreach_constraint");
-        Py_DECREF(pointers);
+            "an error occurred in isl_set_foreach_basic_set");
+        Py_DECREF(list);
         return NULL;
     }
-    return pointers;
+    return list;
 }
 
-
-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 int _append_point_to_list(isl_point *point, void* user) {
+    return _append_pointer_to_list((void *) point, user);
 }
 
-static PyObject * isl_set_basic_sets(PyObject *self, PyObject *args) {
-    long ptr;
+static PyObject * isl_set_points(PyObject *self, PyObject *args) {
+    long pointer;
     isl_set *set;
-    int n;
-    PyObject *pointers;
-    pointer_list *list;
-
-    if (!PyArg_ParseTuple(args, "l", &ptr)) {
+    PyObject *list;
+    if (!PyArg_ParseTuple(args, "l", &pointer)) {
         return NULL;
     }
-    set = (isl_set *) ptr;
-    n = isl_set_n_basic_set(set);
-    if (n == -1) {
+    set = (isl_set *) pointer;
+    list = PyList_New(0);
+    if (list == NULL) {
+        return NULL;
+    }
+    if (isl_set_foreach_point(set, _append_point_to_list, list) == -1) {
         PyErr_SetString(PyExc_RuntimeError,
-            "an error occurred in isl_set_n_basic_set");
+            "an error occurred in isl_set_foreach_point");
+        Py_DECREF(list);
         return NULL;
     }
-    pointers = PyList_New(n);
-    if (pointers == NULL) {
+    return list;
+}
+
+static int _append_vertex_to_list(isl_vertex *vertex, void* user) {
+    return _append_pointer_to_list((void *) vertex, user);
+}
+
+static PyObject * isl_vertices_vertices(PyObject *self, PyObject *args) {
+    long pointer;
+    isl_vertices *vertices;
+    PyObject *list;
+    if (!PyArg_ParseTuple(args, "l", &pointer)) {
         return NULL;
     }
-    list = malloc(sizeof(pointer_list));
+    vertices = (isl_vertices *) pointer;
+    list = PyList_New(0);
     if (list == NULL) {
-        Py_DECREF(pointers);
-        return PyErr_NoMemory();
+        return NULL;
     }
-    list->cursor = 0;
-    list->pointers = pointers;
-    n = isl_set_foreach_basic_set(set, pointer_list_append_basic_set, list);
-    free(list);
-    if (n == -1) {
+    if (isl_vertices_foreach_vertex(vertices, _append_vertex_to_list, list) == -1) {
         PyErr_SetString(PyExc_RuntimeError,
-            "an error occurred in isl_set_foreach_basic_set");
-        Py_DECREF(pointers);
+            "an error occurred in isl_vertices_foreach_vertex");
+        Py_DECREF(list);
         return NULL;
     }
-    return pointers;
+    return list;
 }
 
 
 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},
+    {"isl_set_points", isl_set_points, METH_VARARGS, NULL},
+    {"isl_vertices_vertices", isl_vertices_vertices, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
@@ -139,10 +138,8 @@ PyMODINIT_FUNC PyInit__islhelper(void) {
     if (m == NULL) {
         return NULL;
     }
-
     if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
         return NULL;
     }
-
     return m;
 }