Monkey/Language/Maps

From Wikibooks, open books for an open world
Jump to navigation Jump to search

What are Maps?[edit | edit source]

Maps are lists that require a key-value relationship.

How to Create a Map?[edit | edit source]

Maps in monkey are created by extending the Map class. The extended map class *MUST* implement the Compare() method.

Maps are able to use any type, including objects.

The Compare() method in Monkey returns a "0" if the values are equal, a "1" or greater if the values are higher, or a "-1" or less if the values are lower. Some objects are difficult to compare, but the method must return a value for each field needed in the key.

Class VectorMap <Vector> Extends Map<Vector, Vector>
	Method Compare:Int( lhs:Vector,rhs:Vector )
		If lhs.x<rhs.x Return -1
		If lhs.x>rhs.x Return 1
		If lhs.y<rhs.y Return -1
		If lhs.y>rhs.y Return 1
		If lhs.z<rhs.z Return -1
		Return lhs.z>rhs.z
	End
End

Local mymap:VectorMap<Vector> = New VectorMap<Vector>

Maps are used with Get() and Set() methods.

Local somevector:Vector = New Vector(1,2,3)
Local somevalue:Vector = New Vector(4,5,6)

mymap.Set( somevector, somevalue)

Local returnvalue:Vector = mymap.Get(somevector)

Monkey includes built-in maps which offer use for setting objects, by using integer, floats, and strings as keys: IntMap, FloatMap, StringMap