Avoid creating a temporary list(s) for tree type

Instead of creating a temporary list of the node using
its __iter__() function and then reversing that list just
use the natively provided reverse_iter() method instead
that reduces this wasteful list copying and creating in
the first place.

Also does the same in the pformat() function which was
needlessly creating a temporary list of children nodes
instead of just using the nodes __iter__() functionality
directly.

Change-Id: Ice4001e6d014d2c0a1f7d8b916c60370fd5443a7
This commit is contained in:
Joshua Harlow
2014-12-29 09:32:21 -08:00
parent e182e44d68
commit 49ac8ec3ee

View File

@@ -45,8 +45,7 @@ class _DFSIter(object):
# Visit the node.
yield node
# Traverse the left & right subtree.
for child_node in reversed(list(node)):
stack.append(child_node)
stack.extend(node.reverse_iter())
class Node(object):
@@ -136,10 +135,10 @@ class Node(object):
else:
yield "__%s" % six.text_type(node.item)
prefix = " " * 2
children = list(node)
for (i, child) in enumerate(children):
child_count = node.child_count()
for (i, child) in enumerate(node):
for (j, text) in enumerate(_inner_pformat(child, level + 1)):
if j == 0 or i + 1 < len(children):
if j == 0 or i + 1 < child_count:
text = prefix + "|" + text
else:
text = prefix + " " + text