Changeset 32650 in webkit for trunk/JavaScriptCore/wtf/HashMap.h


Ignore:
Timestamp:
Apr 28, 2008, 10:18:15 AM (17 years ago)
Author:
Darin Adler
Message:

2008-04-28 Darin Adler <Darin Adler>

Reviewed by Adam.

  • make sure RefPtr's default hash doesn't ref/deref when computing the hash
  • remove remnants of the hash table storage type optimization
  • wtf/HashFunctions.h: Used "using" to get the hash and equal functions from PtrHash<P*> into PtrHash<RefPtr<P>>.
  • wtf/HashMap.h: Replaced uses of PairBaseHashTraits with PairHashTraits. Eliminated storage-related typedefs. Removed constructor, destructor, copy constructor, and destructor since the compiler-generated ones are fine. Removed refAll and derefAll. Took out unnnecessary typecasts. Removed use of RefCounter.
  • wtf/HashSet.h: Eliminated storage-related typedefs. Removed constructor, destructor, copy constructor, and destructor since the compiler-generated ones are fine. Removed refAll and derefAll. Removed unneeded template arguents from HashSetTranslatorAdapter. Eliminated unneeded HashSetTranslator template.
  • wtf/HashTable.h: Tweaked formatting. Removed NeedsRef, RefCounterBase, RefCounter, HashTableRefCounterBase, HashTableRefCounter, and Assigner class templates.
  • wtf/HashTraits.h: Removed StorageTraits, needsRef, PairBaseHashTraits, and HashKeyStorageTraits.
  • wtf/RefPtrHashMap.h: Made all the same fixes as in HashMap. Also made the corresponding changes to RefPtrHashMapRawKeyTranslator.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/HashMap.h

    r32609 r32650  
    3535        typedef KeyTraitsArg KeyTraits;
    3636        typedef MappedTraitsArg MappedTraits;
    37         typedef PairBaseHashTraits<KeyTraits, MappedTraits> ValueTraits;
     37        typedef PairHashTraits<KeyTraits, MappedTraits> ValueTraits;
    3838
    3939    public:
     
    4545        typedef HashArg HashFunctions;
    4646
    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;
    5949
    6050    public:
    6151        typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
    6252        typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
    63 
    64         HashMap();
    65         HashMap(const HashMap&);
    66         HashMap& operator=(const HashMap&);
    67         ~HashMap();
    6853
    6954        void swap(HashMap&);
     
    10287    private:
    10388        pair<iterator, bool> inlineAdd(const KeyType&, const MappedType&);
    104         void refAll();
    105         void derefAll();
    10689
    10790        HashTableType m_impl;
     
    11295    };
    11396
    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 {
    11999        typedef typename ValueType::first_type KeyType;
    120100        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;
    128101
    129102        static unsigned hash(const KeyType& key) { return HashFunctions::hash(key); }
    130         static bool equal(const KeyStorageType& a, const KeyType& b) { return HashFunctions::equal(*(KeyType*)&a, b); }
    131         static void translate(ValueStorageType& 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)
    132105        {
    133106            location.first = key;
     
    136109    };
    137110
    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 
    191111    template<typename T, typename U, typename V, typename W, typename X>
    192112    inline void HashMap<T, U, V, W, X>::swap(HashMap& other)
    193113    {
    194114        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();
    201115    }
    202116
     
    246160    inline typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::find(const KeyType& key)
    247161    {
    248         return m_impl.find(*(const KeyStorageType*)&key);
     162        return m_impl.find(key);
    249163    }
    250164
     
    252166    inline typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::find(const KeyType& key) const
    253167    {
    254         return m_impl.find(*(const KeyStorageType*)&key);
     168        return m_impl.find(key);
    255169    }
    256170
     
    258172    inline bool HashMap<T, U, V, W, X>::contains(const KeyType& key) const
    259173    {
    260         return m_impl.contains(*(const KeyStorageType*)&key);
     174        return m_impl.contains(key);
    261175    }
    262176
     
    265179    HashMap<T, U, V, W, X>::inlineAdd(const KeyType& key, const MappedType& mapped)
    266180    {
    267         const bool canReplaceDeletedKey = !KeyTraits::needsDestruction || KeyStorageTraits::needsDestruction;
    268         typedef HashMapTranslator<canReplaceDeletedKey, ValueType, ValueTraits, ValueStorageTraits, HashFunctions> TranslatorType;
     181        typedef HashMapTranslator<ValueType, ValueTraits, HashFunctions> TranslatorType;
    269182        return m_impl.template add<KeyType, MappedType, TranslatorType>(key, mapped);
    270183    }
     
    275188    {
    276189        pair<iterator, bool> result = inlineAdd(key, mapped);
    277         if (!result.second)
     190        if (!result.second) {
    278191            // add call above didn't change anything, so set the mapped value
    279192            result.first->second = mapped;
     193        }
    280194        return result;
    281195    }
     
    292206    HashMap<T, U, V, W, MappedTraits>::get(const KeyType& key) const
    293207    {
    294         ValueStorageType* entry = const_cast<HashTableType&>(m_impl).lookup(*(const KeyStorageType*)&key);
     208        ValueType* entry = const_cast<HashTableType&>(m_impl).lookup(key);
    295209        if (!entry)
    296210            return MappedTraits::emptyValue();
    297         return ((ValueType *)entry)->second;
     211        return entry->second;
    298212    }
    299213
     
    304218            return;
    305219        m_impl.checkTableConsistency();
    306         RefCounter<ValueTraits, ValueStorageTraits>::deref(*it.m_impl);
    307220        m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
    308221    }
     
    317230    inline void HashMap<T, U, V, W, X>::clear()
    318231    {
    319         derefAll();
    320232        m_impl.clear();
    321233    }
     
    365277        iterator end = collection.end();
    366278        for (iterator it = collection.begin(); it != end; ++it)
    367             delete *(MappedType*)&it->second;
     279            delete it->second;
    368280    }
    369281
     
    380292        iterator end = collection.end();
    381293        for (iterator it = collection.begin(); it != end; ++it)
    382             delete *(KeyType*)&it->first;
     294            delete it->first;
    383295    }
    384296
Note: See TracChangeset for help on using the changeset viewer.