Posted to tcl by hypnotoad at Thu May 11 10:38:27 GMT 2017view raw

  1. ###
  2. # Permuation operator, will permute all of the possible values
  3. # of a matrix given by a dict
  4. ###
  5. # usage:
  6. # permuation MATRIX BODY ?valuedictname?
  7. #
  8. # If valuedict name is ommitted, the default "values" is used
  9. ###
  10. proc permutation {combinations body {valuevar values} {values {}} {level 0}} {
  11. incr level
  12. set final 1
  13. foreach {field valuelist} $combinations {
  14. if {[dict exists $values $field]} continue
  15. set final 0
  16. if {[llength $valuelist]==0} {
  17. dict set values $field {}
  18. permutation $combinations $body $valuevar $values $level
  19. } else {
  20. foreach value $valuelist {
  21. dict set values $field $value
  22. permutation $combinations $body $valuevar $values $level
  23. }
  24. }
  25. return
  26. }
  27. uplevel $level [list set $valuevar $values]
  28. uplevel $level [list dict with $valuevar {}]
  29. uplevel $level $body
  30. }
  31.  
  32. # Example
  33. permutation {
  34. compartment {5compt}
  35. generator {one two three}
  36. switchboard {on two three}
  37. } {
  38. puts [list PERMUTATION {*}$combo]
  39. } combo