Tcl: Use Tclx for Lightweight Stack

The problem

You need a lightweight stack for use in your Tcl script. All you need
are push and pop, and nothing fancy.

The Solutions

There are several solutions to this problem. The first is to use the
struct::stack package, which will provide you with a complete stack
implementation. The second is to use the Tclx package, which provides
two commands: lvarpop and lvarpush. The third solution is to role your
own. However, I advice to go with the first solutions unless you have a
good reason to reinvent the wheel.

The first solution is ideal when you need to use a full-feature
stack. The struct::stack package provides the following operations:
create, destroy, clear, get (the whole contents), trim (shrink to a
certain size), get size, and of course, pop and push. Since one of the
operation is destroy, I believe the package use some sort of
object-oriented design to implement the class. For a lightweight use,
the overhead could be noticeable.

The second solution, using lvarpop and lvarpush from Tclx is more
suitable for lightweight use. In this case, the “stack” really is a
normal Tcl list. In this post, I will talk about these two commands and
save discussion of the full-feature struct::stack package to a future
post.

Using lvarpop and lvarpush

Before you can use these two commands, you need to include the Tclx
package in your script. The following is an example script to
demonstrates these two commands.

# Demo: use of lvarpop and lvarpush to pop and push a stack
package require Tclx

# Create a stack and push 3 items
lvarpush myStack peter
lvarpush myStack paul
lvarpush myStack mary

# The stack is just an ordinary Tcl list. Show it.
puts "Stack: $myStack";                 # {mary paul peter}
puts "Items count: [llength $myStack]"; # 3

# Pop some items and display:
puts "First pop: [lvarpop myStack]";    # mary
puts "Second pop: [lvarpop myStack]";   # paul
puts "Third pop: [lvarpop myStack]";    # peter
puts "Fourth pop: [lvarpop myStack]";   # no error, empty

The Output:

Stack: mary paul peter
Items count: 3

First pop: mary
Second pop: paul
Third pop: peter
Fourth pop: 

Discussion

  • Line 2: includes the Tclx package, which provides lvarpop and
    lvarpush
  • Lines 5-7: Push 3 items into the stack ‘myStack’.
  • Lines 10-11: Since the stack is an ordiary list, we can print it
    out and use llength to get the length (stack size).
  • Lines 14-16: Pops the stack using lvarpop to pop the stack.
  • Line 17: Note that popping an empty stack does not cause any
    error. So before popping, you might want to use llength to check the
    stack size.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s