Interupt and resume recursion?

Monkey Forums/Monkey Programming/Interupt and resume recursion?

slenkar(Posted 2013) [#1]
programming challenge!

When saving a large file with reflection it makes the android and html5 app panic a little and makes the player press a resume button because the app has stopped responding.

I had a go at interupting and resuming it but it didnt seem possible without going through all the objects to see which has already been saved.
has anyone been able to resume a recursive process from a certain place?


Heres an example:




Global size_iter:Int

Class village
Global inst:village
Field houses:List<house>
Method New()
houses=New List<house>

For Local x:Int=0 To 100
houses.AddLast(New house)
Next
End Method
End Class

Class house

Field bricks:List<brick>
Method New()
bricks=New List<brick>
bricks.AddLast(New brick(size_iter))
size_iter+=1
End Method
End Class

Class brick
Method New(siz:Int)
size=siz
End Method
Field size:Int
Field done:Bool
End Class

Function Main:Int()

village.inst=New village

Local objects_done:Int=0
For Local h:house=Eachin village.inst.houses
For Local b:brick=Eachin h.bricks
Print b.size
objects_done+=1
b.done=True
If objects_done>20
Exit
Endif
Next
Next

For Local h:house=Eachin village.inst.houses
For Local b:brick=Eachin h.bricks
If b.done=False
Print b.size
Endif
Next
Next

End Function


you cant resume where you left off without cycling through all the bricks that are already done, which makes it take a lot longer


Xaron(Posted 2013) [#2]
Duh... that's not an easy one. I would solve it that way that you use iteration instead of recursion...


Gerry Quinn(Posted 2013) [#3]
I've done this with my game Detective Chess (it can take ten seconds to generate difficult puzzles). Basically you need to be able to push and pop context for your algorithm yourself, and then you can implement your algorithm as an iteration that does X number of bottom-level calculations and then returns (with a flag to say whether it's finished everything). This would be how to do it with a chess game that has to think for a while or similar.

I don't use reflection. If you don't need it, it seems to me that you could implement what you are doing iteratively easily enough. Maybe even using it you could do that. I don't fully understand your program, but from what I see, you could do a few houses on each update, then do a render (showing a progress bar maybe) then do the next few houses. This would be straightforward enough and wouldn't involve any real overhead.


slenkar(Posted 2013) [#4]
reflection makes it even more difficult than the example above,

cos lets say the world object is like this:

class world
field people:list
field houses:list

you could do all the people..pause...then the houses..

but the people have a reference in one of their fields to their house
so their house gets serialized before its reached on the houses list in the world object