Changeset 32650 in webkit for trunk/JavaScriptCore/wtf/HashMap.h
- Timestamp:
- Apr 28, 2008, 10:18:15 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/HashMap.h
r32609 r32650 35 35 typedef KeyTraitsArg KeyTraits; 36 36 typedef MappedTraitsArg MappedTraits; 37 typedef Pair BaseHashTraits<KeyTraits, MappedTraits> ValueTraits;37 typedef PairHashTraits<KeyTraits, MappedTraits> ValueTraits; 38 38 39 39 public: … … 45 45 typedef HashArg HashFunctions; 46 46 47 typedef typename HashKeyStorageTraits<HashFunctions, KeyTraits>::Hash StorageHashFunctions; 48 49 typedef typename HashKeyStorageTraits<HashFunctions, KeyTraits>::Traits KeyStorageTraits; 50 typedef typename MappedTraits::StorageTraits MappedStorageTraits; 51 typedef PairHashTraits<KeyStorageTraits, MappedStorageTraits> ValueStorageTraits; 52 53 typedef typename KeyStorageTraits::TraitType KeyStorageType; 54 typedef typename MappedStorageTraits::TraitType MappedStorageType; 55 typedef typename ValueStorageTraits::TraitType ValueStorageType; 56 57 typedef HashTable<KeyStorageType, ValueStorageType, PairFirstExtractor<ValueStorageType>, 58 StorageHashFunctions, ValueStorageTraits, KeyStorageTraits> HashTableType; 47 typedef HashTable<KeyType, ValueType, PairFirstExtractor<ValueType>, 48 HashFunctions, ValueTraits, KeyTraits> HashTableType; 59 49 60 50 public: 61 51 typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator; 62 52 typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator; 63 64 HashMap();65 HashMap(const HashMap&);66 HashMap& operator=(const HashMap&);67 ~HashMap();68 53 69 54 void swap(HashMap&); … … 102 87 private: 103 88 pair<iterator, bool> inlineAdd(const KeyType&, const MappedType&); 104 void refAll();105 void derefAll();106 89 107 90 HashTableType m_impl; … … 112 95 }; 113 96 114 template<bool canReplaceDeletedKey, typename ValueType, typename ValueTraits, typename ValueStorageTraits, typename HashFunctions> 115 struct HashMapTranslator; 116 117 template<typename ValueType, typename ValueTraits, typename ValueStorageTraits, typename HashFunctions> 118 struct HashMapTranslator<true, ValueType, ValueTraits, ValueStorageTraits, HashFunctions> { 97 template<typename ValueType, typename ValueTraits, typename HashFunctions> 98 struct HashMapTranslator { 119 99 typedef typename ValueType::first_type KeyType; 120 100 typedef typename ValueType::second_type MappedType; 121 typedef typename ValueStorageTraits::TraitType ValueStorageType;122 typedef typename ValueStorageTraits::FirstTraits KeyStorageTraits;123 typedef typename KeyStorageTraits::TraitType KeyStorageType;124 typedef typename ValueStorageTraits::SecondTraits MappedStorageTraits;125 typedef typename MappedStorageTraits::TraitType MappedStorageType;126 typedef typename ValueTraits::FirstTraits KeyTraits;127 typedef typename ValueTraits::SecondTraits MappedTraits;128 101 129 102 static unsigned hash(const KeyType& key) { return HashFunctions::hash(key); } 130 static bool equal(const Key StorageType& a, const KeyType& b) { return HashFunctions::equal(*(KeyType*)&a, b); }131 static void translate(Value StorageType& location, const KeyType& key, const MappedType& mapped)103 static bool equal(const KeyType& a, const KeyType& b) { return HashFunctions::equal(a, b); } 104 static void translate(ValueType& location, const KeyType& key, const MappedType& mapped) 132 105 { 133 106 location.first = key; … … 136 109 }; 137 110 138 template<typename ValueType, typename ValueTraits, typename ValueStorageTraits, typename HashFunctions>139 struct HashMapTranslator<false, ValueType, ValueTraits, ValueStorageTraits, HashFunctions> {140 typedef typename ValueType::first_type KeyType;141 typedef typename ValueType::second_type MappedType;142 typedef typename ValueStorageTraits::TraitType ValueStorageType;143 typedef typename ValueStorageTraits::FirstTraits KeyStorageTraits;144 typedef typename KeyStorageTraits::TraitType KeyStorageType;145 typedef typename ValueStorageTraits::SecondTraits MappedStorageTraits;146 typedef typename MappedStorageTraits::TraitType MappedStorageType;147 typedef typename ValueTraits::FirstTraits KeyTraits;148 typedef typename ValueTraits::SecondTraits MappedTraits;149 150 static unsigned hash(const KeyType& key) { return HashFunctions::hash(key); }151 static bool equal(const KeyStorageType& a, const KeyType& b) { return HashFunctions::equal(*(KeyType*)&a, b); }152 static void translate(ValueStorageType& location, const KeyType& key, const MappedType& mapped)153 {154 location.first = key;155 location.second = mapped;156 }157 };158 159 template<typename T, typename U, typename V, typename W, typename X>160 inline void HashMap<T, U, V, W, X>::refAll()161 {162 HashTableRefCounter<HashTableType, ValueTraits>::refAll(m_impl);163 }164 165 template<typename T, typename U, typename V, typename W, typename X>166 inline void HashMap<T, U, V, W, X>::derefAll()167 {168 HashTableRefCounter<HashTableType, ValueTraits>::derefAll(m_impl);169 }170 171 template<typename T, typename U, typename V, typename W, typename X>172 inline HashMap<T, U, V, W, X>::HashMap()173 {174 }175 176 template<typename T, typename U, typename V, typename W, typename X>177 inline HashMap<T, U, V, W, X>::HashMap(const HashMap& other)178 : m_impl(other.m_impl)179 {180 refAll();181 }182 183 template<typename T, typename U, typename V, typename W, typename X>184 inline HashMap<T, U, V, W, X>& HashMap<T, U, V, W, X>::operator=(const HashMap& other)185 {186 HashMap tmp(other);187 swap(tmp);188 return *this;189 }190 191 111 template<typename T, typename U, typename V, typename W, typename X> 192 112 inline void HashMap<T, U, V, W, X>::swap(HashMap& other) 193 113 { 194 114 m_impl.swap(other.m_impl); 195 }196 197 template<typename T, typename U, typename V, typename W, typename X>198 inline HashMap<T, U, V, W, X>::~HashMap()199 {200 derefAll();201 115 } 202 116 … … 246 160 inline typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::find(const KeyType& key) 247 161 { 248 return m_impl.find( *(const KeyStorageType*)&key);162 return m_impl.find(key); 249 163 } 250 164 … … 252 166 inline typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::find(const KeyType& key) const 253 167 { 254 return m_impl.find( *(const KeyStorageType*)&key);168 return m_impl.find(key); 255 169 } 256 170 … … 258 172 inline bool HashMap<T, U, V, W, X>::contains(const KeyType& key) const 259 173 { 260 return m_impl.contains( *(const KeyStorageType*)&key);174 return m_impl.contains(key); 261 175 } 262 176 … … 265 179 HashMap<T, U, V, W, X>::inlineAdd(const KeyType& key, const MappedType& mapped) 266 180 { 267 const bool canReplaceDeletedKey = !KeyTraits::needsDestruction || KeyStorageTraits::needsDestruction; 268 typedef HashMapTranslator<canReplaceDeletedKey, ValueType, ValueTraits, ValueStorageTraits, HashFunctions> TranslatorType; 181 typedef HashMapTranslator<ValueType, ValueTraits, HashFunctions> TranslatorType; 269 182 return m_impl.template add<KeyType, MappedType, TranslatorType>(key, mapped); 270 183 } … … 275 188 { 276 189 pair<iterator, bool> result = inlineAdd(key, mapped); 277 if (!result.second) 190 if (!result.second) { 278 191 // add call above didn't change anything, so set the mapped value 279 192 result.first->second = mapped; 193 } 280 194 return result; 281 195 } … … 292 206 HashMap<T, U, V, W, MappedTraits>::get(const KeyType& key) const 293 207 { 294 Value StorageType* entry = const_cast<HashTableType&>(m_impl).lookup(*(const KeyStorageType*)&key);208 ValueType* entry = const_cast<HashTableType&>(m_impl).lookup(key); 295 209 if (!entry) 296 210 return MappedTraits::emptyValue(); 297 return ((ValueType *)entry)->second;211 return entry->second; 298 212 } 299 213 … … 304 218 return; 305 219 m_impl.checkTableConsistency(); 306 RefCounter<ValueTraits, ValueStorageTraits>::deref(*it.m_impl);307 220 m_impl.removeWithoutEntryConsistencyCheck(it.m_impl); 308 221 } … … 317 230 inline void HashMap<T, U, V, W, X>::clear() 318 231 { 319 derefAll();320 232 m_impl.clear(); 321 233 } … … 365 277 iterator end = collection.end(); 366 278 for (iterator it = collection.begin(); it != end; ++it) 367 delete *(MappedType*)&it->second;279 delete it->second; 368 280 } 369 281 … … 380 292 iterator end = collection.end(); 381 293 for (iterator it = collection.begin(); it != end; ++it) 382 delete *(KeyType*)&it->first;294 delete it->first; 383 295 } 384 296
Note:
See TracChangeset
for help on using the changeset viewer.