Added libbe.tree.Tree.has_descendant().
authorW. Trevor King <wking@drexel.edu>
Thu, 23 Jul 2009 13:25:34 +0000 (09:25 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 23 Jul 2009 13:25:34 +0000 (09:25 -0400)
Tree equality is now based on instance id.  It had previously used the
default list "equal if all elements are equal", which meant that all
the leaves matched each other.

libbe/tree.py

index 02564072437899bcb07ff9a15446778e57283e05..45ae0855b47cd7a4ff50034f8a4ab79a156ad24a 100644 (file)
@@ -68,7 +68,18 @@ class Tree(list):
     f
     h
     i
+    >>> a.has_descendant(g)
+    True
+    >>> c.has_descendant(g)
+    False
+    >>> a.has_descendant(a)
+    False
+    >>> a.has_descendant(a, match_self=True)
+    True
     """
+    def __eq__(self, other):
+        return id(self) == id(other)
+
     def branch_len(self):
         """
         Exhaustive search every time == SLOW.
@@ -157,4 +168,12 @@ class Tree(list):
             yield (depth,node)
             stack.append(node)
 
+    def has_descendant(self, descendant, depth_first=True, match_self=False):
+        if descendant == self:
+            return match_self
+        for d in self.traverse(depth_first):
+            if descendant == d:
+                return True
+        return False
+
 suite = doctest.DocTestSuite()