Rev 4689: Implement hash, again using the as_tuples() bootstrap. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

John Arbash Meinel john at arbash-meinel.com
Wed Sep 9 22:00:55 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

------------------------------------------------------------
revno: 4689
revision-id: john at arbash-meinel.com-20090909210040-o4ar9chb9bjutqgw
parent: john at arbash-meinel.com-20090909153943-ryi4hotash4l8n46
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-memory-consumption
timestamp: Wed 2009-09-09 16:00:40 -0500
message:
  Implement hash, again using the as_tuples() bootstrap.
-------------- next part --------------
=== modified file 'bzrlib/_keys_type_c.c'
--- a/bzrlib/_keys_type_c.c	2009-09-09 15:39:43 +0000
+++ b/bzrlib/_keys_type_c.c	2009-09-09 21:00:40 +0000
@@ -157,6 +157,21 @@
     return NULL;
 }
 
+static long
+Keys_hash(Keys *self)
+{
+    PyObject *as_tuples = NULL;
+    long hash = -1;
+
+    as_tuples = Keys_as_tuples(self);
+    if (as_tuples == NULL) {
+        return NULL;
+    }
+    hash = PyTuple_Type.tp_hash(as_tuples);
+    Py_DECREF(as_tuples);
+    return hash;
+}
+
 static PyObject *
 Keys_richcompare(PyObject *v, PyObject *w, int op)
 {
@@ -284,8 +299,7 @@
     0,                                           /* tp_as_number */
     &Keys_as_sequence,                           /* tp_as_sequence */
     0,                                           /* tp_as_mapping */
-    // TODO: implement hash() as something similar to tuple.hash()
-    0,                                           /* tp_hash */
+    (hashfunc)Keys_hash,                         /* tp_hash */
     0,                                           /* tp_call */
     0,                                           /* tp_str */
     PyObject_GenericGetAttr,                     /* tp_getattro */

=== modified file 'bzrlib/tests/test__keys_type.py'
--- a/bzrlib/tests/test__keys_type.py	2009-09-09 15:39:43 +0000
+++ b/bzrlib/tests/test__keys_type.py	2009-09-09 21:00:40 +0000
@@ -147,3 +147,13 @@
         k3 = self.module.Keys(2, 'foo', 'zzz')
         self.assertEqual([(('baz', 'bing'),), (('foo', 'bar'),),
                           (('foo', 'zzz'),)], sorted([k1, k2, k3]))
+
+    def test_hash(self):
+        k1 = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
+        as_tuple =(('foo', 'bar'), ('baz', 'bing'), ('foo', 'zzz')) 
+        self.assertEqual(hash(k1), hash(as_tuple))
+        x = {k1: 'foo'}
+        # Because k1 == as_tuple, it replaces the slot, rather than having both
+        # present in the dict.
+        x[as_tuple] = 'bar'
+        self.assertEqual({as_tuple: 'bar'}, x)



More information about the bazaar-commits mailing list