[Python-checkins] python/nondist/sandbox/setobj setobject.c, 1.8, 1.9 test_set.py, 1.8, 1.9

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Thu Nov 13 17:24:00 EST 2003


Update of /cvsroot/python/python/nondist/sandbox/setobj
In directory sc8-pr-cvs1:/tmp/cvs-serv1423
Modified Files:
	setobject.c test_set.py 
Log Message:
Add issubset(), issuperset() and the related comparison operators.
Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/setobject.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** setobject.c	13 Nov 2003 20:59:12 -0000	1.8
--- setobject.c	13 Nov 2003 22:23:58 -0000	1.9
***************
*** 16,20 ****
 /* Fast dictionary access macros */ 
 
! #define DICT_CONTAINS(d, k) d->ob_type->tp_as_sequence->sq_contains(d, k)
 #define IS_SET(so)	(so->ob_type == &set_type || so->ob_type == &frozenset_type)
 
--- 16,20 ----
 /* Fast dictionary access macros */ 
 
! #define DICT_CONTAINS(d, k) (d->ob_type->tp_as_sequence->sq_contains(d, k))
 #define IS_SET(so)	(so->ob_type == &set_type || so->ob_type == &frozenset_type)
 
***************
*** 167,171 ****
 
 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.)\n");
--- 167,171 ----
 
 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.)\n");
***************
*** 226,230 ****
 
 PyDoc_STRVAR(intersection_doc,
! """Return the intersection of two sets as a new set.\n\
 \n\
 (i.e. all elements that are in both sets.)\n");
--- 226,230 ----
 
 PyDoc_STRVAR(intersection_doc,
! "Return the intersection of two sets as a new set.\n\
 \n\
 (i.e. all elements that are in both sets.)\n");
***************
*** 258,262 ****
 
 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");
--- 258,262 ----
 
 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");
***************
*** 319,323 ****
 
 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");
--- 319,323 ----
 
 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");
***************
*** 333,336 ****
--- 333,382 ----
 }
 
+ PyObject *
+ set_issubset(setobject *so, PyObject *other)
+ {
+ 	PyObject *otherdata, *it, *item;
+ 
+ 	if (!IS_SET(other)) {
+ 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
+ 		return NULL;
+ 	}
+ 	if (set_len(so) > set_len((setobject *)other)) {
+ 		Py_INCREF(Py_False);
+ 		return Py_False;
+ 	}
+ 
+ 	it = PyObject_GetIter(so->data);
+ 	if (it == NULL)
+ 		return NULL;
+ 
+ 	otherdata = ((setobject *)other)->data;
+ 	while ((item = PyIter_Next(it)) != NULL) {
+ 		if (!DICT_CONTAINS(otherdata, item)) {
+ 			Py_DECREF(it);
+ 			Py_DECREF(item);
+ 			Py_INCREF(Py_False);
+ 			return Py_False;
+ 		}
+ 		Py_DECREF(item);
+ 	}
+ 	Py_INCREF(Py_True);
+ 	return Py_True;
+ }
+ 
+ PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
+ 
+ PyObject *
+ set_issuperset(setobject *so, PyObject *other)
+ {
+ 	if (!IS_SET(other)) {
+ 		PyErr_SetString(PyExc_TypeError, "can only compare to a set");
+ 		return NULL;
+ 	}
+ 	return set_issubset((setobject *)other, (PyObject *)so);
+ }
+ 
+ PyDoc_STRVAR(issuperset_doc, "Report whether another set contains this set.");
+ 
 static long
 set_nohash(PyObject *self)
***************
*** 369,384 ****
 {
 	PyObject *res;
! 		
 	if (op == Py_EQ && !IS_SET(w))
 		res = Py_False;
 	else if (op == Py_NE && !IS_SET(w))
 		res = Py_True;
! 	else if (op == Py_EQ || op == Py_NE)
 		return PyObject_RichCompare(((setobject *)v)->data, 
 			((setobject *)w)->data, op);
! 	else
! 		res = Py_NotImplemented;
! 	Py_INCREF(res);
! 	return res;
 }
 
--- 415,448 ----
 {
 	PyObject *res;
! 
 	if (op == Py_EQ && !IS_SET(w))
 		res = Py_False;
 	else if (op == Py_NE && !IS_SET(w))
 		res = Py_True;
! 
! 	switch (op) {
! 	case Py_EQ:
! 	case Py_NE:
 		return PyObject_RichCompare(((setobject *)v)->data, 
 			((setobject *)w)->data, op);
! 	case Py_LE:
! 		return set_issubset((setobject *)v, w);
! 	case Py_GE:
! 		return set_issuperset((setobject *)v, w);
! 	case Py_LT:
! 		if (set_len(v) >= set_len((setobject *)w)) {
! 			Py_INCREF(Py_False);
! 			return Py_False;
! 		}
! 		return set_issubset((setobject *)v, w);
! 	case Py_GT:
! 		if (set_len(v) <= set_len((setobject *)w)) {
! 			Py_INCREF(Py_False);
! 			return Py_False;
! 		}
! 		return set_issuperset((setobject *)v, w);
! 	}
! 	Py_INCREF(Py_NotImplemented);
! 	return Py_NotImplemented;
 }
 
***************
*** 420,423 ****
--- 484,491 ----
 	{"intersection",(PyCFunction)set_intersection,	METH_O,
 	 intersection_doc},
+ 	{"issubset",(PyCFunction)set_issubset,		METH_O,
+ 	 issubset_doc},
+ 	{"issuperset",(PyCFunction)set_issuperset,	METH_O,
+ 	 issuperset_doc},
 	{"symmetric_difference",(PyCFunction)set_symmetric_difference,	METH_O,
 	 symmetric_difference_doc},
***************
*** 509,512 ****
--- 577,584 ----
 	{"intersection",(PyCFunction)set_intersection,	METH_O,
 	 intersection_doc},
+ 	{"issubset",(PyCFunction)set_issubset,		METH_O,
+ 	 issubset_doc},
+ 	{"issuperset",(PyCFunction)set_issuperset,	METH_O,
+ 	 issuperset_doc},
 	{"symmetric_difference",(PyCFunction)set_symmetric_difference,	METH_O,
 	 symmetric_difference_doc},
Index: test_set.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/test_set.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** test_set.py	13 Nov 2003 20:59:12 -0000	1.8
--- test_set.py	13 Nov 2003 22:23:58 -0000	1.9
***************
*** 103,106 ****
--- 103,118 ----
 self.assertRaises(TypeError, self.s.__cmp__, self.s)
 
+ def test_sub_and_super(self):
+ p, q, r = map(self.thetype, ['ab', 'abcde', 'def'])
+ self.assert_(p < q)
+ self.assert_(p <= q)
+ self.assert_(q <= q)
+ self.assert_(q > p)
+ self.assert_(q >= p)
+ self.failIf(q < r)
+ self.failIf(q <= r)
+ self.failIf(q > r)
+ self.failIf(q >= r)
+ 
 class TestSet(TestJointOps):
 thetype = set


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /