Notifications
Clear all

[Closed] Recursive deletion problem

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
   )
  )
 )
4 Replies
1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

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.

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!

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

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…

edit: solved, using the two-pass idea