PDA

View Full Version : Recursive deletion problem


stuh505
03-10-2006, 11:18 PM
Hey guys,

I am trying to do some recursive deleting and max is being really finnicky...can't seem to get it to work.

I have a structure which has an array field 'children', which can be filled with any number of the same type of structure, ad infinitum.

Some of these structures might be flagged for deletion, so I have to recursively traverse the DAG of structures, looking for anyone that is flagged and deleting all structures below if I find one that is flagged...

When I find a structure that is flagged, I need to know the index of that structure in the structure above it in order to delete it. So, I can't use "for c in children".

Instead, I am using a while loop...this seems to crash max immediately, it does not seem to like me deleting something from the array while I am iterating over it

fn checkForDeleteBelow=
(
c = 1
while c <= children.count do
(
if children[c].deleteFlag then
(
children[c].deleteBelow()
if (children[c].shape != undefined) then
delete children[c].shape
deleteItem children c
) else
(
children[c].checkForDeleteBelow()
c += 1
)
)
)

Bobo
03-10-2006, 11:49 PM
Instead, I am using a while loop...this seems to crash max immediately, it does not seem to like me deleting something from the array while I am iterating over it


I cannot tell without the whole code, but try a two-pass algorithm.The first pass goes through all branches down to the last child and collects all children to be deleted. Then the second pass goes backwards in the collection, thus always deleting lower children before their parents. Should be stable.

stuh505
03-11-2006, 11:47 PM
I cannot tell without the whole code, but try a two-pass algorithm.The first pass goes through all branches down to the last child and collects all children to be deleted. Then the second pass goes backwards in the collection, thus always deleting lower children before their parents. Should be stable.

Hmmm...

Well first of all to "collect" would require collecting pointers to each array as well as a corresponding index, because we cannot simply collect the objects and delete them.

But, as we delete, the indices of higher elements to delete will change...so that would have to be compensated for by going through and decrementing all indices for higher elements that were flagged for deletion...this is not a very clean method either!

Bobo
03-12-2006, 02:11 AM
Hmmm...

Well first of all to "collect" would require collecting pointers to each array as well as a corresponding index, because we cannot simply collect the objects and delete them.

But, as we delete, the indices of higher elements to delete will change...so that would have to be compensated for by going through and decrementing all indices for higher elements that were flagged for deletion...this is not a very clean method either!

Not seeing your code, I have no idea what you are doing and what sort of objects you are dealing with. But going BACKWARDS makes sure that you always delete higher indices before lower ones, thus index renumbering plays no role. This is the theory. As you know, the difference between theory and practice is larger in practice than in theory... ;)

stuh505
03-12-2006, 02:49 PM
edit: solved, using the two-pass idea

CGTalk Moderation
03-12-2006, 02:49 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.