FileDocCategorySizeDatePackage
SymbolHash.javaAPI DocJava SE 5 API7444Fri Aug 26 14:55:56 BST 2005com.sun.org.apache.xerces.internal.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,v 1.7 2003/05/08 20:12:00 elena Exp $

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 com.sun.org.apache.xerces.internal.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 com.sun.org.apache.xerces.internal.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;