#pragma once

#include "iterator"
#include "xutility"
#include "memory"

#ifdef _MSC_VER
#pragma pack(push, 8)
#endif // _MSC_VER

_STD_BEGIN
		// TEMPLATE CLASS vector_vs6
template<class _Ty, class _A = allocator_<_Ty> >
	class vector_vs6 {
public:
	typedef vector_vs6<_Ty, _A> _Myt;
	typedef _A allocator_type;
	typedef typename _A::size_type size_type;
	typedef typename _A::difference_type difference_type;
	typedef typename _A::pointer _Tptr;
	typedef typename _A::const_pointer _Ctptr;
	typedef typename _A::reference reference;
	typedef typename _A::const_reference const_reference;
	typedef typename _A::value_type value_type;
	typedef _Tptr iterator;
	typedef _Ctptr const_iterator;
	typedef reverse_iterator_<const_iterator, value_type,
		const_reference, _Ctptr, difference_type>
			const_reverse_iterator;
	typedef reverse_iterator_<iterator, value_type,
		reference, _Tptr, difference_type>
			reverse_iterator;
	explicit vector_vs6(const _A& _Al = _A())
		: allocator(_Al), _First(0), _Last(0), _End(0) {}
	explicit vector_vs6(size_type _N, const _Ty& _V = _Ty(),
		const _A& _Al = _A())
		: allocator(_Al)
		{_First = allocator.allocate(_N, (void *)0);
		_Ufill(_First, _N, _V);
		_Last = _First + _N;
		_End = _Last; }
	vector_vs6(const _Myt& _X)
		: allocator(_X.allocator)
		{_First = allocator.allocate(_X.size(), (void *)0);
		_Last = _Ucopy(_X.begin(), _X.end(), _First);
		_End = _Last; }
	typedef const_iterator _It;
	vector_vs6(_It _F, _It _L, const _A& _Al = _A())
		: allocator(_Al), _First(0), _Last(0), _End(0)
		{insert(begin(), _F, _L); }
	~vector_vs6()
		{__Destroy(_First, _Last);
		allocator.deallocate(_First, _End - _First);
		_First = 0, _Last = 0, _End = 0; }
	_Myt& operator=(const _Myt& _X)
		{if (this == &_X)
			;
		else if (_X.size() <= size())
			{iterator_ _S = copy_(_X.begin(), _X.end(), _First);
			__Destroy(_S, _Last);
			_Last = _First + _X.size(); }
		else if (_X.size() <= capacity())
			{const_iterator _S = _X.begin() + size();
			copy_(_X.begin(), _S, _First);
			_Ucopy(_S, _X.end(), _Last);
			_Last = _First + _X.size(); }
		else
			{__Destroy(_First, _Last);
			allocator.deallocate(_First, _End - _First);
			_First = allocator.allocate(_X.size(), (void *)0);
			_Last = _Ucopy(_X.begin(), _X.end(),
				_First);
			_End = _Last; }
		return (*this); }
	void reserve(size_type _N)
		{if (capacity() < _N)
			{iterator_ _S = allocator.allocate(_N, (void *)0);
			_Ucopy(_First, _Last, _S);
			__Destroy(_First, _Last);
			allocator.deallocate(_First, _End - _First);
			_End = _S + _N;
			_Last = _S + size();
			_First = _S; }}
	size_type capacity() const
		{return (_First == 0 ? 0 : _End - _First); }
	iterator begin()
		{return (_First); }
	const_iterator begin() const
		{return ((const_iterator)_First); }
	iterator end()
		{return (_Last); }
	const_iterator end() const
		{return ((const_iterator)_Last); }
	reverse_iterator rbegin()
		{return (reverse_iterator(end())); }
	const_reverse_iterator rbegin() const
		{return (const_reverse_iterator(end())); }
	reverse_iterator rend()
		{return (reverse_iterator(begin())); }
	const_reverse_iterator rend() const
		{return (const_reverse_iterator(begin())); }
	void resize(size_type _N, const _Ty& _X = _Ty())
		{if (size() < _N)
			insert(end(), _N - size(), _X);
		else if (_N < size())
			erase(begin() + _N, end()); }
	size_type size() const
		{return (_First == 0 ? 0 : _Last - _First); }
	size_type max_size() const
		{return (allocator.max_size()); }
	bool empty() const
		{return (size() == 0); }
	_A get_allocator() const
		{return (allocator); }
	const_reference at(size_type _P) const
		{if (size() <= _P)
			_Xran();
		return (*(begin() + _P)); }
	reference at(size_type _P)
		{if (size() <= _P)
			_Xran();
		return (*(begin() + _P)); }
	const_reference operator[](size_type _P) const
		{return (*(begin() + _P)); }
	reference operator[](size_type _P)
		{return (*(begin() + _P)); }
	reference front()
		{return (*begin()); }
	const_reference front() const
		{return (*begin()); }
	reference back()
		{return (*(end() - 1)); }
	const_reference back() const
		{return (*(end() - 1)); }
	void push_back(const _Ty& _X)
		{insert(end(), _X); }
	void pop_back()
		{erase(end() - 1); }
	void assign(_It _F, _It _L)
		{erase(begin(), end());
		insert(begin(), _F, _L); }
	void assign(size_type _N, const _Ty& _X = _Ty())
		{erase(begin(), end());
		insert(begin(), _N, _X); }
	iterator insert(iterator _P, const _Ty& _X = _Ty())
		{size_type _O = size_type(_P - begin());
		insert(_P, 1, _X);
		return (begin() + _O); }
	void insert(iterator _P, size_type _M, const _Ty& _X)
		{if (size_type(_End - _Last) < _M)
			{size_type _N = size() + (_M < size() ? size() : _M);
			iterator _S = allocator.allocate(_N, (void *)0);
			iterator _Q = _Ucopy(_First, _P, _S);
			_Ufill(_Q, _M, _X);
			_Ucopy(_P, _Last, _Q + _M);
			__Destroy(_First, _Last);
			allocator.deallocate(_First, _End - _First);
			_End = _S + _N;
			_Last = _S + size() + _M;
			_First = _S; }
		else if (size_type(_Last - _P) < _M)
			{_Ucopy(_P, _Last, _P + _M);
			_Ufill(_Last, _M - (_Last - _P), _X);
			fill_(_P, _Last, _X);
			_Last += _M; }
		else if (0 < _M)
			{_Ucopy(_Last - _M, _Last, _Last);
			copy_backward_(_P, _Last - _M, _Last);
			fill_(_P, _P + _M, _X);
			_Last += _M; }}
	void insert(iterator _P, _It _F, _It _L)
		{size_type _M = 0;
		__Distance(_F, _L, _M);
		if (_End - _Last < _M)
			{size_type _N = size() + (_M < size() ? size() : _M);
			iterator _S = allocator.allocate(_N, (void *)0);
			iterator _Q = _Ucopy(_First, _P, _S);
			_Q = _Ucopy(_F, _L, _Q);
			_Ucopy(_P, _Last, _Q);
			__Destroy(_First, _Last);
			allocator.deallocate(_First, _End - _First);
			_End = _S + _N;
			_Last = _S + size() + _M;
			_First = _S; }
		else if (_Last - _P < _M)
			{_Ucopy(_P, _Last, _P + _M);
			_Ucopy(_F + (_Last - _P), _L, _Last);
			copy_(_F, _F + (_Last - _P), _P);
			_Last += _M; }
		else if (0 < _M)
			{_Ucopy(_Last - _M, _Last, _Last);
			copy_backward_(_P, _Last - _M, _Last);
			copy_(_F, _L, _P);
			_Last += _M; }}
	iterator erase(iterator _P)
		{copy_(_P + 1, end(), _P);
		__Destroy(_Last - 1, _Last);
		--_Last;
		return (_P); }
	iterator erase(iterator _F, iterator _L)
		{iterator _S = copy_(_L, end(), _F);
		__Destroy(_S, end());
		_Last = _S;
		return (_F); }
	void clear()
		{erase(begin(), end()); }
	bool _Eq(const _Myt& _X) const
		{return (size() == _X.size()
			&& equal_(begin(), end(), _X.begin())); }
	bool _Lt(const _Myt& _X) const
		{return (lexicographical_compare_(begin(), end(),
			_X.begin(), _X.end())); }
	void swap(_Myt& _X)
		{if (allocator == _X.allocator)
			{std::swap_(_First, _X._First);
			std::swap_(_Last, _X._Last);
			std::swap_(_End, _X._End); }
		else
			{_Myt _Ts = *this; *this = _X, _X = _Ts; }}
	friend void swap(_Myt& _X, _Myt& _Y)
		{_X.swap_(_Y); }
protected:
	void __Destroy(iterator _F, iterator _L)
		{for (; _F != _L; ++_F)
			allocator.destroy(_F); }
	iterator _Ucopy(const_iterator _F, const_iterator _L,
		iterator _P)
		{for (; _F != _L; ++_P, ++_F)
			allocator.construct(_P, *_F);
		return (_P); }
	void _Ufill(iterator _F, size_type _N, const _Ty &_X)
		{for (; 0 < _N; --_N, ++_F)
			allocator.construct(_F, _X); }
	void _Xran() const
		{_THROW(out_of_range, "invalid vector_vs6<T> subscript"); }
	_A allocator;
	iterator _First, _Last, _End;
	};
		// CLASS vector_vs6<_Bool, allocator>
typedef unsigned int _Vbase;	// word type for vector<bool> representation
const int __VBITS = CHAR_BIT * sizeof (_Vbase);	// at least CHAR_BITS bits per word

typedef allocator_<_Vbase> _Bool_allocator;
template<class _Bool_allocator>
class vector_vs6<_Bool, _Bool_allocator> {
public:
	typedef _Bool_allocator _A;
	typedef _Bool _Ty;
	typedef vector_vs6<_Ty, _A> _Myt;
	typedef vector_vs6<_Vbase, _A> _Vbtype;
	typedef _A allocator_type;
	typedef typename _A::size_type size_type;
	typedef typename _A::difference_type difference_type;

		// CLASS reference
	class reference {
	public:
		reference()
			: _Mask(0), _Ptr(0) {}
		reference(size_t _O, _Vbase *_P)
			: _Mask((_Vbase)1 << _O), _Ptr(_P) {}
		reference& operator=(const reference& _X)
			{return (*this = bool(_X)); }
		reference& operator=(bool _V)
			{if (_V)
				*_Ptr |= _Mask;
			else
				*_Ptr &= ~_Mask;
			return (*this); }
		void flip()
			{*_Ptr ^= _Mask; }
		bool operator~() const
			{return (!bool(*this)); }
		operator bool() const
			{return ((*_Ptr & _Mask) != 0); }
	protected:
		_Vbase _Mask, *_Ptr;
		};

	typedef const reference const_reference;
	typedef bool value_type;
		// CLASS const_iterator
	class iterator_;
	class const_iterator : public _Ranit<_Bool, difference_type> {
	public:
		const_iterator()
			: _Off(0), _Ptr(0) {}
		const_iterator(size_t _O, const _Vbase *_P)
			: _Off(_O), _Ptr((_Vbase*)_P) {}
		const_iterator(const iterator_& _X)
			: _Off(_X._Off), _Ptr(_X._Ptr) {}
		const_reference operator*() const
			{return (const_reference(_Off, _Ptr)); }
		const_iterator& operator++()
			{_Inc();
			return (*this); }
		const_iterator operator++(int)
			{const_iterator _Tmp = *this;
			_Inc();
			return (_Tmp); }
		const_iterator& operator--()
			{_Dec();
			return (*this); }
		const_iterator operator--(int)
			{const_iterator _Tmp = *this;
			_Dec();
			return (_Tmp); }
		const_iterator& operator+=(difference_type _N)
			{_Off += _N;
			_Ptr += _Off / __VBITS;
			_Off %= __VBITS;
			return (*this); }
		const_iterator& operator-=(difference_type _N)
			{return (*this += -_N); }
		const_iterator operator+(difference_type _N) const
			{const_iterator _Tmp = *this;
			return (_Tmp += _N); }
		const_iterator operator-(difference_type _N) const
			{const_iterator _Tmp = *this;
			return (_Tmp -= _N); }
		difference_type operator-(const const_iterator _X) const
			{return (__VBITS * (_Ptr - _X._Ptr)
				+ (difference_type)_Off
				- (difference_type)_X._Off); }
		const_reference operator[](difference_type _N) const
			{return (*(*this + _N)); }
		bool operator==(const const_iterator& _X) const
			{return (_Ptr == _X._Ptr && _Off == _X._Off); }
		bool operator!=(const const_iterator& _X) const
			{return (!(*this == _X)); }
		bool operator<(const const_iterator& _X) const
			{return (_Ptr < _X._Ptr
				|| _Ptr == _X._Ptr && _Off < _X._Off); }
		bool operator>(const const_iterator& _X) const
			{return (_X < *this); }
		bool operator<=(const const_iterator& _X) const
			{return (!(_X < *this)); }
		bool operator>=(const const_iterator& _X) const
			{return (!(*this < _X)); }
	protected:
		void _Dec()
			{if (_Off != 0)
				--_Off;
			else
				_Off = __VBITS - 1, --_Ptr; }
		void _Inc()
			{if (_Off < __VBITS - 1)
				++_Off;
			else
				_Off = 0, ++_Ptr; }
		size_t _Off;
		_Vbase *_Ptr;
		};
		// CLASS iterator_
	class iterator_ : public const_iterator {
	public:
		iterator_()
			: const_iterator() {}
		iterator_(size_t _O, _Vbase *_P)
			: const_iterator(_O, _P) {}
		reference operator*() const
			{return (reference(_Off, _Ptr)); }
		iterator_& operator++()
			{_Inc();
			return (*this); }
		iterator_ operator++(int)
			{iterator_ _Tmp = *this;
			_Inc();
			return (_Tmp); }
		iterator_& operator--()
			{_Dec();
			return (*this); }
		iterator_ operator--(int)
			{iterator_ _Tmp = *this;
			_Dec();
			return (_Tmp); }
		iterator_& operator+=(difference_type _N)
			{_Off += _N;
			_Ptr += _Off / __VBITS;
			_Off %= __VBITS;
			return (*this); }
		iterator_& operator-=(difference_type _N)
			{return (*this += -_N); }
		iterator_ operator+(difference_type _N) const
			{iterator_ _Tmp = *this;
			return (_Tmp += _N); }
		iterator_ operator-(difference_type _N) const
			{iterator_ _Tmp = *this;
			return (_Tmp -= _N); }
		difference_type operator-(const iterator_ _X) const
			{return (__VBITS * (_Ptr - _X._Ptr)
				+ (difference_type)_Off
				- (difference_type)_X._Off); }
		reference operator[](difference_type _N) const
			{return (*(*this + _N)); }
		bool operator==(const iterator_& _X) const
			{return (_Ptr == _X._Ptr && _Off == _X._Off); }
		bool operator!=(const iterator_& _X) const
			{return (!(*this == _X)); }
		bool operator<(const iterator_& _X) const
			{return (_Ptr < _X._Ptr
				|| _Ptr == _X._Ptr && _Off < _X._Off); }
		bool operator>(const iterator_& _X) const
			{return (_X < *this); }
		bool operator<=(const iterator_& _X) const
			{return (!(_X < *this)); }
		bool operator>=(const iterator_& _X) const
			{return (!(*this < _X)); }
		};
	typedef reverse_iterator_<const_iterator, value_type,
		const_reference, const_reference *, difference_type>
			const_reverse_iterator;
	typedef reverse_iterator_<iterator_, value_type,
		reference, reference *, difference_type>
			reverse_iterator;
	explicit vector_vs6(const _A& _Al = _A())
		: _Size(0), _Vec(_Al) {}
	explicit vector_vs6(size_type _N, const bool _V = false,
		const _A& _Al = _A())
		: _Vec(_Nw(_N), _V ? -1 : 0, _Al) {_Trim(_N); }
	typedef const_iterator _It;
	vector_vs6(_It _F, _It _L, const _A& _Al = _A())
		: _Size(0), _Vec(_Al)
		{insert(begin(), _F, _L); }
	~vector_vs6()
		{_Size = 0; }
	void reserve(size_type _N)
		{_Vec.reserve(_Nw(_N)); }
	size_type capacity() const
		{return (_Vec.capacity() * __VBITS); }
	iterator_ begin()
		{return (iterator_(0, _Vec.begin())); }
	const_iterator begin() const
		{return (const_iterator(0, _Vec.begin())); }
	iterator_ end()
		{iterator_ _Tmp = begin();
		if (0 < _Size)
			_Tmp += _Size;
		return (_Tmp); }
	const_iterator end() const
		{const_iterator _Tmp = begin();
		if (0 < _Size)
			_Tmp += _Size;
		return (_Tmp); }
	reverse_iterator rbegin()
		{return (reverse_iterator(end())); }
	const_reverse_iterator rbegin() const
		{return (const_reverse_iterator(end())); }
	reverse_iterator rend()
		{return (reverse_iterator(begin())); }
	const_reverse_iterator rend() const
		{return (const_reverse_iterator(begin())); }
	void resize(size_type _N, bool _X = false)
		{if (size() < _N)
			insert(end(), _N - size(), _X);
		else if (_N < size())
			erase(begin() + _N, end()); }
	size_type size() const
		{return (_Size); }
	size_type max_size() const
		{return (_Vec.max_size() * __VBITS); }
	bool empty() const
		{return (size() == 0); }
	_A get_allocator() const
		{return (_Vec.get_allocator()); }
	const_reference at(size_type _P) const
		{if (size() <= _P)
			_Xran();
		return (*(begin() + _P)); }
	reference at(size_type _P)
		{if (size() <= _P)
			_Xran();
		return (*(begin() + _P)); }
	const_reference operator[](size_type _P) const
		{return (*(begin() + _P)); }
	reference operator[](size_type _P)
		{return (*(begin() + _P)); }
	reference front()
		{return (*begin()); }
	const_reference front() const
		{return (*begin()); }
	reference back()
		{return (*(end() - 1)); }
	const_reference back() const
		{return (*(end() - 1)); }
	void push_back(const bool _X)
		{insert(end(), _X); }
	void pop_back()
		{erase(end() - 1); }
	void assign(_It _F, _It _L)
		{erase(begin(), end());
		insert(begin(), _F, _L); }
	void assign(size_type _N, const bool _X = false)
		{erase(begin(), end());
		insert(begin(), _N, _X); }
	iterator_ insert(iterator_ _P, const bool _X = false)
		{size_type _O = _P - begin();
		insert(_P, 1, _X);
		return (begin() + _O); }
	void insert(iterator_ _P, size_type _M, const bool _X)
		{if (0 < _M)
			{if (capacity() - size() < _M)
				{size_type _O = _P - begin();
				_Vec.resize(_Nw(size() + _M), 0);
				_P = begin() + _O; }
			copy_backward_(_P, end(), end() + _M);
			fill_(_P, _P + _M, _X);
			_Size += _M; }}
	void insert(iterator_ _P, _It _F, _It _L)
		{size_type _M = 0;
		__Distance(_F, _L, _M);
		if (0 < _M)
			{if (capacity() - size() < _M)
				{size_type _O = _P - begin();
				_Vec.resize(_Nw(size() + _M), 0);
				_P = begin() + _O; }
			copy_backward_(_P, end(), end() + _M);
			copy_(_F, _L, _P);
			_Size += _M; }}
	iterator_ erase(iterator_ _P)
		{copy_(_P + 1, end(), _P);
		_Trim(_Size - 1);
		return (_P); }
	iterator_ erase(iterator_ _F, iterator_ _L)
		{iterator_ _S = copy_(_L, end(), _F);
		_Trim(_S - begin());
		return (_F); }
	void clear()
		{erase(begin(), end()); }
	void flip()
		{for (_Vbtype::iterator_ _S = _Vec.begin();
			_S != _Vec.end(); ++_S)
			*_S = ~*_S;
		_Trim(_Size); }
	bool _Eq(const _Myt& _X) const
		{return (_Size == _X._Size && _Vec._Eq(_X._Vec)); }
	bool _Lt(const _Myt& _X) const
		{return (_Size < _X._Size
			|| _Size == _X._Size && _Vec._Lt(_X._Vec)); }
	void swap(_Myt& _X)
		{std::swap_(_Size, _X._Size);
		_Vec.swap_(_X._Vec); }
	friend void swap(_Myt& _X, _Myt& _Y)
		{_X.swap_(_Y); }
	static void swap(reference _X, reference _Y)
		{bool _V = _X;
		_X = _Y;
		_Y = _V; }
protected:
	static size_type _Nw(size_type _N)
		{return ((_N + __VBITS - 1) / __VBITS); }
	void _Trim(size_type _N)
		{size_type _M = _Nw(_N);
		if (_M < _Vec.size())
			_Vec.erase(_Vec.begin() + _M, _Vec.end());
		_Size = _N;
		_N %= __VBITS;
		if (0 < _N)
			_Vec[_M - 1] &= ((_Vbase)1 << _N) - 1; }
	void _Xran() const
		{_THROW(out_of_range, "invalid vector_vs6<bool> subscript"); }
	size_type _Size;
	_Vbtype _Vec;
	};
typedef vector_vs6<_Bool, _Bool_allocator> _Bvector;
		// vector_vs6 TEMPLATE OPERATORS
template<class _Ty, class _A> inline
	bool operator==(const vector_vs6<_Ty, _A>& _X,
		const vector_vs6<_Ty, _A>& _Y)
	{return (_X._Eq(_Y)); }
template<class _Ty, class _A> inline
	bool operator!=(const vector_vs6<_Ty, _A>& _X,
		const vector_vs6<_Ty, _A>& _Y)
	{return (!(_X == _Y)); }
template<class _Ty, class _A> inline
	bool operator<(const vector_vs6<_Ty, _A>& _X,
		const vector_vs6<_Ty, _A>& _Y)
	{return (_X._Lt(_Y)); }
template<class _Ty, class _A> inline
	bool operator>(const vector_vs6<_Ty, _A>& _X,
		const vector_vs6<_Ty, _A>& _Y)
	{return (_Y < _X); }
template<class _Ty, class _A> inline
	bool operator<=(const vector_vs6<_Ty, _A>& _X,
		const vector_vs6<_Ty, _A>& _Y)
	{return (!(_Y < _X)); }
template<class _Ty, class _A> inline
	bool operator>=(const vector_vs6<_Ty, _A>& _X,
		const vector_vs6<_Ty, _A>& _Y)
	{return (!(_X < _Y)); }
_STD_END

#ifdef _MSC_VER
#pragma pack(pop)
#endif // _MSC_VER