activemq-cpp-3.6.0
decaf::util::HashMap< K, V, HASHCODE > Class Template Reference

Hash table based implementation of the Map interface. More...

#include <src/main/decaf/util/HashMap.h>

Inheritance diagram for decaf::util::HashMap< K, V, HASHCODE >:

Data Structures

class  AbstractMapIterator
class  ConstAbstractMapIterator
class  ConstEntryIterator
class  ConstHashMapEntrySet
class  ConstHashMapKeySet
class  ConstHashMapValueCollection
class  ConstKeyIterator
class  ConstValueIterator
class  EntryIterator
class  HashMapEntry
class  HashMapEntrySet
class  HashMapKeySet
class  HashMapValueCollection
class  KeyIterator
class  ValueIterator

Public Member Functions

 HashMap ()
 Creates a new empty HashMap with default configuration settings.
 HashMap (int capacity)
 Constructs a new HashMap instance with the specified capacity.
 HashMap (int capacity, float loadFactor)
 Constructs a new HashMap instance with the specified capacity.
 HashMap (const HashMap< K, V > &map)
 Creates a new HashMap with default configuration settings and fills it with the contents of the given source Map instance.
 HashMap (const Map< K, V > &map)
 Creates a new HashMap with default configuration settings and fills it with the contents of the given source Map instance.
virtual ~HashMap ()
virtual void clear ()
 Removes all of the mappings from this map (optional operation).
virtual bool isEmpty () const
virtual int size () const
virtual bool containsKey (const K &key) const
 Returns true if this map contains a mapping for the specified key.
virtual bool containsValue (const V &value) const
 Returns true if this map maps one or more keys to the specified value.
virtual V & get (const K &key)
 Gets the value mapped to the specified key in the Map.
virtual const V & get (const K &key) const
 Gets the value mapped to the specified key in the Map.
virtual bool put (const K &key, const V &value)
 Associates the specified value with the specified key in this map (optional operation).
virtual bool put (const K &key, const V &value, V &oldValue)
 Associates the specified value with the specified key in this map (optional operation).
virtual void putAll (const Map< K, V > &map)
 Copies all of the mappings from the specified map to this map (optional operation).
virtual V remove (const K &key)
 Removes the value (key/value pair) for the specified key from the map, returns a copy of the value that was mapped to the key.
virtual Set< MapEntry< K, V > > & entrySet ()
 Returns a Set view of the mappings contained in this map.
virtual const Set< MapEntry< K,
V > > & 
entrySet () const
virtual Set< K > & keySet ()
 Returns a Set view of the keys contained in this map.
virtual const Set< K > & keySet () const
virtual Collection< V > & values ()
 Returns a Collection view of the values contained in this map.
virtual const Collection< V > & values () const
virtual bool equals (const Map< K, V > &source) const
 Compares the specified object with this map for equality.
virtual void copy (const Map< K, V > &source)
 Copies the content of the source map into this map.
virtual std::string toString () const
- Public Member Functions inherited from decaf::util::AbstractMap< K, V >
 AbstractMap ()
 AbstractMap (const Map< K, V > &map)
 AbstractMap (const AbstractMap< K, V > &map)
virtual ~AbstractMap ()
virtual void lock ()
 Locks the object.
virtual bool tryLock ()
 Attempts to Lock the object, if the lock is already held by another thread than this method returns false.
virtual void unlock ()
 Unlocks the object.
virtual void wait ()
 Waits on a signal from this object, which is generated by a call to Notify.
virtual void wait (long long millisecs)
 Waits on a signal from this object, which is generated by a call to Notify.
virtual void wait (long long millisecs, int nanos)
 Waits on a signal from this object, which is generated by a call to Notify.
virtual void notify ()
 Signals a waiter on this object that it can now wake up and continue.
virtual void notifyAll ()
 Signals the waiters on this object that it can now wake up and continue.
- Public Member Functions inherited from decaf::util::Map< K, V >
 Map ()
 Default constructor - does nothing.
virtual ~Map ()
- Public Member Functions inherited from decaf::util::concurrent::Synchronizable
virtual ~Synchronizable ()

Protected Member Functions

HashMapEntry * getEntry (const K &key) const
bool putImpl (const K &key, const V &value, V &oldValue)
bool putImpl (const K &key, const V &value)
void putAllImpl (const Map< K, V > &map)
HashMapEntry * findKeyEntry (const K &key, int index, int keyHash) const
void rehash (int capacity)
void rehash ()
HashMapEntry * createEntry (const K &key, int index, const V &value)
HashMapEntry * createHashedEntry (const K &key, int index, int hash)
void removeEntry (HashMapEntry *entry)
HashMapEntry * removeEntry (const K &key)

Additional Inherited Members

- Protected Attributes inherited from decaf::util::AbstractMap< K, V >
util::concurrent::Mutex mutex

Detailed Description

template<typename K, typename V, typename HASHCODE = HashCode<K>>
class decaf::util::HashMap< K, V, HASHCODE >

Hash table based implementation of the Map interface.

This implementation provides all of the optional map operations, and permits null values and the null key. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

Note that this implementation is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the Collections::synchronizedMap method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

Map<K, V>* map = Collections::synchronizedMap(new HashMap<K, V>());

The iterators returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

Since
1.0

Constructor & Destructor Documentation

template<typename K, typename V, typename HASHCODE = HashCode<K>>
decaf::util::HashMap< K, V, HASHCODE >::HashMap ( )
inline

Creates a new empty HashMap with default configuration settings.

template<typename K, typename V, typename HASHCODE = HashCode<K>>
decaf::util::HashMap< K, V, HASHCODE >::HashMap ( int  capacity)
inline

Constructs a new HashMap instance with the specified capacity.

Parameters
capacityThe initial capacity of this hash map.
Exceptions
IllegalArgumentExceptionwhen the capacity is less than zero.
template<typename K, typename V, typename HASHCODE = HashCode<K>>
decaf::util::HashMap< K, V, HASHCODE >::HashMap ( int  capacity,
float  loadFactor 
)
inline

Constructs a new HashMap instance with the specified capacity.

Parameters
capacityThe initial capacity of this hash map.
loadFactorThe load factor to use for this hash map.
Exceptions
IllegalArgumentExceptionwhen the capacity is less than zero.
template<typename K, typename V, typename HASHCODE = HashCode<K>>
decaf::util::HashMap< K, V, HASHCODE >::HashMap ( const HashMap< K, V > &  map)
inline

Creates a new HashMap with default configuration settings and fills it with the contents of the given source Map instance.

Parameters
mapThe Map instance whose elements are copied into this HashMap instance.

References decaf::util::HashMap< K, V, HASHCODE >::putAll(), and decaf::util::HashMap< K, V, HASHCODE >::size().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
decaf::util::HashMap< K, V, HASHCODE >::HashMap ( const Map< K, V > &  map)
inline

Creates a new HashMap with default configuration settings and fills it with the contents of the given source Map instance.

Parameters
mapThe Map instance whose elements are copied into this HashMap instance.

References decaf::util::HashMap< K, V, HASHCODE >::putAll(), and decaf::util::Map< K, V >::size().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual decaf::util::HashMap< K, V, HASHCODE >::~HashMap ( )
inlinevirtual

Member Function Documentation

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual void decaf::util::HashMap< K, V, HASHCODE >::clear ( )
inlinevirtual

Removes all of the mappings from this map (optional operation).

The map will be empty after this call returns.

Exceptions
UnsupportedOperationExceptionif the clear operation is not supported by this map.

Implements decaf::util::Map< K, V >.

References decaf::lang::ArrayPointer< T >::length(), and NULL.

Referenced by decaf::util::HashMap< K, V, HASHCODE >::copy().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual bool decaf::util::HashMap< K, V, HASHCODE >::containsKey ( const K &  key) const
inlinevirtual

Returns true if this map contains a mapping for the specified key.

More formally, returns true if and only if this map contains a mapping for a key k such that (key == k). (There can be at most one such mapping.)

Parameters
keyThe key to look up.
Returns
true if this map contains the key mapping, otherwise false.

Implements decaf::util::Map< K, V >.

References decaf::util::HashMap< K, V, HASHCODE >::getEntry(), and NULL.

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual bool decaf::util::HashMap< K, V, HASHCODE >::containsValue ( const V &  value) const
inlinevirtual

Returns true if this map maps one or more keys to the specified value.

More formally, returns true if and only if this map contains at least one mapping to a value v such that (value==v). This operation will probably require time linear in the map size for most implementations of the Map interface.

Parameters
valueThe Value to look up in this Map.
Returns
true if this map contains at least one mapping for the value, otherwise false.

Implements decaf::util::Map< K, V >.

References decaf::lang::ArrayPointer< T >::length(), and NULL.

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual void decaf::util::HashMap< K, V, HASHCODE >::copy ( const Map< K, V > &  source)
inlinevirtual

Copies the content of the source map into this map.

Erases all existing mappings in this map. The copy is performed by using the entrySet of the source Map and iterating over those entries, inserting each into the target.

Parameters
sourceThe source object to copy from.

Implements decaf::util::Map< K, V >.

References decaf::util::HashMap< K, V, HASHCODE >::clear(), decaf::lang::ArrayPointer< T >::length(), decaf::util::HashMap< K, V, HASHCODE >::putAll(), and decaf::util::Map< K, V >::size().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
HashMapEntry* decaf::util::HashMap< K, V, HASHCODE >::createEntry ( const K &  key,
int  index,
const V &  value 
)
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
HashMapEntry* decaf::util::HashMap< K, V, HASHCODE >::createHashedEntry ( const K &  key,
int  index,
int  hash 
)
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual Set< MapEntry<K,V> >& decaf::util::HashMap< K, V, HASHCODE >::entrySet ( )
inlinevirtual

Returns a Set view of the mappings contained in this map.

The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns
a reference to a Set<MapEntry<K,V>> that is backed by this Map.

Implements decaf::util::Map< K, V >.

References NULL, and decaf::lang::Pointer< T, REFCOUNTER >::reset().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual const Set< MapEntry<K,V> >& decaf::util::HashMap< K, V, HASHCODE >::entrySet ( ) const
inlinevirtual
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual bool decaf::util::HashMap< K, V, HASHCODE >::equals ( const Map< K, V > &  source) const
inlinevirtual

Compares the specified object with this map for equality.

Returns true if the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

Parameters
sourceMap to compare to this one.
Returns
true if the Map passed is equal in value to this one.

Implements decaf::util::Map< K, V >.

template<typename K, typename V, typename HASHCODE = HashCode<K>>
HashMapEntry* decaf::util::HashMap< K, V, HASHCODE >::findKeyEntry ( const K &  key,
int  index,
int  keyHash 
) const
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual V& decaf::util::HashMap< K, V, HASHCODE >::get ( const K &  key)
inlinevirtual

Gets the value mapped to the specified key in the Map.

If there is no element in the map whose key is equivalent to the key provided then a NoSuchElementException is thrown.

Parameters
keyThe search key whose value should be returned if present.
Returns
A reference to the value for the given key if present in the Map.
Exceptions
NoSuchElementExceptionif the key requests doesn't exist in the Map.

Implements decaf::util::Map< K, V >.

References decaf::util::HashMap< K, V, HASHCODE >::getEntry(), and NULL.

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual const V& decaf::util::HashMap< K, V, HASHCODE >::get ( const K &  key) const
inlinevirtual

Gets the value mapped to the specified key in the Map.

If there is no element in the map whose key is equivalent to the key provided then a NoSuchElementException is thrown.

Parameters
keyThe search key whose value should be returned if present.
Returns
A const reference to the value for the given key if present in the Map.
Exceptions
NoSuchElementExceptionif the key requests doesn't exist in the Map.

Implements decaf::util::Map< K, V >.

References decaf::util::HashMap< K, V, HASHCODE >::getEntry(), and NULL.

template<typename K, typename V, typename HASHCODE = HashCode<K>>
HashMapEntry* decaf::util::HashMap< K, V, HASHCODE >::getEntry ( const K &  key) const
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual bool decaf::util::HashMap< K, V, HASHCODE >::isEmpty ( ) const
inlinevirtual
Returns
if the Map contains any element or not, TRUE or FALSE

Implements decaf::util::Map< K, V >.

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual Set<K>& decaf::util::HashMap< K, V, HASHCODE >::keySet ( )
inlinevirtual

Returns a Set view of the keys contained in this map.

The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns
a set view of the keys contained in this map,

Implements decaf::util::Map< K, V >.

References NULL, and decaf::lang::Pointer< T, REFCOUNTER >::reset().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual const Set<K>& decaf::util::HashMap< K, V, HASHCODE >::keySet ( ) const
inlinevirtual
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual bool decaf::util::HashMap< K, V, HASHCODE >::put ( const K &  key,
const V &  value 
)
inlinevirtual

Associates the specified value with the specified key in this map (optional operation).

If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Parameters
keyThe target key.
valueThe value to be set.
Returns
true if the put operation replaced a value that was associated with an existing mapping to the given key or false otherwise.
Exceptions
UnsupportedOperationExceptionif this map is unmodifiable.
IllegalArgumentExceptionif some property of the specified key or value prevents it from being stored in this map

Implements decaf::util::Map< K, V >.

References decaf::util::HashMap< K, V, HASHCODE >::putImpl().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual bool decaf::util::HashMap< K, V, HASHCODE >::put ( const K &  key,
const V &  value,
V &  oldValue 
)
inlinevirtual

Associates the specified value with the specified key in this map (optional operation).

If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

This method accepts a reference to a value which will be assigned the previous value for the given key (if any). If there was no previous mapping for the given key the out value is not written to. A return of true indicates that a value was replaced by this put operation.

Parameters
keyThe target key.
valueThe value to be set.
oldValue(out) The value previously held in the mapping for this key. .
Returns
true if the put operation replaced a value that was associated with an existing mapping to the given key or false otherwise.
Exceptions
UnsupportedOperationExceptionif this map is unmodifiable.
IllegalArgumentExceptionif some property of the specified key or value prevents it from being stored in this map

Implements decaf::util::Map< K, V >.

References decaf::util::HashMap< K, V, HASHCODE >::putImpl().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual void decaf::util::HashMap< K, V, HASHCODE >::putAll ( const Map< K, V > &  other)
inlinevirtual

Copies all of the mappings from the specified map to this map (optional operation).

The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.

Parameters
otherA Map instance whose elements are to all be inserted in this Map.
Exceptions
UnsupportedOperationExceptionIf the implementing class does not support the putAll operation.

Implements decaf::util::Map< K, V >.

References decaf::util::Map< K, V >::isEmpty(), and decaf::util::HashMap< K, V, HASHCODE >::putAllImpl().

Referenced by decaf::util::HashMap< K, V, HASHCODE >::copy(), and decaf::util::HashMap< K, V, HASHCODE >::HashMap().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
bool decaf::util::HashMap< K, V, HASHCODE >::putImpl ( const K &  key,
const V &  value 
)
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
void decaf::util::HashMap< K, V, HASHCODE >::rehash ( int  capacity)
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
void decaf::util::HashMap< K, V, HASHCODE >::rehash ( )
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual V decaf::util::HashMap< K, V, HASHCODE >::remove ( const K &  key)
inlinevirtual

Removes the value (key/value pair) for the specified key from the map, returns a copy of the value that was mapped to the key.

Care must be taken when using this operation as it will throw an exception if there is no mapping for the given key.

Parameters
keyThe search key whose mapping is to be removed.
Returns
a copy of the element that was previously mapped to the given key.
Exceptions
NoSuchElementExceptionif this key is not in the Map.
UnsupportedOperationExceptionif this map is unmodifiable.

Implements decaf::util::Map< K, V >.

References NULL, and decaf::util::HashMap< K, V, HASHCODE >::removeEntry().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
void decaf::util::HashMap< K, V, HASHCODE >::removeEntry ( HashMapEntry *  entry)
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
HashMapEntry* decaf::util::HashMap< K, V, HASHCODE >::removeEntry ( const K &  key)
inlineprotected
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual int decaf::util::HashMap< K, V, HASHCODE >::size ( ) const
inlinevirtual
Returns
The number of elements (key/value pairs) in this map.

Implements decaf::util::Map< K, V >.

Referenced by decaf::util::HashMap< K, V, HASHCODE >::HashMap().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual std::string decaf::util::HashMap< K, V, HASHCODE >::toString ( ) const
inlinevirtual
template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual Collection<V>& decaf::util::HashMap< K, V, HASHCODE >::values ( )
inlinevirtual

Returns a Collection view of the values contained in this map.

The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations. For the const version of this method the Collection can only be used as a view into the Map.

Returns
a collection view of the values contained in this map.

Implements decaf::util::Map< K, V >.

References NULL, and decaf::lang::Pointer< T, REFCOUNTER >::reset().

template<typename K, typename V, typename HASHCODE = HashCode<K>>
virtual const Collection<V>& decaf::util::HashMap< K, V, HASHCODE >::values ( ) const
inlinevirtual

The documentation for this class was generated from the following file: