Jive Forums API (5.5.20.2-oracle) Developer Javadocs

com.jivesoftware.util
Class LongHashMap

java.lang.Object
  extended by com.jivesoftware.util.LongHashMap

public final class LongHashMap
extends java.lang.Object

Hash map holding (key,value) associations of type (long-->Object); Automatically grows and shrinks as needed; Implemented using open addressing with double hashing.

Adapted from the Colt package by CoolServlets. Please visit the Colt homepage at: http://tilde-hoschek.home.cern.ch/~hoschek/colt/index.htm


Field Summary
protected static int DEFAULT_CAPACITY
           
protected static double DEFAULT_MAX_LOAD_FACTOR
           
protected static double DEFAULT_MIN_LOAD_FACTOR
           
protected  int distinct
           
protected static byte FREE
           
protected  int freeEntries
           
protected static byte FULL
           
protected  int highWaterMark
           
static int LARGEST_PRIME
          The largest prime this class can generate; currently equal to Integer.MAX_VALUE.
protected  int lowWaterMark
          The table capacity c=table.length always satisfies the invariant c * minLoadFactor <= s <= c * maxLoadFactor, where s=size() is the number of associations currently contained.
protected  double maxLoadFactor
           
protected  double minLoadFactor
           
protected static byte REMOVED
           
protected  byte[] state
           
protected  long[] table
           
protected  java.lang.Object[] values
           
 
Constructor Summary
LongHashMap()
          Constructs an empty map with default capacity and default load factors.
LongHashMap(int initialCapacity)
          Constructs an empty map with the specified initial capacity and default load factors.
LongHashMap(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
protected  int chooseLowWaterMark(int capacity, double minLoad)
          Returns new low water mark threshold based on current capacity and minLoadFactor.
protected  int chooseMeanCapacity(int size, double minLoad, double maxLoad)
          Chooses a new prime table capacity neither favoring shrinking nor growing, that (approximately) satisfies the invariant c * minLoadFactor <= size <= c * maxLoadFactor and has at least one FREE slot for the given size.
protected  int chooseShrinkCapacity(int size, double minLoad, double maxLoad)
          Chooses a new prime table capacity optimized for shrinking that (approximately) satisfies the invariant c * minLoadFactor <= size <= c * maxLoadFactor and has at least one FREE slot for the given size.
 void clear()
          Removes all (key,value) associations from the receiver.
 boolean containsKey(long 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(long key)
          Returns the value associated with the specified key.
protected  int indexOfValue(java.lang.Object value)
           
 boolean isEmpty()
          Returns true if the receiver contains no (key,value) associations.
 long keyOf(java.lang.Object value)
          Returns the first key the given value is associated with.
 long[] keys()
          Returns all the keys in the map.
protected  int nextPrime(int desiredCapacity)
          Returns a prime number which is >= desiredCapacity and very close to desiredCapacity (within 11% if desiredCapacity >= 1000).
 boolean put(long 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(long 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.
 int size()
          Returns the number of (key,value) associations currently contained.
 void trimToSize()
          Trims the capacity of the receiver to be the receiver's current size.
 java.lang.Object[] values()
          Returns an array of all the values in the Map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

table

protected long[] table

values

protected java.lang.Object[] values

state

protected byte[] state

freeEntries

protected int freeEntries

distinct

protected int distinct

lowWaterMark

protected int lowWaterMark
The table capacity c=table.length always satisfies the invariant c * minLoadFactor <= s <= c * maxLoadFactor, where s=size() is the number of associations currently contained. The term "c * minLoadFactor" is called the "lowWaterMark", "c * maxLoadFactor" is called the "highWaterMark". In other words, the table capacity (and proportionally the memory used by this class) oscillates within these constraints. The terms are precomputed and cached to avoid recalculating them each time put(..) or removeKey(...) is called.


highWaterMark

protected int highWaterMark

minLoadFactor

protected double minLoadFactor

maxLoadFactor

protected double maxLoadFactor

DEFAULT_CAPACITY

protected static final int DEFAULT_CAPACITY
See Also:
Constant Field Values

DEFAULT_MIN_LOAD_FACTOR

protected static final double DEFAULT_MIN_LOAD_FACTOR
See Also:
Constant Field Values

DEFAULT_MAX_LOAD_FACTOR

protected static final double DEFAULT_MAX_LOAD_FACTOR
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

LARGEST_PRIME

public static final int LARGEST_PRIME
The largest prime this class can generate; currently equal to Integer.MAX_VALUE.

See Also:
Constant Field Values
Constructor Detail

LongHashMap

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


LongHashMap

public LongHashMap(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.

LongHashMap

public LongHashMap(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().


containsKey

public boolean containsKey(long 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.

Parameters:
minCapacity - the desired minimum capacity.

get

public final java.lang.Object get(long key)
Returns the value associated with the specified key. It is often a good idea to first check with containsKey(long) 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.

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 long keyOf(java.lang.Object value)
Returns the first key the given value is associated with.

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

keys

public long[] keys()
Returns all the keys in the map.


put

public boolean put(long 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.

size

public int size()
Returns the number of (key,value) associations currently contained.

Returns:
the number of (key,value) associations currently contained.

isEmpty

public boolean isEmpty()
Returns true if the receiver contains no (key,value) associations.

Returns:
true if the receiver contains no (key,value) associations.

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(long 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.

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.


values

public java.lang.Object[] values()
Returns an array of all the values in the Map.


chooseLowWaterMark

protected int chooseLowWaterMark(int capacity,
                                 double minLoad)
Returns new low water mark threshold based on current capacity and minLoadFactor.

Returns:
int the new threshold.

chooseMeanCapacity

protected int chooseMeanCapacity(int size,
                                 double minLoad,
                                 double maxLoad)
Chooses a new prime table capacity neither favoring shrinking nor growing, that (approximately) satisfies the invariant c * minLoadFactor <= size <= c * maxLoadFactor and has at least one FREE slot for the given size.


chooseShrinkCapacity

protected int chooseShrinkCapacity(int size,
                                   double minLoad,
                                   double maxLoad)
Chooses a new prime table capacity optimized for shrinking that (approximately) satisfies the invariant c * minLoadFactor <= size <= c * maxLoadFactor and has at least one FREE slot for the given size.


nextPrime

protected int nextPrime(int desiredCapacity)
Returns a prime number which is >= desiredCapacity and very close to desiredCapacity (within 11% if desiredCapacity >= 1000).

Parameters:
desiredCapacity - the capacity desired by the user.
Returns:
the capacity which should be used for a hashtable.

Jive Forums Project Page

Copyright © 1999-2006 Jive Software.