use EachIn in reverse?

BlitzMax Forums/BlitzMax Programming/use EachIn in reverse?

Grey Alien(Posted 2007) [#1]
There's no quick way to use EachIn in reverse is there? I've probably got to manually iterate throught the links starting from the last one right?


H&K(Posted 2007) [#2]
Hummm, you could I suppose, when you are doing your AddLast in your new methods, Have "Two" lists, and do a AddFirst as well.
OK, so the creation of each would be longer, and memory would be more by a list per type, and a link per instance, but you could probably set the whole thing up with a few find/replaces.

In fact if you only ever want to do the eachin backwards, then it wont be anymore memory a all.


Perturbatio(Posted 2007) [#3]
Or you could petition Mark for the next three years to add an EachOut command :)


tonyg(Posted 2007) [#4]
Depending on how long the list is you could use the reversed or reverse methods and then use eachin.


TaskMaster(Posted 2007) [#5]
Yep, you could reverse the list/array (there is a command for that), then use EachIn.

Good answer tonyg. :)


Grey Alien(Posted 2007) [#6]
H&K: Yeah I thought of that. It's no biggie to write link reverse iteration code though.

Perturbatio: haha good call.

Tonyg: hmm, didn't know that, thanks. But they will be a bit long to do that really as they are checked each logic loop :-( I know I could make two lists and use the reverse command on it, but everytime I changed the main list (order/insert/delete) I'd need to update the reverse copy.

Thanks all for the great ideas.


tonyg(Posted 2007) [#7]
On my machine list.reversed takes 2ms for 10000 objects.
SuperStrict
Type ttest
	Field x:Int
End Type
Local list_in:tlist = CreateList()
For Local loopy:Int = 0 To 9999
	Local temp:ttest = New ttest 
	temp.x = loopy
	ListAddLast list_in , temp
Next

Local t1:Int = MilliSecs()
Local list_out:tlist = list_in.reversed()
Local t2:Int = MilliSecs()
Print ttest(list_out.first()).x
Print (t2-t1)



Perturbatio(Posted 2007) [#8]
Another thought would be to override TList and have an order flag that determines which way the compare method travels.


ImaginaryHuman(Posted 2007) [#9]
Considering what EachIn will be doing, which is simply doing a loop for you which iterates through the links in the list, you might as well just iterate through them yourself in whatever order you prefer.


Czar Flavius(Posted 2007) [#10]
Isn't it slow to go through a list in an irregular fashion?


Grey Alien(Posted 2007) [#11]
TonyG. That's fast but still I wouldn't want to call it each logic frame for my game.

Perturbatio: Yes that may be the best method because as ImaginaryHuman says, I may as well loop through in the way I want.

Czar Flavius: Nah, not to go in reverse, I should be able to write some code just as fast as the Blitz code to go through it forwards because the Blitz code will have to use links too (I'm assuming here, haven't 100% checked).


Grey Alien(Posted 2007) [#12]
To be honest I've found this to be academic now because I found that I don't need to do it because there was a bug in a level that someone designed, and my code didn't need to deal with it after all :-)


dmaz(Posted 2007) [#13]
yeah, I wrote some code... I'll dig it up.

here you go. (don't you guys check the codearcs? ;)
http://www.blitzbasic.com/codearcs/codearcs.php?code=1807

it extends TList, with it you can do this

For blah:yourtype = eachin list.ReverseFrom(list.LastLink())

and it can go from any TLink.... has a count too...

it adds 3 enumerators... From, ReverseFrom and LinksFrom


Grey Alien(Posted 2007) [#14]
nice. thx.


ImaginaryHuman(Posted 2007) [#15]
It's fast enough to iterate through the linked list in reverse if you do it at a lower level than using the helper functions.


Perturbatio(Posted 2007) [#16]
personally, I'd just use an array and a for loop (or one of the array based alternative lists in the code archives (none in particular of course))


H&K(Posted 2007) [#17]
I still think you might as well just do addfirst rather than addlast, then the list would be in reverse order. Mind you accademic as youve fixed the problem


Grey Alien(Posted 2007) [#18]
I tend to use lists a lot instead of arrays for ease of programming these days.