Protected Parts of VMatrix

The protected part of VMatrix must be accessible to the matrix stack. The signature functions as in the vmm and vdoub. It provides an indication of how corrupt or active the matrix has become.

The function mindex( int i, int j) calculates a vdoub vector index.[*] Stripped of the casts, the function is simply (i-1)*c + (j-1) , where i and j are matrix indices, and c is the number of columns of the matrix. Matrices are indexed from 1 to r, and 1 to c, so there are r*c elements in the corresponding vdoub. However the vdoub indices range from 0 to r*c - 1, so the i-1 and j-1 force the minimum matrix indices to be 1. This calculation forces the matrix to be in row major order. The curvecind is a long integer containing the vdoub index of the current matrix element.

The name is a string type which can be manipulated in the matrix functions. The functions are found in the public part, but are Nameit(), Getname(), Showname(), and StringAddress().

The next three functions control the allocation of the vdoub vectors. Replace() calls PurgeVectors() and SetupVectors() if the VMatrix being replaced has different dimensions than the replacing VMatrix. Replace() is called by the operator=(). PurgeVectors() calls vfree() to free the hdr in the protected part of vdoub. SetupVectors() calls vmalloc() to create a new hdr for vdoub.[*]

For the in-ram version, SetupVectors() allocates r*c+1 elements on the heap using calloc(). The first element is set to SIGNATURE, then the base is shifted by 1. Thus, m[-1] is a check value. If the value is not equal to SIGNATURE, then the matrix has been deleted or clobbered.

The function NewReference() is used by the matrix stack to set up a reference to a base vector instead of a copy. It is part of the deep copy technique used by push.