#ifndef WTF_HashFunctions_h
#define WTF_HashFunctions_h
#include "RefPtr.h"
#include <stdint.h>
namespace WTF {
template<size_t size> struct IntTypes;
template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
inline unsigned intHash(uint32_t key)
{
key += ~(key << 15);
key ^= (key >> 10);
key += (key << 3);
key ^= (key >> 6);
key += ~(key << 11);
key ^= (key >> 16);
return key;
}
inline unsigned intHash(uint64_t key)
{
key += ~(key << 32);
key ^= (key >> 22);
key += ~(key << 13);
key ^= (key >> 8);
key += (key << 3);
key ^= (key >> 15);
key += ~(key << 27);
key ^= (key >> 31);
return static_cast<unsigned>(key);
}
template<typename T> struct IntHash {
static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
static bool equal(T a, T b) { return a == b; }
};
template<typename T> struct PtrHash {
static unsigned hash(T key)
{
#if COMPILER(MSVC)
#pragma warning(push)
#pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's conversion warnings
#endif
return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
#if COMPILER(MSVC)
#pragma warning(pop)
#endif
}
static bool equal(T a, T b) { return a == b; }
};
template<typename P> struct PtrHash<RefPtr<P> > {
static unsigned hash(const RefPtr<P>& key) { return PtrHash<P*>::hash(key.get()); }
static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
};
template<typename T> struct DefaultHash;
template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };
template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
}
using WTF::DefaultHash;
using WTF::IntHash;
using WTF::PtrHash;
#endif // WTF_HashFunctions_h