prefuse.util.collections
Class IntObjectHashMap

java.lang.Object
  extended by prefuse.util.collections.AbstractHashMap
      extended by prefuse.util.collections.IntObjectHashMap
All Implemented Interfaces:
java.lang.Cloneable

public class IntObjectHashMap
extends AbstractHashMap
implements java.lang.Cloneable

Hash map holding (key,value) associations of type (int-->Object); Automatically grows and shrinks as needed; Implemented using open addressing with double hashing. First see the package summary and javadoc tree view to get the broad picture. This class has been adapted from the corresponding class in the COLT library for scientfic computing.

Version:
1.0, 09/24/99
Author:
wolfgang.hoschek@cern.ch
See Also:
HashMap

Field Summary
protected static int defaultCapacity
           
protected static double defaultMaxLoadFactor
           
protected static double defaultMinLoadFactor
           
protected static byte FREE
           
protected  int freeEntries
          The number of table entries in state==FREE.
protected static byte FULL
           
protected static byte REMOVED
           
protected  byte[] state
          The state of each hash table entry (FREE, FULL, REMOVED).
protected  int[] table
          The hash table keys.
protected  java.lang.Object[] values
          The hash table values.
 
Fields inherited from class prefuse.util.collections.AbstractHashMap
distinct, highWaterMark, lowWaterMark, maxLoadFactor, minLoadFactor
 
Constructor Summary
IntObjectHashMap()
          Constructs an empty map with default capacity and default load factors.
IntObjectHashMap(int initialCapacity)
          Constructs an empty map with the specified initial capacity and default load factors.
IntObjectHashMap(int initialCapacity, double minLoadFactor, double maxLoadFactor)
          Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor.
 
Method Summary
 void clear()
          Removes all (key,value) associations from the receiver.
 java.lang.Object clone()
          Returns a deep copy of the receiver.
 boolean containsKey(int key)
          Returns true if the receiver contains the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if the receiver contains the specified value.
 void ensureCapacity(int minCapacity)
          Ensures that the receiver can hold at least the specified number of associations without needing to allocate new internal memory.
 java.lang.Object get(int key)
          Returns the value associated with the specified key.
protected  int indexOfInsertion(int key)
           
protected  int indexOfKey(int key)
           
protected  int indexOfValue(java.lang.Object value)
           
 int keyOf(java.lang.Object value)
          Returns the first key the given value is associated with.
 int keys(int[] list)
          Fills all keys contained in the receiver into the specified list.
 boolean put(int key, java.lang.Object value)
          Associates the given key with the given value.
protected  void rehash(int newCapacity)
          Rehashes the contents of the receiver into a new table with a smaller or larger capacity.
 boolean removeKey(int key)
          Removes the given key with its associated element from the receiver, if present.
protected  void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor)
          Initializes the receiver.
 void trimToSize()
          Trims the capacity of the receiver to be the receiver's current size.
 void values(java.util.ArrayList list)
          Fills all values contained in the receiver into the specified list.
 
Methods inherited from class prefuse.util.collections.AbstractHashMap
chooseGrowCapacity, chooseHighWaterMark, chooseLowWaterMark, chooseMeanCapacity, chooseShrinkCapacity, isEmpty, nextPrime, size
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultCapacity

protected static final int defaultCapacity
See Also:
Constant Field Values

defaultMinLoadFactor

protected static final double defaultMinLoadFactor
See Also:
Constant Field Values

defaultMaxLoadFactor

protected static final double defaultMaxLoadFactor
See Also:
Constant Field Values

FREE

protected static final byte FREE
See Also:
Constant Field Values

FULL

protected static final byte FULL
See Also:
Constant Field Values

REMOVED

protected static final byte REMOVED
See Also:
Constant Field Values

table

protected int[] table
The hash table keys.


values

protected java.lang.Object[] values
The hash table values.


state

protected byte[] state
The state of each hash table entry (FREE, FULL, REMOVED).


freeEntries

protected int freeEntries
The number of table entries in state==FREE.

Constructor Detail

IntObjectHashMap

public IntObjectHashMap()
Constructs an empty map with default capacity and default load factors.


IntObjectHashMap

public IntObjectHashMap(int initialCapacity)
Constructs an empty map with the specified initial capacity and default load factors.

Parameters:
initialCapacity - the initial capacity of the map.
Throws:
java.lang.IllegalArgumentException - if the initial capacity is less than zero.

IntObjectHashMap

public IntObjectHashMap(int initialCapacity,
                        double minLoadFactor,
                        double maxLoadFactor)
Constructs an empty map with the specified initial capacity and the specified minimum and maximum load factor.

Parameters:
initialCapacity - the initial capacity.
minLoadFactor - the minimum load factor.
maxLoadFactor - the maximum load factor.
Throws:
java.lang.IllegalArgumentException - if initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= maxLoadFactor).
Method Detail

clear

public void clear()
Removes all (key,value) associations from the receiver. Implicitly calls trimToSize().

Specified by:
clear in class AbstractHashMap

clone

public java.lang.Object clone()
Returns a deep copy of the receiver.

Overrides:
clone in class java.lang.Object
Returns:
a deep copy of the receiver.

containsKey

public boolean containsKey(int key)
Returns true if the receiver contains the specified key.

Returns:
true if the receiver contains the specified key.

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if the receiver contains the specified value.

Returns:
true if the receiver contains the specified value.

ensureCapacity

public void ensureCapacity(int minCapacity)
Ensures that the receiver can hold at least the specified number of associations without needing to allocate new internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver.

This method never need be called; it is for performance tuning only. Calling this method before put()ing a large number of associations boosts performance, because the receiver will grow only once instead of potentially many times and hash collisions get less probable.

Overrides:
ensureCapacity in class AbstractHashMap
Parameters:
minCapacity - the desired minimum capacity.

get

public java.lang.Object get(int key)
Returns the value associated with the specified key. It is often a good idea to first check with containsKey(int) whether the given key has a value associated or not, i.e. whether there exists an association for the given key or not.

Parameters:
key - the key to be searched for.
Returns:
the value associated with the specified key; null if no such key is present.

indexOfInsertion

protected int indexOfInsertion(int key)
Parameters:
key - the key to be added to the receiver.
Returns:
the index where the key would need to be inserted, if it is not already contained. Returns -index-1 if the key is already contained at slot index. Therefore, if the returned index < 0, then it is already contained at slot -index-1. If the returned index >= 0, then it is NOT already contained and should be inserted at slot index.

indexOfKey

protected int indexOfKey(int key)
Parameters:
key - the key to be searched in the receiver.
Returns:
the index where the key is contained in the receiver, returns -1 if the key was not found.

indexOfValue

protected int indexOfValue(java.lang.Object value)
Parameters:
value - the value to be searched in the receiver.
Returns:
the index where the value is contained in the receiver, returns -1 if the value was not found.

keyOf

public int keyOf(java.lang.Object value)
Returns the first key the given value is associated with. It is often a good idea to first check with containsValue(Object) whether there exists an association from a key to this value.

Parameters:
value - the value to search for.
Returns:
the first key for which holds get(key) == value; returns Integer.MIN_VALUE if no such key exists.

keys

public int keys(int[] list)
Fills all keys contained in the receiver into the specified list. Fills the list, starting at index 0. After this call returns the specified list has a new size that equals this.size().

This method can be used to iterate over the keys of the receiver.

Parameters:
list - the list to be filled

put

public boolean put(int key,
                   java.lang.Object value)
Associates the given key with the given value. Replaces any old (key,someOtherValue) association, if existing.

Parameters:
key - the key the value shall be associated with.
value - the value to be associated.
Returns:
true if the receiver did not already contain such a key; false if the receiver did already contain such a key - the new value has now replaced the formerly associated value.

rehash

protected void rehash(int newCapacity)
Rehashes the contents of the receiver into a new table with a smaller or larger capacity. This method is called automatically when the number of keys in the receiver exceeds the high water mark or falls below the low water mark.


removeKey

public boolean removeKey(int key)
Removes the given key with its associated element from the receiver, if present.

Parameters:
key - the key to be removed from the receiver.
Returns:
true if the receiver contained the specified key, false otherwise.

setUp

protected void setUp(int initialCapacity,
                     double minLoadFactor,
                     double maxLoadFactor)
Initializes the receiver.

Overrides:
setUp in class AbstractHashMap
Parameters:
initialCapacity - the initial capacity of the receiver.
minLoadFactor - the minLoadFactor of the receiver.
maxLoadFactor - the maxLoadFactor of the receiver.
Throws:
java.lang.IllegalArgumentException - if initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= maxLoadFactor).

trimToSize

public void trimToSize()
Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An application can use this operation to minimize the storage of the receiver.

Overrides:
trimToSize in class AbstractHashMap

values

public void values(java.util.ArrayList list)
Fills all values contained in the receiver into the specified list. Fills the list, starting at index 0. After this call returns the specified list has a new size that equals this.size().

This method can be used to iterate over the values of the receiver.

Parameters:
list - the list to be filled, can have any size.


Copyright © 2007 Regents of the University of California