Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / UserGuideExamples / Sets.cs @ 3

1
/*
2
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
3
 Permission is hereby granted, free of charge, to any person obtaining a copy
4
 of this software and associated documentation files (the "Software"), to deal
5
 in the Software without restriction, including without limitation the rights
6
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
 copies of the Software, and to permit persons to whom the Software is
8
 furnished to do so, subject to the following conditions:
9
 
10
 The above copyright notice and this permission notice shall be included in
11
 all copies or substantial portions of the Software.
12
 
13
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
 SOFTWARE.
20
*/
21

    
22
// C5 example: functional sets 2004-12-21
23

    
24
// Compile with 
25
//   csc /r:C5.dll Sets.cs 
26

    
27
using System;
28
using System.Text;
29
using C5;
30
using SCG = System.Collections.Generic;
31

    
32
namespace Sets {
33
  // The class of sets with item type T, implemented as a subclass of
34
  // HashSet<T> but with functional infix operators * + - that compute
35
  // intersection, union and difference functionally.  That is, they
36
  // create a new set object instead of modifying an existing one.
37
  // The hasher is automatically created so that it is appropriate for
38
  // T.  In particular, this is true when T has the form Set<W> for
39
  // some W, since Set<W> implements ICollectionValue<W>.
40

    
41
  public class Set<T> : HashSet<T> {
42
    public Set(SCG.IEnumerable<T> enm) : base() {
43
      AddAll(enm);
44
    }
45

    
46
    public Set(params T[] elems) : this((SCG.IEnumerable<T>)elems) { }
47

    
48
    // Set union (+), difference (-), and intersection (*):
49

    
50
    public static Set<T> operator +(Set<T> s1, Set<T> s2) {
51
      if (s1 == null || s2 == null) 
52
        throw new ArgumentNullException("Set+Set");
53
      else {
54
        Set<T> res = new Set<T>(s1);
55
        res.AddAll(s2);
56
        return res;
57
      }
58
    }
59

    
60
    public static Set<T> operator -(Set<T> s1, Set<T> s2) {
61
      if (s1 == null || s2 == null) 
62
        throw new ArgumentNullException("Set-Set");
63
      else {
64
        Set<T> res = new Set<T>(s1);
65
        res.RemoveAll(s2);
66
        return res;
67
      }
68
    }
69

    
70
    public static Set<T> operator *(Set<T> s1, Set<T> s2) {
71
      if (s1 == null || s2 == null) 
72
        throw new ArgumentNullException("Set*Set");
73
      else {
74
        Set<T> res = new Set<T>(s1);
75
        res.RetainAll(s2);
76
        return res;
77
      }
78
    }
79

    
80
    // Equality of sets; take care to avoid infinite loops
81

    
82
    public static bool operator ==(Set<T> s1, Set<T> s2) {
83
      return EqualityComparer<Set<T>>.Default.Equals(s1, s2);
84
    }
85

    
86
    public static bool operator !=(Set<T> s1, Set<T> s2) {
87
      return !(s1 == s2);
88
    }
89

    
90
    public override bool Equals(Object that) {
91
      return this == (that as Set<T>);
92
    }
93

    
94
    public override int GetHashCode() {
95
      return EqualityComparer<Set<T>>.Default.GetHashCode(this);
96
    }
97

    
98
    // Subset (<=) and superset (>=) relation:
99

    
100
    public static bool operator <=(Set<T> s1, Set<T> s2) {
101
      if (s1 == null || s2 == null) 
102
        throw new ArgumentNullException("Set<=Set");
103
      else
104
        return s1.ContainsAll(s2);
105
    }
106

    
107
    public static bool operator >=(Set<T> s1, Set<T> s2) {
108
      if (s1 == null || s2 == null) 
109
        throw new ArgumentNullException("Set>=Set");
110
      else
111
        return s2.ContainsAll(s1);
112
    }
113
    
114
    public override String ToString() {
115
      StringBuilder sb = new StringBuilder();
116
      sb.Append("{");
117
      bool first = true;
118
      foreach (T x in this) {
119
        if (!first)
120
          sb.Append(",");
121
        sb.Append(x);
122
        first = false;
123
      }
124
      sb.Append("}");
125
      return sb.ToString();
126
    }
127
  }
128

    
129
  class MyTest {
130
    public static void Main(String[] args) {
131
      Set<int> s1 = new Set<int>(2, 3, 5, 7, 11);
132
      Set<int> s2 = new Set<int>(2, 4, 6, 8, 10);
133
      Console.WriteLine("s1 + s2 = {0}", s1 + s2);
134
      Console.WriteLine("s1 * s2 = {0}", s1 * s2);
135
      Console.WriteLine("s1 - s2 = {0}", s1 - s2);
136
      Console.WriteLine("s1 - s1 = {0}", s1 - s1);
137
      Console.WriteLine("s1 + s1 == s1 is {0}", s1 + s1 == s1);
138
      Console.WriteLine("s1 * s1 == s1 is {0}", s1 * s1 == s1);
139
      Set<Set<int>> ss1 = new Set<Set<int>>(s1, s2, s1 + s2);
140
      Console.WriteLine("ss1 = {0}", ss1);
141
      Console.WriteLine("IntersectionClose(ss1) = {0}", IntersectionClose(ss1));
142
      Set<Set<int>> ss2 =
143
        new Set<Set<int>>(new Set<int>(2, 3), new Set<int>(1, 3), new Set<int>(1, 2));
144
      Console.WriteLine("ss2 = {0}", ss2);
145
      Console.WriteLine("IntersectionClose(ss2) = {0}", IntersectionClose(ss2));
146
    }
147

    
148
    // Given a set SS of sets of Integers, compute its intersection
149
    // closure, that is, the least set TT such that SS is a subset of TT
150
    // and such that for any two sets t1 and t2 in TT, their
151
    // intersection is also in TT.  
152

    
153
    // For instance, if SS is {{2,3}, {1,3}, {1,2}}, 
154
    // then TT is {{2,3}, {1,3}, {1,2}, {3}, {2}, {1}, {}}.
155

    
156
    // Both the argument and the result is a Set<Set<int>>
157

    
158
    static Set<Set<T>> IntersectionClose<T>(Set<Set<T>> ss) {
159
      IQueue<Set<T>> worklist = new CircularQueue<Set<T>>();
160
      foreach (Set<T> s in ss)
161
        worklist.Enqueue(s);
162
      HashSet<Set<T>> tt = new HashSet<Set<T>>();
163
      while (worklist.Count != 0) {
164
        Set<T> s = worklist.Dequeue();
165
        foreach (Set<T> t in tt) {
166
          Set<T> ts = t * s;
167
          if (!tt.Contains(ts))
168
            worklist.Enqueue(ts);
169
        }
170
        tt.Add(s);
171
      }
172
      return new Set<Set<T>>((SCG.IEnumerable<Set<T>>)tt);
173
    }
174
  }
175
}