• 🏆 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!

Dimensional Arrays

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Dimensional Arrays v1.1

This system allows to have multidimensional arrays which Blizzard did not accommodate
to the World Editor. You can read the rest at the readme/documentation because it already says it
all.

System Script:
JASS:
library DimensionalArray initializer Setup/*
     ________________
    |                |
    | Written by AGD |
    |________________|
    This system currently supports up to 7-dimensional index for arrays

    ==========================================================================
    System Information:

        This system simulates n-dimensional arrays which Blizzard failed or
        did not bother to accommodate to WE. This system allows you to index
        an array with n number of indexes. This dimensional index generator
        then converts the indexes to an integer which is then used as the
        actual index of the array.

        The system however has the following limitations:
        * Sizes of the dimensions must be constant and must be initially
          specified
        * Since the system converts the given dimensional indexes to an
          integer which is used as the actual index, the dimension sizes
          is limited by the number of dimensions which means that more
          dimensions the index occupy, the smaller the limit of the
          dimension sizes becomes in order for the array size to be within
          the size limit
        * Other limitations not noted

        But there are also possible benefits of this system namely, you can
        set the value of an array variable using an A number of dimensions
        while retrieving its value using B number of dimensions later. You
        can use this ability to perform some tricks that might benefit you.

    ==========================================================================
    API:

        function Dim2 takes integer x, integer y returns integer
        - Allows you to index an array using two dimensions

        function Dim3 takes integer x, integer y, integer z returns integer
        - Allows you to index an array using three dimensions

        function Dim4 takes integer x, integer y, integer z, integer a returns integer
        - Allows you to index an array using four dimensions

        function Dim5 takes integer x, integer y, integer z, integer a, integer b returns integer
        - Allows you to index an array using five dimensions

        function Dim6 takes integer x, integer y, integer z, integer a, integer b, integer c returns integer
        - Allows you to index an array using six dimensions

        function Dim7 takes integer x, integer y, integer z, integer a, integer b, integer c, integer d returns integer
        - Allows you to index an array using seven dimensions

    ==========================================================================
    Sample Usage:

        * Storing value to an array
          set My_UnitArray[Dim2(2, 1)] = GetTriggerUnit()
          set My_PlayerArray[Dim3(5, 2, TempInt)] = GetTriggerPlayer()

        * Retrieving value from an array
          set tempunit = My_UnitArray[Dim2(3, 5)]
          set tempint = My_Int[Dim3(1, 1, 6)]

    ==========================================================================
*/

    globals

        ///////////////////
        // Configuration //
        ///////////////////

        ///////////////////////////////////////////////
        //|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|//
        //| Array sizes:                            |//
        //|[You probably want to go beyond the 8190 |//
        //|instances limit, well you can but the top|//
        //|limit is 409550 which is the largest the |//
        //|preprossesor can allow. If you set it to |//
        //|a value higher than that, it will auto-  |//
        //|matically adjusted to 409550 to make sure|//
        //|that it is actually usable for arrays.   |//
        //|Another thing to remember is that if you |//
        //|go beyond 8190, you have to specify the  |//
        //|array size of the variable when you      |//
        //|declare it to let JassHelper know that   |//
        //|you need extra memory allocation.]       |//
        //|_________________________________________|//
        ///////////////////////////////////////////////
        private constant integer DEFAULT_2D_ARRAY_SIZE = 8190
        private constant integer DEFAULT_3D_ARRAY_SIZE = 8190
        private constant integer DEFAULT_4D_ARRAY_SIZE = 409550
        private constant integer DEFAULT_5D_ARRAY_SIZE = 409550
        private constant integer DEFAULT_6D_ARRAY_SIZE = 409550
        private constant integer DEFAULT_7D_ARRAY_SIZE = 409550

        ///////////////////////////////////////////////
        //|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|//
        //| Arrays' dimensional ratio:              |//
        //| Each field's value should be greater    |//
        //| than zero. By default, all dimensions   |//
        //| have the same ratio (1:1:...:1) which   |//
        //| means that all of the dimensions have   |//
        //| equal sizes.                            |//
        //|_________________________________________|//
        ///////////////////////////////////////////////

        ///////////////////////////////////////////////
        // 2D array dimensional ratio                //
        ///////////////////////////////////////////////
        private constant integer DEFAULT_X_SIZE_2D = 1
        private constant integer DEFAULT_Y_SIZE_2D = 1

        ///////////////////////////////////////////////
        // 3D array dimensional ratio                //
        ///////////////////////////////////////////////
        private constant integer DEFAULT_X_SIZE_3D = 1
        private constant integer DEFAULT_Y_SIZE_3D = 1
        private constant integer DEFAULT_Z_SIZE_3D = 1

        ///////////////////////////////////////////////
        // 4D array dimensional ratio                //
        ///////////////////////////////////////////////
        private constant integer DEFAULT_X_SIZE_4D = 1
        private constant integer DEFAULT_Y_SIZE_4D = 1
        private constant integer DEFAULT_Z_SIZE_4D = 1
        private constant integer DEFAULT_A_SIZE_4D = 1

        ///////////////////////////////////////////////
        // 5D array dimensional ratio                //
        ///////////////////////////////////////////////
        private constant integer DEFAULT_X_SIZE_5D = 1
        private constant integer DEFAULT_Y_SIZE_5D = 1
        private constant integer DEFAULT_Z_SIZE_5D = 1
        private constant integer DEFAULT_A_SIZE_5D = 1
        private constant integer DEFAULT_B_SIZE_5D = 1

        ///////////////////////////////////////////////
        // 6D array dimensional ratio                //
        ///////////////////////////////////////////////
        private constant integer DEFAULT_X_SIZE_6D = 1
        private constant integer DEFAULT_Y_SIZE_6D = 1
        private constant integer DEFAULT_Z_SIZE_6D = 1
        private constant integer DEFAULT_A_SIZE_6D = 1
        private constant integer DEFAULT_B_SIZE_6D = 1
        private constant integer DEFAULT_C_SIZE_6D = 1

        ///////////////////////////////////////////////
        // 7D array dimensional ratio                //
        ///////////////////////////////////////////////
        private constant integer DEFAULT_X_SIZE_7D = 1
        private constant integer DEFAULT_Y_SIZE_7D = 1
        private constant integer DEFAULT_Z_SIZE_7D = 1
        private constant integer DEFAULT_A_SIZE_7D = 1
        private constant integer DEFAULT_B_SIZE_7D = 1
        private constant integer DEFAULT_C_SIZE_7D = 1
        private constant integer DEFAULT_D_SIZE_7D = 1

        private constant string DEBUG_PREFIX = "[ERROR: ]"
        private constant real DEBUG_DURATION = 60.00

        //////////////////////////
        // End of Configuration //
        //////////////////////////

    endglobals

    /***** System code below this line *****/

    globals

        private integer ArraySize2D
        private integer ArraySize3D
        private integer ArraySize4D
        private integer ArraySize5D
        private integer ArraySize6D
        private integer ArraySize7D
        private integer XSize2D
        private integer YSize2D
        private integer XSize3D
        private integer YSize3D
        private integer ZSize3D
        private integer XSize4D
        private integer YSize4D
        private integer ZSize4D
        private integer ASize4D
        private integer XSize5D
        private integer YSize5D
        private integer ZSize5D
        private integer ASize5D
        private integer BSize5D
        private integer XSize6D
        private integer YSize6D
        private integer ZSize6D
        private integer ASize6D
        private integer BSize6D
        private integer CSize6D
        private integer XSize7D
        private integer YSize7D
        private integer ZSize7D
        private integer ASize7D
        private integer BSize7D
        private integer CSize7D
        private integer DSize7D

    endglobals

    //==========================================================================
    ////////////////
    // TextMacros //
    ////////////////

    //! textmacro DEBUG takes d, D, n
    if $d$ > $D$Size$n$D then
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION, DEBUG_PREFIX + "Specified $D$-Location of array exceeds the $D$ size limit")
    endif
    if $d$ == 0 then
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION, DEBUG_PREFIX + "Specified $D$-Location is zero")
    endif
    //! endtextmacro

    //! textmacro LIMITSIZE takes D
        if DEFAULT_$D$D_ARRAY_SIZE > 409550 then
            set ArraySize$D$D = 409550
        else
            set ArraySize$D$D = DEFAULT_$D$D_ARRAY_SIZE
        endif
    //! endtextmacro

    //==========================================================================

    function Dim2 takes integer x, integer y returns integer
        if x > XSize2D or y > YSize2D or x == 0 or y == 0 then
            static if DEBUG_MODE then
                //! runtextmacro DEBUG("x", "X", "2")
                //! runtextmacro DEBUG("y", "Y", "2")
            endif
            return 0
        endif
        return (x - 1)*YSize2D + y
    endfunction

    function Dim3 takes integer x, integer y, integer z returns integer
        if x > XSize3D or y > YSize3D or z > ZSize3D or x == 0 or y == 0 or z == 0 then
            static if DEBUG_MODE then
                //! runtextmacro DEBUG("x", "X", "3")
                //! runtextmacro DEBUG("y", "Y", "3")
                //! runtextmacro DEBUG("z", "Z", "3")
            endif
            return 0
        endif
        return (x - 1)*(ZSize3D*YSize3D) + (y - 1 )*ZSize3D + z
    endfunction

    function Dim4 takes integer x, integer y, integer z, integer a returns integer
        if x > XSize4D or y > YSize4D or z > ZSize4D or a > ASize4D or x == 0 or y == 0 or z == 0 or a == 0 then
            static if DEBUG_MODE then
                //! runtextmacro DEBUG("x", "X", "4")
                //! runtextmacro DEBUG("y", "Y", "4")
                //! runtextmacro DEBUG("z", "Z", "4")
                //! runtextmacro DEBUG("a", "A", "4")
            endif
            return 0
        endif
        return (x - 1)*(ASize4D*ZSize4D*YSize4D) + (y - 1)*(ASize4D*ZSize4D) + (z - 1)*ASize4D + a
    endfunction

    function Dim5 takes integer x, integer y, integer z, integer a, integer b returns integer
        if x > XSize5D or y > YSize5D or z > ZSize5D or a > ASize5D or b > BSize5D or x == 0 or y == 0 or z == 0 or a == 0 or b == 0 then
            static if DEBUG_MODE then
                //! runtextmacro DEBUG("x", "X", "5")
                //! runtextmacro DEBUG("y", "Y", "5")
                //! runtextmacro DEBUG("z", "Z", "5")
                //! runtextmacro DEBUG("a", "A", "5")
                //! runtextmacro DEBUG("b", "B", "5")
            endif
            return 0
        endif
        return (x - 1)*(BSize5D*ASize5D*ZSize5D*YSize5D) + (y - 1)*(BSize5D*ASize5D*ZSize5D) + (z - 1)*(BSize5D*ASize5D) + (a - 1)*BSize5D + b
    endfunction

    function Dim6 takes integer x, integer y, integer z, integer a, integer b, integer c returns integer
        if x > XSize6D or y > YSize6D or z > ZSize6D or a > ASize6D or b > BSize6D or c > CSize6D or x == 0 or y == 0 or z == 0 or a == 0 or b == 0 or c == 0 then
            static if DEBUG_MODE then
                //! runtextmacro DEBUG("x", "X", "6")
                //! runtextmacro DEBUG("y", "Y", "6")
                //! runtextmacro DEBUG("z", "Z", "6")
                //! runtextmacro DEBUG("a", "A", "6")
                //! runtextmacro DEBUG("b", "B", "6")
                //! runtextmacro DEBUG("c", "C", "6")
            endif
            return 0
        endif
        return (x - 1)*(CSize6D*BSize6D*ASize6D*ZSize6D*YSize6D) + (y - 1)*(CSize6D*BSize6D*ASize6D*ZSize6D) + (z - 1)*(CSize6D*BSize6D*ASize6D) + (a - 1)*(CSize6D*BSize6D) + (b - 1)*CSize6D + c
    endfunction

    function Dim7 takes integer x, integer y, integer z, integer a, integer b, integer c, integer d returns integer
        if x > XSize7D or y > YSize7D or z > ZSize7D or a > ASize7D or b > BSize7D or c > CSize7D or d > DSize7D or x == 0 or y == 0 or z == 0 or a == 0 or b == 0 or c == 0 or d == 0 then
            static if DEBUG_MODE then
                //! runtextmacro DEBUG("x", "X", "7")
                //! runtextmacro DEBUG("y", "Y", "7")
                //! runtextmacro DEBUG("z", "Z", "7")
                //! runtextmacro DEBUG("a", "A", "7")
                //! runtextmacro DEBUG("b", "B", "7")
                //! runtextmacro DEBUG("c", "C", "7")
                //! runtextmacro DEBUG("d", "D", "7")
            endif
            return 0
        endif
        return (x - 1)*(DSize7D*CSize7D*BSize7D*ASize7D*ZSize7D*YSize7D) + (y - 1)*(DSize7D*CSize7D*BSize7D*ASize7D*ZSize7D) + (z - 1)*(DSize7D*CSize7D*BSize7D*ASize7D) + (a - 1)*(DSize7D*CSize7D*BSize7D) + (b - 1)*(DSize7D*CSize7D) + (c - 1)*DSize7D + d
    endfunction

    private function Setup takes nothing returns nothing

        local integer Sum

        ///////////////////////////////////////////////////////////////////////
        // Making sure that the specified array sizes does not exceed the    //
        // maximum value that JassHelper can allocate                        //
        ///////////////////////////////////////////////////////////////////////
        //! runtextmacro LIMITSIZE("2")
        //! runtextmacro LIMITSIZE("3")
        //! runtextmacro LIMITSIZE("4")
        //! runtextmacro LIMITSIZE("5")
        //! runtextmacro LIMITSIZE("6")
        //! runtextmacro LIMITSIZE("7")

        ////////////////////////////////////////////////////////////////////////
        // Setting up array dimensions size according to their ratio          //
        ////////////////////////////////////////////////////////////////////////
        set Sum = DEFAULT_X_SIZE_2D + DEFAULT_Y_SIZE_2D
        set XSize2D = R2I(Pow(ArraySize2D, 0.5)*(2.*DEFAULT_X_SIZE_2D/Sum))
        set YSize2D = R2I(Pow(ArraySize2D, 0.5)*(2.*DEFAULT_Y_SIZE_2D/Sum))

        set Sum = DEFAULT_X_SIZE_3D + DEFAULT_Y_SIZE_3D + DEFAULT_Z_SIZE_3D
        set XSize3D = R2I(Pow(ArraySize3D, 1./3)*(3.*DEFAULT_X_SIZE_3D/Sum))
        set YSize3D = R2I(Pow(ArraySize3D, 1./3)*(3.*DEFAULT_Y_SIZE_3D/Sum))
        set ZSize3D = R2I(Pow(ArraySize3D, 1./3)*(3.*DEFAULT_Z_SIZE_3D/Sum))

        set Sum = DEFAULT_X_SIZE_4D + DEFAULT_Y_SIZE_4D + DEFAULT_Z_SIZE_4D + DEFAULT_A_SIZE_4D
        set XSize4D = R2I(Pow(ArraySize4D, 0.25)*(4.*DEFAULT_X_SIZE_4D/Sum))
        set YSize4D = R2I(Pow(ArraySize4D, 0.25)*(4.*DEFAULT_Y_SIZE_4D/Sum))
        set ZSize4D = R2I(Pow(ArraySize4D, 0.25)*(4.*DEFAULT_Z_SIZE_4D/Sum))
        set ASize4D = R2I(Pow(ArraySize4D, 0.25)*(4.*DEFAULT_A_SIZE_4D/Sum))

        set Sum = DEFAULT_X_SIZE_5D + DEFAULT_Y_SIZE_5D + DEFAULT_Z_SIZE_5D + DEFAULT_A_SIZE_5D + DEFAULT_B_SIZE_5D
        set XSize5D = R2I(Pow(ArraySize5D, 0.2)*(5.*DEFAULT_X_SIZE_5D/Sum))
        set YSize5D = R2I(Pow(ArraySize5D, 0.2)*(5.*DEFAULT_Y_SIZE_5D/Sum))
        set ZSize5D = R2I(Pow(ArraySize5D, 0.2)*(5.*DEFAULT_Z_SIZE_5D/Sum))
        set ASize5D = R2I(Pow(ArraySize5D, 0.2)*(5.*DEFAULT_A_SIZE_5D/Sum))
        set BSize5D = R2I(Pow(ArraySize5D, 0.2)*(5.*DEFAULT_B_SIZE_5D/Sum))

        set Sum = DEFAULT_X_SIZE_6D + DEFAULT_Y_SIZE_6D + DEFAULT_Z_SIZE_6D + DEFAULT_A_SIZE_6D + DEFAULT_B_SIZE_6D + DEFAULT_C_SIZE_6D
        set XSize6D = R2I(Pow(ArraySize6D, 1./6)*(6.*DEFAULT_X_SIZE_6D/Sum))
        set YSize6D = R2I(Pow(ArraySize6D, 1./6)*(6.*DEFAULT_Y_SIZE_6D/Sum))
        set ZSize6D = R2I(Pow(ArraySize6D, 1./6)*(6.*DEFAULT_Z_SIZE_6D/Sum))
        set ASize6D = R2I(Pow(ArraySize6D, 1./6)*(6.*DEFAULT_A_SIZE_6D/Sum))
        set BSize6D = R2I(Pow(ArraySize6D, 1./6)*(6.*DEFAULT_B_SIZE_6D/Sum))
        set CSize6D = R2I(Pow(ArraySize6D, 1./6)*(6.*DEFAULT_C_SIZE_6D/Sum))

        set Sum = DEFAULT_X_SIZE_7D + DEFAULT_Y_SIZE_7D + DEFAULT_Z_SIZE_7D + DEFAULT_A_SIZE_7D + DEFAULT_B_SIZE_7D + DEFAULT_C_SIZE_7D + DEFAULT_D_SIZE_7D
        set XSize7D = R2I(Pow(ArraySize7D, 1./7)*(7.*DEFAULT_X_SIZE_7D/Sum))
        set YSize7D = R2I(Pow(ArraySize7D, 1./7)*(7.*DEFAULT_Y_SIZE_7D/Sum))
        set ZSize7D = R2I(Pow(ArraySize7D, 1./7)*(7.*DEFAULT_Z_SIZE_7D/Sum))
        set ASize7D = R2I(Pow(ArraySize7D, 1./7)*(7.*DEFAULT_A_SIZE_7D/Sum))
        set BSize7D = R2I(Pow(ArraySize7D, 1./7)*(7.*DEFAULT_B_SIZE_7D/Sum))
        set CSize7D = R2I(Pow(ArraySize7D, 1./7)*(7.*DEFAULT_C_SIZE_7D/Sum))
        set DSize7D = R2I(Pow(ArraySize7D, 1./7)*(7.*DEFAULT_D_SIZE_7D/Sum))

        static if DEBUG_MODE then
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, "ARRAY SIZES LIMITS")
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION, "2D Arrays: [" + I2S(XSize2D) + ", " + I2S(YSize2D) + "] = " + I2S(Dim2(XSize2D, YSize2D)))
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION,  "3D Arrays: [" + I2S(XSize3D) + ", " + I2S(YSize3D) + ", " + I2S(ZSize3D) + "] = " + I2S(Dim3(XSize3D, YSize3D, ZSize3D)))
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION, "4D Arrays: [" + I2S(XSize4D) + ", " + I2S(YSize4D) + ", " + I2S(ZSize4D) + ", " + I2S(ASize4D) + "] = " + I2S(Dim4(XSize4D, YSize4D, ZSize4D, ASize4D)))
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION, "5D Arrays: [" + I2S(XSize5D) + ", " + I2S(YSize5D) + ", " + I2S(ZSize5D) + ", " + I2S(ASize5D) + ", " + I2S(BSize5D) + "] = " + I2S(Dim5(XSize5D, YSize5D, ZSize5D, ASize5D, BSize5D)))
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION, "6D Arrays: [" + I2S(XSize6D) + ", " + I2S(YSize6D) + ", " + I2S(ZSize6D) + ", " + I2S(ASize6D) + ", " + I2S(BSize6D) + ", " + I2S(CSize6D) + "] = " + I2S(Dim6(XSize6D, YSize6D, ZSize6D, ASize6D, BSize6D, CSize6D)))
            call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, DEBUG_DURATION, "7D Arrays: [" + I2S(XSize7D) + ", " + I2S(YSize7D) + ", " + I2S(ZSize7D) + ", " + I2S(ASize7D) + ", " + I2S(BSize7D) + ", " + I2S(CSize7D) + ", " + I2S(DSize7D) + "] = " + I2S(Dim7(XSize7D, YSize7D, ZSize7D, ASize7D, BSize7D, CSize7D, DSize7D)))
        endif

    endfunction


endlibrary


v1.0
- First Release
v1.1
- Now supports 7 dimensional indexes
- Fixed some errors in the array dimension sizes setup
- Now produces consecutive indexes
- Now you only have to specify the ratio of the dimensions of an array and their sizes
will automatically initialized

 
Last edited:
Level 13
Joined
Nov 7, 2014
Messages
571
This is a minor thing but it seems that your Dim2|3|4|5 functions don't produce consecutive indices:

JASS:
function use_Dim2 takes nothing returns nothing
    local integer x
    local integer y
    local integer index

    // for simplicity
    // [4][3]
    set XSize2D = 4
    set YSize2D = 3

    set x = 1
    loop
        exitwhen x > 4

        set y = 1
        loop
            exitwhen y > 3

            set index = Dim2(x, y)
            call BJDebugMsg(I2S(index))

            set y = y + 1
        endloop

        set x = x + 1
    endloop
endfunction

// got:
// 1
// 5
// 9
// 2
// 6
// 10
// 3
// 7
// 11
// 4
// 8
// 12

// expected:
// 1
// 2
// 3
// ...
// 11
// 12

You can get consecutive indices with something like this:

JASS:
function dim2_consecutive takes integer x, integer y returns integer
    return (x - 1)*YSize2D + y
endfunction

Also, I think you can compute the indices for Dim3|4|5 using less multiplications and/or additions but I'll leave that
for you to figure out =).

PS: +1 for using/supporting 1 based arrays =)!

This < Table

Table is glorified hashtable wrapper, this maps indices of multidimentional arrays to the index of a 1 dimentional arrays, so you are comparing apples to oranges?
 
Level 13
Joined
Nov 7, 2014
Messages
571
Lets use upper case letters for array dimension sizes: I, J, K, L, M, N, O, P, Q, R ...
and lower case letters for indices: i, j, k, l, m, n, o, p, q, r, ...

JASS:
1d array: a[I]
bracket indexing: a[i]
computed indexing: a[i]

2d array: a[I][J]
bracket indexing: a[i][j]
computed indexing: a[ i * J + j ]

3d array: a[I][J][K]
bracket indexing: a[i][j][k]
computed indexing: a[ (i * J + j) * K + k ]

4d array: a[I][J][K][L]
bracket indexing: a[i][j][k][l]
computed indexing: a[ ((i * J + j) * K + k) * L + l ]

5d array: a[I][J][K][L][M]
bracket indexing: a[i][j][k][l][m]
computed indexing: a[ (((i * J + j) * K + k) * L + l) * M + m ]

As you can see the pattern is simple, i.e wrap the previous expression in brackets and append the last dimension size as a factor and the dimension index as a term, so for 6d we would get

JASS:
6d array: a[I][J][K][L][M][N]
bracket indexing: a[i][j][k][l][m][n]
computed indexing: a[ ((((i * J + j) * K + k) * L + l) * M + m) * N + n ]

So it seems the number of multiplications (*) and additions (+) is equal to the number of dimensions - 1.

With your current scheme the number of multiplications (*) for N dimensions = N * (N - 1) / 2 (the number of additions (+) is still equal to the number of dimensions - 1).

Lets compare the two schemes (Dn is the number of dimensions):

D1 - 0 - 0
D2 - 1 - 1
D3 - 2 - 3
D4 - 3 - 6
D5 - 4 - 10
D6 - 5 - 15
D7 - 6 - 21
etc.

So yeah, a bit less multiplications (*) =).

PS: the bracket indexing is what languages with built-in multidimensional arrays usually use
 
Top