From: Vivien Maisonneuve Date: Fri, 4 Jul 2014 09:36:11 +0000 (+0200) Subject: Simplify C code X-Git-Tag: 1.0~149 X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/commitdiff_plain/f6de7444731fbcadb872d64b97fe5b2877c24229?ds=sidebyside Simplify C code --- diff --git a/pypol/_islhelper.c b/pypol/_islhelper.c index 7e2408e..a7b4f33 100644 --- a/pypol/_islhelper.c +++ b/pypol/_islhelper.c @@ -4,154 +4,89 @@ #include -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)) { - return NULL; - } - bset = (isl_basic_set *) ptr; - n = isl_basic_set_n_constraint(bset); - if (n == -1) { - PyErr_SetString(PyExc_RuntimeError, - "an error occurred in isl_basic_set_n_constraint"); - return NULL; - } - pointers = PyList_New(n); - if (pointers == NULL) { +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; } - list = malloc(sizeof(pointer_list)); + bset = (isl_basic_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_basic_set_foreach_constraint(bset, _append_constraint_to_list, list) == -1) { PyErr_SetString(PyExc_RuntimeError, "an error occurred in isl_basic_set_foreach_constraint"); - Py_DECREF(pointers); + 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_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 ptr; + long pointer; isl_set *set; - int n; - PyObject *pointers; - pointer_list *list; - - if (!PyArg_ParseTuple(args, "l", &ptr)) { - return NULL; - } - set = (isl_set *) ptr; - n = isl_set_n_basic_set(set); - if (n == -1) { - PyErr_SetString(PyExc_RuntimeError, - "an error occurred in isl_set_n_basic_set"); - return NULL; - } - pointers = PyList_New(n); - if (pointers == NULL) { + 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_set_foreach_basic_set(set, pointer_list_append_basic_set, 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_set_foreach_basic_set"); - Py_DECREF(pointers); + Py_DECREF(list); return NULL; } - return pointers; + return list; } - -static int pointer_list_append_point(isl_point *point, void *user) { - PyObject *pointers, *value; - - pointers = (PyObject *) user; - value = PyLong_FromVoidPtr(point); - if (value == NULL) { - return -1; - } - return PyList_Append(pointers, value); +static int _append_point_to_list(isl_point *point, void* user) { + return _append_pointer_to_list((void *) point, user); } static PyObject * isl_set_points(PyObject *self, PyObject *args) { - long ptr; + long pointer; isl_set *set; - int n; - PyObject *pointers; - - if (!PyArg_ParseTuple(args, "l", &ptr)) { + PyObject *list; + if (!PyArg_ParseTuple(args, "l", &pointer)) { return NULL; } - set = (isl_set *) ptr; - pointers = PyList_New(0); - if (pointers == NULL) { + set = (isl_set *) pointer; + list = PyList_New(0); + if (list == NULL) { return NULL; } - n = isl_set_foreach_point(set, pointer_list_append_point, pointers); - if (n == -1) { + if (isl_set_foreach_point(set, _append_point_to_list, list) == -1) { PyErr_SetString(PyExc_RuntimeError, "an error occurred in isl_set_foreach_point"); - Py_DECREF(pointers); + Py_DECREF(list); return NULL; } - return pointers; + return list; }