Cycling Through a list by index?

BlitzMax Forums/BlitzMax Programming/Cycling Through a list by index?

Chroma(Posted 2008) [#1]
Let's say I have a TList with 10 objects in it. I want to instantly access the 5th object in the list.

I want to avoid having to do this:
Function GetTileInList:TTile(list:TList, index%)
   Local tile:TTile
   Local n%
   For tile = Eachin list
      n :+ 1
      If n = index Then Exit
   Next
   Return tile
End Function


Is there a way to do a:
(pseudocode)
Local tile:TTile = TileList.Index(5)



Michael Reitzenstein(Posted 2008) [#2]
Local tile:TTile = TTile( TileList.ValueAtIndex( 5 ) )


Chroma(Posted 2008) [#3]
Thanks Michael. For the record it's ValueAtIndex(index-1).

I really like how my stuff is working internally now. Very streamlined.

Gah, gonna run play some Dogfight...


Brucey(Posted 2008) [#4]
I wouldn't advise iterating a TList by index... it'll be a wee bit inefficient.


Michael Reitzenstein(Posted 2008) [#5]
Yep, it's O(n^2) and you can probably do the same thing in O(n), but the thing is that for small values of n it's perfectly valid. The trick is to profile!


Brucey(Posted 2008) [#6]
Well, a TList isn't designed for random-access.
Better to use the right tools for the right jobs, I say :-)

If you feel the need to be accessing a TList by index often, then convert it to an Array first.

But as Michael says, profile :-)


Michael Reitzenstein(Posted 2008) [#7]
Definitely. Also, I guess profiling can only really help if you're covering the possible use cases of the program yourself.

BlitzMax used to have n^2 behaviour for lines of code in a function, which nobody really found to be a problem until I tried to convert a 20k line spaghetti code goto based program over from B3D. That didn't work so well :)


Chroma(Posted 2008) [#8]
When you say profiling you mean?

I know how to change a TList into an Array at least heh.


tonyg(Posted 2008) [#9]
Profile .
I used this as a start point for Bmax. I think there is another example in the Code Archives somewhere.


Chroma(Posted 2008) [#10]
Gotcha. I do that but just didn't know the technical term.


Chroma(Posted 2008) [#11]
Well I'm only accessing the TList randomly during the load process so the ValueAtIndex() works spendidly. Thanks for the help.