WTF
HashTraits.h
Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 4 -*- 00002 /* 00003 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 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 // default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned) 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 // special traits for pairs, helpful for their use in HashMap implementation 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 } // namespace WTF 00144 00145 using WTF::HashTraits; 00146 using WTF::PairHashTraits; 00147 00148 #endif // WTF_HashTraits_h
KDE 4.6 API Reference