FileDocCategorySizeDatePackage
SymbolTable.javaAPI DocApache Tomcat 6.0.1410054Fri Jul 20 04:20:32 BST 2007org.apache.jasper.xmlparser

SymbolTable

public 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.
author
Andy Clark
version
$Id: SymbolTable.java 467222 2006-10-24 03:17:11Z markt $

Fields Summary
protected static final int
TABLE_SIZE
Default table size.
protected Entry[]
fBuckets
Buckets.
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.StringaddSymbol(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.

param
symbol The new symbol.


        // search for identical symbol
        int bucket = hash(symbol) % fTableSize;
        int length = symbol.length();
        OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
            if (length == entry.characters.length) {
                for (int i = 0; i < length; i++) {
                    if (symbol.charAt(i) != entry.characters[i]) {
                        continue OUTER;
                    }
                }
                return entry.symbol;
            }
        }

        // create new entry
        Entry entry = new Entry(symbol, fBuckets[bucket]);
        fBuckets[bucket] = entry;
        return entry.symbol;

    
public java.lang.StringaddSymbol(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.

param
buffer The buffer containing the new symbol.
param
offset The offset into the buffer of the new symbol.
param
length The length of the new symbol in the buffer.


        // search for identical symbol
        int bucket = hash(buffer, offset, length) % fTableSize;
        OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
            if (length == entry.characters.length) {
                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;
        return entry.symbol;

    
public booleancontainsSymbol(java.lang.String symbol)
Returns true if the symbol table already contains the specified symbol.

param
symbol The symbol to look for.


        // search for identical symbol
        int bucket = hash(symbol) % fTableSize;
        int length = symbol.length();
        OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
            if (length == entry.characters.length) {
                for (int i = 0; i < length; i++) {
                    if (symbol.charAt(i) != entry.characters[i]) {
                        continue OUTER;
                    }
                }
                return true;
            }
        }

        return false;

    
public booleancontainsSymbol(char[] buffer, int offset, int length)
Returns true if the symbol table already contains the specified symbol.

param
buffer The buffer containing the symbol to look for.
param
offset The offset into the buffer.
param
length The length of the symbol in the buffer.


        // search for identical symbol
        int bucket = hash(buffer, offset, length) % fTableSize;
        OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
            if (length == entry.characters.length) {
                for (int i = 0; i < length; i++) {
                    if (buffer[offset + i] != entry.characters[i]) {
                        continue OUTER;
                    }
                }
                return true;
            }
        }

        return false;

    
public inthash(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.

param
symbol The symbol to hash.


        int code = 0;
        int length = symbol.length();
        for (int i = 0; i < length; i++) {
            code = code * 37 + symbol.charAt(i);
        }
        return code & 0x7FFFFFF;

    
public inthash(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.

param
buffer The character buffer containing the symbol.
param
offset The offset into the character buffer of the start of the symbol.
param
length The length of the symbol.


        int code = 0;
        for (int i = 0; i < length; i++) {
            code = code * 37 + buffer[offset + i];
        }
        return code & 0x7FFFFFF;