123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- /*
- * Copyright (C) 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public License
- * along with this library; see the file COPYING.LIB. If not, write to
- * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- *
- */
- #ifndef RefPtrHashMap_h
- #define RefPtrHashMap_h
- namespace WTF {
- // This specialization is a copy of HashMap for use with RefPtr keys, with overloaded functions
- // to allow for lookup by pointer instead of RefPtr, avoiding ref-count churn.
-
- // FIXME: Find a way to do this with traits that doesn't require a copy of the HashMap template.
-
- template<typename T, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg, bool shared>
- class HashMap<RefPtr<T>, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, shared> {
- WTF_MAKE_FAST_ALLOCATED;
- private:
- typedef KeyTraitsArg KeyTraits;
- typedef MappedTraitsArg MappedTraits;
- typedef KeyValuePairHashTraits<KeyTraits, MappedTraits, shared> ValueTraits;
- public:
- typedef typename KeyTraits::TraitType KeyType;
- typedef T* RawKeyType;
- typedef typename MappedTraits::TraitType MappedType;
- typedef typename ValueTraits::TraitType ValueType;
- private:
- typedef typename MappedTraits::PassInType MappedPassInType;
- typedef typename MappedTraits::PassOutType MappedPassOutType;
- typedef typename MappedTraits::PeekType MappedPeekType;
- typedef typename ReferenceTypeMaker<MappedPassInType>::ReferenceType MappedPassInReferenceType;
-
- typedef HashArg HashFunctions;
- typedef HashTable<KeyType, ValueType, KeyValuePairKeyExtractor<ValueType>,
- HashFunctions, ValueTraits, KeyTraits> HashTableType;
- typedef HashMapTranslator<ValueTraits, HashFunctions>
- Translator;
- public:
- typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
- typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
- typedef typename HashTableType::AddResult AddResult;
- void swap(HashMap&);
- int size() const;
- int capacity() const;
- bool isEmpty() const;
- // iterators iterate over pairs of keys and values
- iterator begin();
- iterator end();
- const_iterator begin() const;
- const_iterator end() const;
- iterator find(const KeyType&);
- iterator find(RawKeyType);
- const_iterator find(const KeyType&) const;
- const_iterator find(RawKeyType) const;
- bool contains(const KeyType&) const;
- bool contains(RawKeyType) const;
- MappedPeekType get(const KeyType&) const;
- MappedPeekType get(RawKeyType) const;
- MappedPeekType inlineGet(RawKeyType) const;
- // replaces value but not key if key is already present
- // return value is a pair of the iterator to the key location,
- // and a boolean that's true if a new value was actually added
- AddResult set(const KeyType&, MappedPassInType);
- AddResult set(RawKeyType, MappedPassInType);
- // does nothing if key is already present
- // return value is a pair of the iterator to the key location,
- // and a boolean that's true if a new value was actually added
- AddResult add(const KeyType&, MappedPassInType);
- AddResult add(RawKeyType, MappedPassInType);
- void remove(const KeyType&);
- void remove(RawKeyType);
- void remove(iterator);
- void clear();
- MappedPassOutType take(const KeyType&); // efficient combination of get with remove
- MappedPassOutType take(RawKeyType); // efficient combination of get with remove
- private:
- AddResult inlineAdd(const KeyType&, MappedPassInReferenceType);
- AddResult inlineAdd(RawKeyType, MappedPassInReferenceType);
- HashTableType m_impl;
- };
-
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline void HashMap<RefPtr<T>, U, V, W, X, shared>::swap(HashMap& other)
- {
- m_impl.swap(other.m_impl);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline int HashMap<RefPtr<T>, U, V, W, X, shared>::size() const
- {
- return m_impl.size();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline int HashMap<RefPtr<T>, U, V, W, X, shared>::capacity() const
- {
- return m_impl.capacity();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline bool HashMap<RefPtr<T>, U, V, W, X, shared>::isEmpty() const
- {
- return m_impl.isEmpty();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::iterator HashMap<RefPtr<T>, U, V, W, X, shared>::begin()
- {
- return m_impl.begin();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::iterator HashMap<RefPtr<T>, U, V, W, X, shared>::end()
- {
- return m_impl.end();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::const_iterator HashMap<RefPtr<T>, U, V, W, X, shared>::begin() const
- {
- return m_impl.begin();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::const_iterator HashMap<RefPtr<T>, U, V, W, X, shared>::end() const
- {
- return m_impl.end();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::iterator HashMap<RefPtr<T>, U, V, W, X, shared>::find(const KeyType& key)
- {
- return m_impl.find(key);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::iterator HashMap<RefPtr<T>, U, V, W, X, shared>::find(RawKeyType key)
- {
- return m_impl.template find<Translator>(key);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::const_iterator HashMap<RefPtr<T>, U, V, W, X, shared>::find(const KeyType& key) const
- {
- return m_impl.find(key);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::const_iterator HashMap<RefPtr<T>, U, V, W, X, shared>::find(RawKeyType key) const
- {
- return m_impl.template find<Translator>(key);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline bool HashMap<RefPtr<T>, U, V, W, X, shared>::contains(const KeyType& key) const
- {
- return m_impl.contains(key);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline bool HashMap<RefPtr<T>, U, V, W, X, shared>::contains(RawKeyType key) const
- {
- return m_impl.template contains<Translator>(key);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::AddResult
- HashMap<RefPtr<T>, U, V, W, X, shared>::inlineAdd(const KeyType& key, MappedPassInReferenceType mapped)
- {
- return m_impl.template add<Translator>(key, mapped);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline typename HashMap<RefPtr<T>, U, V, W, X, shared>::AddResult
- HashMap<RefPtr<T>, U, V, W, X, shared>::inlineAdd(RawKeyType key, MappedPassInReferenceType mapped)
- {
- return m_impl.template add<Translator>(key, mapped);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, X, shared>::AddResult
- HashMap<RefPtr<T>, U, V, W, X, shared>::set(const KeyType& key, MappedPassInType mapped)
- {
- AddResult result = inlineAdd(key, mapped);
- if (!result.isNewEntry) {
- // The inlineAdd call above found an existing hash table entry; we need to set the mapped value.
- MappedTraits::store(mapped, result.iterator->value);
- }
- return result;
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, X, shared>::AddResult
- HashMap<RefPtr<T>, U, V, W, X, shared>::set(RawKeyType key, MappedPassInType mapped)
- {
- AddResult result = inlineAdd(key, mapped);
- if (!result.isNewEntry) {
- // The inlineAdd call above found an existing hash table entry; we need to set the mapped value.
- MappedTraits::store(mapped, result.iterator->value);
- }
- return result;
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, X, shared>::AddResult
- HashMap<RefPtr<T>, U, V, W, X, shared>::add(const KeyType& key, MappedPassInType mapped)
- {
- return inlineAdd(key, mapped);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, X, shared>::AddResult
- HashMap<RefPtr<T>, U, V, W, X, shared>::add(RawKeyType key, MappedPassInType mapped)
- {
- return inlineAdd(key, mapped);
- }
- template<typename T, typename U, typename V, typename W, typename MappedTraits, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::MappedPeekType
- HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::get(const KeyType& key) const
- {
- ValueType* entry = const_cast<HashTableType&>(m_impl).lookup(key);
- if (!entry)
- return MappedTraits::peek(MappedTraits::emptyValue());
- return MappedTraits::peek(entry->value);
- }
- template<typename T, typename U, typename V, typename W, typename MappedTraits, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::MappedPeekType
- inline HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::inlineGet(RawKeyType key) const
- {
- ValueType* entry = const_cast<HashTableType&>(m_impl).template lookup<Translator>(key);
- if (!entry)
- return MappedTraits::peek(MappedTraits::emptyValue());
- return MappedTraits::peek(entry->value);
- }
- template<typename T, typename U, typename V, typename W, typename MappedTraits, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::MappedPeekType
- HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::get(RawKeyType key) const
- {
- return inlineGet(key);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline void HashMap<RefPtr<T>, U, V, W, X, shared>::remove(iterator it)
- {
- if (it.m_impl == m_impl.end())
- return;
- m_impl.internalCheckTableConsistency();
- m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline void HashMap<RefPtr<T>, U, V, W, X, shared>::remove(const KeyType& key)
- {
- remove(find(key));
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline void HashMap<RefPtr<T>, U, V, W, X, shared>::remove(RawKeyType key)
- {
- remove(find(key));
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline void HashMap<RefPtr<T>, U, V, W, X, shared>::clear()
- {
- m_impl.clear();
- }
- template<typename T, typename U, typename V, typename W, typename MappedTraits, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::MappedPassOutType
- HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::take(const KeyType& key)
- {
- iterator it = find(key);
- if (it == end())
- return MappedTraits::passOut(MappedTraits::emptyValue());
- MappedPassOutType result = MappedTraits::passOut(it->value);
- remove(it);
- return result;
- }
- template<typename T, typename U, typename V, typename W, typename MappedTraits, bool shared>
- typename HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::MappedPassOutType
- HashMap<RefPtr<T>, U, V, W, MappedTraits, shared>::take(RawKeyType key)
- {
- iterator it = find(key);
- if (it == end())
- return MappedTraits::passOut(MappedTraits::emptyValue());
- MappedPassOutType result = MappedTraits::passOut(it->value);
- remove(it);
- return result;
- }
- } // namespace WTF
- #endif // RefPtrHashMap_h
|