Thomas Wouters <thomas@xs4all.net>:
authorFred Drake <fdrake@acm.org>
Thu, 15 Jun 2000 14:50:20 +0000 (14:50 +0000)
committerFred Drake <fdrake@acm.org>
Thu, 15 Jun 2000 14:50:20 +0000 (14:50 +0000)
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.

Objects/listobject.c
Objects/rangeobject.c
Objects/tupleobject.c

index 922444edc118517d865fc18e1924568968b33f80..3bb5aec33d69aae74078115e578743c53d8ab839 100644 (file)
@@ -1489,6 +1489,13 @@ PyTypeObject PyList_Type = {
        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 +1547,7 @@ static PySequenceMethods immutable_list_as_sequence = {
        (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 +1565,12 @@ static PyTypeObject immutable_list_type = {
        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 807cf51234b9298f6c2aff49feceb22b13304874..86144cad1cc05fe3fd9554008b3c5d48d789f4c9 100644 (file)
@@ -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*/
 };
index c26b7dfbf0deefb893d5e276e605180e74e18371..451e794543b7363af77db4d646dc78aac314fb1b 100644 (file)
@@ -447,6 +447,12 @@ PyTypeObject PyTuple_Type = {
        &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: