/*
 * 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.
 *
 */

#pragma once

#include <initializer_list>
#include <wtf/GetPtr.h>
#include <wtf/HashTable.h>
#include <wtf/IteratorRange.h>

namespace WTF {

template<typename KeyTraits, typename MappedTraits> 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.first; }
};

template<typename KeyArg, typename MappedArg, typename HashArg = typename DefaultHash<KeyArg>::Hash,
	typename KeyTraitsArg = HashTraits<KeyArg>, typename MappedTraitsArg = HashTraits<MappedArg> >
class HashMap {
	WTF_MAKE_FAST_ALLOCATED;
private:
	typedef KeyTraitsArg KeyTraits;
	typedef MappedTraitsArg MappedTraits;

	struct KeyValuePairTraits : KeyValuePairHashTraits<KeyTraits, MappedTraits> {
		static const bool hasIsEmptyValueFunction = true;
		static ALWAYS_INLINE bool isEmptyValue(const typename KeyValuePairHashTraits<KeyTraits, MappedTraits>::TraitType& value)
		{
			return isHashTraitsEmptyValue<KeyTraits>(value.key);
		}
	};
	typedef HashMapValueTraits<KeyTraits, MappedTraits> ValueTraits;

public:
	typedef typename KeyTraits::TraitType KeyType;
	typedef typename MappedTraits::TraitType MappedType;
	typedef typename KeyValuePairTraits::TraitType KeyValuePairType;

private:
	typedef typename MappedTraits::PeekType MappedPeekType;
    typedef typename MappedTraits::TakeType MappedTakeType;

	typedef HashArg HashFunctions;

	typedef HashTable<KeyType, KeyValuePairType, KeyValuePairKeyExtractor<KeyValuePairType>,
		HashFunctions, KeyValuePairTraits, KeyTraits> HashTableType;

	class HashMapKeysProxy;
	class HashMapValuesProxy;

public:
	typedef HashTableIteratorAdapter<HashTableType, KeyValuePairType> iterator;
	typedef HashTableConstIteratorAdapter<HashTableType, KeyValuePairType> const_iterator;
	typedef typename HashTableType::AddResult AddResult;

public:
    HashMap()
    {
    }

    HashMap(std::initializer_list<KeyValuePairType> initializerList)
    {
        reserveInitialCapacity(initializerList.size());
        for (const auto& keyValuePair : initializerList)
            add(keyValuePair.first, keyValuePair.second);
    }

	void swap(HashMap&);

	int size() const;
	int capacity() const;
	bool isEmpty() const;

    void reserveInitialCapacity(unsigned keyCount) { m_impl.reserveInitialCapacity(keyCount); }

	// iterators iterate over pairs of keys and values
	iterator begin();
	iterator end();
	const_iterator begin() const;
	const_iterator end() const;

	iterator random() { return m_impl.random(); }
	const_iterator random() const { return m_impl.random(); }

	ALWAYS_INLINE HashMapKeysProxy& keys() { return static_cast<HashMapKeysProxy&>(*this); }
	ALWAYS_INLINE const HashMapKeysProxy& keys() const { return static_cast<const HashMapKeysProxy&>(*this); }

	ALWAYS_INLINE HashMapValuesProxy& values() { return static_cast<HashMapValuesProxy&>(*this); }
	ALWAYS_INLINE const HashMapValuesProxy& values() const { return static_cast<const HashMapValuesProxy&>(*this); }

	iterator find(const KeyType&);
	const_iterator find(const KeyType&) const;
	iterator inlineFind(const KeyType&);
	const_iterator inlineFind(const KeyType&) const;
	bool contains(const KeyType&) const;
	bool inlineContains(const KeyType&) const;
	MappedPeekType inlineGet(const KeyType&) const;
	MappedPeekType get(const KeyType&) const;

	// Same as get(), but aggressively inlined.
	MappedPeekType fastGet(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
	template<typename V> AddResult set(const KeyType&, V&&);
	template<typename V> AddResult set(KeyType&&, V&&);

	// 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
	template<typename V> AddResult add(const KeyType&, V&&);
	template<typename V> AddResult add(KeyType&&, V&&);

	// Same as add(), but aggressively inlined.
	template<typename V> AddResult fastAdd(const KeyType&, V&&);
	template<typename V> AddResult fastAdd(KeyType&&, V&&);

	template<typename Functor> AddResult ensure(const KeyType&, const Functor&);
	template<typename Functor> AddResult ensure(KeyType&&, const Functor&);

	bool remove(const KeyType&);
	bool remove(iterator);
	template<typename Functor>
	bool removeIf(const Functor& functor);
	void clear();

	MappedTakeType 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 HashTranslator, typename T> iterator find(const T&);
    template<typename HashTranslator, typename T> const_iterator find(const T&) const;
    template<typename HashTranslator, typename T> bool contains(const T&) const;
	template<typename HashTranslator, typename T> bool inlineContains(const T&) const;
    template<typename HashTranslator, typename T> MappedPeekType get(const T&) const;
    template<typename HashTranslator, typename T> MappedPeekType inlineGet(const T&) const;
    template<typename HashTranslator, typename T> bool remove(const T&);

	// 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 HashTranslator, typename K, typename V> AddResult add(K&&, V&&);

    // Overloads for smart pointer keys that take the raw pointer type as the parameter.
    template<typename K = KeyType> typename std::enable_if<IsSmartPtr<K>::value, iterator>::type find(typename GetPtrHelper<K>::PtrType);
    template<typename K = KeyType> typename std::enable_if<IsSmartPtr<K>::value, const_iterator>::type find(typename GetPtrHelper<K>::PtrType) const;
    template<typename K = KeyType> typename std::enable_if<IsSmartPtr<K>::value, bool>::type contains(typename GetPtrHelper<K>::PtrType) const;
    template<typename K = KeyType> typename std::enable_if<IsSmartPtr<K>::value, MappedPeekType>::type inlineGet(typename GetPtrHelper<K>::PtrType) const;
    template<typename K = KeyType> typename std::enable_if<IsSmartPtr<K>::value, MappedPeekType>::type get(typename GetPtrHelper<K>::PtrType) const;
    template<typename K = KeyType> typename std::enable_if<IsSmartPtr<K>::value, bool>::type remove(typename GetPtrHelper<K>::PtrType);
    template<typename K = KeyType> typename std::enable_if<IsSmartPtr<K>::value, MappedTakeType>::type take(typename GetPtrHelper<K>::PtrType);

	void checkConsistency() const;

	static bool isValidKey(const KeyType&);

private:
	template<typename K, typename V>
	AddResult inlineSet(K&&, V&&);

	template<typename K, typename V>
	AddResult inlineAdd(K&&, V&&);

	template<typename K, typename F>
	AddResult inlineEnsure(K&&, const F&);

	HashTableType m_impl;
};

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
class HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::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>
class HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::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> struct HashMapValueTraits : KeyValuePairHashTraits<KeyTraits, MappedTraits> {
	static const bool hasIsEmptyValueFunction = true;
	static bool isEmptyValue(const typename KeyValuePairHashTraits<KeyTraits, MappedTraits>::TraitType& value)
	{
		return isHashTraitsEmptyValue<KeyTraits>(value.first);
	}
};

template<typename ValueTraits, typename HashFunctions>
struct HashMapTranslator {
	template<typename T> static ALWAYS_INLINE unsigned hash(const T& key) { return HashFunctions::hash(key); }
	template<typename T, typename U> static ALWAYS_INLINE 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, U&& key, V&& mapped)
	{
        ValueTraits::KeyTraits::assignToEmpty(location.first, std::forward<U>(key));
        ValueTraits::ValueTraits::assignToEmpty(location.second, std::forward<V>(mapped));
    }
};

template<typename ValueTraits, typename HashFunctions>
struct HashMapEnsureTranslator {
	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 Functor> static void translate(T& location, U&& key, const Functor& functor)
	{
        ValueTraits::KeyTraits::assignToEmpty(location.first, std::forward<U>(key));
        ValueTraits::ValueTraits::assignToEmpty(location.second, functor());
    }
};

template<typename ValueTraits, typename Translator>
struct HashMapTranslatorAdapter {
	template<typename T> static ALWAYS_INLINE unsigned hash(const T& key) { return Translator::hash(key); }
	template<typename T, typename U> static ALWAYS_INLINE 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, U&& key, V&& mapped, unsigned hashCode)
	{
		Translator::translate(location.first, key, hashCode);
		location.second = std::forward<V>(mapped);
	}
};

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE void HashMap<T, U, V, W, X>::swap(HashMap& other)
{
	m_impl.swap(other.m_impl);
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE int HashMap<T, U, V, W, X>::size() const
{
	return m_impl.size();
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE int HashMap<T, U, V, W, X>::capacity() const
{
	return m_impl.capacity();
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE bool HashMap<T, U, V, W, X>::isEmpty() const
{
	return m_impl.isEmpty();
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::begin()
{
	return m_impl.begin();
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::end()
{
	return m_impl.end();
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::begin() const
{
	return m_impl.begin();
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::end() const
{
	return m_impl.end();
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::find(const KeyType& key)
{
	return m_impl.find(key);
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::find(const KeyType& key) const
{
	return m_impl.find(key);
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::iterator HashMap<T, U, V, W, X>::inlineFind(const KeyType& key)
{
	return m_impl.inlineFind(key);
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE typename HashMap<T, U, V, W, X>::const_iterator HashMap<T, U, V, W, X>::inlineFind(const KeyType& key) const
{
	return m_impl.inlineFind(key);
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE bool HashMap<T, U, V, W, X>::contains(const KeyType& key) const
{
	return m_impl.contains(key);
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE bool HashMap<T, U, V, W, X>::inlineContains(const KeyType& key) const
{
	return m_impl.inlineContains(key);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename HashTranslator, typename TYPE>
ALWAYS_INLINE bool
HashMap<T, U, V, W, X>::inlineContains(const TYPE& value) const
{
	return m_impl.template inlineContains<HashMapTranslatorAdapter<KeyValuePairTraits, HashTranslator> >(value);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename HashTranslator, typename TYPE>
inline auto HashMap<T, U, V, W, X>::find(const TYPE& value) -> iterator
{
    return m_impl.template find<HashMapTranslatorAdapter<KeyValuePairTraits, HashTranslator>>(value);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename HashTranslator, typename TYPE>
inline auto HashMap<T, U, V, W, X>::find(const TYPE& value) const -> const_iterator
{
    return m_impl.template find<HashMapTranslatorAdapter<KeyValuePairTraits, HashTranslator>>(value);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename HashTranslator, typename TYPE>
auto HashMap<T, U, V, W, X>::get(const TYPE& value) const -> MappedPeekType
{
    auto* entry = const_cast<HashTableType&>(m_impl).template lookup<HashMapTranslatorAdapter<KeyValuePairTraits, HashTranslator>>(value);
    if (!entry)
        return MappedTraits::peek(MappedTraits::emptyValue());
    return MappedTraits::peek(entry->second);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename HashTranslator, typename TYPE>
auto HashMap<T, U, V, W, X>::inlineGet(const TYPE& value) const -> MappedPeekType
{
    auto* entry = const_cast<HashTableType&>(m_impl).template inlineLookup<HashMapTranslatorAdapter<KeyValuePairTraits, HashTranslator>>(value);
    if (!entry)
        return MappedTraits::peek(MappedTraits::emptyValue());
    return MappedTraits::peek(entry->second);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename HashTranslator, typename TYPE>
inline bool HashMap<T, U, V, W, X>::contains(const TYPE& value) const
{
    return m_impl.template contains<HashMapTranslatorAdapter<KeyValuePairTraits, HashTranslator>>(value);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename HashTranslator, typename TYPE>
inline bool HashMap<T, U, V, W, X>::remove(const TYPE& value)
{
    auto it = find<HashTranslator>(value);
    if (it == end())
        return false;
    remove(it);
    return true;
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename K, typename V>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::inlineSet(K&& key, V&& value) -> AddResult
{
	AddResult result = inlineAdd(std::forward<K>(key), std::forward<V>(value));
	if (!result.isNewEntry) {
		// The inlineAdd call above found an existing hash table entry; we need to set the mapped value.
		result.iterator->second = std::forward<V>(value);
	}
	return result;
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename K, typename V>
ALWAYS_INLINE auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::inlineAdd(K&& key, V&& value) -> AddResult
{
	return m_impl.template add<HashMapTranslator<ValueTraits, HashFunctions>>(std::forward<K>(key), std::forward<V>(value));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename K, typename F>
ALWAYS_INLINE auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::inlineEnsure(K&& key, const F& functor) -> AddResult
{
	return m_impl.template add<HashMapEnsureTranslator<KeyValuePairTraits, HashFunctions>>(std::forward<K>(key), functor);
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename T>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::set(const KeyType& key, T&& mapped) -> AddResult
{
	return inlineSet(key, std::forward<T>(mapped));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename T>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::set(KeyType&& key, T&& mapped) -> AddResult
{
	return inlineSet(WTFMove(key), std::forward<T>(mapped));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename HashTranslator, typename K, typename V>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::add(K&& key, V&& value) -> AddResult
{
	return m_impl.template addPassingHashCode<HashMapTranslatorAdapter<ValueTraits, HashTranslator>>(std::forward<K>(key), std::forward<V>(value));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename T>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::add(const KeyType& key, T&& mapped) -> AddResult
{
	return inlineAdd(key, std::forward<T>(mapped));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename T>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::add(KeyType&& key, T&& mapped) -> AddResult
{
	return inlineAdd(WTFMove(key), std::forward<T>(mapped));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename T>
ALWAYS_INLINE auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::fastAdd(const KeyType& key, T&& mapped) -> AddResult
{
	return inlineAdd(key, std::forward<T>(mapped));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename T>
ALWAYS_INLINE auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::fastAdd(KeyType&& key, T&& mapped) -> AddResult
{
	return inlineAdd(WTFMove(key), std::forward<T>(mapped));
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename Functor>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::ensure(const KeyType& key, const Functor& functor) -> AddResult
{
	return inlineEnsure(key, functor);
}

template<typename KeyArg, typename MappedArg, typename HashArg, typename KeyTraitsArg, typename MappedTraitsArg>
template<typename Functor>
auto HashMap<KeyArg, MappedArg, HashArg, KeyTraitsArg, MappedTraitsArg>::ensure(KeyType&& key, const Functor& functor) -> AddResult
{
	return inlineEnsure(WTFMove(key), functor);
}

template<typename T, typename U, typename V, typename W, typename MappedTraits>
typename HashMap<T, U, V, W, MappedTraits>::MappedPeekType
inline HashMap<T, U, V, W, MappedTraits>::inlineGet(const KeyType& key) const
{
	KeyValuePairType* entry = const_cast<HashTableType&>(m_impl).lookup(key);
	if (!entry)
		return MappedTraits::peek(MappedTraits::emptyValue());
	return MappedTraits::peek(entry->second);
}

template<typename T, typename U, typename V, typename W, typename MappedTraits>
typename HashMap<T, U, V, W, MappedTraits>::MappedPeekType
HashMap<T, U, V, W, MappedTraits>::get(const KeyType& key) const
{
    return inlineGet(key);
}

template<typename T, typename U, typename V, typename W, typename MappedTraits>
ALWAYS_INLINE
typename HashMap<T, U, V, W, MappedTraits>::MappedPeekType
HashMap<T, U, V, W, MappedTraits>::fastGet(const KeyType& key) const
{
	KeyValuePairType* entry = const_cast<HashTableType&>(m_impl).template inlineLookup<typename HashTableType::IdentityTranslatorType>(key);
	if (!entry)
		return MappedTraits::peek(MappedTraits::emptyValue());
	return MappedTraits::peek(entry->second);
}

template<typename T, typename U, typename V, typename W, typename X>
inline bool HashMap<T, U, V, W, X>::remove(iterator it)
{
	if (it.m_impl == m_impl.end())
		return false;
	m_impl.internalCheckTableConsistency();
	m_impl.removeWithoutEntryConsistencyCheck(it.m_impl);
	return true;
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename Functor>
inline bool HashMap<T, U, V, W, X>::removeIf(const Functor& functor)
{
	return m_impl.removeIf(functor);
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE bool HashMap<T, U, V, W, X>::remove(const KeyType& key)
{
	return remove(find(key));
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE void HashMap<T, U, V, W, X>::clear()
{
	m_impl.clear();
}

template<typename T, typename U, typename V, typename W, typename MappedTraits>
auto HashMap<T, U, V, W, MappedTraits>::take(const KeyType& key) -> MappedTakeType
{
	iterator it = find(key);
	if (it == end())
        return MappedTraits::take(MappedTraits::emptyValue());
    auto value = MappedTraits::take(WTFMove(it->second));
	remove(it);
	return value;
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename K>
inline auto HashMap<T, U, V, W, X>::find(typename GetPtrHelper<K>::PtrType key) -> typename std::enable_if<IsSmartPtr<K>::value, iterator>::type
{
    return m_impl.template find<HashMapTranslator<KeyValuePairTraits, HashFunctions>>(key);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename K>
inline auto HashMap<T, U, V, W, X>::find(typename GetPtrHelper<K>::PtrType key) const -> typename std::enable_if<IsSmartPtr<K>::value, const_iterator>::type
{
    return m_impl.template find<HashMapTranslator<KeyValuePairTraits, HashFunctions>>(key);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename K>
inline auto HashMap<T, U, V, W, X>::contains(typename GetPtrHelper<K>::PtrType key) const -> typename std::enable_if<IsSmartPtr<K>::value, bool>::type
{
    return m_impl.template contains<HashMapTranslator<KeyValuePairTraits, HashFunctions>>(key);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename K>
inline auto HashMap<T, U, V, W, X>::inlineGet(typename GetPtrHelper<K>::PtrType key) const -> typename std::enable_if<IsSmartPtr<K>::value, MappedPeekType>::type
{
    KeyValuePairType* entry = const_cast<HashTableType&>(m_impl).template inlineLookup<HashMapTranslator<KeyValuePairTraits, HashFunctions>>(key);
    if (!entry)
        return MappedTraits::peek(MappedTraits::emptyValue());
    return MappedTraits::peek(entry->second);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename K>
auto HashMap<T, U, V, W, X>::get(typename GetPtrHelper<K>::PtrType key) const -> typename std::enable_if<IsSmartPtr<K>::value, MappedPeekType>::type
{
    return inlineGet(key);
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename K>
inline auto HashMap<T, U, V, W, X>::remove(typename GetPtrHelper<K>::PtrType key) -> typename std::enable_if<IsSmartPtr<K>::value, bool>::type
{
    return remove(find(key));
}

template<typename T, typename U, typename V, typename W, typename X>
template<typename K>
inline auto HashMap<T, U, V, W, X>::take(typename GetPtrHelper<K>::PtrType key) -> typename std::enable_if<IsSmartPtr<K>::value, MappedTakeType>::type
{
    iterator it = find(key);
    if (it == end())
        return MappedTraits::take(MappedTraits::emptyValue());
    auto value = MappedTraits::take(WTFMove(it->second));
    remove(it);
    return value;
}

template<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE void HashMap<T, U, V, W, X>::checkConsistency() const
{
	m_impl.checkTableConsistency();
}

template<typename T, typename U, typename V, typename W, typename X>
inline bool HashMap<T, U, V, W, X>::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 operator==(const HashMap<T, U, V, W, X>& a, const HashMap<T, U, V, W, X>& b)
{
	if (a.size() != b.size())
		return false;

	typedef typename HashMap<T, U, V, W, X>::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<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE bool operator!=(const HashMap<T, U, V, W, X>& a, const HashMap<T, U, V, W, X>& b)
{
	return !(a == b);
}

template<typename HashTableType>
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<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE void deleteAllValues(const HashMap<T, U, V, W, X>& collection)
{
	deleteAllPairSeconds(collection);
}

template<typename HashTableType>
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<typename T, typename U, typename V, typename W, typename X>
ALWAYS_INLINE void deleteAllKeys(const HashMap<T, U, V, W, X>& collection)
{
	deleteAllPairFirsts(collection);
}

template<typename T, typename U, typename V, typename W, typename X, typename Y>
inline void copyKeysToVector(const HashMap<T, U, V, W, X>& collection, Y& vector)
{
	typedef typename HashMap<T, U, V, W, X>::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>
inline Vector<T> copyKeysToVector(const HashMap<T, U, V, W, X>& collection)
{
	typedef typename HashMap<T, U, V, W, X>::const_iterator::Keys iterator;

	Vector<T> vector(collection.size());

	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;

	return vector;
}

template<typename T, typename U, typename V, typename W, typename X, typename Y>
inline void copyValuesToVector(const HashMap<T, U, V, W, X>& collection, Y& vector)
{
	typedef typename HashMap<T, U, V, W, X>::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;
}

template<typename T, typename U, typename V, typename W, typename X>
inline Vector<U> copyValuesToVector(const HashMap<T, U, V, W, X>& collection)
{
	typedef typename HashMap<T, U, V, W, X>::const_iterator::Values iterator;

	Vector<U> vector(collection.size());

	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;

	return vector;
}

} // namespace WTF

using WTF::HashMap;
