Specific Object Access

BlitzMax Forums/BlitzMax Beginners Area/Specific Object Access

Nathaniel(Posted 2008) [#1]
What's the easiest and fastest way to access a specific object in a list?

I know I could do it this way:
For obj.myobject = EachIn objectList
   If (obj.number = the_number_i_want_to_access)
      'Do something
   End If
Next

But this is fairly inefficient and makes another variable to keep track of.


Gabriel(Posted 2008) [#2]
If you want fast random-access to an object, a TList is probably not the best data structure in which to put it in the first place. Assuming you don't want to write your own data structure, then I'd suggest an array if the number of items is not likely to change often or a TMap if it is. The array you can access by index, obviously, while you can access the TMap via a key, which could be a name.


Nathaniel(Posted 2008) [#3]
The type is for rigid-bodies in a physics engine, so the number of items is guaranteed to change. A TList works well with the rest of the program because it cycles through all the objects quite often. Due to the insufficient explanations of the BlitzMax docs and me being new to BlitzMax, I may need some help on how to use TMaps if they are the best choice.


GfK(Posted 2008) [#4]
You can use ValueAtIndex() to retrieve a specific object from a TList. However, if you plan on doing this a lot, consider using an array of objects instead.


Nathaniel(Posted 2008) [#5]
That's what I was looking for. Thanks Gabriel and GfK.


Gabriel(Posted 2008) [#6]
What's what you were looking for? ValueAtIndex() does exactly what you were doing in your first post.


Dreamora(Posted 2008) [#7]
forget value at index.
If you know the number you are looking for and are always looking for the number only, use a TMap (brl.map) and the string version of the number as key.
then you can look for them basing on their number.

If you need to search for different things and know that you are going to have many many objects, think about using SQLLite