From affed111fe3820c31180f2314918979701dc4835 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Fri, 25 Sep 2009 03:43:00 +0000 Subject: [PATCH] - Add THashTrait specializations for FString, double, and float. SVN r1877 (scripting) --- src/tarray.h | 15 +++++++++++++++ src/zstring.h | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/src/tarray.h b/src/tarray.h index 92a6552e4..afea5db1e 100644 --- a/src/tarray.h +++ b/src/tarray.h @@ -398,11 +398,26 @@ template struct THashTraits { // Returns the hash value for a key. hash_t Hash(const KT key) { return (hash_t)(intptr_t)key; } + hash_t Hash(double key) { return ((hash_t *)&key)[0] ^ ((hash_t *)&key)[1]; } // Compares two keys, returning zero if they are the same. int Compare(const KT left, const KT right) { return left != right; } }; +template<> struct THashTraits +{ + // Use all bits when hashing singles instead of converting them to ints. + hash_t Hash(float key) { return *((hash_t *)&key); } + int Compare(float left, float right) { return left != right; } +}; + +template<> struct THashTraits +{ + // Use all bits when hashing doubles instead of converting them to ints. + hash_t Hash(double key) { return ((hash_t *)&key)[0] ^ ((hash_t *)&key)[1]; } + int Compare(double left, double right) { return left != right; } +}; + template struct TValueTraits { // Initializes a value for TMap. If a regular constructor isn't diff --git a/src/zstring.h b/src/zstring.h index db42b98e4..fc6287126 100644 --- a/src/zstring.h +++ b/src/zstring.h @@ -316,4 +316,12 @@ inline FName &FName::operator = (const FString &text) { Index = NameData.FindNam inline FName &FNameNoInit::operator = (const FString &text) { Index = NameData.FindName (text, text.Len(), false); return *this; } +// Hash FStrings on their contents. (used by TMap) +extern unsigned int SuperFastHash (const char *data, size_t len); +template<> struct THashTraits +{ + hash_t Hash(const FString &key) { return (hash_t)SuperFastHash(key, key.Len()); } + int Compare(const FString &left, const FString &right) { return left.Compare(right); } +}; + #endif