[Python-checkins] python/dist/src/Objects setobject.c,1.18,1.19
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Mon Dec 15 08:23:57 EST 2003
Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv24748
Modified Files:
setobject.c
Log Message:
Improve algorithm for set.difference when the input is not a set.
Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/setobject.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** setobject.c 13 Dec 2003 19:38:47 -0000 1.18
--- setobject.c 15 Dec 2003 13:23:55 -0000 1.19
***************
*** 358,404 ****
static PyObject *
- set_difference(PySetObject *so, PyObject *other)
- {
- PySetObject *result, *otherset=NULL;
- PyObject *otherdata, *tgtdata;
- PyObject *key, *value;
- int pos = 0;
-
- result = (PySetObject *)make_new_set(so->ob_type, NULL);
- if (result == NULL)
- return NULL;
- tgtdata = result->data;
-
- if (PyDict_Check(other))
- otherdata = other;
- else if (PyAnySet_Check(other))
- otherdata = ((PySetObject *)other)->data;
- else {
- otherset = (PySetObject *)make_new_set(so->ob_type, other);
- if (otherset == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- otherdata = otherset->data;
- }
-
- while (PyDict_Next(so->data, &pos, &key, &value)) {
- if (!PyDict_Contains(otherdata, key)) {
- if (PyDict_SetItem(tgtdata, key, Py_True) == -1) {
- Py_XDECREF(otherset);
- return NULL;
- }
- }
- }
- Py_XDECREF(otherset);
- return (PyObject *)result;
- }
-
- PyDoc_STRVAR(difference_doc,
- "Return the difference of two sets as a new set.\n\
- \n\
- (i.e. all elements that are in this set but not the other.)");
-
- static PyObject *
set_difference_update(PySetObject *so, PyObject *other)
{
--- 358,361 ----
***************
*** 431,434 ****
--- 388,434 ----
"Remove all elements of another set from this set.");
+ static PyObject *
+ set_difference(PySetObject *so, PyObject *other)
+ {
+ PyObject *result, *tmp;
+ PyObject *otherdata, *tgtdata;
+ PyObject *key, *value;
+ int pos = 0;
+
+ if (PyDict_Check(other))
+ otherdata = other;
+ else if (PyAnySet_Check(other))
+ otherdata = ((PySetObject *)other)->data;
+ else {
+ result = set_copy(so);
+ if (result == NULL)
+ return result;
+ tmp = set_difference_update((PySetObject *)result, other);
+ if (tmp != NULL) {
+ Py_DECREF(tmp);
+ return result;
+ }
+ Py_DECREF(result);
+ return NULL;
+ }
+
+ result = make_new_set(so->ob_type, NULL);
+ if (result == NULL)
+ return NULL;
+ tgtdata = ((PySetObject *)result)->data;
+
+ while (PyDict_Next(so->data, &pos, &key, &value)) {
+ if (!PyDict_Contains(otherdata, key)) {
+ if (PyDict_SetItem(tgtdata, key, Py_True) == -1)
+ return NULL;
+ }
+ }
+ return result;
+ }
+
+ PyDoc_STRVAR(difference_doc,
+ "Return the difference of two sets as a new set.\n\
+ \n\
+ (i.e. all elements that are in this set but not the other.)");
static PyObject *
set_sub(PySetObject *so, PyObject *other)
More information about the Python-checkins
mailing list