Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Type Sample Vector

DEFINITION MODULE SampleVector;

(* Sample definition of a vector type *)


FROM FileIO IMPORT File; (* required for IO *)


(* component type *)

TYPE ComponentType IS REAL; (* arbitrary numeric type *)


(* SampleVector type *)

TYPE
    SampleVector = OPAQUE RECORD ("V-Type")
        capacity : CARDINAL;
        values   : ARRAY capacity OF ComponentType;
    END;


(* cardinality *)

PROCEDURE [HIGH] highestComponentIndex ( v : SampleVector ) : CARDINAL;


(* constructor *)

PROCEDURE Vector ( items : CARDINAL; VARIADIC v[items] OF c : ComponentType ) : SampleVector;
(* constructs a vector from a value list *)


(* accessor *)

PROCEDURE [.] component ( n : CARDINAL; v : SampleVector ) : REAL;
(* Returns the value of the n-th component of vector v *)


(* mutator *)

PROCEDURE [!] setComponent ( n : CARDINAL; VAR v : SampleVector; c : ComponentType );
(* Overwrites the value of the n-th component of vector v with c *)


(* monadic arithmetic operations *) 

PROCEDURE [ABS] abs ( v : SampleVector ) : SampleVector; 

PROCEDURE [NEG] neg ( v : SampleVector ) : SampleVector; 


(* dyadic arithmetic operations *) 

PROCEDURE [+] add ( v1, v2 : SampleVector ) : SampleVector; 

PROCEDURE [-] sub ( v1, v2 : SampleVector ) : SampleVector; 

PROCEDURE dot ( v1, v2 : SampleVector ) : ComponentType;

PROCEDURE scalar ( a : ComponentType; v : SampleVector ) : SampleVector; 


(* relational operations *) 

PROCEDURE [=] isEqual ( v1, v2 : SampleVector ) : BOOLEAN; 


(* IO operations, bound to READ, WRITE and WRITEF *)

PROCEDURE Read( infile : File; VAR v : SampleVector );
(* Reads the textual representation of a SampleVector value in simple format from input stream infile
   - any leading whitespace is skipped
   - any remaining characters that are part of the numeral being read are removed from infile
   - the numeric value of the numeral string read is assigned to the variable passed in for v
   - the file status is set to any of success, outOfRange, wrongFormat, endOfLine, or endOfInput *)

PROCEDURE Write( outfile : File; v : SampleVector );
(* Writes the textual representation of SampleVector value v in simple format to output stream outfile *)

PROCEDURE WriteF( outfile : File; fmtStr : ARRAY OF CHAR;
                  items : CARDINAL; VARIADIC v[items] OF vector : SampleVector );
(* Writes a formatted textual representation of one or more SampleVector values to output stream outfile. The
   value of parameter items is calculated and inserted automatically. The output format is determined by fmtStr. *)

END SampleVector.