123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #ifndef _FNVHASH_H
- #define _FNVHASH_H
- #include <AK/SoundEngine/Common/AkNumeralTypes.h>
- namespace AK
- {
- struct Hash32
- {
- typedef AkUInt32 HashType;
- typedef AkUInt32 SizeType;
- static inline unsigned int Bits() {return 32;}
- static inline HashType Prime() {return 16777619;}
- static const HashType s_offsetBasis = 2166136261U;
- };
-
- struct Hash30 : public Hash32
- {
- static inline unsigned int Bits() {return 30;}
- };
-
- struct Hash64
- {
- typedef AkUInt64 HashType;
- typedef AkUInt64 SizeType;
- static inline unsigned int Bits() {return 64;}
- static inline HashType Prime() {return 1099511628211ULL;}
- static const HashType s_offsetBasis = 14695981039346656037ULL;
- };
- template <class HashParams>
- class FNVHash
- {
- public:
- inline FNVHash( typename HashParams::HashType in_uBase = HashParams::s_offsetBasis );
-
-
-
- inline typename HashParams::HashType Compute( const void* in_pData, typename HashParams::SizeType in_dataSize );
- inline typename HashParams::HashType Get() const { return m_uHash; }
- template <typename T>
- inline typename HashParams::HashType Compute(const T& in_pData) { return Compute(&in_pData, sizeof(T)); }
-
- static inline typename HashParams::HashType ComputeLowerCase(const char* in_pData);
- private:
- typename HashParams::HashType m_uHash;
- };
- #if defined(_MSC_VER)
- #pragma warning(push)
- #pragma warning(disable:4127)
- #endif
- template <class HashParams>
- FNVHash<HashParams>::FNVHash( typename HashParams::HashType in_uBase )
- : m_uHash( in_uBase )
- {
- }
- template <class HashParams>
- typename HashParams::HashType FNVHash<HashParams>::Compute( const void* in_pData, typename HashParams::SizeType in_dataSize )
- {
- const unsigned char* pData = (const unsigned char*) in_pData;
- const unsigned char* pEnd = pData + in_dataSize;
- typename HashParams::HashType hval = m_uHash;
-
- while( pData < pEnd )
- {
- hval *= HashParams::Prime();
- hval ^= *pData++;
- }
- m_uHash = hval;
-
- if( HashParams::Bits() >= sizeof(typename HashParams::HashType) * 8 )
- return hval;
- typename HashParams::HashType mask = static_cast<typename HashParams::HashType>(((typename HashParams::HashType)1 << HashParams::Bits())-1);
- return (typename HashParams::HashType)(hval >> HashParams::Bits()) ^ (hval & mask);
- }
- template <class HashParams>
- typename HashParams::HashType FNVHash<HashParams>::ComputeLowerCase(const char* in_pData)
- {
- typename HashParams::HashType hval = HashParams::s_offsetBasis;
-
- while (*in_pData != 0)
- {
- hval *= HashParams::Prime();
- unsigned char c = (unsigned char)*in_pData++;
- c = (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c;
- hval ^= c;
- }
-
- if (HashParams::Bits() >= sizeof(typename HashParams::HashType) * 8)
- return hval;
- typename HashParams::HashType mask = static_cast<typename HashParams::HashType>(((typename HashParams::HashType)1 << HashParams::Bits()) - 1);
- return (typename HashParams::HashType)(hval >> HashParams::Bits()) ^ (hval & mask);
- }
- #if defined(_MSC_VER)
- #pragma warning(pop)
- #endif
- typedef FNVHash<Hash32> FNVHash32;
- typedef FNVHash<Hash30> FNVHash30;
- typedef FNVHash<Hash64> FNVHash64;
- }
- #endif
|