]> granicus.if.org Git - python/commitdiff
add list_contains and tuplecontains: efficient implementations of tp_contains
authorJeremy Hylton <jeremy@alum.mit.edu>
Thu, 27 Apr 2000 21:41:03 +0000 (21:41 +0000)
committerJeremy Hylton <jeremy@alum.mit.edu>
Thu, 27 Apr 2000 21:41:03 +0000 (21:41 +0000)
Objects/listobject.c
Objects/tupleobject.c

index 673028f31bb18b7522b5a2c74538ec4f44c977cd..005d7097cbcf68e4e215246d645148241fa46c7e 100644 (file)
@@ -306,6 +306,26 @@ list_length(a)
        return a->ob_size;
 }
 
+
+
+static int
+list_contains(a, el)
+       PyListObject *a;
+       PyObject *el;
+{
+       int i, cmp;
+
+       for (i = 0; i < a->ob_size; ++i) {
+               cmp = PyObject_Compare(el, PyList_GET_ITEM(a, i));
+               if (cmp == 0)
+                       return 1;
+               if (PyErr_Occurred())
+                       return -1;
+       }
+       return 0;
+}
+
+
 static PyObject *
 list_item(a, i)
        PyListObject *a;
@@ -1447,6 +1467,7 @@ static PySequenceMethods list_as_sequence = {
        (intintargfunc)list_slice, /*sq_slice*/
        (intobjargproc)list_ass_item, /*sq_ass_item*/
        (intintobjargproc)list_ass_slice, /*sq_ass_slice*/
+       (objobjproc)list_contains, /*sq_contains*/
 };
 
 PyTypeObject PyList_Type = {
index 98448bd5e0a6f922933332fd7ffd87a6738989ce..d1627a9a2c02c5dbbc0fff3ac46551654525023f 100644 (file)
@@ -281,6 +281,23 @@ tuplelength(a)
        return a->ob_size;
 }
 
+static int
+tuplecontains(a, el)
+       PyTupleObject *a;
+       PyObject *el;
+{
+       int i, cmp;
+
+       for (i = 0; i < a->ob_size; ++i) {
+               cmp = PyObject_Compare(el, PyTuple_GET_ITEM(a, i));
+               if (cmp == 0)
+                       return 1;
+               if (PyErr_Occurred())
+                       return -1;
+       }
+       return 0;
+}
+
 static PyObject *
 tupleitem(a, i)
        register PyTupleObject *a;
@@ -409,6 +426,7 @@ static PySequenceMethods tuple_as_sequence = {
        (intintargfunc)tupleslice, /*sq_slice*/
        0,              /*sq_ass_item*/
        0,              /*sq_ass_slice*/
+       (objobjproc)tuplecontains, /*sq_contains*/
 };
 
 PyTypeObject PyTuple_Type = {