1 # Copyright 2017-2023 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # This file implements some simple data structures in Tcl.
18 # A namespace/commands to support a stack.
20 # To create a stack, call ::Stack::new, recording the returned object ID
21 # for future calls to manipulate the stack object.
25 # set sid [::Stack::new]
28 # stack empty $sid; # returns false
29 # stack pop $sid; # returns "b"
30 # stack pop $sid; # returns "a"
31 # stack pop $sid; # errors with "stack is empty"
34 namespace eval ::Stack {
35 # A counter used to create object IDs
38 # An array holding all object lists, indexed by object ID.
41 # Create a new stack object, returning its object ID.
47 set data_($oid) [list]
51 # Delete the given stack ID.
59 # Returns whether the given stack is empty.
64 return [expr {[llength $data_($oid)] == 0}]
67 # Push ELEM onto the stack given by OID.
68 proc push {oid elem} {
72 lappend data_($oid) $elem
75 # Return and pop the top element on OID. It is an error to pop
81 if {[llength $data_($oid)] == 0} {
82 ::error "stack is empty"
84 set elem [lindex $data_($oid) end]
85 set data_($oid) [lreplace $data_($oid) end end]
89 # Returns the depth of a given ID.
94 return [llength $data_($oid)]
97 # Error handler for invalid object IDs.
101 if {![info exists data_($oid)]} {
102 ::error "object ID $oid does not exist"
106 # Export procs to be used.
107 namespace export empty push pop new delete length error_if
109 # Create an ensemble command to use instead of requiring users
110 # to type namespace proc names.
111 namespace ensemble create -command ::stack
114 # A namespace/commands to support a queue.
116 # To create a queue, call ::Queue::new, recording the returned queue ID
117 # for future calls to manipulate the queue object.
121 # set qid [::Queue::new]
124 # queue empty $qid; # returns false
125 # queue pop $qid; # returns "a"
126 # queue pop $qid; # returns "b"
127 # queue pop $qid; # errors with "queue is empty"
130 namespace eval ::Queue {
132 # Remove and return the oldest element in the queue given by OID.
133 # It is an error to pop an empty queue.
135 variable ::Stack::data_
138 if {[llength $data_($oid)] == 0} {
139 error "queue is empty"
141 set elem [lindex $data_($oid) 0]
142 set data_($oid) [lreplace $data_($oid) 0 0]
146 # "Unpush" ELEM back to the head of the queue given by QID.
147 proc unpush {oid elem} {
148 variable ::Stack::data_
151 set data_($oid) [linsert $data_($oid) 0 $elem]
154 # Re-use some common routines from the Stack implementation.
155 namespace import ::Stack::create ::Stack::new ::Stack::empty \
156 ::Stack::delete ::Stack::push ::Stack::length ::Stack::error_if
158 # Export procs to be used.
159 namespace export new empty push pop new delete length error_if unpush
161 # Create an ensemble command to use instead of requiring users
162 # to type namespace proc names.
163 namespace ensemble create -command ::queue