1*404b540aSrobert // Deque implementation -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert /*
32*404b540aSrobert *
33*404b540aSrobert * Copyright (c) 1994
34*404b540aSrobert * Hewlett-Packard Company
35*404b540aSrobert *
36*404b540aSrobert * Permission to use, copy, modify, distribute and sell this software
37*404b540aSrobert * and its documentation for any purpose is hereby granted without fee,
38*404b540aSrobert * provided that the above copyright notice appear in all copies and
39*404b540aSrobert * that both that copyright notice and this permission notice appear
40*404b540aSrobert * in supporting documentation. Hewlett-Packard Company makes no
41*404b540aSrobert * representations about the suitability of this software for any
42*404b540aSrobert * purpose. It is provided "as is" without express or implied warranty.
43*404b540aSrobert *
44*404b540aSrobert *
45*404b540aSrobert * Copyright (c) 1997
46*404b540aSrobert * Silicon Graphics Computer Systems, Inc.
47*404b540aSrobert *
48*404b540aSrobert * Permission to use, copy, modify, distribute and sell this software
49*404b540aSrobert * and its documentation for any purpose is hereby granted without fee,
50*404b540aSrobert * provided that the above copyright notice appear in all copies and
51*404b540aSrobert * that both that copyright notice and this permission notice appear
52*404b540aSrobert * in supporting documentation. Silicon Graphics makes no
53*404b540aSrobert * representations about the suitability of this software for any
54*404b540aSrobert * purpose. It is provided "as is" without express or implied warranty.
55*404b540aSrobert */
56*404b540aSrobert
57*404b540aSrobert /** @file stl_deque.h
58*404b540aSrobert * This is an internal header file, included by other library headers.
59*404b540aSrobert * You should not attempt to use it directly.
60*404b540aSrobert */
61*404b540aSrobert
62*404b540aSrobert #ifndef _DEQUE_H
63*404b540aSrobert #define _DEQUE_H 1
64*404b540aSrobert
65*404b540aSrobert #include <bits/concept_check.h>
66*404b540aSrobert #include <bits/stl_iterator_base_types.h>
67*404b540aSrobert #include <bits/stl_iterator_base_funcs.h>
68*404b540aSrobert
_GLIBCXX_BEGIN_NESTED_NAMESPACE(std,_GLIBCXX_STD)69*404b540aSrobert _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
70*404b540aSrobert
71*404b540aSrobert /**
72*404b540aSrobert * @if maint
73*404b540aSrobert * @brief This function controls the size of memory nodes.
74*404b540aSrobert * @param size The size of an element.
75*404b540aSrobert * @return The number (not byte size) of elements per node.
76*404b540aSrobert *
77*404b540aSrobert * This function started off as a compiler kludge from SGI, but seems to
78*404b540aSrobert * be a useful wrapper around a repeated constant expression. The '512' is
79*404b540aSrobert * tuneable (and no other code needs to change), but no investigation has
80*404b540aSrobert * been done since inheriting the SGI code.
81*404b540aSrobert * @endif
82*404b540aSrobert */
83*404b540aSrobert inline size_t
84*404b540aSrobert __deque_buf_size(size_t __size)
85*404b540aSrobert { return __size < 512 ? size_t(512 / __size) : size_t(1); }
86*404b540aSrobert
87*404b540aSrobert
88*404b540aSrobert /**
89*404b540aSrobert * @brief A deque::iterator.
90*404b540aSrobert *
91*404b540aSrobert * Quite a bit of intelligence here. Much of the functionality of
92*404b540aSrobert * deque is actually passed off to this class. A deque holds two
93*404b540aSrobert * of these internally, marking its valid range. Access to
94*404b540aSrobert * elements is done as offsets of either of those two, relying on
95*404b540aSrobert * operator overloading in this class.
96*404b540aSrobert *
97*404b540aSrobert * @if maint
98*404b540aSrobert * All the functions are op overloads except for _M_set_node.
99*404b540aSrobert * @endif
100*404b540aSrobert */
101*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
102*404b540aSrobert struct _Deque_iterator
103*404b540aSrobert {
104*404b540aSrobert typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
105*404b540aSrobert typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
106*404b540aSrobert
_S_buffer_size_Deque_iterator107*404b540aSrobert static size_t _S_buffer_size()
108*404b540aSrobert { return __deque_buf_size(sizeof(_Tp)); }
109*404b540aSrobert
110*404b540aSrobert typedef std::random_access_iterator_tag iterator_category;
111*404b540aSrobert typedef _Tp value_type;
112*404b540aSrobert typedef _Ptr pointer;
113*404b540aSrobert typedef _Ref reference;
114*404b540aSrobert typedef size_t size_type;
115*404b540aSrobert typedef ptrdiff_t difference_type;
116*404b540aSrobert typedef _Tp** _Map_pointer;
117*404b540aSrobert typedef _Deque_iterator _Self;
118*404b540aSrobert
119*404b540aSrobert _Tp* _M_cur;
120*404b540aSrobert _Tp* _M_first;
121*404b540aSrobert _Tp* _M_last;
122*404b540aSrobert _Map_pointer _M_node;
123*404b540aSrobert
_Deque_iterator_Deque_iterator124*404b540aSrobert _Deque_iterator(_Tp* __x, _Map_pointer __y)
125*404b540aSrobert : _M_cur(__x), _M_first(*__y),
126*404b540aSrobert _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
127*404b540aSrobert
_Deque_iterator_Deque_iterator128*404b540aSrobert _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
129*404b540aSrobert
_Deque_iterator_Deque_iterator130*404b540aSrobert _Deque_iterator(const iterator& __x)
131*404b540aSrobert : _M_cur(__x._M_cur), _M_first(__x._M_first),
132*404b540aSrobert _M_last(__x._M_last), _M_node(__x._M_node) {}
133*404b540aSrobert
134*404b540aSrobert reference
135*404b540aSrobert operator*() const
136*404b540aSrobert { return *_M_cur; }
137*404b540aSrobert
138*404b540aSrobert pointer
139*404b540aSrobert operator->() const
140*404b540aSrobert { return _M_cur; }
141*404b540aSrobert
142*404b540aSrobert _Self&
143*404b540aSrobert operator++()
144*404b540aSrobert {
145*404b540aSrobert ++_M_cur;
146*404b540aSrobert if (_M_cur == _M_last)
147*404b540aSrobert {
148*404b540aSrobert _M_set_node(_M_node + 1);
149*404b540aSrobert _M_cur = _M_first;
150*404b540aSrobert }
151*404b540aSrobert return *this;
152*404b540aSrobert }
153*404b540aSrobert
154*404b540aSrobert _Self
155*404b540aSrobert operator++(int)
156*404b540aSrobert {
157*404b540aSrobert _Self __tmp = *this;
158*404b540aSrobert ++*this;
159*404b540aSrobert return __tmp;
160*404b540aSrobert }
161*404b540aSrobert
162*404b540aSrobert _Self&
163*404b540aSrobert operator--()
164*404b540aSrobert {
165*404b540aSrobert if (_M_cur == _M_first)
166*404b540aSrobert {
167*404b540aSrobert _M_set_node(_M_node - 1);
168*404b540aSrobert _M_cur = _M_last;
169*404b540aSrobert }
170*404b540aSrobert --_M_cur;
171*404b540aSrobert return *this;
172*404b540aSrobert }
173*404b540aSrobert
174*404b540aSrobert _Self
175*404b540aSrobert operator--(int)
176*404b540aSrobert {
177*404b540aSrobert _Self __tmp = *this;
178*404b540aSrobert --*this;
179*404b540aSrobert return __tmp;
180*404b540aSrobert }
181*404b540aSrobert
182*404b540aSrobert _Self&
183*404b540aSrobert operator+=(difference_type __n)
184*404b540aSrobert {
185*404b540aSrobert const difference_type __offset = __n + (_M_cur - _M_first);
186*404b540aSrobert if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
187*404b540aSrobert _M_cur += __n;
188*404b540aSrobert else
189*404b540aSrobert {
190*404b540aSrobert const difference_type __node_offset =
191*404b540aSrobert __offset > 0 ? __offset / difference_type(_S_buffer_size())
192*404b540aSrobert : -difference_type((-__offset - 1)
193*404b540aSrobert / _S_buffer_size()) - 1;
194*404b540aSrobert _M_set_node(_M_node + __node_offset);
195*404b540aSrobert _M_cur = _M_first + (__offset - __node_offset
196*404b540aSrobert * difference_type(_S_buffer_size()));
197*404b540aSrobert }
198*404b540aSrobert return *this;
199*404b540aSrobert }
200*404b540aSrobert
201*404b540aSrobert _Self
202*404b540aSrobert operator+(difference_type __n) const
203*404b540aSrobert {
204*404b540aSrobert _Self __tmp = *this;
205*404b540aSrobert return __tmp += __n;
206*404b540aSrobert }
207*404b540aSrobert
208*404b540aSrobert _Self&
209*404b540aSrobert operator-=(difference_type __n)
210*404b540aSrobert { return *this += -__n; }
211*404b540aSrobert
212*404b540aSrobert _Self
213*404b540aSrobert operator-(difference_type __n) const
214*404b540aSrobert {
215*404b540aSrobert _Self __tmp = *this;
216*404b540aSrobert return __tmp -= __n;
217*404b540aSrobert }
218*404b540aSrobert
219*404b540aSrobert reference
220*404b540aSrobert operator[](difference_type __n) const
221*404b540aSrobert { return *(*this + __n); }
222*404b540aSrobert
223*404b540aSrobert /** @if maint
224*404b540aSrobert * Prepares to traverse new_node. Sets everything except
225*404b540aSrobert * _M_cur, which should therefore be set by the caller
226*404b540aSrobert * immediately afterwards, based on _M_first and _M_last.
227*404b540aSrobert * @endif
228*404b540aSrobert */
229*404b540aSrobert void
_M_set_node_Deque_iterator230*404b540aSrobert _M_set_node(_Map_pointer __new_node)
231*404b540aSrobert {
232*404b540aSrobert _M_node = __new_node;
233*404b540aSrobert _M_first = *__new_node;
234*404b540aSrobert _M_last = _M_first + difference_type(_S_buffer_size());
235*404b540aSrobert }
236*404b540aSrobert };
237*404b540aSrobert
238*404b540aSrobert // Note: we also provide overloads whose operands are of the same type in
239*404b540aSrobert // order to avoid ambiguous overload resolution when std::rel_ops operators
240*404b540aSrobert // are in scope (for additional details, see libstdc++/3628)
241*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
242*404b540aSrobert inline bool
243*404b540aSrobert operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
244*404b540aSrobert const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
245*404b540aSrobert { return __x._M_cur == __y._M_cur; }
246*404b540aSrobert
247*404b540aSrobert template<typename _Tp, typename _RefL, typename _PtrL,
248*404b540aSrobert typename _RefR, typename _PtrR>
249*404b540aSrobert inline bool
250*404b540aSrobert operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
251*404b540aSrobert const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
252*404b540aSrobert { return __x._M_cur == __y._M_cur; }
253*404b540aSrobert
254*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
255*404b540aSrobert inline bool
256*404b540aSrobert operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
257*404b540aSrobert const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
258*404b540aSrobert { return !(__x == __y); }
259*404b540aSrobert
260*404b540aSrobert template<typename _Tp, typename _RefL, typename _PtrL,
261*404b540aSrobert typename _RefR, typename _PtrR>
262*404b540aSrobert inline bool
263*404b540aSrobert operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
264*404b540aSrobert const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
265*404b540aSrobert { return !(__x == __y); }
266*404b540aSrobert
267*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
268*404b540aSrobert inline bool
269*404b540aSrobert operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
270*404b540aSrobert const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
271*404b540aSrobert { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
272*404b540aSrobert : (__x._M_node < __y._M_node); }
273*404b540aSrobert
274*404b540aSrobert template<typename _Tp, typename _RefL, typename _PtrL,
275*404b540aSrobert typename _RefR, typename _PtrR>
276*404b540aSrobert inline bool
277*404b540aSrobert operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
278*404b540aSrobert const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
279*404b540aSrobert { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
280*404b540aSrobert : (__x._M_node < __y._M_node); }
281*404b540aSrobert
282*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
283*404b540aSrobert inline bool
284*404b540aSrobert operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
285*404b540aSrobert const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
286*404b540aSrobert { return __y < __x; }
287*404b540aSrobert
288*404b540aSrobert template<typename _Tp, typename _RefL, typename _PtrL,
289*404b540aSrobert typename _RefR, typename _PtrR>
290*404b540aSrobert inline bool
291*404b540aSrobert operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
292*404b540aSrobert const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
293*404b540aSrobert { return __y < __x; }
294*404b540aSrobert
295*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
296*404b540aSrobert inline bool
297*404b540aSrobert operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
298*404b540aSrobert const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
299*404b540aSrobert { return !(__y < __x); }
300*404b540aSrobert
301*404b540aSrobert template<typename _Tp, typename _RefL, typename _PtrL,
302*404b540aSrobert typename _RefR, typename _PtrR>
303*404b540aSrobert inline bool
304*404b540aSrobert operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
305*404b540aSrobert const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
306*404b540aSrobert { return !(__y < __x); }
307*404b540aSrobert
308*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
309*404b540aSrobert inline bool
310*404b540aSrobert operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
311*404b540aSrobert const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
312*404b540aSrobert { return !(__x < __y); }
313*404b540aSrobert
314*404b540aSrobert template<typename _Tp, typename _RefL, typename _PtrL,
315*404b540aSrobert typename _RefR, typename _PtrR>
316*404b540aSrobert inline bool
317*404b540aSrobert operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
318*404b540aSrobert const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
319*404b540aSrobert { return !(__x < __y); }
320*404b540aSrobert
321*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
322*404b540aSrobert // According to the resolution of DR179 not only the various comparison
323*404b540aSrobert // operators but also operator- must accept mixed iterator/const_iterator
324*404b540aSrobert // parameters.
325*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
326*404b540aSrobert inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
327*404b540aSrobert operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
328*404b540aSrobert const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
329*404b540aSrobert {
330*404b540aSrobert return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
331*404b540aSrobert (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
332*404b540aSrobert * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
333*404b540aSrobert + (__y._M_last - __y._M_cur);
334*404b540aSrobert }
335*404b540aSrobert
336*404b540aSrobert template<typename _Tp, typename _RefL, typename _PtrL,
337*404b540aSrobert typename _RefR, typename _PtrR>
338*404b540aSrobert inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
339*404b540aSrobert operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
340*404b540aSrobert const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
341*404b540aSrobert {
342*404b540aSrobert return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
343*404b540aSrobert (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
344*404b540aSrobert * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
345*404b540aSrobert + (__y._M_last - __y._M_cur);
346*404b540aSrobert }
347*404b540aSrobert
348*404b540aSrobert template<typename _Tp, typename _Ref, typename _Ptr>
349*404b540aSrobert inline _Deque_iterator<_Tp, _Ref, _Ptr>
350*404b540aSrobert operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
351*404b540aSrobert { return __x + __n; }
352*404b540aSrobert
353*404b540aSrobert template<typename _Tp>
354*404b540aSrobert void
355*404b540aSrobert fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
356*404b540aSrobert const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value);
357*404b540aSrobert
358*404b540aSrobert /**
359*404b540aSrobert * @if maint
360*404b540aSrobert * Deque base class. This class provides the unified face for %deque's
361*404b540aSrobert * allocation. This class's constructor and destructor allocate and
362*404b540aSrobert * deallocate (but do not initialize) storage. This makes %exception
363*404b540aSrobert * safety easier.
364*404b540aSrobert *
365*404b540aSrobert * Nothing in this class ever constructs or destroys an actual Tp element.
366*404b540aSrobert * (Deque handles that itself.) Only/All memory management is performed
367*404b540aSrobert * here.
368*404b540aSrobert * @endif
369*404b540aSrobert */
370*404b540aSrobert template<typename _Tp, typename _Alloc>
371*404b540aSrobert class _Deque_base
372*404b540aSrobert {
373*404b540aSrobert public:
374*404b540aSrobert typedef _Alloc allocator_type;
375*404b540aSrobert
376*404b540aSrobert allocator_type
get_allocator()377*404b540aSrobert get_allocator() const
378*404b540aSrobert { return allocator_type(_M_get_Tp_allocator()); }
379*404b540aSrobert
380*404b540aSrobert typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
381*404b540aSrobert typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
382*404b540aSrobert
_Deque_base(const allocator_type & __a,size_t __num_elements)383*404b540aSrobert _Deque_base(const allocator_type& __a, size_t __num_elements)
384*404b540aSrobert : _M_impl(__a)
385*404b540aSrobert { _M_initialize_map(__num_elements); }
386*404b540aSrobert
_Deque_base(const allocator_type & __a)387*404b540aSrobert _Deque_base(const allocator_type& __a)
388*404b540aSrobert : _M_impl(__a)
389*404b540aSrobert { }
390*404b540aSrobert
391*404b540aSrobert ~_Deque_base();
392*404b540aSrobert
393*404b540aSrobert protected:
394*404b540aSrobert //This struct encapsulates the implementation of the std::deque
395*404b540aSrobert //standard container and at the same time makes use of the EBO
396*404b540aSrobert //for empty allocators.
397*404b540aSrobert typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
398*404b540aSrobert
399*404b540aSrobert typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
400*404b540aSrobert
401*404b540aSrobert struct _Deque_impl
402*404b540aSrobert : public _Tp_alloc_type
403*404b540aSrobert {
404*404b540aSrobert _Tp** _M_map;
405*404b540aSrobert size_t _M_map_size;
406*404b540aSrobert iterator _M_start;
407*404b540aSrobert iterator _M_finish;
408*404b540aSrobert
_Deque_impl_Deque_impl409*404b540aSrobert _Deque_impl(const _Tp_alloc_type& __a)
410*404b540aSrobert : _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
411*404b540aSrobert _M_start(), _M_finish()
412*404b540aSrobert { }
413*404b540aSrobert };
414*404b540aSrobert
415*404b540aSrobert _Tp_alloc_type&
_M_get_Tp_allocator()416*404b540aSrobert _M_get_Tp_allocator()
417*404b540aSrobert { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
418*404b540aSrobert
419*404b540aSrobert const _Tp_alloc_type&
_M_get_Tp_allocator()420*404b540aSrobert _M_get_Tp_allocator() const
421*404b540aSrobert { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
422*404b540aSrobert
423*404b540aSrobert _Map_alloc_type
_M_get_map_allocator()424*404b540aSrobert _M_get_map_allocator() const
425*404b540aSrobert { return _Map_alloc_type(_M_get_Tp_allocator()); }
426*404b540aSrobert
427*404b540aSrobert _Tp*
_M_allocate_node()428*404b540aSrobert _M_allocate_node()
429*404b540aSrobert {
430*404b540aSrobert return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
431*404b540aSrobert }
432*404b540aSrobert
433*404b540aSrobert void
_M_deallocate_node(_Tp * __p)434*404b540aSrobert _M_deallocate_node(_Tp* __p)
435*404b540aSrobert {
436*404b540aSrobert _M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
437*404b540aSrobert }
438*404b540aSrobert
439*404b540aSrobert _Tp**
_M_allocate_map(size_t __n)440*404b540aSrobert _M_allocate_map(size_t __n)
441*404b540aSrobert { return _M_get_map_allocator().allocate(__n); }
442*404b540aSrobert
443*404b540aSrobert void
_M_deallocate_map(_Tp ** __p,size_t __n)444*404b540aSrobert _M_deallocate_map(_Tp** __p, size_t __n)
445*404b540aSrobert { _M_get_map_allocator().deallocate(__p, __n); }
446*404b540aSrobert
447*404b540aSrobert protected:
448*404b540aSrobert void _M_initialize_map(size_t);
449*404b540aSrobert void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
450*404b540aSrobert void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
451*404b540aSrobert enum { _S_initial_map_size = 8 };
452*404b540aSrobert
453*404b540aSrobert _Deque_impl _M_impl;
454*404b540aSrobert };
455*404b540aSrobert
456*404b540aSrobert template<typename _Tp, typename _Alloc>
457*404b540aSrobert _Deque_base<_Tp, _Alloc>::
~_Deque_base()458*404b540aSrobert ~_Deque_base()
459*404b540aSrobert {
460*404b540aSrobert if (this->_M_impl._M_map)
461*404b540aSrobert {
462*404b540aSrobert _M_destroy_nodes(this->_M_impl._M_start._M_node,
463*404b540aSrobert this->_M_impl._M_finish._M_node + 1);
464*404b540aSrobert _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
465*404b540aSrobert }
466*404b540aSrobert }
467*404b540aSrobert
468*404b540aSrobert /**
469*404b540aSrobert * @if maint
470*404b540aSrobert * @brief Layout storage.
471*404b540aSrobert * @param num_elements The count of T's for which to allocate space
472*404b540aSrobert * at first.
473*404b540aSrobert * @return Nothing.
474*404b540aSrobert *
475*404b540aSrobert * The initial underlying memory layout is a bit complicated...
476*404b540aSrobert * @endif
477*404b540aSrobert */
478*404b540aSrobert template<typename _Tp, typename _Alloc>
479*404b540aSrobert void
480*404b540aSrobert _Deque_base<_Tp, _Alloc>::
_M_initialize_map(size_t __num_elements)481*404b540aSrobert _M_initialize_map(size_t __num_elements)
482*404b540aSrobert {
483*404b540aSrobert const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
484*404b540aSrobert + 1);
485*404b540aSrobert
486*404b540aSrobert this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
487*404b540aSrobert size_t(__num_nodes + 2));
488*404b540aSrobert this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
489*404b540aSrobert
490*404b540aSrobert // For "small" maps (needing less than _M_map_size nodes), allocation
491*404b540aSrobert // starts in the middle elements and grows outwards. So nstart may be
492*404b540aSrobert // the beginning of _M_map, but for small maps it may be as far in as
493*404b540aSrobert // _M_map+3.
494*404b540aSrobert
495*404b540aSrobert _Tp** __nstart = (this->_M_impl._M_map
496*404b540aSrobert + (this->_M_impl._M_map_size - __num_nodes) / 2);
497*404b540aSrobert _Tp** __nfinish = __nstart + __num_nodes;
498*404b540aSrobert
499*404b540aSrobert try
500*404b540aSrobert { _M_create_nodes(__nstart, __nfinish); }
501*404b540aSrobert catch(...)
502*404b540aSrobert {
503*404b540aSrobert _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
504*404b540aSrobert this->_M_impl._M_map = 0;
505*404b540aSrobert this->_M_impl._M_map_size = 0;
506*404b540aSrobert __throw_exception_again;
507*404b540aSrobert }
508*404b540aSrobert
509*404b540aSrobert this->_M_impl._M_start._M_set_node(__nstart);
510*404b540aSrobert this->_M_impl._M_finish._M_set_node(__nfinish - 1);
511*404b540aSrobert this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
512*404b540aSrobert this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
513*404b540aSrobert + __num_elements
514*404b540aSrobert % __deque_buf_size(sizeof(_Tp)));
515*404b540aSrobert }
516*404b540aSrobert
517*404b540aSrobert template<typename _Tp, typename _Alloc>
518*404b540aSrobert void
519*404b540aSrobert _Deque_base<_Tp, _Alloc>::
_M_create_nodes(_Tp ** __nstart,_Tp ** __nfinish)520*404b540aSrobert _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
521*404b540aSrobert {
522*404b540aSrobert _Tp** __cur;
523*404b540aSrobert try
524*404b540aSrobert {
525*404b540aSrobert for (__cur = __nstart; __cur < __nfinish; ++__cur)
526*404b540aSrobert *__cur = this->_M_allocate_node();
527*404b540aSrobert }
528*404b540aSrobert catch(...)
529*404b540aSrobert {
530*404b540aSrobert _M_destroy_nodes(__nstart, __cur);
531*404b540aSrobert __throw_exception_again;
532*404b540aSrobert }
533*404b540aSrobert }
534*404b540aSrobert
535*404b540aSrobert template<typename _Tp, typename _Alloc>
536*404b540aSrobert void
537*404b540aSrobert _Deque_base<_Tp, _Alloc>::
_M_destroy_nodes(_Tp ** __nstart,_Tp ** __nfinish)538*404b540aSrobert _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
539*404b540aSrobert {
540*404b540aSrobert for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
541*404b540aSrobert _M_deallocate_node(*__n);
542*404b540aSrobert }
543*404b540aSrobert
544*404b540aSrobert /**
545*404b540aSrobert * @brief A standard container using fixed-size memory allocation and
546*404b540aSrobert * constant-time manipulation of elements at either end.
547*404b540aSrobert *
548*404b540aSrobert * @ingroup Containers
549*404b540aSrobert * @ingroup Sequences
550*404b540aSrobert *
551*404b540aSrobert * Meets the requirements of a <a href="tables.html#65">container</a>, a
552*404b540aSrobert * <a href="tables.html#66">reversible container</a>, and a
553*404b540aSrobert * <a href="tables.html#67">sequence</a>, including the
554*404b540aSrobert * <a href="tables.html#68">optional sequence requirements</a>.
555*404b540aSrobert *
556*404b540aSrobert * In previous HP/SGI versions of deque, there was an extra template
557*404b540aSrobert * parameter so users could control the node size. This extension turned
558*404b540aSrobert * out to violate the C++ standard (it can be detected using template
559*404b540aSrobert * template parameters), and it was removed.
560*404b540aSrobert *
561*404b540aSrobert * @if maint
562*404b540aSrobert * Here's how a deque<Tp> manages memory. Each deque has 4 members:
563*404b540aSrobert *
564*404b540aSrobert * - Tp** _M_map
565*404b540aSrobert * - size_t _M_map_size
566*404b540aSrobert * - iterator _M_start, _M_finish
567*404b540aSrobert *
568*404b540aSrobert * map_size is at least 8. %map is an array of map_size
569*404b540aSrobert * pointers-to-"nodes". (The name %map has nothing to do with the
570*404b540aSrobert * std::map class, and "nodes" should not be confused with
571*404b540aSrobert * std::list's usage of "node".)
572*404b540aSrobert *
573*404b540aSrobert * A "node" has no specific type name as such, but it is referred
574*404b540aSrobert * to as "node" in this file. It is a simple array-of-Tp. If Tp
575*404b540aSrobert * is very large, there will be one Tp element per node (i.e., an
576*404b540aSrobert * "array" of one). For non-huge Tp's, node size is inversely
577*404b540aSrobert * related to Tp size: the larger the Tp, the fewer Tp's will fit
578*404b540aSrobert * in a node. The goal here is to keep the total size of a node
579*404b540aSrobert * relatively small and constant over different Tp's, to improve
580*404b540aSrobert * allocator efficiency.
581*404b540aSrobert *
582*404b540aSrobert * Not every pointer in the %map array will point to a node. If
583*404b540aSrobert * the initial number of elements in the deque is small, the
584*404b540aSrobert * /middle/ %map pointers will be valid, and the ones at the edges
585*404b540aSrobert * will be unused. This same situation will arise as the %map
586*404b540aSrobert * grows: available %map pointers, if any, will be on the ends. As
587*404b540aSrobert * new nodes are created, only a subset of the %map's pointers need
588*404b540aSrobert * to be copied "outward".
589*404b540aSrobert *
590*404b540aSrobert * Class invariants:
591*404b540aSrobert * - For any nonsingular iterator i:
592*404b540aSrobert * - i.node points to a member of the %map array. (Yes, you read that
593*404b540aSrobert * correctly: i.node does not actually point to a node.) The member of
594*404b540aSrobert * the %map array is what actually points to the node.
595*404b540aSrobert * - i.first == *(i.node) (This points to the node (first Tp element).)
596*404b540aSrobert * - i.last == i.first + node_size
597*404b540aSrobert * - i.cur is a pointer in the range [i.first, i.last). NOTE:
598*404b540aSrobert * the implication of this is that i.cur is always a dereferenceable
599*404b540aSrobert * pointer, even if i is a past-the-end iterator.
600*404b540aSrobert * - Start and Finish are always nonsingular iterators. NOTE: this
601*404b540aSrobert * means that an empty deque must have one node, a deque with <N
602*404b540aSrobert * elements (where N is the node buffer size) must have one node, a
603*404b540aSrobert * deque with N through (2N-1) elements must have two nodes, etc.
604*404b540aSrobert * - For every node other than start.node and finish.node, every
605*404b540aSrobert * element in the node is an initialized object. If start.node ==
606*404b540aSrobert * finish.node, then [start.cur, finish.cur) are initialized
607*404b540aSrobert * objects, and the elements outside that range are uninitialized
608*404b540aSrobert * storage. Otherwise, [start.cur, start.last) and [finish.first,
609*404b540aSrobert * finish.cur) are initialized objects, and [start.first, start.cur)
610*404b540aSrobert * and [finish.cur, finish.last) are uninitialized storage.
611*404b540aSrobert * - [%map, %map + map_size) is a valid, non-empty range.
612*404b540aSrobert * - [start.node, finish.node] is a valid range contained within
613*404b540aSrobert * [%map, %map + map_size).
614*404b540aSrobert * - A pointer in the range [%map, %map + map_size) points to an allocated
615*404b540aSrobert * node if and only if the pointer is in the range
616*404b540aSrobert * [start.node, finish.node].
617*404b540aSrobert *
618*404b540aSrobert * Here's the magic: nothing in deque is "aware" of the discontiguous
619*404b540aSrobert * storage!
620*404b540aSrobert *
621*404b540aSrobert * The memory setup and layout occurs in the parent, _Base, and the iterator
622*404b540aSrobert * class is entirely responsible for "leaping" from one node to the next.
623*404b540aSrobert * All the implementation routines for deque itself work only through the
624*404b540aSrobert * start and finish iterators. This keeps the routines simple and sane,
625*404b540aSrobert * and we can use other standard algorithms as well.
626*404b540aSrobert * @endif
627*404b540aSrobert */
628*404b540aSrobert template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
629*404b540aSrobert class deque : protected _Deque_base<_Tp, _Alloc>
630*404b540aSrobert {
631*404b540aSrobert // concept requirements
632*404b540aSrobert typedef typename _Alloc::value_type _Alloc_value_type;
633*404b540aSrobert __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
634*404b540aSrobert __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
635*404b540aSrobert
636*404b540aSrobert typedef _Deque_base<_Tp, _Alloc> _Base;
637*404b540aSrobert typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
638*404b540aSrobert
639*404b540aSrobert public:
640*404b540aSrobert typedef _Tp value_type;
641*404b540aSrobert typedef typename _Tp_alloc_type::pointer pointer;
642*404b540aSrobert typedef typename _Tp_alloc_type::const_pointer const_pointer;
643*404b540aSrobert typedef typename _Tp_alloc_type::reference reference;
644*404b540aSrobert typedef typename _Tp_alloc_type::const_reference const_reference;
645*404b540aSrobert typedef typename _Base::iterator iterator;
646*404b540aSrobert typedef typename _Base::const_iterator const_iterator;
647*404b540aSrobert typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
648*404b540aSrobert typedef std::reverse_iterator<iterator> reverse_iterator;
649*404b540aSrobert typedef size_t size_type;
650*404b540aSrobert typedef ptrdiff_t difference_type;
651*404b540aSrobert typedef _Alloc allocator_type;
652*404b540aSrobert
653*404b540aSrobert protected:
654*404b540aSrobert typedef pointer* _Map_pointer;
655*404b540aSrobert
_S_buffer_size()656*404b540aSrobert static size_t _S_buffer_size()
657*404b540aSrobert { return __deque_buf_size(sizeof(_Tp)); }
658*404b540aSrobert
659*404b540aSrobert // Functions controlling memory layout, and nothing else.
660*404b540aSrobert using _Base::_M_initialize_map;
661*404b540aSrobert using _Base::_M_create_nodes;
662*404b540aSrobert using _Base::_M_destroy_nodes;
663*404b540aSrobert using _Base::_M_allocate_node;
664*404b540aSrobert using _Base::_M_deallocate_node;
665*404b540aSrobert using _Base::_M_allocate_map;
666*404b540aSrobert using _Base::_M_deallocate_map;
667*404b540aSrobert using _Base::_M_get_Tp_allocator;
668*404b540aSrobert
669*404b540aSrobert /** @if maint
670*404b540aSrobert * A total of four data members accumulated down the heirarchy.
671*404b540aSrobert * May be accessed via _M_impl.*
672*404b540aSrobert * @endif
673*404b540aSrobert */
674*404b540aSrobert using _Base::_M_impl;
675*404b540aSrobert
676*404b540aSrobert public:
677*404b540aSrobert // [23.2.1.1] construct/copy/destroy
678*404b540aSrobert // (assign() and get_allocator() are also listed in this section)
679*404b540aSrobert /**
680*404b540aSrobert * @brief Default constructor creates no elements.
681*404b540aSrobert */
682*404b540aSrobert explicit
683*404b540aSrobert deque(const allocator_type& __a = allocator_type())
684*404b540aSrobert : _Base(__a, 0) {}
685*404b540aSrobert
686*404b540aSrobert /**
687*404b540aSrobert * @brief Create a %deque with copies of an exemplar element.
688*404b540aSrobert * @param n The number of elements to initially create.
689*404b540aSrobert * @param value An element to copy.
690*404b540aSrobert *
691*404b540aSrobert * This constructor fills the %deque with @a n copies of @a value.
692*404b540aSrobert */
693*404b540aSrobert explicit
694*404b540aSrobert deque(size_type __n, const value_type& __value = value_type(),
695*404b540aSrobert const allocator_type& __a = allocator_type())
_Base(__a,__n)696*404b540aSrobert : _Base(__a, __n)
697*404b540aSrobert { _M_fill_initialize(__value); }
698*404b540aSrobert
699*404b540aSrobert /**
700*404b540aSrobert * @brief %Deque copy constructor.
701*404b540aSrobert * @param x A %deque of identical element and allocator types.
702*404b540aSrobert *
703*404b540aSrobert * The newly-created %deque uses a copy of the allocation object used
704*404b540aSrobert * by @a x.
705*404b540aSrobert */
deque(const deque & __x)706*404b540aSrobert deque(const deque& __x)
707*404b540aSrobert : _Base(__x._M_get_Tp_allocator(), __x.size())
708*404b540aSrobert { std::__uninitialized_copy_a(__x.begin(), __x.end(),
709*404b540aSrobert this->_M_impl._M_start,
710*404b540aSrobert _M_get_Tp_allocator()); }
711*404b540aSrobert
712*404b540aSrobert /**
713*404b540aSrobert * @brief Builds a %deque from a range.
714*404b540aSrobert * @param first An input iterator.
715*404b540aSrobert * @param last An input iterator.
716*404b540aSrobert *
717*404b540aSrobert * Create a %deque consisting of copies of the elements from [first,
718*404b540aSrobert * last).
719*404b540aSrobert *
720*404b540aSrobert * If the iterators are forward, bidirectional, or random-access, then
721*404b540aSrobert * this will call the elements' copy constructor N times (where N is
722*404b540aSrobert * distance(first,last)) and do no memory reallocation. But if only
723*404b540aSrobert * input iterators are used, then this will do at most 2N calls to the
724*404b540aSrobert * copy constructor, and logN memory reallocations.
725*404b540aSrobert */
726*404b540aSrobert template<typename _InputIterator>
727*404b540aSrobert deque(_InputIterator __first, _InputIterator __last,
728*404b540aSrobert const allocator_type& __a = allocator_type())
_Base(__a)729*404b540aSrobert : _Base(__a)
730*404b540aSrobert {
731*404b540aSrobert // Check whether it's an integral type. If so, it's not an iterator.
732*404b540aSrobert typedef typename std::__is_integer<_InputIterator>::__type _Integral;
733*404b540aSrobert _M_initialize_dispatch(__first, __last, _Integral());
734*404b540aSrobert }
735*404b540aSrobert
736*404b540aSrobert /**
737*404b540aSrobert * The dtor only erases the elements, and note that if the elements
738*404b540aSrobert * themselves are pointers, the pointed-to memory is not touched in any
739*404b540aSrobert * way. Managing the pointer is the user's responsibilty.
740*404b540aSrobert */
~deque()741*404b540aSrobert ~deque()
742*404b540aSrobert { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
743*404b540aSrobert
744*404b540aSrobert /**
745*404b540aSrobert * @brief %Deque assignment operator.
746*404b540aSrobert * @param x A %deque of identical element and allocator types.
747*404b540aSrobert *
748*404b540aSrobert * All the elements of @a x are copied, but unlike the copy constructor,
749*404b540aSrobert * the allocator object is not copied.
750*404b540aSrobert */
751*404b540aSrobert deque&
752*404b540aSrobert operator=(const deque& __x);
753*404b540aSrobert
754*404b540aSrobert /**
755*404b540aSrobert * @brief Assigns a given value to a %deque.
756*404b540aSrobert * @param n Number of elements to be assigned.
757*404b540aSrobert * @param val Value to be assigned.
758*404b540aSrobert *
759*404b540aSrobert * This function fills a %deque with @a n copies of the given
760*404b540aSrobert * value. Note that the assignment completely changes the
761*404b540aSrobert * %deque and that the resulting %deque's size is the same as
762*404b540aSrobert * the number of elements assigned. Old data may be lost.
763*404b540aSrobert */
764*404b540aSrobert void
assign(size_type __n,const value_type & __val)765*404b540aSrobert assign(size_type __n, const value_type& __val)
766*404b540aSrobert { _M_fill_assign(__n, __val); }
767*404b540aSrobert
768*404b540aSrobert /**
769*404b540aSrobert * @brief Assigns a range to a %deque.
770*404b540aSrobert * @param first An input iterator.
771*404b540aSrobert * @param last An input iterator.
772*404b540aSrobert *
773*404b540aSrobert * This function fills a %deque with copies of the elements in the
774*404b540aSrobert * range [first,last).
775*404b540aSrobert *
776*404b540aSrobert * Note that the assignment completely changes the %deque and that the
777*404b540aSrobert * resulting %deque's size is the same as the number of elements
778*404b540aSrobert * assigned. Old data may be lost.
779*404b540aSrobert */
780*404b540aSrobert template<typename _InputIterator>
781*404b540aSrobert void
assign(_InputIterator __first,_InputIterator __last)782*404b540aSrobert assign(_InputIterator __first, _InputIterator __last)
783*404b540aSrobert {
784*404b540aSrobert typedef typename std::__is_integer<_InputIterator>::__type _Integral;
785*404b540aSrobert _M_assign_dispatch(__first, __last, _Integral());
786*404b540aSrobert }
787*404b540aSrobert
788*404b540aSrobert /// Get a copy of the memory allocation object.
789*404b540aSrobert allocator_type
get_allocator()790*404b540aSrobert get_allocator() const
791*404b540aSrobert { return _Base::get_allocator(); }
792*404b540aSrobert
793*404b540aSrobert // iterators
794*404b540aSrobert /**
795*404b540aSrobert * Returns a read/write iterator that points to the first element in the
796*404b540aSrobert * %deque. Iteration is done in ordinary element order.
797*404b540aSrobert */
798*404b540aSrobert iterator
begin()799*404b540aSrobert begin()
800*404b540aSrobert { return this->_M_impl._M_start; }
801*404b540aSrobert
802*404b540aSrobert /**
803*404b540aSrobert * Returns a read-only (constant) iterator that points to the first
804*404b540aSrobert * element in the %deque. Iteration is done in ordinary element order.
805*404b540aSrobert */
806*404b540aSrobert const_iterator
begin()807*404b540aSrobert begin() const
808*404b540aSrobert { return this->_M_impl._M_start; }
809*404b540aSrobert
810*404b540aSrobert /**
811*404b540aSrobert * Returns a read/write iterator that points one past the last
812*404b540aSrobert * element in the %deque. Iteration is done in ordinary
813*404b540aSrobert * element order.
814*404b540aSrobert */
815*404b540aSrobert iterator
end()816*404b540aSrobert end()
817*404b540aSrobert { return this->_M_impl._M_finish; }
818*404b540aSrobert
819*404b540aSrobert /**
820*404b540aSrobert * Returns a read-only (constant) iterator that points one past
821*404b540aSrobert * the last element in the %deque. Iteration is done in
822*404b540aSrobert * ordinary element order.
823*404b540aSrobert */
824*404b540aSrobert const_iterator
end()825*404b540aSrobert end() const
826*404b540aSrobert { return this->_M_impl._M_finish; }
827*404b540aSrobert
828*404b540aSrobert /**
829*404b540aSrobert * Returns a read/write reverse iterator that points to the
830*404b540aSrobert * last element in the %deque. Iteration is done in reverse
831*404b540aSrobert * element order.
832*404b540aSrobert */
833*404b540aSrobert reverse_iterator
rbegin()834*404b540aSrobert rbegin()
835*404b540aSrobert { return reverse_iterator(this->_M_impl._M_finish); }
836*404b540aSrobert
837*404b540aSrobert /**
838*404b540aSrobert * Returns a read-only (constant) reverse iterator that points
839*404b540aSrobert * to the last element in the %deque. Iteration is done in
840*404b540aSrobert * reverse element order.
841*404b540aSrobert */
842*404b540aSrobert const_reverse_iterator
rbegin()843*404b540aSrobert rbegin() const
844*404b540aSrobert { return const_reverse_iterator(this->_M_impl._M_finish); }
845*404b540aSrobert
846*404b540aSrobert /**
847*404b540aSrobert * Returns a read/write reverse iterator that points to one
848*404b540aSrobert * before the first element in the %deque. Iteration is done
849*404b540aSrobert * in reverse element order.
850*404b540aSrobert */
851*404b540aSrobert reverse_iterator
rend()852*404b540aSrobert rend()
853*404b540aSrobert { return reverse_iterator(this->_M_impl._M_start); }
854*404b540aSrobert
855*404b540aSrobert /**
856*404b540aSrobert * Returns a read-only (constant) reverse iterator that points
857*404b540aSrobert * to one before the first element in the %deque. Iteration is
858*404b540aSrobert * done in reverse element order.
859*404b540aSrobert */
860*404b540aSrobert const_reverse_iterator
rend()861*404b540aSrobert rend() const
862*404b540aSrobert { return const_reverse_iterator(this->_M_impl._M_start); }
863*404b540aSrobert
864*404b540aSrobert // [23.2.1.2] capacity
865*404b540aSrobert /** Returns the number of elements in the %deque. */
866*404b540aSrobert size_type
size()867*404b540aSrobert size() const
868*404b540aSrobert { return this->_M_impl._M_finish - this->_M_impl._M_start; }
869*404b540aSrobert
870*404b540aSrobert /** Returns the size() of the largest possible %deque. */
871*404b540aSrobert size_type
max_size()872*404b540aSrobert max_size() const
873*404b540aSrobert { return _M_get_Tp_allocator().max_size(); }
874*404b540aSrobert
875*404b540aSrobert /**
876*404b540aSrobert * @brief Resizes the %deque to the specified number of elements.
877*404b540aSrobert * @param new_size Number of elements the %deque should contain.
878*404b540aSrobert * @param x Data with which new elements should be populated.
879*404b540aSrobert *
880*404b540aSrobert * This function will %resize the %deque to the specified
881*404b540aSrobert * number of elements. If the number is smaller than the
882*404b540aSrobert * %deque's current size the %deque is truncated, otherwise the
883*404b540aSrobert * %deque is extended and new elements are populated with given
884*404b540aSrobert * data.
885*404b540aSrobert */
886*404b540aSrobert void
887*404b540aSrobert resize(size_type __new_size, value_type __x = value_type())
888*404b540aSrobert {
889*404b540aSrobert const size_type __len = size();
890*404b540aSrobert if (__new_size < __len)
891*404b540aSrobert _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size));
892*404b540aSrobert else
893*404b540aSrobert insert(this->_M_impl._M_finish, __new_size - __len, __x);
894*404b540aSrobert }
895*404b540aSrobert
896*404b540aSrobert /**
897*404b540aSrobert * Returns true if the %deque is empty. (Thus begin() would
898*404b540aSrobert * equal end().)
899*404b540aSrobert */
900*404b540aSrobert bool
empty()901*404b540aSrobert empty() const
902*404b540aSrobert { return this->_M_impl._M_finish == this->_M_impl._M_start; }
903*404b540aSrobert
904*404b540aSrobert // element access
905*404b540aSrobert /**
906*404b540aSrobert * @brief Subscript access to the data contained in the %deque.
907*404b540aSrobert * @param n The index of the element for which data should be
908*404b540aSrobert * accessed.
909*404b540aSrobert * @return Read/write reference to data.
910*404b540aSrobert *
911*404b540aSrobert * This operator allows for easy, array-style, data access.
912*404b540aSrobert * Note that data access with this operator is unchecked and
913*404b540aSrobert * out_of_range lookups are not defined. (For checked lookups
914*404b540aSrobert * see at().)
915*404b540aSrobert */
916*404b540aSrobert reference
917*404b540aSrobert operator[](size_type __n)
918*404b540aSrobert { return this->_M_impl._M_start[difference_type(__n)]; }
919*404b540aSrobert
920*404b540aSrobert /**
921*404b540aSrobert * @brief Subscript access to the data contained in the %deque.
922*404b540aSrobert * @param n The index of the element for which data should be
923*404b540aSrobert * accessed.
924*404b540aSrobert * @return Read-only (constant) reference to data.
925*404b540aSrobert *
926*404b540aSrobert * This operator allows for easy, array-style, data access.
927*404b540aSrobert * Note that data access with this operator is unchecked and
928*404b540aSrobert * out_of_range lookups are not defined. (For checked lookups
929*404b540aSrobert * see at().)
930*404b540aSrobert */
931*404b540aSrobert const_reference
932*404b540aSrobert operator[](size_type __n) const
933*404b540aSrobert { return this->_M_impl._M_start[difference_type(__n)]; }
934*404b540aSrobert
935*404b540aSrobert protected:
936*404b540aSrobert /// @if maint Safety check used only from at(). @endif
937*404b540aSrobert void
_M_range_check(size_type __n)938*404b540aSrobert _M_range_check(size_type __n) const
939*404b540aSrobert {
940*404b540aSrobert if (__n >= this->size())
941*404b540aSrobert __throw_out_of_range(__N("deque::_M_range_check"));
942*404b540aSrobert }
943*404b540aSrobert
944*404b540aSrobert public:
945*404b540aSrobert /**
946*404b540aSrobert * @brief Provides access to the data contained in the %deque.
947*404b540aSrobert * @param n The index of the element for which data should be
948*404b540aSrobert * accessed.
949*404b540aSrobert * @return Read/write reference to data.
950*404b540aSrobert * @throw std::out_of_range If @a n is an invalid index.
951*404b540aSrobert *
952*404b540aSrobert * This function provides for safer data access. The parameter
953*404b540aSrobert * is first checked that it is in the range of the deque. The
954*404b540aSrobert * function throws out_of_range if the check fails.
955*404b540aSrobert */
956*404b540aSrobert reference
at(size_type __n)957*404b540aSrobert at(size_type __n)
958*404b540aSrobert {
959*404b540aSrobert _M_range_check(__n);
960*404b540aSrobert return (*this)[__n];
961*404b540aSrobert }
962*404b540aSrobert
963*404b540aSrobert /**
964*404b540aSrobert * @brief Provides access to the data contained in the %deque.
965*404b540aSrobert * @param n The index of the element for which data should be
966*404b540aSrobert * accessed.
967*404b540aSrobert * @return Read-only (constant) reference to data.
968*404b540aSrobert * @throw std::out_of_range If @a n is an invalid index.
969*404b540aSrobert *
970*404b540aSrobert * This function provides for safer data access. The parameter is first
971*404b540aSrobert * checked that it is in the range of the deque. The function throws
972*404b540aSrobert * out_of_range if the check fails.
973*404b540aSrobert */
974*404b540aSrobert const_reference
at(size_type __n)975*404b540aSrobert at(size_type __n) const
976*404b540aSrobert {
977*404b540aSrobert _M_range_check(__n);
978*404b540aSrobert return (*this)[__n];
979*404b540aSrobert }
980*404b540aSrobert
981*404b540aSrobert /**
982*404b540aSrobert * Returns a read/write reference to the data at the first
983*404b540aSrobert * element of the %deque.
984*404b540aSrobert */
985*404b540aSrobert reference
front()986*404b540aSrobert front()
987*404b540aSrobert { return *begin(); }
988*404b540aSrobert
989*404b540aSrobert /**
990*404b540aSrobert * Returns a read-only (constant) reference to the data at the first
991*404b540aSrobert * element of the %deque.
992*404b540aSrobert */
993*404b540aSrobert const_reference
front()994*404b540aSrobert front() const
995*404b540aSrobert { return *begin(); }
996*404b540aSrobert
997*404b540aSrobert /**
998*404b540aSrobert * Returns a read/write reference to the data at the last element of the
999*404b540aSrobert * %deque.
1000*404b540aSrobert */
1001*404b540aSrobert reference
back()1002*404b540aSrobert back()
1003*404b540aSrobert {
1004*404b540aSrobert iterator __tmp = end();
1005*404b540aSrobert --__tmp;
1006*404b540aSrobert return *__tmp;
1007*404b540aSrobert }
1008*404b540aSrobert
1009*404b540aSrobert /**
1010*404b540aSrobert * Returns a read-only (constant) reference to the data at the last
1011*404b540aSrobert * element of the %deque.
1012*404b540aSrobert */
1013*404b540aSrobert const_reference
back()1014*404b540aSrobert back() const
1015*404b540aSrobert {
1016*404b540aSrobert const_iterator __tmp = end();
1017*404b540aSrobert --__tmp;
1018*404b540aSrobert return *__tmp;
1019*404b540aSrobert }
1020*404b540aSrobert
1021*404b540aSrobert // [23.2.1.2] modifiers
1022*404b540aSrobert /**
1023*404b540aSrobert * @brief Add data to the front of the %deque.
1024*404b540aSrobert * @param x Data to be added.
1025*404b540aSrobert *
1026*404b540aSrobert * This is a typical stack operation. The function creates an
1027*404b540aSrobert * element at the front of the %deque and assigns the given
1028*404b540aSrobert * data to it. Due to the nature of a %deque this operation
1029*404b540aSrobert * can be done in constant time.
1030*404b540aSrobert */
1031*404b540aSrobert void
push_front(const value_type & __x)1032*404b540aSrobert push_front(const value_type& __x)
1033*404b540aSrobert {
1034*404b540aSrobert if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1035*404b540aSrobert {
1036*404b540aSrobert this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1037*404b540aSrobert --this->_M_impl._M_start._M_cur;
1038*404b540aSrobert }
1039*404b540aSrobert else
1040*404b540aSrobert _M_push_front_aux(__x);
1041*404b540aSrobert }
1042*404b540aSrobert
1043*404b540aSrobert /**
1044*404b540aSrobert * @brief Add data to the end of the %deque.
1045*404b540aSrobert * @param x Data to be added.
1046*404b540aSrobert *
1047*404b540aSrobert * This is a typical stack operation. The function creates an
1048*404b540aSrobert * element at the end of the %deque and assigns the given data
1049*404b540aSrobert * to it. Due to the nature of a %deque this operation can be
1050*404b540aSrobert * done in constant time.
1051*404b540aSrobert */
1052*404b540aSrobert void
push_back(const value_type & __x)1053*404b540aSrobert push_back(const value_type& __x)
1054*404b540aSrobert {
1055*404b540aSrobert if (this->_M_impl._M_finish._M_cur
1056*404b540aSrobert != this->_M_impl._M_finish._M_last - 1)
1057*404b540aSrobert {
1058*404b540aSrobert this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1059*404b540aSrobert ++this->_M_impl._M_finish._M_cur;
1060*404b540aSrobert }
1061*404b540aSrobert else
1062*404b540aSrobert _M_push_back_aux(__x);
1063*404b540aSrobert }
1064*404b540aSrobert
1065*404b540aSrobert /**
1066*404b540aSrobert * @brief Removes first element.
1067*404b540aSrobert *
1068*404b540aSrobert * This is a typical stack operation. It shrinks the %deque by one.
1069*404b540aSrobert *
1070*404b540aSrobert * Note that no data is returned, and if the first element's data is
1071*404b540aSrobert * needed, it should be retrieved before pop_front() is called.
1072*404b540aSrobert */
1073*404b540aSrobert void
pop_front()1074*404b540aSrobert pop_front()
1075*404b540aSrobert {
1076*404b540aSrobert if (this->_M_impl._M_start._M_cur
1077*404b540aSrobert != this->_M_impl._M_start._M_last - 1)
1078*404b540aSrobert {
1079*404b540aSrobert this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1080*404b540aSrobert ++this->_M_impl._M_start._M_cur;
1081*404b540aSrobert }
1082*404b540aSrobert else
1083*404b540aSrobert _M_pop_front_aux();
1084*404b540aSrobert }
1085*404b540aSrobert
1086*404b540aSrobert /**
1087*404b540aSrobert * @brief Removes last element.
1088*404b540aSrobert *
1089*404b540aSrobert * This is a typical stack operation. It shrinks the %deque by one.
1090*404b540aSrobert *
1091*404b540aSrobert * Note that no data is returned, and if the last element's data is
1092*404b540aSrobert * needed, it should be retrieved before pop_back() is called.
1093*404b540aSrobert */
1094*404b540aSrobert void
pop_back()1095*404b540aSrobert pop_back()
1096*404b540aSrobert {
1097*404b540aSrobert if (this->_M_impl._M_finish._M_cur
1098*404b540aSrobert != this->_M_impl._M_finish._M_first)
1099*404b540aSrobert {
1100*404b540aSrobert --this->_M_impl._M_finish._M_cur;
1101*404b540aSrobert this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1102*404b540aSrobert }
1103*404b540aSrobert else
1104*404b540aSrobert _M_pop_back_aux();
1105*404b540aSrobert }
1106*404b540aSrobert
1107*404b540aSrobert /**
1108*404b540aSrobert * @brief Inserts given value into %deque before specified iterator.
1109*404b540aSrobert * @param position An iterator into the %deque.
1110*404b540aSrobert * @param x Data to be inserted.
1111*404b540aSrobert * @return An iterator that points to the inserted data.
1112*404b540aSrobert *
1113*404b540aSrobert * This function will insert a copy of the given value before the
1114*404b540aSrobert * specified location.
1115*404b540aSrobert */
1116*404b540aSrobert iterator
1117*404b540aSrobert insert(iterator __position, const value_type& __x);
1118*404b540aSrobert
1119*404b540aSrobert /**
1120*404b540aSrobert * @brief Inserts a number of copies of given data into the %deque.
1121*404b540aSrobert * @param position An iterator into the %deque.
1122*404b540aSrobert * @param n Number of elements to be inserted.
1123*404b540aSrobert * @param x Data to be inserted.
1124*404b540aSrobert *
1125*404b540aSrobert * This function will insert a specified number of copies of the given
1126*404b540aSrobert * data before the location specified by @a position.
1127*404b540aSrobert */
1128*404b540aSrobert void
insert(iterator __position,size_type __n,const value_type & __x)1129*404b540aSrobert insert(iterator __position, size_type __n, const value_type& __x)
1130*404b540aSrobert { _M_fill_insert(__position, __n, __x); }
1131*404b540aSrobert
1132*404b540aSrobert /**
1133*404b540aSrobert * @brief Inserts a range into the %deque.
1134*404b540aSrobert * @param position An iterator into the %deque.
1135*404b540aSrobert * @param first An input iterator.
1136*404b540aSrobert * @param last An input iterator.
1137*404b540aSrobert *
1138*404b540aSrobert * This function will insert copies of the data in the range
1139*404b540aSrobert * [first,last) into the %deque before the location specified
1140*404b540aSrobert * by @a pos. This is known as "range insert."
1141*404b540aSrobert */
1142*404b540aSrobert template<typename _InputIterator>
1143*404b540aSrobert void
insert(iterator __position,_InputIterator __first,_InputIterator __last)1144*404b540aSrobert insert(iterator __position, _InputIterator __first,
1145*404b540aSrobert _InputIterator __last)
1146*404b540aSrobert {
1147*404b540aSrobert // Check whether it's an integral type. If so, it's not an iterator.
1148*404b540aSrobert typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1149*404b540aSrobert _M_insert_dispatch(__position, __first, __last, _Integral());
1150*404b540aSrobert }
1151*404b540aSrobert
1152*404b540aSrobert /**
1153*404b540aSrobert * @brief Remove element at given position.
1154*404b540aSrobert * @param position Iterator pointing to element to be erased.
1155*404b540aSrobert * @return An iterator pointing to the next element (or end()).
1156*404b540aSrobert *
1157*404b540aSrobert * This function will erase the element at the given position and thus
1158*404b540aSrobert * shorten the %deque by one.
1159*404b540aSrobert *
1160*404b540aSrobert * The user is cautioned that
1161*404b540aSrobert * this function only erases the element, and that if the element is
1162*404b540aSrobert * itself a pointer, the pointed-to memory is not touched in any way.
1163*404b540aSrobert * Managing the pointer is the user's responsibilty.
1164*404b540aSrobert */
1165*404b540aSrobert iterator
1166*404b540aSrobert erase(iterator __position);
1167*404b540aSrobert
1168*404b540aSrobert /**
1169*404b540aSrobert * @brief Remove a range of elements.
1170*404b540aSrobert * @param first Iterator pointing to the first element to be erased.
1171*404b540aSrobert * @param last Iterator pointing to one past the last element to be
1172*404b540aSrobert * erased.
1173*404b540aSrobert * @return An iterator pointing to the element pointed to by @a last
1174*404b540aSrobert * prior to erasing (or end()).
1175*404b540aSrobert *
1176*404b540aSrobert * This function will erase the elements in the range [first,last) and
1177*404b540aSrobert * shorten the %deque accordingly.
1178*404b540aSrobert *
1179*404b540aSrobert * The user is cautioned that
1180*404b540aSrobert * this function only erases the elements, and that if the elements
1181*404b540aSrobert * themselves are pointers, the pointed-to memory is not touched in any
1182*404b540aSrobert * way. Managing the pointer is the user's responsibilty.
1183*404b540aSrobert */
1184*404b540aSrobert iterator
1185*404b540aSrobert erase(iterator __first, iterator __last);
1186*404b540aSrobert
1187*404b540aSrobert /**
1188*404b540aSrobert * @brief Swaps data with another %deque.
1189*404b540aSrobert * @param x A %deque of the same element and allocator types.
1190*404b540aSrobert *
1191*404b540aSrobert * This exchanges the elements between two deques in constant time.
1192*404b540aSrobert * (Four pointers, so it should be quite fast.)
1193*404b540aSrobert * Note that the global std::swap() function is specialized such that
1194*404b540aSrobert * std::swap(d1,d2) will feed to this function.
1195*404b540aSrobert */
1196*404b540aSrobert void
swap(deque & __x)1197*404b540aSrobert swap(deque& __x)
1198*404b540aSrobert {
1199*404b540aSrobert std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1200*404b540aSrobert std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1201*404b540aSrobert std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1202*404b540aSrobert std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1203*404b540aSrobert
1204*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
1205*404b540aSrobert // 431. Swapping containers with unequal allocators.
1206*404b540aSrobert std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1207*404b540aSrobert __x._M_get_Tp_allocator());
1208*404b540aSrobert }
1209*404b540aSrobert
1210*404b540aSrobert /**
1211*404b540aSrobert * Erases all the elements. Note that this function only erases the
1212*404b540aSrobert * elements, and that if the elements themselves are pointers, the
1213*404b540aSrobert * pointed-to memory is not touched in any way. Managing the pointer is
1214*404b540aSrobert * the user's responsibilty.
1215*404b540aSrobert */
1216*404b540aSrobert void
clear()1217*404b540aSrobert clear()
1218*404b540aSrobert { _M_erase_at_end(begin()); }
1219*404b540aSrobert
1220*404b540aSrobert protected:
1221*404b540aSrobert // Internal constructor functions follow.
1222*404b540aSrobert
1223*404b540aSrobert // called by the range constructor to implement [23.1.1]/9
1224*404b540aSrobert template<typename _Integer>
1225*404b540aSrobert void
_M_initialize_dispatch(_Integer __n,_Integer __x,__true_type)1226*404b540aSrobert _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1227*404b540aSrobert {
1228*404b540aSrobert _M_initialize_map(__n);
1229*404b540aSrobert _M_fill_initialize(__x);
1230*404b540aSrobert }
1231*404b540aSrobert
1232*404b540aSrobert // called by the range constructor to implement [23.1.1]/9
1233*404b540aSrobert template<typename _InputIterator>
1234*404b540aSrobert void
_M_initialize_dispatch(_InputIterator __first,_InputIterator __last,__false_type)1235*404b540aSrobert _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1236*404b540aSrobert __false_type)
1237*404b540aSrobert {
1238*404b540aSrobert typedef typename std::iterator_traits<_InputIterator>::
1239*404b540aSrobert iterator_category _IterCategory;
1240*404b540aSrobert _M_range_initialize(__first, __last, _IterCategory());
1241*404b540aSrobert }
1242*404b540aSrobert
1243*404b540aSrobert // called by the second initialize_dispatch above
1244*404b540aSrobert //@{
1245*404b540aSrobert /**
1246*404b540aSrobert * @if maint
1247*404b540aSrobert * @brief Fills the deque with whatever is in [first,last).
1248*404b540aSrobert * @param first An input iterator.
1249*404b540aSrobert * @param last An input iterator.
1250*404b540aSrobert * @return Nothing.
1251*404b540aSrobert *
1252*404b540aSrobert * If the iterators are actually forward iterators (or better), then the
1253*404b540aSrobert * memory layout can be done all at once. Else we move forward using
1254*404b540aSrobert * push_back on each value from the iterator.
1255*404b540aSrobert * @endif
1256*404b540aSrobert */
1257*404b540aSrobert template<typename _InputIterator>
1258*404b540aSrobert void
1259*404b540aSrobert _M_range_initialize(_InputIterator __first, _InputIterator __last,
1260*404b540aSrobert std::input_iterator_tag);
1261*404b540aSrobert
1262*404b540aSrobert // called by the second initialize_dispatch above
1263*404b540aSrobert template<typename _ForwardIterator>
1264*404b540aSrobert void
1265*404b540aSrobert _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1266*404b540aSrobert std::forward_iterator_tag);
1267*404b540aSrobert //@}
1268*404b540aSrobert
1269*404b540aSrobert /**
1270*404b540aSrobert * @if maint
1271*404b540aSrobert * @brief Fills the %deque with copies of value.
1272*404b540aSrobert * @param value Initial value.
1273*404b540aSrobert * @return Nothing.
1274*404b540aSrobert * @pre _M_start and _M_finish have already been initialized,
1275*404b540aSrobert * but none of the %deque's elements have yet been constructed.
1276*404b540aSrobert *
1277*404b540aSrobert * This function is called only when the user provides an explicit size
1278*404b540aSrobert * (with or without an explicit exemplar value).
1279*404b540aSrobert * @endif
1280*404b540aSrobert */
1281*404b540aSrobert void
1282*404b540aSrobert _M_fill_initialize(const value_type& __value);
1283*404b540aSrobert
1284*404b540aSrobert // Internal assign functions follow. The *_aux functions do the actual
1285*404b540aSrobert // assignment work for the range versions.
1286*404b540aSrobert
1287*404b540aSrobert // called by the range assign to implement [23.1.1]/9
1288*404b540aSrobert template<typename _Integer>
1289*404b540aSrobert void
_M_assign_dispatch(_Integer __n,_Integer __val,__true_type)1290*404b540aSrobert _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1291*404b540aSrobert {
1292*404b540aSrobert _M_fill_assign(static_cast<size_type>(__n),
1293*404b540aSrobert static_cast<value_type>(__val));
1294*404b540aSrobert }
1295*404b540aSrobert
1296*404b540aSrobert // called by the range assign to implement [23.1.1]/9
1297*404b540aSrobert template<typename _InputIterator>
1298*404b540aSrobert void
_M_assign_dispatch(_InputIterator __first,_InputIterator __last,__false_type)1299*404b540aSrobert _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1300*404b540aSrobert __false_type)
1301*404b540aSrobert {
1302*404b540aSrobert typedef typename std::iterator_traits<_InputIterator>::
1303*404b540aSrobert iterator_category _IterCategory;
1304*404b540aSrobert _M_assign_aux(__first, __last, _IterCategory());
1305*404b540aSrobert }
1306*404b540aSrobert
1307*404b540aSrobert // called by the second assign_dispatch above
1308*404b540aSrobert template<typename _InputIterator>
1309*404b540aSrobert void
1310*404b540aSrobert _M_assign_aux(_InputIterator __first, _InputIterator __last,
1311*404b540aSrobert std::input_iterator_tag);
1312*404b540aSrobert
1313*404b540aSrobert // called by the second assign_dispatch above
1314*404b540aSrobert template<typename _ForwardIterator>
1315*404b540aSrobert void
_M_assign_aux(_ForwardIterator __first,_ForwardIterator __last,std::forward_iterator_tag)1316*404b540aSrobert _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1317*404b540aSrobert std::forward_iterator_tag)
1318*404b540aSrobert {
1319*404b540aSrobert const size_type __len = std::distance(__first, __last);
1320*404b540aSrobert if (__len > size())
1321*404b540aSrobert {
1322*404b540aSrobert _ForwardIterator __mid = __first;
1323*404b540aSrobert std::advance(__mid, size());
1324*404b540aSrobert std::copy(__first, __mid, begin());
1325*404b540aSrobert insert(end(), __mid, __last);
1326*404b540aSrobert }
1327*404b540aSrobert else
1328*404b540aSrobert _M_erase_at_end(std::copy(__first, __last, begin()));
1329*404b540aSrobert }
1330*404b540aSrobert
1331*404b540aSrobert // Called by assign(n,t), and the range assign when it turns out
1332*404b540aSrobert // to be the same thing.
1333*404b540aSrobert void
_M_fill_assign(size_type __n,const value_type & __val)1334*404b540aSrobert _M_fill_assign(size_type __n, const value_type& __val)
1335*404b540aSrobert {
1336*404b540aSrobert if (__n > size())
1337*404b540aSrobert {
1338*404b540aSrobert std::fill(begin(), end(), __val);
1339*404b540aSrobert insert(end(), __n - size(), __val);
1340*404b540aSrobert }
1341*404b540aSrobert else
1342*404b540aSrobert {
1343*404b540aSrobert _M_erase_at_end(begin() + difference_type(__n));
1344*404b540aSrobert std::fill(begin(), end(), __val);
1345*404b540aSrobert }
1346*404b540aSrobert }
1347*404b540aSrobert
1348*404b540aSrobert //@{
1349*404b540aSrobert /**
1350*404b540aSrobert * @if maint
1351*404b540aSrobert * @brief Helper functions for push_* and pop_*.
1352*404b540aSrobert * @endif
1353*404b540aSrobert */
1354*404b540aSrobert void _M_push_back_aux(const value_type&);
1355*404b540aSrobert
1356*404b540aSrobert void _M_push_front_aux(const value_type&);
1357*404b540aSrobert
1358*404b540aSrobert void _M_pop_back_aux();
1359*404b540aSrobert
1360*404b540aSrobert void _M_pop_front_aux();
1361*404b540aSrobert //@}
1362*404b540aSrobert
1363*404b540aSrobert // Internal insert functions follow. The *_aux functions do the actual
1364*404b540aSrobert // insertion work when all shortcuts fail.
1365*404b540aSrobert
1366*404b540aSrobert // called by the range insert to implement [23.1.1]/9
1367*404b540aSrobert template<typename _Integer>
1368*404b540aSrobert void
_M_insert_dispatch(iterator __pos,_Integer __n,_Integer __x,__true_type)1369*404b540aSrobert _M_insert_dispatch(iterator __pos,
1370*404b540aSrobert _Integer __n, _Integer __x, __true_type)
1371*404b540aSrobert {
1372*404b540aSrobert _M_fill_insert(__pos, static_cast<size_type>(__n),
1373*404b540aSrobert static_cast<value_type>(__x));
1374*404b540aSrobert }
1375*404b540aSrobert
1376*404b540aSrobert // called by the range insert to implement [23.1.1]/9
1377*404b540aSrobert template<typename _InputIterator>
1378*404b540aSrobert void
_M_insert_dispatch(iterator __pos,_InputIterator __first,_InputIterator __last,__false_type)1379*404b540aSrobert _M_insert_dispatch(iterator __pos,
1380*404b540aSrobert _InputIterator __first, _InputIterator __last,
1381*404b540aSrobert __false_type)
1382*404b540aSrobert {
1383*404b540aSrobert typedef typename std::iterator_traits<_InputIterator>::
1384*404b540aSrobert iterator_category _IterCategory;
1385*404b540aSrobert _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1386*404b540aSrobert }
1387*404b540aSrobert
1388*404b540aSrobert // called by the second insert_dispatch above
1389*404b540aSrobert template<typename _InputIterator>
1390*404b540aSrobert void
1391*404b540aSrobert _M_range_insert_aux(iterator __pos, _InputIterator __first,
1392*404b540aSrobert _InputIterator __last, std::input_iterator_tag);
1393*404b540aSrobert
1394*404b540aSrobert // called by the second insert_dispatch above
1395*404b540aSrobert template<typename _ForwardIterator>
1396*404b540aSrobert void
1397*404b540aSrobert _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1398*404b540aSrobert _ForwardIterator __last, std::forward_iterator_tag);
1399*404b540aSrobert
1400*404b540aSrobert // Called by insert(p,n,x), and the range insert when it turns out to be
1401*404b540aSrobert // the same thing. Can use fill functions in optimal situations,
1402*404b540aSrobert // otherwise passes off to insert_aux(p,n,x).
1403*404b540aSrobert void
1404*404b540aSrobert _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1405*404b540aSrobert
1406*404b540aSrobert // called by insert(p,x)
1407*404b540aSrobert iterator
1408*404b540aSrobert _M_insert_aux(iterator __pos, const value_type& __x);
1409*404b540aSrobert
1410*404b540aSrobert // called by insert(p,n,x) via fill_insert
1411*404b540aSrobert void
1412*404b540aSrobert _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1413*404b540aSrobert
1414*404b540aSrobert // called by range_insert_aux for forward iterators
1415*404b540aSrobert template<typename _ForwardIterator>
1416*404b540aSrobert void
1417*404b540aSrobert _M_insert_aux(iterator __pos,
1418*404b540aSrobert _ForwardIterator __first, _ForwardIterator __last,
1419*404b540aSrobert size_type __n);
1420*404b540aSrobert
1421*404b540aSrobert
1422*404b540aSrobert // Internal erase functions follow.
1423*404b540aSrobert
1424*404b540aSrobert void
1425*404b540aSrobert _M_destroy_data_aux(iterator __first, iterator __last);
1426*404b540aSrobert
1427*404b540aSrobert void
_M_destroy_data_dispatch(iterator,iterator,__true_type)1428*404b540aSrobert _M_destroy_data_dispatch(iterator, iterator, __true_type) { }
1429*404b540aSrobert
1430*404b540aSrobert void
_M_destroy_data_dispatch(iterator __first,iterator __last,__false_type)1431*404b540aSrobert _M_destroy_data_dispatch(iterator __first, iterator __last, __false_type)
1432*404b540aSrobert { _M_destroy_data_aux(__first, __last); }
1433*404b540aSrobert
1434*404b540aSrobert // Called by ~deque().
1435*404b540aSrobert // NB: Doesn't deallocate the nodes.
1436*404b540aSrobert template<typename _Alloc1>
1437*404b540aSrobert void
_M_destroy_data(iterator __first,iterator __last,const _Alloc1 &)1438*404b540aSrobert _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
1439*404b540aSrobert { _M_destroy_data_aux(__first, __last); }
1440*404b540aSrobert
1441*404b540aSrobert void
_M_destroy_data(iterator __first,iterator __last,const std::allocator<_Tp> &)1442*404b540aSrobert _M_destroy_data(iterator __first, iterator __last,
1443*404b540aSrobert const std::allocator<_Tp>&)
1444*404b540aSrobert {
1445*404b540aSrobert typedef typename std::__is_scalar<value_type>::__type
1446*404b540aSrobert _Has_trivial_destructor;
1447*404b540aSrobert _M_destroy_data_dispatch(__first, __last, _Has_trivial_destructor());
1448*404b540aSrobert }
1449*404b540aSrobert
1450*404b540aSrobert // Called by erase(q1, q2).
1451*404b540aSrobert void
_M_erase_at_begin(iterator __pos)1452*404b540aSrobert _M_erase_at_begin(iterator __pos)
1453*404b540aSrobert {
1454*404b540aSrobert _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
1455*404b540aSrobert _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
1456*404b540aSrobert this->_M_impl._M_start = __pos;
1457*404b540aSrobert }
1458*404b540aSrobert
1459*404b540aSrobert // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1460*404b540aSrobert // _M_fill_assign, operator=.
1461*404b540aSrobert void
_M_erase_at_end(iterator __pos)1462*404b540aSrobert _M_erase_at_end(iterator __pos)
1463*404b540aSrobert {
1464*404b540aSrobert _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
1465*404b540aSrobert _M_destroy_nodes(__pos._M_node + 1,
1466*404b540aSrobert this->_M_impl._M_finish._M_node + 1);
1467*404b540aSrobert this->_M_impl._M_finish = __pos;
1468*404b540aSrobert }
1469*404b540aSrobert
1470*404b540aSrobert //@{
1471*404b540aSrobert /**
1472*404b540aSrobert * @if maint
1473*404b540aSrobert * @brief Memory-handling helpers for the previous internal insert
1474*404b540aSrobert * functions.
1475*404b540aSrobert * @endif
1476*404b540aSrobert */
1477*404b540aSrobert iterator
_M_reserve_elements_at_front(size_type __n)1478*404b540aSrobert _M_reserve_elements_at_front(size_type __n)
1479*404b540aSrobert {
1480*404b540aSrobert const size_type __vacancies = this->_M_impl._M_start._M_cur
1481*404b540aSrobert - this->_M_impl._M_start._M_first;
1482*404b540aSrobert if (__n > __vacancies)
1483*404b540aSrobert _M_new_elements_at_front(__n - __vacancies);
1484*404b540aSrobert return this->_M_impl._M_start - difference_type(__n);
1485*404b540aSrobert }
1486*404b540aSrobert
1487*404b540aSrobert iterator
_M_reserve_elements_at_back(size_type __n)1488*404b540aSrobert _M_reserve_elements_at_back(size_type __n)
1489*404b540aSrobert {
1490*404b540aSrobert const size_type __vacancies = (this->_M_impl._M_finish._M_last
1491*404b540aSrobert - this->_M_impl._M_finish._M_cur) - 1;
1492*404b540aSrobert if (__n > __vacancies)
1493*404b540aSrobert _M_new_elements_at_back(__n - __vacancies);
1494*404b540aSrobert return this->_M_impl._M_finish + difference_type(__n);
1495*404b540aSrobert }
1496*404b540aSrobert
1497*404b540aSrobert void
1498*404b540aSrobert _M_new_elements_at_front(size_type __new_elements);
1499*404b540aSrobert
1500*404b540aSrobert void
1501*404b540aSrobert _M_new_elements_at_back(size_type __new_elements);
1502*404b540aSrobert //@}
1503*404b540aSrobert
1504*404b540aSrobert
1505*404b540aSrobert //@{
1506*404b540aSrobert /**
1507*404b540aSrobert * @if maint
1508*404b540aSrobert * @brief Memory-handling helpers for the major %map.
1509*404b540aSrobert *
1510*404b540aSrobert * Makes sure the _M_map has space for new nodes. Does not
1511*404b540aSrobert * actually add the nodes. Can invalidate _M_map pointers.
1512*404b540aSrobert * (And consequently, %deque iterators.)
1513*404b540aSrobert * @endif
1514*404b540aSrobert */
1515*404b540aSrobert void
1516*404b540aSrobert _M_reserve_map_at_back(size_type __nodes_to_add = 1)
1517*404b540aSrobert {
1518*404b540aSrobert if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1519*404b540aSrobert - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1520*404b540aSrobert _M_reallocate_map(__nodes_to_add, false);
1521*404b540aSrobert }
1522*404b540aSrobert
1523*404b540aSrobert void
1524*404b540aSrobert _M_reserve_map_at_front(size_type __nodes_to_add = 1)
1525*404b540aSrobert {
1526*404b540aSrobert if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1527*404b540aSrobert - this->_M_impl._M_map))
1528*404b540aSrobert _M_reallocate_map(__nodes_to_add, true);
1529*404b540aSrobert }
1530*404b540aSrobert
1531*404b540aSrobert void
1532*404b540aSrobert _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1533*404b540aSrobert //@}
1534*404b540aSrobert };
1535*404b540aSrobert
1536*404b540aSrobert
1537*404b540aSrobert /**
1538*404b540aSrobert * @brief Deque equality comparison.
1539*404b540aSrobert * @param x A %deque.
1540*404b540aSrobert * @param y A %deque of the same type as @a x.
1541*404b540aSrobert * @return True iff the size and elements of the deques are equal.
1542*404b540aSrobert *
1543*404b540aSrobert * This is an equivalence relation. It is linear in the size of the
1544*404b540aSrobert * deques. Deques are considered equivalent if their sizes are equal,
1545*404b540aSrobert * and if corresponding elements compare equal.
1546*404b540aSrobert */
1547*404b540aSrobert template<typename _Tp, typename _Alloc>
1548*404b540aSrobert inline bool
1549*404b540aSrobert operator==(const deque<_Tp, _Alloc>& __x,
1550*404b540aSrobert const deque<_Tp, _Alloc>& __y)
1551*404b540aSrobert { return __x.size() == __y.size()
1552*404b540aSrobert && std::equal(__x.begin(), __x.end(), __y.begin()); }
1553*404b540aSrobert
1554*404b540aSrobert /**
1555*404b540aSrobert * @brief Deque ordering relation.
1556*404b540aSrobert * @param x A %deque.
1557*404b540aSrobert * @param y A %deque of the same type as @a x.
1558*404b540aSrobert * @return True iff @a x is lexicographically less than @a y.
1559*404b540aSrobert *
1560*404b540aSrobert * This is a total ordering relation. It is linear in the size of the
1561*404b540aSrobert * deques. The elements must be comparable with @c <.
1562*404b540aSrobert *
1563*404b540aSrobert * See std::lexicographical_compare() for how the determination is made.
1564*404b540aSrobert */
1565*404b540aSrobert template<typename _Tp, typename _Alloc>
1566*404b540aSrobert inline bool
1567*404b540aSrobert operator<(const deque<_Tp, _Alloc>& __x,
1568*404b540aSrobert const deque<_Tp, _Alloc>& __y)
1569*404b540aSrobert { return std::lexicographical_compare(__x.begin(), __x.end(),
1570*404b540aSrobert __y.begin(), __y.end()); }
1571*404b540aSrobert
1572*404b540aSrobert /// Based on operator==
1573*404b540aSrobert template<typename _Tp, typename _Alloc>
1574*404b540aSrobert inline bool
1575*404b540aSrobert operator!=(const deque<_Tp, _Alloc>& __x,
1576*404b540aSrobert const deque<_Tp, _Alloc>& __y)
1577*404b540aSrobert { return !(__x == __y); }
1578*404b540aSrobert
1579*404b540aSrobert /// Based on operator<
1580*404b540aSrobert template<typename _Tp, typename _Alloc>
1581*404b540aSrobert inline bool
1582*404b540aSrobert operator>(const deque<_Tp, _Alloc>& __x,
1583*404b540aSrobert const deque<_Tp, _Alloc>& __y)
1584*404b540aSrobert { return __y < __x; }
1585*404b540aSrobert
1586*404b540aSrobert /// Based on operator<
1587*404b540aSrobert template<typename _Tp, typename _Alloc>
1588*404b540aSrobert inline bool
1589*404b540aSrobert operator<=(const deque<_Tp, _Alloc>& __x,
1590*404b540aSrobert const deque<_Tp, _Alloc>& __y)
1591*404b540aSrobert { return !(__y < __x); }
1592*404b540aSrobert
1593*404b540aSrobert /// Based on operator<
1594*404b540aSrobert template<typename _Tp, typename _Alloc>
1595*404b540aSrobert inline bool
1596*404b540aSrobert operator>=(const deque<_Tp, _Alloc>& __x,
1597*404b540aSrobert const deque<_Tp, _Alloc>& __y)
1598*404b540aSrobert { return !(__x < __y); }
1599*404b540aSrobert
1600*404b540aSrobert /// See std::deque::swap().
1601*404b540aSrobert template<typename _Tp, typename _Alloc>
1602*404b540aSrobert inline void
swap(deque<_Tp,_Alloc> & __x,deque<_Tp,_Alloc> & __y)1603*404b540aSrobert swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1604*404b540aSrobert { __x.swap(__y); }
1605*404b540aSrobert
1606*404b540aSrobert _GLIBCXX_END_NESTED_NAMESPACE
1607*404b540aSrobert
1608*404b540aSrobert #endif /* _DEQUE_H */
1609