News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Array Commands

Started by kevin, March 27, 2004, 12:20:41 PM

Previous topic - Next topic

kevin


Array Commands BackGround INFO

   

 Just going over the array commands.   After a bit use now I'm going to change one aspect of how they work.  Currently When you use a command like FindARRAYCells for example.  We have to pass this command 5 parameters..

FoundInSteps=FindARRAYCells (SearchMyArray,StartElementAddress,NextElementAddress,NumberOFSearchs,SearchForMe)

I'm guess this is very confusing to those aren't all that familiar with how arrays are stored in memory, or more importantly those whom may not have experience with modulo's.
By specifying the address of starting element  and the address of the next element.  It Allows us to use these commands on any array of any dimension.   Effectively what all these commands do, is calculate a Modulo between then StartElementAddress and the NextElement.   This really the distance in memory from one cell to the next.  


In a 1D array.   The data is stored as a que really.. Thus element indexes and the address of elements share a 1 to1 relationship (although the addresses need to be multed by 4, since each cell in memory is 4 bytes).

Now lets look at what you can do...



eg1

 Lets say you wanted to a search a 1D array called Table() starting from the first element, search each element through to the last element..
 So here' how we'd set the input parameters..


 SizeOFArray=GetArrayElements(Table(),1)

 StartElementAddress = 0  

; Since were wish to search every element, we set the NEXT element to STart plus 1

 NextElementAddress = StartElementAddress+1

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,SizeOFArray,FindME)




eg2

 Lets say you wanted to a search a 1D array called Table() starting from the LAST element, search BACK through each element..
 So here' how we'd set the input parameters..



 SizeOFArray=GetArrayElements(Table(),1)

 StartElementAddress = SizeOfArray

; Since were wish to search every element, but backwards, we set the NEXT element to Start minus 1

 NextElementAddress = StartElementAddress-1

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,SizeOFArray,FindME)






eg3

 Ok, Lets say you wanted to a search a 1D array called Table(), but only checking every second element.

 So here' how we'd set the input parameters..


 SizeOFArray=GetArrayElements(Table(),1)

 StartElementAddress = 0  

; Since were wish to only check against every second element, we set the NEXT element to Start plus 2

 NextElementAddress = StartElementAddress+2

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,SizeOFArray,FindME)





This is easy enough to see when working with 1D arrays, but it also works with other dimensions.  Although your bound to get some bizarre results if your not careful here :)..

 So 2D arrays are stored as rows/columns.  Their nothing more than a block of memory like the screen.  To use the arrays commands on these we need to use the built in resolve function to calculate the elements address for us..  Once we have that, it's work as normal..

eg 2D array (10 by 5) in memory.. (each character represents an element)


Y
0)   0,0,0,0,0,0,0,0,0,0,0
1)   0,0,0,0,0,0,0,0,0,0,0
2)   0,0,0,0,0,0,0,0,0,0,0
3)   0,0,0,0,0,0,0,0,0,0,0
4)   0,0,0,0,0,0,0,0,0,0,0
5)   0,0,0,0,0,0,0,0,0,0,0
 X ------->>>>


 in memory these arranged linearly,  so it's just one block of memory.

So the first row of elements are at  addresses 0,1,2,3,4,5,6,7,8,9,10.  Remember that there's really 11 (0 to 10) elements across and 6 rows of elements (0 to 5)

So the second row of elements are at  addresses 11,12,13,14,15,16,17,18,19,20,21

So the third row of elements are at  addresses 22,23,24,25,26,27,28,29,30,31,32
etc

Here's where it gets interesting..  Since the array commands use the distance between elements for how it steps through the array,  were able to search forward/back Rows, forward/back columns, or both at the same time :)

While you could calculate the Address of Each Element yourself (as we were doing in the first examples),  As mentioned previously, PB has a built in feature to specifically to calculate the address of an elements within any array.  This ability comes in the form of the [] operators.  So Placing [] around any array access, Tells PB to calculate the address of the element  rather than  read the contents of this element.  

Thus,

 AddressOfElement5x5y =  [Table(5,5)]
 AddressOfElement7x3y =  [Table(7,3)]
 AddressOfElement1x8y =  [Table(1,8)]


eg 1..

  Lets Search A row of elements in a 2D array Table()..  Search from Left (lowest element to highest) to Right

 So here's how we'd set the input parameters..



 RowWidth=GetArrayElements(Table(),1)

 StartElementAddress = [Table(0,Column)]

 ; Since were wish to search every element on this ROW, we set the NEXT element to Starting at 1

 NextElementAddress = [Table(1,Column)]

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,RowWidth,FindME)






eg 2..

  Lets Search a row of elements in a 2D array Table()..  Search from Right (Highest element to Lowest) to Left

 So here's how we'd set the input parameters..



 RowWidth=GetArrayElements(Table(),1)

 StartElementAddress = [Table(RowWidth,Column)]

 ; Since were wish to search backwards through element on this ROW, we set the NEXT element to Starting at the previous X coord

 NextElementAddress = [Table(RowWidth-1,Column)]

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,RowWidth,FindME)





eg 3..

  Lets Search A Column of elements in a 2D array Table()..  Search from Top (lowest element to highest) to Bottom
 So here's how we'd set the input parameters..



 ColHeight=GetArrayElements(Table(),2)

 StartElementAddress = [Table(Row,0)]

 ; Since were wish to search every element in this COLUMN, we set the NEXT element to Starting drawn the column at 1

 NextElementAddress = [Table(Row,1)]

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,ColHeight,FindME)




eg 4..

  Lets Search A Column of elements in a 2D array Table()..  Search from Bottom (highest element to lowest) to Top

 So here's how we'd set the input parameters..



 ColHeight=GetArrayElements(Table(),2)

 StartElementAddress = [Table(Row,ColHeight)]

 ; Since were wish to search backards through every element in this COLUMN, we set the NEXT element at the previous Y coord, before the starting point.

 NextElementAddress = [Table(Row,ColHeight-1)]

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,ColHeight,FindME)




eg 5..

  Lets Search a diagonal row of elements across and down in a 2D array Table()..  Search from Left (lowest element to highest) to Right, in both Directions.

NOTE:  In this case we'd need a square array, so lets image our array is
 So here's how we'd set the input parameters..



 Searchs=5

 StartElementAddress = [Table(0,0)]

 ; Since were wish to search every element on this ROW, we set the NEXT element to Starting at 1

 NextElementAddress = [Table(1,1)]

 FoundAT=FindArrayCell(Table(),StartElementAddress*4,NextElementAddress*4,Searches,FindME)




Well, there ya go a bit of info on how stuff all works..   One change i'll probably make, is rather than the user having to mult the address of the elements by *4,  I'll think i'll do that inside the functions.  It's safer that way and a bit faster... In case your wondering, yes they already clip to the bounds of the array(s).   So you can't read/write randomly into memory







Fog

QuoteI'm guess this is very confusing to those aren't all that familiar with how arrays are stored in memory, or more importantly those whom may not have experience with modulo's.

Yes  :)   Nice explanation though......very interesting.

I'm just glad you didn't go onto 3D arrays  :D