HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Test out some datastructures for jass

11-04-2004, 02:33 AM#1
weaaddar
Here included is three data structures:
Nodes, vectors, and stacks.
Code:
function objects takes nothing returns gamecache
	return InitGameCache("nodes.w3v")
endfunction

function IdStack takes nothing returns string //the IdStack is unique it can not be destroyed and is well special
	return "0"
endfunction

function Node_getData takes string node returns integer
	return GetStoredInteger(objects(),node,"m_data")
endfunction

function Node_getNext takes string node returns string
	return GetStoredString(objects(),node,"m_next")
endfunction

function Node_setData takes string node,integer data returns nothing
	call StoreInteger(objects(),node,"m_data",data)
endfunction

function Node_setNext takes string node,string next returns nothing
	call StoreString(objects(),node,"m_next",next)
endfunction

function Vector_size takes string vector returns integer
	return GetStoredInteger(objects(),vector,"m_size")
endfunction

function Vector_addElem takes string vector, integer data returns boolean
	local gamecache gc=objects()
	local integer size=Vector_size(vector)
	call StoreInteger(gc,vector,"m_a"+I2S(size),data)
	call StoreInteger(gc,vector,"m_size",size+1)
	return true
endfunction

function Vector_removeElem takes string vector, integer index returns integer
	local integer size=Vector_size(vector)-1
	local gamecache gc=objects()
	local integer val=0
	if(index<=size)then
		set val=GetStoredInteger(gc,vector,"_a"+I2S(index))
		loop
			exitwhen index==size
			call StoreInteger(gc,vector,"m_a"+I2S(index),GetStoredInteger(gc,vector,"_a"+I2S(index+1)))
			set index=index+1
		endloop
		call StoreInteger(gc,vector,"m_size",size)
		call StoreInteger(gc,vector,"m_a"+I2S(size),0) //reset it to zero
	endif
	set gc=null
	return val
endfunction

function Vector_getElem takes string vector,integer index returns integer
	return GetStoredInteger(objects(),vector,"m_a"+I2S(index))
endfunction

function Vector_setElem takes string vector,integer index, integer data returns boolean
	if(index<0 and index>=Vector_size(vector))then
		return false
	endif
	call StoreInteger(objects(),vector,"m_a"+I2S(index),data)
	return true
endfunction

function Stack_push takes string stack, integer data returns nothing
	call Vector_addElem(stack,data)
endfunction

function Stack_peek takes string stack returns integer
	return Vector_getElem(stack,Vector_size(stack)-1)
endfunction

function Stack_pop takes string stack returns integer
	return Vector_removeElem(stack,Vector_size(stack)-1)
endfunction

function Stack_isEmpty takes string stack returns boolean
	return Vector_size(stack)==0
endfunction

function GetUniqueId takes nothing returns integer
	local gamecache gc=null
	local integer last=0
	local string stack="0"
	if(not Stack_isEmpty(stack))then
		return Stack_pop(stack)
	endif
	set gc=objects()
	set last=GetStoredInteger(gc,"index","index")+1
	call StoreInteger(gc,"Index","index",last)
	set gc=null
	return last
endfunction

function DestroyObject takes string object returns nothing
	if(object=="0")then
		return
	endif
	call Stack_push("0",S2I(object)) //recycle object type.
	call FlushStoredMission(objects(),object)
endfunction


function DestroyList takes string node returns nothing
	//this function is meant to destroy every element in a linked list
	local string temp=node
	loop
		exitwhen node=="" or node==null
		set temp=node
		set node=Node_getNext(node)
		call DestroyObject(temp)
	endloop
endfunction

function CreateVector takes nothing returns string
	local gamecache gc=objects()
	local string this=I2S(GetUniqueId())
	call StoreInteger(gc,this,"m_size",0)
	set gc=null
	return this
endfunction

function CreateStack takes nothing returns string
	local gamecache gc=objects()
	local string this=CreateVector()
	set gc=null
	return this
endfunction

function CreateNode takes integer data, string next returns string
	local gamecache gc=objects()
	local string this=I2S(GetUniqueId())
	call StoreInteger(gc,this,"m_data",data)
	call StoreString(gc,this,"m_next",next)
	set gc=null
	return this
endfunction

The most interesting part I think is that I recycle object identifiers. Meaning that let say you have objects like this:
Stack[1]
Vector[2]
List[3]
Stack[4]
Vector[5]
Let say you delete the list, and then want to add another stack your identifiers would look like this
Stack[1]
Vector[2]
Stack[3]
Stack[4]
Vector[5]

Also, I can use the same destroy because they all share Uids which is pretty neat. Unfortuantly this isn't tested so I have no idea if they will work.
11-04-2004, 07:56 PM#2
Luzif3r
and what exactly does it do?
11-05-2004, 04:10 PM#3
KaTTaNa
I havn't tested it, but what makes you think it recycles object ids?
It looks like it simply increases the reference counter by one every time you create an object - but what's the point of recycling the object ids anyway? 32-bit integers should be sufficent.
11-06-2004, 06:51 AM#4
weaaddar
Kattana if you look when you use the destroy it throws the old reference handle into a stack. The stack is popped before it tries incrementing the reference counter.

I'm recycling the counter position because of an effeciency craving. And I figure I needed some practice. Using the nodes I'm sure I can throw up a constant time queue.