Request: Slicing array with more than 1 dimension

BlitzMax Forums/BlitzMax Programming/Request: Slicing array with more than 1 dimension

Fabian.(Posted 2006) [#1]
Hi,

I'd request a BlitzMax built-in feature to slice arrays with more than one dimension; it could work like this:
Local Arr [ 5 , 5 ] 'Create a 5x5 array
Arr = Arr [ .. 10 , .. 10 ] 'Extend it to a 10x10 array
Arr = Arr [ 4 .. 6 , 4 .. 6 ] 'Extract the middle four elements in a 2x2 array
Arr = Arr [ -8 .. , -3 .. ] 'Append empty elements to the beginning of the array so that it's 10x5
Arr = Arr [ .. 5 , .. 10 ] 'Skip 25 elements in x-axis(first dimension) and append 25 empty elements in y-axis(second dimension)
This isn't possible yet. I suggest to add this feature; it'll be usefull, because till now you've to create a new array with same dimensions and then copy the elements if you want to do this. I wrote some functions to perform array slices with arrays with up to 5 dimensions:

Could you implement this as BlitzMax built-in feature? That would be really nice.


Dreamora(Posted 2006) [#2]
It is possible.

If you want to slice with more than 1 dimension, you can use Array of Arrays.

Your idea, while nice, won't work, because it needs to be programmed for any amount of dimensions not only a fixed amount.


Fabian.(Posted 2006) [#3]
Yes, I know that just writing more of these functions won't solve the problem; so I wrote a new one, which works with each number of dimensions. I also added an array concat function, but for now I'm mainly requesting the slice function to be implemented:
ArraySliceEx.c

ArrayConcat.c:

CallFunction.s:




Fabian.(Posted 2006) [#4]
Here's an example:
Example.bmx:

Such a feature would be really nice, for example when writing a high score list as array, where you sometimes need to insert a high score entry between two others - just part the array using slices and then put it all together using concat.
There're lots of situations, where this feature would be really useful.


Fabian.(Posted 2006) [#5]
I wrote another example showing where this feature would be very useful. The example is creating a customized command prompt, storing all entered characters in a two dimensional array. Things like scrolling up the display can easily be done by slicing and concating the array. If I want to do something like this, till now, I have to write a function myself which is doing what I want to do. So I request to implement this to BlitzMax, I think it would be useful for many blitz users.
Command.bmx:

You can see that there're many extern function calls to slice or concat the array. A BlitzMax built-in feature would make this much more readable and easier for the programmer to write it.


Dreamora(Posted 2006) [#6]
Simply using the already present technology instead of insisting on reinventing the wheel would help as well and you wouldn't need a single line extern.


int[][] is sliceable and has scalable sub array sizes instead of a fixed size for each "row" as additional powerfull feature.


Fabian.(Posted 2006) [#7]
Of course there're many ways to work around this. So creating an array of arrays could also solve the problem, I know.
But the actual reason why I request this is that there're some situations where this would be the problems best solution, because you have to represent a two (or any other) dimensional thing in the memory; for example the pixels in a screen or the characters in a command prompt (as the example shows).
So I think it would be the best to have something like a two dimensional array.... err what? This already exists! So I don't have to invent the wheel - multi dimensional arrays are already implemented to BlitzMax. The only problem is that we currently don't have the useful functions to effectively work with them.
I simply request a possibility for better using the features already supported.

Arrays of arrays:
Sure they're sometimes useful, but not for properly solving the problem I mentioned; just because in those cases it's even a risk to have different scales along one of the dimensions (pixels of a screen). Of course it would be possible to go through all the source and make sure that these sub arrays always have the same size, but even the best programmer sometimes makes mistakes, so it's a bit a task of the programming language to notice the programmer if something is done totally wrong.
I know there're many cases where int[][] is needed and useful, but those are not these I'm talking about, so in this case int[,] would be better.
Another disadvantage of that way to solve the problem is that arrays of arrays have a big overhead, you'd always have an array just to store references to the others and each of those has bit header storing information about the array class, reference count, element type, total size, number of dimensions (which is always 1) and so on... wasted memory.

I know, int[][] works, but that's a normal behaviour of a workaround - the much better solution is a two dimensional array.


Dreamora(Posted 2006) [#8]
No, using Array of Array is no workaround.

You are using it, if you either need a dynamic second dimension (unlike multi dim arrays which have fixed sizes for the second dimension) or if you need slicing support.

Thats no workaround but the simple way it is. If you want to have it the "cheap way" (although it is not really cheaper to have a multi dim array, why? A multidim is a single dim with "mod /" support internally. Instead of int[2,5] you could write int[10] and i = x + y * 5 when accessing it), then thats the only way to go with multidim.


Fabian.(Posted 2006) [#9]
You are using it, if you either need a dynamic second dimension (unlike multi dim arrays which have fixed sizes for the second dimension) or if you need slicing support.
That's a recursion. In BlitzMax till now array slicing is only allowed with 1D-arrays. That's why we have to use them. So saying "use them, because you need to use them" isn't what I was asking for. In fact I'm currently using arrays of arrays, but this isn't the proper solution for a problem where a fixed second dimension is needed. Yes, sometimes a dynamic second dimension isn't an advantage, because in these cases (as posted above: pixels in screen, characters in command prompt...) you need to have a fixed second dimension.
So in such situations a 2D array would be the best solution. And as I already said multi dimensional arrays are available in BlitzMax, so why not use them if you need them?
It's just that we would need some features for better handling them -> array slicing/concating.

Of course there's always a way to do something without this feature, but I didn't said "I'm having a ploblem/how to do this?" - of course there's a way to solve my problem till now, and I know this way, but I just think that the language gets more self contained with this feature.


Dreamora(Posted 2006) [#10]
that might be, it would add another feature to it.

Main problem is that it won't work with multi dim as users are used to it with regular arrays.
For that reason it might be added or might be not, we will see if your code is part of a future syncmod or BM 1.24 :)
It surely wouldn't hurt.

But until then, there is at least the possibility to use an array of array which has no further disadvantage over multi dim array than having multiple [] instead of only one :) (oh and naturaly the 20min play around until one understands how initialisation and slicing works)