Baffled By Arrays

BlitzMax Forums/BlitzMax Programming/Baffled By Arrays

Clyde(Posted 2005) [#1]
What I'd like to do is store information for 4 things with 4 sets of values for each one. I've only got to doing 2, as Im baffled by the new ways of Dims.

If someone could help, that would be fantastic.
Thanks.






Perturbatio(Posted 2005) [#2]



Clyde(Posted 2005) [#3]
Ok thanks perturbatio.

So I see there's no need for the number in the brackers, to signify the maximum values as previous with a dim.

Top one!


Clyde(Posted 2005) [#4]
Still Baffling me.

I know how to store it from the example above. But how now do I use a certain portion of this data. Say for example, I want to use a colour based on an variable to indicate which one to use?




Perturbatio(Posted 2005) [#5]
It should be noted that the code I posted is an array of float arrays NOT a multi-dimensional array.

But you can address an individual value in a similar way to a normal array:


Bear in mind that Arrays are zero-based in Blitzmax


Clyde(Posted 2005) [#6]
Thanks Mate!

I thought I almost had the hang of them for a split second. Just tried to make some stars. But still not getting it.




Perturbatio(Posted 2005) [#7]
There are of course much better ways to do this but:




Clyde(Posted 2005) [#8]
Greatest!

Is the slice of 2 making all of the 'array of arrays' with just 2 Dimensions (for the info recording of x and y)?

Once again thank you kindly!
.:|THUMBZ|:.


Russell(Posted 2005) [#9]
Global Stars:Int[][]	'an array of arrays

Stars = Stars[..MaxStars] 'resize the main array using a slice

Ok, what makes the first line an array of arrays rather than a two dimensional array? Is the syntax different for a two dimensional array?
Then on the second line, shouldn't there be another bracket set after the [..MaxStars] since two sets were used in the declaration? Like this:
Global Stars:Int[][]	'an array of arrays

Stars = Stars[..MaxStars][] 'resize the main array using a slice

Perhaps I should really read up on the whole slice thing! Sounds a bit like ReDim in many BASICs, but with more flexibility.

Thanks in advance,
Russell

p.s. Seems like this would make a great tutorial subject (arrays)...

Russell


Perturbatio(Posted 2005) [#10]
Is the slice of 2 making all of the 'array of arrays' with just 2 Dimensions (for the info recording of x and y)?


Yes. (well two elements rather than dimensions, it is still a 1D array) It loops through each of the elements of the main array and resizes the array that those elements contain.

Ok, what makes the first line an array of arrays rather than a two dimensional array? Is the syntax different for a two dimensional array?

The two seperate square braces [][] define it as an array of arrays.

To define a two dimensional array you would do:
Local myArray:Int[,]

The comma denotes a new dimension in the current array

Then on the second line, shouldn't there be another bracket set after the [..MaxStars] since two sets were used in the declaration?

No, because the slice re-dimensions the main array, not the arrays it contains.

It *might* help to think of the main array as a list of Types where each type is another array.

Just to complicate things, you could also do:
Local myArray:Int[][,] 


Which would create an array of 2D arrays

I recently added a page to the BlitzWiki which covers arrays to a certain degree (part of it is the original BMax array docs).

http://www.blitzwiki.org/index.php/Arrays

Perhaps I should really read up on the whole slice thing! Sounds a bit like ReDim in many BASICs, but with more flexibility.


Slices are great! (missing one particular bit of functionality I would like, but hey you can't have everything at once).

It should be pointed out that you cannot use slices with Multi-Dimensional Arrays.


Russell(Posted 2005) [#11]
Ah! Ok, NOOOOOOW it makes sense. I guess ;) Seriously, though, it does. But, what advantage does an array of arrays have over a two dimensional array? They seem to work in a similar way. That is, in this:
Global Arr:Int[2,2]

We have two elements per index. That is, we have:
Arr[0,0] = 1  ; index one's first element
Arr[0,1] = 2  ; index one's second element
Arr[1,0] = 3  ; index two's first element
Arr[1,1] = 4  ; index two's second element

Seems to work the same as:
Arr[0][0] = 1
Arr[0][1] = 2
Arr[1][0] = 3
Arr[1][1] = 4

In this instance, are they the same? I guess the difference is that the 'secondary' array COULD be multidimensional if necessary. Am I close?

Thanks,
Russell


FlameDuck(Posted 2005) [#12]
But, what advantage does an array of arrays have over a two dimensional array?
It depends on your data structure. The main difference is that a x*y 2D array will always have z positions, while an array of arrays allows you to only have the positions that you really need and thus potentially takes up less space.

There might also be a slight speed benefit, depending on how arrays are addressed in BlitzMAX. If 2D arrays have to be in contigous memory, there's a very real chance they won't fit comfortably in the cache.

Only Mark can say for sure.


PowerPC603(Posted 2005) [#13]

But, what advantage does an array of arrays have over a two dimensional array?


For an array of arrays, each index of the main array can have a different amount of subindexes, which can lead to less memory-usage.

If you were to store the handles of 3D models for chess pieces and you would use a 2D array, then you have this setup:

In the first line (first row), all fields (or indexes) are used, because there are 8 pawns in a chess game.
But for all other pieces, most indexes aren't used (the empty fields), but they do exist in memory (the memory for them is allocated).

If you were to use an array of arrays, the first array holds the pointer to all subarrays, which in turn can be sized to any size.
So, at mainarray index 0 (first row), you could create a subarray with 8 indexes.
Type TChessPiece
	Field ModelHandle
End Type

Local ChessPieces:TChessPiece[][]

' There are 6 different kinds of pieces, so set the first array (mainarray) to have 6 indexes (0..5)
ChessPieces = ChessPieces[..6]

' There are 8 pawns, so create a subarray of size 8 at mainarray index 0
ChessPieces[0] = ChessPieces[0][..8]

' There are 2 Rooks, so create a subarray of size 2 at mainarray index 1
ChessPieces[1] = ChessPieces[1][..2]

' There are 2 Knights, so create a subarray of size 2 at mainarray index 2
ChessPieces[2] = ChessPieces[2][..2]

' There are 2 Bishops, so create a subarray of size 2 at mainarray index 3
ChessPieces[3] = ChessPieces[3][..2]

' There is only 1 King, so create a subarray of size 1 at mainarray index 4
ChessPieces[4] = ChessPieces[4][..1]

' There is only 1 Queen, so create a subarray of size 1 at mainarray index 5
ChessPieces[5] = ChessPieces[5][..1]

After this, you have the exact size of the arrays, to hold just the handles which are really used, so there will be no unused arrayindexes.
Now you have the setup as in the picture, with only the marked indexes in memory, the blank fields don't exist in memory and cannot be addressed (you'll get an error if you do).

Now this is a small example, where it is not an issue for a small number of unused indexes, but it can matter in a much bigger project.


Russell(Posted 2005) [#14]
I see. Thanks!

Russell