;Priority Queue - Binary Heap Sort by Lowest Key
;modified by Frankie 'Techlord' Taylor
;References
;http://www.policyalmanac.org/games/binaryHeaps.htm
;http://www.developersdomain.com/vb/articles/queue.htm
;============================
;QUEUEITEM
;============================
Const QUEUEITEM_MAX=4096
Dim queueitemIndex.queueitem(QUEUEITEM_MAX)
Type queueitem
Field id%
Field key%
Field dat%
End Type
Function queueitemStop()
For this.queueitem=Each queueitem
queueitemDelete(this)
Next
End Function
Function queueitemNew.queueitem()
this.queueitem=New queueitem
thisid%=0
thiskey%=0
thisdat%=0
Return this
End Function
Function queueitemDelete(this.queueitem)
Delete this
End Function
Function queueitemMimic(mimic.queueitem,this.queueitem)
mimicid%=thisid%
mimickey%=thiskey%
mimicdat%=thisdat%
End Function
Function queueitemCreate.queueitem(id%=0,key%=0,dat%=0)
this.queueitem=queueitemNew()
thisid%=id%
thiskey%=key%
thisdat%=dat%
Return this
End Function
Function queueitemSwap(queueitem1.queueitem,queueitem2.queueitem)
queueitemkey%=queueitem1key%
queueitemdat%=queueitem1dat%
queueitem1key%=queueitem2key%
queueitem1dat%=queueitem2dat%
queueitem2key%=queueitemkey%
queueitem2dat%=queueitemdat%
End Function
;============================
;QUEUE
;============================
Const QUEUE_MAX=255
Dim queueId.queue(QUEUE_MAX)
Global queueIndex.stack=stackIndexCreate(QUEUE_MAX)
Global queueAvail.stack=stackIndexCreate(QUEUE_MAX)
Type queue
Field id%
Field size%
Field queueitems%
Field queueitem.queueitem[QUEUEITEM_MAX]
End Type
Function queueStart(n=2)
For loop = 1 To n%
queueCreate()
Next
DebugLog "Queues Initialized ["+Str(n)+"]"
End Function
Function queueStop()
For this.queue=Each queue
queueDelete(this)
Next
End Function
Function queueNew.queue()
this.queue=New queue
thisid%=0
thissize%=0
thisqueueitems%=0
thisid%=StackPop(queueIndex.stack)
queueId(thisid)=this
Return this
End Function
Function queueDelete(this.queue)
queueId(thisid)=Null
StackPush(queueIndex.stack,thisid%)
Delete this
End Function
Function queueCreate.queue(size%=QUEUEITEM_MAX)
this.queue=queueNew()
thisqueueitems%=0
thissize%=size%
For loop=0 To thissize%
thisqueueitem.queueitem[loop]=queueitemCreate()
Next
Return this
End Function
Function queueDestroy(this.queue)
For loop=0 To thissize%
queueitemDelete(thisqueueitem[loop])
Next
thisqueueitems%=0
queueDelete(this)
End Function
Function queuePush(this.queue,key%,dat%)
thisqueueitems%=thisqueueitems%+1
thisqueueitem[thisqueueitems%]key%=key%
thisqueueitem[thisqueueitems%]dat%=dat%
queueBuild(this,thisqueueitems%)
End Function
Function queuePop(this.queue)
If thisqueueitems%
dat%=thisqueueitem[1]dat%
queueitemMimic(thisqueueitem[1],thisqueueitem[thisqueueitems%])
thisqueueitems%=thisqueueitems%-1
queueRebuild(this,1)
Return dat%
EndIf
End Function
Function queueBuild(this.queue,queuechild%)
queueparent%=queuechild%/2
If thisqueueitem[queuechild%]key%<thisqueueitem[queueparent%]key%
queueitemSwap(thisqueueitem[queuechild%],thisqueueitem[queueparent%])
queueBuild(this,queueparent%)
EndIf
End Function
Function queueRebuild(this.queue,queueparent%)
queuechild%=2*queueparent%
queuechild2%=queuechild%+1
If queuechild%<thisqueueitems%
If thisqueueitem[queuechild2%]key%<thisqueueitem[queuechild%]key% queuechild%=queuechild2%
If thisqueueitem[queuechild%]key%<thisqueueitem[queueparent%]key%
queueitemSwap(thisqueueitem[queueparent%],thisqueueitem[queuechild%])
queueRebuild(this,queuechild%)
End If
End If
End Function
Function queueDump(this.queue)
For loop = 1 To thissize%
value%=queuePop(this)
If value% DebugLog value%
Next
End Function
Function queuePushLargest(this.queue,key%,dat%)
thisqueueitems%=thisqueueitems%+1
thisqueueitem[thisqueueitems%]key%=key%
thisqueueitem[thisqueueitems%]dat%=dat%
queueBuildLargest(this,thisqueueitems%)
End Function
Function queuePopLargest(this.queue)
If thisqueueitems%
dat%=thisqueueitem[1]dat%
queueitemMimic(thisqueueitem[1],thisqueueitem[thisqueueitems%])
thisqueueitems%=thisqueueitems%-1
queueRebuildLargest(this,1)
Return dat%
EndIf
End Function
Function queueBuildLargest(this.queue,queuechild%)
queueparent%=queuechild%/2
If thisqueueitem[queuechild%]key%>thisqueueitem[queueparent%]key%
queueitemSwap(thisqueueitem[queuechild%],thisqueueitem[queueparent%])
queueBuildLargest(this,queueparent%)
EndIf
End Function
Function queueRebuildLargest(this.queue,queueparent%)
queuechild%=2*queueparent%
queuechild2%=queuechild%+1
If queuechild%<thisqueueitems%
If thisqueueitem[queuechild2%]key%>thisqueueitem[queuechild%]key% queuechild%=queuechild2%
If thisqueueitem[queuechild%]key%>thisqueueitem[queueparent%]key%
queueitemSwap(thisqueueitem[queueparent%],thisqueueitem[queuechild%])
queueRebuildLargest(this,queuechild%)
End If
End If
End Function
Function queuePopLast(this.queue)
If thisqueueitems%
dat%=thisqueueitem[thisqueueitems%]dat%
thisqueueitems%=thisqueueitems%-1
EndIf
Return dat%
End Function