I think with the new features AllocateStructure and CopyArray there is an easier way:
1. Define a "dummy" matrix structure and allocate it
Code: Select all
Structure Matrix
Array.i(0,0)
EndStructure
*p.matrix=AllocateStructure(Matrix)
Code: Select all
rows=??
cols=??
Global DIM TempMatrix(rows,cols)
Code: Select all
COPYARRAY(TempMatrix(),*p\Array())
FreeArray(TempMatrix())
Code: Select all
COPYARRAY(TempMatrix(),*p\Array())
for i=0 to rows
for j=0 to cols
*p\array(i,j)=???
next
next
Code: Select all
; ----------------------------------------------------------------------------------------------
; Structure containing details of dynamically allocated Matrixes
; ----------------------------------------------------------------------------------------------
Structure Matrix
Name$
rows.i
cols.i
*matPtr
EndStructure
;
; ----------------------------------------------------------------------------------------------
; Structure of dynamically allocated Matrixes
; ----------------------------------------------------------------------------------------------
Structure MatArray
Array matrix.i(0,0)
EndStructure
;
; ----------------------------------------------------------------------------------------------
; Allocate MAP containg details of allocated Matrixes
; ----------------------------------------------------------------------------------------------
Global NewMap Matrix.Matrix()
;
; ----------------------------------------------------------------------------------------------
; Create Dynamic Matrix
; ----------------------------------------------------------------------------------------------
Procedure MatrixCreate(name$,rows,cols)
Protected Dim TempMatrix(rows,cols) ; Create temporary Matrix with requested rows and columns
Protected *matx.MatArray
name$=UCase(name$)
*matx=AllocateStructure(matArray) ; Create target Matrix dimension(0,0)
CopyArray(TempMatrix(),*matx\matrix()) ; CopyArray overrides dimensions of target array
Matrix(name$)\Name$=UCase(name$) ; Save array information in Map
Matrix(name$)\rows=rows
Matrix(name$)\cols=cols
Matrix(name$)\matPtr=*matx
FreeArray(TempMatrix()) ; Free temporary Matrix, no longer needed
ProcedureReturn *matx ; return pointer to created Matrix
EndProcedure
;
; ----------------------------------------------------------------------------------------------
; Get Pointer to any dynamic Matrix
; ----------------------------------------------------------------------------------------------
Procedure MatrixADDR(name$)
Protected *matptr.matArray
name$=UCase(name$)
*matptr=Matrix(name$)
If *Matptr=0
ProcedureReturn -1
EndIf
ProcedureReturn Matrix(name$)\matPtr
EndProcedure
;
; ----------------------------------------------------------------------------------------------
; Debug requested Matrix
; ----------------------------------------------------------------------------------------------
Procedure MatrixDebug(name$)
Protected *matptr.matArray, rows, cols, *m.Matarray, line$, i, j
Debug "Matrix "+name$
name$=UCase(name$)
*matptr=Matrix(name$)
If *Matptr=0
ProcedureReturn -1
EndIf
rows=Matrix(name$)\rows
cols=Matrix(name$)\cols
*m=Matrix(name$)\matPtr
For i=1 To rows
line$=""
For j=1 To cols
Line$+RSet(Str(*m\matrix(i,j)),6,"0")+" "
Next
Debug "("+RSet(Str(i),4)+") "+line$
Next
EndProcedure
;
; ----------------------------------------------------------------------------------------------
; Main Program
; ----------------------------------------------------------------------------------------------
;
Define *m.Matarray, i, j, ct
;
MatrixCreate("Fred",47,21) ; Create Matrix "Fred", with it required dimensions
MatrixCreate("Annie",10,10) ; Create Matrix "Annie" ...
;
*m=MatrixADDR("Fred") ; Get Pointer to "Fred"
For i=1 To 47 ; Set Matrix with Values
For j=1 To 21
ct+1
*m\matrix(i,j)=ct
Next
Next
;
*m=MatrixADDR("Annie") ; Get Pointer to "Annie"
For i=1 To 10 ; Set Matrix with Values
For j=1 To 10
*m\matrix(i,j)=i+j
Next
Next
; Debug both Matrixes
MatrixDebug("Fred") ; Debug Matrix "Fred"
MatrixDebug("Annie") ; Debug Matrix "Annie"