- Reaction score
- 333
This library contains the functions (and an interface):
SetHandleUserData allows you to attach an integer value to any handle type under a certain key. Keys can be positive values, negative values or large values. GetHandleUserData retrieves an attached value from a handle under a certain key.
There is a limit of around 8190 attached integers in total, but there are no sub-limits on how many keys can be attached to a single handle. However, adding too many keys could increase the time it takes to find recently added values.
Use DeleteHandleUserData if you are finished using a key, or DeleteAllHandleUserData if you are finished using a handle or otherwise want to clear all associated values.
You can call TraverseHandleUserData to perform an action on every value attached to a handle. The TraversalFunction must take an integer (which will be the value, sort of like GetEnumUnit() in a ForGroup()).
Here is the code (copy paste into empty trigger or map header):
JASS:
function SetHandleUserData takes handle h, integer key, integer value returns nothing
function GetHandleUserData takes handle h, integer key returns integer
function DeleteHandleUserData takes handle h, integer key returns nothing
function DeleteAllHandleUserData takes handle h returns nothing
function interface TraversalFunction takes integer i returns nothing
function TraverseHandleUserData takes handle h, TraversalFunction f returns nothing
SetHandleUserData allows you to attach an integer value to any handle type under a certain key. Keys can be positive values, negative values or large values. GetHandleUserData retrieves an attached value from a handle under a certain key.
There is a limit of around 8190 attached integers in total, but there are no sub-limits on how many keys can be attached to a single handle. However, adding too many keys could increase the time it takes to find recently added values.
Use DeleteHandleUserData if you are finished using a key, or DeleteAllHandleUserData if you are finished using a handle or otherwise want to clear all associated values.
You can call TraverseHandleUserData to perform an action on every value attached to a handle. The TraversalFunction must take an integer (which will be the value, sort of like GetEnumUnit() in a ForGroup()).
Here is the code (copy paste into empty trigger or map header):
JASS:
library HandleUserData
globals
private boolean array g_bools1
endglobals
function interface TraversalFunction takes integer i returns nothing
private struct node
node left = 0
node right = 0
integer key
integer value
static method create takes integer key, integer value returns node
local node n = node.allocate()
set n.key = key
set n.value = value
set g_bools1[n] = true
return n
endmethod
method onDestroy takes nothing returns nothing
set g_bools1[this] = false
endmethod
endstruct
globals
private node array g_nodes1
private node array g_nodes2
private node array g_nodes3
private node array g_nodes4
endglobals
private function H2I takes handle h returns integer
return h
return 0
endfunction
private function InsertNode takes node root, node insert returns nothing
local node n = root
loop
if (n.key > insert.key) then
if not g_bools1[n.left] then
set n.left = insert
exitwhen true
endif
set n = n.left
elseif (n.key < insert.key) then
if not g_bools1[n.right] then
set n.right = insert
exitwhen true
endif
set n = n.right
else
set n.value = insert.value
call insert.destroy()
exitwhen true
endif
endloop
endfunction
private function DeleteNode takes node root returns nothing
local node n
if not g_bools1[root.left] then
if not g_bools1[root.right] then
call root.destroy()
else
set n = root.right
set root.key = n.key
set root.value = n.value
set root.left = n.left
set root.right = n.right
call n.destroy()
endif
else
if not g_bools1[root.right] then
set n = root.left
set root.key = n.key
set root.value = n.value
set root.left = n.left
set root.right = n.right
call n.destroy()
else
set n = root.right
loop
exitwhen not g_bools1[n.left]
set n = n.left
endloop
set root.key = n.key
set root.value = n.value
call DeleteNode.execute(n)
endif
endif
endfunction
private function PruneNodes takes node root returns nothing
if g_bools1[root] then
call PruneNodes.execute(root.left)
call PruneNodes.execute(root.right)
call root.destroy()
endif
endfunction
function SetHandleUserData takes handle h, integer key, integer value returns nothing
local integer index = H2I(h) - 0x100000
local integer i
local node n
if (index < 8190) then
set i = index
set n = g_nodes1<i>
if (not g_bools1[n]) then
set g_nodes1<i> = node.create(key, value)
else
call InsertNode(n, node.create(key, value))
endif
elseif (index < 16380) then
set i = index-8190
set n = g_nodes2<i>
if (not g_bools1[n]) then
set g_nodes2<i> = node.create(key, value)
else
call InsertNode(n, node.create(key, value))
endif
elseif (index < 24570) then
set i = index-16380
set n = g_nodes3<i>
if (not g_bools1[n]) then
set g_nodes3<i> = node.create(key, value)
else
call InsertNode(n, node.create(key, value))
endif
elseif (index < 32760) then
set i = index-24570
set n = g_nodes4<i>
if (not g_bools1[n]) then
set g_nodes4<i> = node.create(key, value)
else
call InsertNode(n, node.create(key, value))
endif
else
//
endif
endfunction
function GetHandleUserData takes handle h, integer key returns integer
local integer index = H2I(h) - 0x100000
local node n
if (index < 8190) then
set n = g_nodes1[index]
elseif (index < 16380) then
set n = g_nodes2[index-8190]
elseif (index < 24570) then
set n = g_nodes3[index-16380]
elseif (index < 32760) then
set n = g_nodes4[index-24570]
else
//
endif
loop
exitwhen not g_bools1[n]
if (n.key > key) then
set n = n.left
elseif (n.key < key) then
set n = n.right
else
return n.value
endif
endloop
return 0
endfunction
private function TraverseNodes takes node root, TraversalFunction f returns nothing
if g_bools1[root] then
call TraverseNodes.execute(root.left, f)
call TraverseNodes.execute(root.right, f)
call f.execute(root.value)
endif
endfunction
function TraverseHandleUserData takes handle h, TraversalFunction f returns nothing
local integer index = H2I(h) - 0x100000
local node n
if (index < 8190) then
set n = g_nodes1[index]
elseif (index < 16380) then
set n = g_nodes2[index-8190]
elseif (index < 24570) then
set n = g_nodes3[index-16380]
elseif (index < 32760) then
set n = g_nodes4[index-24570]
else
//
endif
call TraverseNodes(n, f)
endfunction
function DeleteHandleUserData takes handle h, integer key returns nothing
local integer index = H2I(h) - 0x100000
local node n
if (index < 8190) then
set n = g_nodes1[index]
elseif (index < 16380) then
set n = g_nodes2[index-8190]
elseif (index < 24570) then
set n = g_nodes3[index-16380]
elseif (index < 32760) then
set n = g_nodes4[index-24570]
else
//
endif
loop
exitwhen not g_bools1[n]
if (n.key > key) then
set n = n.left
elseif (n.key < key) then
set n = n.right
else
call DeleteNode(n)
endif
endloop
endfunction
function DeleteAllHandleUserData takes handle h returns nothing
local integer index = H2I(h) - 0x100000
local node n
if (index < 8190) then
set n = g_nodes1[index]
elseif (index < 16380) then
set n = g_nodes2[index-8190]
elseif (index < 24570) then
set n = g_nodes3[index-16380]
elseif (index < 32760) then
set n = g_nodes4[index-24570]
else
//
endif
if (g_bools1[n]) then
call PruneNodes.execute(n)
endif
endfunction
endlibrary
</i></i></i></i></i></i></i></i>