[Python-checkins] python/nondist/sandbox/setobj setobject.c, 1.3,
1.4 test_set.py, 1.3, 1.4
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Thu Nov 13 06:31:08 EST 2003
Update of /cvsroot/python/python/nondist/sandbox/setobj
In directory sc8-pr-cvs1:/tmp/cvs-serv24710
Modified Files:
setobject.c test_set.py
Log Message:
Add difference and symmetric_difference
Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/setobject.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** setobject.c 13 Nov 2003 02:34:15 -0000 1.3
--- setobject.c 13 Nov 2003 11:31:04 -0000 1.4
***************
*** 154,161 ****
}
data = result->data;
! while (1) {
! item = PyIter_Next(it);
! if (item == NULL)
! break; // XXX add error checking
if (PyDict_SetItem(data, item, Py_None) == -1) {
Py_DECREF(it);
--- 154,158 ----
}
data = result->data;
! while ((item = PyIter_Next(it)) != NULL) {
if (PyDict_SetItem(data, item, Py_None) == -1) {
Py_DECREF(it);
***************
*** 181,185 ****
PyObject *item, *selfdata, *tgtdata, *it;
! result = make_new_set(&set_type, NULL);
if (result == NULL)
return NULL;
--- 178,182 ----
PyObject *item, *selfdata, *tgtdata, *it;
! result = (setobject *)make_new_set(&set_type, NULL);
if (result == NULL)
return NULL;
***************
*** 215,218 ****
--- 212,297 ----
+ static PyObject *
+ set_difference(setobject *so, PyObject *other)
+ {
+ setobject *result;
+ PyObject *item, *tgtdata, *it;
+
+ result = (setobject *)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)
+ PyErr_Clear();
+ Py_DECREF(item);
+ }
+ Py_DECREF(it);
+ 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 the first set but not the second.)\n");
+
+ static PyObject *
+ set_symmetric_difference(setobject *so, PyObject *other)
+ {
+ setobject *result, *otherset;
+ PyObject *item, *selfdata, *otherdata, *tgtdata, *it;
+
+ selfdata = so->data;
+
+ result = (setobject *)set_copy(so);
+ if (result == NULL)
+ return NULL;
+ tgtdata = result->data;
+
+ otherset = (setobject *)make_new_set(&set_type, other);
+ if (otherset == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ otherdata = otherset->data;
+
+ it = PyObject_GetIter(otherdata);
+ if (it == NULL) {
+ Py_DECREF(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_None) == -1) {
+ Py_DECREF(it);
+ Py_DECREF(otherset);
+ Py_DECREF(result);
+ PyErr_SetString(PyExc_ValueError,
+ "all set entries must be immutable");
+ return NULL;
+ }
+ }
+ Py_DECREF(item);
+ }
+
+ Py_DECREF(otherset);
+ 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.)\n");
+
static PySequenceMethods set_as_sequence = {
(inquiry)set_len, /* sq_length */
***************
*** 231,236 ****
--- 310,319 ----
{"__copy__", (PyCFunction)set_copy, METH_NOARGS,
copy_doc},
+ {"difference",(PyCFunction)set_difference, METH_O,
+ difference_doc},
{"intersection",(PyCFunction)set_intersection, METH_O,
intersection_doc},
+ {"symmetric_difference",(PyCFunction)set_symmetric_difference, METH_O,
+ symmetric_difference_doc},
{"union", (PyCFunction)set_union, METH_O,
union_doc},
Index: test_set.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/test_set.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** test_set.py 13 Nov 2003 02:34:15 -0000 1.3
--- test_set.py 13 Nov 2003 11:31:04 -0000 1.4
***************
*** 34,37 ****
--- 34,47 ----
self.assertEqual(c in i, c in self.d and c in self.otherword)
+ def test_difference(self):
+ i = self.s.difference(self.otherword)
+ for c in self.letters:
+ self.assertEqual(c in i, c in self.d and c not in self.otherword)
+
+ def test_symmetric_difference(self):
+ i = self.s.symmetric_difference(self.otherword)
+ for c in self.letters:
+ self.assertEqual(c in i, (c in self.d) ^ (c in self.otherword))
+
def test_main(verbose=None):
from test import test_sets
More information about the Python-checkins
mailing list