Cycling Through a list by index?
BlitzMax Forums/BlitzMax Programming/Cycling Through a list by index?
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)
Local tile:TTile = TTile( TileList.ValueAtIndex( 5 ) )
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...
I wouldn't advise iterating a TList by index... it'll be a wee bit inefficient.
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!
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 :-)
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 :)
When you say profiling you mean?
I know how to change a TList into an Array at least heh.
Profile .
I used
this as a start point for Bmax. I think there is another example in the Code Archives somewhere.
Gotcha. I do that but just didn't know the technical term.
Well I'm only accessing the TList randomly during the load process so the ValueAtIndex() works spendidly. Thanks for the help.