// xhash internal header
#ifndef _XHASH_
#define _XHASH_
#include <cstring>
#include <cwchar>
#include <functional>
#include <list>
#include <vector>

#undef _HAS_STLPORT_EMULATION

 #if _HAS_TRADITIONAL_STL
 #define _HAS_STLPORT_EMULATION	1
 #endif /* _HAS_TRADITIONAL_STL */

_STD_BEGIN

		// 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);
	}

 #if _IS_EMBEDDED
inline size_t _Hash_value(const char *_Begin, const char *_End)
	{	// hash range of char
	size_t _Val = 2166136261U;
	while(_Begin != _End)
		_Val = 16777619U * _Val ^ (size_t)*_Begin++;
	return (_Val);
	}

inline size_t _Hash_value(const wchar_t *_Begin, const wchar_t *_End)
	{	// hash range of wchar_t
	size_t _Val = 2166136261U;
	while(_Begin != _End)
		_Val = 16777619U * _Val ^ (size_t)*_Begin++;
	return (_Val);
	}

inline size_t hash_value(const string& _Str)
	{	// hash string to size_t value
	return (_Hash_value(_Str.begin(), _Str.end()));
	}

 #else /* _IS_EMBEDDED */
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);
	}

export
template<class _Elem,
	class _Traits,
	class _Alloc>
	size_t hash_value(const basic_string<_Elem, _Traits, _Alloc>& _Str);
 #endif /* _IS_EMBEDDED */

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

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

		// 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 = 4,	// 0 < bucket_size
		min_buckets = 8};	// min_buckets = 2 ^^ N, 0 < N

	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
		ldiv_t _Qrem = ldiv((size_t)hash_value(_Keyval), 127773);
		_Qrem.rem = 16807 * _Qrem.rem - 2836 * _Qrem.quot;
		if (_Qrem.rem < 0)
			_Qrem.rem += 2147483647;
		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
	};

 #if _HAS_TRADITIONAL_STL
		// TEMPLATE CLASS hash for STLport
template<class _Kty>
	class hash
	{	// traits class for hash function
public:
	size_t operator()(const _Kty& _Keyval) const
		{	// hash _Keyval to size_t value by pseudorandomizing transform
		ldiv_t _Qrem = ldiv((size_t)hash_value(_Keyval), 127773);
		_Qrem.rem = 16807 * _Qrem.rem - 2836 * _Qrem.quot;
		if (_Qrem.rem < 0)
			_Qrem.rem += 2147483647;
		return ((size_t)_Qrem.rem);
		}
	};

		// TEMPLATE CLASS _Hash_compare for STLport
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 = 4,	// 0 < bucket_size
		min_buckets = 8};	// min_buckets = 2 ^^ N, 0 < N

	_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
	};
 #endif /* _HAS_TRADITIONAL_STL */

		// TEMPLATE CLASS _Hash
export
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::key_type key_type;
	typedef typename _Traits::key_compare key_compare;
	typedef typename _Traits::value_compare value_compare;
	enum
		{	// hoist constants from key_compare
		_Bucket_size = key_compare::bucket_size,
		min_buckets = key_compare::min_buckets,
		_Multi = _Traits::_Multi};
	typedef list<typename _Traits::value_type,
		typename _Traits::allocator_type> _Mylist;

	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;

 #if _HAS_IMMUTABLE_SETS
	typedef typename _Mylist::iterator _Myiterator;
	typedef typename _Traits::_ITptr _ITptr;
	typedef typename _Traits::_IReft _IReft;

		// CLASS iterator
	class iterator
		: public _Myiterator
		{	// possibly non-mutable iterator
	public:
		typedef _ITptr pointer;
		typedef _IReft reference;

		iterator()
			{	// construct default iterator
			}

		iterator(const _Myiterator& _Iter)
			: _Myiterator(_Iter)
			{	// construct from _Myiterator
			}

		_IReft operator*() const
			{	// change to const_reference
			return ((_IReft)**(_Myiterator *)this);
			}

		_ITptr operator->() const
			{	// change to const_pointer
			return ((_ITptr)&**(_Myiterator *)this);
			}
		};

 #else /* _HAS_IMMUTABLE_SETS */
	typedef typename _Mylist::iterator iterator;
 #endif /* _HAS_IMMUTABLE_SETS */

	typedef typename _Mylist::const_iterator const_iterator;
	typedef typename _Mylist::reverse_iterator
		reverse_iterator;
	typedef typename _Mylist::const_reverse_iterator
		const_reverse_iterator;
	typedef typename _Mylist::value_type value_type;

	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;

	explicit _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()
		{	// 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());
		}

	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() - 1);
		}

	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 (iterator _Plist = _Vec[_Bucket];
				_Plist != _Vec[_Bucket + 1]; ++_Plist)
				++_Ans;
		return (_Ans);
		}

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

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

	local_iterator end(size_type _Bucket)
		{	// return iterator for bucket following _Bucket
		if (_Bucket < bucket_count())
			return (_Vec[_Bucket + 1]);
		else
			return (end());
		}

	const_local_iterator end(size_type _Bucket) const
		{	// return iterator for bucket following _Bucket
		if (_Bucket < bucket_count())
			return (_Vec[_Bucket + 1]);
		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)
			_THROW(out_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() / 2;
		size_type _Newsize = min_buckets;

		for (; _Newsize < _Buckets && _Newsize < _Maxsize; )
			_Newsize *= 2;	// double until big enough
		if (_Newsize < _Buckets)
			_THROW(out_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();
		}
// ADDED WITH TR1 -- END

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

	iterator insert(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) one at a time
		_List.insert(begin(), _First, _Last);
		_Reinsert();
		}


	iterator erase(iterator _Where);

	iterator erase(iterator _First, iterator _Last);

	size_type erase(const key_type& _Keyval);

	void erase(const key_type *_First,
		const key_type *_Last);

	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;

	iterator lower_bound(const key_type& _Keyval);

	const_iterator lower_bound(const key_type& _Keyval) const;

	iterator upper_bound(const key_type& _Keyval);

	const_iterator upper_bound(const key_type& _Keyval) const;

	_Pairii equal_range(const key_type& _Keyval);

	_Paircc equal_range(const key_type& _Keyval) const;

	void swap(_Myt& _Right)
		{	// exchange contents with _Right
		if (this != &_Right)
			{	// different, worth swapping
			_Myt _Tmp = *this;

			*this = _Right;
			_Right = _Tmp;
			}
		}

 #if _HAS_TRADITIONAL_STL
//	size_type bucket_count() const
//	size_type max_bucket_count() const

	size_type elems_in_bucket(size_type _Bucket) const
		{	// return number of elements in bucket
		return (bucket_size(_Bucket));
		}
 #endif /* _HAS_TRADITIONAL_STL */

protected:
	void _Copy(const _Myt& _Right);

	void _Grow();

	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(_Buckets + 1, end());
		_Mask = _Buckets - 1;
		_Maxidx = _Buckets;
		}

	_Pairib _Insert(const value_type& _Val, iterator _Where);

	void _Reinsert()
		{	// insert elements at beginning of list into table
		iterator _First;
		for (; (_First = _List.begin()) != _Vec[0]; )
			_Insert(*_First, _First);
		}

	_Mylist _List;	// the list of elements, must initialize before _Vec
	_Myvec _Vec;	// the vector of list iterators
	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

 #if _HAS_HASH_STATISTICS
  #include <cmath>
  #include <cstdio>

template <class _Hashtab>
	void _Stats(_Hashtab _Tab)
	{	// display element distribution statistics
	unsigned long _Max = 0;
	unsigned long _Min = (size_t)(-1);
	unsigned long _Buckets = _Tab.bucket_count();
	double _Avg = _Buckets ? (double)_Tab.size() / _Buckets : 0;
	double _Var = 0;

	for (size_t _Idx = 0; _Idx != _Buckets; ++_Idx)
		{	// gather bucket data
		unsigned long _Elts = _Tab.elems_in_bucket(_Idx);
		_Var += ((double)_Elts - _Avg) * ((double)_Elts - _Avg);
		if (_Elts < _Min)
			_Min = _Elts;
		if (_Max < _Elts)
			_Max = _Elts;
		}
	_CSTD printf("elements: %ul, buckets: %ul\n", _Tab.size(), _Buckets);
	_CSTD printf("elements/bucket: %0.2f, variance: %0.2f\n",
		_Avg, _CSTD sqrt(_Var / (_Buckets + 1)));
	_CSTD printf("smallest: %u, largest: %u\n", _Min, _Max);
	}
 #endif /* _HAS_HASH_STATISTICS */

#endif /* _XHASH_ */

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