Changes between Version 5 and Version 6 of Arrays


Ignore:
Timestamp:
09/15/13 09:37:34 (13 years ago)
Author:
siegel
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Arrays

    v5 v6  
    1 == True Arrays ==
     1== First-class Arrays ==
    22
    33C uses an "array-pointer" pun.  This is not a particularly useful feature and leads to a lot of confusion.  In C, code such as
     
    3737  for (int i=0; i<5; i++)
    3838    x[i] = i;
    39   y = double(x);
     39  y = double(x,5);
    4040}
    4141}}}
    4242
    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.
     43is 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
     45We call arrays declared in this way **first-class arrays**.
    4446
    4547Another example:
     
    4749  $message[[]] append_message($message[[]] queue, $message m);
    4850}}}
    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.
     51might 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.
    5052
    51 You can still modify double-bracketed arrays and create references to elements of them, just as with regular arrays:
     53You can still modify first-class arrays and create references to elements of them, just as with regular arrays:
    5254{{{
    53 void f(int a[[]], int n) {
    54   a[0] = 10;
    55  
     55void f(int a[[]]) {
     56  a[0] = 10; 
     57}
     58void g(int *p) {
     59  *p = 10;
     60}
     61void 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}
    5669}
    5770}}}
    5871
     72It 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
    5974== Sequence Operations ==
    6075
    61 The following operations are available for true arrays:
     76The following operations are available for first-class arrays:
    6277
     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`
    6380* `int $length(T a[[]])`
    64  * returns the number of elements in this array
     81 * returns the number of elements in `a`
    6582* `T[[]] $concat(T a[[]], T b[[]])`
    6683 * returns the concatenation of two arrays
     
    7289 * returns the sub-array of `a` starting at index `start` and ending at index `end-1`
    7390* `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`
    7592
    7693== Other related ideas ==