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