-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal_set.cs
335 lines (280 loc) · 9.66 KB
/
terminal_set.cs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
namespace CUP
{
using System;
/// <summary>A set of terminals implemented as a bitset.
/// </summary>
/// <version> last updated: 11/25/95
/// </version>
/// <author> Scott Hudson
///
/// </author>
public class terminal_set
{
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
public override int GetHashCode()
{
return(base.GetHashCode());
}
/// <summary>Constructor for an empty set.
/// </summary>
public terminal_set()
{
/* allocate the bitset at what is probably the right size */
//UPGRADE_NOTE: Class BitArray does not allow calls to methods with index greater than Length property. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1084"'
_elements = new System.Collections.BitArray((terminal.number() % 64 == 0?terminal.number() / 64:terminal.number() / 64 + 1) * 64);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Constructor for cloning from another set.
/// </summary>
/// <param name="other">the set we are cloning from.
///
/// </param>
public terminal_set(terminal_set other)
{
not_null(other);
_elements = (System.Collections.BitArray) other._elements.Clone();
}
/*-----------------------------------------------------------*/
/*--- (Access to) Static (Class) Variables ------------------*/
/*-----------------------------------------------------------*/
/// <summary>Constant for the empty set.
/// </summary>
//UPGRADE_NOTE: Final was removed from the declaration of 'EMPTY '. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1003"'
public static terminal_set EMPTY = new terminal_set();
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Bitset to implement the actual set.
/// </summary>
protected internal System.Collections.BitArray _elements;
/*-----------------------------------------------------------*/
/*--- General Methods ----------------------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Helper function to test for a null object and throw an exception if
/// one is found.
/// </summary>
/// <param name="obj">the object we are testing.
///
/// </param>
protected internal virtual void not_null(object obj)
{
if (obj == null)
throw new internal_error("Null object used in set operation");
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Determine if the set is empty.
/// </summary>
public virtual bool empty()
{
return equals(EMPTY);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Determine if the set contains a particular terminal.
/// </summary>
/// <param name="sym">the terminal symbol we are looking for.
///
/// </param>
public virtual bool contains(terminal sym)
{
not_null(sym);
return _elements.Get(sym.index());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Given its index determine if the set contains a particular terminal.
/// </summary>
/// <param name="indx">the index of the terminal in question.
///
/// </param>
public virtual bool contains(int indx)
{
return _elements.Get(indx);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Determine if this set is an (improper) subset of another.
/// </summary>
/// <param name="other">the set we are testing against.
///
/// </param>
public virtual bool is_subset_of(terminal_set other)
{
not_null(other);
/* make a copy of the other set */
System.Collections.BitArray copy_other = (System.Collections.BitArray) other._elements.Clone();
/* and or in */
copy_other = copy_other.Or(_elements);
/* if it hasn't changed, we were a subset */
return BitArraysEqual(copy_other, other._elements);
/// copy_other.Equals(other._elements);
}
private bool BitArraysEqual(System.Collections.BitArray bits1, System.Collections.BitArray bits2)
{
if(bits1.Count > bits2.Count)
{
// Swap them
System.Collections.BitArray swap = bits1;
bits1 = bits2;
bits2 = swap;
}
for(int i=0; i < bits1.Count; i++)
{
if(i >= bits2.Count)
{
// bits1 is longer than bits2. If there are any
// bits set in bits1, then the two arrays are not
// equal. Zeros don't count if they are trailing
if(bits1[i])
{
return(false);
}
}
else
{
if(bits1[i] != bits2[i])
{
return(false);
}
}
}
return(true);
}
private string BitArrayToString(System.Collections.BitArray bits)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i=0; i < bits.Count; i++)
{
sb.Append(bits[i] ? "1" : "0");
}
return(sb.ToString());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Determine if this set is an (improper) superset of another.
/// </summary>
/// <param name="other">the set we are testing against.
///
/// </param>
public virtual bool is_superset_of(terminal_set other)
{
not_null(other);
return other.is_subset_of(this);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Add a single terminal to the set.
/// </summary>
/// <param name="sym">the terminal being added.
/// </param>
/// <returns>true if this changes the set.
///
/// </returns>
public virtual bool add(terminal sym)
{
bool result;
not_null(sym);
/* see if we already have this */
result = _elements.Get(sym.index());
/* if not we add it */
if (!result)
_elements.Set(sym.index(), true);
return result;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Remove a terminal if it is in the set.
/// </summary>
/// <param name="sym">the terminal being removed.
///
/// </param>
public virtual void remove(terminal sym)
{
not_null(sym);
_elements.Set(sym.index(), false);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Add (union) in a complete set.
/// </summary>
/// <param name="other">the set being added.
/// </param>
/// <returns>true if this changes the set.
///
/// </returns>
public virtual bool add(terminal_set other)
{
not_null(other);
/* make a copy */
System.Collections.BitArray copy = (System.Collections.BitArray) _elements.Clone();
/* or in the other set */
//UPGRADE_NOTE: In .NET BitArrays must be of the same size to allow the 'System.Collections.BitArray.Or' operation. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1083"'
_elements = _elements.Or(other._elements);
/* changed if we are not the same as the copy */
//UPGRADE_TODO: method 'java.util.BitSet.equals' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilBitSetequals_javalangObject"'
return !BitArraysEqual(_elements, copy);
// _elements.Equals(copy);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Determine if this set intersects another.
/// </summary>
/// <param name="other">the other set in question.
///
/// </param>
public virtual bool intersects(terminal_set other)
{
not_null(other);
/* make a copy of the other set */
System.Collections.BitArray copy = (System.Collections.BitArray) other._elements.Clone();
/* xor out our values */
//UPGRADE_NOTE: In .NET BitArrays must be of the same size to allow the 'System.Collections.BitArray.Xor' operation. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1083"'
copy = copy.Xor(this._elements);
/* see if its different */
//UPGRADE_TODO: method 'java.util.BitSet.equals' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilBitSetequals_javalangObject"'
return !BitArraysEqual(copy, other._elements);
// copy.Equals(other._elements);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Equality comparison.
/// </summary>
public virtual bool equals(terminal_set other)
{
if (other == null)
return false;
else
{
//UPGRADE_TODO: method 'java.util.BitSet.equals' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilBitSetequals_javalangObject"'
return _elements.Equals(other._elements);
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Generic equality comparison.
/// </summary>
public override bool Equals(object other)
{
if (!(other is terminal_set))
return false;
else
return equals((terminal_set) other);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Convert to string.
/// </summary>
public override string ToString()
{
System.String result;
bool comma_flag;
result = "{";
comma_flag = false;
for (int t = 0; t < terminal.number(); t++)
{
if (_elements.Get(t))
{
if (comma_flag)
result += ", ";
else
comma_flag = true;
result += terminal.find(t).name_Renamed_Method();
}
}
result += "}";
return result;
}
/*-----------------------------------------------------------*/
}
}