Array Operations – How to Create and Modify Arrays

An array is a container and may be of an enumeration type or one of the primitive types (int, string, double, …). It may even be a node or edge or graph type, then we speak of storages. It shares some similarities with mappings (map<int,T>).

For handling arrays, there are different calls. This article covers the following points:

Basic array operators

Syntax Description
x in arr Returns whether the element x is contained in the array arr, as boolean
arr[x] Array lookup, i.e. returns value stored in the array arr at index x

Query method calls

The query method calls on arrays are:

Query Method CallDefinition
.size()returns the number of elements in the array, as int
.empty()returns whether the array is empty, as boolean
.peek(num)returns the value stored in the array at position num:int (starts with 0) in the sequence of enumeration; retrieval occurs in constant time.
.peek()returns the last value stored in the array; retrieval occurs in constant time.
.indexOf(valueToSearchFor)returns the first position valueToSearchFor:T appears at, as int, or -1 if not found
.indexOf(valueToSearchFor, startIndex)returns the first position valueToSearchFor:T appears at (moving to the end), when we start the search for it at array position startIndex:int, as int, or -1 if not found
.lastIndexOf(valueToSearchFor)returns the last position valueToSearchFor:T appears at, as int, or -1 if not found
.lastIndexOf(valueToSearchFor, startIndex)returns the last position valueToSearchFor:T appears at (moving to the begin), when we start the search for it at array position startIndex:int, as int, or -1 if not found
.indexOfOrdered(valueToSearchFor)returns a position where valueToSearchFor:T appears at, as int, or -1 if not found. The array must be ordered, otherwise the results returned by the binary search employed will be wrong; in case of multiple occurences, an arbitray one is returned
.orderAscending()returns an array with the content of the input array ordered ascendingly, only available for basic types
.reverse()returns an array with the content of the input array reversed
.subarray(startIndex, length)returns subarray of given length:int from startIndex:int on
.asDeque()returns a deque of the array content, as deque for array, in same order
.asSet()returns a set (without order) of the array content, as set for array
.asMap()returns a map of the array content, as map for array, the values are mappedto by their index
.asString(separator)returns the array imploded to a string with the separator:string inserted in between the substrings from the array, only available for array (a string contains an asArray method for reversal)

Query method calls on arrays with graph elements

Some query method calls are available only on arrays of node or edge types bearing attributes,
inspecting the value of an attribute of the graph elements stored in the array, with the
attribute de ned with the operation, instead of the values contained in the array directly:

Query Method CallDefinition
.indexOfBy< attr >(valueToSearchFor)returns the first position valueToSearchFor:T appears at, as int, or -1 if not found
.indexOfBy< attr >(valueToSearchFor, startIndex)returns the first position valueToSearchFor:T appears at (moving to the end), when we start the search for it at array position startIndex:int, as int, or -1 if not found
.lastIndexOfBy< attr >(valueToSearchFor)returns the last position valueToSearchFor:T appears at, as int, or -1 if not found
.lastIndexOfBy< attr >(valueToSearchFor, startIndex)returns the last position valueToSearchFor:T appears at (moving to the begin), when we start the search for it at array position startIndex:int, as int, or -1 if not found
.indexOfOrderedBy< attr >(valueToSearchFor)returns a position where valueToSearchFor:T appears at, as int, or -1 if not found. The array must be ordered, otherwise the results returned by the binary search employed will be wrong; in case of multiple occurences, an arbitray one is returned
.orderAscendingBy< attr >()returns an array with the content of the input array ordered ascendingly; only available for an attribute of basic type, contained in a node or edge type, with the array storing graph elements of that type.

Update method calls

The update method calls on arrays are:

Update Method CallDefinition
Array addition:
a.add(v) adds the value v to the end of array a.
Array addition:
a.add(v,i) inserts the value v at index i to array a.
Array removal:
a.rem() removes the value at then end of the array a.
Array removal:
a.rem(i) removes the value at index i from the array a.
Array clearing:
a.clear() removes all values from the array a.

Binary operators

Binary array operators are:

OperatorDescription
+Array concatenation: returns new array with the right appended to the left array
the left and right operands must be of identical type array< T >.
For comparison:
A == BTrue, if A and B are identical.
A != BTrue, if A and B are not identical.
A < BTrue, if A is a subarray of B, but A and B are not identical.
A > BTrue, if A is a superarray of B, but A and B are not identical.
A <= BTrue, if A is a subarray of B or A and B are identical.
A >= BTrue, if A is a superarray of B or A and B are identical.

Copy operator

To copy an array use copy(array<T>):array<T> as shown in the example below.

Example

An example explaining some array operations.

function arrayExample(ref ai:array<int>) : boolean {
    def ref a:array<int> = array<int>[];               // Define empty int array
    def ref b:array<int> = array<int>[];               // Define empty int array
    a.add(1);                                          // a = [ 1 ]
    a.add(2);                                          // a = [ 1, 2 ]
    a.add(3);                                          // a = [ 1, 2, 3 ], a.size()==3
    b = copy(a);                                       // b = [ 1, 2, 3 ]
    def var tmp:int = a.peek();                        // last element in [ 1, 2, 3 ] is 3
    a.rem();                                           // a = [ 1, 2 ] FIFO
    for(k:int in [0:a.size()-1]) {
       b[k] = a[k];                                    // b = [1 ,2, 3]
    }
    def var i:int = a.indexOf(2);
    if(i != -1) {                                      // a[i]==2
       a[i] = 3;
    }                                                  // a = [ 1, 3 ]
    a.add(2, 1);                                       // a = [ 1, 2, 3 ]
    for(m:int in a) {
       emit(m + "\n");                                 // 1 2 3
    } 
    return (2 in (a + array<int>[ 4, 5 ]));            // true 
} 

rule moreArrayExample { 
   def ref strarrLHS:array<string> = array<string>["hello", "world"];
   yield {yield strarrLHS = array<string>["hello", "beautiful", "world"];}
   yield {emit("strarrLHS: " + strarrLHS + "\n");}

   modify{ 
    eval{ 
     def ref strarrRHS:array<string> = array<string>[];
     strarrRHS = array<sting>["other", "strings"];
     strarrRHS.add("great",1); //add string to value at position 1, first pos in array is 0
    }
  }
}

Was this article helpful?

Related Articles