diff options
author | W. Trevor King <wking@drexel.edu> | 2009-07-23 09:25:34 -0400 |
---|---|---|
committer | W. Trevor King <wking@drexel.edu> | 2009-07-23 09:25:34 -0400 |
commit | 678fccaf505eca6d816b859e6d90d728d6426a02 (patch) | |
tree | 03909209495fc21cc3d267a2f709e52b56ce4e42 | |
parent | 2d381449ece1326b25c5fbbbf3ee7987f03d1ee2 (diff) | |
download | bugseverywhere-678fccaf505eca6d816b859e6d90d728d6426a02.tar.gz |
Added libbe.tree.Tree.has_descendant().
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.
-rw-r--r-- | libbe/tree.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/libbe/tree.py b/libbe/tree.py index 0256407..45ae085 100644 --- a/libbe/tree.py +++ b/libbe/tree.py @@ -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() |