catacomb/__init__.py: Implement equality and hashing for `KeyData' objects.
[catacomb-python] / catacomb / __init__.py
index bd8aa57..94efb6f 100644 (file)
@@ -701,27 +701,41 @@ class _tmp:
       pp.text(','); pp.breakable()
       pp.pretty(me.writeflags(me.flags))
     pp.end_group(ind, ')')
+  def __hash__(me): return me._HASHBASE ^ hash(me._guts())
+  def __eq__(me, kd):
+    return type(me) == type(kd) and \
+      me._guts() == kd._guts() and \
+      me.flags == kd.flags
+  def __ne__(me, kd):
+    return not me == kd
 _augment(KeyData, _tmp)
 
 class _tmp:
   def _guts(me): return me.bin
+  def __eq__(me, kd):
+    return isinstance(kd, KeyDataBinary) and me.bin == kd.bin
 _augment(KeyDataBinary, _tmp)
+KeyDataBinary._HASHBASE = 0x961755c3
 
 class _tmp:
   def _guts(me): return me.ct
 _augment(KeyDataEncrypted, _tmp)
+KeyDataEncrypted._HASHBASE = 0xffe000d4
 
 class _tmp:
   def _guts(me): return me.mp
 _augment(KeyDataMP, _tmp)
+KeyDataMP._HASHBASE = 0x1cb64d69
 
 class _tmp:
   def _guts(me): return me.str
 _augment(KeyDataString, _tmp)
+KeyDataString._HASHBASE = 0x349c33ea
 
 class _tmp:
   def _guts(me): return me.ecpt
 _augment(KeyDataECPt, _tmp)
+KeyDataECPt._HASHBASE = 0x2509718b
 
 class _tmp:
   def __repr__(me):
@@ -732,7 +746,21 @@ class _tmp:
     if cyclep: pp.text('...')
     else: _pp_dict(pp, _iteritems(me))
     pp.end_group(ind, ' })')
+  def __hash__(me):
+    h = me._HASHBASE
+    for k, v in _iteritems(me):
+      h = ((h << 1) ^ 3*hash(k) ^ 5*hash(v))&0xffffffff
+    return h
+  def __eq__(me, kd):
+    if type(me) != type(kd) or me.flags != kd.flags or len(me) != len(kd):
+      return False
+    for k, v in _iteritems(me):
+      try: vv = kd[k]
+      except KeyError: return False
+      if v != vv: return False
+    return True
 _augment(KeyDataStructured, _tmp)
+KeyDataStructured._HASHBASE = 0x85851b21
 
 ###--------------------------------------------------------------------------
 ### Abstract groups.