Example code for maps?

Monkey Forums/Monkey Programming/Example code for maps?

Rex Rhino(Posted 2012) [#1]
Hello,

I am trying to wrap my head around maps for Monkey, but the documentation does not include any code examples.

Does anyone have a simple example of how to use maps?


therevills(Posted 2012) [#2]
Really simple StringMap example:

Strict

Function Main:Int()
	Local myMap:MyMap = New MyMap
	myMap.Set("0", 0)
	myMap.Set("1", 1)
	myMap.Set("20", 2)
	
	Print myMap.GetValue("20") ' Prints 2
	
	For Local key:String = Eachin myMap.Keys()
		Print key + " is stored in the map."
	Next
	
	Return 0
End

Class MyMap Extends StringMap<Int>
	Method GetValue:Int(key:String)
		Local i:Int = Self.Get(key)
		Return i
	End	
End


Hope it helps.


Goodlookinguy(Posted 2012) [#3]
In simplest terms, think of a map as a way to associate several similar objects with keys. These keys must be unique and can only be associated with one object at a time.

Map class
Class Map< KeyType, ValueType >

KeyType = The object type that will be used as a key to other objects
ValueType = The type of object that the map is going to store

Map is an abstract class and must be extended to be used and so that certain methods can be filled in. Thankfully, this has already been done for the major types, String (StringMap), Int (IntMap), Float (FloatMap).

Here's the example using IntMap; in this particular case, KeyType would be Int and ValueType is String.
Function Main()
	Local exampleMap:IntMap<String> = New IntMap<String>()
	exampleMap.Set(1, "Hello") ' 1 is associated with "Hello"
	exampleMap.Set(2, "World") ' 2 is associated with "World"
	
	' Prints HelloWorld
	Print exampleMap.Get(1) + exampleMap.Get(2)
End


The 'Set' Method will set a key to be associated with a value.
The 'Get' Method will get the value associated with the key.

You can find more map methods here: http://blitz-wiki.appspot.com/Map


Rex Rhino(Posted 2012) [#4]
Thanks guys, I super appreciate the help. That is exactly what I need.

I understood the concept of maps, but I just couldn't figure out the actual implementation in code. Sometimes a few lines of code are a lifesaver. For some reason, I had the idea of StringMap or IntMap reversed (i.e. I thought the datatype referred to the value stored, not the key)

Now I can get to work finishing my associative array class (that serializes to a string).


slenkar(Posted 2012) [#5]
why dont you use reflection to serialize an array?

(i have a serialization thingy in the code forum)

Anyway I use Intmaps for actual game maps, they are very fast


Raz(Posted 2012) [#6]
Anyway I use Intmaps for actual game maps, they are very fast


Slenkar, in what way do you use an Intmap for a game map?

Ta!


slenkar(Posted 2012) [#7]
for a 10x10 map i have 10 intmaps that contain intmaps that contain mapsquare objects

its more flexible than an array, i can do minus coordinates which an array cant do, its also pretty fast so I dont notice much difference in speed


In my game I can have a 5000x5000 game map and only make mapsquares for parts of the map that contain something,
If I used an array I would have to reserve 5000x5000 mapsquare objects which would take up several megabytes


Raz(Posted 2012) [#8]
Oh wow of course! :) That's a really clever idea.

How do you handle the initialisation for let's say a 5000x5000 map?

Do you create an Intmap containing 5000 Intmaps straight away?


Rex Rhino(Posted 2012) [#9]
why dont you use reflection to serialize an array?


I am creating a storage object that loads the data from the state, and saves the data to the state. However, in the actual game, the data will be used to create objects, and the objects will be used to store the data during gameplay. For saving, I will write the data back into the associative array.

I want the data to be stored associatively, because I don't know what order I will be creating the objects in.


slenkar(Posted 2012) [#10]

Do you create an Intmap containing 5000 Intmaps straight away?


No, first an empty 'overall' Intmap is created

a new intmap is only created when a mapsquare has an x co-ordinate
that hasnt been used before.
So say you put a tree at 2,3

a new intmap is placed in the overall intmap

its key is 2

then a mapsquare is placed in the new intmap that has a key of 3




another tree is placed at 2,4

this time you get the old intmap from the overall Intmap with a key of 2
then create a mapsquare to go into the old intmap with a key of 4


Samah(Posted 2012) [#11]
I wrote some code in Java that lets you store map information in chunks like minecraft. I can do a monkey conversion if you're interested.


Beaker(Posted 2012) [#12]
You can do a quick and dirty version of what slenkar is describing by creating keys like this: with a map square at 2,3 store it using key "2_3". It worked well in an old flash game I made to run on early windows phones. Slenkars will probably be more efficient with larger denser maps.


Raz(Posted 2012) [#13]
Slenkar: Got ya :) I like that, it still lets you use the whole thing like an array if required too.


Rex Rhino(Posted 2012) [#14]
You can do a quick and dirty version of what slenkar is describing by creating keys like this: with a map square at 2,3 store it using key "2_3". It worked well in an old flash game I made to run on early windows phones. Slenkars will probably be more efficient with larger denser maps.


Assuming that Map uses some sort of hash table or something efficient, I think that your solution might be quicker. Obviously, it would need to be tested.