Changeset 32650 in webkit for trunk/JavaScriptCore/wtf/HashSet.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/HashSet.h

    r31122 r32650  
    2727namespace WTF {
    2828
     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
    2933    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>&);
    3434
    3535    template<typename ValueArg, typename HashArg = typename DefaultHash<ValueArg>::Hash,
     
    3939        typedef TraitsArg ValueTraits;
    4040
    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 
    4941    public:
    5042        typedef typename ValueTraits::TraitType ValueType;
     43
     44    private:
     45        typedef HashTable<ValueType, ValueType, IdentityExtractor<ValueType>,
     46            HashFunctions, ValueTraits, ValueTraits> HashTableType;
     47
     48    public:
    5149        typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
    5250        typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
    53 
    54         HashSet();
    55         HashSet(const HashSet&);
    56         HashSet& operator=(const HashSet&);
    57         ~HashSet();
    5851
    5952        void swap(HashSet&);
     
    9891
    9992    private:
    100         void refAll();
    101         void derefAll();
    102 
    10393        friend void deleteAllValues<>(const HashSet&);
    10494
     
    110100    };
    111101
    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)
    121107        {
    122             Assigner<ValueTraits::needsRef, ValueType, StorageType, ValueTraits>::assign(key, location);
     108            Translator::translate(location, key, hashCode);
    123109        }
    124110    };
    125111
    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 
    198112    template<typename T, typename U, typename V>
    199113    inline void HashSet<T, U, V>::swap(HashSet& other)
     
    203117
    204118    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>
    211119    inline int HashSet<T, U, V>::size() const
    212120    {
     
    253161    inline typename HashSet<T, U, V>::iterator HashSet<T, U, V>::find(const ValueType& value)
    254162    {
    255         return m_impl.find(*(const StorageType*)&value);
     163        return m_impl.find(value);
    256164    }
    257165
     
    259167    inline typename HashSet<T, U, V>::const_iterator HashSet<T, U, V>::find(const ValueType& value) const
    260168    {
    261         return m_impl.find(*(const StorageType*)&value);
     169        return m_impl.find(value);
    262170    }
    263171
     
    265173    inline bool HashSet<T, U, V>::contains(const ValueType& value) const
    266174    {
    267         return m_impl.contains(*(const StorageType*)&value);
     175        return m_impl.contains(value);
    268176    }
    269177
     
    273181    inline HashSet<Value, HashFunctions, Traits>::find(const T& value)
    274182    {
    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;
    277184        return m_impl.find<T, Adapter>(value);
    278185    }
     
    283190    inline HashSet<Value, HashFunctions, Traits>::find(const T& value) const
    284191    {
    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;
    287193        return m_impl.find<T, Adapter>(value);
    288194    }
     
    292198    inline bool HashSet<Value, HashFunctions, Traits>::contains(const T& value) const
    293199    {
    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;
    296201        return m_impl.contains<T, Adapter>(value);
    297202    }
    298203
    299204    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);
    305208    }
    306209
     
    310213    HashSet<Value, HashFunctions, Traits>::add(const T& value)
    311214    {
    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;
    314216        return m_impl.template addPassingHashCode<T, T, Adapter>(value, value);
    315217    }
     
    321223            return;
    322224        m_impl.checkTableConsistency();
    323         RefCounter<ValueTraits, StorageTraits>::deref(*it.m_impl);
    324225        m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
    325226    }
     
    334235    inline void HashSet<T, U, V>::clear()
    335236    {
    336         derefAll();
    337237        m_impl.clear();
    338238    }
     
    344244        iterator end = collection.end();
    345245        for (iterator it = collection.begin(); it != end; ++it)
    346             delete *(ValueType*)&*it;
     246            delete *it;
    347247    }
    348248
Note: See TracChangeset for help on using the changeset viewer.