X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/a9410db7436274a5623ee1dd61f6e45b20f7164c..fd46a88df7b50644bbd2f123f9f733a1d098a229:/algorithms.c diff --git a/algorithms.c b/algorithms.c index a195de9..071894a 100644 --- a/algorithms.c +++ b/algorithms.c @@ -86,12 +86,13 @@ PyObject *keysz_pywrap(const octet *k) case KSZ_SET: { keyszset_pyobj *o = PyObject_New(keyszset_pyobj, keyszset_pytype); + PyObject *l; int i, n; o->dfl = ARG(0); for (i = 0; ARG(i); i++) ; - n = i; o->set = PyTuple_New(n); - for (i = 0; i < n; i++) - PyTuple_SET_ITEM(o->set, i, PyInt_FromLong(ARG(i))); + n = i; l = PyList_New(n); + for (i = 0; i < n; i++) PyList_SET_ITEM(l, i, PyInt_FromLong(ARG(i))); + o->set = PyFrozenSet_New(l); Py_DECREF(l); return ((PyObject *)o); } break; default: @@ -152,41 +153,30 @@ static PyObject *keyszset_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) { static const char *const kwlist[] = { "default", "set", 0 }; - int dfl, i, n, xx; + int dfl, xx; PyObject *set = 0; - PyObject *x = 0, *l = 0; + PyObject *x = 0, *l = 0, *i = 0; keyszset_pyobj *o = 0; if (!PyArg_ParseTupleAndKeywords(arg, kw, "i|O:new", KWLIST, &dfl, &set)) goto end; - if (!set) set = PyTuple_New(0); - else Py_INCREF(set); - if (!PySequence_Check(set)) TYERR("want a sequence"); - n = PySequence_Size(set); if (n < 0) goto end; + if (set) i = PyObject_GetIter(set); + else { set = PyTuple_New(0); i = PyObject_GetIter(set); Py_DECREF(set); } + if (!i) goto end; l = PyList_New(0); if (!l) goto end; if (dfl < 0) VALERR("key size cannot be negative"); - x = PyInt_FromLong(dfl); - PyList_Append(l, x); - Py_DECREF(x); - x = 0; - for (i = 0; i < n; i++) { - if ((x = PySequence_GetItem(set, i)) == 0) goto end; - xx = PyInt_AsLong(x); - if (PyErr_Occurred()) goto end; - if (xx == dfl) continue; + x = PyInt_FromLong(dfl); PyList_Append(l, x); Py_DECREF(x); x = 0; + for (;;) { + x = PyIter_Next(i); if (!x) break; + xx = PyInt_AsLong(x); if (xx == -1 && PyErr_Occurred()) goto end; if (xx < 0) VALERR("key size cannot be negative"); - PyList_Append(l, x); - Py_DECREF(x); - x = 0; + PyList_Append(l, x); Py_DECREF(x); x = 0; } - Py_DECREF(set); - if ((set = PySequence_Tuple(l)) == 0) goto end; + if ((set = PyFrozenSet_New(l)) == 0) goto end; o = (keyszset_pyobj *)ty->tp_alloc(ty, 0); o->dfl = dfl; o->set = set; - Py_INCREF(set); end: - Py_XDECREF(set); Py_XDECREF(l); Py_XDECREF(x); return ((PyObject *)o); @@ -206,25 +196,31 @@ static PyObject *krget_max(PyObject *me, void *hunoz) static PyObject *ksget_min(PyObject *me, void *hunoz) { - PyObject *set = ((keyszset_pyobj *)me)->set; - int i, n, y, x = -1; - n = PyTuple_GET_SIZE(set); - for (i = 0; i < n; i++) { - y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i)); + PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set); + PyObject *v = 0; + int y, x = -1; + for (;;) { + v = PyIter_Next(i); if (!v) break; + y = PyInt_AsLong(v); assert(y >= 0); if (x == -1 || y < x) x = y; } + Py_DECREF(i); Py_XDECREF(v); + if (PyErr_Occurred()) return (0); return (PyInt_FromLong(x)); } static PyObject *ksget_max(PyObject *me, void *hunoz) { - PyObject *set = ((keyszset_pyobj *)me)->set; - int i, n, y, x = -1; - n = PyTuple_GET_SIZE(set); - for (i = 0; i < n; i++) { - y = PyInt_AS_LONG(PyTuple_GET_ITEM(set, i)); + PyObject *i = PyObject_GetIter(((keyszset_pyobj *)me)->set); + PyObject *v = 0; + int y, x = -1; + for (;;) { + v = PyIter_Next(i); if (!v) break; + y = PyInt_AsLong(v); assert(y >= 0); if (y > x) x = y; } + Py_DECREF(i); Py_XDECREF(v); + if (PyErr_Occurred()) return (0); return (PyInt_FromLong(x)); } @@ -488,8 +484,8 @@ static const PyTypeObject keyszset_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ - "KeySZSet(DEFAULT, SEQ)\n" - " Key size constraints: size must be DEFAULT or an element of SEQ.", + "KeySZSet(DEFAULT, ITER)\n" + " Key size constraints: size must be DEFAULT or an element of ITER.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */