Posted to tcl by hypnotoad at Thu May 11 10:38:27 GMT 2017view raw
- ###
- # Permuation operator, will permute all of the possible values
- # of a matrix given by a dict
- ###
- # usage:
- # permuation MATRIX BODY ?valuedictname?
- #
- # If valuedict name is ommitted, the default "values" is used
- ###
- proc permutation {combinations body {valuevar values} {values {}} {level 0}} {
- incr level
- set final 1
- foreach {field valuelist} $combinations {
- if {[dict exists $values $field]} continue
- set final 0
- if {[llength $valuelist]==0} {
- dict set values $field {}
- permutation $combinations $body $valuevar $values $level
- } else {
- foreach value $valuelist {
- dict set values $field $value
- permutation $combinations $body $valuevar $values $level
- }
- }
- return
- }
- uplevel $level [list set $valuevar $values]
- uplevel $level [list dict with $valuevar {}]
- uplevel $level $body
- }
- # Example
- permutation {
- compartment {5compt}
- generator {one two three}
- switchboard {on two three}
- } {
- puts [list PERMUTATION {*}$combo]
- } combo