Page 1 of 1

Defining dynamic arrays in a Structure?

Posted: Sun May 04, 2014 11:07 pm
by eths
How can one define (and use) an array element of unknown size in a structure ?

I may be a bit dense but I can't figure out how to do something like this in PureBasic gracefully:

Code: Select all

Structure Gain
  vert.d
  hori.d
  total.d
EndStructure

Structure Polarization
  axialratio.d
  tilt.d
  sense.s
EndStructure

Structure E
  mag.d
  phase.d
EndStructure

Structure Angle
  theta.d
  phi.d
EndStructure

Structure eTheta
  Theta.E
EndStructure

Structure ePhi
  Phi.E
EndStructure

Structure RaditionPoint
  ang.Angle
  gain.Gain
  pol.Polarization
  et.eTheta
  ep.ePhi
EndStructure

Structure RadiationStruc
  X.b 
  N.b 
  D.b 
  A.b
  I1.d
  NTH.d
  NPH.d 
  THETS.d
  PHIS.d
  DTH.d 
  DPH.d 
  RFLD.d 
  GNOR.d
  radp.RaditionPoint(,) ;two dimensional array of unknown size
EndStructure


Dim radpat.RadiationStruc(2)

; calculate the required size of the arrays...

Dim a.RaditionPoint(x1,y1)
Dim b.RaditionPoint(x2,y2)
Dim c.RaditionPoint(x3,y3)

;fill a, b, and c with data, e.g. with data from a file...

;assign arrays to "instances" of radpat
radpat(0)\radp = a
radpat(1)\radp = b
radpat(2)\radp = c

For foo = 0 To 2
  With radpat(foo)
    For i = 0 To ArraySize(\radp() ,1)
      For j = 0 To ArraySize(\radp() ,2)
        ;do something cool with radpat(foo)\radp(i,j)
      Next
    Next
  EndWith
Next
The background is I want to parse files - output from NEC, an antenna simulation software - which contains one or more blocks which look like this:

Code: Select all

   - - ANGLES - -          - POWER GAINS -        - - - POLARIZATION - - -    - - - E(THETA) - - -    - - - E(PHI) - - -
  THETA     PHI        VERT.   HOR.    TOTAL      AXIAL     TILT   SENSE     MAGNITUDE    PHASE      MAGNITUDE    PHASE 
 DEGREES  DEGREES       DB      DB      DB        RATIO     DEG.              VOLTS/M    DEGREES      VOLTS/M    DEGREES
    0.00     0.00    -999.99   -1.33   -1.33    0.00000   -90.00  LINEAR    1.96723E-15     4.31    1.28490E-04   184.31
    2.00     0.00    -999.99   -0.89   -0.89    0.00000   -90.00  LINEAR    2.06740E-15    12.81    1.35115E-04   192.81
    4.00     0.00    -999.99   -0.55   -0.55    0.00000   -90.00  LINEAR    2.14614E-15    21.13    1.40518E-04   201.13
    6.00     0.00    -999.99   -0.31   -0.31    0.00000   -90.00  LINEAR    2.19989E-15    29.32    1.44478E-04   209.32
    8.00     0.00    -999.99   -0.17   -0.17    0.00000   -90.00  LINEAR    2.22560E-15    37.39    1.46794E-04   217.39
   10.00     0.00    -999.99   -0.14   -0.14    0.00000   -90.00  LINEAR    2.22081E-15    45.40    1.47290E-04   225.40
   12.00     0.00    -999.99   -0.23   -0.23    0.00000   -90.00  LINEAR    2.18379E-15    53.38    1.45821E-04   233.38

; many lines of data removed

    0.00     3.00     -26.95   -1.34   -1.33    0.00000    87.00  LINEAR    6.72465E-06   184.31    1.28314E-04   184.31
    2.00     3.00     -26.52   -0.90   -0.89    0.00000    87.00  LINEAR    7.06663E-06   192.80    1.34922E-04   192.80
    4.00     3.00     -26.19   -0.56   -0.55    0.00000    87.01  LINEAR    7.33552E-06   201.11    1.40312E-04   201.11
    6.00     3.00     -25.98   -0.32   -0.31    0.00000    87.02  LINEAR    7.51923E-06   209.28    1.44266E-04   209.28
    8.00     3.00     -25.88   -0.18   -0.17    0.00000    87.03  LINEAR    7.60733E-06   217.35    1.46583E-04   217.35
   10.00     3.00     -25.90   -0.15   -0.14    0.00000    87.05  LINEAR    7.59147E-06   225.35    1.47088E-04   225.35
   12.00     3.00     -26.04   -0.24   -0.23    0.00000    87.07  LINEAR    7.46576E-06   233.32    1.45638E-04   233.32

; many lines of data removed

    0.00     6.00     -26.95   -1.34   -1.33    0.00000    87.00  LINEAR    6.72465E-06   184.31    1.28314E-04   184.31
    2.00     6.00     -26.52   -0.90   -0.89    0.00000    87.00  LINEAR    7.06663E-06   192.80    1.34922E-04   192.80
    4.00     6.00     -26.19   -0.56   -0.55    0.00000    87.01  LINEAR    7.33552E-06   201.11    1.40312E-04   201.11
    6.00     6.00     -25.98   -0.32   -0.31    0.00000    87.02  LINEAR    7.51923E-06   209.28    1.44266E-04   209.28
    8.00     6.00     -25.88   -0.18   -0.17    0.00000    87.03  LINEAR    7.60733E-06   217.35    1.46583E-04   217.35
   10.00     6.00     -25.90   -0.15   -0.14    0.00000    87.05  LINEAR    7.59147E-06   225.35    1.47088E-04   225.35
   12.00     6.00     -26.04   -0.24   -0.23    0.00000    87.07  LINEAR    7.46576E-06   233.32    1.45638E-04   233.32

; etc. etc. etc. etc.
The number of blocks and the length of each block of data is unknown before reading the file.

Re: Defining dynamic arrays in a Structure?

Posted: Sun May 04, 2014 11:20 pm
by skywalk

Code: Select all

  ...
  Array radp.RaditionPoint(0,0) ;two dimensional array of unknown size
  ;     Remember to ReDim this array later when size is known
EndStructure

; Not sure how you want to structure your data, but this at least compiles...

;assign arrays to "instances" of radpat
radpat(0)\radp(0,0)\ang = a(0,0)\ang
radpat(1)\radp(0,0)\ang = b(0,0)\ang
radpat(2)\radp(0,0)\ang = c(0,0)\ang

Re: Defining dynamic arrays in a Structure?

Posted: Sun May 04, 2014 11:29 pm
by eths
Thanks. I'll try doing this...

I was hoping I could assign an array to an instance of the structure, instead of rediming the array in the structure instance and then assigning the values "directly" in the structure.

Re: Defining dynamic arrays in a Structure?

Posted: Sun May 04, 2014 11:37 pm
by eths
redim does not work because I need to change the size both dimensions...

Time to rethink... ;-)

Re: Defining dynamic arrays in a Structure?

Posted: Sun May 04, 2014 11:43 pm
by IdeasVacuum
Why not turn that around and use a Linked List of a structure? Each new List element would consist of a structure - Have a single structure containing all variables for each data element you are reading from the file. Where is the Radiation Point data in the file? Can't see it in your sample? Just how variable is that Point Data? If it is always within a specific range, then may be you can pre-dimension an array to the max range value.

Re: Defining dynamic arrays in a Structure?

Posted: Sun May 04, 2014 11:46 pm
by IdeasVacuum
You might be able to use separate arrays e.g. Dim aRaditionPointX(iTotal), Dim aRaditionPointY(iTotal)

Re: Defining dynamic arrays in a Structure?

Posted: Mon May 05, 2014 12:01 am
by eths
IdeasVacuum wrote:Why not turn that around and use a Linked List of a structure? Each new List element would consist of a structure - Have a single structure containing all variables for each data element you are reading from the file. Where is the Radiation Point data in the file? Can't see it in your sample? Just how variable is that Point Data? If it is always within a specific range, then may be you can pre-dimension an array to the max range value.
One line of the example data is one "Radiation Point" and later, among other things I want to create a three dimensional representation of the radiation diagram.

I was thinking of using the data something like this (somewhat simplified):

Code: Select all

  ;CreateMesh
  With radpat(foo)
    For i = 0 To ArraySize(\radp() ,1)
      For j = 0 To ArraySize(\radp() ,2)
        ;define a 3d rectangular shape (two triangles) within a mesh using data from radpat(foo)\radp(i,j), radpat(foo)\radp(i+1,j), radpat(foo)\radp(i,j+1) and radpat(foo)\radp(i+1,j+j)
      Next
    Next
  EndWith
 ;FinishMesh(#False)
I don't think this would be so simple with a list or map.

Re: Defining dynamic arrays in a Structure?

Posted: Mon May 05, 2014 12:02 am
by eths
IdeasVacuum wrote:You might be able to use separate arrays e.g. Dim aRaditionPointX(iTotal), Dim aRaditionPointY(iTotal)
I know, but is it elegant & graceful?

;-)

Re: Defining dynamic arrays in a Structure?

Posted: Mon May 05, 2014 12:41 am
by IdeasVacuum
I don't think this would be so simple with a list or map.
Well, you would have elegant code using ForEach with a List, but for sure the task seems complex.
One line of the example data is one "Radiation Point"
That's confusing because your pseudo code is looking at x,y but the sample data lines are:
angThetaDeg, angPhiDeg, pgVertDb, pgHorDb, pgTotalDb, polAxialRatio, polTiltDeg, polSense, eThetaMagVoltsM, eThetaPhaseDeg, ePhiMagVoltsM, ePhiPhaseDeg

Re: Defining dynamic arrays in a Structure?

Posted: Mon May 05, 2014 12:56 am
by skywalk
eths wrote:I know, but is it elegant & graceful? ;-)
These are subjective qualities and cannot be exactly determined. For me, elegance is how readable complex code can be 6 months or years later.
My advice is to think how you want to process the data you import and then design your structures accordingly. Are these scalar quantities or complex numbers? That will impact math functions too.

Re: Defining dynamic arrays in a Structure?

Posted: Mon May 05, 2014 7:10 am
by eths
IdeasVacuum wrote:
I don't think this would be so simple with a list or map.
Well, you would have elegant code using ForEach with a List, but for sure the task seems complex.
Clumsy strike me as more fitting, in this case, than iterating the two indexes of a two dimensional array. I'll probablly use a single dimensional array and have to do some extra index offset math for reading the values out of the array. Defining a structure or a class with an empty array (dim foo as bar(,)), which is assigned later during runtime is very simple, as long as the number of dimensions is unchanged, in VB.Net, for example. I had hoped to do the same in PureBasic. This is the first time in years that I have worked with PureBasic, so I am currently relearning the dialect.
IdeasVacuum wrote:
One line of the example data is one "Radiation Point"
That's confusing because your pseudo code is looking at x,y but the sample data lines are:
angThetaDeg, angPhiDeg, pgVertDb, pgHorDb, pgTotalDb, polAxialRatio, polTiltDeg, polSense, eThetaMagVoltsM, eThetaPhaseDeg, ePhiMagVoltsM, ePhiPhaseDeg
From a 3D graphics point of view, ThetaDeg and PhiDeg are vector angles from the center point and the gain, e.g. TotalDb, is the length of the vector from center. These three values would be translated into X, Y, Z coordinates for building the mesh. Also the code is early days and I simplified it for the posting here.

Re: Defining dynamic arrays in a Structure?

Posted: Mon May 05, 2014 7:14 am
by eths
skywalk wrote:These are subjective qualities and cannot be exactly determined. For me, elegance is how readable complex code can be 6 months or years later.
My advice is to think how you want to process the data you import and then design your structures accordingly.
Very true.

skywalk wrote:Are these scalar quantities or complex numbers? That will impact math functions too.
The numbers are not complex and rather straight forward.