HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

DARY - Dynamic Array System 0.1

09-16-2006, 12:46 AM#1
PipeDream
DARY is a system for working with dynamically allocated arrays. One master global array, udg_heap, is partitioned by DARY_alloc and DARY_free. Use parallel arrays if you want to work with structures or other types. rheap and uheap are intended for this purpose for reals and units respectively.
It includes some secondary functionality for sorting, heaping, shuffling and searching dynamic arrays, since these are rather tricky to get right.
The arrays are primarily intended for temporary use when speed is essential. In the interest of speed, no array lengthening tricks are used, so you can only allocate 8191 elements. 8191 is the upper limit for one array because save games can fail when one uses the 8192nd element.
For permanent storage, structures and even arrays of fixed length, use Karukef's system. DARY is a niche system for the 1% of the time when you need to random access sets of varying length. If you need more permanent arrays, Vex's CSCache implements very similar functionality with graceful overflow handling.

Example: Find a group of the n closest units to some point
Collapse JASS:
function FillArrayWithGroup takes integer arr, integer max, group g returns integer
    local unit u
    local integer i = 0
    loop
        exitwhen i >= max
        set u = FirstOfGroup(g)
        exitwhen u == null
        set udg_uheap[arr+i] = u
        set i = i + 1
    endloop
    return i
endfunction

function FillUdist takes integer arr, integer len, real x, real y returns nothing
    local integer i = 0
    local real ux
    local real uy
    loop
        exitwhen i >= len
        set ux = GetUnitX(udg_uheap[arr+i]) - x
        set uy = GetUnitY(udg_uheap[arr+i]) - y
        set udg_rheap[arr+i] = ux*ux+uy*uy
        set i = i + 1
    endloop
endfunction


function FillGroupWithPermutedArray takes group g, integer arr, integer size, integer max returns nothing
    local integer i = 0
    if size > max then
        set size = max
    endif
    loop
        exitwhen i>= size 
        call GroupAddUnit(g,udg_uheap[udg_heap[arr+i]])
        set i = i + 1
    endloop
endfunction

//Add 5 closest units to a group
function sort_uloc takes nothing returns nothing
    local group g = CreateGroup()
    local integer arr
    local integer n

    local real x = 0.
    local real y = 0.

    set arr = DARY_allocn(100)
    call GroupEnumUnitsInRange(g,x,y,1000000.,null)
    set n = FillArrayWithGroup(arr,100,g)    //this removes everything in the group
    call DARY_initperm(arr,n)
    call FillUdist(arr,n,x,y)

    call DARY_sort(arr,n,udg_callbacks[DARY_RHEAP_LESS()])

    call FillGroupWithPermutedArray(g,arr,n,5)

    call DARY_freen(arr,100)

    call DestroyGroup(g)
    set g = null
endfunction
Attached Images
File type: jpgarray.jpg (25.8 KB)
Attached Files
File type: w3xarraysys.w3x (25.5 KB)
09-17-2006, 03:55 PM#2
ShadowDestroyer
Good Job once again!
09-19-2006, 07:12 AM#3
Toadcop
PipeDream - nice ! this kind of sys's i love at best !

PS i will check it + performance with your StopWatch + it's a offtop but what i need to compile my own natives ??? if the stuff is not large can you give me a link ? + some documentation about JAPI ?
09-19-2006, 07:29 AM#4
PipeDream
http://www.wc3campaigns.net/showthread.php?t=84417
09-19-2006, 08:16 AM#5
Toadcop
PipeDream - Great Thanks !!!
10-17-2006, 10:26 AM#6
Toadcop
Pipe this sys have some strange method to alloc array for example
[code]
set i=AllocArray(32) // return 33
set i=AllocArray(2)
...
set i=AllocArray(2) // return 33 ??? what a ? i mean it alloc allready used array space !
[code/]
the example is not correct but the fact is what it can alloc the same array spaces.

PS or you don't care about it i can understand it because it's a full public domain =)
10-17-2006, 06:42 PM#7
PipeDream
I don't understand your question at all.
10-17-2006, 09:32 PM#8
Toadcop
PipeDream - i know it =)
i mean the array allocation can be not correct ! what's all =)
10-17-2006, 10:04 PM#9
Vexorian
I think he means that AllocArray seems to be able to return an already used index
10-17-2006, 10:15 PM#10
Toadcop
Quote:
I think he means that AllocArray seems to be able to return an already used index
- ege ! :idiotical: =) yes it is ! i see you have learned to undestand my EnGleash =)
10-17-2006, 11:52 PM#11
PipeDream
Oh thanks. The test map calls DARY_init() in init and then DARY_init() in the sort test which corrupts things pretty badly if you put your tests after it. Just be sure to call DARY_init() only once.
10-18-2006, 11:45 AM#12
Toadcop
PipeDream - OK ! thanks !