2008-01-23 Olivier Dufour <olivier.duff@gmail.com>
[mono.git] / mcs / class / Mono.C5 / UserGuideExamples / Sets.cs
1 /*\r
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
9  \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
12  \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
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: functional sets 2004-12-21\r
23 \r
24 // Compile with \r
25 //   csc /r:C5.dll Sets.cs \r
26 \r
27 using System;\r
28 using System.Text;\r
29 using C5;\r
30 using SCG = System.Collections.Generic;\r
31 \r
32 namespace Sets {\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
40 \r
41   public class Set<T> : HashSet<T> {\r
42     public Set(SCG.IEnumerable<T> enm) : base() {\r
43       AddAll(enm);\r
44     }\r
45 \r
46     public Set(params T[] elems) : this((SCG.IEnumerable<T>)elems) { }\r
47 \r
48     // Set union (+), difference (-), and intersection (*):\r
49 \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
53       else {\r
54         Set<T> res = new Set<T>(s1);\r
55         res.AddAll(s2);\r
56         return res;\r
57       }\r
58     }\r
59 \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
63       else {\r
64         Set<T> res = new Set<T>(s1);\r
65         res.RemoveAll(s2);\r
66         return res;\r
67       }\r
68     }\r
69 \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
73       else {\r
74         Set<T> res = new Set<T>(s1);\r
75         res.RetainAll(s2);\r
76         return res;\r
77       }\r
78     }\r
79 \r
80     // Equality of sets; take care to avoid infinite loops\r
81 \r
82     public static bool operator ==(Set<T> s1, Set<T> s2) {\r
83       return EqualityComparer<Set<T>>.Default.Equals(s1, s2);\r
84     }\r
85 \r
86     public static bool operator !=(Set<T> s1, Set<T> s2) {\r
87       return !(s1 == s2);\r
88     }\r
89 \r
90     public override bool Equals(Object that) {\r
91       return this == (that as Set<T>);\r
92     }\r
93 \r
94     public override int GetHashCode() {\r
95       return EqualityComparer<Set<T>>.Default.GetHashCode(this);\r
96     }\r
97 \r
98     // Subset (<=) and superset (>=) relation:\r
99 \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
103       else\r
104         return s1.ContainsAll(s2);\r
105     }\r
106 \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
110       else\r
111         return s2.ContainsAll(s1);\r
112     }\r
113     \r
114     public override String ToString() {\r
115       StringBuilder sb = new StringBuilder();\r
116       sb.Append("{");\r
117       bool first = true;\r
118       foreach (T x in this) {\r
119         if (!first)\r
120           sb.Append(",");\r
121         sb.Append(x);\r
122         first = false;\r
123       }\r
124       sb.Append("}");\r
125       return sb.ToString();\r
126     }\r
127   }\r
128 \r
129   class MyTest {\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
146     }\r
147 \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
152 \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
155 \r
156     // Both the argument and the result is a Set<Set<int>>\r
157 \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
166           Set<T> ts = t * s;\r
167           if (!tt.Contains(ts))\r
168             worklist.Enqueue(ts);\r
169         }\r
170         tt.Add(s);\r
171       }\r
172       return new Set<Set<T>>((SCG.IEnumerable<Set<T>>)tt);\r
173     }\r
174   }\r
175 }\r