GeoGen Script reference
Contructors | Member Functions
Array Class Reference

Ordered map associating values to keys. More...

Contructors

static Array Empty ()
 Creates an empty array. More...
 

Member Functions

Number Count ()
 Gets the number of items in the array. More...
 
 PushBack (Object object)
 Adds object to the end of the array. More...
 
Object GetKeyByIndex (Number index)
 Gets key of index-th key-value pair in the array. More...
 
Object GetValueByIndex (Number index)
 Gets value of index-th key-value pair in the array. More...
 
Object Front ()
 Gets the first item in the array. More...
 
Object Back ()
 Gets the last item in the array. More...
 
Boolean ContainsKey (Object key)
 Determines whether the array contains key-value pair with given key. More...
 
Boolean ContainsValue (Object value)
 Determines whether the array contains key-value pair with given value. More...
 
 RemoveKey (Object key)
 Removes key-value pair with given key from the array. More...
 
 RemoveValue (Object value)
 Removes key-value pair with given value from the array. More...
 
 SortByKeys ()
 Orders the key-value pairs by the keys. More...
 
 SortByValues ()
 Orders the key-value pairs by the values. More...
 
 Shuffle (Number randomSeed)
 Randomizes the order of the key-value pairs. More...
 

Detailed Description

Ordered map associating values to keys.

It can be used to emulate many data structures commonly used in other programming languages lists/vectors, hash tables, dictionaries, stacks, queues and can also be used to emulate structures.

A new array can be created by calling Array.Empty. Operator [] can then be used to read and write items into/from an array.

var array = Array.Empty();
array["hello"] = "world";
var value = array["hello"]; // value is now "hello"

Additionally, an array can be created using the array literal notation:

var array = { "hello": "world" };
var value = array["hello"]; // value is now "hello"

An attempt to read from a key that is not present in the array will result in a runtime error.

Key in an array can be any object. Value in an array can also be any object.

Operator [] can also be used to add items into an array without specifying the key. In such case a number from integer sequence starting at 0 will be used (the key for the newly added value will be the greatest integer key ever added into that particular array plus one).

var array = Array.Empty();
array[] = "first"; // added with key 0
array[] = "second"; // added with key 1
array[5] = "third"; // added with key 5
array[] = "fourth"; // added with key 6

The array literal can also be used without specifying the key. Following code will result in the same array as the above example:

var array = {"first", "second", 5: "third", "fourth"};

The array is ordered by the order in which the items were added into it (this becomes relevant with functions like Array.Front).

Multidimensional arrays are not supported, however arrays can be arbitrarily nested:

var array =
{
{"item 0 - 0", "item 0 - 1"}.
2: {"item 2 - 0", 3: "item 2 - 3"}
};
var value = array[2][0]; // value is now "item 2-0"

Member Function Documentation

Object Array.Back ( )

Gets the last item in the array.

Triggers runtime error if the array is empty.

var array = Array.Empty();
array.PushBack(1);
array.PushBack(2);
var value = array.Back(); // value is now 2
Returns
The item.
Boolean Array.ContainsKey ( Object  key)

Determines whether the array contains key-value pair with given key.

var array = {"a": "itemA", "b": "itemB"};
var value = array.ContainsKey("a"); // value is now true
Parameters
keyThe key.
Returns
True if the array contains key, false otherwise.
Boolean Array.ContainsValue ( Object  value)

Determines whether the array contains key-value pair with given value.

var array = {"a": "itemA", "b": "itemB"};
var value = array.ContainsValue("itemC"); // value is now false
Parameters
valueThe value.
Returns
True if the array contains value, false otherwise.
Number Array.Count ( )

Gets the number of items in the array.

var array = {"a", "b"};
var count = array.Count(); // count is now 2
Returns
The number of items in the array.
static Array Array.Empty ( )
static

Creates an empty array.

Returns
An empty array.
Object Array.Front ( )

Gets the first item in the array.

Triggers runtime error if the array is empty.

var array = Array.Empty();
array.PushBack(1);
array.PushBack(2);
var value = array.Front(); // value is now 1
Returns
The item.
Object Array.GetKeyByIndex ( Number  index)

Gets key of index-th key-value pair in the array.

Triggers runtime error if the index is greater than or equal to size of the array.

var array = {"a": "valueA", "b": "valueB"};
for(var i = 0; i < array.Count(); i++)
{
var key = array.GetKeyByIndex(i);
Print("{0}th item has key {1} and value {2}", i, key, array[key]);
}
Parameters
indexZero-based index.
Returns
The key of index-th pair.
Object Array.GetValueByIndex ( Number  index)

Gets value of index-th key-value pair in the array.

Triggers runtime error if the index is greater than or equal to size of the array.

var array = {"a": "valueA", "b": "valueB"};
for(var i = 0; i < array.Count(); i++)
{
Print("{0}th item has value {1}", i, array.GetValueByIndex(i););
}
Parameters
indexZero-based index.
Returns
The key of index-th pair.
Array.PushBack ( Object  object)

Adds object to the end of the array.

Its key will be the greatest integral key that was ever added to the array plus one (or 0 if the array is empty). This is the equivalent of using the [] operator without a key.

var array = Array.Empty();
array.PushBack(1);
array.PushBack(2);
var value = array[1]; // value is now 2
Parameters
objectThe object.
Array.RemoveKey ( Object  key)

Removes key-value pair with given key from the array.

Triggers runtime error if the array doesn't contain key.

var array = {"a": "itemA", "b": "itemB"};
array.RemoveKey("b"); // the array now contains only a single pair, "a": "itemA"

.

Parameters
keyThe key.
Array.RemoveValue ( Object  value)

Removes key-value pair with given value from the array.

Does nothing if the array doesn't contain value.

var array = {"a": "itemA", "b": "itemB"};
array.RemoveValue("itemA"); // the array now contains only a single pair, "b": "itemB"

.

Parameters
valueThe value.
Array.Shuffle ( Number  randomSeed)

Randomizes the order of the key-value pairs.

Only changes the order of the pairs, not the pairs themselves.

Parameters
randomSeed(optional) The random seed. If not specified, it is generated using the common random sequence.
Array.SortByKeys ( )

Orders the key-value pairs by the keys.

Only changes the order of the pairs, not the pairs themselves.

var array = {"b": "itemB", "a": "itemA"};
array.SortByKeys(); // the order is now "a", "b"

.

Array.SortByValues ( )

Orders the key-value pairs by the values.

Only changes the order of the pairs, not the pairs themselves.

var array = {"a": "itemB", "b": "itemA"};
array.SortByValues(); // the order is now "b", "a"

.