Timber | Implementation | Spar | Overview | foreach | arrays | array expressions | inline | Vectors | Templates | array interface | elastic array interface | example | Sins |
Spar has extensive support for arrays, but this support is designed for bulk operations on variable-length arrays. There are circumstances where a more light-weight construct is appropriate. For this reason Spar provides vectors. Vectors are important as indices into multi-dimensional arrays, in particular in the parameterized classes that implement custom array representations.
A vector type is written as the name of an element type, followed by a
Cartesian exponentiation operator (`^
'), followed by a constant
expression. For example, the following are valid vector declarations:
[int^3] x; [double^2] y;
Like primitive types, vectors do not have to be created explicitly, and like primitive types, they are passed by value. A vector of length 1 is not the same as its element.
Provided that the operations are defined on the elements, Spar allows vector assignment, equality comparison, addition, subtraction, and multiplication. Vector expressions can be written as a list of values surrounded by square brackets. The code below demonstrates most of these operations.
[int^3] x; x[0] = 1; // Fill elements of the vector x[1] = 2; x[3] = 3; x = [1,2,3]; // This is equivalent to the above [int^3] y = x; // Declaration with initialization y += [1,0,0]; // Vector addition y = y-x; // Vector subtraction y *= 2; // Element-wise multiplication
Last modified Sat May 13 15:50:56 2017 UT by Kees van Reeuwijk.