[Python-checkins] python/dist/src/Objects setobject.c,1.10,1.11
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Sun Nov 23 21:57:35 EST 2003
Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv19524/Objects
Modified Files:
setobject.c
Log Message:
* Checkin remaining documentation
* Add more tests
* Refactor and neaten the code a bit.
* Rename union_update() to update().
* Improve the algorithms (making them a closer to sets.py).
Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/setobject.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** setobject.c 23 Nov 2003 02:49:05 -0000 1.10
--- setobject.c 24 Nov 2003 02:57:33 -0000 1.11
***************
*** 13,20 ****
make_new_set(PyTypeObject *type, PyObject *iterable)
{
! PyObject *data;
PyObject *it = NULL;
PyObject *item;
! PySetObject *so;
/* Get iterator. */
--- 13,20 ----
make_new_set(PyTypeObject *type, PyObject *iterable)
{
! PyObject *data = NULL;
PyObject *it = NULL;
PyObject *item;
! PySetObject *so = NULL;
/* Get iterator. */
***************
*** 22,58 ****
it = PyObject_GetIter(iterable);
if (it == NULL)
! return NULL;
}
data = PyDict_New();
! if (data == NULL) {
! Py_DECREF(it);
! return NULL;
! }
while (it != NULL && (item = PyIter_Next(it)) != NULL) {
if (PyDict_SetItem(data, item, Py_True) == -1) {
- Py_DECREF(it);
- Py_DECREF(data);
Py_DECREF(item);
! return NULL;
}
Py_DECREF(item);
}
! Py_XDECREF(it);
! if (PyErr_Occurred()) {
! Py_DECREF(data);
! return NULL;
! }
/* create PySetObject structure */
so = (PySetObject *)type->tp_alloc(type, 0);
! if (so == NULL) {
! Py_DECREF(data);
! return NULL;
! }
so->data = data;
so->hash = -1;
return (PyObject *)so;
}
--- 22,54 ----
it = PyObject_GetIter(iterable);
if (it == NULL)
! goto done;
}
data = PyDict_New();
! if (data == NULL)
! goto done;
while (it != NULL && (item = PyIter_Next(it)) != NULL) {
if (PyDict_SetItem(data, item, Py_True) == -1) {
Py_DECREF(item);
! goto done;
}
Py_DECREF(item);
}
! if (PyErr_Occurred())
! goto done;
/* create PySetObject structure */
so = (PySetObject *)type->tp_alloc(type, 0);
! if (so == NULL)
! goto done;
!
! Py_INCREF(data);
so->data = data;
so->hash = -1;
+ done:
+ Py_XDECREF(data);
+ Py_XDECREF(it);
return (PyObject *)so;
}
***************
*** 65,69 ****
if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
return NULL;
! if (iterable != NULL && iterable->ob_type == &PyFrozenSet_Type) {
Py_INCREF(iterable);
return iterable;
--- 61,65 ----
if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
return NULL;
! if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
Py_INCREF(iterable);
return iterable;
***************
*** 162,166 ****
frozenset_copy(PySetObject *so)
{
! if (so->ob_type == &PyFrozenSet_Type) {
Py_INCREF(so);
return (PyObject *)so;
--- 158,162 ----
frozenset_copy(PySetObject *so)
{
! if (PyFrozenSet_CheckExact(so)) {
Py_INCREF(so);
return (PyObject *)so;
***************
*** 172,221 ****
static PyObject *
- set_union(PySetObject *so, PyObject *other)
- {
- PySetObject *result;
- PyObject *item, *data, *it;
-
- result = (PySetObject *)set_copy(so);
- if (result == NULL)
- return NULL;
-
- if (PyAnySet_Check(other)) {
- if (PyDict_Merge(result->data, ((PySetObject *)other)->data, 0) == -1) {
- Py_DECREF(result);
- return NULL;
- }
- return (PyObject *)result;
- }
-
- it = PyObject_GetIter(other);
- if (it == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- data = result->data;
- while ((item = PyIter_Next(it)) != NULL) {
- if (PyDict_SetItem(data, item, Py_True) == -1) {
- Py_DECREF(it);
- Py_DECREF(result);
- Py_DECREF(item);
- return NULL;
- }
- Py_DECREF(item);
- }
- Py_DECREF(it);
- if (PyErr_Occurred()) {
- Py_DECREF(result);
- return NULL;
- }
- return (PyObject *)result;
- }
-
- PyDoc_STRVAR(union_doc,
- "Return the union of two sets as a new set.\n\
- \n\
- (i.e. all elements that are in either set.)");
-
- static PyObject *
set_union_update(PySetObject *so, PyObject *other)
{
--- 168,171 ----
***************
*** 225,230 ****
if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
return NULL;
! Py_INCREF(so);
! return (PyObject *)so;
}
--- 175,179 ----
if (PyDict_Merge(so->data, ((PySetObject *)other)->data, 0) == -1)
return NULL;
! Py_RETURN_NONE;
}
***************
*** 252,255 ****
--- 201,227 ----
static PyObject *
+ set_union(PySetObject *so, PyObject *other)
+ {
+ PySetObject *result;
+ PyObject *rv;
+
+ result = (PySetObject *)set_copy(so);
+ if (result == NULL)
+ return NULL;
+ rv = set_union_update(result, other);
+ if (rv == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ Py_DECREF(rv);
+ return (PyObject *)result;
+ }
+
+ PyDoc_STRVAR(union_doc,
+ "Return the union of two sets as a new set.\n\
+ \n\
+ (i.e. all elements that are in either set.)");
+
+ static PyObject *
set_or(PySetObject *so, PyObject *other)
{
***************
*** 287,291 ****
if (result == NULL)
return NULL;
!
it = PyObject_GetIter(other);
if (it == NULL) {
--- 259,275 ----
if (result == NULL)
return NULL;
! tgtdata = result->data;
! selfdata = so->data;
!
! if (PyAnySet_Check(other) &&
! PyDict_Size(((PySetObject *)other)->data) > PyDict_Size(selfdata)) {
! selfdata = ((PySetObject *)other)->data;
! other = (PyObject *)so;
! } else if (PyDict_Check(other) &&
! PyDict_Size(other) > PyDict_Size(selfdata)) {
! selfdata = other;
! other = so->data;
! }
!
it = PyObject_GetIter(other);
if (it == NULL) {
***************
*** 294,299 ****
}
- selfdata = so->data;
- tgtdata = result->data;
while ((item = PyIter_Next(it)) != NULL) {
if (PySequence_Contains(selfdata, item)) {
--- 278,281 ----
***************
*** 391,415 ****
set_difference(PySetObject *so, PyObject *other)
{
! PySetObject *result;
! PyObject *item, *tgtdata, *it;
! result = (PySetObject *)set_copy(so);
if (result == NULL)
return NULL;
!
! it = PyObject_GetIter(other);
if (it == NULL) {
Py_DECREF(result);
return NULL;
}
- tgtdata = result->data;
while ((item = PyIter_Next(it)) != NULL) {
! if (PyDict_DelItem(tgtdata, item) == -1) {
! if (PyErr_ExceptionMatches(PyExc_KeyError))
! PyErr_Clear();
! else {
Py_DECREF(it);
- Py_DECREF(result);
Py_DECREF(item);
return NULL;
--- 373,409 ----
set_difference(PySetObject *so, PyObject *other)
{
! PySetObject *result, *otherset=NULL;
! PyObject *item, *otherdata, *tgtdata, *it;
! 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;
! }
!
! it = PyObject_GetIter(so->data);
if (it == NULL) {
+ Py_XDECREF(otherset);
Py_DECREF(result);
return NULL;
}
while ((item = PyIter_Next(it)) != NULL) {
! if (!PySequence_Contains(otherdata, item)) {
! if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
! Py_XDECREF(otherset);
Py_DECREF(it);
Py_DECREF(item);
return NULL;
***************
*** 419,422 ****
--- 413,417 ----
}
Py_DECREF(it);
+ Py_XDECREF(otherset);
if (PyErr_Occurred()) {
Py_DECREF(result);
***************
*** 490,505 ****
static PyObject *
! set_symmetric_difference(PySetObject *so, PyObject *other)
{
! PySetObject *result, *otherset=NULL;
! PyObject *item, *selfdata, *otherdata, *tgtdata, *it;
selfdata = so->data;
- result = (PySetObject *)set_copy(so);
- if (result == NULL)
- return NULL;
- tgtdata = result->data;
-
if (PyDict_Check(other))
otherdata = other;
--- 485,495 ----
static PyObject *
! set_symmetric_difference_update(PySetObject *so, PyObject *other)
{
! PyObject *item, *selfdata, *it, *otherdata;
! PySetObject *otherset = NULL;
selfdata = so->data;
if (PyDict_Check(other))
otherdata = other;
***************
*** 508,559 ****
else {
otherset = (PySetObject *)make_new_set(so->ob_type, other);
! if (otherset == NULL) {
! Py_DECREF(result);
return NULL;
- }
otherdata = otherset->data;
! }
it = PyObject_GetIter(otherdata);
! if (it == NULL) {
! Py_XDECREF(otherset);
! Py_DECREF(result);
return NULL;
- }
while ((item = PyIter_Next(it)) != NULL) {
! if (PyDict_DelItem(tgtdata, item) == -1) {
! PyErr_Clear();
! if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
Py_DECREF(it);
Py_XDECREF(otherset);
! Py_DECREF(result);
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;
}
! PyDoc_STRVAR(symmetric_difference_doc,
! "Return the symmetric difference of two sets as a new set.\n\
! \n\
! (i.e. all elements that are in exactly one of the sets.)");
static PyObject *
! set_symmetric_difference_update(PySetObject *so, PyObject *other)
{
! PyObject *item, *selfdata, *it, *otherdata;
! PySetObject *otherset = NULL;
!
! selfdata = so->data;
if (PyDict_Check(other))
--- 498,543 ----
else {
otherset = (PySetObject *)make_new_set(so->ob_type, other);
! if (otherset == NULL)
return NULL;
otherdata = otherset->data;
! }
it = PyObject_GetIter(otherdata);
! if (it == NULL)
return NULL;
while ((item = PyIter_Next(it)) != NULL) {
! if (PySequence_Contains(selfdata, item)) {
! if (PyDict_DelItem(selfdata, item) == -1) {
! Py_XDECREF(otherset);
Py_DECREF(it);
+ Py_DECREF(item);
+ return NULL;
+ }
+ } else {
+ if (PyDict_SetItem(selfdata, item, 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;
}
! PyDoc_STRVAR(symmetric_difference_update_doc,
! "Update a set with the symmetric difference of itself and another.");
static PyObject *
! set_symmetric_difference(PySetObject *so, PyObject *other)
{
! PySetObject *result;
! PyObject *item, *selfdata, *otherdata, *tgtdata, *it, *rv, *otherset;
if (PyDict_Check(other))
***************
*** 562,586 ****
otherdata = ((PySetObject *)other)->data;
else {
! otherset = (PySetObject *)make_new_set(so->ob_type, other);
if (otherset == NULL)
return NULL;
! otherdata = otherset->data;
! }
! it = PyObject_GetIter(otherdata);
! if (it == NULL)
return NULL;
while ((item = PyIter_Next(it)) != NULL) {
! if (PySequence_Contains(selfdata, item)) {
! if (PyDict_DelItem(selfdata, item) == -1) {
! Py_XDECREF(otherset);
Py_DECREF(it);
Py_DECREF(item);
return NULL;
}
! } else {
! if (PyDict_SetItem(selfdata, item, Py_True) == -1) {
! Py_XDECREF(otherset);
Py_DECREF(it);
Py_DECREF(item);
--- 546,594 ----
otherdata = ((PySetObject *)other)->data;
else {
! otherset = make_new_set(so->ob_type, other);
if (otherset == NULL)
return NULL;
! rv = set_symmetric_difference_update((PySetObject *)otherset, (PyObject *)so);
! if (rv == NULL)
! return NULL;
! Py_DECREF(rv);
! return otherset;
! }
! result = (PySetObject *)make_new_set(so->ob_type, NULL);
! if (result == NULL)
return NULL;
+ 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 (!PySequence_Contains(selfdata, item)) {
! if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
Py_DECREF(it);
Py_DECREF(item);
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 (!PySequence_Contains(otherdata, item)) {
! if (PyDict_SetItem(tgtdata, item, Py_True) == -1) {
Py_DECREF(it);
Py_DECREF(item);
***************
*** 590,602 ****
Py_DECREF(item);
}
- Py_XDECREF(otherset);
Py_DECREF(it);
! if (PyErr_Occurred())
return NULL;
! Py_RETURN_NONE;
}
! PyDoc_STRVAR(symmetric_difference_update_doc,
! "Update a set with the symmetric difference of itself and another.");
static PyObject *
--- 598,614 ----
Py_DECREF(item);
}
Py_DECREF(it);
! if (PyErr_Occurred()) {
! Py_DECREF(result);
return NULL;
! }
!
! return (PyObject *)result;
}
! PyDoc_STRVAR(symmetric_difference_doc,
! "Return the symmetric difference of two sets as a new set.\n\
! \n\
! (i.e. all elements that are in exactly one of the sets.)");
static PyObject *
***************
*** 1013,1017 ****
{"union", (PyCFunction)set_union, METH_O,
union_doc},
! {"union_update",(PyCFunction)set_union_update, METH_O,
union_update_doc},
{NULL, NULL} /* sentinel */
--- 1025,1029 ----
{"union", (PyCFunction)set_union, METH_O,
union_doc},
! {"update", (PyCFunction)set_union_update, METH_O,
union_update_doc},
{NULL, NULL} /* sentinel */
***************
*** 1160,1165 ****
"frozenset", /* tp_name */
sizeof(PySetObject), /* tp_basicsize */
! 0, /* tp_itemsize */
! /* methods */
(destructor)set_dealloc, /* tp_dealloc */
(printfunc)set_tp_print, /* tp_print */
--- 1172,1176 ----
"frozenset", /* tp_name */
sizeof(PySetObject), /* tp_basicsize */
! 0, /* tp_itemsize */ /* methods */
(destructor)set_dealloc, /* tp_dealloc */
(printfunc)set_tp_print, /* tp_print */
More information about the Python-checkins
mailing list