Containers
A container is a data structure, which contains data represented by other objects (its elements). The number of container elements is not limited.
Container Types:
Vector#
Vector is a sequence container, which represents an array of flexible size. Vector elements can be randomly accessed by their indices. Note that Unigine vector uses 0 -based indexes, just like C++ arrays.
You can use the following instruction to create a vector of a fixed size:
int vector[10];
vector[3] = 30;
- If you want to add a value to the vector, you can use container function append().
In the example below, a new value is appended to the end of the vector:
vector.append(20);
- Also you can remove elements of the vector.
vector.remove(1); // remove the 2nd element
- To change the vector size use the resize() function. It is possible to extend or reduce the vector size. For example, you can reduce its size to 5:
vector.resize(5);
- To get the vector size use the size() function.
int size = vector.size();
- Sometimes it is necessary to delete all the vector elements. If you want to set the vector size to 0 use the clear() function. The clear function does not delete the vector.
vector.clear();
- You can sort the vector in the ascending order. If the sort() function receives the vector as an argument, it will be sorted by the values of the first vector.
vector1[0] = ( 3, 2, 10, 8 ); vector2[0] = ( 2, 1, 5, 7 ); vector1.sort(); vector1.sort(vector2);
- To find a position of the element use find(). The function in the example below returns-1 if the element is not found. Also it can receive one or two arguments.
int id = vector.find(value);
Note that when an empty vector is declared, it is required to specify its size:
int vector[0];
A vector can be filled with values at the moment of initialization.
int array[5] = ( 1, -4, 2, 10, 73 );
You can mix components of different types in a vector.
int a[6] = ( 0, 1, 2, vec3(1,2,3), -7.83, "end" );
Note that you always manipulate with references to the vectors, but not with the vectors themselves. That is why when you assign one vector to another, it means that you simply copy the reference. Once the vector is destroyed, its references aren't functional anymore.
It is possible to index four-component vectors. Only ivec4 type is supported for such array indexing. Four values are returned as:
- ivec4 if the array type isint or long
- vec4 if the array type isfloat
- dvec4 if the array type isdouble
int array[0] = ( 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 );
ivec4 vector = array[ivec4( 0, 3, 5, 9 )];
// the vector components are: ( 0, 30, 50, 90 )
Maps#
A map is a type that maps values to keys. Elements of the map are represented by key-value pairs.
Keys are used to identify the elements of the map. Each key is associated with a value. The types of key and value can be different. For example, the following map contains the integer keys and the string values.:
int map[] = ( 1 : "one", 2 : "two", 3 : "three", 4 : "four" );
You can create a map almost the same way as a vector.
int map[];
map["key"] = 20;
- To add a key and a value to the map use append(). You can append a new key or a key-value pair to the map. In the example below, a new key-value pair is appended to the map:If such a key already exists, then its value will be replaced with the new one.
map.append("key",20);
- Also it is possible to remove the element with the key key.
map.remove(key);
- To get the size of the map use the size() function.
int size = map.size();
- If you want to delete all the map elements use the clear() function. The clear function does not delete the map.
map.clear();
- To find a position of the value element use find(). The function in the example below returns-1 if the value is not found. Also it can receive one or two arguments.
int id = map.find(value);
Note that when an empty map is declared, its size isn't specified:
int map[];
A map can be filled with values at the moment of initialization.
int map[] = ( "red" : 2, "black" : 5, "yellow" : 0 );
You can mix values of different types in a map, but all map keys must be of the same type, because there should be comparison operators defined for all keys.
int a[] = ( "red" : 1, "blue" : -1.82, "green" : vec3(1,2,3), "black" : "end" );
Note that you always manipulate with references to the maps, but not with the maps themselves. That is why when you assign one map to another, it means that you simply copy the reference. Once the map is destroyed, its references aren't functional anymore.