library Indexer
//===========================================================================
globals
private integer array Stack
private integer Free = -1
private integer Generated = -1
//Constants
constant group Indexed=CreateGroup()
endglobals
//===========================================================================
function IndexUnit takes unit u returns nothing
local integer this
if(IsUnitInGroup(u,Indexed))then
//call BJDebugMsg("|cffff0000ERROR:|r Attempting to index a unit twice!")
return
endif
if(Free<0)then
set Generated=Generated+1
set this=Generated
else
set this=Stack[Free]
set Free=Free-1
endif
call SetUnitUserData(u,this)
call GroupAddUnit(Indexed,u)
endfunction
//===========================================================================
function IsUnitIndexed takes unit u returns boolean
return IsUnitInGroup(u,Indexed)
endfunction
//===========================================================================
function GetUnitIndex takes unit u returns integer
if not(IsUnitInGroup(u,Indexed))then
call IndexUnit(u)
endif
return GetUnitUserData(u)
endfunction
//===========================================================================
function ReleaseUnitIndex takes unit u returns nothing
local integer this=GetUnitUserData(u)
if not(IsUnitInGroup(u,Indexed))then
//call BJDebugMsg("|cffff0000ERROR:|r Attempting release the index of a unidexed unit!")
return
endif
call GroupRemoveUnit(Indexed,u)
set Free=Free+1
set Stack[Free]=this
endfunction
//===========================================================================
endlibrary