- Add THashTrait specializations for FString, double, and float.

SVN r1877 (scripting)
This commit is contained in:
Randy Heit 2009-09-25 03:43:00 +00:00
parent 1a442742f7
commit affed111fe
2 changed files with 23 additions and 0 deletions

View file

@ -398,11 +398,26 @@ template<class KT> struct THashTraits
{ {
// Returns the hash value for a key. // Returns the hash value for a key.
hash_t Hash(const KT key) { return (hash_t)(intptr_t)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. // Compares two keys, returning zero if they are the same.
int Compare(const KT left, const KT right) { return left != right; } int Compare(const KT left, const KT right) { return left != right; }
}; };
template<> struct THashTraits<float>
{
// 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<double>
{
// 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<class VT> struct TValueTraits template<class VT> struct TValueTraits
{ {
// Initializes a value for TMap. If a regular constructor isn't // Initializes a value for TMap. If a regular constructor isn't

View file

@ -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; } 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<FString>
{
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 #endif