FileDocCategorySizeDatePackage
TwoKeyHashMap.javaAPI DocAndroid 1.5 API15152Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.util

TwoKeyHashMap

public class TwoKeyHashMap extends AbstractMap
Reductive hash with two keys

Fields Summary
static final float
DEFAULT_LOAD_FACTOR
static final int
DEFAULT_INITIAL_SIZE
private Set
entrySet
private Collection
values
private int
size
private int
arrSize
private int
modCount
private Entry[]
arr
private float
loadFactor
int
threshold
Constructors Summary
public TwoKeyHashMap()
Constructs an empty HashMap


             
      
        this(DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR);
    
public TwoKeyHashMap(int initialCapacity)
Constructs an empty HashMap

param
initialCapacity

        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    
public TwoKeyHashMap(int initialCapacity, float initialLoadFactor)
Constructs an empty HashMap

param
initialCapacity
param
initialLoadFactor

         if (initialCapacity < 0) {
            throw new IllegalArgumentException("initialCapacity should be >= 0");
        }
        if (initialLoadFactor <= 0) {
            throw new IllegalArgumentException("initialLoadFactor should be > 0");
        }
        loadFactor = initialLoadFactor;
        if (initialCapacity == Integer.MAX_VALUE) {
            initialCapacity--;
        }
        arrSize = initialCapacity > 0 ? initialCapacity : 1;
        threshold = (int) (arrSize * loadFactor);
        arr = new Entry[arrSize + 1];
    
Methods Summary
public voidclear()
Clears the map

        modCount++;
        size = 0;
        Arrays.fill(arr, 0, arr.length, null);
    
public booleancontainsKey(java.lang.Object key1, java.lang.Object key2)
Returns true if this map contains a mapping for the specified keys

param
key1
param
key2
return

        return findEntry(key1, key2) != null;
    
org.apache.harmony.luni.util.TwoKeyHashMap$EntrycreateEntry(int hashCode, E key1, K key2, V value, org.apache.harmony.luni.util.TwoKeyHashMap$Entry next)
Creates new entry

param
hashCode
param
key1
param
key2
param
value
param
next
return

        return new Entry<E, K, V>(hashCode, key1, key2, value, next);
    
java.util.IteratorcreateEntrySetIterator()
Creates entries iterator

return

        return new EntryIteratorImpl();
    
java.util.IteratorcreateValueCollectionIterator()
Creates values iterator

return

        return new ValueIteratorImpl();
    
public java.util.SetentrySet()
Returns a collection view of the mappings

        if (entrySet == null) {
            entrySet = new EntrySetImpl();
        }
        return entrySet;
    
private final org.apache.harmony.luni.util.TwoKeyHashMap$EntryfindEntry(java.lang.Object key1, java.lang.Object key2)

        if (key1 == null && key2 == null) {
            return arr[arrSize];
        }

        int hash = key1.hashCode() + key2.hashCode();
        int index = (hash & 0x7fffffff) % arrSize;
        Entry<E, K, V> e = arr[index];

        while (e != null) {
            if (hash == e.hash && key1.equals(e.getKey1()) && key2.equals(e.getKey2())) {
                return e;
            }
            e = e.next;
        }
        return null;
    
public Vget(java.lang.Object key1, java.lang.Object key2)
Return the value by keys

param
key1
param
key2
return

        Entry<E, K, V> e = findEntry(key1, key2);
        if (e != null) {
            return e.value;
        }
        return null;
    
public booleanisEmpty()
Returns true if this map contains no key-value mappings

        return size == 0;
    
public Vput(E key1, K key2, V value)
Associates the specified value with the specified keys in this map

param
key1
param
key2
param
value
return

        if (key1 == null && key2 == null) {
            int index = arrSize;
            if (arr[index] == null) {
                arr[index] = createEntry(0, null, null, value, null);
                size++;
                modCount++;
                return null;
            } else {
                V oldValue = arr[index].value;
                arr[index].value = value;
                return oldValue;
            }
        }

        int hash = key1.hashCode() + key2.hashCode();
        int index = (hash & 0x7fffffff) % arrSize;
        Entry<E, K, V> e = arr[index];

        while (e != null) {
            if (hash == e.hash && key1.equals(e.getKey1()) && key2.equals(e.getKey2())) {
                V oldValue = e.value;
                e.value = value;
                return oldValue;
            }
            e = e.next;
        }

        arr[index] = createEntry(hash, key1, key2, value, arr[index]);
        size++;
        modCount++;

        if (size > threshold) {
            rehash();
        }
        return null;
    
voidrehash()
Rehash the map

        int newArrSize = (arrSize + 1) * 2 + 1;
        if (newArrSize < 0) {
            newArrSize = Integer.MAX_VALUE - 1;
        }
        Entry<E, K, V>[] newArr = new Entry[newArrSize + 1];

        for (int i = 0; i < arr.length - 1; i++) {
            Entry<E, K, V> entry = arr[i];
            while (entry != null) {
                Entry<E, K, V> next = entry.next;

                int newIndex = (entry.hash & 0x7fffffff) % newArrSize;
                entry.next = newArr[newIndex];
                newArr[newIndex] = entry;

                entry = next;
            }
        }
        newArr[newArrSize] = arr[arrSize]; // move null entry
        arrSize = newArrSize;

        // The maximum array size is reached, increased loadFactor
        // will keep array from further growing
        if (arrSize == Integer.MAX_VALUE) {
            loadFactor *= 10;
        }
        threshold = (int) (arrSize * loadFactor);
        arr = newArr;
    
public Vremove(java.lang.Object key1, java.lang.Object key2)
Removes the mapping for the keys

param
key1
param
key2
return

        Entry<E, K, V> e = removeEntry(key1, key2);
        return null != e ? e.value : null;
    
private final org.apache.harmony.luni.util.TwoKeyHashMap$EntryremoveEntry(java.lang.Object key1, java.lang.Object key2)

        if (key1 == null && key2 == null) {
            int index = arrSize;
            if (arr[index] != null) {
                Entry<E, K, V> ret = arr[index];
                arr[index] = null;
                size--;
                modCount++;
                return ret;
            }
            return null;
        }

        int hash = key1.hashCode() + key2.hashCode();
        int index = (hash & 0x7fffffff) % arrSize;

        Entry<E, K, V> e = arr[index];
        Entry<E, K, V> prev = e;
        while (e != null) {
            if (hash == e.hash && key1.equals(e.getKey1()) && key2.equals(e.getKey2())) {
                if (prev == e) {
                    arr[index] = e.next;
                } else {
                    prev.next = e.next;
                }
                size--;
                modCount++;
                return e;
            }

            prev = e;
            e = e.next;
        }
        return null;
    
public intsize()
Returns the number of mappings

        return size;
    
public java.util.Collectionvalues()
Returns a collection view of the values

        if (values == null) {
            values = new ValuesCollectionImpl();
        }
        return values;