Use OrderedDict to store Expression coefficients
[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 n = isl_basic_set_n_constraint(bset);
40 if (n == -1) {
41 PyErr_SetString(PyExc_RuntimeError,
42 "an error occurred in isl_basic_set_n_constraint");
43 return NULL;
44 }
45 pointers = PyList_New(n);
46 if (pointers == NULL) {
47 return NULL;
48 }
49 list = malloc(sizeof(pointer_list));
50 if (list == NULL) {
51 Py_DECREF(pointers);
52 return PyErr_NoMemory();
53 }
54 list->cursor = 0;
55 list->pointers = pointers;
56 n = isl_basic_set_foreach_constraint(bset, pointer_list_append_constraint,
57 list);
58 free(list);
59 if (n == -1) {
60 PyErr_SetString(PyExc_RuntimeError,
61 "an error occurred in isl_basic_set_foreach_constraint");
62 Py_DECREF(pointers);
63 return NULL;
64 }
65 return pointers;
66 }
67
68
69 static int pointer_list_append_basic_set(isl_basic_set *bset, void *user) {
70 pointer_list *list;
71 PyObject *value;
72
73 list = (pointer_list *) user;
74 value = PyLong_FromVoidPtr(bset);
75 if (value == NULL) {
76 return -1;
77 }
78 PyList_SET_ITEM(list->pointers, list->cursor++, value);
79 return 0;
80 }
81
82 static PyObject * isl_set_basic_sets(PyObject *self, PyObject *args) {
83 long ptr;
84 isl_set *set;
85 int n;
86 PyObject *pointers;
87 pointer_list *list;
88
89 if (!PyArg_ParseTuple(args, "l", &ptr)) {
90 return NULL;
91 }
92 set = (isl_set *) ptr;
93 n = isl_set_n_basic_set(set);
94 if (n == -1) {
95 PyErr_SetString(PyExc_RuntimeError,
96 "an error occurred in isl_set_n_basic_set");
97 return NULL;
98 }
99 pointers = PyList_New(n);
100 if (pointers == NULL) {
101 return NULL;
102 }
103 list = malloc(sizeof(pointer_list));
104 if (list == NULL) {
105 Py_DECREF(pointers);
106 return PyErr_NoMemory();
107 }
108 list->cursor = 0;
109 list->pointers = pointers;
110 n = isl_set_foreach_basic_set(set, pointer_list_append_basic_set, list);
111 free(list);
112 if (n == -1) {
113 PyErr_SetString(PyExc_RuntimeError,
114 "an error occurred in isl_set_foreach_basic_set");
115 Py_DECREF(pointers);
116 return NULL;
117 }
118 return pointers;
119 }
120
121
122 static PyMethodDef _islhelper_methods[] = {
123 {"isl_basic_set_constraints", isl_basic_set_constraints, METH_VARARGS, NULL},
124 {"isl_set_basic_sets", isl_set_basic_sets, METH_VARARGS, NULL},
125 {NULL, NULL, 0, NULL}
126 };
127
128 static struct PyModuleDef _islhelpermodule = {
129 PyModuleDef_HEAD_INIT,
130 "_islhelper",
131 NULL,
132 0,
133 _islhelper_methods
134 };
135
136 PyMODINIT_FUNC PyInit__islhelper(void) {
137 PyObject *m;
138 m = PyModule_Create(&_islhelpermodule);
139 if (m == NULL) {
140 return NULL;
141 }
142
143 if (PyModule_AddObject(m, "dim_set", PyLong_FromLong(isl_dim_set)) == -1) {
144 return NULL;
145 }
146
147 return m;
148 }