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


Ignore:
Timestamp:
Dec 1, 2009, 4:05:30 PM (15 years ago)
Author:
[email protected]
Message:

JavaScriptCore: Added variants of find/contains/add that allow a foreign key type to be used.
This will allow AtomicString-keyed maps to be queried by C string without
having to create a temporary AtomicString (see HTTPHeaderMap.)
The code for this is adapted from the equivalent in HashSet.h.

WebCore: Add convenience methods to Element and QualifiedName that take
char* instead of AtomicString, in preparation for removing the
implicit conversion between the two types (30187).
https://bugs.webkit.org/show_bug.cgi?id=31749

Reviewed by Darin Adler.

File:
1 edited

Legend:

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

    r45120 r51566  
    8484        MappedType take(const KeyType&); // efficient combination of get with remove
    8585
     86        // An alternate version of find() that finds the object by hashing and comparing
     87        // with some other type, to avoid the cost of type conversion. HashTranslator
     88        // must have the following function members:
     89        //   static unsigned hash(const T&);
     90        //   static bool equal(const ValueType&, const T&);
     91        template<typename T, typename HashTranslator> iterator find(const T&);
     92        template<typename T, typename HashTranslator> const_iterator find(const T&) const;
     93        template<typename T, typename HashTranslator> bool contains(const T&) const;
     94
     95        // An alternate version of add() that finds the object by hashing and comparing
     96        // with some other type, to avoid the cost of type conversion if the object is already
     97        // in the table. HashTranslator must have the following function members:
     98        //   static unsigned hash(const T&);
     99        //   static bool equal(const ValueType&, const T&);
     100        //   static translate(ValueType&, const T&, unsigned hashCode);
     101        template<typename T, typename HashTranslator> pair<iterator, bool> add(const T&, const MappedType&);
     102
    86103    private:
    87104        pair<iterator, bool> inlineAdd(const KeyType&, const MappedType&);
     
    108125    };
    109126
     127    template<typename ValueType, typename ValueTraits, typename T, typename Translator>
     128    struct HashMapTranslatorAdapter {
     129        typedef typename ValueType::first_type KeyType;
     130        typedef typename ValueType::second_type MappedType;
     131
     132        static unsigned hash(const T& key) { return Translator::hash(key); }
     133        static bool equal(const KeyType& a, const T& b) { return Translator::equal(a, b); }
     134        static void translate(ValueType& location, const T& key, const MappedType&, unsigned hashCode)
     135        {
     136            Translator::translate(location.first, key, hashCode);
     137        }
     138    };
     139
    110140    template<typename T, typename U, typename V, typename W, typename X>
    111141    inline void HashMap<T, U, V, W, X>::swap(HashMap& other)
     
    172202    {
    173203        return m_impl.contains(key);
     204    }
     205
     206    template<typename T, typename U, typename V, typename W, typename X>
     207    template<typename TYPE, typename HashTranslator>
     208    inline typename HashMap<T, U, V, W, X>::iterator
     209    HashMap<T, U, V, W, X>::find(const TYPE& value)
     210    {
     211        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     212        return m_impl.template find<TYPE, Adapter>(value);
     213    }
     214
     215    template<typename T, typename U, typename V, typename W, typename X>
     216    template<typename TYPE, typename HashTranslator>
     217    inline typename HashMap<T, U, V, W, X>::const_iterator
     218    HashMap<T, U, V, W, X>::find(const TYPE& value) const
     219    {
     220        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     221        return m_impl.template find<TYPE, Adapter>(value);
     222    }
     223
     224    template<typename T, typename U, typename V, typename W, typename X>
     225    template<typename TYPE, typename HashTranslator>
     226    inline bool
     227    HashMap<T, U, V, W, X>::contains(const TYPE& value) const
     228    {
     229        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     230        return m_impl.template contains<TYPE, Adapter>(value);
    174231    }
    175232
     
    192249        }
    193250        return result;
     251    }
     252
     253    template<typename T, typename U, typename V, typename W, typename X>
     254    template<typename TYPE, typename HashTranslator>
     255    pair<typename HashMap<T, U, V, W, X>::iterator, bool>
     256    HashMap<T, U, V, W, X>::add(const TYPE& key, const MappedType& value)
     257    {
     258        typedef HashMapTranslatorAdapter<ValueType, ValueTraits, TYPE, HashTranslator> Adapter;
     259        return m_impl.template addPassingHashCode<TYPE, MappedType, Adapter>(key, value);
    194260    }
    195261
Note: See TracChangeset for help on using the changeset viewer.