prioqueue

Documentation is at http://tcllib.sourceforge.net/doc/prioqueue.html

This part of the struct (data structures) submodule of tcllib provides a prioritized queue.


Simple usage example:

package require struct 1.3
# create a prioqueue with integer priorities
set pq [::struct::prioqueue -integer]

# get some random things an put them in the queue
for {set i 0} {$i < 100} {incr i} {
    $pq put item$i [expr {int(rand()*500)}]
}

# Iterate over the queue and output the items in priority order
# To process the whole queue in one step better use:
# foreach item [$pq get [$pq size]] { ... } 
while {[$pq size]} {
    puts stdout [$pq get]
}

# destroy the queue after use
$pq destroy

Ken: Is there a way to change the priority to only retrieve the smallest number in the queue instead of the largest number

RS Looks like this should be sufficient:

 set pq [::struct::prioqueue]
 lset struct::prioqueue::sortdir 0 1

This way you don't edit the Tcllib source yourself, but override the default for -integer. However, this applies globally for all prioqueues from that time on.

Ken: Sorry I have another question, if i already have time as my main queue ordering can i also include another ordering beside having time like for example below instance:

Event Queue Status : N1 Awake 300 N2 Awake 300 N1 Broadcast 300 N2 Sleep 4000 Since i can order them accordingly to integers which is the last value? Can i also beside ordering them in integers also order their Index to attain the event queue like below and which allow me to do it anytime:

N1 Awake 300 N1 Broadcast 300 N2 Awake 300 N2 Sleep 4000

schlenk: One way to get the last item would be to use the peek subcommand to peek/peekpriority all items and use lindex end to pick the last one.

 set pq [::struct::prioqueue]
 $pq put 1 20 3 40 5 10
 set lowestprio [lindex [$pq peekpriority [$pq size]] end]

For your second question. No, vector like multi dimensional ordering isn't supported. Depending on your problem domain you could use something like the sqlite database to create such sorted projections or roll your own specialized system.


Stacks and queues also has a prioqueue.
And there is a priority queue based on pairing-heaps called pheap.