From 0e2798621452091ebccb85d096cd83548377da44 Mon Sep 17 00:00:00 2001 From: jules Date: Sun, 28 Jul 2013 19:49:45 +0100 Subject: [PATCH] Modified the HashMap class to store an instance of its hashcode-generator class instead of calling static methods. --- modules/juce_core/containers/juce_HashMap.h | 43 ++++++++++++--------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/modules/juce_core/containers/juce_HashMap.h b/modules/juce_core/containers/juce_HashMap.h index 40ad386c19..2608ad5c3a 100644 --- a/modules/juce_core/containers/juce_HashMap.h +++ b/modules/juce_core/containers/juce_HashMap.h @@ -40,17 +40,16 @@ use with the HashMap class. @see HashMap */ -class DefaultHashFunctions +struct DefaultHashFunctions { -public: /** Generates a simple hash from an integer. */ - static int generateHash (const int key, const int upperLimit) noexcept { return std::abs (key) % upperLimit; } + int generateHash (const int key, const int upperLimit) const noexcept { return std::abs (key) % upperLimit; } /** Generates a simple hash from an int64. */ - static int generateHash (const int64 key, const int upperLimit) noexcept { return std::abs ((int) key) % upperLimit; } + int generateHash (const int64 key, const int upperLimit) const noexcept { return std::abs ((int) key) % upperLimit; } /** Generates a simple hash from a string. */ - static int generateHash (const String& key, const int upperLimit) noexcept { return (int) (((uint32) key.hashCode()) % (uint32) upperLimit); } + int generateHash (const String& key, const int upperLimit) const noexcept { return (int) (((uint32) key.hashCode()) % (uint32) upperLimit); } /** Generates a simple hash from a variant. */ - static int generateHash (const var& key, const int upperLimit) noexcept { return generateHash (key.toString(), upperLimit); } + int generateHash (const var& key, const int upperLimit) const noexcept { return generateHash (key.toString(), upperLimit); } }; @@ -65,7 +64,7 @@ public: @code struct MyHashGenerator { - static int generateHash (MyKeyType key, int upperLimit) + int generateHash (MyKeyType key, int upperLimit) { // The function must return a value 0 <= x < upperLimit return someFunctionOfMyKeyType (key) % upperLimit; @@ -73,11 +72,11 @@ public: }; @endcode - 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. + 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 + If you don't supply a class for the HashFunctionType template parameter, the default one provides some simple mappings for strings and ints. @code @@ -93,11 +92,12 @@ public: DBG (i.getKey() << " -> " << i.getValue()); @endcode + @tparam HashFunctionType The class of hash function, which must be copy-constructible. @see CriticalSection, DefaultHashFunctions, NamedValueSet, SortedSet */ template class HashMap { @@ -109,12 +109,18 @@ public: //============================================================================== /** 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(). + 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(). + + @param hashFunction An 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. */ - explicit HashMap (const int numberOfSlots = defaultHashTableSize) - : totalNumItems (0) + explicit HashMap (int numberOfSlots = defaultHashTableSize, + HashFunctionType hashFunction = HashFunctionType()) + : hashFunctionToUse (hashFunction), totalNumItems (0) { slots.insertMultiple (0, nullptr, numberOfSlots); } @@ -435,13 +441,14 @@ private: enum { defaultHashTableSize = 101 }; friend class Iterator; + HashFunctionType hashFunctionToUse; Array slots; int totalNumItems; TypeOfCriticalSectionToUse lock; int generateHashFor (KeyTypeParameter key) const { - const int hash = HashFunctionToUse::generateHash (key, getNumSlots()); + const int hash = hashFunctionToUse.generateHash (key, getNumSlots()); jassert (isPositiveAndBelow (hash, getNumSlots())); // your hash function is generating out-of-range numbers! return hash; }