tree.tcl

Go to the documentation of this file.
00001 /*  tree.tcl --*/
00002 /* */
00003 /*  Implementation of a tree data structure for Tcl.*/
00004 /* */
00005 /*  Copyright (c) 1998-2000 by Ajuba Solutions.*/
00006 /* */
00007 /*  See the file "license.terms" for information on usage and redistribution*/
00008 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00009 /* */
00010 /*  RCS: @(#) $Id: tree.tcl,v 1.44 2007/06/17 01:22:18 andreas_kupries Exp $*/
00011 
00012 /*  @mdgen EXCLUDE: tree_c.tcl*/
00013 
00014 package require Tcl 8.2
00015 package require struct::list
00016 
00017 namespace ::struct::tree {}
00018 
00019 /*  ### ### ### ######### ######### #########*/
00020 /*  Management of tree implementations.*/
00021 
00022 /*  ::struct::tree::LoadAccelerator --*/
00023 /* */
00024 /*  Loads a named implementation, if possible.*/
00025 /* */
00026 /*  Arguments:*/
00027 /*  key Name of the implementation to load.*/
00028 /* */
00029 /*  Results:*/
00030 /*  A boolean flag. True if the implementation*/
00031 /*  was successfully loaded; and False otherwise.*/
00032 
00033 ret  ::struct::tree::LoadAccelerator (type key) {
00034     variable accel
00035     set r 0
00036     switch -exact -- $key {
00037     critcl {
00038         # Critcl implementation of tree requires Tcl 8.4.
00039         if {![package vsatisfies [package provide Tcl] 8.4]} {return 0}
00040         if {[catch {package require tcllibc}]} {return 0}
00041         set r [llength [info commands ::struct::tree_critcl]]
00042     }
00043     tcl {
00044         variable selfdir
00045         source [file join $selfdir tree_tcl.tcl]
00046         set r 1
00047     }
00048         default {
00049             return -code error "invalid accelerator/impl. package $key:\
00050                 must be one of [join [KnownImplementations] {, }]"
00051         }
00052     }
00053     set accel($key) $r
00054     return $r
00055 }
00056 
00057 /*  ::struct::tree::SwitchTo --*/
00058 /* */
00059 /*  Activates a loaded named implementation.*/
00060 /* */
00061 /*  Arguments:*/
00062 /*  key Name of the implementation to activate.*/
00063 /* */
00064 /*  Results:*/
00065 /*  None.*/
00066 
00067 ret  ::struct::tree::SwitchTo (type key) {
00068     variable accel
00069     variable loaded
00070 
00071     if {[string equal $key $loaded]} {
00072     # No change, nothing to do.
00073     return
00074     } elseif {![string equal $key ""]} {
00075     # Validate the target implementation of the switch.
00076 
00077     if {![info exists accel($key)]} {
00078         return -code error "Unable to activate unknown implementation \"$key\""
00079     } elseif {![info exists accel($key)] || !$accel($key)} {
00080         return -code error "Unable to activate missing implementation \"$key\""
00081     }
00082     }
00083 
00084     # Deactivate the previous implementation, if there was any.
00085 
00086     if {![string equal $loaded ""]} {
00087     rename ::struct::tree        ::struct::tree_$loaded
00088     rename ::struct::tree::prune ::struct::tree::prune_$loaded
00089     }
00090 
00091     # Activate the new implementation, if there is any.
00092 
00093     if {![string equal $key ""]} {
00094     rename ::struct::tree_$key        ::struct::tree
00095     rename ::struct::tree::prune_$key ::struct::tree::prune
00096     }
00097 
00098     # Remember the active implementation, for deactivation by future
00099     # switches.
00100 
00101     set loaded $key
00102     return
00103 }
00104 
00105 /*  ::struct::tree::Implementations --*/
00106 /* */
00107 /*  Determines which implementations are*/
00108 /*  present, i.e. loaded.*/
00109 /* */
00110 /*  Arguments:*/
00111 /*  None.*/
00112 /* */
00113 /*  Results:*/
00114 /*  A list of implementation keys.*/
00115 
00116 ret  ::struct::tree::Implementations () {
00117     variable accel
00118     set res {}
00119     foreach n [array names accel] {
00120     if {!$accel($n)} continue
00121     lappend res $n
00122     }
00123     return $res
00124 }
00125 
00126 /*  ::struct::tree::KnownImplementations --*/
00127 /* */
00128 /*  Determines which implementations are known*/
00129 /*  as possible implementations.*/
00130 /* */
00131 /*  Arguments:*/
00132 /*  None.*/
00133 /* */
00134 /*  Results:*/
00135 /*  A list of implementation keys. In the order*/
00136 /*  of preference, most prefered first.*/
00137 
00138 ret  ::struct::tree::KnownImplementations () {
00139     return {critcl tcl}
00140 }
00141 
00142 ret  ::struct::tree::Names () {
00143     return {
00144     critcl {tcllibc based}
00145     tcl    {pure Tcl}
00146     }
00147 }
00148 
00149 /*  ### ### ### ######### ######### #########*/
00150 /*  Initialization: Data structures.*/
00151 
00152 namespace ::struct::tree {
00153     variable  selfdir [file dirname [info script]]
00154     variable  accel
00155     array  accel =    {tcl 0 critcl 0}
00156     variable  
00157 }
00158 
00159 /*  ### ### ### ######### ######### #########*/
00160 /*  Initialization: Choose an implementation,*/
00161 /*  most prefered first. Loads only one of the*/
00162 /*  possible implementations. And activates it.*/
00163 
00164 namespace ::struct::tree {
00165     variable e
00166     foreach e [KnownImplementations] {
00167     if {[LoadAccelerator $e]} {
00168         SwitchTo $e
00169         break
00170     }
00171     }
00172     un e = 
00173 }
00174 
00175 /*  ### ### ### ######### ######### #########*/
00176 /*  Ready*/
00177 
00178 namespace ::struct {
00179     /*  Export the constructor command.*/
00180     namespace export tree
00181 }
00182 
00183 package provide struct::tree 2.1.1
00184 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1