[Zope-Checkins] CVS: Zope3/lib/python/Persistence/BTrees - BTreeTemplate.c:1.27 BucketTemplate.c:1.10 SetTemplate.c:1.4 TreeSetTemplate.c:1.3

Tim Peters tim.one@comcast.net
Tue, 18 Jun 2002 15:43:14 -0400


Update of /cvs-repository/Zope3/lib/python/Persistence/BTrees
In directory cvs.zope.org:/tmp/cvs-serv25027

Modified Files:
	BTreeTemplate.c BucketTemplate.c SetTemplate.c 
	TreeSetTemplate.c 
Log Message:
Implemented the new-in-Python-2.2 sq_contains slot for Sets, Buckets,
TreeSets, and BTrees.  This allows writing:

    if key in set_bucket_treeset_or_btree:
    if key not in set_bucket_treeset_or_btree:

under Python 2.2 or later.  They're the same as:

    if set_bucket_treeset_or_btree.has_key(key):
    if not set_bucket_treeset_or_btree.has_key(key):

but are a little quicker because the former way avoids needing to load
the "has_key" string and doing a general method lookup.  Note that the
sq_contains slot implements a true Boolean (True/False in 2.3; 1/0 in
2.2) function, while the BTree has_key method actually returns the
depth of the bucket in which a key is found.


=== Zope3/lib/python/Persistence/BTrees/BTreeTemplate.c 1.26 => 1.27 ===
 }
 
-/*
-** BTree_has_key
-*/
 static PyObject *
 BTree_has_key(BTree *self, PyObject *key)
 {
     return _BTree_get(self, key, 1);
 }
 
+/* Search BTree self for key.  This is the sq_contains slot of the
+ * PySequenceMethods.
+ *
+ * Return:
+ *     -1     error
+ *      0     not found
+ *      1     found
+ */
+static int
+BTree_contains(BTree *self, PyObject *key)
+{
+    PyObject *asobj = _BTree_get(self, key, 1);
+    int result = -1;
+
+    if (asobj != NULL) {
+        result = PyInt_AsLong(asobj) ? 1 : 0;
+        Py_DECREF(asobj);
+    }
+    return result;
+}
+
 static PyObject *
 BTree_addUnique(BTree *self, PyObject *args)
 {
@@ -1668,6 +1686,19 @@
   (objobjargproc)BTree_setitem,	        /*mp_ass_subscript*/
 };
 
+static PySequenceMethods BTree_as_sequence = {
+    (inquiry)0,                     /* sq_length */
+    (binaryfunc)0,                  /* sq_concat */
+    (intargfunc)0,                  /* sq_repeat */
+    (intargfunc)0,                  /* sq_item */
+    (intintargfunc)0,               /* sq_slice */
+    (intobjargproc)0,               /* sq_ass_item */
+    (intintobjargproc)0,            /* sq_ass_slice */
+    (objobjproc)BTree_contains,     /* sq_contains */
+    0,                              /* sq_inplace_concat */
+    0,                              /* sq_inplace_repeat */
+};
+
 static int
 BTree_nonzero( BTree *self)
 {
@@ -1691,7 +1722,7 @@
     0,					/* tp_compare */
     0,					/* tp_repr */
     &BTree_as_number_for_nonzero,	/* tp_as_number */
-    0,					/* tp_as_sequence */
+    &BTree_as_sequence,			/* tp_as_sequence */
     &BTree_as_mapping,			/* tp_as_mapping */
     0,					/* tp_hash */
     0,					/* tp_call */


=== Zope3/lib/python/Persistence/BTrees/BucketTemplate.c 1.9 => 1.10 ===
 }
 
-/*
-** bucket_has_key
-**
-*/
 static PyObject *
 bucket_has_key(Bucket *self, PyObject *key)
 {
     return _bucket_get(self, key, 1);
 }
 
+
+/* Search bucket self for key.  This is the sq_contains slot of the
+ * PySequenceMethods.
+ *
+ * Return:
+ *     -1     error
+ *      0     not found
+ *      1     found
+ */
+static int
+bucket_contains(Bucket *self, PyObject *key)
+{
+    PyObject *asobj = _bucket_get(self, key, 1);
+    int result = -1;
+
+    if (asobj != NULL) {
+        result = PyInt_AsLong(asobj) ? 1 : 0;
+        Py_DECREF(asobj);
+    }
+    return result;
+}
+
 /*
 ** bucket_getm
 **
@@ -1361,6 +1379,19 @@
   (objobjargproc)bucket_setitem,	/*mp_ass_subscript*/
 };
 
+static PySequenceMethods Bucket_as_sequence = {
+    (inquiry)0,                     /* sq_length */
+    (binaryfunc)0,                  /* sq_concat */
+    (intargfunc)0,                  /* sq_repeat */
+    (intargfunc)0,                  /* sq_item */
+    (intintargfunc)0,               /* sq_slice */
+    (intobjargproc)0,               /* sq_ass_item */
+    (intintobjargproc)0,            /* sq_ass_slice */
+    (objobjproc)bucket_contains,    /* sq_contains */
+    0,                              /* sq_inplace_concat */
+    0,                              /* sq_inplace_repeat */
+};
+
 static PyObject *
 bucket_repr(Bucket *self)
 {
@@ -1415,7 +1446,7 @@
     0,					/* tp_compare */
     (reprfunc)bucket_repr,		/* tp_repr */
     0,					/* tp_as_number */
-    0,					/* tp_as_sequence */
+    &Bucket_as_sequence,		/* tp_as_sequence */
     &Bucket_as_mapping,			/* tp_as_mapping */
     0,					/* tp_hash */
     0,					/* tp_call */


=== Zope3/lib/python/Persistence/BTrees/SetTemplate.c 1.3 => 1.4 ===
 
 static PySequenceMethods set_as_sequence = {
-	(inquiry)set_length,		/*sq_length*/
-	(binaryfunc)0,		/*sq_concat*/
-	(intargfunc)0,		/*sq_repeat*/
-	(intargfunc)set_item,		/*sq_item*/
-	(intintargfunc)0,		/*sq_slice*/
-	(intobjargproc)0,	/*sq_ass_item*/
-	(intintobjargproc)0,	/*sq_ass_slice*/
+	(inquiry)set_length,		/* sq_length */
+	(binaryfunc)0,                  /* sq_concat */
+	(intargfunc)0,                  /* sq_repeat */
+	(intargfunc)set_item,           /* sq_item */
+	(intintargfunc)0,               /* sq_slice */
+	(intobjargproc)0,               /* sq_ass_item */
+	(intintobjargproc)0,            /* sq_ass_slice */
+        (objobjproc)bucket_contains,    /* sq_contains */
+        0,                              /* sq_inplace_concat */
+        0,                              /* sq_inplace_repeat */
 };
 
 static PyTypeObject SetType = {


=== Zope3/lib/python/Persistence/BTrees/TreeSetTemplate.c 1.2 => 1.3 ===
   Copyright (c) 2001, 2002 Zope Corporation and Contributors.
   All Rights Reserved.
-  
+
   This software is subject to the provisions of the Zope Public License,
   Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
   WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
   FOR A PARTICULAR PURPOSE
-  
+
  ****************************************************************************/
 
 #define TREESETTEMPLATE_C "$Id$\n"
@@ -58,7 +58,7 @@
     /* n starts out at -1, which is the error return value.  If
        this point is reached, then there is no error.  n must be
        incremented to account for the initial value of -1 instead of
-       0.  
+       0.
     */
     n++;
 
@@ -103,8 +103,8 @@
   int r;
 
   if (!PyArg_ParseTuple(args,"O",&args)) return NULL;
- 
-  PER_PREVENT_DEACTIVATION(self); 
+
+  PER_PREVENT_DEACTIVATION(self);
   r=_BTree_setstate(self, args, 1);
   PER_ALLOW_DEACTIVATION(self);
   PyPersist_SetATime(self);
@@ -137,7 +137,7 @@
    "Return the smallest key in the BTree.  If min is specified, return\n"
    "the smallest key >= min."},
   {"clear",	(PyCFunction) BTree_clear,	METH_NOARGS,
-   "clear()\n\nRemove all of the items from the BTree."},  
+   "clear()\n\nRemove all of the items from the BTree."},
   {"insert",	(PyCFunction)TreeSet_insert,	METH_VARARGS,
    "insert(id,[ignored]) -- Add an id to the set"},
   {"update",	(PyCFunction)TreeSet_update,	METH_VARARGS,
@@ -157,6 +157,19 @@
   (inquiry)BTree_length,		/*mp_length*/
 };
 
+static PySequenceMethods TreeSet_as_sequence = {
+    (inquiry)0,                     /* sq_length */
+    (binaryfunc)0,                  /* sq_concat */
+    (intargfunc)0,                  /* sq_repeat */
+    (intargfunc)0,                  /* sq_item */
+    (intintargfunc)0,               /* sq_slice */
+    (intobjargproc)0,               /* sq_ass_item */
+    (intintobjargproc)0,            /* sq_ass_slice */
+    (objobjproc)BTree_contains,     /* sq_contains */
+    0,                              /* sq_inplace_concat */
+    0,                              /* sq_inplace_repeat */
+};
+
 static int
 TreeSet_init(PyObject *self, PyObject *args, PyObject *kwds)
 {
@@ -184,7 +197,7 @@
     0,					/* tp_compare */
     0,					/* tp_repr */
     &BTree_as_number_for_nonzero,	/* tp_as_number */
-    0,					/* tp_as_sequence */
+    &TreeSet_as_sequence,		/* tp_as_sequence */
     &TreeSet_as_mapping,		/* tp_as_mapping */
     0,					/* tp_hash */
     0,					/* tp_call */
@@ -192,7 +205,7 @@
     0,					/* tp_getattro */
     0,					/* tp_setattro */
     0,					/* tp_as_buffer */
-    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | 
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
 	    Py_TPFLAGS_BASETYPE, 	/* tp_flags */
     0,					/* tp_doc */
     (traverseproc)BTree_traverse,	/* tp_traverse */