| 1 |
proc set'contains {set el} {expr {[lsearch -exact $set $el]>=0}} |
| 2 |
|
| 3 |
e.g. {set'contains {A B C} A} -> 1 |
| 4 |
e.g. {set'contains {A B C} D} -> 0 |
| 5 |
|
| 6 |
proc set'add {_set args} { |
| 7 |
upvar 1 $_set set |
| 8 |
foreach el $args { |
| 9 |
if {![set'contains $set $el]} {lappend set $el} |
| 10 |
} |
| 11 |
set set |
| 12 |
} |
| 13 |
|
| 14 |
set example {1 2 3} |
| 15 |
e.g. {set'add example 4} -> {1 2 3 4} |
| 16 |
e.g. {set'add example 4} -> {1 2 3 4} |
| 17 |
|
| 18 |
proc set'remove {_set args} { |
| 19 |
upvar 1 $_set set |
| 20 |
foreach el $args { |
| 21 |
set pos [lsearch -exact $set $el] |
| 22 |
set set [lreplace $set $pos $pos] |
| 23 |
} |
| 24 |
set set |
| 25 |
} |
| 26 |
|
| 27 |
e.g. {set'remove example 3} -> {1 2 4} |
| 28 |
|
| 29 |
proc set'intersection {a b} { |
| 30 |
foreach el $a {set arr($el) ""} |
| 31 |
set res {} |
| 32 |
foreach el $b {if {[info exists arr($el)]} {lappend res $el}} |
| 33 |
set res |
| 34 |
|
| 35 |
e.g. {set'intersection {1 2 3 4} {2 4 6 8}} -> {2 4} |
| 36 |
|
| 37 |
proc set'union {a b} { |
| 38 |
foreach el $a {set arr($el) ""} |
| 39 |
foreach el $b {set arr($el) ""} |
| 40 |
lsort [array names arr] |
| 41 |
} |
| 42 |
|
| 43 |
e.g. {set'union {1 3 5 7} {2 4 6 8}} -> {1 2 3 4 5 6 7 8} |
| 44 |
|
| 45 |
proc set'difference {a b} { |
| 46 |
eval set'remove a $b |
| 47 |
} |
| 48 |
|
| 49 |
e.g. {set'difference {1 2 3 4 5} {2 4 6}} -> {1 3 5} |
| 50 |
|
| 51 |
# https://en.wikibooks.org/wiki/Tcl_Programming/Examples |
| 52 |
|