1 2 package com.sun.java_cup.internal; 3 4 import java.util.Hashtable ; 5 import java.util.Enumeration ; 6 7 14 public class symbol_set { 15 16 17 18 19 20 21 public symbol_set() { } 22 23 26 public symbol_set(symbol_set other) throws internal_error 27 { 28 not_null(other); 29 _all = (Hashtable )other._all.clone(); 30 } 31 32 33 34 35 36 38 protected Hashtable _all = new Hashtable (11); 39 40 41 public Enumeration all() {return _all.elements();} 42 43 44 public int size() {return _all.size();} 45 46 47 48 49 50 54 protected void not_null(Object obj) throws internal_error 55 { 56 if (obj == null) 57 throw new internal_error("Null object used in set operation"); 58 } 59 60 61 62 65 public boolean contains(symbol sym) {return _all.containsKey(sym.name());} 66 67 68 69 72 public boolean is_subset_of(symbol_set other) throws internal_error 73 { 74 not_null(other); 75 76 77 for (Enumeration e = all(); e.hasMoreElements(); ) 78 if (!other.contains((symbol)e.nextElement())) 79 return false; 80 81 82 return true; 83 } 84 85 86 87 90 public boolean is_superset_of(symbol_set other) throws internal_error 91 { 92 not_null(other); 93 return other.is_subset_of(this); 94 } 95 96 97 98 102 public boolean add(symbol sym) throws internal_error 103 { 104 Object previous; 105 106 not_null(sym); 107 108 109 previous = _all.put(sym.name(),sym); 110 111 112 return previous == null; 113 } 114 115 116 117 120 public void remove(symbol sym) throws internal_error 121 { 122 not_null(sym); 123 _all.remove(sym.name()); 124 } 125 126 127 128 132 public boolean add(symbol_set other) throws internal_error 133 { 134 boolean result = false; 135 136 not_null(other); 137 138 139 for (Enumeration e = other.all(); e.hasMoreElements(); ) 140 result = add((symbol)e.nextElement()) || result; 141 142 return result; 143 } 144 145 146 147 150 public void remove(symbol_set other) throws internal_error 151 { 152 not_null(other); 153 154 155 for (Enumeration e = other.all(); e.hasMoreElements(); ) 156 remove((symbol)e.nextElement()); 157 } 158 159 160 161 162 public boolean equals(symbol_set other) 163 { 164 if (other == null || other.size() != size()) return false; 165 166 167 try { 168 return is_subset_of(other); 169 } catch (internal_error e) { 170 171 e.crash(); 172 return false; 173 } 174 } 175 176 177 178 179 public boolean equals(Object other) 180 { 181 if (!(other instanceof symbol_set)) 182 return false; 183 else 184 return equals((symbol_set)other); 185 } 186 187 188 189 190 public int hashCode() 191 { 192 int result = 0; 193 int cnt; 194 Enumeration e; 195 196 197 for (e = all(), cnt=0 ; e.hasMoreElements() && cnt<5; cnt++) 198 result ^= ((symbol)e.nextElement()).hashCode(); 199 200 return result; 201 } 202 203 204 205 206 public String toString() 207 { 208 String result; 209 boolean comma_flag; 210 211 result = "{"; 212 comma_flag = false; 213 for (Enumeration e = all(); e.hasMoreElements(); ) 214 { 215 if (comma_flag) 216 result += ", "; 217 else 218 comma_flag = true; 219 220 result += ((symbol)e.nextElement()).name(); 221 } 222 result += "}"; 223 224 return result; 225 } 226 227 228 229 } 230 231 232 | Popular Tags |