summaryrefslogtreecommitdiffstats
path: root/Objects/rangeobject.c
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2000-06-15 14:50:20 (GMT)
committerFred Drake <fdrake@acm.org>2000-06-15 14:50:20 (GMT)
commit56780257c624996758237f96ce59fe3068f86deb (patch)
tree3dfc09ddbac0f610d3fba43245eac1155e6c04af /Objects/rangeobject.c
parent60bc809d9a80ec12530d224cc680bae42c4de285 (diff)
downloadcpython-56780257c624996758237f96ce59fe3068f86deb.zip
cpython-56780257c624996758237f96ce59fe3068f86deb.tar.gz
cpython-56780257c624996758237f96ce59fe3068f86deb.tar.bz2
Thomas Wouters <thomas@xs4all.net>:
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.
Diffstat (limited to 'Objects/rangeobject.c')
-rw-r--r--Objects/rangeobject.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index 807cf51..86144ca 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -237,6 +237,23 @@ range_getattr(r, name)
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 @@ static PySequenceMethods range_as_sequence = {
(intintargfunc)range_slice, /*sq_slice*/
0, /*sq_ass_item*/
0, /*sq_ass_slice*/
+ (objobjproc)range_contains, /*sq_contains*/
};
PyTypeObject PyRange_Type = {
@@ -262,4 +280,11 @@ PyTypeObject PyRange_Type = {
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*/
};