WTF
HashFunctions.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef WTF_HashFunctions_h
00024 #define WTF_HashFunctions_h
00025
00026 #include <kjs/global.h>
00027 #ifdef HAVE_STDINT_H
00028 #include <stdint.h>
00029 #endif
00030 #include "RefPtr.h"
00031
00032 namespace WTF {
00033
00034 template<size_t size> struct IntTypes;
00035 template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
00036 template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
00037 template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
00038 template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
00039
00040
00041
00042
00043 inline unsigned intHash(uint32_t key)
00044 {
00045 key += ~(key << 15);
00046 key ^= (key >> 10);
00047 key += (key << 3);
00048 key ^= (key >> 6);
00049 key += ~(key << 11);
00050 key ^= (key >> 16);
00051 return key;
00052 }
00053
00054
00055 inline unsigned intHash(uint64_t key)
00056 {
00057 key += ~(key << 32);
00058 key ^= (key >> 22);
00059 key += ~(key << 13);
00060 key ^= (key >> 8);
00061 key += (key << 3);
00062 key ^= (key >> 15);
00063 key += ~(key << 27);
00064 key ^= (key >> 31);
00065 return static_cast<unsigned>(key);
00066 }
00067
00068 template<typename T> struct IntHash {
00069 static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
00070 static bool equal(T a, T b) { return a == b; }
00071 };
00072
00073
00074
00075 template<typename T> struct PtrHash {
00076 static unsigned hash(T key) { return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key)); }
00077 static bool equal(T a, T b) { return a == b; }
00078 };
00079 template<typename P> struct PtrHash<RefPtr<P> > {
00080 static unsigned hash(const RefPtr<P>& key) { return PtrHash<P*>::hash(key.get()); }
00081 static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
00082 };
00083
00084
00085
00086 template<typename T> struct DefaultHash;
00087
00088
00089
00090 template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
00091 template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
00092 template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
00093 template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
00094 template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
00095 template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };
00096
00097
00098
00099 template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
00100 template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
00101
00102 }
00103
00104 using WTF::DefaultHash;
00105 using WTF::IntHash;
00106 using WTF::PtrHash;
00107
00108 #endif // KXLMCORE_HashFunctions_h