Libraries
Array Source Code
 previous   up   next 

Abstract data types
type
array (in type: baseType)
Abstract data type, describing resizable arrays with integer index.

array

const func type: array (in type: baseType)

Abstract data type, describing resizable arrays with integer index. Arrays with non-integer index are described in idxarray.


Operator Summary
void
(inout arrayType: arr) &:= (in arrayType: extension)
Append the array extension to the array arr.
void
(inout arrayType: arr) &:= (in baseType: element)
Append the given element to the array arr.
arrayType
(in arrayType: arr1) & (in arrayType: arr2)
Concatenate two arrays.
baseType
(in arrayType: arr) [ (in integer: index) ]
Access one element from the array arr.
arrayType
(in arrayType: arr) [ (in integer: start) .. ]
Get a sub array beginning at the position start.
arrayType
(in arrayType: arr) [ .. (in integer: stop) ]
Get a sub array ending at the position stop.
arrayType
(in arrayType: arr) [ (in integer: start) .. (in integer: stop) ]
Get a sub array from the position start to the position stop.
arrayType
(in arrayType: arr) [ (in integer: start) len (in integer: length) ]
Get a sub array from the position start with maximum length len.
arrayType
(in integer: factor) times (in baseType: element)
Generate an array by using factor elements.
arrayType
(in ARRAY_IDX_RANGE: indexRange) times (in baseType: element)
Generate an array of elements with indices in a specified range.

Function Summary
void
insert (inout arrayType: arr, in integer: index, in baseType: element)
Insert element at index into arr.
void
insert (inout arrayType: arr, in integer: index, in arrayType: elements)
Insert elements at index into arr.
baseType
remove (inout arrayType: arr, in integer: index)
Remove the element with index from arr.
arrayType
remove (inout arrayType: arr, in integer: index, in integer: length)
Remove the sub-array with index and length from arr.
integer
length (in arrayType: arr)
Determine the length of the array arr.
integer
minIdx (in arrayType: arr)
Minimum index of array arr.
integer
maxIdx (in arrayType: arr)
Maximum index of array arr.
void
for (inout baseType: forVar) range (in arrayType: arr) do (in proc: statements) end for
For-loop where forVar loops over the elements of the array arr.
void
for key (inout integer: keyVar) range (in arrayType: arr) do (in proc: statements) end for
For-loop where keyVar loops over the indices of the array arr.
void
for (inout baseType: forVar) key (inout integer: keyVar) range (in arrayType: arr) do (in proc: statements) end for
For-loop where forVar and keyVar loop over the array arr.
void
for (inout baseType: forVar) range (in arrayType: arr) until (ref func boolean: condition) do (in proc: statements) end for
For-loop where forVar loops over the elements of the array arr.
void
for key (inout integer: keyVar) range (in arrayType: arr) until (ref func boolean: condition) do (in proc: statements) end for
For-loop where keyVar loops over the indices of the array arr.
void
for (inout baseType: forVar) key (inout integer: keyVar) range (in arrayType: arr) until (ref func boolean: condition) do (in proc: statements) end for
For-loop where forVar and keyVar loop over the array arr.
baseType
rand (in arrayType: arr)
Select a random element from arr.
arrayType
sort (in arrayType: arr_obj)
Sort an array with the compare function of the element type.
arrayType
sort (in arrayType: arr_obj, REVERSE)
Reverse sort an array with the compare function of the element type.
void
ENABLE_SORT (in type: arrayType)
Define a sort function for an existing array type.

Operator Detail

&:=

const proc: (inout arrayType: arr) &:= (in arrayType: extension)

Append the array extension to the array arr.

Raises:
MEMORY_ERROR - Not enough memory for the concatenated array.

&:=

const proc: (inout arrayType: arr) &:= (in baseType: element)

Append the given element to the array arr.

Raises:
MEMORY_ERROR - Not enough memory for the concatenated array.

&

const func arrayType: (in arrayType: arr1) & (in arrayType: arr2)

Concatenate two arrays.

Returns:
the result of the concatenation.

[

const func baseType: (in arrayType: arr) [ (in integer: index) ]

Access one element from the array arr.

Returns:
the element with the specified index from arr.
Raises:
INDEX_ERROR - If index is less than minIdx(arr) or greater than maxIdx(arr)

[

const func arrayType: (in arrayType: arr) [ (in integer: start) .. ]

Get a sub array beginning at the position start.

Returns:
the sub array beginning at the start position.
Raises:
INDEX_ERROR - The start position is less than minIdx(arr).
MEMORY_ERROR - Not enough memory to represent the result.

[ ..

const func arrayType: (in arrayType: arr) [ .. (in integer: stop) ]

Get a sub array ending at the position stop.

Returns:
the sub array ending at the stop position.
Raises:
INDEX_ERROR - The stop position is less than pred(minIdx(arr)).
MEMORY_ERROR - Not enough memory to represent the result.

[

const func arrayType: (in arrayType: arr) [ (in integer: start) .. (in integer: stop) ]

Get a sub array from the position start to the position stop.

Returns:
the sub array from position start to stop.
Raises:
INDEX_ERROR - The start position is less than minIdx(arr1), or the stop position is less than pred(start).
MEMORY_ERROR - Not enough memory to represent the result.

[

const func arrayType: (in arrayType: arr) [ (in integer: start) len (in integer: length) ]

Get a sub array from the position start with maximum length len.

Returns:
the sub array from position start with maximum length len.
Raises:
INDEX_ERROR - The start position is less than minIdx(arr), or the length is negative.
MEMORY_ERROR - Not enough memory to represent the result.

times

const func arrayType: (in integer: factor) times (in baseType: element)

Generate an array by using factor elements.

 5 times 'x'    returns  [] ('x', 'x', 'x', 'x', 'x')
 0 times FALSE  returns  (array boolean).value
-1 times 5      raises   RANGE_ERROR
Returns:
an array with factor elements.
Raises:
RANGE_ERROR - If factor is negative.
MEMORY_ERROR - Not enough memory to represent the result.

times

const func arrayType: (in ARRAY_IDX_RANGE: indexRange) times (in baseType: element)

Generate an array of elements with indices in a specified range. The range is specified with a bracketed range expression like [A .. B] or [A len L]. An array with 5 char elements indexed from 0 to 4 is created with:

[0 .. 4] times 'x'    or    [0 len 5] times 'x'

This is equivalent to

[0] ('x', 'x', 'x', 'x', 'x')

An empty array can be generated with

[0 .. -1] times ""    or    [0 len 0] times ""
Returns:
an array with B - A + 1 elements (when [A .. B] is used), or an array with L elements (when [A len L] is used).
Raises:
RANGE_ERROR - If B - A is less than -1, or if L is less than 0.
MEMORY_ERROR - Not enough memory to represent the result.

Function Detail

insert

const proc: insert (inout arrayType: arr, in integer: index, in baseType: element)

Insert element at index into arr. Elements are moved backward to create space for the element to be inserted. This function is tuned for performance and the movement works without copying elements.

anArray := [] ("A", "B");
insert(anArray, 2, "X");  afterwards  anArray = [] ("A", "X", "B")

In this example the former anArray[2] is moved to anArray[3] and "X" is inserted as new anArray[2].

Raises:
INDEX_ERROR - If index is less than minIdx(arr) or greater than succ(maxIdx(arr))

insert

const proc: insert (inout arrayType: arr, in integer: index, in arrayType: elements)

Insert elements at index into arr. Elements are moved backward to create space for the elements to be inserted. This function is tuned for performance and the movement works without copying elements.

Raises:
INDEX_ERROR - If index is less than minIdx(arr) or greater than succ(maxIdx(arr))

remove

const func baseType: remove (inout arrayType: arr, in integer: index)

Remove the element with index from arr. The elements after the removed element are moved forward. This function is tuned for performance and the movement works without copying elements.

anArray := [] ("A", "B", "C");
remove(anArray, 2)  returns  "B"  and  anArray = [] ("A", "C")

In this example the current anArray[2] is removed and the former anArray[3] is now at anArray[2].

Returns:
the removed element.
Raises:
INDEX_ERROR - If index is less than minIdx(arr) or greater than maxIdx(arr)

remove

const func arrayType: remove (inout arrayType: arr, in integer: index, in integer: length)

Remove the sub-array with index and length from arr. The elements after the removed sub-array are moved forward. This function is tuned for performance and the movement works without copying elements.

Returns:
the removed sub-array.
Raises:
INDEX_ERROR - If index is less than minIdx(arr) or greater than maxIdx(arr)

length

const func integer: length (in arrayType: arr)

Determine the length of the array arr.

length([] (2, 3, 5))   returns  3
length([0] (2, 3, 5))  returns  3
length([2] (2, 3, 5))  returns  3
Returns:
the length of the array.

minIdx

const func integer: minIdx (in arrayType: arr)

Minimum index of array arr.

minIdx([] (2, 3, 5))   returns  1
minIdx([0] (2, 3, 5))  returns  0
minIdx([2] (2, 3, 5))  returns  2
Returns:
the minimum index of the array.

maxIdx

const func integer: maxIdx (in arrayType: arr)

Maximum index of array arr.

maxIdx([] (2, 3, 5))   returns  3
maxIdx([0] (2, 3, 5))  returns  2
maxIdx([2] (2, 3, 5))  returns  4
Returns:
the maximum index of the array.

for

const proc: for (inout baseType: forVar) range (in arrayType: arr) do (in proc: statements) end for

For-loop where forVar loops over the elements of the array arr.


for key

const proc: for key (inout integer: keyVar) range (in arrayType: arr) do (in proc: statements) end for

For-loop where keyVar loops over the indices of the array arr.


for

const proc: for (inout baseType: forVar) key (inout integer: keyVar) range (in arrayType: arr) do (in proc: statements) end for

For-loop where forVar and keyVar loop over the array arr. The variable forVar loops over the elements of arr and keyVar loops over the indices of arr.


for

const proc: for (inout baseType: forVar) range (in arrayType: arr) until (ref func boolean: condition) do (in proc: statements) end for

For-loop where forVar loops over the elements of the array arr. Additionally a condition is checked before the statements in the loop body are executed.


for key

const proc: for key (inout integer: keyVar) range (in arrayType: arr) until (ref func boolean: condition) do (in proc: statements) end for

For-loop where keyVar loops over the indices of the array arr. Additionally a condition is checked before the statements in the loop body are executed.


for

const proc: for (inout baseType: forVar) key (inout integer: keyVar) range (in arrayType: arr) until (ref func boolean: condition) do (in proc: statements) end for

For-loop where forVar and keyVar loop over the array arr. The variable forVar loops over the elements of arr and keyVar loops over the indices of arr. Additionally a condition is checked before the statements in the loop body are executed.


rand

const func baseType: rand (in arrayType: arr)

Select a random element from arr. The pseudo-random indices of the elements are uniform distributed.

Returns:
a random element from arr.
Raises:
RANGE_ERROR - If arr is empty.

sort

const func arrayType: sort (in arrayType: arr_obj)

Sort an array with the compare function of the element type.

sort([] (2, 4, 6, 5, 3, 1))             returns  [] (1, 2, 3, 4, 5, 6)
sort([] ('o', 'r', 'g', 'y', 'l'))      returns  [] ('g', 'l', 'o', 'r', 'y')
sort([] ("bravo", "charlie", "alpha"))  returns  [] ("alpha", "bravo", "charlie")
sort([] (pred(2_**107), pred(2_**89)))  returns  [] (pred(2_**89), pred(2_**107))
sort([] (E, sqrt(2.0), PI, 1.0))        returns  [] (1.0, sqrt(2.0), E, PI)

For a user defined element type the following approach can be used:

const type: myType is ...
const func integer: compare (in myType: a, in myType: b) is ...
const type: myArrayType is array myType;

Afterwards myArrayType arrays can be sorted.


sort

const func arrayType: sort (in arrayType: arr_obj, REVERSE)

Reverse sort an array with the compare function of the element type.

sort([] (2, 4, 6, 5, 3, 1), REVERSE)             returns  [] (6, 5, 4, 3, 2, 1)
sort([] ('o', 'r', 'g', 'y', 'l'), REVERSE)      returns  [] ('y', 'r', 'o', 'l', 'g')
sort([] ("bravo", "charlie", "alpha"), REVERSE)  returns  [] ("charlie", "bravo", "alpha")
sort([] (pred(2_**107), pred(2_**89)), REVERSE)  returns  [] (pred(2_**107), pred(2_**89))
sort([] (E, sqrt(2.0), PI, 1.0), REVERSE)        returns  [] (PI, E, sqrt(2.0), 1.0)

For a user defined element type the following approach can be used:

const type: myType is ...
const func integer: compare (in myType: a, in myType: b) is ...
const type: myArrayType is array myType;

Afterwards myArrayType arrays can be sorted.


ENABLE_SORT

const proc: ENABLE_SORT (in type: arrayType)

Define a sort function for an existing array type. This template can be used if the array type has been defined and the compare function is defined afterwards:

const type: myType is ...
const type: myArrayType is array myType;
const func integer: compare (in myType: a, in myType: b) is ...
ENABLE_SORT(myArrayType);

Afterwards myArrayType arrays can be sorted.



 previous   up   next