Version 6 of queue

Updated 2004-12-11 00:31:52 by schlenk

Documentation can be found at http://tcllib.sourceforge.net/doc/queue.html .

This module of the tcllib data structures collection provides the data structure portion of a queue.

See Stacks and Queues for a description about what a queue is.


SEH 10 Dec 04 -- I have found out to my regret that it is not really possible to use [queue] in loop structures, because the "peek" and "get" commands acts differently when the queue size is 1 than when it is any number > 1.

It would be nice to be able to do something like:

 foreach item [$queue peek [$queue size]] {<do stuff>}

but if [$queue size] is 1, then the value is returned as a string value, whereas if it's 2, the values are returned as a list. So you can't use the above construction unless you know all queue values will look the same as strings or lists. For example:

 %set q [struct::queue]
 %$q put "a b"
 %foreach item [$q peek [$q size]] {puts $item}
 a
 b
 %$q put "c d"
 %foreach item [$q peek [$q size]] {puts $item}
 a b
 c d

This behavior is documented and hence not actually a bug, but as a practical matter it makes the [queue] structure useless to me.


schlenk I think the original queue author thought of that as a convenience. Maybe he should just add an lpeek subcommand that does what you want.

This should implement it:

 proc ::struct::queue::_lpeek {name {count 1}} {
    variable queues
    if { $count < 1 } {
        error "invalid item count $count"
    }

    if { $count > [llength $queues($name)] } {
        error "insufficient items in queue to fill request"
    }

    set index [expr {$count - 1}]
    return [lrange $queues($name) 0 $index]
 }

(btw. the current queue implementation is smart enough so you just define this after package require ::struct::queue and have lpeek working for your code, but its a hack nontheless)


[ Arts and Crafts of Tcl-Tk Programming | Category Command, package tcllib, module struct | Category Data Structure | ]