FileDocCategorySizeDatePackage
SymbolHash.javaAPI DocApache Xerces 3.0.15739Fri Sep 14 20:33:52 BST 2007org.apache.xerces.util

SymbolHash

public class SymbolHash extends Object
This class is an unsynchronized hash table primary used for String to Object mapping.

The hash code uses the same algorithm as SymbolTable class.

author
Elena Litani
version
$Id: SymbolHash.java 447241 2006-09-18 05:12:57Z mrglavas $

Fields Summary
protected int
fTableSize
Default table size.
protected Entry[]
fBuckets
Buckets.
protected int
fNum
Number of elements.
Constructors Summary
public SymbolHash()
Constructs a key table with the default size.


    //
    // Constructors
    //

             
      
        fBuckets = new Entry[fTableSize];
    
public SymbolHash(int size)
Constructs a key table with a given size.

param
size the size of the key table.

        fTableSize = size;
        fBuckets = new Entry[fTableSize];
    
Methods Summary
public voidclear()
Remove all key/value assocaition. This tries to save a bit of GC'ing by at least keeping the fBuckets array around.

        for (int i=0; i<fTableSize; i++) {
            fBuckets[i] = null;
        }
        fNum = 0;
    
public java.lang.Objectget(java.lang.Object key)
Get the value associated with the given key.

param
key
return
the value associated with the given key.

        int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize;
        Entry entry = search(key, bucket);
        if (entry != null) {
            return entry.value;
        }
        return null;
    
public intgetLength()
Get the number of key/value pairs stored in this table.

return
the number of key/value pairs stored in this table.

        return fNum;
    
public intgetValues(java.lang.Object[] elements, int from)
Add all values to the given array. The array must have enough entry.

param
elements the array to store the elements
param
from where to start store element in the array
return
number of elements copied to the array

        for (int i=0, j=0; i<fTableSize && j<fNum; i++) {
            for (Entry entry = fBuckets[i]; entry != null; entry = entry.next) {
                elements[from+j] = entry.value;
                j++;
            }
        }
        return fNum;
    
public org.apache.xerces.util.SymbolHashmakeClone()
Make a clone of this object.

        SymbolHash newTable = new SymbolHash(fTableSize);
        newTable.fNum = fNum;
        for (int i = 0; i < fTableSize; i++) {
            if (fBuckets[i] != null)
                newTable.fBuckets[i] = fBuckets[i].makeClone();
        }
        return newTable;
    
public voidput(java.lang.Object key, java.lang.Object value)
Adds the key/value mapping to the key table. If the key already exists, the previous value associated with this key is overwritten by the new value.

param
key
param
value

        int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize;
        Entry entry = search(key, bucket);

        // replace old value
        if (entry != null) {
            entry.value = value;
        }
        // create new entry
        else {
            entry = new Entry(key, value, fBuckets[bucket]);
            fBuckets[bucket] = entry;
            fNum++;
        }
    
protected org.apache.xerces.util.SymbolHash$Entrysearch(java.lang.Object key, int bucket)

        // search for identical key
        for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
            if (key.equals(entry.key))
                return entry;
        }
        return null;