JASS:
static method allocate takes nothing returns integer
local integer instance = recycle[0]
if ( recycle[instance] == 0 ) then
set recycle[0] = instance + 1
else
set recycle[0] = recycle[instance]
endif
return instance
endmethod
static method deallocate takes integer instance returns nothing
set recycle[instance] = recycle[0]
set recycle[0] = instance
endmethod
greetings.
edit: to understand what happens here:
recycle[0] = here we're going to store a fresh/ready-to-use instance
if ( recycle[instance] == 0 )
-> means, if there are no recycled instances then...set recycle[0] = recycle[instance]
-> this is a chain, it gets a little bit tricky, but here is an example:
JASS:
a = allocate() = 1
b = allocate() = 2
c = allocate() = 3
d = allocate() = 4
deallocate(c)
deallocate(d)
e = allocate() = d = 4
f = allocate() = c = 3
JASS:
recycle[3] = recycle[0]
recycle[0] = 3
JASS:
recycle[4] = recycle[0] // recycle[0] = c = 3 //chain time
recycle[0] = 4
we know that we have a fresh instance in recycle[0] (which is d -> 4), but we have to check if there're more recycled ids in the chain
if ( recycle[instance] == 0 )
-> false, we have recycled ids (a chain).so update index 0 of recycle with the next one:
JASS:
set recycle[0] = recycle[instance]
but, what happens if i don't have any chain (recycled instances)?
well,
if ( recycle[instance] == 0 )
-> true, so:set recycle[0] = instance + 1
so, first recycle[0] is 0, and then we do recycle[0] + 1 (instance + 1) which is 1 and so on, pretty simple.
Last edited: