Changeset 32650 in webkit for trunk/JavaScriptCore/wtf/HashSet.h
- Timestamp:
- Apr 28, 2008, 10:18:15 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/HashSet.h
r31122 r32650 27 27 namespace WTF { 28 28 29 template<typename Value, typename HashFunctions, typename Traits> class HashSet; 30 template<typename Value, typename HashFunctions, typename Traits> 31 void deleteAllValues(const HashSet<Value, HashFunctions, Traits>&); 32 29 33 template<typename T> struct IdentityExtractor; 30 31 template<typename Value, typename HashFunctions, typename Traits> class HashSet;32 template<typename Value, typename HashFunctions, typename Traits>33 void deleteAllValues(const HashSet<Value, HashFunctions, Traits>&);34 34 35 35 template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash, … … 39 39 typedef TraitsArg ValueTraits; 40 40 41 typedef typename HashKeyStorageTraits<HashFunctions, ValueTraits>::Hash StorageHashFunctions;42 43 typedef typename HashKeyStorageTraits<HashFunctions, ValueTraits>::Traits StorageTraits;44 typedef typename StorageTraits::TraitType StorageType;45 46 typedef HashTable<StorageType, StorageType, IdentityExtractor<StorageType>,47 StorageHashFunctions, StorageTraits, StorageTraits> HashTableType;48 49 41 public: 50 42 typedef typename ValueTraits::TraitType ValueType; 43 44 private: 45 typedef HashTable<ValueType, ValueType, IdentityExtractor<ValueType>, 46 HashFunctions, ValueTraits, ValueTraits> HashTableType; 47 48 public: 51 49 typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator; 52 50 typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator; 53 54 HashSet();55 HashSet(const HashSet&);56 HashSet& operator=(const HashSet&);57 ~HashSet();58 51 59 52 void swap(HashSet&); … … 98 91 99 92 private: 100 void refAll();101 void derefAll();102 103 93 friend void deleteAllValues<>(const HashSet&); 104 94 … … 110 100 }; 111 101 112 template<bool canReplaceDeletedValue, typename ValueType, typename ValueTraits, typename StorageTraits, typename HashFunctions> 113 struct HashSetTranslator; 114 115 template<typename ValueType, typename ValueTraits, typename StorageTraits, typename HashFunctions> 116 struct HashSetTranslator<true, ValueType, ValueTraits, StorageTraits, HashFunctions> { 117 typedef typename StorageTraits::TraitType StorageType; 118 static unsigned hash(const ValueType& key) { return HashFunctions::hash(key); } 119 static bool equal(const StorageType& a, const ValueType& b) { return HashFunctions::equal(*(const ValueType*)&a, b); } 120 static void translate(StorageType& location, const ValueType& key, const ValueType&) 102 template<typename ValueType, typename ValueTraits, typename T, typename Translator> 103 struct HashSetTranslatorAdapter { 104 static unsigned hash(const T& key) { return Translator::hash(key); } 105 static bool equal(const ValueType& a, const T& b) { return Translator::equal(a, b); } 106 static void translate(ValueType& location, const T& key, const T&, unsigned hashCode) 121 107 { 122 Assigner<ValueTraits::needsRef, ValueType, StorageType, ValueTraits>::assign(key, location);108 Translator::translate(location, key, hashCode); 123 109 } 124 110 }; 125 111 126 template<typename ValueType, typename ValueTraits, typename StorageTraits, typename HashFunctions>127 struct HashSetTranslator<false, ValueType, ValueTraits, StorageTraits, HashFunctions> {128 typedef typename StorageTraits::TraitType StorageType;129 static unsigned hash(const ValueType& key) { return HashFunctions::hash(key); }130 static bool equal(const StorageType& a, const ValueType& b) { return HashFunctions::equal(*(const ValueType*)&a, b); }131 static void translate(StorageType& location, const ValueType& key, const ValueType&)132 {133 if (location == StorageTraits::deletedValue())134 location = StorageTraits::emptyValue();135 Assigner<ValueTraits::needsRef, ValueType, StorageType, ValueTraits>::assign(key, location);136 }137 };138 139 template<bool canReplaceDeletedValue, typename ValueType, typename StorageTraits, typename T, typename Translator>140 struct HashSetTranslatorAdapter;141 142 template<typename ValueType, typename StorageTraits, typename T, typename Translator>143 struct HashSetTranslatorAdapter<true, ValueType, StorageTraits, T, Translator> {144 typedef typename StorageTraits::TraitType StorageType;145 static unsigned hash(const T& key) { return Translator::hash(key); }146 static bool equal(const StorageType& a, const T& b) { return Translator::equal(*(const ValueType*)&a, b); }147 static void translate(StorageType& location, const T& key, const T&, unsigned hashCode)148 {149 Translator::translate(*(ValueType*)&location, key, hashCode);150 }151 };152 153 template<typename ValueType, typename StorageTraits, typename T, typename Translator>154 struct HashSetTranslatorAdapter<false, ValueType, StorageTraits, T, Translator> {155 typedef typename StorageTraits::TraitType StorageType;156 static unsigned hash(const T& key) { return Translator::hash(key); }157 static bool equal(const StorageType& a, const T& b) { return Translator::equal(*(const ValueType*)&a, b); }158 static void translate(StorageType& location, const T& key, const T&, unsigned hashCode)159 {160 if (location == StorageTraits::deletedValue())161 location = StorageTraits::emptyValue();162 Translator::translate(*(ValueType*)&location, key, hashCode);163 }164 };165 166 template<typename T, typename U, typename V>167 inline void HashSet<T, U, V>::refAll()168 {169 HashTableRefCounter<HashTableType, ValueTraits>::refAll(m_impl);170 }171 172 template<typename T, typename U, typename V>173 inline void HashSet<T, U, V>::derefAll()174 {175 HashTableRefCounter<HashTableType, ValueTraits>::derefAll(m_impl);176 }177 178 template<typename T, typename U, typename V>179 inline HashSet<T, U, V>::HashSet()180 {181 }182 183 template<typename T, typename U, typename V>184 inline HashSet<T, U, V>::HashSet(const HashSet& other)185 : m_impl(other.m_impl)186 {187 refAll();188 }189 190 template<typename T, typename U, typename V>191 inline HashSet<T, U, V>& HashSet<T, U, V>::operator=(const HashSet& other)192 {193 HashSet tmp(other);194 swap(tmp);195 return *this;196 }197 198 112 template<typename T, typename U, typename V> 199 113 inline void HashSet<T, U, V>::swap(HashSet& other) … … 203 117 204 118 template<typename T, typename U, typename V> 205 inline HashSet<T, U, V>::~HashSet()206 {207 derefAll();208 }209 210 template<typename T, typename U, typename V>211 119 inline int HashSet<T, U, V>::size() const 212 120 { … … 253 161 inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::find(const ValueType& value) 254 162 { 255 return m_impl.find( *(const StorageType*)&value);163 return m_impl.find(value); 256 164 } 257 165 … … 259 167 inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::find(const ValueType& value) const 260 168 { 261 return m_impl.find( *(const StorageType*)&value);169 return m_impl.find(value); 262 170 } 263 171 … … 265 173 inline bool HashSet<T, U, V>::contains(const ValueType& value) const 266 174 { 267 return m_impl.contains( *(const StorageType*)&value);175 return m_impl.contains(value); 268 176 } 269 177 … … 273 181 inline HashSet<Value, HashFunctions, Traits>::find(const T& value) 274 182 { 275 const bool canReplaceDeletedValue = !ValueTraits::needsDestruction || StorageTraits::needsDestruction; 276 typedef HashSetTranslatorAdapter<canReplaceDeletedValue, ValueType, StorageTraits, T, Translator> Adapter; 183 typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter; 277 184 return m_impl.find<T, Adapter>(value); 278 185 } … … 283 190 inline HashSet<Value, HashFunctions, Traits>::find(const T& value) const 284 191 { 285 const bool canReplaceDeletedValue = !ValueTraits::needsDestruction || StorageTraits::needsDestruction; 286 typedef HashSetTranslatorAdapter<canReplaceDeletedValue, ValueType, StorageTraits, T, Translator> Adapter; 192 typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter; 287 193 return m_impl.find<T, Adapter>(value); 288 194 } … … 292 198 inline bool HashSet<Value, HashFunctions, Traits>::contains(const T& value) const 293 199 { 294 const bool canReplaceDeletedValue = !ValueTraits::needsDestruction || StorageTraits::needsDestruction; 295 typedef HashSetTranslatorAdapter<canReplaceDeletedValue, ValueType, StorageTraits, T, Translator> Adapter; 200 typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter; 296 201 return m_impl.contains<T, Adapter>(value); 297 202 } 298 203 299 204 template<typename T, typename U, typename V> 300 pair<typename HashSet<T, U, V>::iterator, bool> HashSet<T, U, V>::add(const ValueType &value) 301 { 302 const bool canReplaceDeletedValue = !ValueTraits::needsDestruction || StorageTraits::needsDestruction; 303 typedef HashSetTranslator<canReplaceDeletedValue, ValueType, ValueTraits, StorageTraits, HashFunctions> Translator; 304 return m_impl.template add<ValueType, ValueType, Translator>(value, value); 205 pair<typename HashSet<T, U, V>::iterator, bool> HashSet<T, U, V>::add(const ValueType& value) 206 { 207 return m_impl.add(value); 305 208 } 306 209 … … 310 213 HashSet<Value, HashFunctions, Traits>::add(const T& value) 311 214 { 312 const bool canReplaceDeletedValue = !ValueTraits::needsDestruction || StorageTraits::needsDestruction; 313 typedef HashSetTranslatorAdapter<canReplaceDeletedValue, ValueType, StorageTraits, T, Translator> Adapter; 215 typedef HashSetTranslatorAdapter<ValueType, ValueTraits, T, Translator> Adapter; 314 216 return m_impl.template addPassingHashCode<T, T, Adapter>(value, value); 315 217 } … … 321 223 return; 322 224 m_impl.checkTableConsistency(); 323 RefCounter<ValueTraits, StorageTraits>::deref(*it.m_impl);324 225 m_impl.removeWithoutEntryConsistencyCheck(it.m_impl); 325 226 } … … 334 235 inline void HashSet<T, U, V>::clear() 335 236 { 336 derefAll();337 237 m_impl.clear(); 338 238 } … … 344 244 iterator end = collection.end(); 345 245 for (iterator it = collection.begin(); it != end; ++it) 346 delete * (ValueType*)&*it;246 delete *it; 347 247 } 348 248
Note:
See TracChangeset
for help on using the changeset viewer.