[net.lang.mod2] Arrays in Modula-2

powell@decwrl.UUCP (Mike Powell) (10/19/84)

Don't do anything rash (e.g., Ada).  I have recently extended the DECWRL
Modula-2 compiler to better support array operations.  At the end of this
message is a summary of the features provided.  We have converted several
scientific benchmarks to use them (e.g., Linpak), and they look pretty good.

To the minimalists in the audience, we paid substantial attention in designing
these extensions to keep them easy to implement and cheap to use.  I believe
our implementation is similar in spirit and performance to regular Modula-2
open arrays.

Having said that, I would like to solicit people's reactions to the extensions.

					Michael L. Powell
					Digital Equipment Corporation
					Western Research Laboratory
					Los Altos, CA  94022
					{decvax,ucbvax}!decwrl!powell
					powell@decwrl


The following extensions have been added to the DECWRL Modula-2 compiler to
support more flexible array operations:

Open array parameters are now multi-dimensional.  It is now legal to declare

    procedure P(a2: array of array of integer);

The builtin functions high, low, and number accept an optional constant second
parameter that specifies which dimension you want.

One can specify subranges of elements to be used for open array parameters.
E.g., specifying a formal parameter of a[i:n] passes an open array consisting
of the n elements a[i],a[i+1],...,a[i+n-1].

There is a new type of open array parameter, called a subarray.  E.g.,

    procedure Q(a2s: subarray of array of integer; a1s: subarray of integer);

Subarray parameters may have parameters whose elements are not contiguous.
Two examples are subarrays of two-dimensional arrays or columns of arrays.
If you had

    var a: array [1..10],[1..10] of integer;

you could pass the lower right 4x4 submatrix and the 4th column of a by doing

    Q(a[7:4,7:4], a[1:10,4]);

There is a new data type called a dynamic array.  Dynamic arrays are allocated
on the heap and have sizes determined at runtime.  Dynamic arrays are like
pointers to arrays.  Subscripts are checked if checking is enabled.  E.g.,

    var da1: dynarray of integer;		(* 1-d dynamic array *)
	da2: dynarray of array of integer;	(* 2-d dynamic array *)
	almostArgv: dynarray of dynarray of char;
		(* 1-d dynamic array of 1-d dynamic arrays *)

Dynamic arrays are referenced as follows:
	new(da2,n*3,20);		(* allocate a dynamic array *)
	dispose(da2);			(* deallocate a dynamic array *)
	i := number(da2,2);		(* high, low, and number work *)
	i := da1^[i];			(* normal subscripting *)
	da2^[i,j] := i;
	c := almostArgv^[i]^[j];
	P(da2^);			(* pass as an open array parameter *)
	Q(da2^[i:m,j:n],dada^[i]^);	(* subarrays work, too *)

For systems programming, there are extensions similar to those already provided
by the DECWRL Modula-2 compiler.  Like pointers, dynamic arrays may optionally
have address checking turned off.  Like array parameters, dynamic arrays may
optionally have subscript checking turned off.  These features make it easier
to interact with languages (e.g., C) that have arrays without bounds.