Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | List of all members
HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse > Class Template Reference

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

#include <juce_HashMap.h>

Classes

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.
 

Public Member Functions

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

Detailed Description

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
{
// 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.

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..
DBG (i.getKey() << " -> " << i.getValue());
Holds a set of mappings between some key/value pairs.
Definition juce_HashMap.h:107
void set(KeyTypeParameter newKey, ValueTypeParameter newValue)
Adds or replaces an element in the hash-map.
Definition juce_HashMap.h:235
#define DBG(textToWrite)
Writes a string to the standard error stream.
Definition juce_PlatformDefs.h:148
Iterates over the items in a HashMap.
Definition juce_HashMap.h:403
bool next() noexcept
Moves to the next item, if one is available.
Definition juce_HashMap.h:416
Template Parameters
HashFunctionTypeThe class of hash function, which must be copy-constructible.
See also
CriticalSection, DefaultHashFunctions, NamedValueSet, SortedSet

Member Typedef Documentation

◆ ScopedLockType

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

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

Constructor & Destructor Documentation

◆ HashMap()

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

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.

References Array< ElementType, TypeOfCriticalSectionToUse, minimumAllocatedSize >::insertMultiple().

◆ ~HashMap()

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

Member Function Documentation

◆ clear()

template<typename KeyType , typename ValueType , class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::clear ( )

◆ size()

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

Returns the current number of items in the map.

◆ operator[]()

template<typename KeyType , typename ValueType , class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
ValueType HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::operator[] ( KeyTypeParameter keyToLookFor) const

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

References HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getLock().

◆ getReference()

template<typename KeyType , typename ValueType , class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
ValueType & HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getReference ( KeyTypeParameter keyToLookFor)

◆ contains()

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

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

References HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getLock().

◆ containsValue()

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

◆ set()

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

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.

References HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getReference().

◆ remove()

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

◆ removeValue()

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

◆ remapTable()

template<typename KeyType , typename ValueType , class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::remapTable ( int newNumberOfSlots)

◆ getNumSlots()

template<typename KeyType , typename ValueType , class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getNumSlots ( ) const
noexcept

◆ swapWith()

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

◆ getLock()

template<typename KeyType , typename ValueType , class HashFunctionType = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
const TypeOfCriticalSectionToUse & HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::getLock ( ) const
noexcept

◆ begin()

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

Returns a start iterator for the values in this tree.

References HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::Iterator::next().

◆ end()

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

Returns an end iterator for the values in this tree.

References HashMap< KeyType, ValueType, HashFunctionType, TypeOfCriticalSectionToUse >::Iterator::resetToEnd().


The documentation for this class was generated from the following file:
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram