-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol_set.cs
299 lines (251 loc) · 9.43 KB
/
symbol_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
namespace CUP
{
using System;
/// <summary>This class represents a set of symbols and provides a series of
/// set operations to manipulate them.
/// *
/// </summary>
/// <seealso cref=" CUP.symbol
/// "/>
/// <version> last updated: 11/25/95
/// </version>
/// <author> Scott Hudson
///
/// </author>
public class symbol_set
{
private void InitBlock()
{
_all = new System.Collections.Hashtable(11);
}
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Constructor for an empty set.
/// </summary>
public symbol_set()
{
InitBlock();
}
/// <summary>Constructor for cloning from another set.
/// </summary>
/// <param name="other">the set we are cloning from.
///
/// </param>
public symbol_set(symbol_set other)
{
InitBlock();
not_null(other);
_all = (System.Collections.Hashtable) other._all.Clone();
}
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/// <summary>A hash table to hold the set. Symbols are keyed using their name string.
/// </summary>
//UPGRADE_NOTE: The initialization of '_all' was moved to method 'InitBlock'. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1005"'
protected internal System.Collections.Hashtable _all;
/// <summary>Access to all elements of the set.
/// </summary>
public virtual System.Collections.IEnumerator all()
{
return _all.Values.GetEnumerator();
}
/// <summary>size of the set
/// </summary>
public virtual int size()
{
return _all.Count;
}
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/// <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 contains a particular symbol.
/// </summary>
/// <param name="sym">the symbol we are looking for.
///
/// </param>
public virtual bool contains(symbol sym)
{
return _all.ContainsKey(sym.name_Renamed_Method());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <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(symbol_set other)
{
not_null(other);
/* walk down our set and make sure every element is in the other */
//UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
for (System.Collections.IEnumerator e = all(); e.MoveNext(); )
{
//UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
if (!other.contains((symbol) e.Current))
return false;
}
/* they were all there */
return true;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Determine if this set is an (improper) superset of another.
/// </summary>
/// <param name="other">the set we are are testing against.
///
/// </param>
public virtual bool is_superset_of(symbol_set other)
{
not_null(other);
return other.is_subset_of(this);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Add a single symbol to the set.
/// </summary>
/// <param name="sym">the symbol we are adding.
/// </param>
/// <returns>true if this changes the set.
///
/// </returns>
public virtual bool add(symbol sym)
{
System.Object previous;
not_null(sym);
/* put the object in */
previous = SupportClass.PutElement(_all, sym.name_Renamed_Method(), sym);
/* if we had a previous, this is no change */
return previous == null;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Remove a single symbol if it is in the set.
/// </summary>
/// <param name="sym">the symbol we are removing.
///
/// </param>
public virtual void remove(symbol sym)
{
not_null(sym);
_all.Remove(sym.name_Renamed_Method());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Add (union) in a complete set.
/// </summary>
/// <param name="other">the set we are adding in.
/// </param>
/// <returns>true if this changes the set.
///
/// </returns>
public virtual bool add(symbol_set other)
{
bool result = false;
not_null(other);
/* walk down the other set and do the adds individually */
//UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
for (System.Collections.IEnumerator e = other.all(); e.MoveNext(); )
{
//UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
result = add((symbol) e.Current) || result;
}
return result;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Remove (set subtract) a complete set.
/// </summary>
/// <param name="other">the set we are removing.
///
/// </param>
public virtual void remove(symbol_set other)
{
not_null(other);
/* walk down the other set and do the removes individually */
//UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
for (System.Collections.IEnumerator e = other.all(); e.MoveNext(); )
{
//UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
remove((symbol) e.Current);
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Equality comparison.
/// </summary>
public virtual bool equals(symbol_set other)
{
if (other == null || other.size() != size())
return false;
/* once we know they are the same size, then improper subset does test */
try
{
return is_subset_of(other);
}
catch (internal_error e)
{
/* can't throw the error (because super class doesn't), so we crash */
e.crash();
return false;
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Generic equality comparison.
/// </summary>
public override bool Equals(object other)
{
if (!(other is symbol_set))
return false;
else
return equals((symbol_set) other);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Compute a hash code.
/// </summary>
public override int GetHashCode()
{
int result = 0;
int cnt;
System.Collections.IEnumerator e;
/* hash together codes from at most first 5 elements */
//UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
for (e = all(), cnt = 0; e.MoveNext() && cnt < 5; cnt++)
{
//UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
result ^= ((symbol) e.Current).GetHashCode();
}
return result;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Convert to a string.
/// </summary>
public override string ToString()
{
System.String result;
bool comma_flag;
result = "{";
comma_flag = false;
//UPGRADE_TODO: method 'java.util.Enumeration.hasMoreElements' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationhasMoreElements"'
for (System.Collections.IEnumerator e = all(); e.MoveNext(); )
{
if (comma_flag)
result += ", ";
else
comma_flag = true;
//UPGRADE_TODO: method 'java.util.Enumeration.nextElement' was converted to ' ' which has a different behavior. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1073_javautilEnumerationnextElement"'
result += ((symbol) e.Current).name_Renamed_Method();
}
result += "}";
return result;
}
/*-----------------------------------------------------------*/
}
}