• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] [Snippet] Array2D

Kazeon

Hosted Project: EC
Level 33
Joined
Oct 12, 2011
Messages
3,449
Erm.. Just a nice functionality of vJass. Dunno if this is going to be insta-gy-ed. Comment plz..

Code
JASS:
library_once Array2D // v1.0.0

    /****************************************************************************************
    
        == Array 2D ==
                by: Dalvengyr
                
                
        I. Description
        ¯¯¯¯¯¯¯¯¯¯¯¯¯¯
            Allows you to declare a variable with 2 dimensional
            index with max size of 1023.
                       ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
        
        II. Disadvantages
        ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
            - Can only declare global variable.
            - Has max size of 1023.
            
            
        III. API
        ¯¯¯¯¯¯¯¯
            (Declaring new variable)
            
                //! runtextmacro DECLARE_ARRAY_2D(PREFIX, TYPE, NAME, ROW_SIZE, COLUMN_SIZE)
                        PREFIX  => variable prefix (private, public, etc.)
                        TYPE    => variable type (integer, unit, string, etc.)
                        NAME    => variable name
                    
                    
        IV. Usage
        ¯¯¯¯¯¯¯¯¯
            //! runtextmacro DECLARE_ARRAY_2D("private", "string", "SomeStr", "50", "1023")
            
            You can use it like:
            
                set SomeStr[0][0] = "hello"         => at first index
                set SomeStr[50][1023] = "world"     => at max index
                                    
        
        V. Link
        ¯¯¯¯¯¯¯
            -
            
     ***************************************************************************************/

    //! textmacro DECLARE_ARRAY_2D takes PREFIX, TYPE, NAME, ROW_SIZE, COLUMN_SIZE

        type $NAME$_col extends $TYPE$ array[$COLUMN_SIZE$]
        type $NAME$_row extends $NAME$_col array[$ROW_SIZE$]
    
        globals
            $PREFIX$ $NAME$_row $NAME$
        endglobals

        struct $NAME$_s extends array
            
            private static method onInit takes nothing returns nothing
            
                local integer i = 0
                
                set $NAME$ = $NAME$_row.create()
                loop
                    exitwhen i > $NAME$_row.size
                    set $NAME$[i] = $NAME$_col.create()
                    set i = i + 1
                endloop
                
            endmethod
            
        endstruct

    //! endtextmacro
    
endlibrary

Demo
JASS:
    // Declare the variable
    //! runtextmacro DECLARE_ARRAY_2D("", "unit", "Blabla", "10", "10")
    
    struct Array2DDemo
    
        static method onInit takes nothing returns nothing
            local integer i = 0
            local integer j
            
            loop
                exitwhen i > 10
                set j = 0
                loop
                    exitwhen j > 10
                    set Blabla[i][j] = CreateUnit(Player(0), 'hpea', 0, 0, 0)
                    set j = j + 1
                endloop
                set i = i + 1
            endloop
            
            set i = 0
            loop
                exitwhen i > 10
                set j = 0
                loop
                    exitwhen j > 10
                    call BJDebugMsg(I2S(GetHandleId(Blabla[i][j])) + " Named: " + GetUnitName(Blabla[i][j]))
                    set j = j + 1
                endloop
                set i = i + 1
            endloop
            
        endmethod
    endstruct
 
People said hashtable is slow ^^

still, TableArray wins.

Comparison:

Array2D:
Memory : NONONONONONONONONONOONONON
Performance : Okay.

TableArray
Memory : Chuck Norris Approves
Performance : A slight margin against Array2D.


I once created an AllocTable using just a single array variable, but Mag says it is not enough, even though it is fast.
 
Level 14
Joined
Dec 12, 2012
Messages
1,007
I would say this could be a better approach possible in vJass, since basically this is one of vJass' features. I just made the textmacro version so user may use this feature easily. There is documentation about this feature somewhere in vJass' manual.

I don't think this is flexible enough for a 2D array implementation. You might also take a look at the discussion of this resource, which is very similar.

Furthermore, I don't see what you exactly think you gain with a textmacro version over the inbuilt vJass feature? As specified in the specs, users can already use 2D arrays of any type (except code of course) with larger sizes than 89 (which gets compiled to raw arrays and a few if's, without any hashtables involved).

The technique used in vJass isn't even a linear search but similar to a binary search, and with that, almost as efficient as it can get. The maximum allowed total size is 409550 (considerably higher than stated in the documentation).
 
Top