// xhash internal header
#pragma once
#ifndef _XHASH_
#define _XHASH_
#ifndef RC_INVOKED
#include <cstring>
#include <cwchar>
#include <xfunctional>
#include <list>
#include <vector>

 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)

 #ifndef _HAS_INCREMENTAL_HASH
  #define _HAS_INCREMENTAL_HASH	0	/* 1 for steady growth, 0 for leaps */
 #endif /* _HAS_INCREMENTAL_HASH */

#undef _HAS_STLPORT_EMULATION

 #pragma warning(disable: 4127)

namespace stdext {
using _STD basic_string;
using _STD less;
using _STD size_t;

		// TEMPLATE FUNCTION hash_value
 #define _HASH_SEED	(size_t)0xdeadbeef

template<class _Kty> inline
	size_t hash_value(const _Kty& _Keyval)
	{	// hash _Keyval to size_t value one-to-one
	return ((size_t)_Keyval ^ _HASH_SEED);
	}

template <class _InIt>
	inline size_t _Hash_value(_InIt _Begin, _InIt _End)
	{	// hash range of elements
	size_t _Val = 2166136261U;

	while (_Begin != _End)
		_Val = 16777619U * _Val ^ (size_t)*_Begin++;
	return (_Val);
	}

template<class _Elem,
	class _Traits,
	class _Alloc> inline
	size_t hash_value(const basic_string<_Elem, _Traits, _Alloc>& _Str)
	{	// hash string to size_t value
	const _Elem *_Ptr = _Str.c_str();

	return (_Hash_value(_Ptr, _Ptr + _Str.size()));
	}

inline size_t hash_value(_In_z_ const char *_Str)
	{	// hash NTBS to size_t value
	return (_Hash_value(_Str, _Str + _CSTD strlen(_Str)));
	}

inline size_t hash_value(_In_z_ const wchar_t *_Str)
	{	// hash NTWCS to size_t value
	return (_Hash_value(_Str, _Str + _CSTD wcslen(_Str)));
	}

 #ifdef _NATIVE_WCHAR_T_DEFINED
inline size_t hash_value(_In_z_ const unsigned short *_Str)
	{	// hash NTWCS to size_t value
	const unsigned short *_End = _Str;

	while (*_End != 0)
		++_End;
	return (_Hash_value(_Str, _End));
	}
 #endif /* _NATIVE_WCHAR_T_DEFINED */

		// TEMPLATE CLASS hash_compare
template<class _Kty,
	class _Pr = less<_Kty> >
	class hash_compare
	{	// traits class for hash containers
public:
	enum
		{	// parameters for hash table
		bucket_size = 1		// 0 < bucket_size
		};

	hash_compare()
		: comp()
		{	// construct with default comparator
		}

	hash_compare(_Pr _Pred)
		: comp(_Pred)
		{	// construct with _Pred comparator
		}

	size_t operator()(const _Kty& _Keyval) const
		{	// hash _Keyval to size_t value by pseudorandomizing transform
		long _Quot = (long)(hash_value(_Keyval) & LONG_MAX);
		ldiv_t _Qrem = _CSTD ldiv(_Quot, 127773);

		_Qrem.rem = 16807 * _Qrem.rem - 2836 * _Qrem.quot;
		if (_Qrem.rem < 0)
			_Qrem.rem += LONG_MAX;
		return ((size_t)_Qrem.rem);
		}

	bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const
		{	// test if _Keyval1 ordered before _Keyval2
		return (comp(_Keyval1, _Keyval2));
		}

protected:
	_Pr comp;	// the comparator object
	};
}	// namespace stdext
_STD_BEGIN
using stdext::hash_compare;
using stdext::hash_value;
_STD_END

_STD_BEGIN
		// TEMPLATE CLASS _Hash_compare
template<class _Kty,
	class _Hasher,
	class _Keyeq>
	class _Hash_compare
	{	// traits class for hash containers
public:
	typedef _Hasher hasher;
	enum
		{	// parameters for hash table
		bucket_size = 1	// 0 < bucket_size
		};

	_Hash_compare()
		{	// construct with default hasher and equality comparator
		}

	_Hash_compare(hasher _Hasharg)
		: _Hashobj(_Hasharg)
		{	// construct with hasher and default equality comparator
		}

	_Hash_compare(hasher _Hasharg, _Keyeq _Keyeqarg)
		: _Hashobj(_Hasharg), _Keyeqobj(_Keyeqarg)
		{	// construct with hasher and equality comparator
		}

	size_t operator()(const _Kty& _Keyval) const
		{	// hash _Keyval to size_t value
		return ((size_t)_Hashobj(_Keyval));
		}

	bool operator()(const _Kty& _Keyval1, const _Kty& _Keyval2) const
		{	// test if _Keyval1 NOT equal to _Keyval2
		return (!_Keyeqobj(_Keyval1, _Keyval2));
		}

	hasher _Hashobj;	// the hash object
	_Keyeq _Keyeqobj;	// the equality comparator object
	};

		// TEMPLATE CLASS _Hash
template<class _Traits>
	class _Hash
		: public _Traits	// traits serves as base class
	{	// hash table -- list with vector of iterators for quick access
public:
	typedef _Hash<_Traits> _Myt;
	typedef typename _Traits::_Val_type _Val_type;

	typedef typename _Traits::key_type key_type;
	typedef typename _Traits::key_compare key_compare;
	typedef typename _Traits::value_compare value_compare;
	enum
		{	// various constants
		_Bucket_size = key_compare::bucket_size,
		_Min_buckets = 8,	// min_buckets = 2 ^^ N, 0 < N
		_Multi = _Traits::_Multi};
	typedef list<typename _Traits::value_type,
		typename _Traits::allocator_type> _Mylist;

	typedef typename _Mylist::value_type value_type;
	typedef typename _Mylist::allocator_type allocator_type;
	typedef typename _Mylist::size_type size_type;
	typedef typename _Mylist::difference_type difference_type;
	typedef typename _Mylist::pointer pointer;
	typedef typename _Mylist::const_pointer const_pointer;
	typedef typename _Mylist::reference reference;
	typedef typename _Mylist::const_reference const_reference;

	typedef typename _STD tr1::conditional<
		_STD tr1::is_same<key_type, value_type>::value,
		typename _Mylist::const_iterator,
		typename _Mylist::iterator>::type iterator;
	typedef typename _Mylist::const_iterator const_iterator;

	typedef _STD reverse_iterator<iterator> reverse_iterator;
	typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;

	typedef vector<iterator,
		typename allocator_type::template
			rebind<iterator>::other> _Myvec;

	typedef pair<iterator, bool> _Pairib;
	typedef pair<iterator, iterator> _Pairii;
	typedef pair<const_iterator, const_iterator> _Paircc;

	_Hash(const key_compare& _Parg,
		const allocator_type& _Al)
		: _Traits(_Parg),
			_List(_Al),

			_Vec(_Al),

			_Max_bucket_size(_Bucket_size)
		{	// construct empty hash table
		_Init();
		}

	_Hash(const value_type *_First, const value_type *_Last,
		const key_compare& _Parg, const allocator_type& _Al)
		: _Traits(_Parg),
			_List(_Al),

			_Vec(_Al),

			_Max_bucket_size(_Bucket_size)
		{	// construct hash table from [_First, _Last) array
		_Init();
		insert(_First, _Last);
		}

	_Hash(const _Myt& _Right)
		: _Traits(_Right.comp),
			_List(_Right.get_allocator()),
			_Vec(_Right.get_allocator())
		{	// construct hash table by copying right
		_Copy(_Right);
		}

	_Hash(_Myt&& _Right)
		: _Traits(_Right.comp),
			_List(_Right.get_allocator()),
			_Vec(_Right.get_allocator())
		{	// construct hash table by copying right
		_Assign_rv(_STD forward<_Myt>(_Right));
		}

	_Myt& operator=(_Myt&& _Right)
		{	// assign by moving _Right
		_Assign_rv(_STD forward<_Myt>(_Right));
		return (*this);
		}

	void _Assign_rv(_Myt&& _Right)
		{	// assign by moving _Right
		if (this == &_Right)
			;
		else if (get_allocator() != _Right.get_allocator())
			_Xinvalid_argument("invalid hash move assign");
		else
			{	// clear this and steal from _Right
			clear();
			_Swap_adl(this->comp, _Right.comp);
			this->_List.swap(_Right._List);
			this->_Vec.swap(_Right._Vec);
			_STD swap(this->_Mask, _Right._Mask);
			_STD swap(this->_Maxidx, _Right._Maxidx);
			_STD swap(this->_Max_bucket_size, _Right._Max_bucket_size);
			}
		}

	template<class _Valty>
		_Pairib insert(_Valty&& _Val)
		{	// try to insert node with value _Val
		_List.emplace_front(_STD forward<_Valty>(_Val));
		return (_Insert(_List.front(), begin()));
		}

	template<class _Valty>
		typename _STD tr1::enable_if<!_STD tr1::is_same<const_iterator,
			typename _STD tr1::remove_reference<_Valty>::type>::value,
				iterator>::type
		insert(const_iterator, _Valty&& _Val)

		{	// try to insert node with value _Val, ignore hint
		_List.emplace_front(_STD forward<_Valty>(_Val));
		return (_Insert(_List.front(), begin()).first);
		}

	template<class _Valty>
		_Pairib emplace(_Valty&& _Val)
		{	// try to insert node with value _Val...
		_List.emplace_front(_STD forward<_Valty>(_Val));
		return (_Insert(_List.front(), begin()));
		}

	template<class _Valty>
		iterator emplace_hint(const_iterator, _Valty&& _Val)
		{	// try to insert node with value _Val..., ignore hint
		_List.emplace_front(_STD forward<_Valty>(_Val));
		return (_Insert(_List.front(), begin()).first);
		}

	void swap(_Myt&& _Right)
		{	// exchange contents with movable _Right
		_Assign_rv(_STD forward<_Myt>(_Right));
		}

	~_Hash()
		{	// destroy hash table
		}

	_Myt& operator=(const _Myt& _Right)
		{	// replace contents from _Right
		if (this != &_Right)
			_Copy(_Right);
		return (*this);
		}

	iterator begin()
		{	// return iterator for beginning of mutable sequence
		return (_List.begin());
		}

	const_iterator begin() const
		{	// return iterator for beginning of nonmutable sequence
		return (_List.begin());
		}

	iterator end()
		{	// return iterator for end of mutable sequence
		return (_List.end());
		}

	const_iterator end() const
		{	// return iterator for end of nonmutable sequence
		return (_List.end());
		}

	reverse_iterator rbegin()
		{	// return iterator for beginning of reversed mutable sequence
		return (_List.rbegin());
		}

	const_reverse_iterator rbegin() const
		{	// return iterator for beginning of reversed nonmutable sequence
		return (_List.rbegin());
		}

	reverse_iterator rend()
		{	// return iterator for end of reversed mutable sequence
		return (_List.rend());
		}

	const_reverse_iterator rend() const
		{	// return iterator for end of reversed nonmutable sequence
		return (_List.rend());
		}

 #if _HAS_CPP0X
	const_iterator cbegin() const
		{	// return iterator for beginning of nonmutable sequence
		return (((const _Myt *)this)->begin());
		}

	const_iterator cend() const
		{	// return iterator for end of nonmutable sequence
		return (((const _Myt *)this)->end());
		}

	const_reverse_iterator crbegin() const
		{	// return iterator for beginning of reversed nonmutable sequence
		return (((const _Myt *)this)->rbegin());
		}

	const_reverse_iterator crend() const
		{	// return iterator for ebd of reversed nonmutable sequence
		return (((const _Myt *)this)->rend());
		}
 #endif /* _HAS_CPP0X */

	size_type size() const
		{	// return length of sequence
		return (_List.size());
		}

	size_type max_size() const
		{	// return maximum possible length of sequence
		return (_List.max_size());
		}

	bool empty() const
		{	// return true only if sequence is empty
		return (_List.empty());
		}

	allocator_type get_allocator() const
		{	// return allocator object for values
		return (_List.get_allocator());
		}

	key_compare key_comp() const
		{	// return object for comparing keys
		return (this->comp);
		}

	value_compare value_comp() const
		{	// return object for comparing values
		return (value_compare(key_comp()));
		}

// ADDED WITH TR1
	typedef iterator local_iterator;
	typedef const_iterator const_local_iterator;

	size_type bucket_count() const
		{	// return number of buckets
		return (_Maxidx);
		}

	size_type max_bucket_count() const
		{	// return maximum number of buckets
		return (_Vec.size() / 2);
		}

	size_type bucket(const key_type& _Keyval) const
		{	// return bucket corresponding to _Key
		return (_Hashval(_Keyval));
		}

	size_type bucket_size(size_type _Bucket) const
		{	// return size of bucket _Bucket
		size_type _Ans = 0;
		if (_Bucket < _Maxidx)
			for (const_iterator _Plist = _Begin(_Bucket);
				_Plist != _End(_Bucket); ++_Plist)
				++_Ans;
		return (_Ans);
		}

	local_iterator begin(size_type _Bucket)
		{	// return iterator for bucket _Bucket
		if (_Bucket < bucket_count())
			return (_Begin(_Bucket));
		else
			return (end());
		}

	const_local_iterator begin(size_type _Bucket) const
		{	// return iterator for bucket _Bucket
		if (_Bucket < bucket_count())
			return (_Begin(_Bucket));
		else
			return (end());
		}

	local_iterator end(size_type _Bucket)
		{	// return iterator for bucket following _Bucket
		if (_Bucket < bucket_count())
			return (_End(_Bucket));
		else
			return (end());
		}

	const_local_iterator end(size_type _Bucket) const
		{	// return iterator for bucket following _Bucket
		if (_Bucket < bucket_count())
			return (_End(_Bucket));
		else
			return (end());
		}

	float load_factor() const
		{	// return elements per bucket
		return ((float)size() / (float)bucket_count());
		}

	float max_load_factor() const
		{	// return maximum elements per bucket
		return (_Max_bucket_size);
		}

	void max_load_factor(float _Newmax)
		{	// set new load factor
		if (_Newmax != _Newmax	// may detect a NaN
			|| _Newmax < 0)
			_STD _Xout_of_range("invalid hash load factor");

		_Max_bucket_size = _Newmax;
		}

	void rehash(size_type _Buckets)
		{	// rebuild table with at least _Buckets buckets
		size_type _Maxsize = _Vec.max_size() / 4;
		size_type _Newsize = _Min_buckets;

		for (; _Newsize < _Buckets && _Newsize < _Maxsize; )
			_Newsize *= 2;	// double until big enough
		if (_Newsize < _Buckets)
			_STD _Xout_of_range("invalid hash bucket count");
		for (float _Size = (float)size();
			max_load_factor() < _Size / _Newsize && _Newsize < _Maxsize; )
			_Newsize *= 2;	// double until load factor okay

		_Init(_Newsize);
		_Reinsert(end());
		}
// ADDED WITH TR1 -- END

	_Pairib insert(const value_type& _Val)
		{	// try to insert node with value _Val
		_List.push_front(_Val);
		return (_Insert(_Val, begin()));
		}

	iterator insert(const_iterator, const value_type& _Val)
		{	// try to insert node with value _Val, ignore hint
		return (insert(_Val).first);
		}

	template<class _Iter>
		void insert(_Iter _First, _Iter _Last)
		{	// insert [_First, _Last) at front, then put in place
		iterator _Oldlast = begin();
		_List.insert(begin(), _First, _Last);
		if (max_load_factor() < load_factor())
			rehash(bucket_count());
		else
			_Reinsert(_Oldlast);
		}

	iterator erase(const_iterator _Plist)
		{	// erase element at _Plist
		size_type _Bucket = _Hashval(this->_Kfn(*_Plist));

		_Erase_bucket(_List._Make_iter(_Plist), _Bucket);
		return (_List.erase(_Plist));
		}

	iterator erase(const_iterator _First, const_iterator _Last)
		{	// erase [_First, _Last)
		_DEBUG_RANGE(_First, _Last);
		if (_First == begin() && _Last == end())
			{	// erase all
			clear();
			return (begin());
			}
		else
			{	// partial erase, one at a time
			while (_First != _Last)
				erase(_First++);
			return (_List._Make_iter(_First));
			}
		}

	size_type erase(const key_type& _Keyval)
		{	// erase and count all that match _Keyval
		_Pairii _Where = equal_range(_Keyval);
		size_type _Num = 0;
		_Distance(_Where.first, _Where.second, _Num);
		erase(_Where.first, _Where.second);
		return (_Num);
		}

	void erase(const key_type *_First,
		const key_type *_Last)
		{	// erase all that match array of keys [_First, _Last)
		_DEBUG_RANGE(_First, _Last);
		for (; _First != _Last; ++_First)
			erase(*_First);
		}

	void clear()
		{	// erase all
		_List.clear();
		_Init();
		}

	iterator find(const key_type& _Keyval)
		{	// find an element in mutable hash table that matches _Keyval
		return (lower_bound(_Keyval));
		}

	const_iterator find(const key_type& _Keyval) const
		{	// find an element in nonmutable hash table that matches _Keyval
		return (lower_bound(_Keyval));
		}

	size_type count(const key_type& _Keyval) const
		{	// count all elements that match _Keyval
		_Paircc _Ans = equal_range(_Keyval);
		size_type _Num = 0;
		_Distance(_Ans.first, _Ans.second, _Num);
		return (_Num);
		}

	iterator lower_bound(const key_type& _Keyval)
		{	// find leftmost not less than _Keyval in mutable hash table
		size_type _Bucket = _Hashval(_Keyval);
		for (iterator _Where = _Begin(_Bucket);
			_Where != _End(_Bucket); ++_Where)
			if (!this->comp(this->_Kfn(*_Where), _Keyval))
				return (this->comp(_Keyval,
					this->_Kfn(*_Where)) ? end() : _Where);
		return (end());
		}

	const_iterator lower_bound(const key_type& _Keyval) const
		{	// find leftmost not less than _Keyval in nonmutable hash table
		size_type _Bucket = _Hashval(_Keyval);
		for (const_iterator _Where = _Begin(_Bucket);
			_Where != _End(_Bucket); ++_Where)
			if (!this->comp(this->_Kfn(*_Where), _Keyval))
				return (this->comp(_Keyval,
					this->_Kfn(*_Where)) ? end() : _Where);
		return (end());
		}

	iterator upper_bound(const key_type& _Keyval)
		{	// find leftmost not greater than _Keyval in mutable hash table
		size_type _Bucket = _Hashval(_Keyval);
		for (iterator _Where = _End(_Bucket);
			_Where != _Begin(_Bucket); )
			if (!this->comp(_Keyval, this->_Kfn(*--_Where)))
				return (this->comp(this->_Kfn(*_Where),
					_Keyval) ? end() : (iterator)++_Where);
		return (end());
		}

	const_iterator upper_bound(const key_type& _Keyval) const
		{	// find leftmost not greater than _Keyval in nonmutable hash table
		size_type _Bucket = _Hashval(_Keyval);
		for (const_iterator _Where = _End(_Bucket);
			_Where != _Begin(_Bucket); )
			if (!this->comp(_Keyval, this->_Kfn(*--_Where)))
				return (this->comp(this->_Kfn(*_Where),
					_Keyval) ? end() : (const_iterator)++_Where);
		return (end());
		}

	_Pairii equal_range(const key_type& _Keyval)
		{	// find range equivalent to _Keyval in mutable hash table
		size_type _Bucket = _Hashval(_Keyval);
		for (iterator _Where = _Begin(_Bucket);
			_Where != _End(_Bucket); ++_Where)
			if (!this->comp(this->_Kfn(*_Where), _Keyval))
				{	// found _First, look for end of range
				iterator _First = _Where;
				for (; _Where != _End(_Bucket); ++_Where)
					if (this->comp(_Keyval, this->_Kfn(*_Where)))
						break;
				if (_First == _Where)
					break;
				return (_Pairii(_First, _Where));
				}
		return (_Pairii(end(), end()));
		}

	_Paircc equal_range(const key_type& _Keyval) const
		{	// find range equivalent to _Keyval in nonmutable hash table
		size_type _Bucket = _Hashval(_Keyval);
		for (const_iterator _Where = _Begin(_Bucket);
			_Where != _End(_Bucket); ++_Where)
			if (!this->comp(this->_Kfn(*_Where), _Keyval))
				{	// found _First, look for end of range
				const_iterator _First = _Where;
				for (; _Where != _End(_Bucket); ++_Where)
					if (this->comp(_Keyval, this->_Kfn(*_Where)))
						break;
				if (_First == _Where)
					break;
				return (_Paircc(_First, _Where));
				}
		return (_Paircc(end(), end()));
		}

	void swap(_Myt& _Right)
		{	// exchange contents with _Right
		if (this != &_Right)
			{	// different, worth swapping
			_Swap_adl(this->comp, _Right.comp);
			this->_List.swap(_Right._List);
			this->_Vec.swap(_Right._Vec);
			_STD swap(this->_Mask, _Right._Mask);
			_STD swap(this->_Maxidx, _Right._Maxidx);
			_STD swap(this->_Max_bucket_size, _Right._Max_bucket_size);
			}
		}

protected:
	iterator& _Vec_lo(size_type _Bucket)
		{	// return reference to begin() for _Bucket
		return (_Vec[2 * _Bucket]);
		}

	const iterator& _Vec_lo(size_type _Bucket) const
		{	// return reference to begin() for _Bucket
		return (_Vec[2 * _Bucket]);
		}

	iterator& _Vec_hi(size_type _Bucket)
		{	// return reference to end()-1 for _Bucket
		return (_Vec[2 * _Bucket + 1]);
		}

	const iterator& _Vec_hi(size_type _Bucket) const
		{	// return reference to end()-1 for _Bucket
		return (_Vec[2 * _Bucket + 1]);
		}

	iterator _Begin(size_type _Bucket)
		{	// return begin iterator for bucket _Bucket
		return (_Vec_lo(_Bucket));
		}

	const_iterator _Begin(size_type _Bucket) const
		{	// return begin iterator for bucket _Bucket
		return (_Vec_lo(_Bucket));
		}

	iterator _End(size_type _Bucket)
		{	// return end iterator for bucket _Bucket
		if (_Vec_lo(_Bucket) == end())
			return (end());
		else
			{	// point past last element
			iterator _Ans = _Vec_hi(_Bucket);
			return (++_Ans);
			}
		}

	const_iterator _End(size_type _Bucket) const
		{	// return end iterator for bucket _Bucket
		if (_Vec_lo(_Bucket) == end())
			return (_List._Make_iter(end()));
		else
			{	// point past last element
			iterator _Ans = _Vec_hi(_Bucket);
			return (++_Ans);
			}
		}

	void _Erase_bucket(iterator _Plist, size_type _Bucket)
		{	// fix iterators before erasing _Plist before _Where
		if (_Vec_hi(_Bucket) == _Plist)
			if (_Vec_lo(_Bucket) == _Plist)
				{	// make bucket empty
				_Vec_lo(_Bucket) = end();
				_Vec_hi(_Bucket) = end();
				}
			else
				_Vec_hi(_Bucket) = --_Plist;	// move end back one element
		else if (_Vec_lo(_Bucket) == _Plist)
			_Vec_lo(_Bucket) = ++_Plist;	// move beginning up one element
		}

	void _Insert_bucket(iterator _Plist, iterator _Where,
		size_type _Bucket)
		{	// fix iterators after inserting _Plist before _Where
		if (_Vec_lo(_Bucket) == end())
			{	// make bucket non-empty
			_Vec_lo(_Bucket) = _Plist;
			_Vec_hi(_Bucket) = _Plist;
			}
		else if (_Vec_lo(_Bucket) == _Where)
			_Vec_lo(_Bucket) = _Plist;	// move beginning back one element
		else if (++_Vec_hi(_Bucket) != _Plist)	// move end up one element
			--_Vec_hi(_Bucket);	// or not
		}

	void _Copy(const _Myt& _Right)
		{	// copy entire hash table
		_Mask = _Right._Mask;
		_Maxidx = _Right._Maxidx;
		_Max_bucket_size = _Right._Max_bucket_size;
		_List.clear();

		_TRY_BEGIN
		_List.insert(end(), _Right.begin(), _Right.end());
		this->comp = _Right.comp;
		_CATCH_ALL
		_List.clear();	// list or compare copy failed, bail out
		_RERAISE;
		_CATCH_END

		_Vec.assign(_Right._Vec.size(), end());
		_Reinsert(end());
		}

	void _Grow()
		{	// grow hash table by one bucket
		if (_Vec.size() / 2 <= _Maxidx)
			{	// table full, double its size
			_Mask = _Vec.size() - 1;
			_Vec.insert(_Vec.end(), _Vec.size(), end());
			}
		else if (_Mask < _Maxidx)
			_Mask = (_Mask << 1) + 1;

		size_type _Bucket = _Maxidx - (_Mask >> 1) - 1;
		for (iterator _Plist = _Begin(_Bucket);
			_Plist != _End(_Bucket); )
			{	// split old bucket
			size_type _Newbucket =
				this->comp(this->_Kfn(*_Plist)) & _Mask;
			if (_Newbucket == _Bucket)
				++_Plist;	// leave element in old bucket

 #if _ITERATOR_DEBUG_LEVEL == 2
			else if (_Newbucket != _Maxidx)
				_DEBUG_ERROR("bad hash function");
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */

			else
				{	// move element to new bucket
				iterator _Where = _Plist;

				++_Where;
				_Erase_bucket(_Plist, _Bucket);
				_List._Splice_same(end(), _List, _Plist, _Where, 0);
				_Insert_bucket(_Plist, end(), _Newbucket);
				_Plist = _Where;
				}
			}
		++_Maxidx;	// open new bucket for hash lookup
		}

	size_type _Hashval(const key_type& _Keyval) const
		{	// return hash value, masked and wrapped to current table size
		size_type _Num = this->comp(_Keyval) & _Mask;
		if (_Maxidx <= _Num)
			_Num -= (_Mask >> 1) + 1;
		return (_Num);
		}

	void _Init(size_type _Buckets = _Min_buckets)
		{	// initialize hash table with _Buckets buckets, leave list alone
		_Vec.assign(2 * _Buckets, end());
		_Mask = _Buckets - 1;
		_Maxidx = _Buckets;
		}

	_Pairib _Insert(const value_type& _Val, iterator _Plist)
		{	// try to insert existing node with value _Val
		size_type _Bucket = _Hashval(this->_Kfn(_Val));
		iterator _Where = _End(_Bucket);

		for (; _Where != _Begin(_Bucket); )
			if (this->comp(this->_Kfn(_Val), this->_Kfn(*--_Where)))
				;	// still too high in bucket list
			else if (_Multi
				|| this->comp(this->_Kfn(*_Where), this->_Kfn(_Val)))
				{	// found insertion point, back up to it
				++_Where;
				break;
				}
			else
				{	// discard new list element and return existing
				_List.erase(_Plist);
				return (_Pairib(_Where, false));
				}

		iterator _Next = _Plist;
		if (_Where != ++_Next)
			_List._Splice_same(_Where, _List,
				_Plist, _Next, 1);	// move element into place

		_Insert_bucket(_Plist, _Where, _Bucket);
		_Check_size();
		return (_Pairib(_Plist, true));	// return iterator for new element
		}

	void _Check_size()
		{	// grow table as needed
		if (max_load_factor() < load_factor())

 #if _HAS_INCREMENTAL_HASH
			_Grow();	// too dense, need to grow hash table

 #else /* _HAS_INCREMENTAL_HASH */
			{	// rehash to bigger table
			size_type _Maxsize = _Vec.max_size() / 2;
			size_type _Newsize = bucket_count();

			for (int _Idx = 0; _Idx < 3 && _Newsize < _Maxsize; ++_Idx)
				_Newsize *= 2;	// multiply safely by 8
			_Init(_Newsize);
			_Reinsert(end());
			}
 #endif /* _HAS_INCREMENTAL_HASH */
		}

	void _Reinsert(iterator _Last)
		{	// insert elements in [begin(), _Last)
		if (begin() != _Last)
			for (--_Last; ; )
				{	// reinsert elements in [begin(), _Last]
				iterator _First = begin();
				bool _Done = _First == _Last;
				_Insert(*_First, _First);
				if (_Done)
					break;
				}
		}

	_Mylist _List;	// list of elements, must initialize before _Vec
	_Myvec _Vec;	// vector of list iterators, begin() then end()-1
	size_type _Mask;	// the key mask
	size_type _Maxidx;	// current maximum key value
	float _Max_bucket_size;	// current maximum bucket size
	};

		// _Hash TEMPLATE OPERATORS
template<class _Ty> inline
	bool operator==(const _Hash<_Ty>& _Left, const _Hash<_Ty>& _Right)
	{	// test for hash table equality
	return (_Left.size() == _Right.size()
		&& equal(_Left.begin(), _Left.end(), _Right.begin()));
	}

template<class _Ty> inline
	bool operator!=(const _Hash<_Ty>& _Left, const _Hash<_Ty>& _Right)
	{	// test for hash table inequality
	return (!(_Left == _Right));
	}

template<class _Ty> inline
	bool operator<(const _Hash<_Ty>& _Left, const _Hash<_Ty>& _Right)
	{	// test if _Left < _Right for hash tables
	return (lexicographical_compare(_Left.begin(), _Left.end(),
		_Right.begin(), _Right.end()));
	}

template<class _Ty> inline
	bool operator>(const _Hash<_Ty>& _Left, const _Hash<_Ty>& _Right)
	{	// test if _Left > _Right for hash tables
	return (_Right < _Left);
	}

template<class _Ty> inline
	bool operator<=(const _Hash<_Ty>& _Left, const _Hash<_Ty>& _Right)
	{	// test if _Left <= _Right for hash tables
	return (!(_Right < _Left));
	}

template<class _Ty> inline
	bool operator>=(const _Hash<_Ty>& _Left, const _Hash<_Ty>& _Right)
	{	// test if _Left >= _Right for hash tables
	return (!(_Left < _Right));
	}
_STD_END

 #pragma warning(pop)
 #pragma pack(pop)

#endif /* RC_INVOKED */
#endif /* _XHASH_ */

/*
 * Copyright (c) 1992-2009 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
V5.20:0009 */
