123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- /*
- * 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 WTF_HashMap_h
- #define WTF_HashMap_h
- #include <wtf/HashTable.h>
- namespace WTF {
- template<typename KeyTraits, typename MappedTraits, bool shared> struct HashMapValueTraits;
- template<typename T> struct ReferenceTypeMaker {
- typedef T& ReferenceType;
- };
- template<typename T> struct ReferenceTypeMaker<T&> {
- typedef T& ReferenceType;
- };
- template<typename T> struct KeyValuePairKeyExtractor {
- static const typename T::KeyType& extract(const T& p) { return p.key; }
- };
- template<typename KeyArg, typename MappedArg, typename HashArg = typename DefaultHash<KeyArg>::Hash,
- typename KeyTraitsArg = HashTraits<KeyArg>, typename MappedTraitsArg = HashTraits<MappedArg>, bool shared = false>
- class HashMap {
- WTF_MAKE_FAST_ALLOCATED;
- private:
- typedef KeyTraitsArg KeyTraits;
- typedef MappedTraitsArg MappedTraits;
- typedef HashMapValueTraits<KeyTraits, MappedTraits, shared> ValueTraits;
- public:
- typedef typename KeyTraits::TraitType KeyType;
- 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;
- class HashMapKeysProxy;
- class HashMapValuesProxy;
- public:
- typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;
- typedef HashTableConstIteratorAdapter<HashTableType, ValueType> const_iterator;
- typedef typename HashTableType::AddResult AddResult;
- public:
- 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;
- HashMapKeysProxy& keys() { return static_cast<HashMapKeysProxy&>(*this); }
- const HashMapKeysProxy& keys() const { return static_cast<const HashMapKeysProxy&>(*this); }
- HashMapValuesProxy& values() { return static_cast<HashMapValuesProxy&>(*this); }
- const HashMapValuesProxy& values() const { return static_cast<const HashMapValuesProxy&>(*this); }
- iterator find(const KeyType&);
- const_iterator find(const KeyType&) const;
- bool contains(const KeyType&) const;
- MappedPeekType get(const KeyType&) 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);
- // 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);
- void remove(const KeyType&);
- void remove(iterator);
- void clear();
- MappedPassOutType take(const KeyType&); // efficient combination of get with remove
- // An alternate version of find() that finds the object by hashing and comparing
- // with some other type, to avoid the cost of type conversion. HashTranslator
- // must have the following function members:
- // static unsigned hash(const T&);
- // static bool equal(const ValueType&, const T&);
- template<typename T, typename HashTranslator> iterator find(const T&);
- template<typename T, typename HashTranslator> const_iterator find(const T&) const;
- template<typename T, typename HashTranslator> bool contains(const T&) const;
- // An alternate version of add() that finds the object by hashing and comparing
- // with some other type, to avoid the cost of type conversion if the object is already
- // in the table. HashTranslator must have the following function members:
- // static unsigned hash(const T&);
- // static bool equal(const ValueType&, const T&);
- // static translate(ValueType&, const T&, unsigned hashCode);
- template<typename T, typename HashTranslator> AddResult add(const T&, MappedPassInType);
- void checkConsistency() const;
- static bool isValidKey(const KeyType&);
- private:
- AddResult inlineAdd(const KeyType&, MappedPassInReferenceType);
- HashTableType m_impl;
- };
- template<typename KeyArg, typename MappedArg, typename HashArg = typename DefaultHash<KeyArg>::Hash,
- typename KeyTraitsArg = HashTraits<KeyArg>, typename MappedTraitsArg = HashTraits<MappedArg> >
- class HashMap_shared : public HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, true> {};
-
- template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg, bool shared>
- class HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, shared>::HashMapKeysProxy :
- private HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> {
- public:
- typedef HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> HashMapType;
- typedef typename HashMapType::iterator::Keys iterator;
- typedef typename HashMapType::const_iterator::Keys const_iterator;
- iterator begin()
- {
- return HashMapType::begin().keys();
- }
- iterator end()
- {
- return HashMapType::end().keys();
- }
- const_iterator begin() const
- {
- return HashMapType::begin().keys();
- }
- const_iterator end() const
- {
- return HashMapType::end().keys();
- }
- private:
- friend class HashMap;
- // These are intentionally not implemented.
- HashMapKeysProxy();
- HashMapKeysProxy(const HashMapKeysProxy&);
- HashMapKeysProxy& operator=(const HashMapKeysProxy&);
- ~HashMapKeysProxy();
- };
- template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg, bool shared>
- class HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg, shared>::HashMapValuesProxy :
- private HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> {
- public:
- typedef HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg> HashMapType;
- typedef typename HashMapType::iterator::Values iterator;
- typedef typename HashMapType::const_iterator::Values const_iterator;
- iterator begin()
- {
- return HashMapType::begin().values();
- }
- iterator end()
- {
- return HashMapType::end().values();
- }
- const_iterator begin() const
- {
- return HashMapType::begin().values();
- }
- const_iterator end() const
- {
- return HashMapType::end().values();
- }
- private:
- friend class HashMap;
- // These are intentionally not implemented.
- HashMapValuesProxy();
- HashMapValuesProxy(const HashMapValuesProxy&);
- HashMapValuesProxy& operator=(const HashMapValuesProxy&);
- ~HashMapValuesProxy();
- };
- template<typename KeyTraits, typename MappedTraits, bool shared> struct HashMapValueTraits : KeyValuePairHashTraits<KeyTraits, MappedTraits, shared> {
- static const bool hasIsEmptyValueFunction = true;
- static bool isEmptyValue(const typename KeyValuePairHashTraits<KeyTraits, MappedTraits>::TraitType& value)
- {
- return isHashTraitsEmptyValue<KeyTraits>(value.key);
- }
- };
- template<typename ValueTraits, typename HashFunctions>
- struct HashMapTranslator {
- template<typename T> static unsigned hash(const T& key) { return HashFunctions::hash(key); }
- template<typename T, typename U> static bool equal(const T& a, const U& b) { return HashFunctions::equal(a, b); }
- template<typename T, typename U, typename V> static void translate(T& location, const U& key, const V& mapped)
- {
- location.key = key;
- ValueTraits::ValueTraits::store(mapped, location.value);
- }
- };
- template<typename ValueTraits, typename Translator>
- struct HashMapTranslatorAdapter {
- template<typename T> static unsigned hash(const T& key) { return Translator::hash(key); }
- template<typename T, typename U> static bool equal(const T& a, const U& b) { return Translator::equal(a, b); }
- template<typename T, typename U, typename V> static void translate(T& location, const U& key, const V& mapped, unsigned hashCode)
- {
- Translator::translate(location.key, key, hashCode);
- ValueTraits::ValueTraits::store(mapped, location.value);
- }
- };
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline void HashMap<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<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<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<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<T, U, V, W, X, shared>::iterator HashMap<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<T, U, V, W, X, shared>::iterator HashMap<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<T, U, V, W, X, shared>::const_iterator HashMap<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<T, U, V, W, X, shared>::const_iterator HashMap<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<T, U, V, W, X, shared>::iterator HashMap<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<T, U, V, W, X, shared>::const_iterator HashMap<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 bool HashMap<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>
- template<typename TYPE, typename HashTranslator>
- inline typename HashMap<T, U, V, W, X, shared>::iterator
- HashMap<T, U, V, W, X, shared>::find(const TYPE& value)
- {
- return m_impl.template find<HashMapTranslatorAdapter<ValueTraits, HashTranslator> >(value);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- template<typename TYPE, typename HashTranslator>
- inline typename HashMap<T, U, V, W, X, shared>::const_iterator
- HashMap<T, U, V, W, X, shared>::find(const TYPE& value) const
- {
- return m_impl.template find<HashMapTranslatorAdapter<ValueTraits, HashTranslator> >(value);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- template<typename TYPE, typename HashTranslator>
- inline bool
- HashMap<T, U, V, W, X, shared>::contains(const TYPE& value) const
- {
- return m_impl.template contains<HashMapTranslatorAdapter<ValueTraits, HashTranslator> >(value);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- typename HashMap<T, U, V, W, X, shared>::AddResult
- HashMap<T, U, V, W, X, shared>::inlineAdd(const KeyType& key, MappedPassInReferenceType mapped)
- {
- return m_impl.template add<HashMapTranslator<ValueTraits, HashFunctions> >(key, mapped);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- typename HashMap<T, U, V, W, X, shared>::AddResult
- HashMap<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>
- template<typename TYPE, typename HashTranslator>
- typename HashMap<T, U, V, W, X, shared>::AddResult
- HashMap<T, U, V, W, X, shared>::add(const TYPE& key, MappedPassInType value)
- {
- return m_impl.template addPassingHashCode<HashMapTranslatorAdapter<ValueTraits, HashTranslator> >(key, value);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- typename HashMap<T, U, V, W, X, shared>::AddResult
- HashMap<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 MappedTraits, bool shared>
- typename HashMap<T, U, V, W, MappedTraits, shared>::MappedPeekType
- HashMap<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 X, bool shared>
- inline void HashMap<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<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<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<T, U, V, W, MappedTraits, shared>::MappedPassOutType
- HashMap<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 X, bool shared>
- inline void HashMap<T, U, V, W, X, shared>::checkConsistency() const
- {
- m_impl.checkTableConsistency();
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline bool HashMap<T, U, V, W, X, shared>::isValidKey(const KeyType& key)
- {
- if (KeyTraits::isDeletedValue(key))
- return false;
- if (HashFunctions::safeToCompareToEmptyOrDeleted) {
- if (key == KeyTraits::emptyValue())
- return false;
- } else {
- if (isHashTraitsEmptyValue<KeyTraits>(key))
- return false;
- }
- return true;
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- bool operator==(const HashMap<T, U, V, W, X, shared>& a, const HashMap<T, U, V, W, X, shared>& b)
- {
- if (a.size() != b.size())
- return false;
- typedef typename HashMap<T, U, V, W, X, shared>::const_iterator const_iterator;
- const_iterator end = a.end();
- const_iterator notFound = b.end();
- for (const_iterator it = a.begin(); it != end; ++it) {
- const_iterator bPos = b.find(it->key);
- if (bPos == notFound || it->value != bPos->value)
- return false;
- }
- return true;
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline bool operator!=(const HashMap<T, U, V, W, X, shared>& a, const HashMap<T, U, V, W, X, shared>& b)
- {
- return !(a == b);
- }
- template<typename T, typename U, typename V, typename W, typename X, bool shared>
- inline void deleteAllValues(const HashMap<T, U, V, W, X, shared>& collection)
- {
- typedef typename HashMap<T, U, V, W, X, shared>::const_iterator iterator;
- iterator end = collection.end();
- for (iterator it = collection.begin(); it != end; ++it)
- delete it->value;
- }
-
- template<typename T, typename U, typename V, typename W, typename X, typename Y, bool shared>
- inline void copyKeysToVector(const HashMap<T, U, V, W, X, shared>& collection, Y& vector)
- {
- typedef typename HashMap<T, U, V, W, X, shared>::const_iterator::Keys iterator;
-
- vector.resize(collection.size());
-
- iterator it = collection.begin().keys();
- iterator end = collection.end().keys();
- for (unsigned i = 0; it != end; ++it, ++i)
- vector[i] = *it;
- }
- template<typename T, typename U, typename V, typename W, typename X, typename Y, bool shared>
- inline void copyValuesToVector(const HashMap<T, U, V, W, X, shared>& collection, Y& vector)
- {
- typedef typename HashMap<T, U, V, W, X, shared>::const_iterator::Values iterator;
-
- vector.resize(collection.size());
-
- iterator it = collection.begin().values();
- iterator end = collection.end().values();
- for (unsigned i = 0; it != end; ++it, ++i)
- vector[i] = *it;
- }
- } // namespace WTF
- using WTF::HashMap;
- using WTF::HashMap_shared;
- #include <wtf/RefPtrHashMap.h>
- #endif /* WTF_HashMap_h */
|