Difference between revisions of "HashMap"

From Bohemia Interactive Community
Jump to navigation Jump to search
(Fill page with some basics)
(Fill page with some basics)
Line 1: Line 1:
 
{{SideTOC}}
 
{{SideTOC}}
A '''HashMap''' is a container storing Key-Value pairs which is very efficient with many entries that have different keys.
+
A '''HashMap''' is a container storing Key-Value pairs which is very efficient with many entries that have different keys.<br>
 +
Due to keys being stored by their hash, a HashMap provides constant-time lookup for keys, meaning its very efficient for finding a key. More information on wikipedia: https://en.wikipedia.org/wiki/Hash_table<br>
 
As a HashMap is a container, it stores some traits/properties with [[Array|Arrays]]
 
As a HashMap is a container, it stores some traits/properties with [[Array|Arrays]]
 
This container was added with v2.01 #TODO make a icon thingy or note thingy
 
This container was added with v2.01 #TODO make a icon thingy or note thingy
Line 13: Line 14:
 
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]] [["a",1], ["b",2], ["c",3]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2], ["c",3]]</nowiki>;
 
  [[private]] _myNewMap = _myMap;
 
  [[private]] _myNewMap = _myMap;
 
  _myMap [[set]] ["z", 4];
 
  _myMap [[set]] ["z", 4];
Line 19: Line 20:
  
 
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]] [["a",1], ["b",2], ["c", 3]]];
+
  [[player]] [[setVariable]] ["myMap", [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2], ["c",3]]</nowiki>];
 
  [[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 [["a",1], ["b",2], ["c",3], ["z",4]]}}
  
 
=== Creating a HashMap ===
 
=== Creating a HashMap ===
Line 31: Line 32:
 
   
 
   
 
  {{cc|Example of a prefilled HashMap}}
 
  {{cc|Example of a prefilled HashMap}}
  [[private]] _myFilledMap = [[createHashMapFromArray]] [["a",1], ["b",2], ["c", 3]];
+
  [[private]] _myFilledMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2], ["c", 3]]</nowiki>;
 
  [[count]] _myFilledMap; {{cc|returns 2}}
 
  [[count]] _myFilledMap; {{cc|returns 2}}
  
Line 41: Line 42:
 
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]] [["a",1], ["b",2]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
 
  _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 [["a",1337], ["b",2]] and _overwritten is true}}
  
Line 55: Line 56:
 
Values are retrieved by their key:
 
Values are retrieved by their key:
  
  [[private]] _myMap = [[createHashMapFromArray]] [["a",1], ["b",2]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
 
  _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 63: Line 64:
 
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]] [["a",1], ["b",2]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
 
  [[count]] _myMap ; {{cc|returns 2}}
 
  [[count]] _myMap ; {{cc|returns 2}}
  
 
=== HashMap Copy ===
 
=== HashMap Copy ===
  
  [[private]] _myMap = [[createHashMapFromArray]] [["a",1], ["b",2]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
 
  [[private]] _myNewMap = _myMap;
 
  [[private]] _myNewMap = _myMap;
 
  _myMap [[set]] ["a", 1337];
 
  _myMap [[set]] ["a", 1337];
Line 76: Line 77:
  
 
  {{cc|making copy}}
 
  {{cc|making copy}}
  [[private]] _myMap = [[createHashMapFromArray]] [["a",1], ["b",2]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
 
  [[private]] _myNewMap = [[+]]_myMap;
 
  [[private]] _myNewMap = [[+]]_myMap;
 
  _myMap [[set]] ["a", 1337];
 
  _myMap [[set]] ["a", 1337];
Line 87: Line 88:
 
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]] [["a",1], ["b",2]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
 
  _myMap [[deleteAt]] "b"; {{cc|_myMap is [["a",1]]}}
 
  _myMap [[deleteAt]] "b"; {{cc|_myMap is [["a",1]]}}
  
Line 94: Line 95:
 
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]] [["a",1], ["b",2]];
+
  [[private]] _myMap = [[createHashMapFromArray]] <nowiki>[["a",1], ["b",2]]</nowiki>;
 
  { [[systemChat]] [[str]] [ [[_x]], _y ] } [[forEach]] _myMap;
 
  { [[systemChat]] [[str]] [ [[_x]], _y ] } [[forEach]] _myMap;
  

Revision as of 13:12, 14 December 2020

Template:SideTOC 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 its very efficient for finding a key. More information on wikipedia: https://en.wikipedia.org/wiki/Hash_table
As a HashMap is a container, it stores some traits/properties with Arrays This container was added with v2.01 #TODO make a icon thingy or note thingy

See also: HashMaps

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]]

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

Keys inside the Hashmap need to be of a supported type, not all types are supported.

GameScalar | GameBool | GameString | GameArray | GameCode | GameSide | GameConfig | GameNamespace | GameNaN

  1. TODO
  2. TODO HashMapKey type


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 get ["z", "NotFound"];	// returns "NotFound"

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

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

bla bla when using scalar key bla bla link to wikipedia floating point precision


See Also