SymbolTablepublic class SymbolTable extends Object This class is a symbol table implementation that guarantees that
strings used as identifiers are unique references. Multiple calls
to addSymbol will always return the same string
reference.
The symbol table performs the same task as String.intern()
with the following differences:
-
A new string object does not need to be created in order to
retrieve a unique reference. Symbols can be added by using
a series of characters in a character array.
-
Users of the symbol table can provide their own symbol hashing
implementation. For example, a simple string hashing algorithm
may fail to produce a balanced set of hashcodes for symbols
that are mostly unique. Strings with similar leading
characters are especially prone to this poor hashing behavior.
|
Fields Summary |
---|
protected static final int | TABLE_SIZEDefault table size. | protected Entry[] | fBucketsBuckets. | protected int | fTableSize |
Constructors Summary |
---|
public SymbolTable()Constructs a symbol table with a default number of buckets.
//
// Constructors
//
this(TABLE_SIZE);
| public SymbolTable(int tableSize)Constructs a symbol table with a specified number of buckets.
fTableSize = tableSize;
fBuckets = new Entry[fTableSize];
|
Methods Summary |
---|
public java.lang.String | addSymbol(java.lang.String symbol)Adds the specified symbol to the symbol table and returns a
reference to the unique symbol. If the symbol already exists,
the previous symbol reference is returned instead, in order
guarantee that symbol references remain unique.
// search for identical symbol
final int hash = hash(symbol);
final int bucket = hash % fTableSize;
final int length = symbol.length();
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length && hash == entry.hashCode) {
if(symbol.regionMatches(0,entry.symbol,0,length)){
return entry.symbol;
}
else{
continue OUTER;
}
/**
for (int i = 0; i < length; i++) {
if (symbol.charAt(i) != entry.characters[i]) {
continue OUTER;
}
}
symbolAsArray = entry.characters;
return entry.symbol;
*/
}
}
// create new entry
Entry entry = new Entry(symbol, fBuckets[bucket]);
entry.hashCode = hash;
fBuckets[bucket] = entry;
return entry.symbol;
| public java.lang.String | addSymbol(char[] buffer, int offset, int length)Adds the specified symbol to the symbol table and returns a
reference to the unique symbol. If the symbol already exists,
the previous symbol reference is returned instead, in order
guarantee that symbol references remain unique.
// search for identical symbol
int hash = hash(buffer, offset, length);
int bucket = hash % fTableSize;
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length && hash ==entry.hashCode) {
for (int i = 0; i < length; i++) {
if (buffer[offset + i] != entry.characters[i]) {
continue OUTER;
}
}
return entry.symbol;
}
}
// add new entry
Entry entry = new Entry(buffer, offset, length, fBuckets[bucket]);
fBuckets[bucket] = entry;
entry.hashCode = hash;
return entry.symbol;
| public boolean | containsSymbol(java.lang.String symbol)Returns true if the symbol table already contains the specified
symbol.
// search for identical symbol
int hash = hash(symbol);
int bucket = hash % fTableSize;
int length = symbol.length();
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length && hash == entry.hashCode) {
if(symbol.regionMatches(0,entry.symbol,0,length)){
return true;
}
else {
continue OUTER;
}
/**
for (int i = 0; i < length; i++) {
if (symbol.charAt(i) != entry.characters[i]) {
continue OUTER;
}
}
return true;
*/
}
}
return false;
| public boolean | containsSymbol(char[] buffer, int offset, int length)Returns true if the symbol table already contains the specified
symbol.
// search for identical symbol
int hash = hash(buffer, offset, length) ;
int bucket = hash % fTableSize;
OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
if (length == entry.characters.length && hash == entry.hashCode) {
for (int i = 0; i < length; i++) {
if (buffer[offset + i] != entry.characters[i]) {
continue OUTER;
}
}
return true;
}
}
return false;
| public int | hash(java.lang.String symbol)Returns a hashcode value for the specified symbol. The value
returned by this method must be identical to the value returned
by the hash(char[],int,int) method when called
with the character array that comprises the symbol string.
int code = 0;
int length = symbol.length();
for (int i = 0; i < length; i++) {
code = code * 37 + symbol.charAt(i);
}
return code & 0x7FFFFFF;
| public int | hash(char[] buffer, int offset, int length)Returns a hashcode value for the specified symbol information.
The value returned by this method must be identical to the value
returned by the hash(String) method when called
with the string object created from the symbol information.
int code = 0;
for (int i = 0; i < length; i++) {
code = code * 37 + buffer[offset + i];
}
return code & 0x7FFFFFF;
|
|