Multi-dimensional arrays ?

blockypixels

Member
Joined
Sep 12, 2017
it took me ages to convert my big Blitz Max project to Cerberus , most if the time working out how to impliment multi dimension arrays !

This could be part of an import tool ? Or a preprocessor ?

I knew GOTO is no longer cool but multidimension arrays are useful , is it a memory wastage thing ? im not sure.
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
Each multi dimensional array can be implemented as a one dimensional array. Here an example for a two dimensional array:

Code:
Function Main:Int()
   Const WIDTH:Int = 3
   Const HEIGHT:Int = 5
   Local my2DIntArray := New Int[WIDTH*HEIGHT]
   my2DIntArray[0+0*WIDTH] = 23 ' => write 23 at [0,0]
   my2DIntArray[2+4*WIDTH] = 42 ' => write 23 at [2,4]
   
   For Local y:Int = 0 Until 5
       Local s:String = ""
       For Local x:Int = 0 Until 3
           If x <> 0 s += ", "
           s += my2DIntArray[x+y*WIDTH]
       End
       Print s
   End
   
   Return 0
End
However a while ago I have also seen a more friendly way to implement multi dimensional arrays using generic classes:

Code:
Class Array2D<T>
   Function Create:T[][](width:Int, height:Int)
       Local a := New T[width][]
       For Local i:Int = 0 Until width
           a[i] = New T[height]
       End
       Return a
   End
End


Function Main_:Int()
   Const WIDTH:Int = 3
   Const HEIGHT:Int = 5
   Local my2DIntArray := Array2D<Int>.Create(WIDTH, HEIGHT)
   my2DIntArray[0][0] = 23
   my2DIntArray[2][4] = 42
   
   For Local y:Int = 0 Until 5
       Local s:String = ""
       For Local x:Int = 0 Until 3
           If x <> 0 s += ", "
           s += my2DIntArray[x][y]
       End
       Print s
   End
   
   Return 0
End
I don't know who posted this originally (found it on the old Monkey X forum).

I don't think that multi dimensional arrays are a memory issue but it could be complicated to implement this feature for all build targets.
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
These 1D C-style arrays are faster than standard multidimensional arrays (arrays within arrays) and usually just as good in terms of memory use. The standard arrays are only better if you want ragged rather than rectangular arrays.

What I've always wished for are inline functions, then you could write maximally fast Get/Set functions for these pseudo-multidimensional arrays.

The 1D arrays gain you a lot of speed if you are doing things like pathfinding, procedural map generation, chess-like games etc. This is because you can use the index of the underlying 1D array like a C-style pointer.

Using the index as a pointer is a win in two ways:

(1) You can jump around freely using just addition, which is not only faster but often simpler to code:
Code:
Field adjacent_deltas = [ -WIDTH, 1, WIDTH, -1 ]
(2) You don't need an object to hold an index or a difference between two indexes; an integer will do, saving on space and on garbage collection.

This is all target-agnostic, I think with inline functions and methods you could write self-contained generic classes that would run at maximum speed. I haven't bothered with that myself partly because of the lack of inlines, but also because most of the time when I'm using these arrays I'll want their innards exposed anyway, so there's not much gain.
 
Last edited:
Top Bottom