Moderator
M
Moderator
14:49, 19th Jul 2013
PurgeandFire: Approved. Works.
PurgeandFire: Approved. Works.
//*************************************************************************************
//*
//* QueueTable by Almia
//*
//*************************************************************************************
//*
//* Table of Queues, allows you to create Queues.
//*
//*************************************************************************************
//*
//* API
//*
//* function CreateQueue takes nothing returns integer
//* function EnqueueNode takes integer queue, integer node returns nothing
//* - Inserts a queue node.
//* function PopQueueNode takes integer queue returns integer
//* - Retrieves a new node by FIFO(first in, first out) method
//* function ClearQueue takes integer queue returns nothing
//* function DestroyQueue takes integer queue returns nothing
//*
//*************************************************************************************
//*
//* Credits
//*
//* Nestharus : [Std] Collections
//*
//*************************************************************************************
//*
//* Variables that needs to be created
//*
//* udg_Queue_Table = type : hashtable
//* udg_Queue_Count = type : integer
//*
//*************************************************************************************
constant function Queue takes nothing returns hashtable
return udg_Queue_Table
endfunction
function CreateQueue takes nothing returns integer
if null == udg_Queue_Table then
set udg_Queue_Table = InitHashtable()
endif
set udg_Queue_Count = udg_Queue_Count + 1
call SaveBoolean(Queue(), udg_Queue_Count, -2, true)
return udg_Queue_Count
endfunction
function IsQueue takes integer queue returns boolean
return LoadBoolean(Queue(), udg_Queue_Count, -2)
endfunction
function QueueSet takes integer queue, integer key, integer node, boolean next returns nothing
if next then
call SaveInteger(Queue(), queue, key, node)
else
call SaveInteger(Queue(), queue, -1, node)
endif
endfunction
function QueueGet takes integer queue, integer key, boolean next returns integer
if next then
return LoadInteger(Queue(), queue, key)
endif
return LoadInteger(Queue(), queue, -1)
endfunction
function EnqueueNode takes integer queue, integer node returns nothing
if IsQueue(queue) then
call QueueSet(queue, QueueGet(queue, 0, false), node, true)
call QueueSet(queue, 0, node, false)
call QueueSet(queue, node, 0, true)
else
call BJDebugMsg(" QueueTable Error : Attempted to Enqueue a node to a non-Queue integer. ")
endif
endfunction
function PopQueueNode takes integer queue returns integer
local integer node
if IsQueue(queue) then
set node = QueueGet(queue, 0, true)
call QueueSet(queue, 0, QueueGet(queue, node, true), true)
if 0 == QueueGet(queue, 0, true) then
call QueueSet(queue, 0, 0, false)
endif
return node
endif
call BJDebugMsg(" QueueTable Error : Attempted to pop a node from a non-Queue integer. ")
return 0
endfunction
function ClearQueue takes integer queue returns nothing
if IsQueue(queue) then
call QueueSet(queue, 0, 0, true)
call QueueSet(queue, 0, 0, false)
else
call BJDebugMsg(" QueueTable Error : Attempted to clear a non-Queue integer. ")
endif
endfunction
function DestroyQueue takes integer queue returns nothing
if IsQueue(queue) then
call FlushChildHashtable(Queue(), queue)
else
call BJDebugMsg(" QueueTable Error : Attempted to destroy a non-Queue integer. ")
endif
endfunction
function QueueNodeTest takes nothing returns nothing
// Create Queue
local integer queue = CreateQueue()
local integer i = 1
// Insert/ Enqueue node
call EnqueueNode(queue, i)
set i = i + 1
call EnqueueNode(queue, i)
set i = i + 1
call EnqueueNode(queue, i)
set i = i + 1
call EnqueueNode(queue, i)
// Retrieve/ Pop queue node
set i = PopQueueNode(queue) // 1
call BJDebugMsg(I2S(i))
set i = PopQueueNode(queue) // 2
call BJDebugMsg(I2S(i))
set i = PopQueueNode(queue) // 3
call BJDebugMsg(I2S(i))
set i = PopQueueNode(queue) // 4
call BJDebugMsg(I2S(i))
// Clear queue
call ClearQueue(queue)
// Destroy Queue
call DestroyQueue(queue)
endfunction
function InitTrig_Demo takes nothing returns nothing
call QueueNodeTest()
endfunction