HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

2D array in struct?

01-20-2010, 02:29 AM#1
Ammorth
Collapse JASS:
library test
    struct dummy
        integer array 2d [1][1]
    endstruct
endlibrary

line XXX: Unexpected: "[1]"

Any way to cheat the system for a 2d array inside a struct, without using a method/math inside the []?
01-20-2010, 03:21 AM#2
Fledermaus
Table array?
01-20-2010, 04:05 AM#3
Deaod
manually manage the 2D array.
01-20-2010, 05:39 AM#4
DioD
library test
struct dummy[8190
integer array x2d[64]
endstruct
endlibrary
01-20-2010, 05:46 AM#5
Ammorth
Quote:
Originally Posted by Deaod
manually manage the 2D array.

I was afraid of that...

Quote:
Originally Posted by DioD
Collapse JASS:
library test
    struct dummy[8190]
        integer array x2d[64]
    endstruct
endlibrary

hmm, that is interesting..., but still requires me to do some rough work. I think the easiest way is to use a regular array in the struct--with either a constant method (possible?) or in-line my math commands--and just live with it. Thanks guys.
01-20-2010, 06:15 AM#6
DioD
check pools script it uses 256 size sub arrays.
01-20-2010, 09:04 AM#7
Anitarf
We used to simulate 2D arrays with dynamic arrays: basically by having an array of arrays.
01-20-2010, 12:41 PM#8
Tot
easy:
Collapse JASS:
globals
     constant integer ARRAY_SIZE_X=<>
     constant integer ARRAY_SIZE_Y=<>
     constant integer ARRAY_SIZE=ARRAY_SIZE_X*ARRAY_SIZE_Y
endglobals

struct bääää
   integer array arr[ARRAY_SIZE]

   method getArrayData takes integer x, integer y returns integer
     if x>=ARRAY_SIZE_X or y>=ARRAY_SIZE_Y then //that no 1 accesses invalid indices
        return 0
     endif
     return .arr[y*ARRAY_SIZE_Y+x]
   endmethod

   method setArrayData takes integer x, integer y, integer data returns nothing
     if x>=ARRAY_SIZE_X or y>=ARRAY_SIZE_Y then //that no 1 accesses invalid indices
        return 
     endif
     set .arr[y*ARRAY_SIZE_Y+x]=data
   endmethod
endstruct
01-20-2010, 08:41 PM#9
Anitarf
That does not give you the nice 2D array syntax though.
Collapse JASS:
type innerarray extends integer array[ARRAY_SIZE_Y]

struct foo
    innerarray array bar[ARRAY_SIZE_X]

    static method create takes nothing returns foo
        local foo this=foo.allocate()
        local integer i=0
        loop
            exitwhen i>=ARRAY_SIZE_X
            set .bar[i]=innerarray.create()
            set i=i+1
        endloop
        return this
    endmethod

    method onDestroy takes nothing returns nothing
        local integer i=0
        loop
            exitwhen i>=ARRAY_SIZE_X
            call bar[i].destroy()
            set i=i+1
        endloop
    endmethod
endstruct


    local foo f=foo.create()
    set f.bar[1][3]=1
01-20-2010, 08:57 PM#10
Tot
Quote:
Originally Posted by Anitarf
That does not give you the nice 2D array syntax though.
but works fine and is less to type and less work for the cpu (loopfree!!)

if jh has "real" operators (no blame for vex), you can simulate real arrays, but without...
01-20-2010, 10:10 PM#11
Ammorth
Quote:
Originally Posted by Anitarf
That does not give you the nice 2D array syntax though.
Collapse JASS:
type innerarray extends integer array[ARRAY_SIZE_Y]

struct foo
    innerarray array bar[ARRAY_SIZE_X]

    static method create takes nothing returns foo
        local foo this=foo.allocate()
        local integer i=0
        loop
            exitwhen i>=ARRAY_SIZE_X
            set .bar[i]=innerarray.create()
            set i=i+1
        endloop
        return this
    endmethod

    method onDestroy takes nothing returns nothing
        local integer i=0
        loop
            exitwhen i>=ARRAY_SIZE_X
            call bar[i].destroy()
            set i=i+1
        endloop
    endmethod
endstruct


    local foo f=foo.create()
    set f.bar[1][3]=1

That's pretty neat. Thanks.
01-20-2010, 11:21 PM#12
Vexorian
With enough hacking and a text macro I think you can have tot's solution AND [][] syntax. I'll leave it as an exercise.
01-21-2010, 06:13 PM#13
Tot
Quote:
Originally Posted by Vexorian
With enough hacking and a text macro I think you can have tot's solution AND [][] syntax. I'll leave it as an exercise.

??? [][] operators...in vJass???
have i missed something??
01-21-2010, 07:52 PM#14
Vexorian
The trick is to be creative.
01-21-2010, 08:42 PM#15
Ammorth
In all honesty, I am not sure how the textmacro would help in allowing [][] syntax. Truthfully, I have used text-macros once, and only for readability (cause I could have copied and pasted the code and changed what I wanted faster than I learnt how to use them).

with structs you can overload [], so I'm guessing thats one of the operations. Arrays are the only other object that use []...

I'm just thinking here...