JUCE  v5.4.1-191-g0ab5e696f
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse > Class Template Reference

Holds a set of mappings between some key/value pairs. More...

#include <juce_HashMap.h>

Inheritance diagram for juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >:
Collaboration diagram for juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >:

Classes

class  HashEntry
 
struct  Iterator
 Iterates over the items in a HashMap. More...
 

Public Types

using ScopedLockType = typename TypeOfCriticalSectionToUse::ScopedLockType
 Returns the type of scoped lock to use for locking this array. More...
 

Public Member Functions

 HashMap (int numberOfSlots=defaultHashTableSize, HashFunctionType hashFunction=HashFunctionType())
 Creates an empty hash-map. More...
 
 ~HashMap ()
 Destructor. More...
 
Iterator begin () const noexcept
 Returns a start iterator for the values in this tree. More...
 
void clear ()
 Removes all values from the map. More...
 
bool contains (KeyTypeParameter keyToLookFor) const
 Returns true if the map contains an item with the specied key. More...
 
bool containsValue (ValueTypeParameter valueToLookFor) const
 Returns true if the hash contains at least one occurrence of a given value. More...
 
Iterator end () const noexcept
 Returns an end iterator for the values in this tree. More...
 
const TypeOfCriticalSectionToUse & getLock () const noexcept
 Returns the CriticalSection that locks this structure. More...
 
int getNumSlots () const noexcept
 Returns the number of slots which are available for hashing. More...
 
ValueType & getReference (KeyTypeParameter keyToLookFor)
 Returns a reference to the value corresponding to a given key. More...
 
ValueType operator[] (KeyTypeParameter keyToLookFor) const
 Returns the value corresponding to a given key. More...
 
void remapTable (int newNumberOfSlots)
 Remaps the hash-map to use a different number of slots for its hash function. More...
 
void remove (KeyTypeParameter keyToRemove)
 Removes an item with the given key. More...
 
void removeValue (ValueTypeParameter valueToRemove)
 Removes all items with the given value. More...
 
void set (KeyTypeParameter newKey, ValueTypeParameter newValue)
 Adds or replaces an element in the hash-map. More...
 
int size () const noexcept
 Returns the current number of items in the map. More...
 
template<class OtherHashMapType >
void swapWith (OtherHashMapType &otherHashMap) noexcept
 Efficiently swaps the contents of two hash-maps. More...
 

Private Types

enum  { defaultHashTableSize = 101 }
 
using KeyTypeParameter = typename TypeHelpers::ParameterType< KeyType >::type
 
using ValueTypeParameter = typename TypeHelpers::ParameterType< ValueType >::type
 

Private Member Functions

int generateHashFor (KeyTypeParameter key, int numSlots) const
 
HashEntrygetSlot (KeyType key) const noexcept
 

Static Private Member Functions

static HashEntrygetEntry (HashEntry *firstEntry, KeyType keyToLookFor) noexcept
 

Private Attributes

HashFunctionType hashFunctionToUse
 
Array< HashEntry * > hashSlots
 
TypeOfCriticalSectionToUse lock
 
int totalNumItems
 

Friends

struct Iterator
 

Detailed Description

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
class juce::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
{
// The function must return a value 0 <= x < upperLimit
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.

HashMap<int, String> hash;
hash.set (1, "item1");
hash.set (2, "item2");
DBG (hash [1]); // prints "item1"
DBG (hash [2]); // prints "item2"
// This iterates the map, printing all of its key -> value pairs..
for (HashMap<int, String>::Iterator i (hash); i.next();)
DBG (i.getKey() << " -> " << i.getValue());
Template Parameters
HashFunctionTypeThe class of hash function, which must be copy-constructible.
See also
CriticalSection, DefaultHashFunctions, NamedValueSet, SortedSet

{Core}

Member Typedef Documentation

◆ KeyTypeParameter

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
using juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::KeyTypeParameter = typename TypeHelpers::ParameterType<KeyType>::type
private

◆ ScopedLockType

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
using juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::ScopedLockType = typename TypeOfCriticalSectionToUse::ScopedLockType

Returns the type of scoped lock to use for locking this array.

◆ ValueTypeParameter

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
using juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::ValueTypeParameter = typename TypeHelpers::ParameterType<ValueType>::type
private

Member Enumeration Documentation

◆ anonymous enum

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
anonymous enum
private
Enumerator
defaultHashTableSize 

Constructor & Destructor Documentation

◆ HashMap()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::HashMap ( int  numberOfSlots = defaultHashTableSize,
HashFunctionType  hashFunction = HashFunctionType() 
)
inlineexplicit

Creates an empty hash-map.

Parameters
numberOfSlotsSpecifies the number of hash entries the map will use. This will be the "upperLimit" parameter that is passed to your generateHash() function. The number of hash slots will grow automatically if necessary, or it can be remapped manually using remapTable().
hashFunctionAn instance of HashFunctionType, which will be copied and stored to use with the HashMap. This parameter can be omitted if HashFunctionType has a default constructor.

◆ ~HashMap()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::~HashMap ( )
inline

Destructor.

Member Function Documentation

◆ begin()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
Iterator juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::begin ( ) const
inlinenoexcept

Returns a start iterator for the values in this tree.

◆ clear()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void juce::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).

◆ contains()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
bool juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::contains ( KeyTypeParameter  keyToLookFor) const
inline

Returns true if the map contains an item with the specied key.

◆ containsValue()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
bool juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::containsValue ( ValueTypeParameter  valueToLookFor) const
inline

Returns true if the hash contains at least one occurrence of a given value.

◆ end()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
Iterator juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::end ( ) const
inlinenoexcept

Returns an end iterator for the values in this tree.

◆ generateHashFor()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::generateHashFor ( KeyTypeParameter  key,
int  numSlots 
) const
inlineprivate

◆ getEntry()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
static HashEntry* juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getEntry ( HashEntry firstEntry,
KeyType  keyToLookFor 
)
inlinestaticprivatenoexcept

◆ getLock()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
const TypeOfCriticalSectionToUse& juce::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.

◆ getNumSlots()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int juce::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()

◆ getReference()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
ValueType& juce::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
keyToLookForthe key of the item being requested

◆ getSlot()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
HashEntry* juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getSlot ( KeyType  key) const
inlineprivatenoexcept

◆ operator[]()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
ValueType juce::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
keyToLookForthe key of the item being requested

◆ remapTable()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void juce::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()

◆ remove()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::remove ( KeyTypeParameter  keyToRemove)
inline

◆ removeValue()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::removeValue ( ValueTypeParameter  valueToRemove)
inline

Removes all items with the given value.

◆ set()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::set ( KeyTypeParameter  newKey,
ValueTypeParameter  newValue 
)
inline

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.

Referenced by juce::HeavyweightLeakedObjectDetector< OwnerClass >::HeavyweightLeakedObjectDetector().

◆ size()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::size ( ) const
inlinenoexcept

Returns the current number of items in the map.

Referenced by juce::HeavyweightLeakedObjectDetector< OwnerClass >::BacktraceMapHolder::~BacktraceMapHolder().

◆ swapWith()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
template<class OtherHashMapType >
void juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::swapWith ( OtherHashMapType &  otherHashMap)
inlinenoexcept

Efficiently swaps the contents of two hash-maps.

Friends And Related Function Documentation

◆ Iterator

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
friend struct Iterator
friend

Member Data Documentation

◆ hashFunctionToUse

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
HashFunctionType juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::hashFunctionToUse
private

◆ hashSlots

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
Array<HashEntry*> juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::hashSlots
private

◆ lock

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
TypeOfCriticalSectionToUse juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::lock
private

◆ totalNumItems

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int juce::HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::totalNumItems
private

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