Mercurial > cpython-lru_cache-weakref
changeset 31175:5cc1a400a02c legacy-trunk
Use dictionary specific looping idiom where possible.
Simplifies and speeds-up the code.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Sat, 13 Dec 2003 14:46:46 +0000 |
| parents | 88e80a04328a |
| children | 381140e7e9d7 |
| files | Objects/setobject.c |
| diffstat | 1 files changed, 41 insertions(+), 115 deletions(-) [+] |
line wrap: on
line diff
--- a/Objects/setobject.c Sat Dec 13 13:31:55 2003 +0000 +++ b/Objects/setobject.c Sat Dec 13 14:46:46 2003 +0000 @@ -369,7 +369,9 @@ set_difference(PySetObject *so, PyObject *other) { PySetObject *result, *otherset=NULL; - PyObject *item, *otherdata, *tgtdata, *it; + PyObject *otherdata, *tgtdata; + PyObject *key, *value; + int pos = 0; result = (PySetObject *)make_new_set(so->ob_type, NULL); if (result == NULL) @@ -389,30 +391,15 @@ otherdata = otherset->data; } - it = PyObject_GetIter(so->data); - if (it == NULL) { - Py_XDECREF(otherset); - Py_DECREF(result); - return NULL; - } - - while ((item = PyIter_Next(it)) != NULL) { - if (!PyDict_Contains(otherdata, item)) { - if (PyDict_SetItem(tgtdata, item, Py_True) == -1) { + while (PyDict_Next(so->data, &pos, &key, &value)) { + if (!PyDict_Contains(otherdata, key)) { + if (PyDict_SetItem(tgtdata, key, Py_True) == -1) { Py_XDECREF(otherset); - Py_DECREF(it); - Py_DECREF(item); return NULL; } } - Py_DECREF(item); } - Py_DECREF(it); Py_XDECREF(otherset); - if (PyErr_Occurred()) { - Py_DECREF(result); - return NULL; - } return (PyObject *)result; } @@ -482,11 +469,12 @@ static PyObject * set_symmetric_difference_update(PySetObject *so, PyObject *other) { - PyObject *item, *selfdata, *it, *otherdata; + PyObject *selfdata, *otherdata; PySetObject *otherset = NULL; + PyObject *key, *value; + int pos = 0; selfdata = so->data; - if (PyDict_Check(other)) otherdata = other; else if (PyAnySet_Check(other)) @@ -498,32 +486,20 @@ otherdata = otherset->data; } - it = PyObject_GetIter(otherdata); - if (it == NULL) - return NULL; - - while ((item = PyIter_Next(it)) != NULL) { - if (PyDict_Contains(selfdata, item)) { - if (PyDict_DelItem(selfdata, item) == -1) { + while (PyDict_Next(otherdata, &pos, &key, &value)) { + if (PyDict_Contains(selfdata, key)) { + if (PyDict_DelItem(selfdata, key) == -1) { Py_XDECREF(otherset); - Py_DECREF(it); - Py_DECREF(item); return NULL; } } else { - if (PyDict_SetItem(selfdata, item, Py_True) == -1) { + if (PyDict_SetItem(selfdata, key, Py_True) == -1) { Py_XDECREF(otherset); - Py_DECREF(it); - Py_DECREF(item); return NULL; } } - Py_DECREF(item); } Py_XDECREF(otherset); - Py_DECREF(it); - if (PyErr_Occurred()) - return NULL; Py_RETURN_NONE; } @@ -534,7 +510,9 @@ set_symmetric_difference(PySetObject *so, PyObject *other) { PySetObject *result; - PyObject *item, *selfdata, *otherdata, *tgtdata, *it, *rv, *otherset; + PyObject *selfdata, *otherdata, *tgtdata, *rv, *otherset; + PyObject *key, *value; + int pos = 0; if (PyDict_Check(other)) otherdata = other; @@ -557,46 +535,23 @@ tgtdata = result->data; selfdata = so->data; - it = PyObject_GetIter(otherdata); - if (it == NULL) { - Py_DECREF(result); - return NULL; - } - while ((item = PyIter_Next(it)) != NULL) { - if (!PyDict_Contains(selfdata, item)) { - if (PyDict_SetItem(tgtdata, item, Py_True) == -1) { - Py_DECREF(it); - Py_DECREF(item); + while (PyDict_Next(otherdata, &pos, &key, &value)) { + if (!PyDict_Contains(selfdata, key)) { + if (PyDict_SetItem(tgtdata, key, Py_True) == -1) { + Py_DECREF(result); return NULL; } } - Py_DECREF(item); - } - Py_DECREF(it); - if (PyErr_Occurred()) { - Py_DECREF(result); - return NULL; } - it = PyObject_GetIter(selfdata); - if (it == NULL) { - Py_DECREF(result); - return NULL; - } - while ((item = PyIter_Next(it)) != NULL) { - if (!PyDict_Contains(otherdata, item)) { - if (PyDict_SetItem(tgtdata, item, Py_True) == -1) { - Py_DECREF(it); - Py_DECREF(item); + pos = 0; + while (PyDict_Next(selfdata, &pos, &key, &value)) { + if (!PyDict_Contains(otherdata, key)) { + if (PyDict_SetItem(tgtdata, key, Py_True) == -1) { + Py_DECREF(result); return NULL; } } - Py_DECREF(item); - } - Py_DECREF(it); - if (PyErr_Occurred()) { - Py_DECREF(result); - return NULL; } return (PyObject *)result; @@ -637,7 +592,9 @@ static PyObject * set_issubset(PySetObject *so, PyObject *other) { - PyObject *otherdata, *it, *item, *tmp, *result; + PyObject *otherdata, *tmp, *result; + PyObject *key, *value; + int pos = 0; if (!PyAnySet_Check(other)) { tmp = make_new_set(&PySet_Type, other); @@ -649,23 +606,12 @@ } if (set_len(so) > set_len((PySetObject *)other)) Py_RETURN_FALSE; - - it = PyObject_GetIter(so->data); - if (it == NULL) - return NULL; otherdata = ((PySetObject *)other)->data; - while ((item = PyIter_Next(it)) != NULL) { - if (!PyDict_Contains(otherdata, item)) { - Py_DECREF(it); - Py_DECREF(item); + while (PyDict_Next(((PySetObject *)so)->data, &pos, &key, &value)) { + if (!PyDict_Contains(otherdata, key)) Py_RETURN_FALSE; - } - Py_DECREF(item); } - Py_DECREF(it); - if (PyErr_Occurred()) - return NULL; Py_RETURN_TRUE; } @@ -706,29 +652,22 @@ static long frozenset_hash(PyObject *self) { - PyObject *it, *item; PySetObject *so = (PySetObject *)self; + PyObject *key, *value; + int pos = 0; + long hash = 0; - if (so->hash != -1) return so->hash; - - it = PyObject_GetIter(((PySetObject *)so)->data); - if (it == NULL) - return -1; - while ((item = PyIter_Next(it)) != NULL) { + while (PyDict_Next(so->data, &pos, &key, &value)) { /* Multiplying by a large prime increases the bit dispersion for closely spaced hash values. The is important because some use cases have many combinations of a small number of elements with nearby hashes so that many distinct combinations collapse to only a handful of distinct hash values. */ - hash ^= PyObject_Hash(item) * 3644798167u; - Py_DECREF(item); + hash ^= PyObject_Hash(key) * 3644798167u; } - Py_DECREF(it); - if (PyErr_Occurred()) - return -1; so->hash = hash; return hash; } @@ -788,30 +727,17 @@ static int set_tp_print(PySetObject *so, FILE *fp, int flags) { - PyObject *it, *item; - int firstpass=1; - - it = PyObject_GetIter(so->data); - if (it == NULL) - return -1; - fprintf(fp, "%s([", so->ob_type->tp_name); + PyObject *key, *value; + int pos = 0; - while ((item = PyIter_Next(it)) != NULL) { - if (firstpass == 1) - firstpass = 0; - else + fprintf(fp, "%s([", so->ob_type->tp_name); + while (PyDict_Next(so->data, &pos, &key, &value)) { + if (pos) fprintf(fp, ", "); - if (PyObject_Print(item, fp, 0) != 0) { - Py_DECREF(it); - Py_DECREF(item); + if (PyObject_Print(key, fp, 0) != 0) return -1; - } - Py_DECREF(item); } - Py_DECREF(it); fprintf(fp, "])"); - if (PyErr_Occurred()) - return -1; return 0; }
