graph.tcl

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

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1