]>
git.zerfleddert.de Git - micropolis/blob - src/tclx/tclsrc/setfuncs.tcl
4 # Perform set functions on lists. Also has a procedure for removing duplicate
6 #------------------------------------------------------------------------------
7 # Copyright 1992 Karl Lehenbauer and Mark Diekhans.
9 # Permission to use, copy, modify, and distribute this software and its
10 # documentation for any purpose and without fee is hereby granted, provided
11 # that the above copyright notice appear in all copies. Karl Lehenbauer and
12 # Mark Diekhans make no representations about the suitability of this
13 # software for any purpose. It is provided "as is" without express or
15 #------------------------------------------------------------------------------
16 # $Id: setfuncs.tcl,v 2.0 1992/10/16 04:52:10 markd Rel $
17 #------------------------------------------------------------------------------
20 #@package: TclX-set_functions union intersect intersect3 lrmdups
23 # return the logical union of two lists, removing any duplicates
25 proc union
{lista listb
} {
26 set full_list
[lsort [concat $lista $listb]]
27 set check_element
[lindex $full_list 0]
28 set outlist
$check_element
29 foreach element
[lrange $full_list 1 end
] {
30 if {$check_element == $element} continue
31 lappend outlist
$element
32 set check_element
$element
38 # sort a list, returning the sorted version minus any duplicates
41 set list [lsort $list]
42 set result
[lvarpop
list]
44 foreach element
$list {
45 if {$last != $element} {
46 lappend result
$element
54 # intersect3 - perform the intersecting of two lists, returning a list
55 # containing three lists. The first list is everything in the first
56 # list that wasn't in the second, the second list contains the intersection
57 # of the two lists, the third list contains everything in the second list
58 # that wasn't in the first.
61 proc intersect3
{list1 list2
} {
66 set list1
[lrmdups
$list1]
67 set list2
[lrmdups
$list2]
72 set list2Result
[concat $list2Result $list2]
77 set list1Result
[concat $list1Result $list1]
80 set compareResult
[string compare
[lindex $list1 0] [lindex $list2 0]]
82 if {$compareResult < 0} {
83 lappend list1Result
[lvarpop list1
]
86 if {$compareResult > 0} {
87 lappend list2Result
[lvarpop list2
]
90 lappend intersectList
[lvarpop list1
]
93 return [list $list1Result $intersectList $list2Result]
97 # intersect - perform an intersection of two lists, returning a list
98 # containing every element that was present in both lists
100 proc intersect
{list1 list2
} {
103 set list1
[lsort $list1]
104 set list2
[lsort $list2]
107 if {[lempty
$list1] ||
[lempty
$list2]} break
109 set compareResult
[string compare
[lindex $list1 0] [lindex $list2 0]]
111 if {$compareResult < 0} {
116 if {$compareResult > 0} {
121 lappend intersectList
[lvarpop list1
]
124 return $intersectList