• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Multi-dimensional arrays ?

blockypixels

New member
Joined
Sep 12, 2017
Messages
31
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.
 
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.
 
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:
Back
Top Bottom