bi dimensionnal static array

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

bi dimensionnal static array

Post by Mesa »

As a wish, a bi dimensionnal static array in structure should be good to have, there are a lot of them in C code.

Structure str2d
Tab[2][3]
EndStructure

(Yes i can do Tab[2x3] but it is prone to making mistakes, especially if I want to achieve Tab[x][y])

Mesa.
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: bi dimensionnal static array

Post by NicTheQuick »

The only thing that is similar to your wish would be this 2-structure solution as a workaround:

Code: Select all

Structure tab_y
	y.i[3]
EndStructure
Structure str2d
	tab.tab_y[2]
EndStructure

Define v.str2d
v\tab[0]\y[0] = 11
v\tab[1]\y[1] = 22
v\tab[1]\y[2] = 23

Debug v\tab[0]\y[0]
Debug v\tab[1]\y[1]
Debug v\tab[1]\y[2]
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: bi dimensionnal static array

Post by Mesa »

Very good trick.

We can use a macro.

Code: Select all

Macro Array2D(name, type, cx, cy)
	Structure y
	y.type[cy]
EndStructure
Structure xy
	x.y[cx]
EndStructure
EndMacro

Macro Array2DD(name, type, namex, namey, cx, cy)
	Structure namey
	namey.type[cy]
EndStructure
Structure namex#namey
	namex.y[cx]
EndStructure
EndMacro


; Array2D(Tab, i, 2, 3)
Array2DD(Tab, i, x, y, 2, 3)

Define Tab.xy
Tab\x[0]\y[0] = 11
Tab\x[1]\y[1] = 22
Tab\x[1]\y[2] = 23

Debug Tab\x[0]\y[0]
Debug Tab\x[1]\y[1]
Debug Tab\x[1]\y[2]
M.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: bi dimensionnal static array

Post by skywalk »

I find it easier without a nested structure :wink:

Code: Select all

  Macro arRC(Row, Col)    ;FYI; (Row, Col) is Human interpretation, NOT original Dim(col,row) order!
    (nRows * Row + Col)   ; Row-major. PureBasic's default.
  EndMacro
  ;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  ; Static array in Structure
  ;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  If 1  ;-! Row-major ar[3*3] example.
    nRows=3 : nCols=3
    #nRows = 3
    #nCols = 3
    #nCols2 = 2
    Structure arSt
      ar1D.i[#nRows*#nCols]
    EndStructure
    Structure arSt2
      ar1D.i[#nRows*#nCols2]
    EndStructure
    Define arSt.arSt
    Define arSt2.arSt2
    With arSt
      \ar1D[0]=1 :   \ar1D[1]=2 :   \ar1D[2]=3
      \ar1D[3]=4 :   \ar1D[4]=5 :   \ar1D[5]=6
      \ar1D[6]=7 :   \ar1D[7]=8 :   \ar1D[8]=9
    EndWith
    Dim ar2D.i(nCols-1, nRows-1)    ;<-- 2D array
    ar2D(0,0)=1 :    ar2D(0,1)=2 :  ar2D(0,2)=3
    ar2D(1,0)=4 :    ar2D(1,1)=5 :  ar2D(1,2)=6
    ar2D(2,0)=7 :    ar2D(2,1)=8 :  ar2D(2,2)=9
    Debug "-- Compare 3x3 arSt\ar1D[nCols*nRows] array to ar2D(nCols-1,nRows-1) --"
    Debug "ar1D[0,2] = " + Str(arSt\ar1D[arRC(0,2)])
    Debug "ar2D(0,2) = " + Str(ar2D(0,2))
    Debug "ar1D[1,1] = " + Str(arSt\ar1D[arRC(1,1)])
    Debug "ar2D(1,1) = " + Str(ar2D(1, 1))
    Debug "ar1D[1,2] = " + Str(arSt\ar1D[arRC(1,2)])
    Debug "ar2D(1,2) = " + Str(ar2D(1, 2))
    ShowMemoryViewer(@arST\ar1D[0], 512)
    ;CallDebugger
    ShowMemoryViewer(@ar2D(0,0), 512)
    ;-! Row-major ar[3*2] example.
    nRows=3: nCols=2
    With arSt2
      \ar1D[0]=1 :   \ar1D[1]=2 :  \ar1D[2]=3
      \ar1D[3]=4 :   \ar1D[4]=5 :  \ar1D[5]=6
    EndWith
    Dim ar2D(nCols-1, nRows-1)     ;<-- 2D array
    ar2D(0,0)=1 :    ar2D(0,1)=2 : ar2D(0,2)=3
    ar2D(1,0)=4 :    ar2D(1,1)=5 : ar2D(1,2)=6
    Debug "-- Compare 3x2 arSt\ar1D[nCols*nRows] array to ar2D(nCols-1,nRows-1) --"
    Debug "ar1D[0,2] = " + Str(arSt2\ar1D[arRC(0,2)])
    Debug "ar2D(0,2) = " + Str(ar2D(0,2))
    Debug "ar1D[1,1] = " + Str(arSt2\ar1D[arRC(1,1)])
    Debug "ar2D(1,1) = " + Str(ar2D(1, 1))
    Debug "ar1D[1,2] = " + Str(arSt2\ar1D[arRC(1,2)])
    Debug "ar2D(1,2) = " + Str(ar2D(1, 2))
    ShowMemoryViewer(@arST2\ar1D[0], 512)
    CallDebugger
    ShowMemoryViewer(@ar2D(0,0), 512)
  EndIf
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
threedslider
Enthusiast
Enthusiast
Posts: 377
Joined: Sat Feb 12, 2022 7:15 pm

Re: bi dimensionnal static array

Post by threedslider »

You can even do it directly with C Backend :)

In console, you type as here more short :

Code: Select all

OpenConsole()

! typedef struct str2d { int tab[3][2]; } str2d;

! str2d t;

! t.tab[0][0] = 00;
! t.tab[0][1] = 01;
! t.tab[1][0] = 10;
! t.tab[1][1] = 11;
! t.tab[2][0] = 20;
! t.tab[2][1] = 21;

!printf("%d\n", t.tab[0][0]);
!printf("%d\n", t.tab[0][1]);
!printf("%d\n", t.tab[1][0]);
!printf("%d\n", t.tab[1][1]);
!printf("%d\n", t.tab[2][0]);
!printf("%d\n", t.tab[2][1]);


Input()
Post Reply