Difference between revisions of "Array"
(Created page with '-- Syntax -- array[type,d1$,d2$,...,dn] -- Semantics -- An array, where ''type'' is the element type, followed by the dimensions. The programmer can provide any number of dime…') |
m (11 revisions imported) |
||
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | == Syntax == | |
− | array[type, | + | array[type,d<sub>1</sub>,d<sub>2</sub>,...,d<sub>n</sub>] |
− | + | == Semantics == | |
− | An array, where ''type'' is the element type, followed by the dimensions. The programmer can provide any number of dimensions to create an n dimension array. Default is row major allocation (although this can be overridden via types.) In order to access an element of an array, the programmer | + | An array, where ''type'' is the element or record type, followed by the dimensions. The programmer can provide any number of dimensions to create an n dimension array. Default is row major allocation (although this can be overridden via types.) In order to access an element of an array, the programmer uses the traditional ''name[index]'' syntax.<br><br> |
+ | ''Note:'' If the dimensions are omitted then it is assumed to be a one dimensional array of infinite size without any explicit memory allocation (i.e. data provided into a function.) Be aware, without any size information then it is not possible to bounds check indexes. | ||
− | + | === Default typing === | |
+ | |||
+ | In the absence of further type information, the following types are added to the chain: | ||
+ | |||
+ | * [[allocated]] | ||
+ | * [[multiple]] | ||
+ | * [[heap]] | ||
+ | * [[onesided]] | ||
+ | |||
+ | == Communication == | ||
When an array variable is assigned to another, depending on where each variable is allocated to, there may be communication to achieve this assignment. The table details the communication rules for this assignment ''assigned variable := assigning variable''. As with the element type, default communication of arrays is safe. | When an array variable is assigned to another, depending on where each variable is allocated to, there may be communication to achieve this assignment. The table details the communication rules for this assignment ''assigned variable := assigning variable''. As with the element type, default communication of arrays is safe. | ||
Line 22: | Line 32: | ||
| single[on[i]] | | single[on[i]] | ||
| multiple[] | | multiple[] | ||
− | | | + | | individual processes write values to process i |
|- | |- | ||
| multiple[] | | multiple[] | ||
| single[on[i]] | | single[on[i]] | ||
− | | | + | | individual processes read values from process i |
|- | |- | ||
| single[on[i]] | | single[on[i]] | ||
Line 37: | Line 47: | ||
|} | |} | ||
− | + | == Example == | |
− | var a:array[String,2 | + | #include <io> |
− | + | #include <string> | |
− | + | function void main() { | |
− | + | var a:array[String,2]; | |
+ | a[0]:="Hello"; | ||
+ | a[1]:="World"; | ||
+ | print(itostring(a[0])+" "+itostring(a[1])+"\n"); | ||
+ | }; | ||
This example will declare variable ''a'' to be an array of 2 Strings. Then the first location in the array will be set to ''Hello'' and the second location set to ''World''. Lastly the code will display on stdio both these array string locations followed by newline. | This example will declare variable ''a'' to be an array of 2 Strings. Then the first location in the array will be set to ''Hello'' and the second location set to ''World''. Lastly the code will display on stdio both these array string locations followed by newline. | ||
+ | ''Since: Version 0.41b'' | ||
[[Category:Type Library]] | [[Category:Type Library]] | ||
− | [[Category: | + | [[Category:Compound Types]] |
[[Category:Collection Types]] | [[Category:Collection Types]] |
Latest revision as of 15:44, 15 April 2019
Syntax
array[type,d1,d2,...,dn]
Semantics
An array, where type is the element or record type, followed by the dimensions. The programmer can provide any number of dimensions to create an n dimension array. Default is row major allocation (although this can be overridden via types.) In order to access an element of an array, the programmer uses the traditional name[index] syntax.
Note: If the dimensions are omitted then it is assumed to be a one dimensional array of infinite size without any explicit memory allocation (i.e. data provided into a function.) Be aware, without any size information then it is not possible to bounds check indexes.
Default typing
In the absence of further type information, the following types are added to the chain:
Communication
When an array variable is assigned to another, depending on where each variable is allocated to, there may be communication to achieve this assignment. The table details the communication rules for this assignment assigned variable := assigning variable. As with the element type, default communication of arrays is safe.
Assigned Variable | Assigning Variable | Semantics |
---|---|---|
multiple[] | multiple[] | local assignment |
single[on[i]] | multiple[] | individual processes write values to process i |
multiple[] | single[on[i]] | individual processes read values from process i |
single[on[i]] | single[on[i]] | local assignment where i==i |
single[on[i]] | single[on[j]] | communication from j to i where i!=j |
Example
#include <io> #include <string> function void main() { var a:array[String,2]; a[0]:="Hello"; a[1]:="World"; print(itostring(a[0])+" "+itostring(a[1])+"\n"); };
This example will declare variable a to be an array of 2 Strings. Then the first location in the array will be set to Hello and the second location set to World. Lastly the code will display on stdio both these array string locations followed by newline.
Since: Version 0.41b