Difference between revisions of "HashMap"

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Key Types)
m (Fix cc nowiki usage)
Line 12: Line 12:
 
this means that if the HashMap is edited, all the scripts/functions using a reference to this HashMap will see the edition.
 
this means that if the HashMap is edited, all the scripts/functions using a reference to this HashMap will see the edition.
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2], ["c",3]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2], ["c",3]];
 
  [[private]] _myNewMap = _myMap;
 
  [[private]] _myNewMap = _myMap;
 
  _myMap [[set]] ["z", 4];
 
  _myMap [[set]] ["z", 4];
Line 18: Line 18:
  
 
An array set through [[setVariable]] does not need to be assigned again if you modify it by reference:
 
An array set through [[setVariable]] does not need to be assigned again if you modify it by reference:
  [[player]] [[setVariable]] ["myMap", [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2], ["c",3]]</nowiki>];
+
  [[player]] [[setVariable]] ["myMap", [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2], ["c",3]]];
 
  [[private]] _myMap = [[player]] [[getVariable]] "myMap";
 
  [[private]] _myMap = [[player]] [[getVariable]] "myMap";
 
  _myMap [[set]] ["z", 4];
 
  _myMap [[set]] ["z", 4];
  [[player]] [[getVariable]] "myMap"; {{cc|is [["a",1], ["b",2], ["c",3], ["z",4]]}}
+
  [[player]] [[getVariable]] "myMap"; {{cc|is [<nowiki/>["a",1], ["b",2], ["c",3], ["z",4]]}}
  
 
=== Key Types ===
 
=== Key Types ===
Line 28: Line 28:
  
 
Supported types are:
 
Supported types are:
<div style="columns: 2">
+
<div style="columns: 3">
 
* [[Scalar]]
 
* [[Scalar]]
 
* [[Bool]]
 
* [[Bool]]
Line 52: Line 52:
 
   
 
   
 
  {{cc|Example of a prefilled HashMap}}
 
  {{cc|Example of a prefilled HashMap}}
  [[private]] _myFilledMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2], ["c", 3]]</nowiki>;
+
  [[private]] _myFilledMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2], ["c", 3]];
 
  [[count]] _myFilledMap; {{cc|returns 2}}
 
  [[count]] _myFilledMap; {{cc|returns 2}}
  
Line 58: Line 58:
  
 
  [[private]] _myMap = [[createHashMap]];
 
  [[private]] _myMap = [[createHashMap]];
  _myMap [[set]] [1, "hello there"]; {{cc|_myMap is [[1, "hello there"]]}}
+
  _myMap [[set]] [1, "hello there"]; {{cc|_myMap is [<nowiki/>[1, "hello there"]]}}
  
 
Inserting an element with a key that already exists inside the HashMap, will overwrite the existing key.
 
Inserting an element with a key that already exists inside the HashMap, will overwrite the existing key.
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
  _overwritten = _myMap [[set]] ["a", 1337]; {{cc|_myMap is now [["a",1337], ["b",2]] and _overwritten is true}}
+
  _overwritten = _myMap [[set]] ["a", 1337]; {{cc|_myMap is now [<nowiki/>["a",1337], ["b",2]] and _overwritten is true}}
  
 
=== Getting an element ===
 
=== Getting an element ===
Line 69: Line 69:
 
Values are retrieved by their key:
 
Values are retrieved by their key:
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  _myMap [[get]] "a"; {{cc|returns 1}}
 
  _myMap [[get]] "a"; {{cc|returns 1}}
 
  _myMap [[get]] "z"; {{cc|returns [[Nothing]]}}
 
  _myMap [[get]] "z"; {{cc|returns [[Nothing]]}}
Line 78: Line 78:
 
You can check if a key is in the HashMap using the [[in]] command:
 
You can check if a key is in the HashMap using the [[in]] command:
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  "a" [[in]] _myMap; {{cc|returns true}}
 
  "a" [[in]] _myMap; {{cc|returns true}}
 
  "z" [[in]] _myMap; {{cc|returns false}}
 
  "z" [[in]] _myMap; {{cc|returns false}}
  
 
=== Counting elements ===
 
=== Counting elements ===
 +
 
The [[count]] command can be used to return the number of Key-Value Pairs stored in the HashMap:
 
The [[count]] command can be used to return the number of Key-Value Pairs stored in the HashMap:
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  [[count]] _myMap ; {{cc|returns 2}}
 
  [[count]] _myMap ; {{cc|returns 2}}
  
Line 92: Line 93:
 
You can retrieve an [[Array]] of all keys in the HashMap using the [[keys]] command:
 
You can retrieve an [[Array]] of all keys in the HashMap using the [[keys]] command:
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  [[keys]] _myMap; {{cc|returns ["a", "b"]}}
 
  [[keys]] _myMap; {{cc|returns ["a", "b"]}}
  
 
=== HashMap Copy ===
 
=== HashMap Copy ===
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  [[private]] _myNewMap = _myMap;
 
  [[private]] _myNewMap = _myMap;
 
  _myMap [[set]] ["a", 1337];
 
  _myMap [[set]] ["a", 1337];
Line 105: Line 106:
  
 
  {{cc|making copy}}
 
  {{cc|making copy}}
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  [[private]] _myNewMap = [[+]]_myMap;
 
  [[private]] _myNewMap = [[+]]_myMap;
 
  _myMap [[set]] ["a", 1337];
 
  _myMap [[set]] ["a", 1337];
Line 116: Line 117:
 
You can remove elements from the HashMap by using [[deleteAt]] with the elements key:
 
You can remove elements from the HashMap by using [[deleteAt]] with the elements key:
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  _myMap [[deleteAt]] "b"; {{cc|_myMap is [["a",1]]}}
 
  _myMap [[deleteAt]] "b"; {{cc|_myMap is [["a",1]]}}
  
Line 123: Line 124:
 
The simplest way to iterate through a HashMap is the [[forEach]] command:
 
The simplest way to iterate through a HashMap is the [[forEach]] command:
  
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
+
  [[private]] _myMap = [[createHashMapFromArray]] [<nowiki/>["a",1], ["b",2]];
 
  { [[systemChat]] [[str]] [ [[_x]], _y ] } [[forEach]] _myMap;
 
  { [[systemChat]] [[str]] [ [[_x]], _y ] } [[forEach]] _myMap;
  
 
When iterating through a HashMap with forEach, the _x variable holds the key of the current element, while the _y variable holds its value.
 
When iterating through a HashMap with forEach, the _x variable holds the key of the current element, while the _y variable holds its value.
 +
  
 
== Advanced usage ==
 
== Advanced usage ==
Line 135: Line 137:
 
=== Scalar Key precision ===
 
=== Scalar Key precision ===
  
 +
{{wip}}
 
bla bla when using scalar key bla bla link to wikipedia floating point precision
 
bla bla when using scalar key bla bla link to wikipedia floating point precision
  
  
 +
== See Also ==
  
== See Also ==
 
 
* [[:Category:Command Group: HashMap|All HashMap commands]]  
 
* [[:Category:Command Group: HashMap|All HashMap commands]]  
 +
  
 
[[Category: Data Types]]
 
[[Category: Data Types]]

Revision as of 16:03, 14 December 2020

Template:SideTOC Template:Feature arma3 A HashMap is a container storing Key-Value pairs which is very efficient with many entries that have different keys.
Due to keys being stored by their hash, a HashMap provides constant-time lookup for keys, meaning it is very efficient for finding a key. More information on Wikipedia.
As a HashMap is a container, it stores some traits/properties with Arrays

Working with Hash Maps

Array properties

A HashMap variable is a reference to the HashMap(see Wikipedia reference page); this means that if the HashMap is edited, all the scripts/functions using a reference to this HashMap will see the edition.

private _myMap = createHashMapFromArray [["a",1], ["b",2], ["c",3]];
private _myNewMap = _myMap;
_myMap set ["z", 4];
_myNewMap get "z"; // will be 4

An array set through setVariable does not need to be assigned again if you modify it by reference:

player setVariable ["myMap", createHashMapFromArray [["a",1], ["b",2], ["c",3]]];
private _myMap = player getVariable "myMap";
_myMap set ["z", 4];
player getVariable "myMap"; // is [["a",1], ["b",2], ["c",3], ["z",4]]

Key Types

Due to the keys requirement to be hashable not all variable types are supported to be used as a key in the HashMap.

Supported types are:

Template:Important

Creating a HashMap

// Example of an empty HashMap
private _myMap = createHashMap;
count _myMap;			// returns 0

// Example of a prefilled HashMap
private _myFilledMap = createHashMapFromArray [["a",1], ["b",2], ["c", 3]];
count _myFilledMap;	// returns 2

Setting an element

private _myMap = createHashMap;
_myMap set [1, "hello there"];	// _myMap is [[1, "hello there"]]

Inserting an element with a key that already exists inside the HashMap, will overwrite the existing key.

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
_overwritten = _myMap set ["a", 1337];	// _myMap is now [["a",1337], ["b",2]] and _overwritten is true

Getting an element

Values are retrieved by their key:

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
_myMap get "a";	// returns 1
_myMap get "z";	// returns Nothing
_myMap getOrDefault ["z", "NotFound"];	// returns "NotFound"

Checking if an element exists

You can check if a key is in the HashMap using the in command:

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
"a" in _myMap;	// returns true
"z" in _myMap;	// returns false

Counting elements

The count command can be used to return the number of Key-Value Pairs stored in the HashMap:

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
count _myMap ; // returns 2

Retrieving Keys

You can retrieve an Array of all keys in the HashMap using the keys command:

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
keys _myMap; // returns ["a", "b"]

HashMap Copy

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
private _myNewMap = _myMap;
_myMap set ["a", 1337];
_myNewMap get "a"; // will be 1337

In order to avoid this behaviour, copy the HashMap with + (plus):

// making copy
private _myMap = createHashMapFromArray [["a",1], ["b",2]];
private _myNewMap = +_myMap;
_myMap set ["a", 1337];
_myNewMap get "a"; // still 1

Arrays stored as Key or value in the HashMap will also be deep-copied.

Removing (deleting) elements

You can remove elements from the HashMap by using deleteAt with the elements key:

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
_myMap deleteAt "b"; // _myMap is "a",1

Iterating through the HashMap

The simplest way to iterate through a HashMap is the forEach command:

private _myMap = createHashMapFromArray [["a",1], ["b",2]];
{ systemChat str [ _x, _y ] } forEach _myMap;

When iterating through a HashMap with forEach, the _x variable holds the key of the current element, while the _y variable holds its value.


Advanced usage

Common errors

Scalar Key precision

Template:wip bla bla when using scalar key bla bla link to wikipedia floating point precision


See Also