xifo.tcl

Go to the documentation of this file.
00001 /*  The lifo and fifo classes (for the stooop object oriented extension)*/
00002 /* */
00003 /*  Copyright (c) 2002 by Jean-Luc Fontaine <jfontain@free.fr>.*/
00004 /*  This code may be distributed under the same terms as Tcl.*/
00005 /* */
00006 /*  $Id: xifo.tcl,v 1.4 2004/07/19 19:12:45 jfontain Exp $*/
00007 
00008 
00009 /*  Here is a sample FIFO/LIFO implementation with stooop.*/
00010 /*  Sample test code is at the bottom of this file.*/
00011 
00012 
00013 /*  Uncomment the following lines for the bottom sample code to work:*/
00014 /*  package require stooop*/
00015 /*  namespace import stooop::**/
00016 
00017 
00018 ::stooop::class xifo {
00019 
00020     ret  xifo (type this , type size) {
00021         set ($this,size) $size
00022         empty $this
00023     }
00024 
00025     ret  ~xifo (type this) {
00026         variable ${this}data
00027         catch {unset ${this}data}
00028     }
00029 
00030     ret  in (type this , type data) {
00031         variable ${this}data
00032         tidyUp $this
00033         if {[array size ${this}data] >= $($this,size)} {
00034             unset ${this}data($($this,first))
00035             incr ($this,first)
00036         }
00037         set ${this}data([incr ($this,last)]) $data
00038     }
00039 
00040     ret  tidyUp (type this) {                       ;# warning: for internal use only
00041         variable ${this}data
00042         catch {
00043             unset ${this}data($($this,unset))
00044             unset ($this,unset)
00045         }
00046     }
00047 
00048     ret  empty (type this) {
00049         variable ${this}data
00050         catch {unset ${this}data}
00051         catch {unset ($this,unset)}
00052         set ($this,first) 0
00053         set ($this,last) -1
00054     }
00055 
00056     ret  isEmpty (type this) {
00057         return [expr {$($this,last) < $($this,first)}]
00058     }
00059 
00060     ::stooop::virtual ret  out (type this)
00061 
00062     ::stooop::virtual proc data {this}
00063 }
00064 
00065 
00066 ::stooop::class lifo {
00067 
00068     ret  lifo (type this , optional size =2147483647) xifo {$size} {}
00069 
00070     ret  ~lifo (type this) {}
00071 
00072     ret  out (type this) {
00073         xifo::tidyUp $this
00074         if {[array size xifo::${this}data] == 0} {
00075             error "lifo $this out error, empty"
00076         }
00077         # delay unsetting popped data to improve performance by avoiding a data
00078         # copy:
00079         set xifo::($this,unset) $xifo::($this,last)
00080         incr xifo::($this,last) -1
00081         return [set xifo::${this}data($xifo::($this,unset))]
00082     }
00083 
00084     ret  data (type this) {
00085         set list {}
00086         set first $xifo::($this,first)
00087         for {set index $xifo::($this,last)} {$index >= $first} {incr index -1} {
00088             lappend list [set xifo::${this}data($index)]
00089         }
00090         return $list
00091     }
00092 
00093 }
00094 
00095 
00096 ::stooop::class fifo {
00097 
00098     ret  fifo (type this , optional size =2147483647) xifo {$size} {}
00099 
00100     ret  ~fifo (type this) {}
00101 
00102     ret  out (type this) {
00103         xifo::tidyUp $this
00104         if {[array size xifo::${this}data] == 0} {
00105             error "fifo $this out error, empty"
00106         }
00107         # delay unsetting popped data to improve performance by avoiding a data
00108         # copy:
00109         set xifo::($this,unset) $xifo::($this,first)
00110         incr xifo::($this,first)
00111         return [set xifo::${this}data($xifo::($this,unset))]
00112     }
00113 
00114     ret  data (type this) {
00115         set list {}
00116         set last $xifo::($this,last)
00117         for {set index $xifo::($this,first)} {$index <= $last} {incr index} {
00118             lappend list [set xifo::${this}data($index)]
00119         }
00120         return $list
00121     }
00122 
00123 }
00124 
00125 
00126 /*  Here are a few lines of sample code:*/
00127 /*     proc exercise {id} {*/
00128 /*         for {set u 0} {$u < 10} {incr u} {*/
00129 /*             xifo::in $id $u*/
00130 /*         }*/
00131 /*         puts [xifo::out $id]*/
00132 /*         puts [xifo::data $id]*/
00133 /*         xifo::in $id $u*/
00134 /*         xifo::in $id [incr u]*/
00135 /*         puts [xifo::data $id]*/
00136 /*     }*/
00137 /*     set id [stooop::new lifo 10]*/
00138 /*     exercise $id*/
00139 /*     stooop::delete $id*/
00140 /*     set id [stooop::new fifo 10]*/
00141 /*     exercise $id*/
00142 /*     stooop::delete $id*/
00143 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1