Changeset 32609 in webkit for trunk/JavaScriptCore/wtf/HashTraits.h
- Timestamp:
- Apr 27, 2008, 10:59:07 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/HashTraits.h
r31335 r32609 1 1 // -*- mode: c++; c-basic-offset: 4 -*- 2 2 /* 3 * This file is part of the KDE libraries 4 * Copyright (C) 2005, 2006 Apple Computer, Inc. 3 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 5 4 * 6 5 * This library is free software; you can redistribute it and/or … … 78 77 79 78 template<bool isInteger, typename T> struct GenericHashTraitsBase; 80 template<typename T> struct GenericHashTraitsBase<true, T> { 81 typedef T TraitType; 82 typedef HashTraits<typename IntTypes<sizeof(T)>::SignedType> StorageTraits; 83 static const bool emptyValueIsZero = true; 84 static const bool needsDestruction = false; 85 }; 79 86 80 template<typename T> struct GenericHashTraitsBase<false, T> { 87 typedef T TraitType;88 typedef HashTraits<T> StorageTraits;89 81 static const bool emptyValueIsZero = false; 90 82 static const bool needsDestruction = true; 91 83 }; 92 84 85 // default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned) 86 template<typename T> struct GenericHashTraitsBase<true, T> { 87 static const bool emptyValueIsZero = true; 88 static const bool needsDestruction = false; 89 static void constructDeletedValue(T* slot) { *slot = static_cast<T>(-1); } 90 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } 91 }; 92 93 93 template<typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> { 94 typedef T TraitType; 95 typedef HashTraits<T> StorageTraits; 94 96 static T emptyValue() { return T(); } 95 97 static const bool needsRef = false; … … 98 100 template<typename T> struct HashTraits : GenericHashTraits<T> { }; 99 101 100 // signed integer traits may not be appropriate for all uses since they disallow 0 and -1 as keys 101 template<> struct HashTraits<signed char> : GenericHashTraits<int> { 102 static signed char deletedValue() { return -1; } 103 }; 104 template<> struct HashTraits<short> : GenericHashTraits<short> { 105 static short deletedValue() { return -1; } 106 }; 107 template<> struct HashTraits<unsigned short> : GenericHashTraits<unsigned short> { 108 static short deletedValue() { return static_cast<unsigned short>(-1); } 109 }; 110 template<> struct HashTraits<int> : GenericHashTraits<int> { 111 static int deletedValue() { return -1; } 112 }; 113 template<> struct HashTraits<unsigned int> : GenericHashTraits<unsigned int> { 114 static unsigned int deletedValue() { return static_cast<unsigned int>(-1); } 115 }; 116 template<> struct HashTraits<long> : GenericHashTraits<long> { 117 static long deletedValue() { return -1; } 118 }; 119 template<> struct HashTraits<unsigned long> : GenericHashTraits<unsigned long> { 120 static unsigned long deletedValue() { return static_cast<unsigned long>(-1); } 121 }; 122 template<> struct HashTraits<long long> : GenericHashTraits<long long> { 123 static long long deletedValue() { return -1; } 124 }; 125 template<> struct HashTraits<unsigned long long> : GenericHashTraits<unsigned long long> { 126 static unsigned long long deletedValue() { return static_cast<unsigned long long>(-1); } 127 }; 128 129 #if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) 130 template<> struct HashTraits<wchar_t> : GenericHashTraits<wchar_t> { 131 static wchar_t deletedValue() { return static_cast<wchar_t>(-1); } 132 }; 133 #endif 134 135 template<typename T> struct FloatHashTraits { 136 typedef T TraitType; 137 typedef HashTraits<T> StorageTraits; 102 template<typename T> struct FloatHashTraits : GenericHashTraits<T> { 103 static const bool needsDestruction = false; 138 104 static T emptyValue() { return std::numeric_limits<T>::infinity(); } 139 static T deletedValue() { return -std::numeric_limits<T>::infinity(); } 140 static const bool emptyValueIsZero = false; 141 static const bool needsDestruction = false; 142 static const bool needsRef = false; 143 }; 144 template<> struct HashTraits<float> : FloatHashTraits<float> { 145 }; 146 template<> struct HashTraits<double> : FloatHashTraits<double> { 105 static void constructDeletedValue(T* slot) { *slot = -std::numeric_limits<T>::infinity(); } 106 static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>::infinity(); } 147 107 }; 148 108 109 template<> struct HashTraits<float> : FloatHashTraits<float> { }; 110 template<> struct HashTraits<double> : FloatHashTraits<double> { }; 111 149 112 template<typename P> struct HashTraits<P*> : GenericHashTraits<P*> { 150 typedef HashTraits<typename IntTypes<sizeof(P*)>::SignedType> StorageTraits;151 113 static const bool emptyValueIsZero = true; 152 114 static const bool needsDestruction = false; 153 static P* deletedValue() { return reinterpret_cast<P*>(-1); } 115 static void constructDeletedValue(P** slot) { *slot = reinterpret_cast<P*>(-1); } 116 static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1); } 154 117 }; 155 118 156 119 template<typename P> struct HashTraits<RefPtr<P> > : GenericHashTraits<RefPtr<P> > { 157 typedef HashTraits<typename IntTypes<sizeof(P*)>::SignedType> StorageTraits;158 typedef typename StorageTraits::TraitType StorageType;159 120 static const bool emptyValueIsZero = true; 160 static const bool needsRef = true; 161 162 typedef union { 163 P* m_p; 164 StorageType m_s; 165 } UnionType; 166 167 static void ref(const StorageType& s) 168 { 169 if (const P* p = reinterpret_cast<const UnionType*>(&s)->m_p) 170 const_cast<P*>(p)->ref(); 171 } 172 static void deref(const StorageType& s) 173 { 174 if (const P* p = reinterpret_cast<const UnionType*>(&s)->m_p) 175 const_cast<P*>(p)->deref(); 176 } 121 static void constructDeletedValue(RefPtr<P>* slot) { new (slot) RefPtr<P>(HashTableDeletedValue); } 122 static bool isDeletedValue(const RefPtr<P>& value) { return value.isHashTableDeletedValue(); } 177 123 }; 178 179 // template to set deleted values180 181 template<typename Traits> struct DeletedValueAssigner {182 static void assignDeletedValue(typename Traits::TraitType& location) { location = Traits::deletedValue(); }183 };184 185 template<typename T, typename Traits> inline void assignDeleted(T& location)186 {187 DeletedValueAssigner<Traits>::assignDeletedValue(location);188 }189 124 190 125 // special traits for pairs, helpful for their use in HashMap implementation … … 193 128 194 129 template<typename FirstTraitsArg, typename SecondTraitsArg> 195 struct Pair BaseHashTraits : GenericHashTraits<pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > {130 struct PairHashTraits : GenericHashTraits<pair<typename FirstTraitsArg::TraitType, typename SecondTraitsArg::TraitType> > { 196 131 typedef FirstTraitsArg FirstTraits; 197 132 typedef SecondTraitsArg SecondTraits; 198 133 typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType; 199 134 200 typedef PairHashTraits<typename FirstTraits::StorageTraits, typename SecondTraits::StorageTraits> StorageTraits; 135 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; 136 static TraitType emptyValue() { return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } 201 137 202 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;138 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; 203 139 204 static TraitType emptyValue() 205 { 206 return make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); 207 } 140 static void constructDeletedValue(TraitType* slot) { FirstTraits::constructDeletedValue(&slot->first); } 141 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); } 208 142 }; 209 143 210 144 template<typename FirstTraits, typename SecondTraits> 211 struct PairHashTraits : PairBaseHashTraits<FirstTraits, SecondTraits> { 212 typedef pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType> TraitType; 213 214 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; 215 216 static TraitType deletedValue() 217 { 218 return TraitType(FirstTraits::deletedValue(), SecondTraits::emptyValue()); 219 } 220 221 static void assignDeletedValue(TraitType& location) 222 { 223 assignDeleted<typename FirstTraits::TraitType, FirstTraits>(location.first); 224 location.second = SecondTraits::emptyValue(); 225 } 226 }; 145 struct PairBaseHashTraits : PairHashTraits<FirstTraits, SecondTraits> { }; 227 146 228 147 template<typename First, typename Second> 229 148 struct HashTraits<pair<First, Second> > : public PairHashTraits<HashTraits<First>, HashTraits<Second> > { }; 230 149 231 template<typename FirstTraits, typename SecondTraits> 232 struct DeletedValueAssigner<PairHashTraits<FirstTraits, SecondTraits> > 233 { 234 static void assignDeletedValue(pair<typename FirstTraits::TraitType, typename SecondTraits::TraitType>& location) 235 { 236 PairHashTraits<FirstTraits, SecondTraits>::assignDeletedValue(location); 237 } 238 }; 239 240 template<typename First, typename Second> 241 struct DeletedValueAssigner<HashTraits<pair<First, Second> > > 242 { 243 static void assignDeletedValue(pair<First, Second>& location) 244 { 245 HashTraits<pair<First, Second> >::assignDeletedValue(location); 246 } 247 }; 248 249 // hash functions and traits that are equivalent (for code sharing) 250 150 // obsolete code sharing traits -- to be deleted 251 151 template<typename HashArg, typename TraitsArg> struct HashKeyStorageTraits { 252 152 typedef HashArg Hash; 253 153 typedef TraitsArg Traits; 254 };255 template<typename P> struct HashKeyStorageTraits<PtrHash<P*>, HashTraits<P*> > {256 typedef typename IntTypes<sizeof(P*)>::SignedType IntType;257 typedef IntHash<IntType> Hash;258 typedef HashTraits<IntType> Traits;259 };260 template<typename P> struct HashKeyStorageTraits<PtrHash<RefPtr<P> >, HashTraits<RefPtr<P> > > {261 typedef typename IntTypes<sizeof(P*)>::SignedType IntType;262 typedef IntHash<IntType> Hash;263 typedef HashTraits<IntType> Traits;264 154 }; 265 155
Note:
See TracChangeset
for help on using the changeset viewer.