gm2
[Top][All Lists]

## Re: Dynamic mutidimensional arrays

 From: Benjamin Kowarsch Subject: Re: Dynamic mutidimensional arrays Date: Wed, 5 Apr 2023 18:59:52 +0900

Here is another variant of the earlier example library using a predefined allocation size, but dynamic bounding.

DEFINITION MODULE Matrix;

IMPORT Vector;
FROM Vector IMPORT VectorT;

CONST
MaxRows = 50;
MaxColumns = 50;
MaxCapacity = MaxRows*MaxColumns;

TYPE Matrix; (* OPAQUE *)

TYPE MatrixT = Matrix; (* for unqualified use *)

TYPE MutatorProc = PROCEDURE ( Matrix );

(* allocator, passes back null matrix *)
PROCEDURE New ( VAR m : Matrix; rows, columns : CARDINAL );

(* introspection *)
PROCEDURE rowCount ( m : Matrix ) : CARDINAL;

PROCEDURE columnCount ( m : Matrix ) : CARDINAL;

PROCEDURE isInvertible ( m : Matrix ) : BOOLEAN;

(* constructors *)
PROCEDURE NewWithRows ( VAR m : Matrix; rowValues : ARRAY OF VectorT );

PROCEDURE NewWithColumns ( VAR m : Matrix; colValues : ARRAY OF VectorT );

(* mutators *)
PROCEDURE StoreValue ( m : Matrix; row, col : CARDINAL; value : REAL )
<*INLINE*>;

PROCEDURE StoreRow ( m : Matrix; rowIndex : CARDINAL; rowValues : ARRAY OF VectorT )
<*INLINE*>;

PROCEDURE StoreColumn ( m : Matrix; colIndex : CARDINAL; colValues : ARRAY OF VectorT )
<*INLINE*>;

(* accessors *)
PROCEDURE value ( m : Matrix; row, col : CARDINAL ) : REAL
<*INLINE*>;

PROCEDURE row ( m : Matrix; rowIndex : CARDINAL ) : VectorT
<*INLINE*>;

PROCEDURE column ( m : Matrix; colIndex : CARDINAL ) : VectorT
<*INLINE*>;

(* operations *)
PROCEDURE eq ( m1, m2 : Matrix ) : BOOLEAN;

PROCEDURE sum ( m1, m2 : Matrix ) : Matrix;

PROCEDURE diff ( m1, m2 : Matrix ) : Matrix;

PROCEDURE prod ( m1, m2 : Matrix ) : Matrix;

PROCEDURE scalarProd ( m : Matrix; scalar : REAL ) : Matrix;

PROCEDURE vectorProd ( m : Matrix; vector : VectorT ) : VectorT;

PROCEDURE inverse ( m : Matrix ) : Matrix;

PROCEDURE transpose ( m : Matrix ) : Matrix;

(* iterators, user defined mutator procedures should be marked inline *)

PROCEDURE WithValuesDo ( m : Matrix; proc : MutatorProc );

PROCEDURE WithRowDo ( m : Matrix; proc : MutatorProc );

PROCEDURE WithColumnDo ( m : Matrix; proc : MutatorProc );

(* destructor, deallocates and passes back NIL *)
PROCEDURE Release ( VAR m : Matrix );

END Matrix.

IMPLEMENTATION MODULE Matrix;

TYPE Matrix = POINTER TO RECORD
rows, columns : CARDINAL;
capacity : LONGCARD;
data : ARRAY [0..MaxCapacity-1] OF REAL
END; (* Matrix *)

...

END Matrix.

hth
rgds
benjamin