4 import java.util.Hashtable;
5 import java.util.Enumeration;
7 /** This class represents a set of symbols and provides a series of
8 * set operations to manipulate them.
10 * @see java_cup.symbol
11 * @version last updated: 11/25/95
12 * @author Scott Hudson
14 public class symbol_set {
16 /*-----------------------------------------------------------*/
17 /*--- Constructor(s) ----------------------------------------*/
18 /*-----------------------------------------------------------*/
20 /** Constructor for an empty set. */
21 public symbol_set() { }
23 /** Constructor for cloning from another set.
24 * @param other the set we are cloning from.
26 public symbol_set(symbol_set other) throws internal_error
29 _all = (Hashtable)other._all.clone();
32 /*-----------------------------------------------------------*/
33 /*--- (Access to) Instance Variables ------------------------*/
34 /*-----------------------------------------------------------*/
36 /** A hash table to hold the set. Symbols are keyed using their name string.
38 protected Hashtable _all = new Hashtable(11);
40 /** Access to all elements of the set. */
41 public Enumeration all() {return _all.elements();}
43 /** size of the set */
44 public int size() {return _all.size();}
46 /*-----------------------------------------------------------*/
47 /*--- (Access to) Instance Variables ------------------------*/
48 /*-----------------------------------------------------------*/
50 /** Helper function to test for a null object and throw an exception
52 * @param obj the object we are testing.
54 protected void not_null(Object obj) throws internal_error
57 throw new internal_error("Null object used in set operation");
60 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
62 /** Determine if the set contains a particular symbol.
63 * @param sym the symbol we are looking for.
65 public boolean contains(symbol sym) {return _all.containsKey(sym.name());}
67 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
69 /** Determine if this set is an (improper) subset of another.
70 * @param other the set we are testing against.
72 public boolean is_subset_of(symbol_set other) throws internal_error
76 /* walk down our set and make sure every element is in the other */
77 for (Enumeration e = all(); e.hasMoreElements(); )
78 if (!other.contains((symbol)e.nextElement()))
81 /* they were all there */
85 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
87 /** Determine if this set is an (improper) superset of another.
88 * @param other the set we are are testing against.
90 public boolean is_superset_of(symbol_set other) throws internal_error
93 return other.is_subset_of(this);
96 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
98 /** Add a single symbol to the set.
99 * @param sym the symbol we are adding.
100 * @return true if this changes the set.
102 public boolean add(symbol sym) throws internal_error
108 /* put the object in */
109 previous = _all.put(sym.name(),sym);
111 /* if we had a previous, this is no change */
112 return previous == null;
115 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
117 /** Remove a single symbol if it is in the set.
118 * @param sym the symbol we are removing.
120 public void remove(symbol sym) throws internal_error
123 _all.remove(sym.name());
126 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
128 /** Add (union) in a complete set.
129 * @param other the set we are adding in.
130 * @return true if this changes the set.
132 public boolean add(symbol_set other) throws internal_error
134 boolean result = false;
138 /* walk down the other set and do the adds individually */
139 for (Enumeration e = other.all(); e.hasMoreElements(); )
140 result = add((symbol)e.nextElement()) || result;
145 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
147 /** Remove (set subtract) a complete set.
148 * @param other the set we are removing.
150 public void remove(symbol_set other) throws internal_error
154 /* walk down the other set and do the removes individually */
155 for (Enumeration e = other.all(); e.hasMoreElements(); )
156 remove((symbol)e.nextElement());
159 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
161 /** Equality comparison. */
162 public boolean equals(symbol_set other)
164 if (other == null || other.size() != size()) return false;
166 /* once we know they are the same size, then improper subset does test */
168 return is_subset_of(other);
169 } catch (internal_error e) {
170 /* can't throw the error (because super class doesn't), so we crash */
176 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
178 /** Generic equality comparison. */
179 public boolean equals(Object other)
181 if (!(other instanceof symbol_set))
184 return equals((symbol_set)other);
187 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
189 /** Compute a hash code. */
190 public int hashCode()
196 /* hash together codes from at most first 5 elements */
197 for (e = all(), cnt=0 ; e.hasMoreElements() && cnt<5; cnt++)
198 result ^= ((symbol)e.nextElement()).hashCode();
203 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
205 /** Convert to a string. */
206 public String toString()
213 for (Enumeration e = all(); e.hasMoreElements(); )
220 result += ((symbol)e.nextElement()).name();
227 /*-----------------------------------------------------------*/