BlitzMax/Modules/Data structures/Maps
A map contains a set of key/value pairs. The keys and values may be any object type. A Red-Black Tree is used as the data structure.
For ordered collections of objects, see Linked lists.
Functions
editCreateMap
editFunction CreateMap:TMap()
Description: Create a map
Returns: A new map object
ClearMap
editFunction ClearMap( map:TMap )
Description: Clear a map
Information: ClearMap removes all keys and values from map
MapIsEmpty
editFunction MapIsEmpty( map:TMap )
Description: Check if a map is empty
Returns: True if map is empty, otherwise false
MapInsert
editFunction MapInsert( map:TMap,key:Object,value:Object )
Description: Insert a key/value pair into a map
Information: If map already contained key, its value is overwritten with value.
Example:
' mapinsert.bmx Local map:TMap = CreateMap() MapInsert(map, "Key 1", "Value 1") Print MapIsEmpty(map) ClearMap(map) Print MapIsEmpty(map)
MapValueForKey
editFunction MapValueForKey:Object( map:TMap,key:Object )
Description: Find a value given a key
Returns: The value associated with key
Information: If map does not contain key, a Null object is returned.
Example:
' mapvalueforkey.bmx Local map:TMap = CreateMap() MapInsert(map, "Key 1", "Value 1") Print String(MapValueForKey(map, "Key 1"))
MapContains
editFunction MapContains( map:TMap,key:Object )
Description: Check if a map contains a key
Returns: True if map contains key
Example:
'mapcontains.bmx Local map:TMap = CreateMap() MapInsert(map, "Key 1", "Value 1") Print MapContains(map, "Key 1") Print MapContains(map, "Key 2")
MapRemove
editFunction MapRemove( map:TMap,key:Object )
Description: Remove a key/value pair from a map
Comment: Unlike the Remove method for TMap, this function does not return whether the key/value pair was removed or not.
Example:
' mapremove.bmx Local map:TMap = CreateMap() MapInsert(map, "Key 1", "Value 1") Print MapContains(map, "Key 1") MapRemove(map, "Key 1") Print MapContains(map, "Key 1")
MapKeys
editFunction MapKeys:TMapEnumerator( map:TMap )
Description: Get map keys
Returns: An iterator object
Information: The object returned by MapKeys can be used with EachIn to iterate through the keys in map.
Example:
' mapkeys.bmx Local map:TMap = CreateMap() MapInsert(map, "Key 1", "Value 1") MapInsert(map, "Key 2", "Value 2") Local obj:Object For obj = EachIn MapKeys(map) Print(String(obj) + "," + String(MapValueForKey(map, obj))) Next
MapValues
editFunction MapValues:TMapEnumerator( map:TMap )
Description: Get map values
Returns: An iterator object
Information: The object returned by MapValues can be used with EachIn to iterate through the values in map.
Example:
' mapvalues.bmx Local map:TMap = CreateMap() MapInsert(map, "Key 1", "Value 1") MapInsert(map, "Key 2", "Value 2") Local obj:Object For obj = EachIn MapValues(map) Print String(obj) Next
CopyMap
editFunction CopyMap:TMap( map:TMap )
Description: Copy a map
Returns: A copy of map
Example:
' copymap.bmx Local map:TMap = CreateMap() MapInsert(map, "Key 1", "Value 1") Local map2:TMap = CopyMap(map) Print String(MapValueForKey(map2, "Key 1"))