Dynamic Multi Arrays
Posted: Wed Jan 14, 2015 9:27 am
I had the use of dynamic multi arrays (matrixes). Neither number of matrixes, nor their dimensions are known in the program (I use it to play with matrix arithmetic). As described in http://www.purebasic.fr/english/viewtop ... mic+Arrays it is possible to emulate a multi dimensional array in a one dimension array by calculating its position.
I think with the new features AllocateStructure and CopyArray there is an easier way:
1. Define a "dummy" matrix structure and allocate it
2. DIM a temporary array with the dimensions you want
3. Copy "dimmed" Array to allocated array structure. COPYARRAY copies and changes the maximum row and column entries.
4. Now you can work with the matrix in the usal way
A tested sample of allocating dynamic arrays:
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"