next up previous contents
Next: OBJECTCLASS: an Object-Oriented Up: CHAPTER.9 RECORDSVECTORS Previous: Defining new record

Defining new vector types in Pop-11

A vector class is a class of data whose instances may be of different lengths, though once an instance is created, its length cannot be changed.

Pop-11 includes two main built in vector classes, namely strings and vectors. A string is an instance of a vector class that is constrained to contain an ordered set of characters, i.e. 8-bit integers. Strings may be of different lengths, but each component of a string must be an 8-bit character. E.g.

    'a tiny string'         'a much longer string of characters'
Ordinary Pop-11 vectors can also be of different lengths, but their contents are unconstrained.

    {1 2 3}     {1 2 3 a vector with numbers [words and] [lists]}
Vectors play a major role in the construction of arrays. See REF ARRAYS Roughly speaking a multidimensional array in Pop-11 is a combination of a one dimensional vector and a procedure for mapping between the high dimensional array and the low dimensional vector.

Using DEFCLASS and VECTORCLASS

The defclass construct described in REF DEFSTRUCT can be used to create new vector classes as well as new record classes. However it is often simpler to use the vectorclass construct.

Example of creation of a new vector class.

This example is based on HELP VECTORCLASS. The imperative

    vectorclass short 16;
declares a new vector class called "short" which is restricted to containing 16 bit integers in its fields. This creates procedures

    initshort, consshort, destshort, isshort
    subscrshort, fast_subscrshort,
and declares the variable

    short_key
with the new key as its value.

The newer syntax for doing this uses defclass, as follows:

    defclass short :16;
To create an instance of short, with 32 fields do this:

    vars shortie = initshort(32);
    shortie =>
    ** <short 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
         0 0>
By default the number 0 is put into each of the fields of a newly created short vector. But this can be changed, e.g. by using the updater of the subscriptor function subscrshort:

    99 -> subscrshort(3, shortie);
    shortie =>
    ** <short 0 0 99 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
         0 0 0>
Because of the use of the number "16" in its declaration, vectors of the class "short" can only contain integers in the range 0 to (2 ** 16) - 1. Attempting to assign something else will cause an error. E.g.

    "cat" -> subscrshort(5, shortie);

    ;;; MISHAP - INTEGER 0 TO 65535 NEEDED
    ;;; INVOLVING:  cat
    ;;; DOING    :  subscrshort ....
By contrast, the word "undef" is used as the default value for fields of unconstrained vectors. For example, create a five element ordinary vector:

    vars vec5 = initv(5);

    vec5 =>
    ** {undef undef undef undef undef}
Ordinary vectors have associated procedures initv, consvector, isvector, subscrv, destvector. See REF VECTORS

The anomaly that some of the names end in "v" and some in "vector" is a historical accident. Similarly for strings the corresponding procedures are inits, consstring, isstring, subscrs, deststring. (See REF STRINGS).

For creating ordinary vectors the `curly braces' can be used to simplify the syntax.

There are several procedures that apply equally to different sorts of vectors, notably <> which can be used to join two vectors of the same type, e.g. two strings, or two full vectors:

    'one string ' <> 'and another' =>
    ** one string and another

    {one vector} <> {and another} =>
    ** {one vector and another}
As explained above, there are other generic procedures that apply equally to vectors and records, e.g. appdata, fill, explode, datalist, copydata.



next up previous contents
Next: OBJECTCLASS: an Object-Oriented Up: CHAPTER.9 RECORDSVECTORS Previous: Defining new record



Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998