// utility standard header
#pragma once
#ifndef _UTILITY_
#define _UTILITY_
#ifndef RC_INVOKED
#include <xstddef>
#include <iosfwd>
#include <type_traits>

 #pragma pack(push,_CRT_PACKING)
 #pragma warning(push,3)
 #pragma push_macro("new")
 #undef new

 #pragma warning(disable: 4180 4512)

_STD_BEGIN
		// TEMPLATE FUNCTION iter_swap (from <xutility>)
template<class _Ty> inline
	void swap(_Ty&, _Ty&);

template<class _FwdIt1,
	class _FwdIt2> inline
	void iter_swap(_FwdIt1 _Left, _FwdIt2 _Right)
	{	// swap *_Left and *_Right
	swap(*_Left, *_Right);
	}

		// TEMPLATE FUNCTION swap
template<class _Ty,
	size_t _Size> inline
	void swap(_Ty (&_Left)[_Size], _Ty (&_Right)[_Size])
	{	// exchange arrays stored at _Left and _Right
	if (&_Left != &_Right)
		{	// worth swapping, swap ranges
		_Ty *_First1 = _Left;
		_Ty *_Last1 = _First1 + _Size;
		_Ty *_First2 = _Right;
		for (; _First1 != _Last1; ++_First1, ++_First2)
			_STD iter_swap(_First1, _First2);
		}
	}

template<class _Ty> inline
	void swap(_Ty& _Left, _Ty& _Right)
	{	// exchange values stored at _Left and _Right
	_Ty _Tmp = _Move(_Left);
	_Left = _Move(_Right);
	_Right = _Move(_Tmp);
	}

		// TEMPLATE FUNCTION _Swap_adl
template<class _Ty> inline
	void _Swap_adl(_Ty& _Left, _Ty& _Right)
	{	// exchange values stored at _Left and _Right, using ADL
	swap(_Left, _Right);
	}

		// STRUCT piecewise_construct_t
struct piecewise_construct_t
	{	// tag type for pair tuple arguments
	};

const piecewise_construct_t piecewise_construct =
	piecewise_construct_t();

		// TEMPLATE STRUCT pair
template<class _Ty1,
	class _Ty2>
	struct pair;

template<class = _Nil, _MAX_CLASS_LIST>
	class tuple;

template<class _Ty1,
	class _Ty2>
	struct pair
	{	// store a pair of values
	typedef pair<_Ty1, _Ty2> _Myt;
	typedef _Ty1 first_type;
	typedef _Ty2 second_type;

	pair()
		: first(), second()
		{	// default construct
		}

	pair(const _Ty1& _Val1, const _Ty2& _Val2)
		: first(_Val1), second(_Val2)
		{	// construct from specified values
		}

	template<class _Other1,
		class _Other2>
		pair(const pair<_Other1, _Other2>& _Right,
			typename enable_if<is_convertible<const _Other1&, _Ty1>::value
				&& is_convertible<const _Other2&, _Ty2>::value,
				void>::type ** = 0)
		: first(_Right.first), second(_Right.second)
		{	// construct from compatible pair
		}

	void swap(_Myt& _Right)
		{	// exchange contents with _Right
		if (this != &_Right)
			{	// different, worth swapping
			_Swap_adl(first, _Right.first);
			_Swap_adl(second, _Right.second);
			}
		}

	_Myt& operator=(const _Myt& _Right)
		{	// assign from copied pair
		first = _Right.first;
		second = _Right.second;
		return (*this);
		}

 #if _HAS_CPP0X
	template<class _Other1,
		class _Other2>
		_Myt& operator=(const pair<_Other1, _Other2>& _Right)
		{	// assign from compatible pair
		first = _Right.first;
		second = _Right.second;
		return (*this);
		}
 #endif /* _HAS_CPP0X */

	template<class _Other1,
		class _Other2>
		pair(_Other1&& _Val1, _Other2&& _Val2,
			typename enable_if<is_convertible<_Other1, _Ty1>::value
				&& is_convertible<_Other2, _Ty2>::value,
				void>::type ** = 0)
		: first(_STD forward<_Other1>(_Val1)),
				second(_STD forward<_Other2>(_Val2))
		{	// construct from moved values
		}

#define _PAIR_TUPLE_CONSTRUCTOR_DECL0( \
	TEMPLATE_LIST1, PADDING_LIST1, LIST1, COMMA1, \
	TEMPLATE_LIST2, PADDING_LIST2, LIST2, COMMA2) \
	TEMPLATE_LIST1(_CLASS_TYPE) \
		pair(piecewise_construct_t, \
			tuple<LIST1(_TYPE)>, \
			tuple<>);

_VARIADIC_EXPAND_0X_0(_PAIR_TUPLE_CONSTRUCTOR_DECL0)
#undef _PAIR_TUPLE_CONSTRUCTOR_DECL0

#define _PAIR_TUPLE_CONSTRUCTOR_DECL1( \
	TEMPLATE_LIST1, PADDING_LIST1, LIST1, COMMA1, \
	TEMPLATE_LIST2, PADDING_LIST2, LIST2, COMMA2) \
	template<LIST2(_CLASS_TYPEX)> \
		pair(piecewise_construct_t, \
			tuple<>, \
			tuple<LIST2(_TYPEX)>);

_VARIADIC_EXPAND_0_1X(_PAIR_TUPLE_CONSTRUCTOR_DECL1)
#undef _PAIR_TUPLE_CONSTRUCTOR_DECL1

#define _PAIR_TUPLE_CONSTRUCTOR_DECL2( \
	TEMPLATE_LIST1, PADDING_LIST1, LIST1, COMMA1, \
	TEMPLATE_LIST2, PADDING_LIST2, LIST2, COMMA2) \
	template<LIST1(_CLASS_TYPE), LIST2(_CLASS_TYPEX)> \
		pair(piecewise_construct_t, \
			tuple<LIST1(_TYPE)>, \
			tuple<LIST2(_TYPEX)>);

_VARIADIC_EXPAND_1X_1X(_PAIR_TUPLE_CONSTRUCTOR_DECL2)
#undef _PAIR_TUPLE_CONSTRUCTOR_DECL2

	template<class _Other1,
		class _Other2>
		pair(pair<_Other1, _Other2>&& _Right,
			typename enable_if<is_convertible<_Other1, _Ty1>::value
				&& is_convertible<_Other2, _Ty2>::value,
				void>::type ** = 0)
		: first(_STD forward<_Other1>(_Right.first)),
			second(_STD forward<_Other2>(_Right.second))
		{	// construct from moved compatible pair
		}

	template<class _Other1,
		class _Other2>
		_Myt& operator=(pair<_Other1, _Other2>&& _Right)
		{	// assign from moved compatible pair
		first = _STD forward<_Other1>(_Right.first);
		second = _STD forward<_Other2>(_Right.second);
		return (*this);
		}

	_Myt& operator=(_Myt&& _Right)
		{	// assign from moved pair
		first = _STD forward<_Ty1>(_Right.first);
		second = _STD forward<_Ty2>(_Right.second);
		return (*this);
		}

	_Ty1 first;	// the first stored value
	_Ty2 second;	// the second stored value
	};

		// pair TEMPLATE FUNCTIONS

template<class _Ty1,
	class _Ty2> inline
	void swap(pair<_Ty1, _Ty2>& _Left, pair<_Ty1, _Ty2>& _Right)
	{	// swap _Left and _Right pairs
	_Left.swap(_Right);
	}

template<class _Ty1,
	class _Ty2> inline
	bool operator==(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test for pair equality
	return (_Left.first == _Right.first && _Left.second == _Right.second);
	}

template<class _Ty1,
	class _Ty2> inline
	bool operator!=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test for pair inequality
	return (!(_Left == _Right));
	}

template<class _Ty1,
	class _Ty2> inline
	bool operator<(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left < _Right for pairs
	return (_Left.first < _Right.first ||
		!(_Right.first < _Left.first) && _Left.second < _Right.second);
	}

template<class _Ty1,
	class _Ty2> inline
	bool operator>(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left > _Right for pairs
	return (_Right < _Left);
	}

template<class _Ty1,
	class _Ty2> inline
	bool operator<=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left <= _Right for pairs
	return (!(_Right < _Left));
	}

template<class _Ty1,
	class _Ty2> inline
	bool operator>=(const pair<_Ty1, _Ty2>& _Left,
		const pair<_Ty1, _Ty2>& _Right)
	{	// test if _Left >= _Right for pairs
	return (!(_Left < _Right));
	}

	// TEMPLATE FUNCTION make_pair

template<class _Ty1,
	class _Ty2> inline
	pair<typename _Unrefwrap<_Ty1>::type,
		typename _Unrefwrap<_Ty2>::type>
		make_pair(_Ty1&& _Val1, _Ty2&& _Val2)
	{	// return pair composed from arguments
	typedef pair<typename _Unrefwrap<_Ty1>::type,
		typename _Unrefwrap<_Ty2>::type> _Mypair;
	return (_Mypair(_STD forward<_Ty1>(_Val1),
		_STD forward<_Ty2>(_Val2)));
	}

		// TEMPLATE OPERATORS
	namespace rel_ops
		{	// nested namespace to hide relational operators from std
template<class _Ty> inline
	bool operator!=(const _Ty& _Left, const _Ty& _Right)
	{	// test for inequality, in terms of equality
	return (!(_Left == _Right));
	}

template<class _Ty> inline
	bool operator>(const _Ty& _Left, const _Ty& _Right)
	{	// test if _Left > _Right, in terms of operator<
	return (_Right < _Left);
	}

template<class _Ty> inline
	bool operator<=(const _Ty& _Left, const _Ty& _Right)
	{	// test if _Left <= _Right, in terms of operator<
	return (!(_Right < _Left));
	}

template<class _Ty> inline
	bool operator>=(const _Ty& _Left, const _Ty& _Right)
	{	// test if _Left >= _Right, in terms of operator<
	return (!(_Left < _Right));
	}
		}
_STD_END

_STD_BEGIN
	// TUPLE INTERFACE TO pair
template<class _Tuple>
	struct tuple_size;
template<size_t _Idx,
	class _Tuple>
	struct tuple_element;
template<class _Ty1,
	class _Ty2>
	struct tuple_size<pair<_Ty1, _Ty2> >
	{	// struct to determine number of elements in pair
	static const int value = 2;
	};

template<int _Idx,
	class _Ty>
	struct _Pair_data;
template<class _Ty1,
	class _Ty2>
	struct _Pair_data<0, pair<_Ty1, _Ty2> >
	{	// struct to pick out argument type and value at index 0
	typedef typename add_lvalue_reference<const _Ty1>::type _Ctype;
	typedef typename add_lvalue_reference<_Ty1>::type _Rtype;
	typedef typename add_rvalue_reference<_Ty1>::type _RRtype;

	static _Rtype _Val(pair<_Ty1, _Ty2>& _Pr)
		{	// return value
		return (_Pr.first);
		}

	static _Ctype _Val(const pair<_Ty1, _Ty2>& _Pr)
		{	// return value
		return (_Pr.first);
		}

	static _RRtype _Val(pair<_Ty1, _Ty2>&& _Pr)
		{	// return value
		return (_STD forward<_Ty1>(_Pr.first));
		}
	};

template<class _Ty1,
	class _Ty2>
	struct _Pair_data<1, pair<_Ty1, _Ty2> >
	{	// struct to pick out argument type and value at index 1
	typedef typename add_lvalue_reference<const _Ty2>::type _Ctype;
	typedef typename add_lvalue_reference<_Ty2>::type _Rtype;
	typedef typename add_rvalue_reference<_Ty2>::type _RRtype;

	static _Rtype _Val(pair<_Ty1, _Ty2>& _Pr)
		{	// return value
		return (_Pr.second);
		}

	static _Ctype _Val(const pair<_Ty1, _Ty2>& _Pr)
		{	// return value
		return (_Pr.second);
		}

	static _RRtype _Val(pair<_Ty1, _Ty2>&& _Pr)
		{	// return value
		return (_STD forward<_Ty2>(_Pr.second));
		}
	};

template<class _Ty1,
	class _Ty2>
	struct tuple_element<0, pair<_Ty1, _Ty2> >
	{	// struct to determine type of element 0 in pair
	typedef _Ty1 type;
	};

template<class _Ty1,
	class _Ty2>
	struct tuple_element<1, pair<_Ty1, _Ty2> >
	{	// struct to determine type of element 1 in pair
	typedef _Ty2 type;
	};

template<int _Idx,
	class _Ty1,
	class _Ty2> inline
	typename _Pair_data<_Idx, pair<_Ty1, _Ty2> >::_Rtype
		get(pair<_Ty1, _Ty2>& _Pr) _NOEXCEPT
	{	// get reference to element at _Idx in pair _Pr
	return (_Pair_data<_Idx, pair<_Ty1, _Ty2> >::_Val(_Pr));
	}

template<int _Idx,
	class _Ty1,
	class _Ty2> inline
	typename _Pair_data<_Idx, pair<_Ty1, _Ty2> >::_Ctype
		get(const pair<_Ty1, _Ty2>& _Pr) _NOEXCEPT
	{	// get const reference to element at _Idx in pair _Pr
	return (_Pair_data<_Idx, pair<_Ty1, _Ty2> >::_Val(_Pr));
	}

template<int _Idx,
	class _Ty1,
	class _Ty2> inline
	typename _Pair_data<_Idx, pair<_Ty1, _Ty2> >::_RRtype
		get(pair<_Ty1, _Ty2>&& _Pr) _NOEXCEPT
	{	// get rvalue reference to element at _Idx in pair _Pr
	typedef typename _Pair_data<_Idx, pair<_Ty1, _Ty2> >::_RRtype
		_RRtype;
	return (_STD forward<_RRtype>(
		_Pair_data<_Idx, pair<_Ty1, _Ty2> >::_Val(_Pr)));
	}
_STD_END

_STD_BEGIN
namespace tr1 {	// TR1 additions
using _STD get;
using _STD tuple_element;
using _STD tuple_size;
}	// namespace tr1
_STD_END

 #pragma pop_macro("new")
 #pragma warning(pop)
 #pragma pack(pop)
#endif /* RC_INVOKED */
#endif /* _UTILITY_ */

/*
 * This file is derived from software bearing the following
 * restrictions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this
 * software and its documentation for any purpose is hereby
 * granted without fee, provided that the above copyright notice
 * appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation.
 * Hewlett-Packard Company makes no representations about the
 * suitability of this software for any purpose. It is provided
 * "as is" without express or implied warranty.
 */

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