/* * 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 "HashTable.h" namespace WTF { template struct PairFirstExtractor; template struct ReferenceTypeMaker { typedef T& ReferenceType; }; template struct ReferenceTypeMaker { typedef T& ReferenceType; }; template::Hash, typename KeyTraitsArg = HashTraits, typename MappedTraitsArg = HashTraits > class HashMap { WTF_MAKE_FAST_ALLOCATED; private: typedef KeyTraitsArg KeyTraits; typedef MappedTraitsArg MappedTraits; typedef PairHashTraits 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::ReferenceType MappedPassInReferenceType; typedef HashArg HashFunctions; typedef HashTable, HashFunctions, ValueTraits, KeyTraits> HashTableType; class HashMapKeysProxy; class HashMapValuesProxy; public: typedef HashTableIteratorAdapter iterator; typedef HashTableConstIteratorAdapter const_iterator; 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(*this); } const HashMapKeysProxy& keys() const { return static_cast(*this); } HashMapValuesProxy& values() { return static_cast(*this); } const HashMapValuesProxy& values() const { return static_cast(*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 pair 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 pair 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 iterator find(const T&); template const_iterator find(const T&) const; template 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 pair add(const T&, MappedPassInType); void checkConsistency() const; private: pair inlineAdd(const KeyType&, MappedPassInReferenceType); class HashMapKeysProxy : private HashMap { public: typedef typename HashMap::iterator::Keys iterator; typedef typename HashMap::const_iterator::Keys const_iterator; iterator begin() { return HashMap::begin().keys(); } iterator end() { return HashMap::end().keys(); } const_iterator begin() const { return HashMap::begin().keys(); } const_iterator end() const { return HashMap::end().keys(); } private: friend class HashMap; // These are intentionally not implemented. HashMapKeysProxy(); HashMapKeysProxy(const HashMapKeysProxy&); HashMapKeysProxy& operator=(const HashMapKeysProxy&); ~HashMapKeysProxy(); }; class HashMapValuesProxy : private HashMap { public: typedef typename HashMap::iterator::Values iterator; typedef typename HashMap::const_iterator::Values const_iterator; iterator begin() { return HashMap::begin().values(); } iterator end() { return HashMap::end().values(); } const_iterator begin() const { return HashMap::begin().values(); } const_iterator end() const { return HashMap::end().values(); } private: friend class HashMap; // These are intentionally not implemented. HashMapValuesProxy(); HashMapValuesProxy(const HashMapValuesProxy&); HashMapValuesProxy& operator=(const HashMapValuesProxy&); ~HashMapValuesProxy(); }; HashTableType m_impl; }; template struct PairFirstExtractor { static const typename PairType::first_type& extract(const PairType& p) { return p.first; } }; template struct HashMapTranslator { template static unsigned hash(const T& key) { return HashFunctions::hash(key); } template static bool equal(const T& a, const U& b) { return HashFunctions::equal(a, b); } template static void translate(T& location, const U& key, const V& mapped) { location.first = key; ValueTraits::SecondTraits::store(mapped, location.second); } }; template struct HashMapTranslatorAdapter { template static unsigned hash(const T& key) { return Translator::hash(key); } template static bool equal(const T& a, const U& b) { return Translator::equal(a, b); } template static void translate(T& location, const U& key, const V& mapped, unsigned hashCode) { Translator::translate(location.first, key, hashCode); ValueTraits::SecondTraits::store(mapped, location.second); } }; template inline void HashMap::swap(HashMap& other) { m_impl.swap(other.m_impl); } template inline int HashMap::size() const { return m_impl.size(); } template inline int HashMap::capacity() const { return m_impl.capacity(); } template inline bool HashMap::isEmpty() const { return m_impl.isEmpty(); } template inline typename HashMap::iterator HashMap::begin() { return m_impl.begin(); } template inline typename HashMap::iterator HashMap::end() { return m_impl.end(); } template inline typename HashMap::const_iterator HashMap::begin() const { return m_impl.begin(); } template inline typename HashMap::const_iterator HashMap::end() const { return m_impl.end(); } template inline typename HashMap::iterator HashMap::find(const KeyType& key) { return m_impl.find(key); } template inline typename HashMap::const_iterator HashMap::find(const KeyType& key) const { return m_impl.find(key); } template inline bool HashMap::contains(const KeyType& key) const { return m_impl.contains(key); } template template inline typename HashMap::iterator HashMap::find(const TYPE& value) { return m_impl.template find >(value); } template template inline typename HashMap::const_iterator HashMap::find(const TYPE& value) const { return m_impl.template find >(value); } template template inline bool HashMap::contains(const TYPE& value) const { return m_impl.template contains >(value); } template inline pair::iterator, bool> HashMap::inlineAdd(const KeyType& key, MappedPassInReferenceType mapped) { return m_impl.template add >(key, mapped); } template pair::iterator, bool> HashMap::set(const KeyType& key, MappedPassInType mapped) { pair result = inlineAdd(key, mapped); if (!result.second) { // The inlineAdd call above found an existing hash table entry; we need to set the mapped value. MappedTraits::store(mapped, result.first->second); } return result; } template template pair::iterator, bool> HashMap::add(const TYPE& key, MappedPassInType value) { return m_impl.template addPassingHashCode >(key, value); } template pair::iterator, bool> HashMap::add(const KeyType& key, MappedPassInType mapped) { return inlineAdd(key, mapped); } template typename HashMap::MappedPeekType HashMap::get(const KeyType& key) const { ValueType* entry = const_cast(m_impl).lookup(key); if (!entry) return MappedTraits::peek(MappedTraits::emptyValue()); return MappedTraits::peek(entry->second); } template inline void HashMap::remove(iterator it) { if (it.m_impl == m_impl.end()) return; m_impl.internalCheckTableConsistency(); m_impl.removeWithoutEntryConsistencyCheck(it.m_impl); } template inline void HashMap::remove(const KeyType& key) { remove(find(key)); } template inline void HashMap::clear() { m_impl.clear(); } template typename HashMap::MappedPassOutType HashMap::take(const KeyType& key) { iterator it = find(key); if (it == end()) return MappedTraits::passOut(MappedTraits::emptyValue()); MappedPassOutType result = MappedTraits::passOut(it->second); remove(it); return result; } template inline void HashMap::checkConsistency() const { m_impl.checkTableConsistency(); } template bool operator==(const HashMap& a, const HashMap& b) { if (a.size() != b.size()) return false; typedef typename HashMap::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->first); if (bPos == notFound || it->second != bPos->second) return false; } return true; } template inline bool operator!=(const HashMap& a, const HashMap& b) { return !(a == b); } template void deleteAllPairSeconds(HashTableType& collection) { typedef typename HashTableType::const_iterator iterator; iterator end = collection.end(); for (iterator it = collection.begin(); it != end; ++it) delete it->second; } template inline void deleteAllValues(const HashMap& collection) { deleteAllPairSeconds(collection); } template void deleteAllPairFirsts(HashTableType& collection) { typedef typename HashTableType::const_iterator iterator; iterator end = collection.end(); for (iterator it = collection.begin(); it != end; ++it) delete it->first; } template inline void deleteAllKeys(const HashMap& collection) { deleteAllPairFirsts(collection); } template inline void copyKeysToVector(const HashMap& collection, Y& vector) { typedef typename HashMap::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 inline void copyValuesToVector(const HashMap& collection, Y& vector) { typedef typename HashMap::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; #include "RefPtrHashMap.h" #endif /* WTF_HashMap_h */