WTF
HashTraits.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 #ifndef WTF_HashTraits_h
00023 #define WTF_HashTraits_h
00024
00025 #include "Assertions.h"
00026 #include "HashFunctions.h"
00027 #include <utility>
00028 #include <limits>
00029
00030 namespace WTF {
00031
00032 using std::pair;
00033 using std::make_pair;
00034
00035 template<typename T> struct IsInteger { static const bool value = false; };
00036 template<> struct IsInteger<bool> { static const bool value = true; };
00037 template<> struct IsInteger<char> { static const bool value = true; };
00038 template<> struct IsInteger<signed char> { static const bool value = true; };
00039 template<> struct IsInteger<unsigned char> { static const bool value = true; };
00040 template<> struct IsInteger<short> { static const bool value = true; };
00041 template<> struct IsInteger<unsigned short> { static const bool value = true; };
00042 template<> struct IsInteger<int> { static const bool value = true; };
00043 template<> struct IsInteger<unsigned int> { static const bool value = true; };
00044 template<> struct IsInteger<long> { static const bool value = true; };
00045 template<> struct IsInteger<unsigned long> { static const bool value = true; };
00046 template<> struct IsInteger<long long> { static const bool value = true; };
00047 template<> struct IsInteger<unsigned long long> { static const bool value = true; };
00048
00049 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
00050 template<> struct IsInteger<wchar_t> { static const bool value = true; };
00051 #endif
00052
00053 COMPILE_ASSERT(IsInteger<bool>::value, WTF_IsInteger_bool_true)
00054 COMPILE_ASSERT(IsInteger<char>::value, WTF_IsInteger_char_true)
00055 COMPILE_ASSERT(IsInteger<signed char>::value, WTF_IsInteger_signed_char_true)
00056 COMPILE_ASSERT(IsInteger<unsigned char>::value, WTF_IsInteger_unsigned_char_true)
00057 COMPILE_ASSERT(IsInteger<short>::value, WTF_IsInteger_short_true)
00058 COMPILE_ASSERT(IsInteger<unsigned short>::value, WTF_IsInteger_unsigned_short_true)
00059 COMPILE_ASSERT(IsInteger<int>::value, WTF_IsInteger_int_true)
00060 COMPILE_ASSERT(IsInteger<unsigned int>::value, WTF_IsInteger_unsigned_int_true)
00061 COMPILE_ASSERT(IsInteger<long>::value, WTF_IsInteger_long_true)
00062 COMPILE_ASSERT(IsInteger<unsigned long>::value, WTF_IsInteger_unsigned_long_true)
00063 COMPILE_ASSERT(IsInteger<long long>::value, WTF_IsInteger_long_long_true)
00064 COMPILE_ASSERT(IsInteger<unsigned long long>::value, WTF_IsInteger_unsigned_long_long_true)
00065
00066 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
00067 COMPILE_ASSERT(IsInteger<wchar_t>::value, WTF_IsInteger_wchar_t_true)
00068 #endif
00069
00070 COMPILE_ASSERT(!IsInteger<char*>::value, WTF_IsInteger_char_pointer_false)
00071 COMPILE_ASSERT(!IsInteger<const char* >::value, WTF_IsInteger_const_char_pointer_false)
00072 COMPILE_ASSERT(!IsInteger<volatile char* >::value, WTF_IsInteger_volatile_char_pointer__false)
00073 COMPILE_ASSERT(!IsInteger<double>::value, WTF_IsInteger_double_false)
00074 COMPILE_ASSERT(!IsInteger<float>::value, WTF_IsInteger_float_false)
00075
00076 template<typename T> struct HashTraits;
00077
00078 template<bool isInteger, typename T> struct GenericHashTraitsBase;
00079
00080 template<typename T> struct GenericHashTraitsBase<false, T> {
00081 static const bool emptyValueIsZero = false;
00082 static const bool needsDestruction = true;
00083 };
00084
00085
00086 template<typename T> struct GenericHashTraitsBase<true, T> {
00087 static const bool emptyValueIsZero = true;
00088 static const bool needsDestruction = false;
00089 static void constructDeletedValue(T* slot) { *slot = static_cast<T>(-1); }
00090 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); }
00091 };
00092
00093 template<typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> {
00094 typedef T TraitType;
00095 static T emptyValue() { return T(); }
00096 };
00097
00098 template<typename T> struct HashTraits : GenericHashTraits<T> { };
00099
00100 template<typename T> struct FloatHashTraits : GenericHashTraits<T> {
00101 static const bool needsDestruction = false;
00102 static T emptyValue() { return std::numeric_limits<T>::infinity(); }
00103 static void constructDeletedValue(T* slot) { *slot = -std::numeric_limits<T>::infinity(); }
00104 static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>::infinity(); }
00105 };
00106
00107 template<> struct HashTraits<float> : FloatHashTraits<float> { };
00108 template<> struct HashTraits<double> : FloatHashTraits<double> { };
00109
00110 template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> {
00111 static const bool emptyValueIsZero = true;
00112 static const bool needsDestruction = false;
00113 static void constructDeletedValue(P** slot) { *slot = reinterpret_cast<P*>(-1); }
00114 static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1); }
00115 };
00116
00117 template<typename P> struct HashTraits<RefPtr<P> > : GenericHashTraits<RefPtr<P> > {
00118 static const bool emptyValueIsZero = true;
00119 static void constructDeletedValue(RefPtr<P>* slot) { new (slot) RefPtr<P>(HashTableDeletedValue); }
00120 static bool isDeletedValue(const RefPtr<P>& value) { return value.isHashTableDeletedValue(); }
00121 };
00122
00123
00124
00125 template<typename FirstTraitsArg, typename SecondTraitsArg>
00126 struct PairHashTraits : GenericHashTraits<pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > {
00127 typedef FirstTraitsArg FirstTraits;
00128 typedef SecondTraitsArg SecondTraits;
00129 typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType;
00130
00131 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
00132 static TraitType emptyValue() { return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
00133
00134 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
00135
00136 static void constructDeletedValue(TraitType* slot) { FirstTraits::constructDeletedValue(&slot->first); }
00137 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); }
00138 };
00139
00140 template<typename First, typename Second>
00141 struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > { };
00142
00143 }
00144
00145 using WTF::HashTraits;
00146 using WTF::PairHashTraits;
00147
00148 #endif // WTF_HashTraits_h