1 | // Sets.cs HDO, 2006-08-28
|
---|
2 | // -------
|
---|
3 | // Definition of types BitSet and Set256 as well as methods for their manipulation.
|
---|
4 | //=====================================|========================================
|
---|
5 |
|
---|
6 | #undef TEST_SETS
|
---|
7 |
|
---|
8 | using System;
|
---|
9 | using System.Text;
|
---|
10 |
|
---|
11 | public class Sets {
|
---|
12 |
|
---|
13 | public const String MODULENAME = "Sets";
|
---|
14 |
|
---|
15 | public static void SetsMethod(Utils.ModuleAction action, out String moduleName) {
|
---|
16 | //-----------------------------------|----------------------------------------
|
---|
17 | moduleName = MODULENAME;
|
---|
18 | switch (action) {
|
---|
19 | case Utils.ModuleAction.getModuleName:
|
---|
20 | return;
|
---|
21 | case Utils.ModuleAction.initModule:
|
---|
22 | emptySet = new Set256();
|
---|
23 | break;
|
---|
24 | case Utils.ModuleAction.resetModule:
|
---|
25 | break;
|
---|
26 | case Utils.ModuleAction.cleanupModule:
|
---|
27 | break;
|
---|
28 | } // switch
|
---|
29 | } // SetsFunc
|
---|
30 |
|
---|
31 | public const ushort BITSET_BIT = 16;
|
---|
32 | public const ushort BITSET_MASK = 15;
|
---|
33 | public const ushort ELEM_MAX = 256; // max. nr. of elems in a Set256
|
---|
34 | public const ushort SET256_LEN = 16;
|
---|
35 |
|
---|
36 | public class Set256 {
|
---|
37 |
|
---|
38 | public ushort[] ushorts;
|
---|
39 |
|
---|
40 | public Set256() {
|
---|
41 | ushorts = new ushort[SET256_LEN];
|
---|
42 | } // Set256
|
---|
43 |
|
---|
44 | public Set256(params ushort[] paramUshorts) {
|
---|
45 | ushorts = new ushort[SET256_LEN];
|
---|
46 | for (int i = 0; i < paramUshorts.Length; i++)
|
---|
47 | ushorts[i] = paramUshorts[i];
|
---|
48 | } // Set256
|
---|
49 |
|
---|
50 | public ushort this[int i] {
|
---|
51 | get {
|
---|
52 | return ushorts[i];
|
---|
53 | }
|
---|
54 | set {
|
---|
55 | ushorts[i] = value;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override String ToString() {
|
---|
60 | StringBuilder sb = new StringBuilder("{");
|
---|
61 | bool first = true;
|
---|
62 | for (ushort i = 0; i < ELEM_MAX; i++)
|
---|
63 | if (member(i, this)) {
|
---|
64 | if (!first)
|
---|
65 | sb.Append(", ");
|
---|
66 | sb.Append(i.ToString());
|
---|
67 | first = false;
|
---|
68 | } // if
|
---|
69 | sb.Append("}");
|
---|
70 | return sb.ToString();
|
---|
71 | } // ToString
|
---|
72 |
|
---|
73 | } // Set256
|
---|
74 |
|
---|
75 | public static Set256 emptySet; // empty set constant
|
---|
76 |
|
---|
77 | // --- operations for BitSet arrays ---
|
---|
78 |
|
---|
79 | private static void checkVal(ushort x) {
|
---|
80 | if ((x < 0) || (x > 255)) {
|
---|
81 | Errors.Restriction("Sets", "setincl", "value {0} out of supported range 0..255", x);
|
---|
82 | } // if
|
---|
83 | } //checkVal
|
---|
84 |
|
---|
85 | // x member of s?
|
---|
86 | public static bool member(ushort x, Set256 s) {
|
---|
87 | //-----------------------------------|----------------------------------------
|
---|
88 | checkVal(x);
|
---|
89 | return Utils.TestBit(s.ushorts[x / BITSET_BIT], (ushort)(x & BITSET_MASK));
|
---|
90 | } // member
|
---|
91 |
|
---|
92 | // include x in s
|
---|
93 | public static void setincl(ref Set256 s, ushort x) {
|
---|
94 | //-----------------------------------|----------------------------------------
|
---|
95 | checkVal(x);
|
---|
96 | Utils.SetBit(ref s.ushorts[x / BITSET_BIT], (ushort)(x & BITSET_MASK));
|
---|
97 | } // setincl
|
---|
98 |
|
---|
99 | // exclude x from s
|
---|
100 | //-----------------------------------|----------------------------------------
|
---|
101 | public static void setexcl(ref Set256 s, ushort x) {
|
---|
102 | checkVal(x);
|
---|
103 | Utils.ClrBit(ref s.ushorts[x / BITSET_BIT], (ushort)(x & BITSET_MASK));
|
---|
104 | } // setexcl
|
---|
105 |
|
---|
106 |
|
---|
107 | // --- operations for type Set256 only ---
|
---|
108 |
|
---|
109 | // init s, use
|
---|
110 | // e or E for an <e>lement, e.g., setinit(s, "E", e)
|
---|
111 | // r or R for a <r>ange, e.g., setinit(s, "R", lb, ub)
|
---|
112 | public static void setinit(out Set256 s, String form, params ushort[] p) {
|
---|
113 | //-----------------------------------|----------------------------------------
|
---|
114 | int pi = 0;
|
---|
115 | ushort e, lb, ub;
|
---|
116 | s = new Set256();
|
---|
117 | foreach (char op in form) {
|
---|
118 | switch (op) {
|
---|
119 | case 'E':
|
---|
120 | case 'e':
|
---|
121 | e = p[pi++]; // single element e
|
---|
122 | setincl(ref s, e);
|
---|
123 | break;
|
---|
124 | case 'R':
|
---|
125 | case 'r':
|
---|
126 | lb = p[pi++]; // range lb .. ub
|
---|
127 | ub = p[pi++];
|
---|
128 | if (lb > ub)
|
---|
129 | Utils.FatalError(MODULENAME, "setinit", "lb > ub");
|
---|
130 | e = lb;
|
---|
131 | while (e <= ub) {
|
---|
132 | setincl(ref s, e);
|
---|
133 | e++;
|
---|
134 | } // while
|
---|
135 | break;
|
---|
136 | default:
|
---|
137 | Utils.FatalError(MODULENAME, "setinit",
|
---|
138 | "unknown operation \'{0}\'", op.ToString());
|
---|
139 | break;
|
---|
140 | } // switch
|
---|
141 | } // while
|
---|
142 | } // setinit
|
---|
143 |
|
---|
144 |
|
---|
145 | // s1 <= s2?
|
---|
146 | public static bool setle(Set256 s1, Set256 s2) {
|
---|
147 | //-----------------------------------|----------------------------------------
|
---|
148 | int i = 0;
|
---|
149 | while (i < SET256_LEN) {
|
---|
150 | if ((s1.ushorts[i] & ~(s2.ushorts[i])) != 0)
|
---|
151 | return false;
|
---|
152 | i++;
|
---|
153 | } // while
|
---|
154 | return true;
|
---|
155 | } // setle
|
---|
156 |
|
---|
157 |
|
---|
158 | // sets s1 and s2 disjoint?
|
---|
159 | public static bool setdisjoint(Set256 s1, Set256 s2) {
|
---|
160 | //-----------------------------------|----------------------------------------
|
---|
161 | int i = 0;
|
---|
162 | while (i < SET256_LEN) {
|
---|
163 | if ((s1.ushorts[i] & s2.ushorts[i]) != 0)
|
---|
164 | return false;
|
---|
165 | i++;
|
---|
166 | } // while
|
---|
167 | return true;
|
---|
168 | } // setdisjoint
|
---|
169 |
|
---|
170 |
|
---|
171 | // s1 = s1 inters s2
|
---|
172 | public static void setinters(ref Set256 s1, Set256 s2) {
|
---|
173 | //-----------------------------------|----------------------------------------
|
---|
174 | int i = 0;
|
---|
175 | while (i < SET256_LEN) {
|
---|
176 | s1.ushorts[i] &= s2.ushorts[i];
|
---|
177 | i++;
|
---|
178 | } // while
|
---|
179 | } // setinters
|
---|
180 |
|
---|
181 |
|
---|
182 | // s1 = s1 union s2
|
---|
183 | public static void setunion(ref Set256 s1, Set256 s2) {
|
---|
184 | //-----------------------------------|----------------------------------------
|
---|
185 | int i = 0;
|
---|
186 | while (i < SET256_LEN) {
|
---|
187 | s1.ushorts[i] |= s2.ushorts[i];
|
---|
188 | i++;
|
---|
189 | } // while
|
---|
190 | } // setunion
|
---|
191 |
|
---|
192 | // s1 = s1 - s2
|
---|
193 | public static void setdiff(ref Set256 s1, Set256 s2) {
|
---|
194 | //-----------------------------------|----------------------------------------
|
---|
195 | int i = 0;
|
---|
196 | while (i < SET256_LEN) {
|
---|
197 | s1.ushorts[i] &= (ushort)(~(s2.ushorts[i]));
|
---|
198 | i++;
|
---|
199 | } // while
|
---|
200 | } // setdiff
|
---|
201 |
|
---|
202 |
|
---|
203 | // clear s, s = {}
|
---|
204 | public static void setclear(ref Set256 s) {
|
---|
205 | //-----------------------------------|----------------------------------------
|
---|
206 | for (int i = 0; i < SET256_LEN; i++)
|
---|
207 | s.ushorts[i] = 0;
|
---|
208 | } // setclear
|
---|
209 |
|
---|
210 | // set assignment, s1 = s2
|
---|
211 | public static void setcpy(ref Set256 s1, Set256 s2) {
|
---|
212 | //-----------------------------------|----------------------------------------
|
---|
213 | for (int i = 0; i < SET256_LEN; i++)
|
---|
214 | s1.ushorts[i] = s2.ushorts[i];
|
---|
215 | } // setcpy
|
---|
216 |
|
---|
217 | // set comparison, s1 == s2?
|
---|
218 | public static bool seteq(Set256 s1, Set256 s2) {
|
---|
219 | //-----------------------------------|----------------------------------------
|
---|
220 | for (int i = 0; i < SET256_LEN; i++)
|
---|
221 | if (s1.ushorts[i] != s2.ushorts[i])
|
---|
222 | return false;
|
---|
223 | return true;
|
---|
224 | } // seteq
|
---|
225 |
|
---|
226 | // s empty?
|
---|
227 | public static bool setempty(Set256 s) {
|
---|
228 | //-----------------------------------|----------------------------------------
|
---|
229 | for (int i = 0; i < SET256_LEN; i++)
|
---|
230 | if (s.ushorts[i] != 0)
|
---|
231 | return false;
|
---|
232 | return true;
|
---|
233 | } // setempty
|
---|
234 |
|
---|
235 |
|
---|
236 | #if TEST_SETS
|
---|
237 |
|
---|
238 | public static void Main(String[] args) {
|
---|
239 | Console.WriteLine("START: Sets");
|
---|
240 |
|
---|
241 | Console.WriteLine("installing ...");
|
---|
242 | Utils.InstallModule("Utils", new Utils.ModuleMethodDelegate(Utils.UtilsMethod));
|
---|
243 | Utils.InstallModule("Sets", new Utils.ModuleMethodDelegate(Sets.SetsMethod ));
|
---|
244 | Console.WriteLine("initModule ...");
|
---|
245 | Utils.Modules(Utils.ModuleAction.initModule);
|
---|
246 |
|
---|
247 | Console.WriteLine("emptySet = " + emptySet);
|
---|
248 |
|
---|
249 | Set256 s = new Set256();
|
---|
250 | Console.WriteLine("s = " + s);
|
---|
251 | setincl(ref s, 177);
|
---|
252 | setincl(ref s, 4);
|
---|
253 | Console.WriteLine("after setincl(ref s, 177) and setincl(ref s, 4), s = " + s);
|
---|
254 | setexcl(ref s, 177);
|
---|
255 | setexcl(ref s, 4);
|
---|
256 | Console.WriteLine("after setexcl(ref s, 177) and setexcl(ref s, 4), s = " + s);
|
---|
257 |
|
---|
258 | setinit(out s, "E", 4);
|
---|
259 | Console.WriteLine("after setinit(out s, \"E\", 4), s = " + s);
|
---|
260 | setinit(out s, "R", 4, 177);
|
---|
261 | Console.WriteLine("after setinit(out s, \"R\", 4, 177), s = " + s);
|
---|
262 |
|
---|
263 | Set256 a, b;
|
---|
264 | setinit(out a, "E", 4);
|
---|
265 | setinit(out b, "E", 5);
|
---|
266 | Console.WriteLine("setle(a, b) = " + setle(a, b));
|
---|
267 |
|
---|
268 | setinit(out a, "R", 0, 9);
|
---|
269 | setinit(out b, "R", 9, 100);
|
---|
270 | Console.WriteLine("setdisjoint(a, b) = " + setdisjoint(a, b));
|
---|
271 |
|
---|
272 | setinit(out a, "R", 0, 99);
|
---|
273 | setinit(out b, "R", 80, 100);
|
---|
274 | setinters(ref a, b);
|
---|
275 | Console.WriteLine("setinters(ref a, b) = " + a);
|
---|
276 |
|
---|
277 | setinit(out a, "R", 0, 9);
|
---|
278 | setinit(out b, "R", 100, 109);
|
---|
279 | setunion(ref a, b);
|
---|
280 | Console.WriteLine("setunion(ref a, b) = " + a);
|
---|
281 |
|
---|
282 | setinit(out a, "R", 0, 99);
|
---|
283 | setinit(out b, "R", 1, 98);
|
---|
284 | setdiff(ref a, b);
|
---|
285 | Console.WriteLine("setdiff(ref a, b) = " + a);
|
---|
286 |
|
---|
287 | setclear(ref a);
|
---|
288 | Console.WriteLine("setclear(a) = " + a);
|
---|
289 |
|
---|
290 | setinit(out b, "R", 0, 9);
|
---|
291 | setcpy(ref a, b);
|
---|
292 | Console.WriteLine("setcpy(a) = " + a);
|
---|
293 |
|
---|
294 | Console.WriteLine("seteq(a, b) = " + seteq(a, b));
|
---|
295 |
|
---|
296 | Console.WriteLine("setempty(a) = " + setempty(a));
|
---|
297 | Console.WriteLine("setempty(emptySet) = " + setempty(emptySet));
|
---|
298 |
|
---|
299 | Console.WriteLine("resetModule ...");
|
---|
300 | Utils.Modules(Utils.ModuleAction.resetModule);
|
---|
301 |
|
---|
302 | Console.WriteLine("cleanupModule ...");
|
---|
303 | Utils.Modules(Utils.ModuleAction.cleanupModule);
|
---|
304 | Console.WriteLine("END");
|
---|
305 | // Console.WriteLine("type [CR] to continue ...");
|
---|
306 | // Console.ReadLine();
|
---|
307 | } // Main
|
---|
308 |
|
---|
309 | #endif
|
---|
310 |
|
---|
311 | } // Sets
|
---|
312 |
|
---|
313 | // End of Sets.cs
|
---|
314 | //=====================================|========================================
|
---|