template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
class HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >
Holds a set of mappings between some key/value pairs.
The types of the key and value objects are set as template parameters. You can also specify a class to supply a hash function that converts a key value into an hashed integer. This class must have the form:
struct MyHashGenerator
{
int generateHash (MyKeyType key, int upperLimit) const
{
return someFunctionOfMyKeyType (key) % upperLimit;
}
};
Like the Array class, the key and value types are expected to be copy-by-value types, so if you define them to be pointer types, this class won't delete the objects that they point to.
If you don't supply a class for the HashFunctionType template parameter, the default one provides some simple mappings for strings and ints.
DBG (i.getKey() <<
" -> " << i.getValue());
- Template Parameters
-
HashFunctionType | The class of hash function, which must be copy-constructible. |
- See also
- CriticalSection, DefaultHashFunctions, NamedValueSet, SortedSet
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::clear |
( |
| ) |
|
|
inline |
Removes all values from the map.
Note that this will clear the content, but won't affect the number of slots (see remapTable and getNumSlots).
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
const TypeOfCriticalSectionToUse& HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getLock |
( |
| ) |
const |
|
inlinenoexcept |
Returns the CriticalSection that locks this structure.
To lock, you can call getLock().enter() and getLock().exit(), or preferably use an object of ScopedLockType as an RAII lock for it.
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getNumSlots |
( |
| ) |
const |
|
inlinenoexcept |
Returns the number of slots which are available for hashing.
Each slot corresponds to a single hash-code, and each one can contain multiple items.
- See also
- getNumSlots()
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
ValueType& HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getReference |
( |
KeyTypeParameter |
keyToLookFor | ) |
|
|
inline |
Returns a reference to the value corresponding to a given key.
If the map doesn't contain the key, a default instance of the value type is added to the map and a reference to this is returned.
- Parameters
-
keyToLookFor | the key of the item being requested |
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
ValueType HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::operator[] |
( |
KeyTypeParameter |
keyToLookFor | ) |
const |
|
inline |
Returns the value corresponding to a given key.
If the map doesn't contain the key, a default instance of the value type is returned.
- Parameters
-
keyToLookFor | the key of the item being requested |
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::remapTable |
( |
int |
newNumberOfSlots | ) |
|
|
inline |
Remaps the hash-map to use a different number of slots for its hash function.
Each slot corresponds to a single hash-code, and each one can contain multiple items.
- See also
- getNumSlots()
References Array< ElementType, TypeOfCriticalSectionToUse, minimumAllocatedSize >::getUnchecked(), Array< ElementType, TypeOfCriticalSectionToUse, minimumAllocatedSize >::insertMultiple(), HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::HashEntry::nextEntry, and Array< ElementType, TypeOfCriticalSectionToUse, minimumAllocatedSize >::set().
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
Adds or replaces an element in the hash-map.
If there's already an item with the given key, this will replace its value. Otherwise, a new item will be added to the map.
template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
template<class OtherHashMapType >
void HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::swapWith |
( |
OtherHashMapType & |
otherHashMap | ) |
|
|
inlinenoexcept |
Efficiently swaps the contents of two hash-maps.