FileDocCategorySizeDatePackage
KVMHashtable.javaAPI DocJ2ME CLDC 1.14746Wed Feb 05 15:56:02 GMT 2003runtime

KVMHashtable

public abstract class KVMHashtable extends Object

Fields Summary
Class
type
private int
size
boolean
closed
private Object[]
firstEntry
private Hashtable
nextEntry
private Hashtable
seen
private Hashtable
keys
Constructors Summary
KVMHashtable(int size, Class type)

 
        this.size = size;
        firstEntry = new Object[size];
        this.type = type;
        this.closed = false;
    
Methods Summary
public voidaddEntry(java.lang.Object x)

 
        if (closed) {
            throw new Error("Hashtable is closed");
        } 
        // Don't do anything if we've already seen this guy.
        if (seen.get(x) != null) { 
            return;
        }
        checkType(x);
        seen.put(x, x);
        int bucket = (int)(hash(x) % size);
        if (bucket < 0) { 
            System.out.println("Bad bucket?? " + this);
        }
        // Add this guy to the beginning of his bucket.
        Object oldNext = firstEntry[bucket];
        firstEntry[bucket] = x;
        if (oldNext != null) { 
            nextEntry.put(x, oldNext);
        }
        addNewEntryCallback(bucket, x);
    
public voidaddNewEntryCallback(int bucket, java.lang.Object neww)

public voidchainPrefix(CCodeWriter c)

public voidchainSuffix(CCodeWriter c)

private voidcheckType(java.lang.Object x)

        if (!type.isAssignableFrom(x.getClass())) {
            throw new Error(x.getClass() + " is not assignable to " + type);
        }
    
public java.util.Enumerationenumerate()


        
        return seen.keys();
    
protected java.lang.ObjectgetFirst(int bucket)

 
        return firstEntry[bucket];
    
protected intgetKey(java.lang.Object x)

 
        Integer Key = (Integer)keys.get(x);
        if (Key == null) { 
            addEntry(x);
            Key = (Integer)keys.get(x);
        } 
        return Key.intValue();
    
protected java.lang.ObjectgetNext(java.lang.Object x)

 
        checkType(x);
        return nextEntry.get(x);
    
protected java.lang.ObjectgetObject(int key)

 
        return keys.get(new Integer(key));
    
intgetSize()

 
        return size;
    
abstract longhash(java.lang.Object x)

protected voidsetKey(java.lang.Object x, int key)

 
        checkType(x);
        Integer Key = new Integer(key);
        Object oldKey = keys.put(x, Key);
        Object oldVal = keys.put(Key, x);
        if (oldKey != null) { 
            System.out.println("Changing key(" + x + ") from " + oldKey + " to "
                   + Key);
        }
        if (oldVal != null) { 
            System.out.println("Both " + x + " and " + oldVal 
                   + " have the same key value " + Key);
        }
    
public static longstringHash(java.lang.String string)

 
        int raw_hash = 0;
        for (int i = 0; i < string.length(); i++) { 
            raw_hash = raw_hash * 37 + string.charAt(i);
        }
        long result = (((long)raw_hash) << 32) >>> 32;
        return result;
    
abstract java.lang.ObjecttableChain(CCodeWriter c, int bucket, java.lang.Object[] chain)

abstract voidtableEntry(CCodeWriter c, int bucket, java.lang.Object token)

voidwriteTable(CCodeWriter out, java.lang.String tableName)

 
        closed = true;
        chainPrefix(out);
        Vector list = new Vector();
        Object tokens[] = new Object[size];
        for (int i = 0; i < size; i++) { 
            list.setSize(0);
            for (Object item = firstEntry[i]; item != null; 
                item = nextEntry.get(item)) { 
                list.addElement(item);
            }
            Object[] listArray = new Object[list.size()];
            list.copyInto(listArray);
            tokens[i] = tableChain(out, i, listArray);
        }
        chainSuffix(out);

        out.println("static HASHTABLE_X(" + size + ") " + tableName + "Data = {");
        out.println("\tHASHTABLE_HEADER(" + size + ", " + seen.size() + "),");
        out.println("\t{");
        for (int i = 0; i < size; i++) { 
            if (tokens[i] == null) { 
                out.print("\t\tNULL,\n");
            } else { 
                out.print("\t\t((void *)");
                tableEntry(out, i, tokens[i]);
                out.print("),\n");
            }
        }
        out.println("\t}");
        out.println("};\n");    
        out.println("#if !RELOCATABLE_ROM");
        out.println("HASHTABLE " + tableName + " = (HASHTABLE)&" + tableName + "Data;\n");
        out.println("#endif");