I would suggest another approach. What is the essence of this operation? If 
we compare the tree before and after this operation, the difference between 
them is just a bunch of links that were cut. To be able to specify which 
links are/(or should be) cut we can use the pair of values (childIndex, 
parent_v) for each link.

For each position in the input list we can produce the pair of values 
(childIndex, parent_v) that should be broken after this operation.
Now, if we try to delete some children of the cloned nodes (where parent_v 
is a clone), it is possible that the input list contains two or more 
different positions that will result in the same link being cut. That is 
why we need to eliminate duplicates from this list of pairs. Once we have 
this list of links that needs to be cut, we can sort them from the largest 
childIndex to the smallest one. Afterwards we can safely cut each of the 
links in the final list. They all should be cut and they all will exist 
until their turn to be processed is ready.
  
def p2link(p):
    parent_v = p.stack[-1][0] if p.stack else c.hiddenRootNode
    return (p._childIndex, parent_v)

def deletePositionsInList2(aList):
    links_to_be_cut = sorted(set(p2link(x) for x in aList), reverse=True)
    for i, v in links_to_be_cut:
        ch = v.children[i]
        j = ch.parents.index(v)
        del ch.parents[j]
        del v.children[i]


However, I believe it has the same effect as the solution I wrote before:

def deletePositionsInList(self, aList, redraw=True):
    seen = set()
    for p in sorted(aList, reverse=True):
        par = p.stack[-1][0] if p.stack else c.hiddenRootNode
        x = par, p._childIndex
        if x not in seen:
            seen.add(x)
            p.doDelete()
    if redraw:
        c.redraw()
    return


Although I expect the new solution (deletePositionsInList2) to be faster.

The only requirement for this function to work is that the input list 
contains only valid positions, but that should be the caller's 
responsibility.

Vitalije


-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/99ac68d8-5854-4f00-a7c5-7fd76479aa40%40googlegroups.com.

Reply via email to