Moderator
M
Moderator
22:05, 29th Jun 2013
Magtheridon96: K.
Magtheridon96: K.
//*************************************************************************************
//*
//* StackTable by Almia
//*
//*************************************************************************************
//*
//* Table of Stacks, allows you to create your own stack.
//*
//* Maximum number of stacks : 2147483647
//* Maximum number of nodes per stack : 2147483646 (due to that it will cycle to
//* lowest value again)
//* Overall spaces : 4611686014132420609
//*
//* Usable nodes : 8192
//*
//*************************************************************************************
//*
//* API
//*
//* function CreateStack takes nothing returns integer
//* function PushStackNode takes integer stack, integer node returns nothing
//* - inserts node
//* function PopStackNode takes integer stack returns integer
//* - retrieves node(last in, first out)
//* function ClearStack takes integer stack returns nothing
//* - Clears stack nodes
//* function DestroyStack takes integer stack returns nothing
//* - Destroys stack
//*
//*************************************************************************************
//*
//* Credits
//*
//* Nestharus : [Std] Collections
//*
//*************************************************************************************
//*
//* Variables that needs to be created
//*
//* udg_Stack_Table = type : hashtable
//* udg_Stack_Count = type : integer
//*
//*************************************************************************************
constant function StackTable takes nothing returns hashtable
return udg_Stack_Table
endfunction
function CreateStack takes nothing returns integer
if null == udg_Stack_Table then
set udg_Stack_Table = InitHashtable()
endif
set udg_Stack_Count = udg_Stack_Count + 1
call SaveBoolean(StackTable(), udg_Stack_Count, -1, true)
return udg_Stack_Count
endfunction
function IsStack takes integer stack returns boolean
return LoadBoolean(StackTable(), stack, -1)
endfunction
function StackSetNext takes integer stack, integer size, integer node returns nothing
call SaveInteger(StackTable(), stack, size, node)
endfunction
function StackGetNext takes integer stack, integer size returns integer
return LoadInteger(StackTable(), stack, size)
endfunction
function PushStackNode takes integer stack, integer node returns nothing
if IsStack(stack) then
call StackSetNext(stack, node, StackGetNext(stack, 0))
call StackSetNext(stack, 0, node)
else
call BJDebugMsg(" StackTable Error : Attempted to push a non-stack node. ")
endif
endfunction
function PopStackNode takes integer stack returns integer
local integer node
if IsStack(stack) then
set node = StackGetNext(stack, 0)
call StackSetNext(stack, 0, StackGetNext(stack, node))
return node
else
call BJDebugMsg(" StackTable Error : Attempted to pop a non-stack node. ")
endif
return 0
endfunction
function ClearStack takes integer stack returns nothing
if IsStack(stack) then
call StackSetNext(stack, 0, 0)
else
call BJDebugMsg(" StackTable Error : Attempted to clear a non-stack. ")
endif
endfunction
function DestroyStack takes integer stack returns nothing
if IsStack(stack) then
call FlushChildHashtable(StackTable(), stack)
else
call BJDebugMsg(" StackTable Error : Attempted to destroy a non-stack. ")
endif
endfunction
function NodeTest takes nothing returns nothing
// Create Stack
local integer stack = CreateStack()
local integer i = 1
// Insert Node
call PushStackNode(stack, i)
set i = i + 1
call PushStackNode(stack, i)
set i = i + 1
call PushStackNode(stack, i)
set i = i + 1
call PushStackNode(stack, i)
// Retrieve Stack Node
// where Stack became famous as LIFO(last in, first out) Data Structure
set i = PopStackNode(stack)
call BJDebugMsg(I2S(i))
set i = PopStackNode(stack)
call BJDebugMsg(I2S(i))
set i = PopStackNode(stack)
call BJDebugMsg(I2S(i))
set i = PopStackNode(stack)
call BJDebugMsg(I2S(i))
// Clear Stack
call ClearStack(stack)
// Destroy Stack
call DestroyStack(stack)
endfunction
function InitTrig_Demo takes nothing returns nothing
call NodeTest()
endfunction