diff --git a/modules/juce_core/containers/juce_HashMap.h b/modules/juce_core/containers/juce_HashMap.h index f2b31dbf40..6fab721daf 100644 --- a/modules/juce_core/containers/juce_HashMap.h +++ b/modules/juce_core/containers/juce_HashMap.h @@ -47,6 +47,8 @@ struct DefaultHashFunctions static int generateHash (const var& key, int upperLimit) noexcept { return generateHash (key.toString(), upperLimit); } /** Generates a simple hash from a void ptr. */ static int generateHash (const void* key, int upperLimit) noexcept { return generateHash ((pointer_sized_uint) key, upperLimit); } + /** Generates a simple hash from a UUID. */ + static int generateHash (const Uuid& key, int upperLimit) noexcept { return generateHash (key.hash(), upperLimit); } }; diff --git a/modules/juce_core/juce_core.h b/modules/juce_core/juce_core.h index 275b633066..b785cc464a 100644 --- a/modules/juce_core/juce_core.h +++ b/modules/juce_core/juce_core.h @@ -250,6 +250,7 @@ namespace juce #include "text/juce_LocalisedStrings.h" #include "text/juce_Base64.h" #include "misc/juce_Result.h" +#include "misc/juce_Uuid.h" #include "containers/juce_Variant.h" #include "containers/juce_NamedValueSet.h" #include "containers/juce_DynamicObject.h" @@ -280,7 +281,6 @@ namespace juce #include "maths/juce_Expression.h" #include "maths/juce_Random.h" #include "misc/juce_RuntimePermissions.h" -#include "misc/juce_Uuid.h" #include "misc/juce_WindowsRegistry.h" #include "threads/juce_ChildProcess.h" #include "threads/juce_DynamicLibrary.h" diff --git a/modules/juce_core/misc/juce_Uuid.cpp b/modules/juce_core/misc/juce_Uuid.cpp index 807031d02b..b4a830cf29 100644 --- a/modules/juce_core/misc/juce_Uuid.cpp +++ b/modules/juce_core/misc/juce_Uuid.cpp @@ -134,4 +134,24 @@ uint8 Uuid::getClockSeqAndReserved() const noexcept { return uuid[8]; } uint8 Uuid::getClockSeqLow() const noexcept { return uuid[9]; } uint64 Uuid::getNode() const noexcept { return (((uint64) ByteOrder::bigEndianShort (uuid + 10)) << 32) + ByteOrder::bigEndianInt (uuid + 12); } +uint64 Uuid::hash() const noexcept +{ + uint64 result = 0; + + for (auto n : uuid) + result = ((uint64) 101) * result + n; + + return result; +} + } // namespace juce + +#if ! DOXYGEN +namespace std +{ + template <> struct hash + { + size_t operator() (const juce::Uuid& u) const noexcept { return (size_t) u.hash(); } + }; +} +#endif diff --git a/modules/juce_core/misc/juce_Uuid.h b/modules/juce_core/misc/juce_Uuid.h index 08bb4da94e..f15d6e8724 100644 --- a/modules/juce_core/misc/juce_Uuid.h +++ b/modules/juce_core/misc/juce_Uuid.h @@ -105,6 +105,9 @@ public: /** Returns the node section of the UUID. */ uint64 getNode() const noexcept; + /** Returns a hash of the UUID. */ + uint64 hash() const noexcept; + //============================================================================== /** Returns a pointer to the internal binary representation of the ID.