[Patches] sq_contains support for listobject and rangeobject

Thomas Wouters thomas@xs4all.net
Thu, 15 Jun 2000 14:45:23 +0200


--VS++wcV0S1rZb1Fb
Content-Type: text/plain; charset=us-ascii


The following patch adds 'sq_contains' support to rangeobject, and enables
the already-written support for sq_contains in listobject and tupleobject.

The rangeobject 'contains' code should be a bit more efficient than the
current default 'in' implementation ;-) It might not get used much, but it's
not that much to add.

listobject.c and tupleobject.c already had code for sq_contains, and the
proper struct member was set, but the PyType structure was not extended to
include tp_flags, so the object-specific code was not getting called (Go
ahead, test it ;-). I also did this for the immutable_list_type in
listobject.c, eventhough it is probably never used. Symmetry and all that.

The patch is so small I thought it would be OK to submit these three patches
in one posting, with one attachement ;P

	I confirm that, to the best of my knowledge and belief, this
	contribution is free of any claims of third parties under copyright,
	patent or other rights or interests ("claims").  To the extent that
	I have any such claims, I hereby grant to CNRI a nonexclusive,
	irrevocable, royalty-free, worldwide license to reproduce,
	distribute, perform and/or display publicly, prepare derivative
	versions, and otherwise use this contribution as part of the Python
	software and its related documentation, or any derivative versions
	thereof, at no cost to CNRI or its licensed users, and to authorize
	others to do so.

	I acknowledge that CNRI may, at its sole discretion, decide whether
	or not to incorporate this contribution in the Python software and
	its related documentation.  I further grant CNRI permission to use
	my name and other identifying information provided to CNRI by me for
	use in connection with the Python software and its related
	documentation.
-ly y'rs,

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

--VS++wcV0S1rZb1Fb
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="contains.diff"

Index: Objects/listobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v
retrieving revision 2.71
diff -u -r2.71 listobject.c
--- Objects/listobject.c	2000/06/01 14:31:03	2.71
+++ Objects/listobject.c	2000/06/15 12:41:53
@@ -1489,6 +1490,13 @@
 	0,		/*tp_as_number*/
 	&list_as_sequence,	/*tp_as_sequence*/
 	0,		/*tp_as_mapping*/
+	0,		/*tp_hash*/
+	0,		/*tp_call*/
+	0,		/*tp_str*/
+	0,		/*tp_getattro*/
+	0,		/*tp_setattro*/
+	0,		/*tp_as_buffer*/
+	Py_TPFLAGS_DEFAULT,	/*tp_flags*/
 };
 
 
@@ -1540,6 +1548,7 @@
 	(intintargfunc)list_slice, /*sq_slice*/
 	(intobjargproc)immutable_list_ass, /*sq_ass_item*/
 	(intintobjargproc)immutable_list_ass, /*sq_ass_slice*/
+	(objobjproc)list_contains, /*sq_contains*/
 };
 
 static PyTypeObject immutable_list_type = {
@@ -1557,5 +1566,12 @@
 	0,		/*tp_as_number*/
 	&immutable_list_as_sequence,	/*tp_as_sequence*/
 	0,		/*tp_as_mapping*/
+	0,		/*tp_hash*/
+	0,		/*tp_call*/
+	0,		/*tp_str*/
+	0,		/*tp_getattro*/
+	0,		/*tp_setattro*/
+	0,		/*tp_as_buffer*/
+	Py_TPFLAGS_DEFAULT,	/*tp_flags*/
 };
 
Index: Objects/rangeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/rangeobject.c,v
retrieving revision 2.12
diff -u -r2.12 rangeobject.c
--- Objects/rangeobject.c	2000/05/03 23:44:35	2.12
+++ Objects/rangeobject.c	2000/06/15 12:41:53
@@ -237,6 +237,23 @@
 	return Py_FindMethod(range_methods, (PyObject *) r, name);
 }
 
+static int
+range_contains(r, obj)
+	rangeobject * r;
+	PyObject * obj;
+{
+	long num = PyInt_AsLong(obj);
+	
+	if (num < 0 && PyErr_Occurred())
+		return -1;
+	
+	if (num < r->start || (num - r->start) % r->step)
+		return 0;
+	if (num > (r->start + (r->len * r->step)))
+		return 0;
+	return 1;
+}
+
 static PySequenceMethods range_as_sequence = {
 	(inquiry)range_length, /*sq_length*/
 	(binaryfunc)range_concat, /*sq_concat*/
@@ -245,6 +262,7 @@
 	(intintargfunc)range_slice, /*sq_slice*/
 	0,		/*sq_ass_item*/
 	0,		/*sq_ass_slice*/
+	(objobjproc)range_contains, /*sq_contains*/
 };
 
 PyTypeObject PyRange_Type = {
@@ -262,4 +280,12 @@
 	0,			/*tp_as_number*/
 	&range_as_sequence,	/*tp_as_sequence*/
 	0,			/*tp_as_mapping*/
+	0,			/*tp_hash*/
+	0,			/*tp_call*/
+	0,			/*tp_str*/
+	0,			/*tp_getattro*/
+	0,			/*tp_setattro*/
+	0,			/*tp_as_buffer*/
+	Py_TPFLAGS_DEFAULT,	/*tp_flags*/
 };
+c
\ No newline at end of file
Index: Objects/tupleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/tupleobject.c,v
retrieving revision 2.34
diff -u -r2.34 tupleobject.c
--- Objects/tupleobject.c	2000/06/01 03:12:13	2.34
+++ Objects/tupleobject.c	2000/06/15 12:41:54
@@ -447,6 +447,12 @@
 	&tuple_as_sequence,	/*tp_as_sequence*/
 	0,		/*tp_as_mapping*/
 	(hashfunc)tuplehash, /*tp_hash*/
+	0,		/*tp_call*/
+	0,		/*tp_str*/
+	0,		/*tp_getattro*/
+	0,		/*tp_setattro*/
+	0,		/*tp_as_buffer*/
+	Py_TPFLAGS_DEFAULT, /*tp_flags*/
 };
 
 /* The following function breaks the notion that tuples are immutable:

--VS++wcV0S1rZb1Fb--