1 /* 2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * 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. 8 * 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. 13 * 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. 18 * 19 */ 20 21 #ifndef WTF_HashTraits_h 22 #define WTF_HashTraits_h 23 24 #include "HashFunctions.h" 25 #include "TypeTraits.h" 26 #include <utility> 27 #include <limits> 28 29 namespace WTF { 30 31 using std::pair; 32 using std::make_pair; 33 34 template<typename T> struct HashTraits; 35 36 template<bool isInteger, typename T> struct GenericHashTraitsBase; 37 38 template<typename T> struct GenericHashTraitsBase<false, T> { 39 static const bool emptyValueIsZero = false; 40 static const bool needsDestruction = true; 41 }; 42 43 // Default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned). 44 template<typename T> struct GenericHashTraitsBase<true, T> { 45 static const bool emptyValueIsZero = true; 46 static const bool needsDestruction = false; 47 static void constructDeletedValue(T& slot) { slot = static_cast<T>(-1); } 48 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } 49 }; 50 51 template<typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> { 52 typedef T TraitType; 53 static T emptyValue() { return T(); } 54 }; 55 56 template<typename T> struct HashTraits : GenericHashTraits<T> { }; 57 58 template<typename T> struct FloatHashTraits : GenericHashTraits<T> { 59 static const bool needsDestruction = false; 60 static T emptyValue() { return std::numeric_limits<T>::infinity(); } 61 static void constructDeletedValue(T& slot) { slot = -std::numeric_limits<T>::infinity(); } 62 static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>::infinity(); } 63 }; 64 65 template<> struct HashTraits<float> : FloatHashTraits<float> { }; 66 template<> struct HashTraits<double> : FloatHashTraits<double> { }; 67 68 // Default unsigned traits disallow both 0 and max as keys -- use these traits to allow zero and disallow max - 1. 69 template<typename T> struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T> { 70 static const bool emptyValueIsZero = false; 71 static const bool needsDestruction = false; 72 static T emptyValue() { return std::numeric_limits<T>::max(); } 73 static void constructDeletedValue(T& slot) { slot = std::numeric_limits<T>::max() - 1; } 74 static bool isDeletedValue(T value) { return value == std::numeric_limits<T>::max() - 1; } 75 }; 76 77 template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> { 78 static const bool emptyValueIsZero = true; 79 static const bool needsDestruction = false; 80 static void constructDeletedValue(P*& slot) { slot = reinterpret_cast<P*>(-1); } 81 static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1); } 82 }; 83 84 template<typename T> struct SimpleClassHashTraits : GenericHashTraits<T> { 85 static const bool emptyValueIsZero = true; 86 static void constructDeletedValue(T& slot) { new (&slot) T(HashTableDeletedValue); } 87 static bool isDeletedValue(const T& value) { return value.isHashTableDeletedValue(); } 88 }; 89 90 template<typename P> struct HashTraits<RefPtr<P> > : SimpleClassHashTraits<RefPtr<P> > { }; 91 92 // special traits for pairs, helpful for their use in HashMap implementation 93 94 template<typename FirstTraitsArg, typename SecondTraitsArg> 95 struct PairHashTraits : GenericHashTraits<pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > { 96 typedef FirstTraitsArg FirstTraits; 97 typedef SecondTraitsArg SecondTraits; 98 typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType; 99 100 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; 101 static TraitType emptyValue() { return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } 102 103 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; 104 105 static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); } 106 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); } 107 }; 108 109 template<typename First, typename Second> 110 struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > { }; 111 112 } // namespace WTF 113 114 using WTF::HashTraits; 115 using WTF::PairHashTraits; 116 117 #endif // WTF_HashTraits_h 118