2
* STANDARD ML OF NEW JERSEY COPYRIGHT NOTICE, LICENSE AND DISCLAIMER.
4
* Copyright (c) 1989-1998 by Lucent Technologies
6
* Permission to use, copy, modify, and distribute this software and its
7
* documentation for any purpose and without fee is hereby granted, provided
8
* that the above copyright notice appear in all copies and that both the
9
* copyright notice and this permission notice and warranty disclaimer appear
10
* in supporting documentation, and that the name of Lucent Technologies, Bell
11
* Labs or any Lucent entity not be used in advertising or publicity pertaining
12
* to distribution of the software without specific, written prior permission.
14
* Lucent disclaims all warranties with regard to this software, including all
15
* implied warranties of merchantability and fitness. In no event shall Lucent
16
* be liable for any special, indirect or consequential damages or any damages
17
* whatsoever resulting from loss of use, data or profits, whether in an action
18
* of contract, negligence or other tortious action, arising out of or in
19
* connection with the use or performance of this software.
21
* Taken from this URL:
22
* http://www.smlnj.org/license.html
24
* This license is compatible with the GNU GPL (see section "Standard ML of New
25
* Jersey Copyright License"):
26
* http://www.gnu.org/licenses/license-list.html#StandardMLofNJ
30
* Copyright 1996-1999 by Scott Hudson, Frank Flannery, C. Scott Ananian
33
package weka.core.parser.java_cup;
35
import java.util.BitSet;
37
/** A set of terminals implemented as a bitset.
38
* @version last updated: 11/25/95
39
* @author Scott Hudson
41
public class terminal_set {
43
/*-----------------------------------------------------------*/
44
/*--- Constructor(s) ----------------------------------------*/
45
/*-----------------------------------------------------------*/
47
/** Constructor for an empty set. */
50
/* allocate the bitset at what is probably the right size */
51
_elements = new BitSet(terminal.number());
54
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
56
/** Constructor for cloning from another set.
57
* @param other the set we are cloning from.
59
public terminal_set(terminal_set other)
63
_elements = (BitSet)other._elements.clone();
66
/*-----------------------------------------------------------*/
67
/*--- (Access to) Static (Class) Variables ------------------*/
68
/*-----------------------------------------------------------*/
70
/** Constant for the empty set. */
71
public static final terminal_set EMPTY = new terminal_set();
73
/*-----------------------------------------------------------*/
74
/*--- (Access to) Instance Variables ------------------------*/
75
/*-----------------------------------------------------------*/
77
/** Bitset to implement the actual set. */
78
protected BitSet _elements;
80
/*-----------------------------------------------------------*/
81
/*--- General Methods ----------------------------------------*/
82
/*-----------------------------------------------------------*/
84
/** Helper function to test for a null object and throw an exception if
86
* @param obj the object we are testing.
88
protected void not_null(Object obj) throws internal_error
91
throw new internal_error("Null object used in set operation");
94
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
96
/** Determine if the set is empty. */
97
public boolean empty()
102
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
104
/** Determine if the set contains a particular terminal.
105
* @param sym the terminal symbol we are looking for.
107
public boolean contains(terminal sym)
108
throws internal_error
111
return _elements.get(sym.index());
114
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
116
/** Given its index determine if the set contains a particular terminal.
117
* @param indx the index of the terminal in question.
119
public boolean contains(int indx)
121
return _elements.get(indx);
124
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
126
/** Determine if this set is an (improper) subset of another.
127
* @param other the set we are testing against.
129
public boolean is_subset_of(terminal_set other)
130
throws internal_error
134
/* make a copy of the other set */
135
BitSet copy_other = (BitSet)other._elements.clone();
138
copy_other.or(_elements);
140
/* if it hasn't changed, we were a subset */
141
return copy_other.equals(other._elements);
144
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
146
/** Determine if this set is an (improper) superset of another.
147
* @param other the set we are testing against.
149
public boolean is_superset_of(terminal_set other)
150
throws internal_error
153
return other.is_subset_of(this);
156
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
158
/** Add a single terminal to the set.
159
* @param sym the terminal being added.
160
* @return true if this changes the set.
162
public boolean add(terminal sym)
163
throws internal_error
169
/* see if we already have this */
170
result = _elements.get(sym.index());
172
/* if not we add it */
174
_elements.set(sym.index());
179
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
181
/** Remove a terminal if it is in the set.
182
* @param sym the terminal being removed.
184
public void remove(terminal sym)
185
throws internal_error
188
_elements.clear(sym.index());
191
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
193
/** Add (union) in a complete set.
194
* @param other the set being added.
195
* @return true if this changes the set.
197
public boolean add(terminal_set other)
198
throws internal_error
203
BitSet copy = (BitSet)_elements.clone();
205
/* or in the other set */
206
_elements.or(other._elements);
208
/* changed if we are not the same as the copy */
209
return !_elements.equals(copy);
212
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
214
/** Determine if this set intersects another.
215
* @param other the other set in question.
217
public boolean intersects(terminal_set other)
218
throws internal_error
222
/* make a copy of the other set */
223
BitSet copy = (BitSet)other._elements.clone();
225
/* xor out our values */
226
copy.xor(this._elements);
228
/* see if its different */
229
return !copy.equals(other._elements);
232
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
234
/** Equality comparison. */
235
public boolean equals(terminal_set other)
240
return _elements.equals(other._elements);
243
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
245
/** Generic equality comparison. */
246
public boolean equals(Object other)
248
if (!(other instanceof terminal_set))
251
return equals((terminal_set)other);
254
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
256
/** Convert to string. */
257
public String toString()
264
for (int t = 0; t < terminal.number(); t++)
266
if (_elements.get(t))
273
result += terminal.find(t).name();
281
/*-----------------------------------------------------------*/