[pypy-svn] r7339 - pypy/trunk/src/pypy/tool

bob at codespeak.net bob at codespeak.net
Wed Nov 17 17:26:26 CET 2004


Author: bob
Date: Wed Nov 17 17:26:25 2004
New Revision: 7339

Modified:
   pypy/trunk/src/pypy/tool/frozendict.py
Log:
smarter hash implementation



Modified: pypy/trunk/src/pypy/tool/frozendict.py
==============================================================================
--- pypy/trunk/src/pypy/tool/frozendict.py	(original)
+++ pypy/trunk/src/pypy/tool/frozendict.py	Wed Nov 17 17:26:25 2004
@@ -1,10 +1,16 @@
 
 # hacks += 1
 class frozendict(dict):
-    def __setitem__(self, *args): 
+    _hash_cache = None
+    def __setitem__(self, *args):
         raise TypeError, "this dict is already frozen, you are too late!" 
-    __delitem__ = setdefault = update = pop = popitem = clear = __setitem__ 
+    __delitem__ = setdefault = update = pop = popitem = clear = __setitem__
 
     def __hash__(self):
-        return id(self) 
+        rval = self._hash_cache
+        if rval is None:
+            dct = self.items()
+            dct.sort()
+            rval = self._hash_cache = hash(tuple(dct)) ^ 0x18293742
+        return rval
 



More information about the Pypy-commit mailing list