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

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed Nov 12 21:34:17 EST 2003


Update of /cvsroot/python/python/nondist/sandbox/setobj
In directory sc8-pr-cvs1:/tmp/cvs-serv3886
Modified Files:
	setobject.c test_set.py 
Log Message:
Add intersection.
Index: setobject.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/setobj/setobject.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** setobject.c	12 Nov 2003 22:46:45 -0000	1.2
--- setobject.c	13 Nov 2003 02:34:15 -0000	1.3
***************
*** 14,17 ****
--- 14,22 ----
 */
 
+ /* Fast dictionary access macros */ 
+ 
+ #define DICT_CONTAINS(d, k) d->ob_type->tp_as_sequence->sq_contains(d, k)
+ 
+ 
 /* set object **********************************************************/
 
***************
*** 23,42 ****
 static PyTypeObject set_type;
 
 static PyObject *
! set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	PyObject *data;
 	PyObject *it;
- 	PyObject *iterable = NULL;
 	PyObject *item;
 	setobject *so;
 
- 	if (!PyArg_UnpackTuple(args, "set", 0, 1, &iterable))
- 		return NULL;
- 
 	/* Get iterator. */
! 	it = PyObject_GetIter(iterable);
! 	if (it == NULL)
! 		return NULL;
 
 	data = PyDict_New();
--- 28,46 ----
 static PyTypeObject set_type;
 
+ 
 static PyObject *
! make_new_set(PyTypeObject *type, PyObject *iterable)
 {
 	PyObject *data;
 	PyObject *it;
 	PyObject *item;
 	setobject *so;
 
 	/* Get iterator. */
! 	if (iterable != NULL) {
! 		it = PyObject_GetIter(iterable);
! 		if (it == NULL)
! 			return NULL;
! 	}
 
 	data = PyDict_New();
***************
*** 72,75 ****
--- 76,89 ----
 }
 
+ static PyObject *
+ set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+ 	PyObject *iterable = NULL;
+ 
+ 	if (!PyArg_UnpackTuple(args, "set", 0, 1, &iterable))
+ 		return NULL;
+ 	return make_new_set(type, iterable);
+ }
+ 
 static void
 set_dealloc(setobject *so)
***************
*** 103,107 ****
 set_contains(setobject *so, PyObject *key)
 {
! 	return so->data->ob_type->tp_as_sequence->sq_contains(so->data, key);
 }
 
--- 117,122 ----
 set_contains(setobject *so, PyObject *key)
 {
! 	return DICT_CONTAINS(so->data, key);
! 	//return so->data->ob_type->tp_as_sequence->sq_contains(so->data, key);
 }
 
***************
*** 160,163 ****
--- 175,218 ----
 (i.e. all elements that are in either set.)\n");
 
+ static PyObject *
+ set_intersection(setobject *so, PyObject *other)
+ {
+ 	setobject *result;
+ 	PyObject *item, *selfdata, *tgtdata, *it;
+ 
+ 	result = make_new_set(&set_type, NULL);
+ 	if (result == NULL)
+ 		return NULL;
+ 	
+ 	it = PyObject_GetIter(other);
+ 	if (it == NULL) {
+ 		Py_DECREF(result);
+ 		return NULL;
+ 	}
+ 
+ 	selfdata = so->data;
+ 	tgtdata = result->data;
+ 	while ((item = PyIter_Next(it)) != NULL) {
+ 		if (DICT_CONTAINS(selfdata, item)) {
+ 			if (PyDict_SetItem(tgtdata, item, Py_None) == -1) {
+ 				Py_DECREF(it);
+ 				Py_DECREF(result);
+ 				PyErr_SetString(PyExc_ValueError,
+ 						"all set entries must be immutable");
+ 				return NULL;
+ 			} 
+ 		}
+ 		Py_DECREF(item);
+ 	}
+ 	Py_DECREF(it);
+ 	return (PyObject *)result;
+ }
+ 
+ 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");
+ 
+ 
 static PySequenceMethods set_as_sequence = {
 	(inquiry)set_len,		/* sq_length */
***************
*** 176,179 ****
--- 231,236 ----
 	{"__copy__",	(PyCFunction)set_copy,		METH_NOARGS,
 	 copy_doc},
+ 	{"intersection",(PyCFunction)set_intersection,	METH_O,
+ 	 intersection_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.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** test_set.py	12 Nov 2003 22:46:45 -0000	1.2
--- test_set.py	13 Nov 2003 02:34:15 -0000	1.3
***************
*** 29,32 ****
--- 29,36 ----
 self.assertEqual(c in u, c in self.d or c in self.otherword)
 
+ def test_intersection(self):
+ i = self.s.intersection(self.otherword)
+ for c in self.letters:
+ self.assertEqual(c in i, c in self.d and c in self.otherword)
 
 def test_main(verbose=None):


More information about the Python-checkins mailing list

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