FileDocCategorySizeDatePackage
ByteArrayHashMap.javaAPI DocAzureus 3.0.3.49656Fri Mar 03 10:26:50 GMT 2006org.gudy.azureus2.core3.util

ByteArrayHashMap

public class ByteArrayHashMap extends Object

Fields Summary
static final int
DEFAULT_INITIAL_CAPACITY
The default initial capacity - MUST be a power of two.
static final int
MAXIMUM_CAPACITY
The maximum capacity, used if a higher value is implicitly specified by either of the constructors with arguments. MUST be a power of two <= 1<<30.
static final float
DEFAULT_LOAD_FACTOR
private Entry[]
table
private int
size
private int
threshold
final float
loadFactor
Constructors Summary
public ByteArrayHashMap(int initialCapacity, float loadFactor)


  
         
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY)
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);

        // Find a power of 2 >= initialCapacity
        int capacity = 1;
        while (capacity < initialCapacity) 
            capacity <<= 1;
    
        this.loadFactor = loadFactor;
        threshold = (int)(capacity * loadFactor);
        table = new Entry[capacity];
    
public ByteArrayHashMap(int initialCapacity)

        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    
public ByteArrayHashMap()

        this.loadFactor = DEFAULT_LOAD_FACTOR;
        threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
        table = new Entry[DEFAULT_INITIAL_CAPACITY];
    
Methods Summary
voidaddEntry(int hash, byte[] key, java.lang.Object value, int bucketIndex)

        table[bucketIndex] = new Entry(hash, key, value, table[bucketIndex]);
        if (size++ >= threshold) 
            resize(2 * table.length);
    
public voidclear()

      
        Entry tab[] = table;
        for (int i = 0; i < tab.length; i++) 
            tab[i] = null;
        size = 0;
    
public booleancontainsKey(byte[] key)

        int hash = hash(key);
        int i = indexFor(hash, table.length);
        Entry e = table[i]; 
        while (true) {
            if (e == null)
                return( false );
            if (e.hash == hash && eq(key, e.key)) 
                return( true );
            e = e.next;
        }
    
voidcreateEntry(int hash, byte[] key, java.lang.Object value, int bucketIndex)

        table[bucketIndex] = new Entry(hash, key, value, table[bucketIndex]);
        size++;
    
public org.gudy.azureus2.core3.util.ByteArrayHashMapduplicate()
Bit inefficient at the moment

return

    	ByteArrayHashMap	res = new ByteArrayHashMap(size,loadFactor);
    	
        for (int j = 0; j < table.length; j++) {
	         Entry e = table[j];
	         while( e != null ){
              	res.put( e.key, e.value );
               	
               e = e.next;
	        }
	    }
       
       return( res );
    
private static final booleaneq(byte[] x, byte[] y)

        if ( x == y ){
        	return( true );
        }
        
        int	len = x.length;
        
        if ( len != y.length ){
        	return( false );
        }
        
        for (int i=0;i<len;i++){
        	if ( x[i] != y[i] ){
        		return( false );
        	}
        }
        
        return( true );
    
public java.lang.Objectget(byte[] key, int offset, int len)

    	byte[]	k = new byte[len];
    	System.arraycopy( key, offset, k, 0, len );
    	return( get( k ));
    
public java.lang.Objectget(byte[] key)

        
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        Entry e = table[i]; 
        while (true) {
            if (e == null)
                return e;
            if (e.hash == hash && eq(key, e.key)) 
                return e.value;
            e = e.next;
        }
    
private static final inthash(byte[] x)

	
    	int	hash = 0;
    	
    	int	len = x.length;
    	
        for (int i = 0; i < len; i++){
      
        	hash = 31*hash + x[i];
        }
        
        return( hash );
    
private static final intindexFor(int h, int length)

        return h & (length-1);
    
public booleanisEmpty()

        return size == 0;
    
public java.util.Listkeys()

    	List	res = new ArrayList();
    	
        for (int j = 0; j < table.length; j++) {
	         Entry e = table[j];
	         while( e != null ){
               	res.add( e.key );
                	
                 e = e.next;
	        }
	    }
        
        return( res );
    
public java.lang.Objectput(byte[] key, java.lang.Object value)

        int hash = hash(key);
        int i = indexFor(hash, table.length);

        for (Entry e = table[i]; e != null; e = e.next) {
            if (e.hash == hash && eq(key, e.key)) {
                Object oldValue = e.value;
                e.value = value;
              
                return oldValue;
            }
        }

        addEntry(hash, key, value, i);
        return null;
    
public java.lang.Objectremove(byte[] key)

        Entry e = removeEntryForKey(key);
        return (e == null ? e : e.value);
    
org.gudy.azureus2.core3.util.ByteArrayHashMap$EntryremoveEntryForKey(byte[] key)

        int hash = hash(key);
        int i = indexFor(hash, table.length);
        Entry prev = table[i];
        Entry e = prev;

        while (e != null) {
            Entry next = e.next;
            if (e.hash == hash && eq(key, e.key)) {
               
                size--;
                if (prev == e) 
                    table[i] = next;
                else
                    prev.next = next;
       
                return e;
            }
            prev = e;
            e = next;
        }
   
        return e;
    
voidresize(int newCapacity)

        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }

        Entry[] newTable = new Entry[newCapacity];
        transfer(newTable);
        table = newTable;
        threshold = (int)(newCapacity * loadFactor);
    
public intsize()

        return size;
    
voidtransfer(org.gudy.azureus2.core3.util.ByteArrayHashMap$Entry[] newTable)

        Entry[] src = table;
        int newCapacity = newTable.length;
        for (int j = 0; j < src.length; j++) {
            Entry e = src[j];
            if (e != null) {
                src[j] = null;
                do {
                    Entry next = e.next;
                    int i = indexFor(e.hash, newCapacity);  
                    e.next = newTable[i];
                    newTable[i] = e;
                    e = next;
                } while (e != null);
            }
        }
    
public java.util.Listvalues()

    	List	res = new ArrayList();
    	
        for (int j = 0; j < table.length; j++) {
	         Entry e = table[j];
	         while( e != null ){
               	res.add( e.value );
                	
                e = e.next;
	        }
	    }
        
        return( res );