1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 89 public class SymbolTable { 90 91 95 96 protected static final int TABLE_SIZE = 101; 97 98 102 103 protected Entry[] fBuckets = null; 104 105 protected int fTableSize; 107 108 112 113 public SymbolTable() { 114 this(TABLE_SIZE); 115 } 116 117 118 public SymbolTable(int tableSize) { 119 fTableSize = tableSize; 120 fBuckets = new Entry[fTableSize]; 121 } 122 123 127 135 public String addSymbol(String symbol) { 136 137 int bucket = hash(symbol) % fTableSize; 139 int length = symbol.length(); 140 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 141 if (length == entry.characters.length) { 142 for (int i = 0; i < length; i++) { 143 if (symbol.charAt(i) != entry.characters[i]) { 144 continue OUTER; 145 } 146 } 147 return entry.symbol; 148 } 149 } 150 151 Entry entry = new Entry(symbol, fBuckets[bucket]); 153 fBuckets[bucket] = entry; 154 return entry.symbol; 155 156 } 158 168 public String addSymbol(char[] buffer, int offset, int length) { 169 170 int bucket = hash(buffer, offset, length) % fTableSize; 172 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 173 if (length == entry.characters.length) { 174 for (int i = 0; i < length; i++) { 175 if (buffer[offset + i] != entry.characters[i]) { 176 continue OUTER; 177 } 178 } 179 return entry.symbol; 180 } 181 } 182 183 Entry entry = new Entry(buffer, offset, length, fBuckets[bucket]); 185 fBuckets[bucket] = entry; 186 return entry.symbol; 187 188 } 190 198 public int hash(String symbol) { 199 200 int code = 0; 201 int length = symbol.length(); 202 for (int i = 0; i < length; i++) { 203 code = code * 37 + symbol.charAt(i); 204 } 205 return code & 0x7FFFFFF; 206 207 } 209 220 public int hash(char[] buffer, int offset, int length) { 221 222 int code = 0; 223 for (int i = 0; i < length; i++) { 224 code = code * 37 + buffer[offset + i]; 225 } 226 return code & 0x7FFFFFF; 227 228 } 230 236 public boolean containsSymbol(String symbol) { 237 238 int bucket = hash(symbol) % fTableSize; 240 int length = symbol.length(); 241 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 242 if (length == entry.characters.length) { 243 for (int i = 0; i < length; i++) { 244 if (symbol.charAt(i) != entry.characters[i]) { 245 continue OUTER; 246 } 247 } 248 return true; 249 } 250 } 251 252 return false; 253 254 } 256 264 public boolean containsSymbol(char[] buffer, int offset, int length) { 265 266 int bucket = hash(buffer, offset, length) % fTableSize; 268 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 269 if (length == entry.characters.length) { 270 for (int i = 0; i < length; i++) { 271 if (buffer[offset + i] != entry.characters[i]) { 272 continue OUTER; 273 } 274 } 275 return true; 276 } 277 } 278 279 return false; 280 281 } 283 287 291 protected static final class Entry { 292 293 297 298 public String symbol; 299 300 304 public char[] characters; 305 306 307 public Entry next; 308 309 313 317 public Entry(String symbol, Entry next) { 318 this.symbol = symbol.intern(); 319 characters = new char[symbol.length()]; 320 symbol.getChars(0, characters.length, characters, 0); 321 this.next = next; 322 } 323 324 328 public Entry(char[] ch, int offset, int length, Entry next) { 329 characters = new char[length]; 330 System.arraycopy(ch, offset, characters, 0, length); 331 symbol = new String (characters).intern(); 332 this.next = next; 333 } 334 335 } 337 } | Popular Tags |