2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #ifndef WTF_HashFunctions_h
22 #define WTF_HashFunctions_h
29 template<size_t size> struct IntTypes;
30 template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
31 template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
32 template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
33 template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
35 // integer hash function
37 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
38 inline unsigned intHash(uint8_t key8)
50 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
51 inline unsigned intHash(uint16_t key16)
63 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
64 inline unsigned intHash(uint32_t key)
75 // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
76 inline unsigned intHash(uint64_t key)
86 return static_cast<unsigned>(key);
89 template<typename T> struct IntHash {
90 static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
91 static bool equal(T a, T b) { return a == b; }
92 static const bool safeToCompareToEmptyOrDeleted = true;
95 template<typename T> struct FloatHash {
96 static unsigned hash(T key)
100 typename IntTypes<sizeof(T)>::UnsignedType bits;
103 return intHash(u.bits);
105 static bool equal(T a, T b) { return a == b; }
106 static const bool safeToCompareToEmptyOrDeleted = true;
109 // pointer identity hash function
111 template<typename T> struct PtrHash {
112 static unsigned hash(T key)
115 #pragma warning(push)
116 #pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's conversion warnings
118 return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
123 static bool equal(T a, T b) { return a == b; }
124 static const bool safeToCompareToEmptyOrDeleted = true;
126 template<typename P> struct PtrHash<RefPtr<P> > : PtrHash<P*> {
127 using PtrHash<P*>::hash;
128 static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
129 using PtrHash<P*>::equal;
130 static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
131 static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
132 static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
135 // default hash function for each type
137 template<typename T> struct DefaultHash;
139 template<typename T, typename U> struct PairHash {
140 static unsigned hash(const std::pair<T, U>& p)
142 return intHash((static_cast<uint64_t>(DefaultHash<T>::Hash::hash(p.first)) << 32 | DefaultHash<U>::Hash::hash(p.second)));
144 static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b)
146 return DefaultHash<T>::Hash::equal(a.first, b.first) && DefaultHash<U>::Hash::equal(a.second, b.second);
148 static const bool safeToCompareToEmptyOrDeleted = DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted
149 && DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted;
152 // make IntHash the default hash function for many integer types
154 template<> struct DefaultHash<short> { typedef IntHash<unsigned> Hash; };
155 template<> struct DefaultHash<unsigned short> { typedef IntHash<unsigned> Hash; };
156 template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
157 template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
158 template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
159 template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
160 template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
161 template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };
163 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
164 template<> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; };
167 template<> struct DefaultHash<float> { typedef FloatHash<float> Hash; };
168 template<> struct DefaultHash<double> { typedef FloatHash<double> Hash; };
170 // make PtrHash the default hash function for pointer types that don't specialize
172 template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
173 template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
175 template<typename T, typename U> struct DefaultHash<std::pair<T, U> > { typedef PairHash<T, U> Hash; };
179 using WTF::DefaultHash;
183 #endif // WTF_HashFunctions_h