1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// C5 example: functional sets 2004-12-21
// Compile with
// csc /r:C5.dll Sets.cs
using System;
using System.Text;
using C5;
using SCG = System.Collections.Generic;
namespace Sets {
// The class of sets with item type T, implemented as a subclass of
// HashSet<T> but with functional infix operators * + - that compute
// intersection, union and difference functionally. That is, they
// create a new set object instead of modifying an existing one.
// The hasher is automatically created so that it is appropriate for
// T. In particular, this is true when T has the form Set<W> for
// some W, since Set<W> implements ICollectionValue<W>.
public class Set<T> : HashSet<T> {
public Set(SCG.IEnumerable<T> enm) : base() {
AddAll(enm);
}
public Set(params T[] elems) : this((SCG.IEnumerable<T>)elems) { }
// Set union (+), difference (-), and intersection (*):
public static Set<T> operator +(Set<T> s1, Set<T> s2) {
if (s1 == null || s2 == null)
throw new ArgumentNullException("Set+Set");
else {
Set<T> res = new Set<T>(s1);
res.AddAll(s2);
return res;
}
}
public static Set<T> operator -(Set<T> s1, Set<T> s2) {
if (s1 == null || s2 == null)
throw new ArgumentNullException("Set-Set");
else {
Set<T> res = new Set<T>(s1);
res.RemoveAll(s2);
return res;
}
}
public static Set<T> operator *(Set<T> s1, Set<T> s2) {
if (s1 == null || s2 == null)
throw new ArgumentNullException("Set*Set");
else {
Set<T> res = new Set<T>(s1);
res.RetainAll(s2);
return res;
}
}
// Equality of sets; take care to avoid infinite loops
public static bool operator ==(Set<T> s1, Set<T> s2) {
return EqualityComparer<Set<T>>.Default.Equals(s1, s2);
}
public static bool operator !=(Set<T> s1, Set<T> s2) {
return !(s1 == s2);
}
public override bool Equals(Object that) {
return this == (that as Set<T>);
}
public override int GetHashCode() {
return EqualityComparer<Set<T>>.Default.GetHashCode(this);
}
// Subset (<=) and superset (>=) relation:
public static bool operator <=(Set<T> s1, Set<T> s2) {
if (s1 == null || s2 == null)
throw new ArgumentNullException("Set<=Set");
else
return s1.ContainsAll(s2);
}
public static bool operator >=(Set<T> s1, Set<T> s2) {
if (s1 == null || s2 == null)
throw new ArgumentNullException("Set>=Set");
else
return s2.ContainsAll(s1);
}
public override String ToString() {
StringBuilder sb = new StringBuilder();
sb.Append("{");
bool first = true;
foreach (T x in this) {
if (!first)
sb.Append(",");
sb.Append(x);
first = false;
}
sb.Append("}");
return sb.ToString();
}
}
class MyTest {
public static void Main(String[] args) {
Set<int> s1 = new Set<int>(2, 3, 5, 7, 11);
Set<int> s2 = new Set<int>(2, 4, 6, 8, 10);
Console.WriteLine("s1 + s2 = {0}", s1 + s2);
Console.WriteLine("s1 * s2 = {0}", s1 * s2);
Console.WriteLine("s1 - s2 = {0}", s1 - s2);
Console.WriteLine("s1 - s1 = {0}", s1 - s1);
Console.WriteLine("s1 + s1 == s1 is {0}", s1 + s1 == s1);
Console.WriteLine("s1 * s1 == s1 is {0}", s1 * s1 == s1);
Set<Set<int>> ss1 = new Set<Set<int>>(s1, s2, s1 + s2);
Console.WriteLine("ss1 = {0}", ss1);
Console.WriteLine("IntersectionClose(ss1) = {0}", IntersectionClose(ss1));
Set<Set<int>> ss2 =
new Set<Set<int>>(new Set<int>(2, 3), new Set<int>(1, 3), new Set<int>(1, 2));
Console.WriteLine("ss2 = {0}", ss2);
Console.WriteLine("IntersectionClose(ss2) = {0}", IntersectionClose(ss2));
}
// Given a set SS of sets of Integers, compute its intersection
// closure, that is, the least set TT such that SS is a subset of TT
// and such that for any two sets t1 and t2 in TT, their
// intersection is also in TT.
// For instance, if SS is {{2,3}, {1,3}, {1,2}},
// then TT is {{2,3}, {1,3}, {1,2}, {3}, {2}, {1}, {}}.
// Both the argument and the result is a Set<Set<int>>
static Set<Set<T>> IntersectionClose<T>(Set<Set<T>> ss) {
IQueue<Set<T>> worklist = new CircularQueue<Set<T>>();
foreach (Set<T> s in ss)
worklist.Enqueue(s);
HashSet<Set<T>> tt = new HashSet<Set<T>>();
while (worklist.Count != 0) {
Set<T> s = worklist.Dequeue();
foreach (Set<T> t in tt) {
Set<T> ts = t * s;
if (!tt.Contains(ts))
worklist.Enqueue(ts);
}
tt.Add(s);
}
return new Set<Set<T>>((SCG.IEnumerable<Set<T>>)tt);
}
}
}
|