org.apache.mahout.math.map
Class OpenHashMap<K,V>

java.lang.Object
  extended by org.apache.mahout.math.PersistentObject
      extended by org.apache.mahout.math.set.AbstractSet
          extended by org.apache.mahout.math.map.OpenHashMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

public class OpenHashMap<K,V>
extends AbstractSet
implements Map<K,V>

Open hash map. This implements Map, but it does not respect several aspects of the Map contract that impose the very sorts of performance penalities that this class exists to avoid. entrySet(), values, and keySet() do not return collections that share storage with the main map, and changes to those returned objects are not reflected in the container.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected static byte FREE
           
protected  int freeEntries
          The number of table entries in state==FREE.
protected static byte FULL
           
protected static Object NO_KEY_VALUE
           
protected static byte REMOVED
           
protected  byte[] state
          The state of each hash table entry (FREE, FULL, REMOVED).
protected  Object[] table
          The hash table keys.
protected  Object[] values
          The hash table values.
 
Fields inherited from class org.apache.mahout.math.set.AbstractSet
DEFAULT_CAPACITY, DEFAULT_MAX_LOAD_FACTOR, DEFAULT_MIN_LOAD_FACTOR, distinct, highWaterMark, lowWaterMark, maxLoadFactor, minLoadFactor
 
Constructor Summary
OpenHashMap()
          Constructs an empty map with default capacity and default load factors.
OpenHashMap(int initialCapacity)
          Constructs an empty map with the specified initial capacity and default load factors.
OpenHashMap(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.
 Object clone()
          Returns a deep copy of the receiver.
 boolean containsKey(Object key)
          Returns true if the receiver contains the specified key.
 boolean containsValue(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.
 Set<Map.Entry<K,V>> entrySet()
          Allocate a set to contain Map.Entry objects for the pairs and return it.
 boolean equals(Object obj)
           
 boolean forEachKey(ObjectProcedure<K> procedure)
          Applies a procedure to each key of the receiver, if any.
 boolean forEachPair(ObjectObjectProcedure<K,V> procedure)
          Applies a procedure to each (key,value) pair of the receiver, if any.
 V get(Object key)
          Returns the value associated with the specified key.
protected  int indexOfInsertion(K key)
           
protected  int indexOfKey(K key)
           
protected  int indexOfValue(V value)
           
 void keys(List<K> list)
          Fills all keys contained in the receiver into the specified list.
 Set<K> keySet()
          Allocate a set to contain keys and return it.
 V put(K key, V value)
          Associates the given key with the given value.
 void putAll(Map<? extends K,? extends V> m)
           
protected  void rehash(int newCapacity)
          Rehashes the contents of the receiver into a new table with a smaller or larger capacity.
 V remove(Object 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.
 String toString()
           
 void trimToSize()
          Trims the capacity of the receiver to be the receiver's current size.
 Collection<V> values()
          Allocate a list to contain the values and return it.
 
Methods inherited from class org.apache.mahout.math.set.AbstractSet
chooseGrowCapacity, chooseHighWaterMark, chooseLowWaterMark, chooseMeanCapacity, chooseShrinkCapacity, equalsMindTheNull, isEmpty, nextPrime, size
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
hashCode, isEmpty, size
 

Field Detail

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

NO_KEY_VALUE

protected static final Object NO_KEY_VALUE

table

protected Object[] table
The hash table keys.


values

protected 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

OpenHashMap

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


OpenHashMap

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

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

OpenHashMap

public OpenHashMap(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:
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 interface Map<K,V>
Specified by:
clear in class AbstractSet

clone

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

Overrides:
clone in class PersistentObject
Returns:
a deep copy of the receiver.

containsKey

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

Specified by:
containsKey in interface Map<K,V>
Returns:
true if the receiver contains the specified key.

containsValue

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

Specified by:
containsValue in interface Map<K,V>
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 AbstractSet
Parameters:
minCapacity - the desired minimum capacity.

forEachKey

public boolean forEachKey(ObjectProcedure<K> procedure)
Applies a procedure to each key of the receiver, if any. Note: Iterates over the keys in no particular order. Subclasses can define a particular order, for example, "sorted by key". All methods which can be expressed in terms of this method (most methods can) must guarantee to use the same order defined by this method, even if it is no particular order. This is necessary so that, for example, methods keys and values will yield association pairs, not two uncorrelated lists.

Parameters:
procedure - the procedure to be applied. Stops iteration if the procedure returns false, otherwise continues.
Returns:
false if the procedure stopped before all keys where iterated over, true otherwise.

forEachPair

public boolean forEachPair(ObjectObjectProcedure<K,V> procedure)
Applies a procedure to each (key,value) pair of the receiver, if any. Iteration order is guaranteed to be identical to the order used by method forEachKey(ObjectProcedure).

Parameters:
procedure - the procedure to be applied. Stops iteration if the procedure returns false, otherwise continues.
Returns:
false if the procedure stopped before all keys where iterated over, true otherwise.

get

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

Specified by:
get in interface Map<K,V>
Parameters:
key - the key to be searched for.
Returns:
the value associated with the specified key; 0 if no such key is present.

indexOfInsertion

protected int indexOfInsertion(K 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(K 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(V 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.

keys

public void keys(List<K> 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, can have any size.

put

public V put(K key,
             V value)
Associates the given key with the given value. Replaces any old (key,someOtherValue) association, if existing.

Specified by:
put in interface Map<K,V>
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.


remove

public V remove(Object key)
Removes the given key with its associated element from the receiver, if present.

Specified by:
remove in interface Map<K,V>
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 AbstractSet
Parameters:
initialCapacity - the initial capacity of the receiver.
minLoadFactor - the minLoadFactor of the receiver.
maxLoadFactor - the maxLoadFactor of the receiver.
Throws:
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 AbstractSet

entrySet

public Set<Map.Entry<K,V>> entrySet()
Allocate a set to contain Map.Entry objects for the pairs and return it.

Specified by:
entrySet in interface Map<K,V>

keySet

public Set<K> keySet()
Allocate a set to contain keys and return it. This violates the 'backing' provisions of the map interface.

Specified by:
keySet in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> m)
Specified by:
putAll in interface Map<K,V>

values

public Collection<V> values()
Allocate a list to contain the values and return it. This violates the 'backing' provision of the Map interface.

Specified by:
values in interface Map<K,V>

equals

public boolean equals(Object obj)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.