1*c2c66affSColin Finck /*
2*c2c66affSColin Finck *
3*c2c66affSColin Finck *
4*c2c66affSColin Finck * Copyright (c) 1994
5*c2c66affSColin Finck * Hewlett-Packard Company
6*c2c66affSColin Finck *
7*c2c66affSColin Finck * Copyright (c) 1996,1997
8*c2c66affSColin Finck * Silicon Graphics Computer Systems, Inc.
9*c2c66affSColin Finck *
10*c2c66affSColin Finck * Copyright (c) 1997
11*c2c66affSColin Finck * Moscow Center for SPARC Technology
12*c2c66affSColin Finck *
13*c2c66affSColin Finck * Copyright (c) 1999
14*c2c66affSColin Finck * Boris Fomitchev
15*c2c66affSColin Finck *
16*c2c66affSColin Finck * This material is provided "as is", with absolutely no warranty expressed
17*c2c66affSColin Finck * or implied. Any use is at your own risk.
18*c2c66affSColin Finck *
19*c2c66affSColin Finck * Permission to use or copy this software for any purpose is hereby granted
20*c2c66affSColin Finck * without fee, provided the above notices are retained on all copies.
21*c2c66affSColin Finck * Permission to modify the code and to distribute modified code is granted,
22*c2c66affSColin Finck * provided the above notices are retained, and a notice that the code was
23*c2c66affSColin Finck * modified is included with the above copyright notice.
24*c2c66affSColin Finck *
25*c2c66affSColin Finck */
26*c2c66affSColin Finck #ifndef _STLP_DEQUE_C
27*c2c66affSColin Finck #define _STLP_DEQUE_C
28*c2c66affSColin Finck
29*c2c66affSColin Finck #ifndef _STLP_INTERNAL_DEQUE_H
30*c2c66affSColin Finck # include <stl/_deque.h>
31*c2c66affSColin Finck #endif
32*c2c66affSColin Finck
33*c2c66affSColin Finck _STLP_BEGIN_NAMESPACE
34*c2c66affSColin Finck
35*c2c66affSColin Finck _STLP_MOVE_TO_PRIV_NAMESPACE
36*c2c66affSColin Finck
37*c2c66affSColin Finck // Non-inline member functions from _Deque_base.
38*c2c66affSColin Finck
39*c2c66affSColin Finck template <class _Tp, class _Alloc >
~_Deque_base()40*c2c66affSColin Finck _Deque_base<_Tp,_Alloc >::~_Deque_base() {
41*c2c66affSColin Finck if (_M_map._M_data) {
42*c2c66affSColin Finck _M_destroy_nodes(_M_start._M_node, this->_M_finish._M_node + 1);
43*c2c66affSColin Finck _M_map.deallocate(_M_map._M_data, _M_map_size._M_data);
44*c2c66affSColin Finck }
45*c2c66affSColin Finck }
46*c2c66affSColin Finck
47*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_initialize_map(size_t __num_elements)48*c2c66affSColin Finck void _Deque_base<_Tp,_Alloc>::_M_initialize_map(size_t __num_elements) {
49*c2c66affSColin Finck size_t __num_nodes = __num_elements / this->buffer_size() + 1 ;
50*c2c66affSColin Finck
51*c2c66affSColin Finck _M_map_size._M_data = (max)((size_t) _S_initial_map_size, __num_nodes + 2);
52*c2c66affSColin Finck _M_map._M_data = _M_map.allocate(_M_map_size._M_data);
53*c2c66affSColin Finck
54*c2c66affSColin Finck _Tp** __nstart = _M_map._M_data + (_M_map_size._M_data - __num_nodes) / 2;
55*c2c66affSColin Finck _Tp** __nfinish = __nstart + __num_nodes;
56*c2c66affSColin Finck
57*c2c66affSColin Finck _STLP_TRY {
58*c2c66affSColin Finck _M_create_nodes(__nstart, __nfinish);
59*c2c66affSColin Finck }
60*c2c66affSColin Finck _STLP_UNWIND((_M_map.deallocate(_M_map._M_data, _M_map_size._M_data),
61*c2c66affSColin Finck _M_map._M_data = 0, _M_map_size._M_data = 0))
62*c2c66affSColin Finck _M_start._M_set_node(__nstart);
63*c2c66affSColin Finck this->_M_finish._M_set_node(__nfinish - 1);
64*c2c66affSColin Finck _M_start._M_cur = _M_start._M_first;
65*c2c66affSColin Finck this->_M_finish._M_cur = this->_M_finish._M_first + __num_elements % this->buffer_size();
66*c2c66affSColin Finck }
67*c2c66affSColin Finck
68*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_create_nodes(_Tp ** __nstart,_Tp ** __nfinish)69*c2c66affSColin Finck void _Deque_base<_Tp,_Alloc>::_M_create_nodes(_Tp** __nstart,
70*c2c66affSColin Finck _Tp** __nfinish) {
71*c2c66affSColin Finck _Tp** __cur = __nstart;
72*c2c66affSColin Finck _STLP_TRY {
73*c2c66affSColin Finck for (; __cur < __nfinish; ++__cur)
74*c2c66affSColin Finck *__cur = _M_map_size.allocate(this->buffer_size());
75*c2c66affSColin Finck }
76*c2c66affSColin Finck _STLP_UNWIND(_M_destroy_nodes(__nstart, __cur))
77*c2c66affSColin Finck }
78*c2c66affSColin Finck
79*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_destroy_nodes(_Tp ** __nstart,_Tp ** __nfinish)80*c2c66affSColin Finck void _Deque_base<_Tp,_Alloc>::_M_destroy_nodes(_Tp** __nstart,
81*c2c66affSColin Finck _Tp** __nfinish) {
82*c2c66affSColin Finck for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
83*c2c66affSColin Finck _M_map_size.deallocate(*__n, this->buffer_size());
84*c2c66affSColin Finck }
85*c2c66affSColin Finck
86*c2c66affSColin Finck #if defined (_STLP_USE_PTR_SPECIALIZATIONS)
87*c2c66affSColin Finck # define deque _STLP_PTR_IMPL_NAME(deque)
88*c2c66affSColin Finck #elif defined (_STLP_DEBUG)
89*c2c66affSColin Finck # define deque _STLP_NON_DBG_NAME(deque)
90*c2c66affSColin Finck #else
91*c2c66affSColin Finck _STLP_MOVE_TO_STD_NAMESPACE
92*c2c66affSColin Finck #endif
93*c2c66affSColin Finck
94*c2c66affSColin Finck #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
95*c2c66affSColin Finck // qualified references
96*c2c66affSColin Finck # define __iterator__ _Deque_iterator<_Tp, _Nonconst_traits<_Tp> >
97*c2c66affSColin Finck # define const_iterator _Deque_iterator<_Tp, _Const_traits<_Tp> >
98*c2c66affSColin Finck # define iterator __iterator__
99*c2c66affSColin Finck # define size_type size_t
100*c2c66affSColin Finck # define value_type _Tp
101*c2c66affSColin Finck #else
102*c2c66affSColin Finck # define __iterator__ _STLP_TYPENAME_ON_RETURN_TYPE deque<_Tp, _Alloc>::iterator
103*c2c66affSColin Finck #endif
104*c2c66affSColin Finck
105*c2c66affSColin Finck template <class _Tp, class _Alloc >
106*c2c66affSColin Finck deque<_Tp, _Alloc >&
107*c2c66affSColin Finck deque<_Tp, _Alloc >::operator= (const deque<_Tp, _Alloc >& __x) {
108*c2c66affSColin Finck const size_type __len = size();
109*c2c66affSColin Finck if (&__x != this) {
110*c2c66affSColin Finck if (__len >= __x.size())
111*c2c66affSColin Finck erase(_STLP_STD::copy(__x.begin(), __x.end(), this->_M_start), this->_M_finish);
112*c2c66affSColin Finck else {
113*c2c66affSColin Finck const_iterator __mid = __x.begin() + difference_type(__len);
114*c2c66affSColin Finck _STLP_STD::copy(__x.begin(), __mid, this->_M_start);
115*c2c66affSColin Finck insert(this->_M_finish, __mid, __x.end());
116*c2c66affSColin Finck }
117*c2c66affSColin Finck }
118*c2c66affSColin Finck return *this;
119*c2c66affSColin Finck }
120*c2c66affSColin Finck
121*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_fill_insert(iterator __pos,size_type __n,const value_type & __x)122*c2c66affSColin Finck void deque<_Tp, _Alloc >::_M_fill_insert(iterator __pos,
123*c2c66affSColin Finck size_type __n, const value_type& __x) {
124*c2c66affSColin Finck #if !defined (_STLP_NO_MOVE_SEMANTIC)
125*c2c66affSColin Finck typedef typename __move_traits<_Tp>::implemented _Movable;
126*c2c66affSColin Finck #endif
127*c2c66affSColin Finck if (__pos._M_cur == this->_M_start._M_cur) {
128*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
129*c2c66affSColin Finck _STLP_TRY {
130*c2c66affSColin Finck uninitialized_fill(__new_start, this->_M_start, __x);
131*c2c66affSColin Finck }
132*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
133*c2c66affSColin Finck this->_M_start = __new_start;
134*c2c66affSColin Finck }
135*c2c66affSColin Finck else if (__pos._M_cur == this->_M_finish._M_cur) {
136*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
137*c2c66affSColin Finck _STLP_TRY {
138*c2c66affSColin Finck uninitialized_fill(this->_M_finish, __new_finish, __x);
139*c2c66affSColin Finck }
140*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node+1, __new_finish._M_node+1))
141*c2c66affSColin Finck this->_M_finish = __new_finish;
142*c2c66affSColin Finck }
143*c2c66affSColin Finck else
144*c2c66affSColin Finck _M_fill_insert_aux(__pos, __n, __x, _Movable());
145*c2c66affSColin Finck }
146*c2c66affSColin Finck
147*c2c66affSColin Finck #if !defined (_STLP_MEMBER_TEMPLATES)
148*c2c66affSColin Finck
149*c2c66affSColin Finck template <class _Tp, class _Alloc >
insert(iterator __pos,const value_type * __first,const value_type * __last)150*c2c66affSColin Finck void deque<_Tp, _Alloc>::insert(iterator __pos,
151*c2c66affSColin Finck const value_type* __first, const value_type* __last) {
152*c2c66affSColin Finck #if !defined (_STLP_NO_MOVE_SEMANTIC)
153*c2c66affSColin Finck typedef typename __move_traits<_Tp>::implemented _Movable;
154*c2c66affSColin Finck #endif
155*c2c66affSColin Finck size_type __n = __last - __first;
156*c2c66affSColin Finck if (__pos._M_cur == this->_M_start._M_cur) {
157*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
158*c2c66affSColin Finck _STLP_TRY {
159*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, __new_start);
160*c2c66affSColin Finck }
161*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
162*c2c66affSColin Finck this->_M_start = __new_start;
163*c2c66affSColin Finck }
164*c2c66affSColin Finck else if (__pos._M_cur == this->_M_finish._M_cur) {
165*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
166*c2c66affSColin Finck _STLP_TRY {
167*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, this->_M_finish);
168*c2c66affSColin Finck }
169*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1,
170*c2c66affSColin Finck __new_finish._M_node + 1))
171*c2c66affSColin Finck this->_M_finish = __new_finish;
172*c2c66affSColin Finck }
173*c2c66affSColin Finck else
174*c2c66affSColin Finck _M_insert_range_aux(__pos, __first, __last, __n, _Movable());
175*c2c66affSColin Finck }
176*c2c66affSColin Finck
177*c2c66affSColin Finck template <class _Tp, class _Alloc >
insert(iterator __pos,const_iterator __first,const_iterator __last)178*c2c66affSColin Finck void deque<_Tp,_Alloc>::insert(iterator __pos,
179*c2c66affSColin Finck const_iterator __first, const_iterator __last) {
180*c2c66affSColin Finck #if !defined (_STLP_NO_MOVE_SEMANTIC)
181*c2c66affSColin Finck typedef typename __move_traits<_Tp>::implemented _Movable;
182*c2c66affSColin Finck #endif
183*c2c66affSColin Finck size_type __n = __last - __first;
184*c2c66affSColin Finck if (__pos._M_cur == this->_M_start._M_cur) {
185*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
186*c2c66affSColin Finck _STLP_TRY {
187*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, __new_start);
188*c2c66affSColin Finck }
189*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
190*c2c66affSColin Finck this->_M_start = __new_start;
191*c2c66affSColin Finck }
192*c2c66affSColin Finck else if (__pos._M_cur == this->_M_finish._M_cur) {
193*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
194*c2c66affSColin Finck _STLP_TRY {
195*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, this->_M_finish);
196*c2c66affSColin Finck }
197*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1,
198*c2c66affSColin Finck __new_finish._M_node + 1))
199*c2c66affSColin Finck this->_M_finish = __new_finish;
200*c2c66affSColin Finck }
201*c2c66affSColin Finck else
202*c2c66affSColin Finck _M_insert_range_aux(__pos, __first, __last, __n, _Movable());
203*c2c66affSColin Finck }
204*c2c66affSColin Finck
205*c2c66affSColin Finck #endif /* _STLP_MEMBER_TEMPLATES */
206*c2c66affSColin Finck
207*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_erase(iterator __pos,const __true_type &)208*c2c66affSColin Finck __iterator__ deque<_Tp,_Alloc>::_M_erase(iterator __pos,
209*c2c66affSColin Finck const __true_type& /*_Movable*/) {
210*c2c66affSColin Finck difference_type __index = __pos - this->_M_start;
211*c2c66affSColin Finck if (size_type(__index) < this->size() >> 1) {
212*c2c66affSColin Finck //We move the start of the deque one position to the right
213*c2c66affSColin Finck //starting from the rightmost element to move.
214*c2c66affSColin Finck iterator __src = __pos, __dst = __pos;
215*c2c66affSColin Finck _STLP_STD::_Destroy(&(*__dst));
216*c2c66affSColin Finck if (__src != this->_M_start) {
217*c2c66affSColin Finck for (--__src; __dst != this->_M_start; --__src, --__dst) {
218*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
219*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
220*c2c66affSColin Finck }
221*c2c66affSColin Finck }
222*c2c66affSColin Finck _M_pop_front_aux();
223*c2c66affSColin Finck }
224*c2c66affSColin Finck else {
225*c2c66affSColin Finck iterator __src = __pos, __dst = __pos;
226*c2c66affSColin Finck _STLP_STD::_Destroy(&(*__dst));
227*c2c66affSColin Finck for (++__src; __src != this->_M_finish; ++__src, ++__dst) {
228*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
229*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
230*c2c66affSColin Finck }
231*c2c66affSColin Finck //Duplication of the pop_back code without the destroy which has already been done:
232*c2c66affSColin Finck if (this->_M_finish._M_cur != this->_M_finish._M_first) {
233*c2c66affSColin Finck --this->_M_finish._M_cur;
234*c2c66affSColin Finck }
235*c2c66affSColin Finck else {
236*c2c66affSColin Finck _M_pop_back_aux();
237*c2c66affSColin Finck }
238*c2c66affSColin Finck }
239*c2c66affSColin Finck return this->_M_start + __index;
240*c2c66affSColin Finck }
241*c2c66affSColin Finck
242*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_erase(iterator __pos,const __false_type &)243*c2c66affSColin Finck __iterator__ deque<_Tp,_Alloc>::_M_erase(iterator __pos,
244*c2c66affSColin Finck const __false_type& /*_Movable*/) {
245*c2c66affSColin Finck iterator __next = __pos;
246*c2c66affSColin Finck ++__next;
247*c2c66affSColin Finck difference_type __index = __pos - this->_M_start;
248*c2c66affSColin Finck if (size_type(__index) < this->size() >> 1) {
249*c2c66affSColin Finck copy_backward(this->_M_start, __pos, __next);
250*c2c66affSColin Finck pop_front();
251*c2c66affSColin Finck }
252*c2c66affSColin Finck else {
253*c2c66affSColin Finck _STLP_STD::copy(__next, this->_M_finish, __pos);
254*c2c66affSColin Finck pop_back();
255*c2c66affSColin Finck }
256*c2c66affSColin Finck return this->_M_start + __index;
257*c2c66affSColin Finck }
258*c2c66affSColin Finck
259*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_erase(iterator __first,iterator __last,const __true_type &)260*c2c66affSColin Finck __iterator__ deque<_Tp,_Alloc>::_M_erase(iterator __first, iterator __last,
261*c2c66affSColin Finck const __true_type& /*_Movable*/) {
262*c2c66affSColin Finck difference_type __n = __last - __first;
263*c2c66affSColin Finck difference_type __elems_before = __first - this->_M_start;
264*c2c66affSColin Finck if (__elems_before <= difference_type(this->size() - __n) / 2) {
265*c2c66affSColin Finck iterator __src = __first, __dst = __last;
266*c2c66affSColin Finck if (__src != this->_M_start) {
267*c2c66affSColin Finck for (--__src, --__dst; (__src >= this->_M_start) && (__dst >= __first); --__src, --__dst) {
268*c2c66affSColin Finck _STLP_STD::_Destroy(&(*__dst));
269*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
270*c2c66affSColin Finck }
271*c2c66affSColin Finck if (__dst >= __first) {
272*c2c66affSColin Finck //There are more elements to erase than elements to move
273*c2c66affSColin Finck _STLP_STD::_Destroy_Range(__first, ++__dst);
274*c2c66affSColin Finck _STLP_STD::_Destroy_Moved_Range(this->_M_start, __first);
275*c2c66affSColin Finck }
276*c2c66affSColin Finck else {
277*c2c66affSColin Finck //There are more elements to move than elements to erase
278*c2c66affSColin Finck for (; __src >= this->_M_start; --__src, --__dst) {
279*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__dst));
280*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
281*c2c66affSColin Finck }
282*c2c66affSColin Finck _STLP_STD::_Destroy_Moved_Range(this->_M_start, ++__dst);
283*c2c66affSColin Finck }
284*c2c66affSColin Finck }
285*c2c66affSColin Finck else {
286*c2c66affSColin Finck _STLP_STD::_Destroy_Range(this->_M_start, __last);
287*c2c66affSColin Finck }
288*c2c66affSColin Finck iterator __new_start = this->_M_start + __n;
289*c2c66affSColin Finck this->_M_destroy_nodes(this->_M_start._M_node, __new_start._M_node);
290*c2c66affSColin Finck this->_M_start = __new_start;
291*c2c66affSColin Finck }
292*c2c66affSColin Finck else {
293*c2c66affSColin Finck if (__last != this->_M_finish) {
294*c2c66affSColin Finck iterator __src = __last, __dst = __first;
295*c2c66affSColin Finck for (; (__src != this->_M_finish) && (__dst != __last); ++__src, ++__dst) {
296*c2c66affSColin Finck _STLP_STD::_Destroy(&(*__dst));
297*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
298*c2c66affSColin Finck }
299*c2c66affSColin Finck if (__dst != __last) {
300*c2c66affSColin Finck //There are more elements to erase than elements to move
301*c2c66affSColin Finck _STLP_STD::_Destroy_Range(__dst, __last);
302*c2c66affSColin Finck _STLP_STD::_Destroy_Moved_Range(__last, this->_M_finish);
303*c2c66affSColin Finck }
304*c2c66affSColin Finck else {
305*c2c66affSColin Finck //There are more elements to move than elements to erase
306*c2c66affSColin Finck for (; __src != this->_M_finish; ++__src, ++__dst) {
307*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__dst));
308*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
309*c2c66affSColin Finck }
310*c2c66affSColin Finck _STLP_STD::_Destroy_Moved_Range(__dst, this->_M_finish);
311*c2c66affSColin Finck }
312*c2c66affSColin Finck }
313*c2c66affSColin Finck else {
314*c2c66affSColin Finck _STLP_STD::_Destroy_Range(__first, this->_M_finish);
315*c2c66affSColin Finck }
316*c2c66affSColin Finck iterator __new_finish = this->_M_finish - __n;
317*c2c66affSColin Finck this->_M_destroy_nodes(__new_finish._M_node + 1, this->_M_finish._M_node + 1);
318*c2c66affSColin Finck this->_M_finish = __new_finish;
319*c2c66affSColin Finck }
320*c2c66affSColin Finck return this->_M_start + __elems_before;
321*c2c66affSColin Finck }
322*c2c66affSColin Finck
323*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_erase(iterator __first,iterator __last,const __false_type &)324*c2c66affSColin Finck __iterator__ deque<_Tp,_Alloc>::_M_erase(iterator __first, iterator __last,
325*c2c66affSColin Finck const __false_type& /*_Movable*/) {
326*c2c66affSColin Finck difference_type __n = __last - __first;
327*c2c66affSColin Finck difference_type __elems_before = __first - this->_M_start;
328*c2c66affSColin Finck if (__elems_before <= difference_type(this->size() - __n) / 2) {
329*c2c66affSColin Finck copy_backward(this->_M_start, __first, __last);
330*c2c66affSColin Finck iterator __new_start = this->_M_start + __n;
331*c2c66affSColin Finck _STLP_STD::_Destroy_Range(this->_M_start, __new_start);
332*c2c66affSColin Finck this->_M_destroy_nodes(this->_M_start._M_node, __new_start._M_node);
333*c2c66affSColin Finck this->_M_start = __new_start;
334*c2c66affSColin Finck }
335*c2c66affSColin Finck else {
336*c2c66affSColin Finck _STLP_STD::copy(__last, this->_M_finish, __first);
337*c2c66affSColin Finck iterator __new_finish = this->_M_finish - __n;
338*c2c66affSColin Finck _STLP_STD::_Destroy_Range(__new_finish, this->_M_finish);
339*c2c66affSColin Finck this->_M_destroy_nodes(__new_finish._M_node + 1, this->_M_finish._M_node + 1);
340*c2c66affSColin Finck this->_M_finish = __new_finish;
341*c2c66affSColin Finck }
342*c2c66affSColin Finck return this->_M_start + __elems_before;
343*c2c66affSColin Finck }
344*c2c66affSColin Finck
345*c2c66affSColin Finck template <class _Tp, class _Alloc >
clear()346*c2c66affSColin Finck void deque<_Tp,_Alloc>::clear() {
347*c2c66affSColin Finck for (_Map_pointer __node = this->_M_start._M_node + 1;
348*c2c66affSColin Finck __node < this->_M_finish._M_node;
349*c2c66affSColin Finck ++__node) {
350*c2c66affSColin Finck _STLP_STD::_Destroy_Range(*__node, *__node + this->buffer_size());
351*c2c66affSColin Finck this->_M_map_size.deallocate(*__node, this->buffer_size());
352*c2c66affSColin Finck }
353*c2c66affSColin Finck
354*c2c66affSColin Finck if (this->_M_start._M_node != this->_M_finish._M_node) {
355*c2c66affSColin Finck _STLP_STD::_Destroy_Range(this->_M_start._M_cur, this->_M_start._M_last);
356*c2c66affSColin Finck _STLP_STD::_Destroy_Range(this->_M_finish._M_first, this->_M_finish._M_cur);
357*c2c66affSColin Finck this->_M_map_size.deallocate(this->_M_finish._M_first, this->buffer_size());
358*c2c66affSColin Finck }
359*c2c66affSColin Finck else
360*c2c66affSColin Finck _STLP_STD::_Destroy_Range(this->_M_start._M_cur, this->_M_finish._M_cur);
361*c2c66affSColin Finck
362*c2c66affSColin Finck this->_M_finish = this->_M_start;
363*c2c66affSColin Finck }
364*c2c66affSColin Finck
365*c2c66affSColin Finck // Precondition: this->_M_start and this->_M_finish have already been initialized,
366*c2c66affSColin Finck // but none of the deque's elements have yet been constructed.
367*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_fill_initialize(const value_type & __val,const __false_type &)368*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_fill_initialize(const value_type& __val,
369*c2c66affSColin Finck const __false_type& /*_TrivialInit*/) {
370*c2c66affSColin Finck _Map_pointer __cur = this->_M_start._M_node;
371*c2c66affSColin Finck _STLP_TRY {
372*c2c66affSColin Finck for (; __cur < this->_M_finish._M_node; ++__cur)
373*c2c66affSColin Finck uninitialized_fill(*__cur, *__cur + this->buffer_size(), __val);
374*c2c66affSColin Finck uninitialized_fill(this->_M_finish._M_first, this->_M_finish._M_cur, __val);
375*c2c66affSColin Finck }
376*c2c66affSColin Finck _STLP_UNWIND(_STLP_STD::_Destroy_Range(this->_M_start, iterator(*__cur, __cur)))
377*c2c66affSColin Finck }
378*c2c66affSColin Finck
379*c2c66affSColin Finck
380*c2c66affSColin Finck // Called only if this->_M_finish._M_cur == this->_M_finish._M_last - 1.
381*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_push_back_aux_v(const value_type & __t)382*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_push_back_aux_v(const value_type& __t) {
383*c2c66affSColin Finck _M_reserve_map_at_back();
384*c2c66affSColin Finck *(this->_M_finish._M_node + 1) = this->_M_map_size.allocate(this->buffer_size());
385*c2c66affSColin Finck _STLP_TRY {
386*c2c66affSColin Finck _Copy_Construct(this->_M_finish._M_cur, __t);
387*c2c66affSColin Finck this->_M_finish._M_set_node(this->_M_finish._M_node + 1);
388*c2c66affSColin Finck this->_M_finish._M_cur = this->_M_finish._M_first;
389*c2c66affSColin Finck }
390*c2c66affSColin Finck _STLP_UNWIND(this->_M_map_size.deallocate(*(this->_M_finish._M_node + 1),
391*c2c66affSColin Finck this->buffer_size()))
392*c2c66affSColin Finck }
393*c2c66affSColin Finck
394*c2c66affSColin Finck #if defined(_STLP_DONT_SUP_DFLT_PARAM) && !defined(_STLP_NO_ANACHRONISMS)
395*c2c66affSColin Finck // Called only if this->_M_finish._M_cur == this->_M_finish._M_last - 1.
396*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_push_back_aux()397*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_push_back_aux() {
398*c2c66affSColin Finck _M_reserve_map_at_back();
399*c2c66affSColin Finck *(this->_M_finish._M_node + 1) = this->_M_map_size.allocate(this->buffer_size());
400*c2c66affSColin Finck _STLP_TRY {
401*c2c66affSColin Finck _STLP_STD::_Construct(this->_M_finish._M_cur);
402*c2c66affSColin Finck this->_M_finish._M_set_node(this->_M_finish._M_node + 1);
403*c2c66affSColin Finck this->_M_finish._M_cur = this->_M_finish._M_first;
404*c2c66affSColin Finck }
405*c2c66affSColin Finck _STLP_UNWIND(this->_M_map_size.deallocate(*(this->_M_finish._M_node + 1),
406*c2c66affSColin Finck this->buffer_size()))
407*c2c66affSColin Finck }
408*c2c66affSColin Finck #endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
409*c2c66affSColin Finck
410*c2c66affSColin Finck // Called only if this->_M_start._M_cur == this->_M_start._M_first.
411*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_push_front_aux_v(const value_type & __t)412*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_push_front_aux_v(const value_type& __t) {
413*c2c66affSColin Finck _M_reserve_map_at_front();
414*c2c66affSColin Finck *(this->_M_start._M_node - 1) = this->_M_map_size.allocate(this->buffer_size());
415*c2c66affSColin Finck _STLP_TRY {
416*c2c66affSColin Finck this->_M_start._M_set_node(this->_M_start._M_node - 1);
417*c2c66affSColin Finck this->_M_start._M_cur = this->_M_start._M_last - 1;
418*c2c66affSColin Finck _Copy_Construct(this->_M_start._M_cur, __t);
419*c2c66affSColin Finck }
420*c2c66affSColin Finck _STLP_UNWIND((++this->_M_start,
421*c2c66affSColin Finck this->_M_map_size.deallocate(*(this->_M_start._M_node - 1), this->buffer_size())))
422*c2c66affSColin Finck }
423*c2c66affSColin Finck
424*c2c66affSColin Finck
425*c2c66affSColin Finck #if defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
426*c2c66affSColin Finck // Called only if this->_M_start._M_cur == this->_M_start._M_first.
427*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_push_front_aux()428*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_push_front_aux() {
429*c2c66affSColin Finck _M_reserve_map_at_front();
430*c2c66affSColin Finck *(this->_M_start._M_node - 1) = this->_M_map_size.allocate(this->buffer_size());
431*c2c66affSColin Finck _STLP_TRY {
432*c2c66affSColin Finck this->_M_start._M_set_node(this->_M_start._M_node - 1);
433*c2c66affSColin Finck this->_M_start._M_cur = this->_M_start._M_last - 1;
434*c2c66affSColin Finck _STLP_STD::_Construct(this->_M_start._M_cur);
435*c2c66affSColin Finck }
436*c2c66affSColin Finck _STLP_UNWIND((++this->_M_start, this->_M_map_size.deallocate(*(this->_M_start._M_node - 1),
437*c2c66affSColin Finck this->buffer_size())))
438*c2c66affSColin Finck }
439*c2c66affSColin Finck #endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
440*c2c66affSColin Finck
441*c2c66affSColin Finck // Called only if this->_M_finish._M_cur == this->_M_finish._M_first.
442*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_pop_back_aux()443*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_pop_back_aux() {
444*c2c66affSColin Finck this->_M_map_size.deallocate(this->_M_finish._M_first, this->buffer_size());
445*c2c66affSColin Finck this->_M_finish._M_set_node(this->_M_finish._M_node - 1);
446*c2c66affSColin Finck this->_M_finish._M_cur = this->_M_finish._M_last - 1;
447*c2c66affSColin Finck }
448*c2c66affSColin Finck
449*c2c66affSColin Finck // Note that if the deque has at least one element (a precondition for this member
450*c2c66affSColin Finck // function), and if this->_M_start._M_cur == this->_M_start._M_last, then the deque
451*c2c66affSColin Finck // must have at least two nodes.
452*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_pop_front_aux()453*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_pop_front_aux() {
454*c2c66affSColin Finck if (this->_M_start._M_cur != this->_M_start._M_last - 1)
455*c2c66affSColin Finck ++this->_M_start._M_cur;
456*c2c66affSColin Finck else {
457*c2c66affSColin Finck this->_M_map_size.deallocate(this->_M_start._M_first, this->buffer_size());
458*c2c66affSColin Finck this->_M_start._M_set_node(this->_M_start._M_node + 1);
459*c2c66affSColin Finck this->_M_start._M_cur = this->_M_start._M_first;
460*c2c66affSColin Finck }
461*c2c66affSColin Finck }
462*c2c66affSColin Finck
463*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_fill_insert_aux(iterator __pos,size_type __n,const value_type & __x,const __true_type &)464*c2c66affSColin Finck __iterator__ deque<_Tp,_Alloc>::_M_fill_insert_aux(iterator __pos, size_type __n,
465*c2c66affSColin Finck const value_type& __x,
466*c2c66affSColin Finck const __true_type& /*_Movable*/) {
467*c2c66affSColin Finck const difference_type __elems_before = __pos - this->_M_start;
468*c2c66affSColin Finck size_type __length = this->size();
469*c2c66affSColin Finck value_type __x_copy = __x;
470*c2c66affSColin Finck if (__elems_before <= difference_type(__length / 2)) {
471*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
472*c2c66affSColin Finck __pos = this->_M_start + __elems_before;
473*c2c66affSColin Finck _STLP_TRY {
474*c2c66affSColin Finck iterator __dst = __new_start;
475*c2c66affSColin Finck iterator __src = this->_M_start;
476*c2c66affSColin Finck for (; __src != __pos; ++__dst, ++__src) {
477*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
478*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
479*c2c66affSColin Finck }
480*c2c66affSColin Finck this->_M_start = __new_start;
481*c2c66affSColin Finck uninitialized_fill(__dst, __src, __x_copy);
482*c2c66affSColin Finck __pos = __dst;
483*c2c66affSColin Finck }
484*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
485*c2c66affSColin Finck }
486*c2c66affSColin Finck else {
487*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
488*c2c66affSColin Finck const difference_type __elems_after = difference_type(__length) - __elems_before;
489*c2c66affSColin Finck __pos = this->_M_finish - __elems_after;
490*c2c66affSColin Finck _STLP_TRY {
491*c2c66affSColin Finck iterator __dst = __new_finish;
492*c2c66affSColin Finck iterator __src = this->_M_finish;
493*c2c66affSColin Finck for (--__src, --__dst; __src >= __pos; --__src, --__dst) {
494*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
495*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
496*c2c66affSColin Finck }
497*c2c66affSColin Finck this->_M_finish = __new_finish;
498*c2c66affSColin Finck uninitialized_fill(__pos, __pos + __n, __x_copy);
499*c2c66affSColin Finck }
500*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1))
501*c2c66affSColin Finck }
502*c2c66affSColin Finck return __pos;
503*c2c66affSColin Finck }
504*c2c66affSColin Finck
505*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_fill_insert_aux(iterator __pos,size_type __n,const value_type & __x,const __false_type &)506*c2c66affSColin Finck __iterator__ deque<_Tp,_Alloc>::_M_fill_insert_aux(iterator __pos, size_type __n,
507*c2c66affSColin Finck const value_type& __x,
508*c2c66affSColin Finck const __false_type& /*_Movable*/) {
509*c2c66affSColin Finck const difference_type __elems_before = __pos - this->_M_start;
510*c2c66affSColin Finck size_type __length = this->size();
511*c2c66affSColin Finck value_type __x_copy = __x;
512*c2c66affSColin Finck if (__elems_before <= difference_type(__length / 2)) {
513*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
514*c2c66affSColin Finck iterator __old_start = this->_M_start;
515*c2c66affSColin Finck __pos = this->_M_start + __elems_before;
516*c2c66affSColin Finck _STLP_TRY {
517*c2c66affSColin Finck if (__elems_before >= difference_type(__n)) {
518*c2c66affSColin Finck iterator __start_n = this->_M_start + difference_type(__n);
519*c2c66affSColin Finck _STLP_PRIV __ucopy(this->_M_start, __start_n, __new_start);
520*c2c66affSColin Finck this->_M_start = __new_start;
521*c2c66affSColin Finck _STLP_STD::copy(__start_n, __pos, __old_start);
522*c2c66affSColin Finck _STLP_STD::fill(__pos - difference_type(__n), __pos, __x_copy);
523*c2c66affSColin Finck __pos -= difference_type(__n);
524*c2c66affSColin Finck }
525*c2c66affSColin Finck else {
526*c2c66affSColin Finck _STLP_PRIV __uninitialized_copy_fill(this->_M_start, __pos, __new_start,
527*c2c66affSColin Finck this->_M_start, __x_copy);
528*c2c66affSColin Finck this->_M_start = __new_start;
529*c2c66affSColin Finck fill(__old_start, __pos, __x_copy);
530*c2c66affSColin Finck }
531*c2c66affSColin Finck }
532*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
533*c2c66affSColin Finck }
534*c2c66affSColin Finck else {
535*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
536*c2c66affSColin Finck iterator __old_finish = this->_M_finish;
537*c2c66affSColin Finck const difference_type __elems_after =
538*c2c66affSColin Finck difference_type(__length) - __elems_before;
539*c2c66affSColin Finck __pos = this->_M_finish - __elems_after;
540*c2c66affSColin Finck _STLP_TRY {
541*c2c66affSColin Finck if (__elems_after > difference_type(__n)) {
542*c2c66affSColin Finck iterator __finish_n = this->_M_finish - difference_type(__n);
543*c2c66affSColin Finck _STLP_PRIV __ucopy(__finish_n, this->_M_finish, this->_M_finish);
544*c2c66affSColin Finck this->_M_finish = __new_finish;
545*c2c66affSColin Finck copy_backward(__pos, __finish_n, __old_finish);
546*c2c66affSColin Finck fill(__pos, __pos + difference_type(__n), __x_copy);
547*c2c66affSColin Finck }
548*c2c66affSColin Finck else {
549*c2c66affSColin Finck _STLP_PRIV __uninitialized_fill_copy(this->_M_finish, __pos + difference_type(__n),
550*c2c66affSColin Finck __x_copy, __pos, this->_M_finish);
551*c2c66affSColin Finck this->_M_finish = __new_finish;
552*c2c66affSColin Finck fill(__pos, __old_finish, __x_copy);
553*c2c66affSColin Finck }
554*c2c66affSColin Finck }
555*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1))
556*c2c66affSColin Finck }
557*c2c66affSColin Finck return __pos;
558*c2c66affSColin Finck }
559*c2c66affSColin Finck
560*c2c66affSColin Finck #if !defined (_STLP_MEMBER_TEMPLATES)
561*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_insert_range_aux(iterator __pos,const value_type * __first,const value_type * __last,size_type __n,const __true_type &)562*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_insert_range_aux(iterator __pos,
563*c2c66affSColin Finck const value_type* __first, const value_type* __last,
564*c2c66affSColin Finck size_type __n, const __true_type& /*_Movable*/) {
565*c2c66affSColin Finck const difference_type __elems_before = __pos - this->_M_start;
566*c2c66affSColin Finck size_type __length = size();
567*c2c66affSColin Finck if (__elems_before <= difference_type(__length / 2)) {
568*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
569*c2c66affSColin Finck __pos = this->_M_start + __elems_before;
570*c2c66affSColin Finck _STLP_TRY {
571*c2c66affSColin Finck iterator __dst = __new_start;
572*c2c66affSColin Finck iterator __src = this->_M_start;
573*c2c66affSColin Finck for (; __src != __pos; ++__dst, ++__src) {
574*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
575*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
576*c2c66affSColin Finck }
577*c2c66affSColin Finck this->_M_start = __new_start;
578*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, __dst);
579*c2c66affSColin Finck }
580*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
581*c2c66affSColin Finck }
582*c2c66affSColin Finck else {
583*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
584*c2c66affSColin Finck const difference_type __elems_after = difference_type(__length) - __elems_before;
585*c2c66affSColin Finck __pos = this->_M_finish - __elems_after;
586*c2c66affSColin Finck _STLP_TRY {
587*c2c66affSColin Finck iterator __dst = __new_finish;
588*c2c66affSColin Finck iterator __src = this->_M_finish;
589*c2c66affSColin Finck for (--__src, --__dst; __src >= __pos; --__src, --__dst) {
590*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
591*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
592*c2c66affSColin Finck }
593*c2c66affSColin Finck this->_M_finish = __new_finish;
594*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, __pos);
595*c2c66affSColin Finck }
596*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1))
597*c2c66affSColin Finck }
598*c2c66affSColin Finck }
599*c2c66affSColin Finck
600*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_insert_range_aux(iterator __pos,const value_type * __first,const value_type * __last,size_type __n,const __false_type &)601*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_insert_range_aux(iterator __pos,
602*c2c66affSColin Finck const value_type* __first, const value_type* __last,
603*c2c66affSColin Finck size_type __n, const __false_type& /*_Movable*/) {
604*c2c66affSColin Finck const difference_type __elems_before = __pos - this->_M_start;
605*c2c66affSColin Finck size_type __length = size();
606*c2c66affSColin Finck if (__elems_before <= difference_type(__length / 2)) {
607*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
608*c2c66affSColin Finck iterator __old_start = this->_M_start;
609*c2c66affSColin Finck __pos = this->_M_start + __elems_before;
610*c2c66affSColin Finck _STLP_TRY {
611*c2c66affSColin Finck if (__elems_before >= difference_type(__n)) {
612*c2c66affSColin Finck iterator __start_n = this->_M_start + difference_type(__n);
613*c2c66affSColin Finck _STLP_PRIV __ucopy(this->_M_start, __start_n, __new_start);
614*c2c66affSColin Finck this->_M_start = __new_start;
615*c2c66affSColin Finck _STLP_STD::copy(__start_n, __pos, __old_start);
616*c2c66affSColin Finck _STLP_STD::copy(__first, __last, __pos - difference_type(__n));
617*c2c66affSColin Finck }
618*c2c66affSColin Finck else {
619*c2c66affSColin Finck const value_type* __mid = __first + (difference_type(__n) - __elems_before);
620*c2c66affSColin Finck _STLP_PRIV __uninitialized_copy_copy(this->_M_start, __pos, __first, __mid, __new_start);
621*c2c66affSColin Finck this->_M_start = __new_start;
622*c2c66affSColin Finck _STLP_STD::copy(__mid, __last, __old_start);
623*c2c66affSColin Finck }
624*c2c66affSColin Finck }
625*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
626*c2c66affSColin Finck }
627*c2c66affSColin Finck else {
628*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
629*c2c66affSColin Finck iterator __old_finish = this->_M_finish;
630*c2c66affSColin Finck const difference_type __elems_after =
631*c2c66affSColin Finck difference_type(__length) - __elems_before;
632*c2c66affSColin Finck __pos = this->_M_finish - __elems_after;
633*c2c66affSColin Finck _STLP_TRY {
634*c2c66affSColin Finck
635*c2c66affSColin Finck if (__elems_after > difference_type(__n)) {
636*c2c66affSColin Finck iterator __finish_n = this->_M_finish - difference_type(__n);
637*c2c66affSColin Finck _STLP_PRIV __ucopy(__finish_n, this->_M_finish, this->_M_finish);
638*c2c66affSColin Finck this->_M_finish = __new_finish;
639*c2c66affSColin Finck _STLP_STD::copy_backward(__pos, __finish_n, __old_finish);
640*c2c66affSColin Finck _STLP_STD::copy(__first, __last, __pos);
641*c2c66affSColin Finck }
642*c2c66affSColin Finck else {
643*c2c66affSColin Finck const value_type* __mid = __first + __elems_after;
644*c2c66affSColin Finck _STLP_PRIV __uninitialized_copy_copy(__mid, __last, __pos, this->_M_finish, this->_M_finish);
645*c2c66affSColin Finck this->_M_finish = __new_finish;
646*c2c66affSColin Finck _STLP_STD::copy(__first, __mid, __pos);
647*c2c66affSColin Finck }
648*c2c66affSColin Finck }
649*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1))
650*c2c66affSColin Finck }
651*c2c66affSColin Finck }
652*c2c66affSColin Finck
653*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_insert_range_aux(iterator __pos,const_iterator __first,const_iterator __last,size_type __n,const __true_type &)654*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_insert_range_aux(iterator __pos,
655*c2c66affSColin Finck const_iterator __first, const_iterator __last,
656*c2c66affSColin Finck size_type __n, const __true_type& /*_Movable*/) {
657*c2c66affSColin Finck const difference_type __elems_before = __pos - this->_M_start;
658*c2c66affSColin Finck size_type __length = size();
659*c2c66affSColin Finck if (__elems_before <= difference_type(__length / 2)) {
660*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
661*c2c66affSColin Finck __pos = this->_M_start + __elems_before;
662*c2c66affSColin Finck _STLP_TRY {
663*c2c66affSColin Finck iterator __dst = __new_start;
664*c2c66affSColin Finck iterator __src = this->_M_start;
665*c2c66affSColin Finck for (; __src != __pos; ++__dst, ++__src) {
666*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
667*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
668*c2c66affSColin Finck }
669*c2c66affSColin Finck this->_M_start = __new_start;
670*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, __dst);
671*c2c66affSColin Finck }
672*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
673*c2c66affSColin Finck }
674*c2c66affSColin Finck else {
675*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
676*c2c66affSColin Finck const difference_type __elems_after = difference_type(__length) - __elems_before;
677*c2c66affSColin Finck __pos = this->_M_finish - __elems_after;
678*c2c66affSColin Finck _STLP_TRY {
679*c2c66affSColin Finck iterator __dst = __new_finish;
680*c2c66affSColin Finck iterator __src = this->_M_finish;
681*c2c66affSColin Finck for (--__src, --__dst; __src >= __pos; --__src, --__dst) {
682*c2c66affSColin Finck _STLP_STD::_Move_Construct(&(*__dst), *__src);
683*c2c66affSColin Finck _STLP_STD::_Destroy_Moved(&(*__src));
684*c2c66affSColin Finck }
685*c2c66affSColin Finck this->_M_finish = __new_finish;
686*c2c66affSColin Finck _STLP_PRIV __ucopy(__first, __last, __pos);
687*c2c66affSColin Finck }
688*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1))
689*c2c66affSColin Finck }
690*c2c66affSColin Finck }
691*c2c66affSColin Finck
692*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_insert_range_aux(iterator __pos,const_iterator __first,const_iterator __last,size_type __n,const __false_type &)693*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_insert_range_aux(iterator __pos,
694*c2c66affSColin Finck const_iterator __first, const_iterator __last,
695*c2c66affSColin Finck size_type __n, const __false_type& /*_Movable*/) {
696*c2c66affSColin Finck const difference_type __elems_before = __pos - this->_M_start;
697*c2c66affSColin Finck size_type __length = size();
698*c2c66affSColin Finck if (__elems_before < difference_type(__length / 2)) {
699*c2c66affSColin Finck iterator __new_start = _M_reserve_elements_at_front(__n);
700*c2c66affSColin Finck iterator __old_start = this->_M_start;
701*c2c66affSColin Finck __pos = this->_M_start + __elems_before;
702*c2c66affSColin Finck _STLP_TRY {
703*c2c66affSColin Finck if (__elems_before >= difference_type(__n)) {
704*c2c66affSColin Finck iterator __start_n = this->_M_start + __n;
705*c2c66affSColin Finck _STLP_PRIV __ucopy(this->_M_start, __start_n, __new_start);
706*c2c66affSColin Finck this->_M_start = __new_start;
707*c2c66affSColin Finck _STLP_STD::copy(__start_n, __pos, __old_start);
708*c2c66affSColin Finck _STLP_STD::copy(__first, __last, __pos - difference_type(__n));
709*c2c66affSColin Finck }
710*c2c66affSColin Finck else {
711*c2c66affSColin Finck const_iterator __mid = __first + (__n - __elems_before);
712*c2c66affSColin Finck _STLP_PRIV __uninitialized_copy_copy(this->_M_start, __pos, __first, __mid, __new_start);
713*c2c66affSColin Finck this->_M_start = __new_start;
714*c2c66affSColin Finck _STLP_STD::copy(__mid, __last, __old_start);
715*c2c66affSColin Finck }
716*c2c66affSColin Finck }
717*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(__new_start._M_node, this->_M_start._M_node))
718*c2c66affSColin Finck }
719*c2c66affSColin Finck else {
720*c2c66affSColin Finck iterator __new_finish = _M_reserve_elements_at_back(__n);
721*c2c66affSColin Finck iterator __old_finish = this->_M_finish;
722*c2c66affSColin Finck const difference_type __elems_after = __length - __elems_before;
723*c2c66affSColin Finck __pos = this->_M_finish - __elems_after;
724*c2c66affSColin Finck _STLP_TRY {
725*c2c66affSColin Finck if (__elems_after > difference_type(__n)) {
726*c2c66affSColin Finck iterator __finish_n = this->_M_finish - difference_type(__n);
727*c2c66affSColin Finck _STLP_PRIV __ucopy(__finish_n, this->_M_finish, this->_M_finish);
728*c2c66affSColin Finck this->_M_finish = __new_finish;
729*c2c66affSColin Finck _STLP_STD::copy_backward(__pos, __finish_n, __old_finish);
730*c2c66affSColin Finck _STLP_STD::copy(__first, __last, __pos);
731*c2c66affSColin Finck }
732*c2c66affSColin Finck else {
733*c2c66affSColin Finck const_iterator __mid = __first + __elems_after;
734*c2c66affSColin Finck _STLP_PRIV __uninitialized_copy_copy(__mid, __last, __pos, this->_M_finish, this->_M_finish);
735*c2c66affSColin Finck this->_M_finish = __new_finish;
736*c2c66affSColin Finck _STLP_STD::copy(__first, __mid, __pos);
737*c2c66affSColin Finck }
738*c2c66affSColin Finck }
739*c2c66affSColin Finck _STLP_UNWIND(this->_M_destroy_nodes(this->_M_finish._M_node + 1, __new_finish._M_node + 1))
740*c2c66affSColin Finck }
741*c2c66affSColin Finck }
742*c2c66affSColin Finck #endif /* _STLP_MEMBER_TEMPLATES */
743*c2c66affSColin Finck
744*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_new_elements_at_front(size_type __new_elems)745*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_new_elements_at_front(size_type __new_elems) {
746*c2c66affSColin Finck size_type __new_nodes
747*c2c66affSColin Finck = (__new_elems + this->buffer_size() - 1) / this->buffer_size();
748*c2c66affSColin Finck _M_reserve_map_at_front(__new_nodes);
749*c2c66affSColin Finck size_type __i = 1;
750*c2c66affSColin Finck _STLP_TRY {
751*c2c66affSColin Finck for (; __i <= __new_nodes; ++__i)
752*c2c66affSColin Finck *(this->_M_start._M_node - __i) = this->_M_map_size.allocate(this->buffer_size());
753*c2c66affSColin Finck }
754*c2c66affSColin Finck _STLP_UNWIND(for (size_type __j = 1; __j < __i; ++__j)
755*c2c66affSColin Finck this->_M_map_size.deallocate(*(this->_M_start._M_node - __j), this->buffer_size()))
756*c2c66affSColin Finck }
757*c2c66affSColin Finck
758*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_new_elements_at_back(size_type __new_elems)759*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_new_elements_at_back(size_type __new_elems) {
760*c2c66affSColin Finck size_type __new_nodes
761*c2c66affSColin Finck = (__new_elems + this->buffer_size() - 1) / this->buffer_size();
762*c2c66affSColin Finck _M_reserve_map_at_back(__new_nodes);
763*c2c66affSColin Finck size_type __i = 1;
764*c2c66affSColin Finck _STLP_TRY {
765*c2c66affSColin Finck for (; __i <= __new_nodes; ++__i)
766*c2c66affSColin Finck *(this->_M_finish._M_node + __i) = this->_M_map_size.allocate(this->buffer_size());
767*c2c66affSColin Finck }
768*c2c66affSColin Finck _STLP_UNWIND(for (size_type __j = 1; __j < __i; ++__j)
769*c2c66affSColin Finck this->_M_map_size.deallocate(*(this->_M_finish._M_node + __j), this->buffer_size()))
770*c2c66affSColin Finck }
771*c2c66affSColin Finck
772*c2c66affSColin Finck template <class _Tp, class _Alloc >
_M_reallocate_map(size_type __nodes_to_add,bool __add_at_front)773*c2c66affSColin Finck void deque<_Tp,_Alloc>::_M_reallocate_map(size_type __nodes_to_add,
774*c2c66affSColin Finck bool __add_at_front) {
775*c2c66affSColin Finck size_type __old_num_nodes = this->_M_finish._M_node - this->_M_start._M_node + 1;
776*c2c66affSColin Finck size_type __new_num_nodes = __old_num_nodes + __nodes_to_add;
777*c2c66affSColin Finck
778*c2c66affSColin Finck _Map_pointer __new_nstart;
779*c2c66affSColin Finck if (this->_M_map_size._M_data > 2 * __new_num_nodes) {
780*c2c66affSColin Finck __new_nstart = this->_M_map._M_data + (this->_M_map_size._M_data - __new_num_nodes) / 2
781*c2c66affSColin Finck + (__add_at_front ? __nodes_to_add : 0);
782*c2c66affSColin Finck if (__new_nstart < this->_M_start._M_node)
783*c2c66affSColin Finck _STLP_STD::copy(this->_M_start._M_node, this->_M_finish._M_node + 1, __new_nstart);
784*c2c66affSColin Finck else
785*c2c66affSColin Finck _STLP_STD::copy_backward(this->_M_start._M_node, this->_M_finish._M_node + 1,
786*c2c66affSColin Finck __new_nstart + __old_num_nodes);
787*c2c66affSColin Finck }
788*c2c66affSColin Finck else {
789*c2c66affSColin Finck size_type __new_map_size =
790*c2c66affSColin Finck this->_M_map_size._M_data + (max)((size_t)this->_M_map_size._M_data, __nodes_to_add) + 2;
791*c2c66affSColin Finck
792*c2c66affSColin Finck _Map_pointer __new_map = this->_M_map.allocate(__new_map_size);
793*c2c66affSColin Finck __new_nstart = __new_map + (__new_map_size - __new_num_nodes) / 2
794*c2c66affSColin Finck + (__add_at_front ? __nodes_to_add : 0);
795*c2c66affSColin Finck _STLP_STD::copy(this->_M_start._M_node, this->_M_finish._M_node + 1, __new_nstart);
796*c2c66affSColin Finck this->_M_map.deallocate(this->_M_map._M_data, this->_M_map_size._M_data);
797*c2c66affSColin Finck
798*c2c66affSColin Finck this->_M_map._M_data = __new_map;
799*c2c66affSColin Finck this->_M_map_size._M_data = __new_map_size;
800*c2c66affSColin Finck }
801*c2c66affSColin Finck
802*c2c66affSColin Finck this->_M_start._M_set_node(__new_nstart);
803*c2c66affSColin Finck this->_M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
804*c2c66affSColin Finck }
805*c2c66affSColin Finck
806*c2c66affSColin Finck #if defined (deque)
807*c2c66affSColin Finck # undef deque
808*c2c66affSColin Finck _STLP_MOVE_TO_STD_NAMESPACE
809*c2c66affSColin Finck #endif
810*c2c66affSColin Finck
811*c2c66affSColin Finck _STLP_END_NAMESPACE
812*c2c66affSColin Finck
813*c2c66affSColin Finck #undef __iterator__
814*c2c66affSColin Finck #undef iterator
815*c2c66affSColin Finck #undef const_iterator
816*c2c66affSColin Finck #undef size_type
817*c2c66affSColin Finck #undef value_type
818*c2c66affSColin Finck
819*c2c66affSColin Finck #endif /* _STLP_DEQUE_C */
820*c2c66affSColin Finck
821*c2c66affSColin Finck // Local Variables:
822*c2c66affSColin Finck // mode:C++
823*c2c66affSColin Finck // End:
824