Simplify implementation of Coordinates.__new__()
[linpy.git] / linpy / _islhelper.c
1 // Copyright 2014 MINES ParisTech
2 //
3 // This file is part of LinPy.
4 //
5 // LinPy is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // LinPy is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with LinPy. If not, see <http://www.gnu.org/licenses/>.
17
18 #include <Python.h>
19
20 #include <isl/constraint.h>
21 #include <isl/set.h>
22 #include <isl/vertices.h>
23
24
25 static inline int _append_pointer_to_list(void *pointer, void *user) {
26 PyObject *list, *item;
27 list = (PyObject *) user;
28 item = PyLong_FromVoidPtr(pointer);
29 if (item == NULL) {
30 return -1;
31 }
32 return PyList_Append(list, item);
33 }
34
35 static int _append_constraint_to_list(isl_constraint *constraint, void *user) {
36 return _append_pointer_to_list((void *) constraint, user);
37 }
38
39 static PyObject * isl_basic_set_constraints(PyObject *self, PyObject *args) {
40 long pointer;
41 isl_basic_set *bset;
42 PyObject *list;
43 if (!PyArg_ParseTuple(args, "l", &pointer)) {
44 return NULL;
45 }
46 bset = (isl_basic_set *) pointer;
47 list = PyList_New(0);
48 if (list == NULL) {
49 return NULL;
50 }
51 if (isl_basic_set_foreach_constraint(bset, _append_constraint_to_list, list) == -1) {
52 PyErr_SetString(PyExc_RuntimeError,
53 "an error occurred in isl_basic_set_foreach_constraint");
54 Py_DECREF(list);
55 return NULL;
56 }
57 return list;
58 }
59
60 static int _append_basic_set_to_list(isl_basic_set *bset, void *user) {
61 return _append_pointer_to_list((void *) bset, user);
62 }
63
64 static PyObject * isl_set_basic_sets(PyObject *self, PyObject *args) {
65 long pointer;
66 isl_set *set;
67 PyObject *list;
68 if (!PyArg_ParseTuple(args, "l", &pointer)) {
69 return NULL;
70 }
71 set = (isl_set *) pointer;
72 list = PyList_New(0);
73 if (list == NULL) {
74 return NULL;
75 }
76 if (isl_set_foreach_basic_set(set, _append_basic_set_to_list, list) == -1) {
77 PyErr_SetString(PyExc_RuntimeError,
78 "an error occurred in isl_set_foreach_basic_set");
79 Py_DECREF(list);
80 return NULL;
81 }
82 return list;
83 }
84
85 static int _append_point_to_list(isl_point *point, void* user) {
86 return _append_pointer_to_list((void *) point, user);
87 }
88
89 static PyObject * isl_set_points(PyObject *self, PyObject *args) {
90 long pointer;
91 isl_set *set;
92 PyObject *list;
93 if (!PyArg_ParseTuple(args, "l", &pointer)) {
94 return NULL;
95 }
96 set = (isl_set *) pointer;
97 list = PyList_New(0);
98 if (list == NULL) {
99 return NULL;
100 }
101 if (isl_set_foreach_point(set, _append_point_to_list, list) == -1) {
102 PyErr_SetString(PyExc_RuntimeError,
103 "an error occurred in isl_set_foreach_point");
104 Py_DECREF(list);
105 return NULL;
106 }
107 return list;
108 }
109
110 static int _append_vertex_to_list(isl_vertex *vertex, void* user) {
111 return _append_pointer_to_list((void *) vertex, user);
112 }
113
114 static PyObject * isl_vertices_vertices(PyObject *self, PyObject *args) {
115 long pointer;
116 isl_vertices *vertices;
117 PyObject *list;
118 if (!PyArg_ParseTuple(args, "l", &pointer)) {
119 return NULL;
120 }
121 vertices = (isl_vertices *) pointer;
122 list = PyList_New(0);
123 if (list == NULL) {
124 return NULL;
125 }
126 if (isl_vertices_foreach_vertex(vertices, _append_vertex_to_list, list) == -1) {
127 PyErr_SetString(PyExc_RuntimeError,
128 "an error occurred in isl_vertices_foreach_vertex");
129 Py_DECREF(list);
130 return NULL;
131 }
132 return list;
133 }
134
135
136 static PyMethodDef _islhelper_methods[] = {
137 {"isl_basic_set_constraints", isl_basic_set_constraints, METH_VARARGS, NULL},
138 {"isl_set_basic_sets", isl_set_basic_sets, METH_VARARGS, NULL},
139 {"isl_set_points", isl_set_points, METH_VARARGS, NULL},
140 {"isl_vertices_vertices", isl_vertices_vertices, METH_VARARGS, NULL},
141 {NULL, NULL, 0, NULL}
142 };
143
144 static struct PyModuleDef _islhelpermodule = {
145 PyModuleDef_HEAD_INIT,
146 "_islhelper",
147 NULL,
148 0,
149 _islhelper_methods
150 };
151
152 PyMODINIT_FUNC PyInit__islhelper(void) {
153 PyObject *m;
154 m = PyModule_Create(&_islhelpermodule);
155 if (m == NULL) {
156 return NULL;
157 }
158 if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
159 return NULL;
160 }
161 return m;
162 }