Methods Polyhedron.fromsympy(), Polyhedron.tosympy()
[linpy.git] / pypol / _isl.c
1 #include <Python.h>
2
3 #include <isl/constraint.h>
4 #include <isl/set.h>
5
6 struct pointer_list {
7 int cursor;
8 PyObject *pointers;
9 };
10 typedef struct pointer_list pointer_list;
11
12 static int pointer_list_append_constraint(isl_constraint *c, void *user) {
13 pointer_list *list;
14 PyObject *value;
15
16 list = (pointer_list *) user;
17 value = PyLong_FromVoidPtr(c);
18 if (value == NULL) {
19 return -1;
20 }
21 PyList_SET_ITEM(list->pointers, list->cursor++, value);
22 return 0;
23 }
24
25 static PyObject * basic_set_constraints(PyObject *self, PyObject* args) {
26 long ptr;
27 isl_basic_set *bset;
28 int n;
29 PyObject *pointers;
30 pointer_list *list;
31
32 if (!PyArg_ParseTuple(args, "l", &ptr))
33 return NULL;
34 bset = (isl_basic_set*) ptr;
35 n = isl_basic_set_n_constraint(bset);
36 if (n == -1) {
37 PyErr_SetString(PyExc_RuntimeError,
38 "an error occurred in isl_basic_set_n_constraint");
39 return NULL;
40 }
41 pointers = PyList_New(n);
42 if (pointers == NULL) {
43 return NULL;
44 }
45 list = malloc(sizeof(pointer_list));
46 if (list == NULL) {
47 Py_DECREF(pointers);
48 return PyErr_NoMemory();
49 }
50 list->cursor = 0;
51 list->pointers = pointers;
52 n = isl_basic_set_foreach_constraint(bset, pointer_list_append_constraint,
53 list);
54 free(list);
55 if (n == -1) {
56 PyErr_SetString(PyExc_RuntimeError,
57 "an error occurred in isl_basic_set_foreach_constraint");
58 Py_DECREF(pointers);
59 return NULL;
60 }
61 return pointers;
62 }
63
64 static PyMethodDef _isl_methods[] = {
65 {"basic_set_constraints", basic_set_constraints, METH_VARARGS, NULL},
66 {NULL, NULL, 0, NULL}
67 };
68
69 static struct PyModuleDef _islmodule = {
70 PyModuleDef_HEAD_INIT,
71 "_isl",
72 NULL,
73 0,
74 _isl_methods
75 };
76
77 PyMODINIT_FUNC PyInit__isl(void) {
78 PyObject *m;
79 m = PyModule_Create(&_islmodule);
80 if (m == NULL) {
81 return NULL;
82 }
83
84 if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
85 return NULL;
86 }
87
88 return m;
89 }