16.3 The Append Statement

 

 The append statement is part of a facility in Basis which assists in the process of collecting lists of values, such as time histories, in an efficient manner. The components of the facility are:

The routine setlast(name, n) limits the LAST dimension (only) of the variable name to length of n. If n is greater than the current length (unlimited) of last subscript of name, then an attempt is made to expand storage so that the length will be at least n.

If n is greater than the current maximum value, then the maximum is set to 1.5 times the existing value or at least 16. This exponential growth is used to help reduce memory fragmentation while preserving constant time operation cost. setlast can be used on static arrays as long as no attempt is made to exceed the actual storage available. 

 

The append operator := works as follows: x := y is equivalent to storing y after the current end of x, increasing the final subscript of x appropriately (using setlast's internal routine rtsetdl). y must be of an appropriate shape to be so stored. If y is of the same dimension as x, y is viewed as an array of values to be added, and the final subscript of x will increase appropriately. If x is a scalar, it is first made a one-dimensional vector of length 1.

rtadddim("name") adds a new subscript of 1 to name. This can be useful in setting name up as a target for a :=.

Example 1:

real x(3,3) 
call setlast("x", 2)  # x will act as if it is shaped (3,2)

Example 2:

real x(3,3) 
call setlast("x", 0)  #x will act as if it is shaped (3,0) 
x:=iota(3)   # now x is (3,1) (but storage is still (3,3) ) 
x:=iota(3)   # now x is (3,2) (but storage is still (3,3) ) 
x:=iota(3)   # now x is (3,3) (but storage is still (3,3) ) 
x:=iota(3)   # now x is (3,4) (but storage is now   (3,16) ) 
x:=iota(3)   # now x is (3,5) (but storage is still (3,16) ) 
x:=[iota(3),iota(3)] 
             # now x is (3,7) (but storage is still (3,16) )

Example 3:

integer y(0) # set up an empty array 
integer i 
do i=1, 1000 
   y:=i 
enddo 
# After this loop, y is the same as iota(1000)