deque

来自「pwlib源码库」· 代码 · 共 634 行 · 第 1/2 页

TXT
634
字号
	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_front(const _Ty& _X)		{if (_Offset % _DEQUESIZ == 0			&& _Mapsize <= (_Size + _DEQUESIZ) / _DEQUESIZ)			_Growmap(1);		size_type _Newoff = _Offset != 0 ? _Offset			: _Mapsize * _DEQUESIZ;		size_type _Block = --_Newoff / _DEQUESIZ;		if (_Map[_Block] == 0)			_Map[_Block] = _Alval.allocate(_DEQUESIZ, (void *)0);		_TRY_BEGIN		_Offset = _Newoff;		++_Size;		_Alval.construct(_Map[_Block] + _Newoff % _DEQUESIZ, _X);		_CATCH_ALL		pop_front();		_RERAISE;		_CATCH_END }	void pop_front()		{if (!empty())			{size_type _Block = _Offset / _DEQUESIZ;			_Alval.destroy(_Map[_Block] + _Offset % _DEQUESIZ);			if (_Mapsize * _DEQUESIZ <= ++_Offset)				_Offset = 0;			if (--_Size == 0)				_Offset = 0; }}	void push_back(const _Ty& _X)		{if ((_Offset + _Size) % _DEQUESIZ == 0			&& _Mapsize <= (_Size + _DEQUESIZ) / _DEQUESIZ)			_Growmap(1);		size_type _Newoff = _Offset + _Size;		size_type _Block = _Newoff / _DEQUESIZ;		if (_Mapsize <= _Block)			_Block -= _Mapsize;		if (_Map[_Block] == 0)			_Map[_Block] = _Alval.allocate(_DEQUESIZ, (void *)0);		_TRY_BEGIN		++_Size;		_Alval.construct(_Map[_Block] + _Newoff % _DEQUESIZ, _X);		_CATCH_ALL		pop_back();		_RERAISE;		_CATCH_END }	void pop_back()		{if (!empty())			{size_type _Newoff = _Size + _Offset - 1;			size_type _Block = _Newoff / _DEQUESIZ;			if (_Mapsize <= _Block)				_Block -= _Mapsize;			_Alval.destroy(_Map[_Block] + _Newoff % _DEQUESIZ);			if (--_Size == 0)				_Offset = 0; }}	template<class _It>		void assign(_It _F, _It _L)		{_Assign(_F, _L, _Iter_cat(_F)); }	template<class _It>		void _Assign(_It _F, _It _L, input_iterator_tag)		{erase(begin(), end());		insert(begin(), _F, _L); }	void assign(size_type _N)		{_Ty _Tx;		erase(begin(), end());		insert(begin(), _N, _Tx); }	void assign(size_type _N, const _Ty& _X)		{_Ty _Tx = _X;		erase(begin(), end());		insert(begin(), _N, _Tx); }	iterator insert(iterator _P, const _Ty& _X)		{if (_P == begin())			{push_front(_X);			return (begin()); }		else if (_P == end())			{push_back(_X);			return (end() - 1); }		else			{iterator _S;			size_type _Off = _P - begin();			_Ty _Tx = _X;			if (_Off < size() / 2)				{push_front(front());				_S = begin() + _Off;				copy(begin() + 2, _S + 1, begin() + 1); }			else				{push_back(back());				_S = begin() + _Off;				copy_backward(_S, end() - 2, end() - 1); }			*_S = _Tx;			return (_S); }}	void insert(iterator _P, size_type _M, const _Ty& _X)		{iterator _S;		size_type _I;		size_type _Off = _P - begin();		size_type _Rem = _Size - _Off;		if (_Off < _Rem)			if (_Off < _M)				{for (_I = _M - _Off; 0 < _I; --_I)					push_front(_X);				for (_I = _Off; 0 < _I; --_I)					push_front(begin()[_M - 1]);				_S = begin() + _M;				fill(_S, _S + _Off, _X); }			else				{for (_I = _M; 0 < _I; --_I)					push_front(begin()[_M - 1]);				_S = begin() + _M;				_Ty _Tx = _X;				copy(_S + _M, _S + _Off, _S);				fill(begin() + _Off, _S + _Off, _Tx); }		else			if (_Rem < _M)				{for (_I = _M - _Rem; 0 < _I; --_I)					push_back(_X);				for (_I = 0; _I < _Rem; ++_I)					push_back(begin()[_Off + _I]);				_S = begin() + _Off;				fill(_S, _S + _Rem, _X); }			else				{for (_I = 0; _I < _M; ++_I)					push_back(begin()[_Off + _Rem - _M + _I]);				_S = begin() + _Off;				_Ty _Tx = _X;				copy_backward(_S, _S + _Rem - _M, _S + _Rem);				fill(_S, _S + _M, _Tx); }}	template<class _It>		void insert(iterator _P, _It _F, _It _L)		{_Insert(_P, _F, _L, _Iter_cat(_F)); }	template<class _It>		void _Insert(iterator _P, _It _F, _It _L,			input_iterator_tag)		{size_type _Off = _P - begin();		for (; _F != _L; ++_F, ++_Off)			insert(begin() + _Off, *_F); }	template<class _It>		void _Insert(iterator _P, _It _F, _It _L,			bidirectional_iterator_tag)		{size_type _M = 0;		_Distance(_F, _L, _M);		size_type _I;		size_type _Off = _P - begin();		size_type _Rem = _Size - _Off;		if (_Off < _Rem)			if (_Off < _M)				{_It _Qx = _F;				advance(_Qx, _M - _Off);				for (_It _Q = _Qx; _F != _Q; )					push_front(*--_Q);				for (_I = _Off; 0 < _I; --_I)					push_front(begin()[_M - 1]);				copy(_Qx, _L, begin() + _M); }			else				{for (_I = _M; 0 < _I; --_I)					push_front(begin()[_M - 1]);				iterator _S = begin() + _M;				copy(_S + _M, _S + _Off, _S);				copy(_F, _L, begin() + _Off); }		else			if (_Rem < _M)				{_It _Qx = _F;				advance(_Qx, _Rem);				for (_It _Q = _Qx; _Q != _L; ++_Q)					push_back(*_Q);				for (_I = 0; _I < _Rem; ++_I)					push_back(begin()[_Off + _I]);				copy(_F, _Qx, begin() + _Off); }			else				{for (_I = 0; _I < _M; ++_I)					push_back(begin()[_Off + _Rem - _M + _I]);				iterator _S = begin() + _Off;				copy_backward(_S, _S + _Rem - _M, _S + _Rem);				copy(_F, _L, _S); }}	iterator erase(iterator _P)		{return (erase(_P, _P + 1)); }	iterator erase(iterator _F, iterator _L)		{size_type _N = _L - _F;		size_type _M = _F - begin();		if (_M < (size_type)(end() - _L))			{copy_backward(begin(), _F, _L);			for (; 0 < _N; --_N)				pop_front(); }		else			{copy(_L, end(), _F);			for (; 0 < _N; --_N)				pop_back(); }		return (_M == 0 ? begin() : begin() + _M); }	void clear()		{while (!empty())			pop_back();		_Freemap(); }	void swap(_Myt& _X)		{if (_Alval == _X._Alval)			{std::swap(_Map, _X._Map);			std::swap(_Mapsize, _X._Mapsize);			std::swap(_Offset, _X._Offset);			std::swap(_Size, _X._Size); }		else			{_Myt _Ts = *this; *this = _X, _X = _Ts; }}	friend void swap(_Myt& _X, _Myt& _Y)		{_X.swap(_Y); }protected:	void _Xlen() const		{_THROW(length_error, "deque<T> too long"); }	void _Xran() const		{_THROW(out_of_range, "invalid deque<T> subscript"); }	void _Freemap()		{for (size_type _M = _Mapsize; 0 < _M; )			_Alval.deallocate(*(_Map + --_M), _DEQUESIZ);		_Alval.deallocate(_Map, _Mapsize);		_Mapsize = 0;		_Map = 0; }	void _Growmap(size_type _N)		{if (max_size() / _DEQUESIZ - _Mapsize < _N)			_Xlen();		size_type _I = _Mapsize / 2;	// try to grow by 50%		if (_I < _DEQUEMAPSIZ)			_I = _DEQUEMAPSIZ;		if (_N < _I && _Mapsize <= max_size() / _DEQUESIZ - _I)			_N = _I;		size_type _Ib = _Offset / _DEQUESIZ;		_Mapptr _M = (_Mapptr)_Alval._Charalloc(			(_Mapsize + _N) * sizeof (_Tptr));		_Mapptr _Mn = copy(_Map + _Ib, _Map + _Mapsize,			_M + _Ib);		if (_Ib <= _N)			{_Mn = copy(_Map, _Map + _Ib, _Mn);			fill_n(_Mn, _N - _Ib, (_Tptr)0);			fill_n(_M, _Ib, (_Tptr)0); }		else			{copy(_Map, _Map + _N, _Mn);			_Mn = copy(_Map + _N, _Map + _Ib, _M);			fill_n(_Mn, _N, (_Tptr)0); }		_Alval.deallocate(_Map, _Mapsize);	// free storage for old		_Map = _M;		_Mapsize += _N; }	_Mapptr _Map;	size_type _Mapsize, _Offset, _Size;	};		// deque TEMPLATE OPERATORStemplate<class _Ty, class _A> inline	bool operator==(const deque<_Ty, _A>& _X,		const deque<_Ty, _A>& _Y)	{return (_X.size() == _Y.size()		&& equal(_X.begin(), _X.end(), _Y.begin())); }template<class _Ty, class _A> inline	bool operator!=(const deque<_Ty, _A>& _X,		const deque<_Ty, _A>& _Y)	{return (!(_X == _Y)); }template<class _Ty, class _A> inline	bool operator<(const deque<_Ty, _A>& _X,		const deque<_Ty, _A>& _Y)	{return (lexicographical_compare(_X.begin(), _X.end(),		_Y.begin(), _Y.end())); }template<class _Ty, class _A> inline	bool operator<=(const deque<_Ty, _A>& _X,		const deque<_Ty, _A>& _Y)	{return (!(_Y < _X)); }template<class _Ty, class _A> inline	bool operator>(const deque<_Ty, _A>& _X,		const deque<_Ty, _A>& _Y)	{return (_Y < _X); }template<class _Ty, class _A> inline	bool operator>=(const deque<_Ty, _A>& _X,		const deque<_Ty, _A>& _Y)	{return (!(_X < _Y)); }_STD_END #if 1200 <= _MSC_VER #pragma warning(default:4284) #endif#if 1200 <= _MSC_VER#pragma warning(pop)#endif#ifdef  _MSC_VER#pragma pack(pop)#endif  /* _MSC_VER */#endif /* _DEQUE_ *//* * Copyright (c) 1995-1999 by P.J. Plauger.  ALL RIGHTS RESERVED.  * Consult your license regarding permissions and restrictions. *//* * 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. V2.33:0009 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?