JUCE
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
Classes | Public Types | Public Member Functions
HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse > Class Template Reference

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

List of all members.

Classes

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

Public Types

typedef
TypeOfCriticalSectionToUse::ScopedLockType 
ScopedLockType
 Returns the type of scoped lock to use for locking this array.

Public Member Functions

 HashMap (const int numberOfSlots=defaultHashTableSize)
 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.
const ValueType operator[] (KeyTypeParameter keyToLookFor) const
 Returns the value corresponding to a given key.
bool contains (KeyTypeParameter keyToLookFor) const
 Returns true if the map contains an item with the specied 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.
void swapWith (HashMap &otherHashMap) noexcept
 Efficiently swaps the contents of two hash-maps.
const TypeOfCriticalSectionToUsegetLock () const noexcept
 Returns the CriticalSection that locks this structure.

Detailed Description

template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
class HashMap< KeyType, ValueType, HashFunctionToUse, 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
    {
        static int generateHash (MyKeyType key, int upperLimit)
        {
            // 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 HashFunctionToUse 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());
See also:
CriticalSection, DefaultHashFunctions, NamedValueSet, SortedSet

Member Typedef Documentation

template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
typedef TypeOfCriticalSectionToUse::ScopedLockType HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::ScopedLockType

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


Constructor & Destructor Documentation

template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::HashMap ( const int  numberOfSlots = defaultHashTableSize) [explicit]

Creates an empty hash-map.

The numberOfSlots parameter specifies 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().

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

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

Member Function Documentation

template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::clear ( )
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::size ( ) const

Returns the current number of items in the map.

template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
const ValueType HashMap< KeyType, ValueType, HashFunctionToUse, 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, HashFunctionToUse, TypeOfCriticalSectionToUse >::getLock(), and Array< ElementType, TypeOfCriticalSectionToUse >::getUnchecked().

template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
bool HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::contains ( KeyTypeParameter  keyToLookFor) const
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
bool HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::containsValue ( ValueTypeParameter  valueToLookFor) const
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::set ( KeyTypeParameter  newKey,
ValueTypeParameter  newValue 
)
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::remove ( KeyTypeParameter  keyToRemove)
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::removeValue ( ValueTypeParameter  valueToRemove)
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::remapTable ( int  newNumberOfSlots)
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
int HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::getNumSlots ( ) const
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
void HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::swapWith ( HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse > &  otherHashMap)
template<typename KeyType, typename ValueType, class HashFunctionToUse = DefaultHashFunctions, class TypeOfCriticalSectionToUse = DummyCriticalSection>
const TypeOfCriticalSectionToUse& HashMap< KeyType, ValueType, HashFunctionToUse, TypeOfCriticalSectionToUse >::getLock ( ) const

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