Variable list... List or Array?

BlitzMax Forums/BlitzMax Programming/Variable list... List or Array?

I'd use a hash table. It offers the best of all worlds. For instance, it has constant time lookup like an array (where a TMap will have O(log n) lookup time) and you can use a string as the key. They grow as needed and so in this regard behave like a list (this is the only time they're 'slow', as they need to allocate new memory and rehash all of the entries. This can be avoided by selecting an appropriate initial size, and an appropriate growth factor.)


Some implementations of them will also allow you to store multiple entries per key, should you so desire.

In fact, I've got a hash table module I've been working on. It's mighty fast and can store either a single object per key, or multiple objects per key. The only functionality I don't have fully implemented is automatic resizing (although you can manually call the grow function...adding automatic resizing should be quite simple, too.)

The hashtable that I've been working on stores objects, so you can store basically anything in the table. All you have to do is cast it back to the proper type when you get it back from the hash table. If you want to store integers/floats/doubles etc, you can wrap them in an object and then store that (unfortunately you can't store them directly - although you could easily modify the code to make it hold a specific type of variable.)

Basically the code would look like this
'initial size of 25
    Hashtable:THashtable = THashtable.constructor( 25 )

'strings are treated as objects in BMax, and so can be 
'inserted directly into the table
    Hashtable.insertEntry( "TestKey", "TestValue" )

'cast the result back to a string and print it
    print( string(Hashtable.getEntry( "TestKey") )  'prints "Testvalue"

'you can also insert other types into the table. You can even store a hash table in a hashtable
    secondhashtable:THashtable = THashtable.constructor( 5 )

    secondhashtable.insertentry( "2ndTestIndex", "2ndTestValue" )

    hashtable.insertentry( "Hashtable" , secondhashtable )

'now we'll retrieve the second hash table from the first 
'hash table, and get & print the value for the key "2ndTestIndex"
    local returnedhash:THashtable

'we've got to always cast the result because the table returns a plain ol' object
    returnedhash = THashtable( hashtable.getentry( "Hashtable" )) 

    print( returnedhash.getentry( "2ndTestIndex" ) '<---prints "2ndTestvalue"


If you're interested let me know. I can send the code your way (it's fairly well documented, too).