Purpose: Definition and discussion of set functionality, especially the function to merge two or more sets.
Back to the Chart of proposed set functionality.
Arguments:
Result:
Implementation:
Several variants are possible:
Timing information:
Summary:
Update (1999 Mar 12):
Variant A.
proc ::setops::union {args} { switch [llength $args] { 0 { return {} } 1 { return [lindex $args 0] } default { foreach set $args { foreach e $set { set tmp($e) . } } return [array names tmp] } } }
Variant B.
proc ::setops::union {args} { switch [llength $args] { 0 { return {} } 1 { return [lindex $args 0] } default { set tmp {} foreach set $args { foreach e $set { lappend tmp $e } } # remove duplicates -- # sort and scan. shortcut in case of empty or # single-element result. if {[llength $tmp] < 2} { return $tmp } set tmp [lsort $tmp] set last [lindex $tmp 0] set tmp [lrange $tmp 1 end] set res $last foreach e $tmp { if {[string compare $e $last] != 0} { lappend res $e set last $e } } return $res } } }
Variant C.
proc ::setops::union {args} { switch [llength $args] { 0 { return {} } 1 { return [lindex $args 0] } default { set tmp {} foreach set $args { foreach e $set { lappend tmp $e } } # -W- remove duplicates -- # hash out. shortcut in case of empty or # single-element result. if {[llength $tmp] < 2} { return $tmp } foreach e $tmp { set tmpa($e) . } return [array names tmpa] } } }
Variant D.
proc ::setops::union {args} { switch [llength $args] { 0 { return {} } 1 { return [lindex $args 0] } default { foreach set $args { if {[llength $set] > 0} { foreach $set {.} {break} } } unset args set info locals } } }
-- AK