EMJ But why bother? Because I wanted a queue and ended up going with the NEM version from Implementing FIFO queues. Then when I wanted a stack to use in the same program, I thought it would be nice if it had the same "feel" as the queue. So I took the internals of SimpleStack2 from Performance of Various Stack Implementations and wrapped them in a dict just like the queue.
So here it is:
namespace eval stack {
namespace export create empty push pop peek size tolist
namespace ensemble create
proc create args {
dict create stk $args
}
proc push {stackVar args} {
upvar 1 $stackVar stack
dict lappend stack stk {*}$args
}
proc pop stackVar {
upvar 1 $stackVar stack
set stk [dict get $stack stk]
dict set stack stk [list]
set ret [lindex $stk end]
dict set stack stk [lreplace $stk [set stk end] end]
return $ret
}
proc peek stack {
set stk [dict get $stack stk]
return [lindex $stk end]
}
proc size stack {
llength [dict get $stack stk]
}
proc empty stack {
llength [dict get $stack stk]
}
proc tolist stack {
dict get $stack stk
}
}