Map examples

BlitzMax Forums/BlitzMax Beginners Area/Map examples

oddchild(Posted 2011) [#1]
Does anyone have any simple map examples that are available online?


Kryzon(Posted 2011) [#2]
Most of the time you'll have to scavenge these forums for explanatory threads and posts. The online guides are existant but very few; you can start here - it's a TMap description from the BlitzMax wiki.

EDIT: Hm, there's the original thread about TMaps, and tonyG's post there links to some examples about them.

Last edited 2011


Czar Flavius(Posted 2011) [#3]
You can think of a map as like an array but the index can be anything you want and it doesn't have to be in order. The map increases in size on its own. You could have a map of fruits to prices.

Strict
Local shop:TMap = New TMap
shop.Insert("apple", "10")
shop.Insert("orange", "15")
shop.Insert("pear", "5")

Print "The price of an apple is " + String(shop.ValueForKey("apple"))


Compare this to an array, where the index would have to be an int. Your shop might not have all the fruits so there'd be gaps. You might want the fruits to be entered custom by the user, so you can't assign number constants in the code. Or a TList, but then you'd have to search linearly for the result.

Maps are slower than arrays but faster than lists to access a random element given its id. Maps are slower than lists but faster than arrays to insert an element in the middle or remove it. They are kind of a compromise.

They are very useful when you have something which you want to relate to a particular piece of data. In a multiplayer game, you could have user name as key and player data as the value.

Maps, like lists, can only store objects so you can't store ints or floats directly. You can store strings and types in them. A word of warning: it's an unwritten bug/restriction that you can't mix string and type KEYS in a single map. You can use the different methods in seperate maps.

Another example you could have file name strings as the key and TImage or TPixmap as the value and use it as a kind of cache. If that string is already in the map, retreive the image we have already loaded. Otherwise, load from harddisk and store it in the map for next time.

It's easy to be lazy with maps so remember arrays and lists are faster at their respective strengths than maps.

Last edited 2011


degac(Posted 2011) [#4]
Have you tried the online manual?

CreateMap