LMMS
Loading...
Searching...
No Matches
water::HashMap< KeyType, ValueType, HashFunctionType > Class Template Reference

#include <HashMap.h>

Classes

class  HashEntry
struct  Iterator

Public Member Functions

 HashMap (int numberOfSlots=defaultHashTableSize, HashFunctionType hashFunction=HashFunctionType())
 ~HashMap ()
void clear ()
int size () const noexcept
ValueType operator[] (KeyTypeParameter keyToLookFor) const
bool contains (KeyTypeParameter keyToLookFor) const
bool containsValue (ValueTypeParameter valueToLookFor) const
void set (KeyTypeParameter newKey, ValueTypeParameter newValue)
void remove (KeyTypeParameter keyToRemove)
void removeValue (ValueTypeParameter valueToRemove)
void remapTable (int newNumberOfSlots)
int getNumSlots () const noexcept
template<class OtherHashMapType>
void swapWith (OtherHashMapType &otherHashMap) noexcept
Iterator begin () const noexcept
Iterator end () const noexcept

Private Types

enum  { defaultHashTableSize = 101 }

Private Member Functions

typedef PARAMETER_TYPE (KeyType) KeyTypeParameter
typedef PARAMETER_TYPE (ValueType) ValueTypeParameter
int generateHashFor (KeyTypeParameter key) const

Private Attributes

HashFunctionType hashFunctionToUse
Array< HashEntry * > hashSlots
int totalNumItems

Friends

struct Iterator

Detailed Description

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
class water::HashMap< KeyType, ValueType, HashFunctionType >

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;
}
};
ZCONST char * key
Definition crypt.c:587

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..
for (HashMap<int, String>::Iterator i (hash); i.next();)
DBG (i.getKey() << " -> " << i.getValue());
void set(KeyTypeParameter newKey, ValueTypeParameter newValue)
Definition HashMap.h:201
HashMap(int numberOfSlots=defaultHashTableSize, HashFunctionType hashFunction=HashFunctionType())
Definition HashMap.h:118
register unsigned i
Definition inflate.c:1575
#define DBG(textToWrite)
Definition HashMap.h:359
Template Parameters
HashFunctionTypeThe class of hash function, which must be copy-constructible.
See also
CriticalSection, DefaultHashFunctions, NamedValueSet, SortedSet

Member Enumeration Documentation

◆ anonymous enum

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

Constructor & Destructor Documentation

◆ HashMap()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
water::HashMap< KeyType, ValueType, HashFunctionType >::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>
water::HashMap< KeyType, ValueType, HashFunctionType >::~HashMap ( )
inline

Destructor.

Member Function Documentation

◆ begin()

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

Returns a start iterator for the values in this tree.

◆ clear()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
void water::HashMap< KeyType, ValueType, HashFunctionType >::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>
bool water::HashMap< KeyType, ValueType, HashFunctionType >::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>
bool water::HashMap< KeyType, ValueType, HashFunctionType >::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>
Iterator water::HashMap< KeyType, ValueType, HashFunctionType >::end ( ) const
inlinenoexcept

Returns an end iterator for the values in this tree.

◆ generateHashFor()

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

◆ getNumSlots()

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

◆ operator[]()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
ValueType water::HashMap< KeyType, ValueType, HashFunctionType >::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

◆ PARAMETER_TYPE() [1/2]

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
typedef water::HashMap< KeyType, ValueType, HashFunctionType >::PARAMETER_TYPE ( KeyType )
private

◆ PARAMETER_TYPE() [2/2]

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
typedef water::HashMap< KeyType, ValueType, HashFunctionType >::PARAMETER_TYPE ( ValueType )
private

◆ remapTable()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
void water::HashMap< KeyType, ValueType, HashFunctionType >::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>
void water::HashMap< KeyType, ValueType, HashFunctionType >::remove ( KeyTypeParameter keyToRemove)
inline

Removes an item with the given key.

◆ removeValue()

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

Removes all items with the given value.

◆ set()

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
void water::HashMap< KeyType, ValueType, HashFunctionType >::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.

◆ size()

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

Returns the current number of items in the map.

◆ swapWith()

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

Efficiently swaps the contents of two hash-maps.

◆ Iterator

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

Member Data Documentation

◆ hashFunctionToUse

template<typename KeyType, typename ValueType, class HashFunctionType = DefaultHashFunctions>
HashFunctionType water::HashMap< KeyType, ValueType, HashFunctionType >::hashFunctionToUse
private

◆ hashSlots

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

◆ totalNumItems

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

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