tree.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 package require Tcl 8.2
00015 package require struct::list
00016
00017 namespace ::struct::tree {}
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00058
00059
00060
00061
00062
00063
00064
00065
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
00106
00107
00108
00109
00110
00111
00112
00113
00114
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
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
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
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
00161
00162
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
00177
00178 namespace ::struct {
00179
00180 namespace export tree
00181 }
00182
00183 package provide struct::tree 2.1.1
00184