1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
# queue.tcl --
#
# Queue implementation for Tcl.
#
# Copyright (c) 1998-2000 by Ajuba Solutions.
# Copyright (c) 2008-2010 Andreas Kupries
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: queue_tcl.tcl,v 1.2 2010/03/24 06:13:00 andreas_kupries Exp $
namespace eval ::struct::queue {
# counter is used to give a unique name for unnamed queues
variable counter 0
# Only export one command, the one used to instantiate a new queue
namespace export queue_tcl
}
# ::struct::queue::queue_tcl --
#
# Create a new queue with a given name; if no name is given, use
# queueX, where X is a number.
#
# Arguments:
# name name of the queue; if null, generate one.
#
# Results:
# name name of the queue created
proc ::struct::queue::queue_tcl {args} {
variable I::qat
variable I::qret
variable I::qadd
variable counter
switch -exact -- [llength [info level 0]] {
1 {
# Missing name, generate one.
incr counter
set name "queue${counter}"
}
2 {
# Standard call. New empty queue.
set name [lindex $args 0]
}
default {
# Error.
return -code error \
"wrong # args: should be \"queue ?name?\""
}
}
# FIRST, qualify the name.
if {![string match "::*" $name]} {
# Get caller's namespace; append :: if not global namespace.
set ns [uplevel 1 [list namespace current]]
if {"::" != $ns} {
append ns "::"
}
set name "$ns$name"
}
if {[llength [info commands $name]]} {
return -code error \
"command \"$name\" already exists, unable to create queue"
}
# Initialize the queue as empty
set qat($name) 0
set qret($name) [list]
set qadd($name) [list]
# Create the command to manipulate the queue
interp alias {} $name {} ::struct::queue::QueueProc $name
return $name
}
##########################
# Private functions follow
# ::struct::queue::QueueProc --
#
# Command that processes all queue object commands.
#
# Arguments:
# name name of the queue object to manipulate.
# args command name and args for the command
#
# Results:
# Varies based on command to perform
if {[package vsatisfies [package provide Tcl] 8.5]} {
# In 8.5+ we can do an ensemble for fast dispatch.
proc ::struct::queue::QueueProc {name cmd args} {
# Shuffle method to front and then simply run the ensemble.
# Dispatch, argument checking, and error message generation
# are all done in the C-level.
I $cmd $name {*}$args
}
namespace eval ::struct::queue::I {
namespace export clear destroy get peek \
put unget size
namespace ensemble create
}
} else {
# Before 8.5 we have to code our own dispatch, including error
# checking.
proc ::struct::queue::QueueProc {name cmd args} {
# Do minimal args checks here
if { [llength [info level 0]] == 2 } {
return -code error "wrong # args: should be \"$name option ?arg arg ...?\""
}
# Split the args into command and args components
if { [llength [info commands ::struct::queue::I::$cmd]] == 0 } {
set optlist [lsort [info commands ::struct::queue::I::*]]
set xlist {}
foreach p $optlist {
set p [namespace tail $p]
if {($p eq "K") || ($p eq "Shift") || ($p eq "Shift?")} continue
lappend xlist $p
}
set optlist [linsert [join $xlist ", "] "end-1" "or"]
return -code error \
"bad option \"$cmd\": must be $optlist"
}
uplevel 1 [linsert $args 0 ::struct::queue::I::$cmd $name]
}
}
namespace eval ::struct::queue::I {
# The arrays hold all of the queues which were made.
variable qat ; # Index in qret of next element to return
variable qret ; # List of elements waiting for return
variable qadd ; # List of elements added and not yet reached for return.
}
# ::struct::queue::I::clear --
#
# Clear a queue.
#
# Arguments:
# name name of the queue object.
#
# Results:
# None.
proc ::struct::queue::I::clear {name} {
variable qat
variable qret
variable qadd
set qat($name) 0
set qret($name) [list]
set qadd($name) [list]
return
}
# ::struct::queue::I::destroy --
#
# Destroy a queue object by removing it's storage space and
# eliminating it's proc.
#
# Arguments:
# name name of the queue object.
#
# Results:
# None.
proc ::struct::queue::I::destroy {name} {
variable qat ; unset qat($name)
variable qret ; unset qret($name)
variable qadd ; unset qadd($name)
interp alias {} $name {}
return
}
# ::struct::queue::I::get --
#
# Get an item from a queue.
#
# Arguments:
# name name of the queue object.
# count number of items to get; defaults to 1
#
# Results:
# item first count items from the queue; if there are not enough
# items in the queue, throws an error.
proc ::struct::queue::I::get {name {count 1}} {
if { $count < 1 } {
error "invalid item count $count"
} elseif { $count > [size $name] } {
error "insufficient items in queue to fill request"
}
Shift? $name
variable qat ; upvar 0 qat($name) AT
variable qret ; upvar 0 qret($name) RET
variable qadd ; upvar 0 qadd($name) ADD
if { $count == 1 } {
# Handle this as a special case, so single item gets aren't
# listified
set item [lindex $RET $AT]
incr AT
Shift? $name
return $item
}
# Otherwise, return a list of items
if {$count > ([llength $RET] - $AT)} {
# Need all of RET (from AT on) and parts of ADD, maybe all.
set max [expr {$count - ([llength $RET] - $AT) - 1}]
set result [concat [lrange $RET $AT end] [lrange $ADD 0 $max]]
Shift $name
set AT $max
} else {
# Request can be satisified from RET alone.
set max [expr {$AT + $count - 1}]
set result [lrange $RET $AT $max]
set AT $max
}
incr AT
Shift? $name
return $result
}
# ::struct::queue::I::peek --
#
# Retrieve the value of an item on the queue without removing it.
#
# Arguments:
# name name of the queue object.
# count number of items to peek; defaults to 1
#
# Results:
# items top count items from the queue; if there are not enough items
# to fulfill the request, throws an error.
proc ::struct::queue::I::peek {name {count 1}} {
variable queues
if { $count < 1 } {
error "invalid item count $count"
} elseif { $count > [size $name] } {
error "insufficient items in queue to fill request"
}
Shift? $name
variable qat ; upvar 0 qat($name) AT
variable qret ; upvar 0 qret($name) RET
variable qadd ; upvar 0 qadd($name) ADD
if { $count == 1 } {
# Handle this as a special case, so single item pops aren't
# listified
return [lindex $RET $AT]
}
# Otherwise, return a list of items
if {$count > [llength $RET] - $AT} {
# Need all of RET (from AT on) and parts of ADD, maybe all.
set over [expr {$count - ([llength $RET] - $AT) - 1}]
return [concat [lrange $RET $AT end] [lrange $ADD 0 $over]]
} else {
# Request can be satisified from RET alone.
return [lrange $RET $AT [expr {$AT + $count - 1}]]
}
}
# ::struct::queue::I::put --
#
# Put an item into a queue.
#
# Arguments:
# name name of the queue object
# args items to put.
#
# Results:
# None.
proc ::struct::queue::I::put {name args} {
variable qadd
if { [llength $args] == 0 } {
error "wrong # args: should be \"$name put item ?item ...?\""
}
foreach item $args {
lappend qadd($name) $item
}
return
}
# ::struct::queue::I::unget --
#
# Put an item into a queue. At the _front_!
#
# Arguments:
# name name of the queue object
# item item to put at the front of the queue
#
# Results:
# None.
proc ::struct::queue::I::unget {name item} {
variable qat ; upvar 0 qat($name) AT
variable qret ; upvar 0 qret($name) RET
if {![llength $RET]} {
set RET [list $item]
} elseif {$AT == 0} {
set RET [linsert [K $RET [unset RET]] 0 $item]
} else {
# step back and modify return buffer
incr AT -1
set RET [lreplace [K $RET [unset RET]] $AT $AT $item]
}
return
}
# ::struct::queue::I::size --
#
# Return the number of objects on a queue.
#
# Arguments:
# name name of the queue object.
#
# Results:
# count number of items on the queue.
proc ::struct::queue::I::size {name} {
variable qat
variable qret
variable qadd
return [expr {
[llength $qret($name)] + [llength $qadd($name)] - $qat($name)
}]
}
# ### ### ### ######### ######### #########
proc ::struct::queue::I::Shift? {name} {
variable qat
variable qret
if {$qat($name) < [llength $qret($name)]} return
Shift $name
return
}
proc ::struct::queue::I::Shift {name} {
variable qat
variable qret
variable qadd
set qat($name) 0
set qret($name) $qadd($name)
set qadd($name) [list]
return
}
proc ::struct::queue::I::K {x y} { set x }
# ### ### ### ######### ######### #########
## Ready
namespace eval ::struct {
# Get 'queue::queue' into the general structure namespace for
# pickup by the main management.
namespace import -force queue::queue_tcl
}
|