Changes between Version 5 and Version 6 of Arrays
- Timestamp:
- 09/15/13 09:37:34 (13 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Arrays
v5 v6 1 == TrueArrays ==1 == First-class Arrays == 2 2 3 3 C uses an "array-pointer" pun. This is not a particularly useful feature and leads to a lot of confusion. In C, code such as … … 37 37 for (int i=0; i<5; i++) 38 38 x[i] = i; 39 y = double(x );39 y = double(x,5); 40 40 } 41 41 }}} 42 42 43 is a legal CIVL-C program and at termination y hold the array value {0,2,4,6,8}. Note no pointers are ever used in this program. 43 is a legal CIVL-C program and at termination `y` holds the array value {0,2,4,6,8}. Note no pointers are ever used in this program. 44 45 We call arrays declared in this way **first-class arrays**. 44 46 45 47 Another example: … … 47 49 $message[[]] append_message($message[[]] queue, $message m); 48 50 }}} 49 takes an array of messages and returns an array one longer which is equivalent to the original array with $m$ added. There is no "sharing" between these two arrays.51 might be the declaration of a function that takes an array of messages and returns an array one longer which is equivalent to the original array with $m$ added. There is no "sharing" between these two arrays. 50 52 51 You can still modify double-bracketed arrays andcreate references to elements of them, just as with regular arrays:53 You can still modify first-class arrays and create references to elements of them, just as with regular arrays: 52 54 {{{ 53 void f(int a[[]], int n) { 54 a[0] = 10; 55 55 void f(int a[[]]) { 56 a[0] = 10; 57 } 58 void g(int *p) { 59 *p = 10; 60 } 61 void main() { 62 int a[[1]]; 63 64 a[0] = 1; 65 f(a); 66 // a still is {1} 67 g(&a[0]); 68 // now a = {10} 56 69 } 57 70 }}} 58 71 72 It should be pointed out that anything that can be done with ordinary C arrays can also be done with first-class arrays. One just has to manually insert the conversions (`&a[0]`, or `T*` in place of `T[]` in a formal parameter declaration) that C performs automatically. The ordinary C arrays are kept in CIVL-C just for backwards compatibility. 73 59 74 == Sequence Operations == 60 75 61 The following operations are available for truearrays:76 The following operations are available for first-class arrays: 62 77 78 * T[[]] $array_create(T *p, int n) 79 * returns a new first-class array value obtained from the sequence of elements of length `n` starting from `p` 63 80 * `int $length(T a[[]])` 64 * returns the number of elements in this array81 * returns the number of elements in `a` 65 82 * `T[[]] $concat(T a[[]], T b[[]])` 66 83 * returns the concatenation of two arrays … … 72 89 * returns the sub-array of `a` starting at index `start` and ending at index `end-1` 73 90 * `T[[]] $write(T a[[]], int i, T x)` 74 * returns the array obtained by replacing the element of `a` at index `i` with `x`91 * returns the array value obtained by replacing the element of `a` at index `i` with `x` 75 92 76 93 == Other related ideas ==
