• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

QueueTable v1.0

JASS:
//*************************************************************************************
//*
//*    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

Demo Code:
JASS:
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
Contents

QueueTable v1.0 (Map)

Reviews
14:49, 19th Jul 2013 PurgeandFire: Approved. Works.
what's so different about this?
JASS:
function haha takes nothing returns nothing
    local integer i = 0
    loop
        set i = i+1
        call SaveInteger(udg_hash, 1, i, i)
        exitwhen i==4
    endloop
    
    set i = 0
    loop
        set i = i+1
        call BJDebugMsg(I2S(LoadInteger(udg_hash, 1, i)))        
        exitwhen i==4
    endloop
endfunction

also, the Queue function is not readable, set it to normal udg_Queue_Table
 
what's so different about this?
JASS:
function haha takes nothing returns nothing
    local integer i = 0
    loop
        set i = i+1
        call SaveInteger(udg_hash, 1, i, i)
        exitwhen i==4
    endloop
    
    set i = 0
    loop
        set i = i+1
        call BJDebugMsg(I2S(LoadInteger(udg_hash, 1, i)))        
        exitwhen i==4
    endloop
endfunction

also, the Queue function is not readable, set it to normal udg_Queue_Table

I liked Queue function as its name,nobody will touch it.
And also, this is a QueueTable,you can make 2147483647 queues with it.(and it is much faster than what you shown.)
 
Do you think you could explain what this does with a more indepth example for idiots like me? I seriously have no idea what this is or how to use it. :3
I'm sure if you were to explain it a bit more it'd benefit more people than just myself
Sorry for not answering this question properly.
What if you use Google to search "Queue" Data structure?
 
one usage of this is for keys for hashtable, like Table by Bribe, it uses data structure queue as a parent key, and childID is the handle of a unit/item, etc...thus you may do this:
JASS:
call SaveStr(hash, queue, 1234, "I am saved")    
call BJDebugMsg(LoadStr(hash, queue, 1234))
In which 1234 is the childID and queue is the parent, so instead of having only 255 hashtable, you may have 1 hashtable with millions of instance...

But, this can be created manually by:
JASS:
local integer hashtable1 = 1
local integer hashtable2 = 2
call SaveStr(hash, hashtable1, 1234, "I am hashtable 1")    
call BJDebugMsg(LoadStr(hash, hashtable1, 1234))
call SaveStr(hash, hashtable2, 1234, "I am hashtable 2")    
call BJDebugMsg(LoadStr(hash, hashtable2, 1234))

EDIT:
and it is much faster than what you shown.
there's nothing faster than direct to native calls :D
 
But how do you get the queue you want?
This pretty much turns hashtables into a normal variable with an unlimited array, correct? How does the queue play into this.
Also a hashtable starts to slow down the more data you store to it. Especially if you plan on giving a single hashtable a million instances lol
Honestly doesn't seem too useful but I can easily be wrong.
 
Oh I get it now. Oh my this is genious. Though you'd have to go out of your way to do this, and honestly it is slower than using pretty much any other method. Actually this seems like a hassle to use tbh. The user would have to remember the queue in which he saved stuff. I don't think I'd ever use this, but it is rather neat.
+Rep
 
Top