Changeset 101806 in webkit for trunk/Source/JavaScriptCore/wtf/RefPtrHashMap.h
- Timestamp:
- Dec 2, 2011, 8:50:32 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/wtf/RefPtrHashMap.h
r76248 r101806 1 1 /* 2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.2 * Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. 3 3 * 4 4 * This library is free software; you can redistribute it and/or … … 24 24 namespace WTF { 25 25 26 // This specialization is a direct copy of HashMap, with overloaded functions26 // This specialization is a copy of HashMap for use with RefPtr keys, with overloaded functions 27 27 // to allow for lookup by pointer instead of RefPtr, avoiding ref-count churn. 28 28 29 // FIXME: Find a better way that doesn't require an entirecopy of the HashMap template.29 // FIXME: Find a way to do this with traits that doesn't require a copy of the HashMap template. 30 30 31 template<typename RawKeyType, typename ValueType, typename ValueTraits, typename HashFunctions>32 struct RefPtrHashMapRawKeyTranslator {33 typedef typename ValueType::first_type KeyType;34 typedef typename ValueType::second_type MappedType;35 typedef typename ValueTraits::FirstTraits KeyTraits;36 typedef typename ValueTraits::SecondTraits MappedTraits;37 38 static unsigned hash(RawKeyType key) { return HashFunctions::hash(key); }39 static bool equal(const KeyType& a, RawKeyType b) { return HashFunctions::equal(a, b); }40 static void translate(ValueType& location, RawKeyType key, const MappedType& mapped)41 {42 location.first = key;43 location.second = mapped;44 }45 };46 47 31 template<typename T, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg> 48 32 class HashMap<RefPtr<T>, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> { … … 60 44 61 45 private: 46 typedef const MappedType& MappedPassInType; 47 typedef MappedType MappedPassOutType; 48 typedef MappedType MappedPeekType; 49 62 50 typedef HashArg HashFunctions; 63 51 … … 65 53 HashFunctions, ValueTraits, KeyTraits> HashTableType; 66 54 67 typedef RefPtrHashMapRawKeyTranslator<RawKeyType, ValueType,ValueTraits, HashFunctions>68 RawKeyTranslator;55 typedef HashMapTranslator<ValueTraits, HashFunctions> 56 Translator; 69 57 70 58 public: … … 90 78 bool contains(const KeyType&) const; 91 79 bool contains(RawKeyType) const; 92 Mapped Type get(const KeyType&) const;93 Mapped Type get(RawKeyType) const;94 Mapped Type inlineGet(RawKeyType) const;80 MappedPeekType get(const KeyType&) const; 81 MappedPeekType get(RawKeyType) const; 82 MappedPeekType inlineGet(RawKeyType) const; 95 83 96 84 // replaces value but not key if key is already present 97 85 // return value is a pair of the iterator to the key location, 98 86 // and a boolean that's true if a new value was actually added 99 pair<iterator, bool> set(const KeyType&, const MappedType&);100 pair<iterator, bool> set(RawKeyType, const MappedType&);87 pair<iterator, bool> set(const KeyType&, MappedPassInType); 88 pair<iterator, bool> set(RawKeyType, MappedPassInType); 101 89 102 90 // does nothing if key is already present 103 91 // return value is a pair of the iterator to the key location, 104 92 // and a boolean that's true if a new value was actually added 105 pair<iterator, bool> add(const KeyType&, const MappedType&);106 pair<iterator, bool> add(RawKeyType, const MappedType&);93 pair<iterator, bool> add(const KeyType&, MappedPassInType); 94 pair<iterator, bool> add(RawKeyType, MappedPassInType); 107 95 108 96 void remove(const KeyType&); … … 111 99 void clear(); 112 100 113 Mapped Type take(const KeyType&); // efficient combination of get with remove114 Mapped Type take(RawKeyType); // efficient combination of get with remove101 MappedPassOutType take(const KeyType&); // efficient combination of get with remove 102 MappedPassOutType take(RawKeyType); // efficient combination of get with remove 115 103 116 104 private: 117 pair<iterator, bool> inlineAdd(const KeyType&, const MappedType&);118 pair<iterator, bool> inlineAdd(RawKeyType, const MappedType&);105 pair<iterator, bool> inlineAdd(const KeyType&, MappedPassInType); 106 pair<iterator, bool> inlineAdd(RawKeyType, MappedPassInType); 119 107 120 108 HashTableType m_impl; … … 178 166 inline typename HashMap<RefPtr<T>, U, V, W, X>::iterator HashMap<RefPtr<T>, U, V, W, X>::find(RawKeyType key) 179 167 { 180 return m_impl.template find< RawKeyType, RawKeyTranslator>(key);168 return m_impl.template find<Translator>(key); 181 169 } 182 170 … … 190 178 inline typename HashMap<RefPtr<T>, U, V, W, X>::const_iterator HashMap<RefPtr<T>, U, V, W, X>::find(RawKeyType key) const 191 179 { 192 return m_impl.template find< RawKeyType, RawKeyTranslator>(key);180 return m_impl.template find<Translator>(key); 193 181 } 194 182 … … 202 190 inline bool HashMap<RefPtr<T>, U, V, W, X>::contains(RawKeyType key) const 203 191 { 204 return m_impl.template contains< RawKeyType, RawKeyTranslator>(key);192 return m_impl.template contains<Translator>(key); 205 193 } 206 194 207 195 template<typename T, typename U, typename V, typename W, typename X> 208 196 inline pair<typename HashMap<RefPtr<T>, U, V, W, X>::iterator, bool> 209 HashMap<RefPtr<T>, U, V, W, X>::inlineAdd(const KeyType& key, const MappedType& mapped) 210 { 211 typedef HashMapTranslator<ValueType, ValueTraits, HashFunctions> TranslatorType; 212 return m_impl.template add<KeyType, MappedType, TranslatorType>(key, mapped); 197 HashMap<RefPtr<T>, U, V, W, X>::inlineAdd(const KeyType& key, MappedPassInType mapped) 198 { 199 return m_impl.template add<Translator>(key, mapped); 213 200 } 214 201 215 202 template<typename T, typename U, typename V, typename W, typename X> 216 203 inline pair<typename HashMap<RefPtr<T>, U, V, W, X>::iterator, bool> 217 HashMap<RefPtr<T>, U, V, W, X>::inlineAdd(RawKeyType key, const MappedType&mapped)218 { 219 return m_impl.template add< RawKeyType, MappedType, RawKeyTranslator>(key, mapped);204 HashMap<RefPtr<T>, U, V, W, X>::inlineAdd(RawKeyType key, MappedPassInType mapped) 205 { 206 return m_impl.template add<Translator>(key, mapped); 220 207 } 221 208 222 209 template<typename T, typename U, typename V, typename W, typename X> 223 210 pair<typename HashMap<RefPtr<T>, U, V, W, X>::iterator, bool> 224 HashMap<RefPtr<T>, U, V, W, X>::set(const KeyType& key, const MappedType&mapped)211 HashMap<RefPtr<T>, U, V, W, X>::set(const KeyType& key, MappedPassInType mapped) 225 212 { 226 213 pair<iterator, bool> result = inlineAdd(key, mapped); … … 234 221 template<typename T, typename U, typename V, typename W, typename X> 235 222 pair<typename HashMap<RefPtr<T>, U, V, W, X>::iterator, bool> 236 HashMap<RefPtr<T>, U, V, W, X>::set(RawKeyType key, const MappedType&mapped)223 HashMap<RefPtr<T>, U, V, W, X>::set(RawKeyType key, MappedPassInType mapped) 237 224 { 238 225 pair<iterator, bool> result = inlineAdd(key, mapped); … … 246 233 template<typename T, typename U, typename V, typename W, typename X> 247 234 pair<typename HashMap<RefPtr<T>, U, V, W, X>::iterator, bool> 248 HashMap<RefPtr<T>, U, V, W, X>::add(const KeyType& key, const MappedType&mapped)235 HashMap<RefPtr<T>, U, V, W, X>::add(const KeyType& key, MappedPassInType mapped) 249 236 { 250 237 return inlineAdd(key, mapped); … … 253 240 template<typename T, typename U, typename V, typename W, typename X> 254 241 pair<typename HashMap<RefPtr<T>, U, V, W, X>::iterator, bool> 255 HashMap<RefPtr<T>, U, V, W, X>::add(RawKeyType key, const MappedType&mapped)242 HashMap<RefPtr<T>, U, V, W, X>::add(RawKeyType key, MappedPassInType mapped) 256 243 { 257 244 return inlineAdd(key, mapped); … … 259 246 260 247 template<typename T, typename U, typename V, typename W, typename MappedTraits> 261 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::Mapped Type248 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::MappedPeekType 262 249 HashMap<RefPtr<T>, U, V, W, MappedTraits>::get(const KeyType& key) const 263 250 { … … 269 256 270 257 template<typename T, typename U, typename V, typename W, typename MappedTraits> 271 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::Mapped Type258 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::MappedPeekType 272 259 inline HashMap<RefPtr<T>, U, V, W, MappedTraits>::inlineGet(RawKeyType key) const 273 260 { 274 ValueType* entry = const_cast<HashTableType&>(m_impl).template lookup< RawKeyType, RawKeyTranslator>(key);261 ValueType* entry = const_cast<HashTableType&>(m_impl).template lookup<Translator>(key); 275 262 if (!entry) 276 263 return MappedTraits::emptyValue(); … … 279 266 280 267 template<typename T, typename U, typename V, typename W, typename MappedTraits> 281 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::Mapped Type268 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::MappedPeekType 282 269 HashMap<RefPtr<T>, U, V, W, MappedTraits>::get(RawKeyType key) const 283 270 { … … 313 300 314 301 template<typename T, typename U, typename V, typename W, typename MappedTraits> 315 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::Mapped Type302 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::MappedPassOutType 316 303 HashMap<RefPtr<T>, U, V, W, MappedTraits>::take(const KeyType& key) 317 304 { … … 320 307 if (it == end()) 321 308 return MappedTraits::emptyValue(); 322 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::MappedType result = it->second;309 MappedPassOutType result = it->second; 323 310 remove(it); 324 311 return result; … … 326 313 327 314 template<typename T, typename U, typename V, typename W, typename MappedTraits> 328 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::Mapped Type315 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::MappedPassOutType 329 316 HashMap<RefPtr<T>, U, V, W, MappedTraits>::take(RawKeyType key) 330 317 { … … 333 320 if (it == end()) 334 321 return MappedTraits::emptyValue(); 335 typename HashMap<RefPtr<T>, U, V, W, MappedTraits>::MappedType result = it->second;322 MappedPassOutType result = it->second; 336 323 remove(it); 337 324 return result;
Note:
See TracChangeset
for help on using the changeset viewer.