On Mon, Aug 2, 2010 at 10:54 PM, Edward K. Ream <[email protected]> wrote:
> 3. Generalizing the code, say by creating p.deletePositionsInList,
> would be straightforward.
Provided one redefines the meaning of straightforward :-)
Here is my third or fourth attempt::
def deletePositionsInList (self,aList,callback,theIter):
'''Traverse root's subtree and call the callback for all positions in the
list, except positions in trees previously passed to the callback.
The callback takes one explicit parameter, p. The callback must delete p or
move p out of the tree out of the tree traversed by theIter. The callback
**must not** move p within the tree traversed by theIter. '''
# Compute the first node and the node after theIter completes.
# This allows us to simulate theIter without copying all the
# positions that theIter returns. That would be too expensive.
for p in theIter():
first = p.copy()
after = p.nodeAfterTree()
break
else: return g.trace('oops: theIter fails')
# Simulate the iterator.
skip = None # not skipping
while p and p != after
if p == skip:
skip = None # Stop skipping, do not skip p.
if not skip and p in aList:
# Skip deleted/moved positions.
skip = p.nodeAfterTree()
# Call the callback to delete or move the node.
callback(p.copy())
# Special case: we're done if there is no after node.
if not skip: break
else:
p.moveToThreadNext()
There are a few subtle points here:
1. There is *no way* to use an iterator in the main loop. Indeed,
suppose the loop deletes p or moves p (anywhere). This invalidates or
changes p.v, so the iterator will have no way to continue.
2. We could do the following, but it is horribly expensive: it would
create one position for every position in the tree. In general, that
would be a gc bug::
positionsList = [z.copy() for z in theIter()]
...
for p in positionsList:
In this kind of loop, a 'pass' would replace p.moveToThreadNext().
Other details omitted.
I haven't tested this code yet, but I wanted to capture my present
thinking here before it changes :-) Given the difficulty of this
code, the method should be available to all, and it should be safe and
efficient in all cases.
Edward
--
You received this message because you are subscribed to the Google Groups
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/leo-editor?hl=en.