/* * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2013 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 #include #include namespace WTF { template struct KeyValuePairKeyExtractor { static const typename T::KeyType& extract(const T& p) { return p.key; } }; template::Hash, typename KeyTraitsArg = HashTraits, typename MappedTraitsArg = HashTraits> class HashMap final { WTF_MAKE_FAST_ALLOCATED; private: typedef KeyTraitsArg KeyTraits; typedef MappedTraitsArg MappedTraits; struct KeyValuePairTraits : KeyValuePairHashTraits { static const bool hasIsEmptyValueFunction = true; static bool isEmptyValue(const typename KeyValuePairHashTraits::TraitType& value) { return isHashTraitsEmptyValue(value.key); } }; public: typedef typename KeyTraits::TraitType KeyType; typedef typename MappedTraits::TraitType MappedType; typedef typename KeyValuePairTraits::TraitType KeyValuePairType; private: typedef typename MappedTraits::PeekType MappedPeekType; typedef HashArg HashFunctions; typedef HashTable, HashFunctions, KeyValuePairTraits, KeyTraits> HashTableType; class HashMapKeysProxy; class HashMapValuesProxy; public: typedef HashTableIteratorAdapter iterator; typedef HashTableConstIteratorAdapter const_iterator; typedef typename HashTableType::AddResult AddResult; public: HashMap() { } HashMap(std::initializer_list initializerList) { for (const auto& keyValuePair : initializerList) add(keyValuePair.key, keyValuePair.value); } void swap(HashMap&); unsigned size() const; unsigned 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; IteratorRange keys() { return makeIteratorRange(begin().keys(), end().keys()); } const IteratorRange keys() const { return makeIteratorRange(begin().keys(), end().keys()); } IteratorRange values() { return makeIteratorRange(begin().values(), end().values()); } const IteratorRange values() const { return makeIteratorRange(begin().values(), end().values()); } iterator find(const KeyType&); const_iterator find(const KeyType&) const; bool contains(const KeyType&) const; MappedPeekType get(const KeyType&) const; // Same as get(), but aggressively inlined. MappedPeekType fastGet(const KeyType&) const; // Replaces the value but not the key if the key is already present. // Return value includes both an iterator to the key location, // and an isNewEntry boolean that's true if a new entry was added. template AddResult set(const KeyType&, V&&); template AddResult set(KeyType&&, V&&); // Does nothing if the key is already present. // Return value includes both an iterator to the key location, // and an isNewEntry boolean that's true if a new entry was added. template AddResult add(const KeyType&, V&&); template AddResult add(KeyType&&, V&&); // Same as add(), but aggressively inlined. template AddResult fastAdd(const KeyType&, V&&); template AddResult fastAdd(KeyType&&, V&&); template AddResult ensure(const KeyType&, const Functor&); template AddResult ensure(KeyType&&, const Functor&); bool remove(const KeyType&); bool remove(iterator); template void removeIf(const Functor& functor); void clear(); MappedType 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 AddResult add(K&&, V&&); // Overloads for smart pointer keys that take the raw pointer type as the parameter. template typename std::enable_if::value, iterator>::type find(typename GetPtrHelper::PtrType); template typename std::enable_if::value, const_iterator>::type find(typename GetPtrHelper::PtrType) const; template typename std::enable_if::value, bool>::type contains(typename GetPtrHelper::PtrType) const; template typename std::enable_if::value, MappedPeekType>::type inlineGet(typename GetPtrHelper::PtrType) const; template typename std::enable_if::value, MappedPeekType>::type get(typename GetPtrHelper::PtrType) const; template typename std::enable_if::value, bool>::type remove(typename GetPtrHelper::PtrType); template typename std::enable_if::value, MappedType>::type take(typename GetPtrHelper::PtrType); void checkConsistency() const; static bool isValidKey(const KeyType&); private: template AddResult inlineSet(K&&, V&&); template AddResult inlineAdd(K&&, V&&); template AddResult inlineEnsure(K&&, const F&); HashTableType m_impl; }; 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, U&& key, V&& mapped) { location.key = std::forward(key); location.value = std::forward(mapped); } }; template struct HashMapEnsureTranslator { 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, U&& key, const Functor& functor) { location.key = std::forward(key); location.value = functor(); } }; 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, U&& key, V&& mapped, unsigned hashCode) { Translator::translate(location.key, key, hashCode); location.value = std::forward(mapped); } }; template inline void HashMap::swap(HashMap& other) { m_impl.swap(other.m_impl); } template inline unsigned HashMap::size() const { return m_impl.size(); } template inline unsigned HashMap::capacity() const { return m_impl.capacity(); } template inline bool HashMap::isEmpty() const { return m_impl.isEmpty(); } template inline auto HashMap::begin() -> iterator { return m_impl.begin(); } template inline auto HashMap::end() -> iterator { return m_impl.end(); } template inline auto HashMap::begin() const -> const_iterator { return m_impl.begin(); } template inline auto HashMap::end() const -> const_iterator { return m_impl.end(); } template inline auto HashMap::find(const KeyType& key) -> iterator { return m_impl.find(key); } template inline auto HashMap::find(const KeyType& key) const -> const_iterator { 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 template auto HashMap::inlineSet(K&& key, V&& value) -> AddResult { AddResult result = inlineAdd(std::forward(key), std::forward(value)); if (!result.isNewEntry) { // The inlineAdd call above found an existing hash table entry; we need to set the mapped value. result.iterator->value = std::forward(value); } return result; } template template ALWAYS_INLINE auto HashMap::inlineAdd(K&& key, V&& value) -> AddResult { return m_impl.template add>(std::forward(key), std::forward(value)); } template template ALWAYS_INLINE auto HashMap::inlineEnsure(K&& key, const F& functor) -> AddResult { return m_impl.template add>(std::forward(key), functor); } template template auto HashMap::set(const KeyType& key, T&& mapped) -> AddResult { return inlineSet(key, std::forward(mapped)); } template template auto HashMap::set(KeyType&& key, T&& mapped) -> AddResult { return inlineSet(WTFMove(key), std::forward(mapped)); } template template auto HashMap::add(K&& key, V&& value) -> AddResult { return m_impl.template addPassingHashCode>(std::forward(key), std::forward(value)); } template template auto HashMap::add(const KeyType& key, T&& mapped) -> AddResult { return inlineAdd(key, std::forward(mapped)); } template template auto HashMap::add(KeyType&& key, T&& mapped) -> AddResult { return inlineAdd(WTFMove(key), std::forward(mapped)); } template template ALWAYS_INLINE auto HashMap::fastAdd(const KeyType& key, T&& mapped) -> AddResult { return inlineAdd(key, std::forward(mapped)); } template template ALWAYS_INLINE auto HashMap::fastAdd(KeyType&& key, T&& mapped) -> AddResult { return inlineAdd(WTFMove(key), std::forward(mapped)); } template template auto HashMap::ensure(const KeyType& key, const Functor& functor) -> AddResult { return inlineEnsure(key, functor); } template template auto HashMap::ensure(KeyType&& key, const Functor& functor) -> AddResult { return inlineEnsure(WTFMove(key), functor); } template auto HashMap::get(const KeyType& key) const -> MappedPeekType { KeyValuePairType* entry = const_cast(m_impl).lookup(key); if (!entry) return MappedTraits::peek(MappedTraits::emptyValue()); return MappedTraits::peek(entry->value); } template ALWAYS_INLINE auto HashMap::fastGet(const KeyType& key) const -> MappedPeekType { KeyValuePairType* entry = const_cast(m_impl).template inlineLookup(key); if (!entry) return MappedTraits::peek(MappedTraits::emptyValue()); return MappedTraits::peek(entry->value); } template inline bool HashMap::remove(iterator it) { if (it.m_impl == m_impl.end()) return false; m_impl.internalCheckTableConsistency(); m_impl.removeWithoutEntryConsistencyCheck(it.m_impl); return true; } template template inline void HashMap::removeIf(const Functor& functor) { m_impl.removeIf(functor); } template inline bool HashMap::remove(const KeyType& key) { return remove(find(key)); } template inline void HashMap::clear() { m_impl.clear(); } template auto HashMap::take(const KeyType& key) -> MappedType { iterator it = find(key); if (it == end()) return MappedTraits::emptyValue(); MappedType value = WTFMove(it->value); remove(it); return value; } template template inline auto HashMap::find(typename GetPtrHelper::PtrType key) -> typename std::enable_if::value, iterator>::type { return m_impl.template find>(key); } template template inline auto HashMap::find(typename GetPtrHelper::PtrType key) const -> typename std::enable_if::value, const_iterator>::type { return m_impl.template find>(key); } template template inline auto HashMap::contains(typename GetPtrHelper::PtrType key) const -> typename std::enable_if::value, bool>::type { return m_impl.template contains>(key); } template template inline auto HashMap::inlineGet(typename GetPtrHelper::PtrType key) const -> typename std::enable_if::value, MappedPeekType>::type { KeyValuePairType* entry = const_cast(m_impl).template inlineLookup>(key); if (!entry) return MappedTraits::peek(MappedTraits::emptyValue()); return MappedTraits::peek(entry->value); } template template auto HashMap::get(typename GetPtrHelper::PtrType key) const -> typename std::enable_if::value, MappedPeekType>::type { return inlineGet(key); } template template inline auto HashMap::remove(typename GetPtrHelper::PtrType key) -> typename std::enable_if::value, bool>::type { return remove(find(key)); } template template inline auto HashMap::take(typename GetPtrHelper::PtrType key) -> typename std::enable_if::value, MappedType>::type { iterator it = find(key); if (it == end()) return MappedTraits::emptyValue(); MappedType value = WTFMove(it->value); remove(it); return value; } template inline void HashMap::checkConsistency() const { m_impl.checkTableConsistency(); } template inline bool HashMap::isValidKey(const KeyType& key) { if (KeyTraits::isDeletedValue(key)) return false; if (HashFunctions::safeToCompareToEmptyOrDeleted) { if (key == KeyTraits::emptyValue()) return false; } else { if (isHashTraitsEmptyValue(key)) return false; } return true; } 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->key); if (bPos == notFound || it->value != bPos->value) return false; } return true; } template inline bool operator!=(const HashMap& a, const HashMap& b) { return !(a == b); } template inline void copyToVector(const HashMap& collection, Y& vector) { typedef typename HashMap::const_iterator iterator; vector.resize(collection.size()); iterator it = collection.begin(); iterator end = collection.end(); for (unsigned i = 0; it != end; ++it, ++i) vector[i] = { (*it).key, (*it).value }; } 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; #endif /* WTF_HashMap_h */