Add helper function islhelper.isl_set_points()
[linpy.git] / pypol / _islhelper.c
1 #include <Python.h>
2
3 #include <isl/constraint.h>
4 #include <isl/set.h>
5
6
7 struct pointer_list {
8 int cursor;
9 PyObject *pointers;
10 };
11
12 typedef struct pointer_list pointer_list;
13
14
15 static int pointer_list_append_constraint(isl_constraint *c, void *user) {
16 pointer_list *list;
17 PyObject *value;
18
19 list = (pointer_list *) user;
20 value = PyLong_FromVoidPtr(c);
21 if (value == NULL) {
22 return -1;
23 }
24 PyList_SET_ITEM(list->pointers, list->cursor++, value);
25 return 0;
26 }
27
28 static PyObject * isl_basic_set_constraints(PyObject *self, PyObject* args) {
29 long ptr;
30 isl_basic_set *bset;
31 int n;
32 PyObject *pointers;
33 pointer_list *list;
34
35 if (!PyArg_ParseTuple(args, "l", &ptr)) {
36 return NULL;
37 }
38 bset = (isl_basic_set *) ptr;
39 bset = isl_basic_set_finalize(bset); // this instruction should not be required
40 n = isl_basic_set_n_constraint(bset);
41 if (n == -1) {
42 PyErr_SetString(PyExc_RuntimeError,
43 "an error occurred in isl_basic_set_n_constraint");
44 return NULL;
45 }
46 pointers = PyList_New(n);
47 if (pointers == NULL) {
48 return NULL;
49 }
50 list = malloc(sizeof(pointer_list));
51 if (list == NULL) {
52 Py_DECREF(pointers);
53 return PyErr_NoMemory();
54 }
55 list->cursor = 0;
56 list->pointers = pointers;
57 n = isl_basic_set_foreach_constraint(bset, pointer_list_append_constraint,
58 list);
59 free(list);
60 if (n == -1) {
61 PyErr_SetString(PyExc_RuntimeError,
62 "an error occurred in isl_basic_set_foreach_constraint");
63 Py_DECREF(pointers);
64 return NULL;
65 }
66 return pointers;
67 }
68
69
70 static int pointer_list_append_basic_set(isl_basic_set *bset, void *user) {
71 pointer_list *list;
72 PyObject *value;
73
74 list = (pointer_list *) user;
75 value = PyLong_FromVoidPtr(bset);
76 if (value == NULL) {
77 return -1;
78 }
79 PyList_SET_ITEM(list->pointers, list->cursor++, value);
80 return 0;
81 }
82
83 static PyObject * isl_set_basic_sets(PyObject *self, PyObject *args) {
84 long ptr;
85 isl_set *set;
86 int n;
87 PyObject *pointers;
88 pointer_list *list;
89
90 if (!PyArg_ParseTuple(args, "l", &ptr)) {
91 return NULL;
92 }
93 set = (isl_set *) ptr;
94 n = isl_set_n_basic_set(set);
95 if (n == -1) {
96 PyErr_SetString(PyExc_RuntimeError,
97 "an error occurred in isl_set_n_basic_set");
98 return NULL;
99 }
100 pointers = PyList_New(n);
101 if (pointers == NULL) {
102 return NULL;
103 }
104 list = malloc(sizeof(pointer_list));
105 if (list == NULL) {
106 Py_DECREF(pointers);
107 return PyErr_NoMemory();
108 }
109 list->cursor = 0;
110 list->pointers = pointers;
111 n = isl_set_foreach_basic_set(set, pointer_list_append_basic_set, list);
112 free(list);
113 if (n == -1) {
114 PyErr_SetString(PyExc_RuntimeError,
115 "an error occurred in isl_set_foreach_basic_set");
116 Py_DECREF(pointers);
117 return NULL;
118 }
119 return pointers;
120 }
121
122
123 static int pointer_list_append_point(isl_point *point, void *user) {
124 PyObject *pointers, *value;
125
126 pointers = (PyObject *) user;
127 value = PyLong_FromVoidPtr(point);
128 if (value == NULL) {
129 return -1;
130 }
131 return PyList_Append(pointers, value);
132 }
133
134 static PyObject * isl_set_points(PyObject *self, PyObject *args) {
135 long ptr;
136 isl_set *set;
137 int n;
138 PyObject *pointers;
139
140 if (!PyArg_ParseTuple(args, "l", &ptr)) {
141 return NULL;
142 }
143 set = (isl_set *) ptr;
144 pointers = PyList_New(0);
145 if (pointers == NULL) {
146 return NULL;
147 }
148 n = isl_set_foreach_point(set, pointer_list_append_point, pointers);
149 if (n == -1) {
150 PyErr_SetString(PyExc_RuntimeError,
151 "an error occurred in isl_set_foreach_point");
152 Py_DECREF(pointers);
153 return NULL;
154 }
155 return pointers;
156 }
157
158
159 static PyMethodDef _islhelper_methods[] = {
160 {"isl_basic_set_constraints", isl_basic_set_constraints, METH_VARARGS, NULL},
161 {"isl_set_basic_sets", isl_set_basic_sets, METH_VARARGS, NULL},
162 {"isl_set_points", isl_set_points, METH_VARARGS, NULL},
163 {NULL, NULL, 0, NULL}
164 };
165
166 static struct PyModuleDef _islhelpermodule = {
167 PyModuleDef_HEAD_INIT,
168 "_islhelper",
169 NULL,
170 0,
171 _islhelper_methods
172 };
173
174 PyMODINIT_FUNC PyInit__islhelper(void) {
175 PyObject *m;
176 m = PyModule_Create(&_islhelpermodule);
177 if (m == NULL) {
178 return NULL;
179 }
180
181 if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
182 return NULL;
183 }
184
185 return m;
186 }