1*38fd1498Szrj // Deque implementation -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc.
4*38fd1498Szrj //
5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free
6*38fd1498Szrj // software; you can redistribute it and/or modify it under the
7*38fd1498Szrj // terms of the GNU General Public License as published by the
8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj
11*38fd1498Szrj // This library is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj /*
26*38fd1498Szrj *
27*38fd1498Szrj * Copyright (c) 1994
28*38fd1498Szrj * Hewlett-Packard Company
29*38fd1498Szrj *
30*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software
31*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee,
32*38fd1498Szrj * provided that the above copyright notice appear in all copies and
33*38fd1498Szrj * that both that copyright notice and this permission notice appear
34*38fd1498Szrj * in supporting documentation. Hewlett-Packard Company makes no
35*38fd1498Szrj * representations about the suitability of this software for any
36*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty.
37*38fd1498Szrj *
38*38fd1498Szrj *
39*38fd1498Szrj * Copyright (c) 1997
40*38fd1498Szrj * Silicon Graphics Computer Systems, Inc.
41*38fd1498Szrj *
42*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software
43*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee,
44*38fd1498Szrj * provided that the above copyright notice appear in all copies and
45*38fd1498Szrj * that both that copyright notice and this permission notice appear
46*38fd1498Szrj * in supporting documentation. Silicon Graphics makes no
47*38fd1498Szrj * representations about the suitability of this software for any
48*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty.
49*38fd1498Szrj */
50*38fd1498Szrj
51*38fd1498Szrj /** @file bits/stl_deque.h
52*38fd1498Szrj * This is an internal header file, included by other library headers.
53*38fd1498Szrj * Do not attempt to use it directly. @headername{deque}
54*38fd1498Szrj */
55*38fd1498Szrj
56*38fd1498Szrj #ifndef _STL_DEQUE_H
57*38fd1498Szrj #define _STL_DEQUE_H 1
58*38fd1498Szrj
59*38fd1498Szrj #include <bits/concept_check.h>
60*38fd1498Szrj #include <bits/stl_iterator_base_types.h>
61*38fd1498Szrj #include <bits/stl_iterator_base_funcs.h>
62*38fd1498Szrj #if __cplusplus >= 201103L
63*38fd1498Szrj #include <initializer_list>
64*38fd1498Szrj #endif
65*38fd1498Szrj
66*38fd1498Szrj #include <debug/assertions.h>
67*38fd1498Szrj
_GLIBCXX_VISIBILITY(default)68*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
69*38fd1498Szrj {
70*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
71*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
72*38fd1498Szrj
73*38fd1498Szrj /**
74*38fd1498Szrj * @brief This function controls the size of memory nodes.
75*38fd1498Szrj * @param __size The size of an element.
76*38fd1498Szrj * @return The number (not byte size) of elements per node.
77*38fd1498Szrj *
78*38fd1498Szrj * This function started off as a compiler kludge from SGI, but
79*38fd1498Szrj * seems to be a useful wrapper around a repeated constant
80*38fd1498Szrj * expression. The @b 512 is tunable (and no other code needs to
81*38fd1498Szrj * change), but no investigation has been done since inheriting the
82*38fd1498Szrj * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
83*38fd1498Szrj * you are doing, however: changing it breaks the binary
84*38fd1498Szrj * compatibility!!
85*38fd1498Szrj */
86*38fd1498Szrj
87*38fd1498Szrj #ifndef _GLIBCXX_DEQUE_BUF_SIZE
88*38fd1498Szrj #define _GLIBCXX_DEQUE_BUF_SIZE 512
89*38fd1498Szrj #endif
90*38fd1498Szrj
91*38fd1498Szrj _GLIBCXX_CONSTEXPR inline size_t
92*38fd1498Szrj __deque_buf_size(size_t __size)
93*38fd1498Szrj { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
94*38fd1498Szrj ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
95*38fd1498Szrj
96*38fd1498Szrj
97*38fd1498Szrj /**
98*38fd1498Szrj * @brief A deque::iterator.
99*38fd1498Szrj *
100*38fd1498Szrj * Quite a bit of intelligence here. Much of the functionality of
101*38fd1498Szrj * deque is actually passed off to this class. A deque holds two
102*38fd1498Szrj * of these internally, marking its valid range. Access to
103*38fd1498Szrj * elements is done as offsets of either of those two, relying on
104*38fd1498Szrj * operator overloading in this class.
105*38fd1498Szrj *
106*38fd1498Szrj * All the functions are op overloads except for _M_set_node.
107*38fd1498Szrj */
108*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
109*38fd1498Szrj struct _Deque_iterator
110*38fd1498Szrj {
111*38fd1498Szrj #if __cplusplus < 201103L
112*38fd1498Szrj typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
113*38fd1498Szrj typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
114*38fd1498Szrj typedef _Tp* _Elt_pointer;
115*38fd1498Szrj typedef _Tp** _Map_pointer;
116*38fd1498Szrj #else
117*38fd1498Szrj private:
118*38fd1498Szrj template<typename _Up>
119*38fd1498Szrj using __ptr_to = typename pointer_traits<_Ptr>::template rebind<_Up>;
120*38fd1498Szrj template<typename _CvTp>
121*38fd1498Szrj using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_to<_CvTp>>;
122*38fd1498Szrj public:
123*38fd1498Szrj typedef __iter<_Tp> iterator;
124*38fd1498Szrj typedef __iter<const _Tp> const_iterator;
125*38fd1498Szrj typedef __ptr_to<_Tp> _Elt_pointer;
126*38fd1498Szrj typedef __ptr_to<_Elt_pointer> _Map_pointer;
127*38fd1498Szrj #endif
128*38fd1498Szrj
129*38fd1498Szrj static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
130*38fd1498Szrj { return __deque_buf_size(sizeof(_Tp)); }
131*38fd1498Szrj
132*38fd1498Szrj typedef std::random_access_iterator_tag iterator_category;
133*38fd1498Szrj typedef _Tp value_type;
134*38fd1498Szrj typedef _Ptr pointer;
135*38fd1498Szrj typedef _Ref reference;
136*38fd1498Szrj typedef size_t size_type;
137*38fd1498Szrj typedef ptrdiff_t difference_type;
138*38fd1498Szrj typedef _Deque_iterator _Self;
139*38fd1498Szrj
140*38fd1498Szrj _Elt_pointer _M_cur;
141*38fd1498Szrj _Elt_pointer _M_first;
142*38fd1498Szrj _Elt_pointer _M_last;
143*38fd1498Szrj _Map_pointer _M_node;
144*38fd1498Szrj
145*38fd1498Szrj _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT
146*38fd1498Szrj : _M_cur(__x), _M_first(*__y),
147*38fd1498Szrj _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
148*38fd1498Szrj
149*38fd1498Szrj _Deque_iterator() _GLIBCXX_NOEXCEPT
150*38fd1498Szrj : _M_cur(), _M_first(), _M_last(), _M_node() { }
151*38fd1498Szrj
152*38fd1498Szrj _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
153*38fd1498Szrj : _M_cur(__x._M_cur), _M_first(__x._M_first),
154*38fd1498Szrj _M_last(__x._M_last), _M_node(__x._M_node) { }
155*38fd1498Szrj
156*38fd1498Szrj iterator
157*38fd1498Szrj _M_const_cast() const _GLIBCXX_NOEXCEPT
158*38fd1498Szrj { return iterator(_M_cur, _M_node); }
159*38fd1498Szrj
160*38fd1498Szrj reference
161*38fd1498Szrj operator*() const _GLIBCXX_NOEXCEPT
162*38fd1498Szrj { return *_M_cur; }
163*38fd1498Szrj
164*38fd1498Szrj pointer
165*38fd1498Szrj operator->() const _GLIBCXX_NOEXCEPT
166*38fd1498Szrj { return _M_cur; }
167*38fd1498Szrj
168*38fd1498Szrj _Self&
169*38fd1498Szrj operator++() _GLIBCXX_NOEXCEPT
170*38fd1498Szrj {
171*38fd1498Szrj ++_M_cur;
172*38fd1498Szrj if (_M_cur == _M_last)
173*38fd1498Szrj {
174*38fd1498Szrj _M_set_node(_M_node + 1);
175*38fd1498Szrj _M_cur = _M_first;
176*38fd1498Szrj }
177*38fd1498Szrj return *this;
178*38fd1498Szrj }
179*38fd1498Szrj
180*38fd1498Szrj _Self
181*38fd1498Szrj operator++(int) _GLIBCXX_NOEXCEPT
182*38fd1498Szrj {
183*38fd1498Szrj _Self __tmp = *this;
184*38fd1498Szrj ++*this;
185*38fd1498Szrj return __tmp;
186*38fd1498Szrj }
187*38fd1498Szrj
188*38fd1498Szrj _Self&
189*38fd1498Szrj operator--() _GLIBCXX_NOEXCEPT
190*38fd1498Szrj {
191*38fd1498Szrj if (_M_cur == _M_first)
192*38fd1498Szrj {
193*38fd1498Szrj _M_set_node(_M_node - 1);
194*38fd1498Szrj _M_cur = _M_last;
195*38fd1498Szrj }
196*38fd1498Szrj --_M_cur;
197*38fd1498Szrj return *this;
198*38fd1498Szrj }
199*38fd1498Szrj
200*38fd1498Szrj _Self
201*38fd1498Szrj operator--(int) _GLIBCXX_NOEXCEPT
202*38fd1498Szrj {
203*38fd1498Szrj _Self __tmp = *this;
204*38fd1498Szrj --*this;
205*38fd1498Szrj return __tmp;
206*38fd1498Szrj }
207*38fd1498Szrj
208*38fd1498Szrj _Self&
209*38fd1498Szrj operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
210*38fd1498Szrj {
211*38fd1498Szrj const difference_type __offset = __n + (_M_cur - _M_first);
212*38fd1498Szrj if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
213*38fd1498Szrj _M_cur += __n;
214*38fd1498Szrj else
215*38fd1498Szrj {
216*38fd1498Szrj const difference_type __node_offset =
217*38fd1498Szrj __offset > 0 ? __offset / difference_type(_S_buffer_size())
218*38fd1498Szrj : -difference_type((-__offset - 1)
219*38fd1498Szrj / _S_buffer_size()) - 1;
220*38fd1498Szrj _M_set_node(_M_node + __node_offset);
221*38fd1498Szrj _M_cur = _M_first + (__offset - __node_offset
222*38fd1498Szrj * difference_type(_S_buffer_size()));
223*38fd1498Szrj }
224*38fd1498Szrj return *this;
225*38fd1498Szrj }
226*38fd1498Szrj
227*38fd1498Szrj _Self
228*38fd1498Szrj operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
229*38fd1498Szrj {
230*38fd1498Szrj _Self __tmp = *this;
231*38fd1498Szrj return __tmp += __n;
232*38fd1498Szrj }
233*38fd1498Szrj
234*38fd1498Szrj _Self&
235*38fd1498Szrj operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
236*38fd1498Szrj { return *this += -__n; }
237*38fd1498Szrj
238*38fd1498Szrj _Self
239*38fd1498Szrj operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
240*38fd1498Szrj {
241*38fd1498Szrj _Self __tmp = *this;
242*38fd1498Szrj return __tmp -= __n;
243*38fd1498Szrj }
244*38fd1498Szrj
245*38fd1498Szrj reference
246*38fd1498Szrj operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
247*38fd1498Szrj { return *(*this + __n); }
248*38fd1498Szrj
249*38fd1498Szrj /**
250*38fd1498Szrj * Prepares to traverse new_node. Sets everything except
251*38fd1498Szrj * _M_cur, which should therefore be set by the caller
252*38fd1498Szrj * immediately afterwards, based on _M_first and _M_last.
253*38fd1498Szrj */
254*38fd1498Szrj void
255*38fd1498Szrj _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT
256*38fd1498Szrj {
257*38fd1498Szrj _M_node = __new_node;
258*38fd1498Szrj _M_first = *__new_node;
259*38fd1498Szrj _M_last = _M_first + difference_type(_S_buffer_size());
260*38fd1498Szrj }
261*38fd1498Szrj };
262*38fd1498Szrj
263*38fd1498Szrj // Note: we also provide overloads whose operands are of the same type in
264*38fd1498Szrj // order to avoid ambiguous overload resolution when std::rel_ops operators
265*38fd1498Szrj // are in scope (for additional details, see libstdc++/3628)
266*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
267*38fd1498Szrj inline bool
268*38fd1498Szrj operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
269*38fd1498Szrj const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
270*38fd1498Szrj { return __x._M_cur == __y._M_cur; }
271*38fd1498Szrj
272*38fd1498Szrj template<typename _Tp, typename _RefL, typename _PtrL,
273*38fd1498Szrj typename _RefR, typename _PtrR>
274*38fd1498Szrj inline bool
275*38fd1498Szrj operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
276*38fd1498Szrj const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
277*38fd1498Szrj { return __x._M_cur == __y._M_cur; }
278*38fd1498Szrj
279*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
280*38fd1498Szrj inline bool
281*38fd1498Szrj operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
282*38fd1498Szrj const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
283*38fd1498Szrj { return !(__x == __y); }
284*38fd1498Szrj
285*38fd1498Szrj template<typename _Tp, typename _RefL, typename _PtrL,
286*38fd1498Szrj typename _RefR, typename _PtrR>
287*38fd1498Szrj inline bool
288*38fd1498Szrj operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
289*38fd1498Szrj const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
290*38fd1498Szrj { return !(__x == __y); }
291*38fd1498Szrj
292*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
293*38fd1498Szrj inline bool
294*38fd1498Szrj operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
295*38fd1498Szrj const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
296*38fd1498Szrj { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
297*38fd1498Szrj : (__x._M_node < __y._M_node); }
298*38fd1498Szrj
299*38fd1498Szrj template<typename _Tp, typename _RefL, typename _PtrL,
300*38fd1498Szrj typename _RefR, typename _PtrR>
301*38fd1498Szrj inline bool
302*38fd1498Szrj operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
303*38fd1498Szrj const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
304*38fd1498Szrj { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
305*38fd1498Szrj : (__x._M_node < __y._M_node); }
306*38fd1498Szrj
307*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
308*38fd1498Szrj inline bool
309*38fd1498Szrj operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
310*38fd1498Szrj const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
311*38fd1498Szrj { return __y < __x; }
312*38fd1498Szrj
313*38fd1498Szrj template<typename _Tp, typename _RefL, typename _PtrL,
314*38fd1498Szrj typename _RefR, typename _PtrR>
315*38fd1498Szrj inline bool
316*38fd1498Szrj operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
317*38fd1498Szrj const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
318*38fd1498Szrj { return __y < __x; }
319*38fd1498Szrj
320*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
321*38fd1498Szrj inline bool
322*38fd1498Szrj operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
323*38fd1498Szrj const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
324*38fd1498Szrj { return !(__y < __x); }
325*38fd1498Szrj
326*38fd1498Szrj template<typename _Tp, typename _RefL, typename _PtrL,
327*38fd1498Szrj typename _RefR, typename _PtrR>
328*38fd1498Szrj inline bool
329*38fd1498Szrj operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
330*38fd1498Szrj const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
331*38fd1498Szrj { return !(__y < __x); }
332*38fd1498Szrj
333*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
334*38fd1498Szrj inline bool
335*38fd1498Szrj operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
336*38fd1498Szrj const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
337*38fd1498Szrj { return !(__x < __y); }
338*38fd1498Szrj
339*38fd1498Szrj template<typename _Tp, typename _RefL, typename _PtrL,
340*38fd1498Szrj typename _RefR, typename _PtrR>
341*38fd1498Szrj inline bool
342*38fd1498Szrj operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
343*38fd1498Szrj const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
344*38fd1498Szrj { return !(__x < __y); }
345*38fd1498Szrj
346*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
347*38fd1498Szrj // According to the resolution of DR179 not only the various comparison
348*38fd1498Szrj // operators but also operator- must accept mixed iterator/const_iterator
349*38fd1498Szrj // parameters.
350*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
351*38fd1498Szrj inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
352*38fd1498Szrj operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
353*38fd1498Szrj const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
354*38fd1498Szrj {
355*38fd1498Szrj return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
356*38fd1498Szrj (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
357*38fd1498Szrj * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
358*38fd1498Szrj + (__y._M_last - __y._M_cur);
359*38fd1498Szrj }
360*38fd1498Szrj
361*38fd1498Szrj template<typename _Tp, typename _RefL, typename _PtrL,
362*38fd1498Szrj typename _RefR, typename _PtrR>
363*38fd1498Szrj inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
364*38fd1498Szrj operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
365*38fd1498Szrj const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
366*38fd1498Szrj {
367*38fd1498Szrj return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
368*38fd1498Szrj (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
369*38fd1498Szrj * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
370*38fd1498Szrj + (__y._M_last - __y._M_cur);
371*38fd1498Szrj }
372*38fd1498Szrj
373*38fd1498Szrj template<typename _Tp, typename _Ref, typename _Ptr>
374*38fd1498Szrj inline _Deque_iterator<_Tp, _Ref, _Ptr>
375*38fd1498Szrj operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
376*38fd1498Szrj _GLIBCXX_NOEXCEPT
377*38fd1498Szrj { return __x + __n; }
378*38fd1498Szrj
379*38fd1498Szrj template<typename _Tp>
380*38fd1498Szrj void
381*38fd1498Szrj fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&,
382*38fd1498Szrj const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&);
383*38fd1498Szrj
384*38fd1498Szrj template<typename _Tp>
385*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>
386*38fd1498Szrj copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
387*38fd1498Szrj _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
388*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>);
389*38fd1498Szrj
390*38fd1498Szrj template<typename _Tp>
391*38fd1498Szrj inline _Deque_iterator<_Tp, _Tp&, _Tp*>
392*38fd1498Szrj copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
393*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
394*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
395*38fd1498Szrj { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
396*38fd1498Szrj _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
397*38fd1498Szrj __result); }
398*38fd1498Szrj
399*38fd1498Szrj template<typename _Tp>
400*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>
401*38fd1498Szrj copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
402*38fd1498Szrj _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
403*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>);
404*38fd1498Szrj
405*38fd1498Szrj template<typename _Tp>
406*38fd1498Szrj inline _Deque_iterator<_Tp, _Tp&, _Tp*>
407*38fd1498Szrj copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
408*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
409*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
410*38fd1498Szrj { return std::copy_backward(_Deque_iterator<_Tp,
411*38fd1498Szrj const _Tp&, const _Tp*>(__first),
412*38fd1498Szrj _Deque_iterator<_Tp,
413*38fd1498Szrj const _Tp&, const _Tp*>(__last),
414*38fd1498Szrj __result); }
415*38fd1498Szrj
416*38fd1498Szrj #if __cplusplus >= 201103L
417*38fd1498Szrj template<typename _Tp>
418*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>
419*38fd1498Szrj move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
420*38fd1498Szrj _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
421*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>);
422*38fd1498Szrj
423*38fd1498Szrj template<typename _Tp>
424*38fd1498Szrj inline _Deque_iterator<_Tp, _Tp&, _Tp*>
425*38fd1498Szrj move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
426*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
427*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
428*38fd1498Szrj { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
429*38fd1498Szrj _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
430*38fd1498Szrj __result); }
431*38fd1498Szrj
432*38fd1498Szrj template<typename _Tp>
433*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>
434*38fd1498Szrj move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
435*38fd1498Szrj _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
436*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*>);
437*38fd1498Szrj
438*38fd1498Szrj template<typename _Tp>
439*38fd1498Szrj inline _Deque_iterator<_Tp, _Tp&, _Tp*>
440*38fd1498Szrj move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
441*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
442*38fd1498Szrj _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
443*38fd1498Szrj { return std::move_backward(_Deque_iterator<_Tp,
444*38fd1498Szrj const _Tp&, const _Tp*>(__first),
445*38fd1498Szrj _Deque_iterator<_Tp,
446*38fd1498Szrj const _Tp&, const _Tp*>(__last),
447*38fd1498Szrj __result); }
448*38fd1498Szrj #endif
449*38fd1498Szrj
450*38fd1498Szrj /**
451*38fd1498Szrj * Deque base class. This class provides the unified face for %deque's
452*38fd1498Szrj * allocation. This class's constructor and destructor allocate and
453*38fd1498Szrj * deallocate (but do not initialize) storage. This makes %exception
454*38fd1498Szrj * safety easier.
455*38fd1498Szrj *
456*38fd1498Szrj * Nothing in this class ever constructs or destroys an actual Tp element.
457*38fd1498Szrj * (Deque handles that itself.) Only/All memory management is performed
458*38fd1498Szrj * here.
459*38fd1498Szrj */
460*38fd1498Szrj template<typename _Tp, typename _Alloc>
461*38fd1498Szrj class _Deque_base
462*38fd1498Szrj {
463*38fd1498Szrj protected:
464*38fd1498Szrj typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
465*38fd1498Szrj rebind<_Tp>::other _Tp_alloc_type;
466*38fd1498Szrj typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
467*38fd1498Szrj
468*38fd1498Szrj #if __cplusplus < 201103L
469*38fd1498Szrj typedef _Tp* _Ptr;
470*38fd1498Szrj typedef const _Tp* _Ptr_const;
471*38fd1498Szrj #else
472*38fd1498Szrj typedef typename _Alloc_traits::pointer _Ptr;
473*38fd1498Szrj typedef typename _Alloc_traits::const_pointer _Ptr_const;
474*38fd1498Szrj #endif
475*38fd1498Szrj
476*38fd1498Szrj typedef typename _Alloc_traits::template rebind<_Ptr>::other
477*38fd1498Szrj _Map_alloc_type;
478*38fd1498Szrj typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits;
479*38fd1498Szrj
480*38fd1498Szrj public:
481*38fd1498Szrj typedef _Alloc allocator_type;
482*38fd1498Szrj typedef typename _Alloc_traits::size_type size_type;
483*38fd1498Szrj
484*38fd1498Szrj allocator_type
485*38fd1498Szrj get_allocator() const _GLIBCXX_NOEXCEPT
486*38fd1498Szrj { return allocator_type(_M_get_Tp_allocator()); }
487*38fd1498Szrj
488*38fd1498Szrj typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator;
489*38fd1498Szrj typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator;
490*38fd1498Szrj
491*38fd1498Szrj _Deque_base()
492*38fd1498Szrj : _M_impl()
493*38fd1498Szrj { _M_initialize_map(0); }
494*38fd1498Szrj
495*38fd1498Szrj _Deque_base(size_t __num_elements)
496*38fd1498Szrj : _M_impl()
497*38fd1498Szrj { _M_initialize_map(__num_elements); }
498*38fd1498Szrj
499*38fd1498Szrj _Deque_base(const allocator_type& __a, size_t __num_elements)
500*38fd1498Szrj : _M_impl(__a)
501*38fd1498Szrj { _M_initialize_map(__num_elements); }
502*38fd1498Szrj
503*38fd1498Szrj _Deque_base(const allocator_type& __a)
504*38fd1498Szrj : _M_impl(__a)
505*38fd1498Szrj { /* Caller must initialize map. */ }
506*38fd1498Szrj
507*38fd1498Szrj #if __cplusplus >= 201103L
508*38fd1498Szrj _Deque_base(_Deque_base&& __x, false_type)
509*38fd1498Szrj : _M_impl(__x._M_move_impl())
510*38fd1498Szrj { }
511*38fd1498Szrj
512*38fd1498Szrj _Deque_base(_Deque_base&& __x, true_type)
513*38fd1498Szrj : _M_impl(std::move(__x._M_get_Tp_allocator()))
514*38fd1498Szrj {
515*38fd1498Szrj _M_initialize_map(0);
516*38fd1498Szrj if (__x._M_impl._M_map)
517*38fd1498Szrj this->_M_impl._M_swap_data(__x._M_impl);
518*38fd1498Szrj }
519*38fd1498Szrj
520*38fd1498Szrj _Deque_base(_Deque_base&& __x)
521*38fd1498Szrj : _Deque_base(std::move(__x), typename _Alloc_traits::is_always_equal{})
522*38fd1498Szrj { }
523*38fd1498Szrj
524*38fd1498Szrj _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_type __n)
525*38fd1498Szrj : _M_impl(__a)
526*38fd1498Szrj {
527*38fd1498Szrj if (__x.get_allocator() == __a)
528*38fd1498Szrj {
529*38fd1498Szrj if (__x._M_impl._M_map)
530*38fd1498Szrj {
531*38fd1498Szrj _M_initialize_map(0);
532*38fd1498Szrj this->_M_impl._M_swap_data(__x._M_impl);
533*38fd1498Szrj }
534*38fd1498Szrj }
535*38fd1498Szrj else
536*38fd1498Szrj {
537*38fd1498Szrj _M_initialize_map(__n);
538*38fd1498Szrj }
539*38fd1498Szrj }
540*38fd1498Szrj #endif
541*38fd1498Szrj
542*38fd1498Szrj ~_Deque_base() _GLIBCXX_NOEXCEPT;
543*38fd1498Szrj
544*38fd1498Szrj protected:
545*38fd1498Szrj typedef typename iterator::_Map_pointer _Map_pointer;
546*38fd1498Szrj
547*38fd1498Szrj //This struct encapsulates the implementation of the std::deque
548*38fd1498Szrj //standard container and at the same time makes use of the EBO
549*38fd1498Szrj //for empty allocators.
550*38fd1498Szrj struct _Deque_impl
551*38fd1498Szrj : public _Tp_alloc_type
552*38fd1498Szrj {
553*38fd1498Szrj _Map_pointer _M_map;
554*38fd1498Szrj size_t _M_map_size;
555*38fd1498Szrj iterator _M_start;
556*38fd1498Szrj iterator _M_finish;
557*38fd1498Szrj
558*38fd1498Szrj _Deque_impl()
559*38fd1498Szrj : _Tp_alloc_type(), _M_map(), _M_map_size(0),
560*38fd1498Szrj _M_start(), _M_finish()
561*38fd1498Szrj { }
562*38fd1498Szrj
563*38fd1498Szrj _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
564*38fd1498Szrj : _Tp_alloc_type(__a), _M_map(), _M_map_size(0),
565*38fd1498Szrj _M_start(), _M_finish()
566*38fd1498Szrj { }
567*38fd1498Szrj
568*38fd1498Szrj #if __cplusplus >= 201103L
569*38fd1498Szrj _Deque_impl(_Deque_impl&&) = default;
570*38fd1498Szrj
571*38fd1498Szrj _Deque_impl(_Tp_alloc_type&& __a) noexcept
572*38fd1498Szrj : _Tp_alloc_type(std::move(__a)), _M_map(), _M_map_size(0),
573*38fd1498Szrj _M_start(), _M_finish()
574*38fd1498Szrj { }
575*38fd1498Szrj #endif
576*38fd1498Szrj
577*38fd1498Szrj void _M_swap_data(_Deque_impl& __x) _GLIBCXX_NOEXCEPT
578*38fd1498Szrj {
579*38fd1498Szrj using std::swap;
580*38fd1498Szrj swap(this->_M_start, __x._M_start);
581*38fd1498Szrj swap(this->_M_finish, __x._M_finish);
582*38fd1498Szrj swap(this->_M_map, __x._M_map);
583*38fd1498Szrj swap(this->_M_map_size, __x._M_map_size);
584*38fd1498Szrj }
585*38fd1498Szrj };
586*38fd1498Szrj
587*38fd1498Szrj _Tp_alloc_type&
588*38fd1498Szrj _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
589*38fd1498Szrj { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
590*38fd1498Szrj
591*38fd1498Szrj const _Tp_alloc_type&
592*38fd1498Szrj _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
593*38fd1498Szrj { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
594*38fd1498Szrj
595*38fd1498Szrj _Map_alloc_type
596*38fd1498Szrj _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
597*38fd1498Szrj { return _Map_alloc_type(_M_get_Tp_allocator()); }
598*38fd1498Szrj
599*38fd1498Szrj _Ptr
600*38fd1498Szrj _M_allocate_node()
601*38fd1498Szrj {
602*38fd1498Szrj typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
603*38fd1498Szrj return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
604*38fd1498Szrj }
605*38fd1498Szrj
606*38fd1498Szrj void
607*38fd1498Szrj _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
608*38fd1498Szrj {
609*38fd1498Szrj typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
610*38fd1498Szrj _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
611*38fd1498Szrj }
612*38fd1498Szrj
613*38fd1498Szrj _Map_pointer
614*38fd1498Szrj _M_allocate_map(size_t __n)
615*38fd1498Szrj {
616*38fd1498Szrj _Map_alloc_type __map_alloc = _M_get_map_allocator();
617*38fd1498Szrj return _Map_alloc_traits::allocate(__map_alloc, __n);
618*38fd1498Szrj }
619*38fd1498Szrj
620*38fd1498Szrj void
621*38fd1498Szrj _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
622*38fd1498Szrj {
623*38fd1498Szrj _Map_alloc_type __map_alloc = _M_get_map_allocator();
624*38fd1498Szrj _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
625*38fd1498Szrj }
626*38fd1498Szrj
627*38fd1498Szrj protected:
628*38fd1498Szrj void _M_initialize_map(size_t);
629*38fd1498Szrj void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
630*38fd1498Szrj void _M_destroy_nodes(_Map_pointer __nstart,
631*38fd1498Szrj _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
632*38fd1498Szrj enum { _S_initial_map_size = 8 };
633*38fd1498Szrj
634*38fd1498Szrj _Deque_impl _M_impl;
635*38fd1498Szrj
636*38fd1498Szrj #if __cplusplus >= 201103L
637*38fd1498Szrj private:
638*38fd1498Szrj _Deque_impl
639*38fd1498Szrj _M_move_impl()
640*38fd1498Szrj {
641*38fd1498Szrj if (!_M_impl._M_map)
642*38fd1498Szrj return std::move(_M_impl);
643*38fd1498Szrj
644*38fd1498Szrj // Create a copy of the current allocator.
645*38fd1498Szrj _Tp_alloc_type __alloc{_M_get_Tp_allocator()};
646*38fd1498Szrj // Put that copy in a moved-from state.
647*38fd1498Szrj _Tp_alloc_type __sink __attribute((__unused__)) {std::move(__alloc)};
648*38fd1498Szrj // Create an empty map that allocates using the moved-from allocator.
649*38fd1498Szrj _Deque_base __empty{__alloc};
650*38fd1498Szrj __empty._M_initialize_map(0);
651*38fd1498Szrj // Now safe to modify current allocator and perform non-throwing swaps.
652*38fd1498Szrj _Deque_impl __ret{std::move(_M_get_Tp_allocator())};
653*38fd1498Szrj _M_impl._M_swap_data(__ret);
654*38fd1498Szrj _M_impl._M_swap_data(__empty._M_impl);
655*38fd1498Szrj return __ret;
656*38fd1498Szrj }
657*38fd1498Szrj #endif
658*38fd1498Szrj };
659*38fd1498Szrj
660*38fd1498Szrj template<typename _Tp, typename _Alloc>
661*38fd1498Szrj _Deque_base<_Tp, _Alloc>::
662*38fd1498Szrj ~_Deque_base() _GLIBCXX_NOEXCEPT
663*38fd1498Szrj {
664*38fd1498Szrj if (this->_M_impl._M_map)
665*38fd1498Szrj {
666*38fd1498Szrj _M_destroy_nodes(this->_M_impl._M_start._M_node,
667*38fd1498Szrj this->_M_impl._M_finish._M_node + 1);
668*38fd1498Szrj _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
669*38fd1498Szrj }
670*38fd1498Szrj }
671*38fd1498Szrj
672*38fd1498Szrj /**
673*38fd1498Szrj * @brief Layout storage.
674*38fd1498Szrj * @param __num_elements The count of T's for which to allocate space
675*38fd1498Szrj * at first.
676*38fd1498Szrj * @return Nothing.
677*38fd1498Szrj *
678*38fd1498Szrj * The initial underlying memory layout is a bit complicated...
679*38fd1498Szrj */
680*38fd1498Szrj template<typename _Tp, typename _Alloc>
681*38fd1498Szrj void
682*38fd1498Szrj _Deque_base<_Tp, _Alloc>::
683*38fd1498Szrj _M_initialize_map(size_t __num_elements)
684*38fd1498Szrj {
685*38fd1498Szrj const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
686*38fd1498Szrj + 1);
687*38fd1498Szrj
688*38fd1498Szrj this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
689*38fd1498Szrj size_t(__num_nodes + 2));
690*38fd1498Szrj this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
691*38fd1498Szrj
692*38fd1498Szrj // For "small" maps (needing less than _M_map_size nodes), allocation
693*38fd1498Szrj // starts in the middle elements and grows outwards. So nstart may be
694*38fd1498Szrj // the beginning of _M_map, but for small maps it may be as far in as
695*38fd1498Szrj // _M_map+3.
696*38fd1498Szrj
697*38fd1498Szrj _Map_pointer __nstart = (this->_M_impl._M_map
698*38fd1498Szrj + (this->_M_impl._M_map_size - __num_nodes) / 2);
699*38fd1498Szrj _Map_pointer __nfinish = __nstart + __num_nodes;
700*38fd1498Szrj
701*38fd1498Szrj __try
702*38fd1498Szrj { _M_create_nodes(__nstart, __nfinish); }
703*38fd1498Szrj __catch(...)
704*38fd1498Szrj {
705*38fd1498Szrj _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
706*38fd1498Szrj this->_M_impl._M_map = _Map_pointer();
707*38fd1498Szrj this->_M_impl._M_map_size = 0;
708*38fd1498Szrj __throw_exception_again;
709*38fd1498Szrj }
710*38fd1498Szrj
711*38fd1498Szrj this->_M_impl._M_start._M_set_node(__nstart);
712*38fd1498Szrj this->_M_impl._M_finish._M_set_node(__nfinish - 1);
713*38fd1498Szrj this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
714*38fd1498Szrj this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
715*38fd1498Szrj + __num_elements
716*38fd1498Szrj % __deque_buf_size(sizeof(_Tp)));
717*38fd1498Szrj }
718*38fd1498Szrj
719*38fd1498Szrj template<typename _Tp, typename _Alloc>
720*38fd1498Szrj void
721*38fd1498Szrj _Deque_base<_Tp, _Alloc>::
722*38fd1498Szrj _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
723*38fd1498Szrj {
724*38fd1498Szrj _Map_pointer __cur;
725*38fd1498Szrj __try
726*38fd1498Szrj {
727*38fd1498Szrj for (__cur = __nstart; __cur < __nfinish; ++__cur)
728*38fd1498Szrj *__cur = this->_M_allocate_node();
729*38fd1498Szrj }
730*38fd1498Szrj __catch(...)
731*38fd1498Szrj {
732*38fd1498Szrj _M_destroy_nodes(__nstart, __cur);
733*38fd1498Szrj __throw_exception_again;
734*38fd1498Szrj }
735*38fd1498Szrj }
736*38fd1498Szrj
737*38fd1498Szrj template<typename _Tp, typename _Alloc>
738*38fd1498Szrj void
739*38fd1498Szrj _Deque_base<_Tp, _Alloc>::
740*38fd1498Szrj _M_destroy_nodes(_Map_pointer __nstart,
741*38fd1498Szrj _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
742*38fd1498Szrj {
743*38fd1498Szrj for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
744*38fd1498Szrj _M_deallocate_node(*__n);
745*38fd1498Szrj }
746*38fd1498Szrj
747*38fd1498Szrj /**
748*38fd1498Szrj * @brief A standard container using fixed-size memory allocation and
749*38fd1498Szrj * constant-time manipulation of elements at either end.
750*38fd1498Szrj *
751*38fd1498Szrj * @ingroup sequences
752*38fd1498Szrj *
753*38fd1498Szrj * @tparam _Tp Type of element.
754*38fd1498Szrj * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
755*38fd1498Szrj *
756*38fd1498Szrj * Meets the requirements of a <a href="tables.html#65">container</a>, a
757*38fd1498Szrj * <a href="tables.html#66">reversible container</a>, and a
758*38fd1498Szrj * <a href="tables.html#67">sequence</a>, including the
759*38fd1498Szrj * <a href="tables.html#68">optional sequence requirements</a>.
760*38fd1498Szrj *
761*38fd1498Szrj * In previous HP/SGI versions of deque, there was an extra template
762*38fd1498Szrj * parameter so users could control the node size. This extension turned
763*38fd1498Szrj * out to violate the C++ standard (it can be detected using template
764*38fd1498Szrj * template parameters), and it was removed.
765*38fd1498Szrj *
766*38fd1498Szrj * Here's how a deque<Tp> manages memory. Each deque has 4 members:
767*38fd1498Szrj *
768*38fd1498Szrj * - Tp** _M_map
769*38fd1498Szrj * - size_t _M_map_size
770*38fd1498Szrj * - iterator _M_start, _M_finish
771*38fd1498Szrj *
772*38fd1498Szrj * map_size is at least 8. %map is an array of map_size
773*38fd1498Szrj * pointers-to-@a nodes. (The name %map has nothing to do with the
774*38fd1498Szrj * std::map class, and @b nodes should not be confused with
775*38fd1498Szrj * std::list's usage of @a node.)
776*38fd1498Szrj *
777*38fd1498Szrj * A @a node has no specific type name as such, but it is referred
778*38fd1498Szrj * to as @a node in this file. It is a simple array-of-Tp. If Tp
779*38fd1498Szrj * is very large, there will be one Tp element per node (i.e., an
780*38fd1498Szrj * @a array of one). For non-huge Tp's, node size is inversely
781*38fd1498Szrj * related to Tp size: the larger the Tp, the fewer Tp's will fit
782*38fd1498Szrj * in a node. The goal here is to keep the total size of a node
783*38fd1498Szrj * relatively small and constant over different Tp's, to improve
784*38fd1498Szrj * allocator efficiency.
785*38fd1498Szrj *
786*38fd1498Szrj * Not every pointer in the %map array will point to a node. If
787*38fd1498Szrj * the initial number of elements in the deque is small, the
788*38fd1498Szrj * /middle/ %map pointers will be valid, and the ones at the edges
789*38fd1498Szrj * will be unused. This same situation will arise as the %map
790*38fd1498Szrj * grows: available %map pointers, if any, will be on the ends. As
791*38fd1498Szrj * new nodes are created, only a subset of the %map's pointers need
792*38fd1498Szrj * to be copied @a outward.
793*38fd1498Szrj *
794*38fd1498Szrj * Class invariants:
795*38fd1498Szrj * - For any nonsingular iterator i:
796*38fd1498Szrj * - i.node points to a member of the %map array. (Yes, you read that
797*38fd1498Szrj * correctly: i.node does not actually point to a node.) The member of
798*38fd1498Szrj * the %map array is what actually points to the node.
799*38fd1498Szrj * - i.first == *(i.node) (This points to the node (first Tp element).)
800*38fd1498Szrj * - i.last == i.first + node_size
801*38fd1498Szrj * - i.cur is a pointer in the range [i.first, i.last). NOTE:
802*38fd1498Szrj * the implication of this is that i.cur is always a dereferenceable
803*38fd1498Szrj * pointer, even if i is a past-the-end iterator.
804*38fd1498Szrj * - Start and Finish are always nonsingular iterators. NOTE: this
805*38fd1498Szrj * means that an empty deque must have one node, a deque with <N
806*38fd1498Szrj * elements (where N is the node buffer size) must have one node, a
807*38fd1498Szrj * deque with N through (2N-1) elements must have two nodes, etc.
808*38fd1498Szrj * - For every node other than start.node and finish.node, every
809*38fd1498Szrj * element in the node is an initialized object. If start.node ==
810*38fd1498Szrj * finish.node, then [start.cur, finish.cur) are initialized
811*38fd1498Szrj * objects, and the elements outside that range are uninitialized
812*38fd1498Szrj * storage. Otherwise, [start.cur, start.last) and [finish.first,
813*38fd1498Szrj * finish.cur) are initialized objects, and [start.first, start.cur)
814*38fd1498Szrj * and [finish.cur, finish.last) are uninitialized storage.
815*38fd1498Szrj * - [%map, %map + map_size) is a valid, non-empty range.
816*38fd1498Szrj * - [start.node, finish.node] is a valid range contained within
817*38fd1498Szrj * [%map, %map + map_size).
818*38fd1498Szrj * - A pointer in the range [%map, %map + map_size) points to an allocated
819*38fd1498Szrj * node if and only if the pointer is in the range
820*38fd1498Szrj * [start.node, finish.node].
821*38fd1498Szrj *
822*38fd1498Szrj * Here's the magic: nothing in deque is @b aware of the discontiguous
823*38fd1498Szrj * storage!
824*38fd1498Szrj *
825*38fd1498Szrj * The memory setup and layout occurs in the parent, _Base, and the iterator
826*38fd1498Szrj * class is entirely responsible for @a leaping from one node to the next.
827*38fd1498Szrj * All the implementation routines for deque itself work only through the
828*38fd1498Szrj * start and finish iterators. This keeps the routines simple and sane,
829*38fd1498Szrj * and we can use other standard algorithms as well.
830*38fd1498Szrj */
831*38fd1498Szrj template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
832*38fd1498Szrj class deque : protected _Deque_base<_Tp, _Alloc>
833*38fd1498Szrj {
834*38fd1498Szrj #ifdef _GLIBCXX_CONCEPT_CHECKS
835*38fd1498Szrj // concept requirements
836*38fd1498Szrj typedef typename _Alloc::value_type _Alloc_value_type;
837*38fd1498Szrj # if __cplusplus < 201103L
838*38fd1498Szrj __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
839*38fd1498Szrj # endif
840*38fd1498Szrj __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
841*38fd1498Szrj #endif
842*38fd1498Szrj
843*38fd1498Szrj #if __cplusplus >= 201103L
844*38fd1498Szrj static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
845*38fd1498Szrj "std::deque must have a non-const, non-volatile value_type");
846*38fd1498Szrj # ifdef __STRICT_ANSI__
847*38fd1498Szrj static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
848*38fd1498Szrj "std::deque must have the same value_type as its allocator");
849*38fd1498Szrj # endif
850*38fd1498Szrj #endif
851*38fd1498Szrj
852*38fd1498Szrj typedef _Deque_base<_Tp, _Alloc> _Base;
853*38fd1498Szrj typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
854*38fd1498Szrj typedef typename _Base::_Alloc_traits _Alloc_traits;
855*38fd1498Szrj typedef typename _Base::_Map_pointer _Map_pointer;
856*38fd1498Szrj
857*38fd1498Szrj public:
858*38fd1498Szrj typedef _Tp value_type;
859*38fd1498Szrj typedef typename _Alloc_traits::pointer pointer;
860*38fd1498Szrj typedef typename _Alloc_traits::const_pointer const_pointer;
861*38fd1498Szrj typedef typename _Alloc_traits::reference reference;
862*38fd1498Szrj typedef typename _Alloc_traits::const_reference const_reference;
863*38fd1498Szrj typedef typename _Base::iterator iterator;
864*38fd1498Szrj typedef typename _Base::const_iterator const_iterator;
865*38fd1498Szrj typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
866*38fd1498Szrj typedef std::reverse_iterator<iterator> reverse_iterator;
867*38fd1498Szrj typedef size_t size_type;
868*38fd1498Szrj typedef ptrdiff_t difference_type;
869*38fd1498Szrj typedef _Alloc allocator_type;
870*38fd1498Szrj
871*38fd1498Szrj protected:
872*38fd1498Szrj static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
873*38fd1498Szrj { return __deque_buf_size(sizeof(_Tp)); }
874*38fd1498Szrj
875*38fd1498Szrj // Functions controlling memory layout, and nothing else.
876*38fd1498Szrj using _Base::_M_initialize_map;
877*38fd1498Szrj using _Base::_M_create_nodes;
878*38fd1498Szrj using _Base::_M_destroy_nodes;
879*38fd1498Szrj using _Base::_M_allocate_node;
880*38fd1498Szrj using _Base::_M_deallocate_node;
881*38fd1498Szrj using _Base::_M_allocate_map;
882*38fd1498Szrj using _Base::_M_deallocate_map;
883*38fd1498Szrj using _Base::_M_get_Tp_allocator;
884*38fd1498Szrj
885*38fd1498Szrj /**
886*38fd1498Szrj * A total of four data members accumulated down the hierarchy.
887*38fd1498Szrj * May be accessed via _M_impl.*
888*38fd1498Szrj */
889*38fd1498Szrj using _Base::_M_impl;
890*38fd1498Szrj
891*38fd1498Szrj public:
892*38fd1498Szrj // [23.2.1.1] construct/copy/destroy
893*38fd1498Szrj // (assign() and get_allocator() are also listed in this section)
894*38fd1498Szrj
895*38fd1498Szrj /**
896*38fd1498Szrj * @brief Creates a %deque with no elements.
897*38fd1498Szrj */
898*38fd1498Szrj deque() : _Base() { }
899*38fd1498Szrj
900*38fd1498Szrj /**
901*38fd1498Szrj * @brief Creates a %deque with no elements.
902*38fd1498Szrj * @param __a An allocator object.
903*38fd1498Szrj */
904*38fd1498Szrj explicit
905*38fd1498Szrj deque(const allocator_type& __a)
906*38fd1498Szrj : _Base(__a, 0) { }
907*38fd1498Szrj
908*38fd1498Szrj #if __cplusplus >= 201103L
909*38fd1498Szrj /**
910*38fd1498Szrj * @brief Creates a %deque with default constructed elements.
911*38fd1498Szrj * @param __n The number of elements to initially create.
912*38fd1498Szrj * @param __a An allocator.
913*38fd1498Szrj *
914*38fd1498Szrj * This constructor fills the %deque with @a n default
915*38fd1498Szrj * constructed elements.
916*38fd1498Szrj */
917*38fd1498Szrj explicit
918*38fd1498Szrj deque(size_type __n, const allocator_type& __a = allocator_type())
919*38fd1498Szrj : _Base(__a, __n)
920*38fd1498Szrj { _M_default_initialize(); }
921*38fd1498Szrj
922*38fd1498Szrj /**
923*38fd1498Szrj * @brief Creates a %deque with copies of an exemplar element.
924*38fd1498Szrj * @param __n The number of elements to initially create.
925*38fd1498Szrj * @param __value An element to copy.
926*38fd1498Szrj * @param __a An allocator.
927*38fd1498Szrj *
928*38fd1498Szrj * This constructor fills the %deque with @a __n copies of @a __value.
929*38fd1498Szrj */
930*38fd1498Szrj deque(size_type __n, const value_type& __value,
931*38fd1498Szrj const allocator_type& __a = allocator_type())
932*38fd1498Szrj : _Base(__a, __n)
933*38fd1498Szrj { _M_fill_initialize(__value); }
934*38fd1498Szrj #else
935*38fd1498Szrj /**
936*38fd1498Szrj * @brief Creates a %deque with copies of an exemplar element.
937*38fd1498Szrj * @param __n The number of elements to initially create.
938*38fd1498Szrj * @param __value An element to copy.
939*38fd1498Szrj * @param __a An allocator.
940*38fd1498Szrj *
941*38fd1498Szrj * This constructor fills the %deque with @a __n copies of @a __value.
942*38fd1498Szrj */
943*38fd1498Szrj explicit
944*38fd1498Szrj deque(size_type __n, const value_type& __value = value_type(),
945*38fd1498Szrj const allocator_type& __a = allocator_type())
946*38fd1498Szrj : _Base(__a, __n)
947*38fd1498Szrj { _M_fill_initialize(__value); }
948*38fd1498Szrj #endif
949*38fd1498Szrj
950*38fd1498Szrj /**
951*38fd1498Szrj * @brief %Deque copy constructor.
952*38fd1498Szrj * @param __x A %deque of identical element and allocator types.
953*38fd1498Szrj *
954*38fd1498Szrj * The newly-created %deque uses a copy of the allocator object used
955*38fd1498Szrj * by @a __x (unless the allocator traits dictate a different object).
956*38fd1498Szrj */
957*38fd1498Szrj deque(const deque& __x)
958*38fd1498Szrj : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
959*38fd1498Szrj __x.size())
960*38fd1498Szrj { std::__uninitialized_copy_a(__x.begin(), __x.end(),
961*38fd1498Szrj this->_M_impl._M_start,
962*38fd1498Szrj _M_get_Tp_allocator()); }
963*38fd1498Szrj
964*38fd1498Szrj #if __cplusplus >= 201103L
965*38fd1498Szrj /**
966*38fd1498Szrj * @brief %Deque move constructor.
967*38fd1498Szrj * @param __x A %deque of identical element and allocator types.
968*38fd1498Szrj *
969*38fd1498Szrj * The newly-created %deque contains the exact contents of @a __x.
970*38fd1498Szrj * The contents of @a __x are a valid, but unspecified %deque.
971*38fd1498Szrj */
972*38fd1498Szrj deque(deque&& __x)
973*38fd1498Szrj : _Base(std::move(__x)) { }
974*38fd1498Szrj
975*38fd1498Szrj /// Copy constructor with alternative allocator
976*38fd1498Szrj deque(const deque& __x, const allocator_type& __a)
977*38fd1498Szrj : _Base(__a, __x.size())
978*38fd1498Szrj { std::__uninitialized_copy_a(__x.begin(), __x.end(),
979*38fd1498Szrj this->_M_impl._M_start,
980*38fd1498Szrj _M_get_Tp_allocator()); }
981*38fd1498Szrj
982*38fd1498Szrj /// Move constructor with alternative allocator
983*38fd1498Szrj deque(deque&& __x, const allocator_type& __a)
984*38fd1498Szrj : _Base(std::move(__x), __a, __x.size())
985*38fd1498Szrj {
986*38fd1498Szrj if (__x.get_allocator() != __a)
987*38fd1498Szrj {
988*38fd1498Szrj std::__uninitialized_move_a(__x.begin(), __x.end(),
989*38fd1498Szrj this->_M_impl._M_start,
990*38fd1498Szrj _M_get_Tp_allocator());
991*38fd1498Szrj __x.clear();
992*38fd1498Szrj }
993*38fd1498Szrj }
994*38fd1498Szrj
995*38fd1498Szrj /**
996*38fd1498Szrj * @brief Builds a %deque from an initializer list.
997*38fd1498Szrj * @param __l An initializer_list.
998*38fd1498Szrj * @param __a An allocator object.
999*38fd1498Szrj *
1000*38fd1498Szrj * Create a %deque consisting of copies of the elements in the
1001*38fd1498Szrj * initializer_list @a __l.
1002*38fd1498Szrj *
1003*38fd1498Szrj * This will call the element type's copy constructor N times
1004*38fd1498Szrj * (where N is __l.size()) and do no memory reallocation.
1005*38fd1498Szrj */
1006*38fd1498Szrj deque(initializer_list<value_type> __l,
1007*38fd1498Szrj const allocator_type& __a = allocator_type())
1008*38fd1498Szrj : _Base(__a)
1009*38fd1498Szrj {
1010*38fd1498Szrj _M_range_initialize(__l.begin(), __l.end(),
1011*38fd1498Szrj random_access_iterator_tag());
1012*38fd1498Szrj }
1013*38fd1498Szrj #endif
1014*38fd1498Szrj
1015*38fd1498Szrj /**
1016*38fd1498Szrj * @brief Builds a %deque from a range.
1017*38fd1498Szrj * @param __first An input iterator.
1018*38fd1498Szrj * @param __last An input iterator.
1019*38fd1498Szrj * @param __a An allocator object.
1020*38fd1498Szrj *
1021*38fd1498Szrj * Create a %deque consisting of copies of the elements from [__first,
1022*38fd1498Szrj * __last).
1023*38fd1498Szrj *
1024*38fd1498Szrj * If the iterators are forward, bidirectional, or random-access, then
1025*38fd1498Szrj * this will call the elements' copy constructor N times (where N is
1026*38fd1498Szrj * distance(__first,__last)) and do no memory reallocation. But if only
1027*38fd1498Szrj * input iterators are used, then this will do at most 2N calls to the
1028*38fd1498Szrj * copy constructor, and logN memory reallocations.
1029*38fd1498Szrj */
1030*38fd1498Szrj #if __cplusplus >= 201103L
1031*38fd1498Szrj template<typename _InputIterator,
1032*38fd1498Szrj typename = std::_RequireInputIter<_InputIterator>>
1033*38fd1498Szrj deque(_InputIterator __first, _InputIterator __last,
1034*38fd1498Szrj const allocator_type& __a = allocator_type())
1035*38fd1498Szrj : _Base(__a)
1036*38fd1498Szrj { _M_initialize_dispatch(__first, __last, __false_type()); }
1037*38fd1498Szrj #else
1038*38fd1498Szrj template<typename _InputIterator>
1039*38fd1498Szrj deque(_InputIterator __first, _InputIterator __last,
1040*38fd1498Szrj const allocator_type& __a = allocator_type())
1041*38fd1498Szrj : _Base(__a)
1042*38fd1498Szrj {
1043*38fd1498Szrj // Check whether it's an integral type. If so, it's not an iterator.
1044*38fd1498Szrj typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1045*38fd1498Szrj _M_initialize_dispatch(__first, __last, _Integral());
1046*38fd1498Szrj }
1047*38fd1498Szrj #endif
1048*38fd1498Szrj
1049*38fd1498Szrj /**
1050*38fd1498Szrj * The dtor only erases the elements, and note that if the elements
1051*38fd1498Szrj * themselves are pointers, the pointed-to memory is not touched in any
1052*38fd1498Szrj * way. Managing the pointer is the user's responsibility.
1053*38fd1498Szrj */
1054*38fd1498Szrj ~deque()
1055*38fd1498Szrj { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
1056*38fd1498Szrj
1057*38fd1498Szrj /**
1058*38fd1498Szrj * @brief %Deque assignment operator.
1059*38fd1498Szrj * @param __x A %deque of identical element and allocator types.
1060*38fd1498Szrj *
1061*38fd1498Szrj * All the elements of @a x are copied.
1062*38fd1498Szrj *
1063*38fd1498Szrj * The newly-created %deque uses a copy of the allocator object used
1064*38fd1498Szrj * by @a __x (unless the allocator traits dictate a different object).
1065*38fd1498Szrj */
1066*38fd1498Szrj deque&
1067*38fd1498Szrj operator=(const deque& __x);
1068*38fd1498Szrj
1069*38fd1498Szrj #if __cplusplus >= 201103L
1070*38fd1498Szrj /**
1071*38fd1498Szrj * @brief %Deque move assignment operator.
1072*38fd1498Szrj * @param __x A %deque of identical element and allocator types.
1073*38fd1498Szrj *
1074*38fd1498Szrj * The contents of @a __x are moved into this deque (without copying,
1075*38fd1498Szrj * if the allocators permit it).
1076*38fd1498Szrj * @a __x is a valid, but unspecified %deque.
1077*38fd1498Szrj */
1078*38fd1498Szrj deque&
1079*38fd1498Szrj operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
1080*38fd1498Szrj {
1081*38fd1498Szrj using __always_equal = typename _Alloc_traits::is_always_equal;
1082*38fd1498Szrj _M_move_assign1(std::move(__x), __always_equal{});
1083*38fd1498Szrj return *this;
1084*38fd1498Szrj }
1085*38fd1498Szrj
1086*38fd1498Szrj /**
1087*38fd1498Szrj * @brief Assigns an initializer list to a %deque.
1088*38fd1498Szrj * @param __l An initializer_list.
1089*38fd1498Szrj *
1090*38fd1498Szrj * This function fills a %deque with copies of the elements in the
1091*38fd1498Szrj * initializer_list @a __l.
1092*38fd1498Szrj *
1093*38fd1498Szrj * Note that the assignment completely changes the %deque and that the
1094*38fd1498Szrj * resulting %deque's size is the same as the number of elements
1095*38fd1498Szrj * assigned.
1096*38fd1498Szrj */
1097*38fd1498Szrj deque&
1098*38fd1498Szrj operator=(initializer_list<value_type> __l)
1099*38fd1498Szrj {
1100*38fd1498Szrj _M_assign_aux(__l.begin(), __l.end(),
1101*38fd1498Szrj random_access_iterator_tag());
1102*38fd1498Szrj return *this;
1103*38fd1498Szrj }
1104*38fd1498Szrj #endif
1105*38fd1498Szrj
1106*38fd1498Szrj /**
1107*38fd1498Szrj * @brief Assigns a given value to a %deque.
1108*38fd1498Szrj * @param __n Number of elements to be assigned.
1109*38fd1498Szrj * @param __val Value to be assigned.
1110*38fd1498Szrj *
1111*38fd1498Szrj * This function fills a %deque with @a n copies of the given
1112*38fd1498Szrj * value. Note that the assignment completely changes the
1113*38fd1498Szrj * %deque and that the resulting %deque's size is the same as
1114*38fd1498Szrj * the number of elements assigned.
1115*38fd1498Szrj */
1116*38fd1498Szrj void
1117*38fd1498Szrj assign(size_type __n, const value_type& __val)
1118*38fd1498Szrj { _M_fill_assign(__n, __val); }
1119*38fd1498Szrj
1120*38fd1498Szrj /**
1121*38fd1498Szrj * @brief Assigns a range to a %deque.
1122*38fd1498Szrj * @param __first An input iterator.
1123*38fd1498Szrj * @param __last An input iterator.
1124*38fd1498Szrj *
1125*38fd1498Szrj * This function fills a %deque with copies of the elements in the
1126*38fd1498Szrj * range [__first,__last).
1127*38fd1498Szrj *
1128*38fd1498Szrj * Note that the assignment completely changes the %deque and that the
1129*38fd1498Szrj * resulting %deque's size is the same as the number of elements
1130*38fd1498Szrj * assigned.
1131*38fd1498Szrj */
1132*38fd1498Szrj #if __cplusplus >= 201103L
1133*38fd1498Szrj template<typename _InputIterator,
1134*38fd1498Szrj typename = std::_RequireInputIter<_InputIterator>>
1135*38fd1498Szrj void
1136*38fd1498Szrj assign(_InputIterator __first, _InputIterator __last)
1137*38fd1498Szrj { _M_assign_dispatch(__first, __last, __false_type()); }
1138*38fd1498Szrj #else
1139*38fd1498Szrj template<typename _InputIterator>
1140*38fd1498Szrj void
1141*38fd1498Szrj assign(_InputIterator __first, _InputIterator __last)
1142*38fd1498Szrj {
1143*38fd1498Szrj typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1144*38fd1498Szrj _M_assign_dispatch(__first, __last, _Integral());
1145*38fd1498Szrj }
1146*38fd1498Szrj #endif
1147*38fd1498Szrj
1148*38fd1498Szrj #if __cplusplus >= 201103L
1149*38fd1498Szrj /**
1150*38fd1498Szrj * @brief Assigns an initializer list to a %deque.
1151*38fd1498Szrj * @param __l An initializer_list.
1152*38fd1498Szrj *
1153*38fd1498Szrj * This function fills a %deque with copies of the elements in the
1154*38fd1498Szrj * initializer_list @a __l.
1155*38fd1498Szrj *
1156*38fd1498Szrj * Note that the assignment completely changes the %deque and that the
1157*38fd1498Szrj * resulting %deque's size is the same as the number of elements
1158*38fd1498Szrj * assigned.
1159*38fd1498Szrj */
1160*38fd1498Szrj void
1161*38fd1498Szrj assign(initializer_list<value_type> __l)
1162*38fd1498Szrj { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
1163*38fd1498Szrj #endif
1164*38fd1498Szrj
1165*38fd1498Szrj /// Get a copy of the memory allocation object.
1166*38fd1498Szrj allocator_type
1167*38fd1498Szrj get_allocator() const _GLIBCXX_NOEXCEPT
1168*38fd1498Szrj { return _Base::get_allocator(); }
1169*38fd1498Szrj
1170*38fd1498Szrj // iterators
1171*38fd1498Szrj /**
1172*38fd1498Szrj * Returns a read/write iterator that points to the first element in the
1173*38fd1498Szrj * %deque. Iteration is done in ordinary element order.
1174*38fd1498Szrj */
1175*38fd1498Szrj iterator
1176*38fd1498Szrj begin() _GLIBCXX_NOEXCEPT
1177*38fd1498Szrj { return this->_M_impl._M_start; }
1178*38fd1498Szrj
1179*38fd1498Szrj /**
1180*38fd1498Szrj * Returns a read-only (constant) iterator that points to the first
1181*38fd1498Szrj * element in the %deque. Iteration is done in ordinary element order.
1182*38fd1498Szrj */
1183*38fd1498Szrj const_iterator
1184*38fd1498Szrj begin() const _GLIBCXX_NOEXCEPT
1185*38fd1498Szrj { return this->_M_impl._M_start; }
1186*38fd1498Szrj
1187*38fd1498Szrj /**
1188*38fd1498Szrj * Returns a read/write iterator that points one past the last
1189*38fd1498Szrj * element in the %deque. Iteration is done in ordinary
1190*38fd1498Szrj * element order.
1191*38fd1498Szrj */
1192*38fd1498Szrj iterator
1193*38fd1498Szrj end() _GLIBCXX_NOEXCEPT
1194*38fd1498Szrj { return this->_M_impl._M_finish; }
1195*38fd1498Szrj
1196*38fd1498Szrj /**
1197*38fd1498Szrj * Returns a read-only (constant) iterator that points one past
1198*38fd1498Szrj * the last element in the %deque. Iteration is done in
1199*38fd1498Szrj * ordinary element order.
1200*38fd1498Szrj */
1201*38fd1498Szrj const_iterator
1202*38fd1498Szrj end() const _GLIBCXX_NOEXCEPT
1203*38fd1498Szrj { return this->_M_impl._M_finish; }
1204*38fd1498Szrj
1205*38fd1498Szrj /**
1206*38fd1498Szrj * Returns a read/write reverse iterator that points to the
1207*38fd1498Szrj * last element in the %deque. Iteration is done in reverse
1208*38fd1498Szrj * element order.
1209*38fd1498Szrj */
1210*38fd1498Szrj reverse_iterator
1211*38fd1498Szrj rbegin() _GLIBCXX_NOEXCEPT
1212*38fd1498Szrj { return reverse_iterator(this->_M_impl._M_finish); }
1213*38fd1498Szrj
1214*38fd1498Szrj /**
1215*38fd1498Szrj * Returns a read-only (constant) reverse iterator that points
1216*38fd1498Szrj * to the last element in the %deque. Iteration is done in
1217*38fd1498Szrj * reverse element order.
1218*38fd1498Szrj */
1219*38fd1498Szrj const_reverse_iterator
1220*38fd1498Szrj rbegin() const _GLIBCXX_NOEXCEPT
1221*38fd1498Szrj { return const_reverse_iterator(this->_M_impl._M_finish); }
1222*38fd1498Szrj
1223*38fd1498Szrj /**
1224*38fd1498Szrj * Returns a read/write reverse iterator that points to one
1225*38fd1498Szrj * before the first element in the %deque. Iteration is done
1226*38fd1498Szrj * in reverse element order.
1227*38fd1498Szrj */
1228*38fd1498Szrj reverse_iterator
1229*38fd1498Szrj rend() _GLIBCXX_NOEXCEPT
1230*38fd1498Szrj { return reverse_iterator(this->_M_impl._M_start); }
1231*38fd1498Szrj
1232*38fd1498Szrj /**
1233*38fd1498Szrj * Returns a read-only (constant) reverse iterator that points
1234*38fd1498Szrj * to one before the first element in the %deque. Iteration is
1235*38fd1498Szrj * done in reverse element order.
1236*38fd1498Szrj */
1237*38fd1498Szrj const_reverse_iterator
1238*38fd1498Szrj rend() const _GLIBCXX_NOEXCEPT
1239*38fd1498Szrj { return const_reverse_iterator(this->_M_impl._M_start); }
1240*38fd1498Szrj
1241*38fd1498Szrj #if __cplusplus >= 201103L
1242*38fd1498Szrj /**
1243*38fd1498Szrj * Returns a read-only (constant) iterator that points to the first
1244*38fd1498Szrj * element in the %deque. Iteration is done in ordinary element order.
1245*38fd1498Szrj */
1246*38fd1498Szrj const_iterator
1247*38fd1498Szrj cbegin() const noexcept
1248*38fd1498Szrj { return this->_M_impl._M_start; }
1249*38fd1498Szrj
1250*38fd1498Szrj /**
1251*38fd1498Szrj * Returns a read-only (constant) iterator that points one past
1252*38fd1498Szrj * the last element in the %deque. Iteration is done in
1253*38fd1498Szrj * ordinary element order.
1254*38fd1498Szrj */
1255*38fd1498Szrj const_iterator
1256*38fd1498Szrj cend() const noexcept
1257*38fd1498Szrj { return this->_M_impl._M_finish; }
1258*38fd1498Szrj
1259*38fd1498Szrj /**
1260*38fd1498Szrj * Returns a read-only (constant) reverse iterator that points
1261*38fd1498Szrj * to the last element in the %deque. Iteration is done in
1262*38fd1498Szrj * reverse element order.
1263*38fd1498Szrj */
1264*38fd1498Szrj const_reverse_iterator
1265*38fd1498Szrj crbegin() const noexcept
1266*38fd1498Szrj { return const_reverse_iterator(this->_M_impl._M_finish); }
1267*38fd1498Szrj
1268*38fd1498Szrj /**
1269*38fd1498Szrj * Returns a read-only (constant) reverse iterator that points
1270*38fd1498Szrj * to one before the first element in the %deque. Iteration is
1271*38fd1498Szrj * done in reverse element order.
1272*38fd1498Szrj */
1273*38fd1498Szrj const_reverse_iterator
1274*38fd1498Szrj crend() const noexcept
1275*38fd1498Szrj { return const_reverse_iterator(this->_M_impl._M_start); }
1276*38fd1498Szrj #endif
1277*38fd1498Szrj
1278*38fd1498Szrj // [23.2.1.2] capacity
1279*38fd1498Szrj /** Returns the number of elements in the %deque. */
1280*38fd1498Szrj size_type
1281*38fd1498Szrj size() const _GLIBCXX_NOEXCEPT
1282*38fd1498Szrj { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1283*38fd1498Szrj
1284*38fd1498Szrj /** Returns the size() of the largest possible %deque. */
1285*38fd1498Szrj size_type
1286*38fd1498Szrj max_size() const _GLIBCXX_NOEXCEPT
1287*38fd1498Szrj { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
1288*38fd1498Szrj
1289*38fd1498Szrj #if __cplusplus >= 201103L
1290*38fd1498Szrj /**
1291*38fd1498Szrj * @brief Resizes the %deque to the specified number of elements.
1292*38fd1498Szrj * @param __new_size Number of elements the %deque should contain.
1293*38fd1498Szrj *
1294*38fd1498Szrj * This function will %resize the %deque to the specified
1295*38fd1498Szrj * number of elements. If the number is smaller than the
1296*38fd1498Szrj * %deque's current size the %deque is truncated, otherwise
1297*38fd1498Szrj * default constructed elements are appended.
1298*38fd1498Szrj */
1299*38fd1498Szrj void
1300*38fd1498Szrj resize(size_type __new_size)
1301*38fd1498Szrj {
1302*38fd1498Szrj const size_type __len = size();
1303*38fd1498Szrj if (__new_size > __len)
1304*38fd1498Szrj _M_default_append(__new_size - __len);
1305*38fd1498Szrj else if (__new_size < __len)
1306*38fd1498Szrj _M_erase_at_end(this->_M_impl._M_start
1307*38fd1498Szrj + difference_type(__new_size));
1308*38fd1498Szrj }
1309*38fd1498Szrj
1310*38fd1498Szrj /**
1311*38fd1498Szrj * @brief Resizes the %deque to the specified number of elements.
1312*38fd1498Szrj * @param __new_size Number of elements the %deque should contain.
1313*38fd1498Szrj * @param __x Data with which new elements should be populated.
1314*38fd1498Szrj *
1315*38fd1498Szrj * This function will %resize the %deque to the specified
1316*38fd1498Szrj * number of elements. If the number is smaller than the
1317*38fd1498Szrj * %deque's current size the %deque is truncated, otherwise the
1318*38fd1498Szrj * %deque is extended and new elements are populated with given
1319*38fd1498Szrj * data.
1320*38fd1498Szrj */
1321*38fd1498Szrj void
1322*38fd1498Szrj resize(size_type __new_size, const value_type& __x)
1323*38fd1498Szrj {
1324*38fd1498Szrj const size_type __len = size();
1325*38fd1498Szrj if (__new_size > __len)
1326*38fd1498Szrj _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1327*38fd1498Szrj else if (__new_size < __len)
1328*38fd1498Szrj _M_erase_at_end(this->_M_impl._M_start
1329*38fd1498Szrj + difference_type(__new_size));
1330*38fd1498Szrj }
1331*38fd1498Szrj #else
1332*38fd1498Szrj /**
1333*38fd1498Szrj * @brief Resizes the %deque to the specified number of elements.
1334*38fd1498Szrj * @param __new_size Number of elements the %deque should contain.
1335*38fd1498Szrj * @param __x Data with which new elements should be populated.
1336*38fd1498Szrj *
1337*38fd1498Szrj * This function will %resize the %deque to the specified
1338*38fd1498Szrj * number of elements. If the number is smaller than the
1339*38fd1498Szrj * %deque's current size the %deque is truncated, otherwise the
1340*38fd1498Szrj * %deque is extended and new elements are populated with given
1341*38fd1498Szrj * data.
1342*38fd1498Szrj */
1343*38fd1498Szrj void
1344*38fd1498Szrj resize(size_type __new_size, value_type __x = value_type())
1345*38fd1498Szrj {
1346*38fd1498Szrj const size_type __len = size();
1347*38fd1498Szrj if (__new_size > __len)
1348*38fd1498Szrj _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1349*38fd1498Szrj else if (__new_size < __len)
1350*38fd1498Szrj _M_erase_at_end(this->_M_impl._M_start
1351*38fd1498Szrj + difference_type(__new_size));
1352*38fd1498Szrj }
1353*38fd1498Szrj #endif
1354*38fd1498Szrj
1355*38fd1498Szrj #if __cplusplus >= 201103L
1356*38fd1498Szrj /** A non-binding request to reduce memory use. */
1357*38fd1498Szrj void
1358*38fd1498Szrj shrink_to_fit() noexcept
1359*38fd1498Szrj { _M_shrink_to_fit(); }
1360*38fd1498Szrj #endif
1361*38fd1498Szrj
1362*38fd1498Szrj /**
1363*38fd1498Szrj * Returns true if the %deque is empty. (Thus begin() would
1364*38fd1498Szrj * equal end().)
1365*38fd1498Szrj */
1366*38fd1498Szrj bool
1367*38fd1498Szrj empty() const _GLIBCXX_NOEXCEPT
1368*38fd1498Szrj { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1369*38fd1498Szrj
1370*38fd1498Szrj // element access
1371*38fd1498Szrj /**
1372*38fd1498Szrj * @brief Subscript access to the data contained in the %deque.
1373*38fd1498Szrj * @param __n The index of the element for which data should be
1374*38fd1498Szrj * accessed.
1375*38fd1498Szrj * @return Read/write reference to data.
1376*38fd1498Szrj *
1377*38fd1498Szrj * This operator allows for easy, array-style, data access.
1378*38fd1498Szrj * Note that data access with this operator is unchecked and
1379*38fd1498Szrj * out_of_range lookups are not defined. (For checked lookups
1380*38fd1498Szrj * see at().)
1381*38fd1498Szrj */
1382*38fd1498Szrj reference
1383*38fd1498Szrj operator[](size_type __n) _GLIBCXX_NOEXCEPT
1384*38fd1498Szrj {
1385*38fd1498Szrj __glibcxx_requires_subscript(__n);
1386*38fd1498Szrj return this->_M_impl._M_start[difference_type(__n)];
1387*38fd1498Szrj }
1388*38fd1498Szrj
1389*38fd1498Szrj /**
1390*38fd1498Szrj * @brief Subscript access to the data contained in the %deque.
1391*38fd1498Szrj * @param __n The index of the element for which data should be
1392*38fd1498Szrj * accessed.
1393*38fd1498Szrj * @return Read-only (constant) reference to data.
1394*38fd1498Szrj *
1395*38fd1498Szrj * This operator allows for easy, array-style, data access.
1396*38fd1498Szrj * Note that data access with this operator is unchecked and
1397*38fd1498Szrj * out_of_range lookups are not defined. (For checked lookups
1398*38fd1498Szrj * see at().)
1399*38fd1498Szrj */
1400*38fd1498Szrj const_reference
1401*38fd1498Szrj operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1402*38fd1498Szrj {
1403*38fd1498Szrj __glibcxx_requires_subscript(__n);
1404*38fd1498Szrj return this->_M_impl._M_start[difference_type(__n)];
1405*38fd1498Szrj }
1406*38fd1498Szrj
1407*38fd1498Szrj protected:
1408*38fd1498Szrj /// Safety check used only from at().
1409*38fd1498Szrj void
1410*38fd1498Szrj _M_range_check(size_type __n) const
1411*38fd1498Szrj {
1412*38fd1498Szrj if (__n >= this->size())
1413*38fd1498Szrj __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1414*38fd1498Szrj "(which is %zu)>= this->size() "
1415*38fd1498Szrj "(which is %zu)"),
1416*38fd1498Szrj __n, this->size());
1417*38fd1498Szrj }
1418*38fd1498Szrj
1419*38fd1498Szrj public:
1420*38fd1498Szrj /**
1421*38fd1498Szrj * @brief Provides access to the data contained in the %deque.
1422*38fd1498Szrj * @param __n The index of the element for which data should be
1423*38fd1498Szrj * accessed.
1424*38fd1498Szrj * @return Read/write reference to data.
1425*38fd1498Szrj * @throw std::out_of_range If @a __n is an invalid index.
1426*38fd1498Szrj *
1427*38fd1498Szrj * This function provides for safer data access. The parameter
1428*38fd1498Szrj * is first checked that it is in the range of the deque. The
1429*38fd1498Szrj * function throws out_of_range if the check fails.
1430*38fd1498Szrj */
1431*38fd1498Szrj reference
1432*38fd1498Szrj at(size_type __n)
1433*38fd1498Szrj {
1434*38fd1498Szrj _M_range_check(__n);
1435*38fd1498Szrj return (*this)[__n];
1436*38fd1498Szrj }
1437*38fd1498Szrj
1438*38fd1498Szrj /**
1439*38fd1498Szrj * @brief Provides access to the data contained in the %deque.
1440*38fd1498Szrj * @param __n The index of the element for which data should be
1441*38fd1498Szrj * accessed.
1442*38fd1498Szrj * @return Read-only (constant) reference to data.
1443*38fd1498Szrj * @throw std::out_of_range If @a __n is an invalid index.
1444*38fd1498Szrj *
1445*38fd1498Szrj * This function provides for safer data access. The parameter is first
1446*38fd1498Szrj * checked that it is in the range of the deque. The function throws
1447*38fd1498Szrj * out_of_range if the check fails.
1448*38fd1498Szrj */
1449*38fd1498Szrj const_reference
1450*38fd1498Szrj at(size_type __n) const
1451*38fd1498Szrj {
1452*38fd1498Szrj _M_range_check(__n);
1453*38fd1498Szrj return (*this)[__n];
1454*38fd1498Szrj }
1455*38fd1498Szrj
1456*38fd1498Szrj /**
1457*38fd1498Szrj * Returns a read/write reference to the data at the first
1458*38fd1498Szrj * element of the %deque.
1459*38fd1498Szrj */
1460*38fd1498Szrj reference
1461*38fd1498Szrj front() _GLIBCXX_NOEXCEPT
1462*38fd1498Szrj {
1463*38fd1498Szrj __glibcxx_requires_nonempty();
1464*38fd1498Szrj return *begin();
1465*38fd1498Szrj }
1466*38fd1498Szrj
1467*38fd1498Szrj /**
1468*38fd1498Szrj * Returns a read-only (constant) reference to the data at the first
1469*38fd1498Szrj * element of the %deque.
1470*38fd1498Szrj */
1471*38fd1498Szrj const_reference
1472*38fd1498Szrj front() const _GLIBCXX_NOEXCEPT
1473*38fd1498Szrj {
1474*38fd1498Szrj __glibcxx_requires_nonempty();
1475*38fd1498Szrj return *begin();
1476*38fd1498Szrj }
1477*38fd1498Szrj
1478*38fd1498Szrj /**
1479*38fd1498Szrj * Returns a read/write reference to the data at the last element of the
1480*38fd1498Szrj * %deque.
1481*38fd1498Szrj */
1482*38fd1498Szrj reference
1483*38fd1498Szrj back() _GLIBCXX_NOEXCEPT
1484*38fd1498Szrj {
1485*38fd1498Szrj __glibcxx_requires_nonempty();
1486*38fd1498Szrj iterator __tmp = end();
1487*38fd1498Szrj --__tmp;
1488*38fd1498Szrj return *__tmp;
1489*38fd1498Szrj }
1490*38fd1498Szrj
1491*38fd1498Szrj /**
1492*38fd1498Szrj * Returns a read-only (constant) reference to the data at the last
1493*38fd1498Szrj * element of the %deque.
1494*38fd1498Szrj */
1495*38fd1498Szrj const_reference
1496*38fd1498Szrj back() const _GLIBCXX_NOEXCEPT
1497*38fd1498Szrj {
1498*38fd1498Szrj __glibcxx_requires_nonempty();
1499*38fd1498Szrj const_iterator __tmp = end();
1500*38fd1498Szrj --__tmp;
1501*38fd1498Szrj return *__tmp;
1502*38fd1498Szrj }
1503*38fd1498Szrj
1504*38fd1498Szrj // [23.2.1.2] modifiers
1505*38fd1498Szrj /**
1506*38fd1498Szrj * @brief Add data to the front of the %deque.
1507*38fd1498Szrj * @param __x Data to be added.
1508*38fd1498Szrj *
1509*38fd1498Szrj * This is a typical stack operation. The function creates an
1510*38fd1498Szrj * element at the front of the %deque and assigns the given
1511*38fd1498Szrj * data to it. Due to the nature of a %deque this operation
1512*38fd1498Szrj * can be done in constant time.
1513*38fd1498Szrj */
1514*38fd1498Szrj void
1515*38fd1498Szrj push_front(const value_type& __x)
1516*38fd1498Szrj {
1517*38fd1498Szrj if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1518*38fd1498Szrj {
1519*38fd1498Szrj _Alloc_traits::construct(this->_M_impl,
1520*38fd1498Szrj this->_M_impl._M_start._M_cur - 1,
1521*38fd1498Szrj __x);
1522*38fd1498Szrj --this->_M_impl._M_start._M_cur;
1523*38fd1498Szrj }
1524*38fd1498Szrj else
1525*38fd1498Szrj _M_push_front_aux(__x);
1526*38fd1498Szrj }
1527*38fd1498Szrj
1528*38fd1498Szrj #if __cplusplus >= 201103L
1529*38fd1498Szrj void
1530*38fd1498Szrj push_front(value_type&& __x)
1531*38fd1498Szrj { emplace_front(std::move(__x)); }
1532*38fd1498Szrj
1533*38fd1498Szrj template<typename... _Args>
1534*38fd1498Szrj #if __cplusplus > 201402L
1535*38fd1498Szrj reference
1536*38fd1498Szrj #else
1537*38fd1498Szrj void
1538*38fd1498Szrj #endif
1539*38fd1498Szrj emplace_front(_Args&&... __args);
1540*38fd1498Szrj #endif
1541*38fd1498Szrj
1542*38fd1498Szrj /**
1543*38fd1498Szrj * @brief Add data to the end of the %deque.
1544*38fd1498Szrj * @param __x Data to be added.
1545*38fd1498Szrj *
1546*38fd1498Szrj * This is a typical stack operation. The function creates an
1547*38fd1498Szrj * element at the end of the %deque and assigns the given data
1548*38fd1498Szrj * to it. Due to the nature of a %deque this operation can be
1549*38fd1498Szrj * done in constant time.
1550*38fd1498Szrj */
1551*38fd1498Szrj void
1552*38fd1498Szrj push_back(const value_type& __x)
1553*38fd1498Szrj {
1554*38fd1498Szrj if (this->_M_impl._M_finish._M_cur
1555*38fd1498Szrj != this->_M_impl._M_finish._M_last - 1)
1556*38fd1498Szrj {
1557*38fd1498Szrj _Alloc_traits::construct(this->_M_impl,
1558*38fd1498Szrj this->_M_impl._M_finish._M_cur, __x);
1559*38fd1498Szrj ++this->_M_impl._M_finish._M_cur;
1560*38fd1498Szrj }
1561*38fd1498Szrj else
1562*38fd1498Szrj _M_push_back_aux(__x);
1563*38fd1498Szrj }
1564*38fd1498Szrj
1565*38fd1498Szrj #if __cplusplus >= 201103L
1566*38fd1498Szrj void
1567*38fd1498Szrj push_back(value_type&& __x)
1568*38fd1498Szrj { emplace_back(std::move(__x)); }
1569*38fd1498Szrj
1570*38fd1498Szrj template<typename... _Args>
1571*38fd1498Szrj #if __cplusplus > 201402L
1572*38fd1498Szrj reference
1573*38fd1498Szrj #else
1574*38fd1498Szrj void
1575*38fd1498Szrj #endif
1576*38fd1498Szrj emplace_back(_Args&&... __args);
1577*38fd1498Szrj #endif
1578*38fd1498Szrj
1579*38fd1498Szrj /**
1580*38fd1498Szrj * @brief Removes first element.
1581*38fd1498Szrj *
1582*38fd1498Szrj * This is a typical stack operation. It shrinks the %deque by one.
1583*38fd1498Szrj *
1584*38fd1498Szrj * Note that no data is returned, and if the first element's data is
1585*38fd1498Szrj * needed, it should be retrieved before pop_front() is called.
1586*38fd1498Szrj */
1587*38fd1498Szrj void
1588*38fd1498Szrj pop_front() _GLIBCXX_NOEXCEPT
1589*38fd1498Szrj {
1590*38fd1498Szrj __glibcxx_requires_nonempty();
1591*38fd1498Szrj if (this->_M_impl._M_start._M_cur
1592*38fd1498Szrj != this->_M_impl._M_start._M_last - 1)
1593*38fd1498Szrj {
1594*38fd1498Szrj _Alloc_traits::destroy(this->_M_impl,
1595*38fd1498Szrj this->_M_impl._M_start._M_cur);
1596*38fd1498Szrj ++this->_M_impl._M_start._M_cur;
1597*38fd1498Szrj }
1598*38fd1498Szrj else
1599*38fd1498Szrj _M_pop_front_aux();
1600*38fd1498Szrj }
1601*38fd1498Szrj
1602*38fd1498Szrj /**
1603*38fd1498Szrj * @brief Removes last element.
1604*38fd1498Szrj *
1605*38fd1498Szrj * This is a typical stack operation. It shrinks the %deque by one.
1606*38fd1498Szrj *
1607*38fd1498Szrj * Note that no data is returned, and if the last element's data is
1608*38fd1498Szrj * needed, it should be retrieved before pop_back() is called.
1609*38fd1498Szrj */
1610*38fd1498Szrj void
1611*38fd1498Szrj pop_back() _GLIBCXX_NOEXCEPT
1612*38fd1498Szrj {
1613*38fd1498Szrj __glibcxx_requires_nonempty();
1614*38fd1498Szrj if (this->_M_impl._M_finish._M_cur
1615*38fd1498Szrj != this->_M_impl._M_finish._M_first)
1616*38fd1498Szrj {
1617*38fd1498Szrj --this->_M_impl._M_finish._M_cur;
1618*38fd1498Szrj _Alloc_traits::destroy(this->_M_impl,
1619*38fd1498Szrj this->_M_impl._M_finish._M_cur);
1620*38fd1498Szrj }
1621*38fd1498Szrj else
1622*38fd1498Szrj _M_pop_back_aux();
1623*38fd1498Szrj }
1624*38fd1498Szrj
1625*38fd1498Szrj #if __cplusplus >= 201103L
1626*38fd1498Szrj /**
1627*38fd1498Szrj * @brief Inserts an object in %deque before specified iterator.
1628*38fd1498Szrj * @param __position A const_iterator into the %deque.
1629*38fd1498Szrj * @param __args Arguments.
1630*38fd1498Szrj * @return An iterator that points to the inserted data.
1631*38fd1498Szrj *
1632*38fd1498Szrj * This function will insert an object of type T constructed
1633*38fd1498Szrj * with T(std::forward<Args>(args)...) before the specified location.
1634*38fd1498Szrj */
1635*38fd1498Szrj template<typename... _Args>
1636*38fd1498Szrj iterator
1637*38fd1498Szrj emplace(const_iterator __position, _Args&&... __args);
1638*38fd1498Szrj
1639*38fd1498Szrj /**
1640*38fd1498Szrj * @brief Inserts given value into %deque before specified iterator.
1641*38fd1498Szrj * @param __position A const_iterator into the %deque.
1642*38fd1498Szrj * @param __x Data to be inserted.
1643*38fd1498Szrj * @return An iterator that points to the inserted data.
1644*38fd1498Szrj *
1645*38fd1498Szrj * This function will insert a copy of the given value before the
1646*38fd1498Szrj * specified location.
1647*38fd1498Szrj */
1648*38fd1498Szrj iterator
1649*38fd1498Szrj insert(const_iterator __position, const value_type& __x);
1650*38fd1498Szrj #else
1651*38fd1498Szrj /**
1652*38fd1498Szrj * @brief Inserts given value into %deque before specified iterator.
1653*38fd1498Szrj * @param __position An iterator into the %deque.
1654*38fd1498Szrj * @param __x Data to be inserted.
1655*38fd1498Szrj * @return An iterator that points to the inserted data.
1656*38fd1498Szrj *
1657*38fd1498Szrj * This function will insert a copy of the given value before the
1658*38fd1498Szrj * specified location.
1659*38fd1498Szrj */
1660*38fd1498Szrj iterator
1661*38fd1498Szrj insert(iterator __position, const value_type& __x);
1662*38fd1498Szrj #endif
1663*38fd1498Szrj
1664*38fd1498Szrj #if __cplusplus >= 201103L
1665*38fd1498Szrj /**
1666*38fd1498Szrj * @brief Inserts given rvalue into %deque before specified iterator.
1667*38fd1498Szrj * @param __position A const_iterator into the %deque.
1668*38fd1498Szrj * @param __x Data to be inserted.
1669*38fd1498Szrj * @return An iterator that points to the inserted data.
1670*38fd1498Szrj *
1671*38fd1498Szrj * This function will insert a copy of the given rvalue before the
1672*38fd1498Szrj * specified location.
1673*38fd1498Szrj */
1674*38fd1498Szrj iterator
1675*38fd1498Szrj insert(const_iterator __position, value_type&& __x)
1676*38fd1498Szrj { return emplace(__position, std::move(__x)); }
1677*38fd1498Szrj
1678*38fd1498Szrj /**
1679*38fd1498Szrj * @brief Inserts an initializer list into the %deque.
1680*38fd1498Szrj * @param __p An iterator into the %deque.
1681*38fd1498Szrj * @param __l An initializer_list.
1682*38fd1498Szrj *
1683*38fd1498Szrj * This function will insert copies of the data in the
1684*38fd1498Szrj * initializer_list @a __l into the %deque before the location
1685*38fd1498Szrj * specified by @a __p. This is known as <em>list insert</em>.
1686*38fd1498Szrj */
1687*38fd1498Szrj iterator
1688*38fd1498Szrj insert(const_iterator __p, initializer_list<value_type> __l)
1689*38fd1498Szrj {
1690*38fd1498Szrj auto __offset = __p - cbegin();
1691*38fd1498Szrj _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(),
1692*38fd1498Szrj std::random_access_iterator_tag());
1693*38fd1498Szrj return begin() + __offset;
1694*38fd1498Szrj }
1695*38fd1498Szrj #endif
1696*38fd1498Szrj
1697*38fd1498Szrj #if __cplusplus >= 201103L
1698*38fd1498Szrj /**
1699*38fd1498Szrj * @brief Inserts a number of copies of given data into the %deque.
1700*38fd1498Szrj * @param __position A const_iterator into the %deque.
1701*38fd1498Szrj * @param __n Number of elements to be inserted.
1702*38fd1498Szrj * @param __x Data to be inserted.
1703*38fd1498Szrj * @return An iterator that points to the inserted data.
1704*38fd1498Szrj *
1705*38fd1498Szrj * This function will insert a specified number of copies of the given
1706*38fd1498Szrj * data before the location specified by @a __position.
1707*38fd1498Szrj */
1708*38fd1498Szrj iterator
1709*38fd1498Szrj insert(const_iterator __position, size_type __n, const value_type& __x)
1710*38fd1498Szrj {
1711*38fd1498Szrj difference_type __offset = __position - cbegin();
1712*38fd1498Szrj _M_fill_insert(__position._M_const_cast(), __n, __x);
1713*38fd1498Szrj return begin() + __offset;
1714*38fd1498Szrj }
1715*38fd1498Szrj #else
1716*38fd1498Szrj /**
1717*38fd1498Szrj * @brief Inserts a number of copies of given data into the %deque.
1718*38fd1498Szrj * @param __position An iterator into the %deque.
1719*38fd1498Szrj * @param __n Number of elements to be inserted.
1720*38fd1498Szrj * @param __x Data to be inserted.
1721*38fd1498Szrj *
1722*38fd1498Szrj * This function will insert a specified number of copies of the given
1723*38fd1498Szrj * data before the location specified by @a __position.
1724*38fd1498Szrj */
1725*38fd1498Szrj void
1726*38fd1498Szrj insert(iterator __position, size_type __n, const value_type& __x)
1727*38fd1498Szrj { _M_fill_insert(__position, __n, __x); }
1728*38fd1498Szrj #endif
1729*38fd1498Szrj
1730*38fd1498Szrj #if __cplusplus >= 201103L
1731*38fd1498Szrj /**
1732*38fd1498Szrj * @brief Inserts a range into the %deque.
1733*38fd1498Szrj * @param __position A const_iterator into the %deque.
1734*38fd1498Szrj * @param __first An input iterator.
1735*38fd1498Szrj * @param __last An input iterator.
1736*38fd1498Szrj * @return An iterator that points to the inserted data.
1737*38fd1498Szrj *
1738*38fd1498Szrj * This function will insert copies of the data in the range
1739*38fd1498Szrj * [__first,__last) into the %deque before the location specified
1740*38fd1498Szrj * by @a __position. This is known as <em>range insert</em>.
1741*38fd1498Szrj */
1742*38fd1498Szrj template<typename _InputIterator,
1743*38fd1498Szrj typename = std::_RequireInputIter<_InputIterator>>
1744*38fd1498Szrj iterator
1745*38fd1498Szrj insert(const_iterator __position, _InputIterator __first,
1746*38fd1498Szrj _InputIterator __last)
1747*38fd1498Szrj {
1748*38fd1498Szrj difference_type __offset = __position - cbegin();
1749*38fd1498Szrj _M_insert_dispatch(__position._M_const_cast(),
1750*38fd1498Szrj __first, __last, __false_type());
1751*38fd1498Szrj return begin() + __offset;
1752*38fd1498Szrj }
1753*38fd1498Szrj #else
1754*38fd1498Szrj /**
1755*38fd1498Szrj * @brief Inserts a range into the %deque.
1756*38fd1498Szrj * @param __position An iterator into the %deque.
1757*38fd1498Szrj * @param __first An input iterator.
1758*38fd1498Szrj * @param __last An input iterator.
1759*38fd1498Szrj *
1760*38fd1498Szrj * This function will insert copies of the data in the range
1761*38fd1498Szrj * [__first,__last) into the %deque before the location specified
1762*38fd1498Szrj * by @a __position. This is known as <em>range insert</em>.
1763*38fd1498Szrj */
1764*38fd1498Szrj template<typename _InputIterator>
1765*38fd1498Szrj void
1766*38fd1498Szrj insert(iterator __position, _InputIterator __first,
1767*38fd1498Szrj _InputIterator __last)
1768*38fd1498Szrj {
1769*38fd1498Szrj // Check whether it's an integral type. If so, it's not an iterator.
1770*38fd1498Szrj typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1771*38fd1498Szrj _M_insert_dispatch(__position, __first, __last, _Integral());
1772*38fd1498Szrj }
1773*38fd1498Szrj #endif
1774*38fd1498Szrj
1775*38fd1498Szrj /**
1776*38fd1498Szrj * @brief Remove element at given position.
1777*38fd1498Szrj * @param __position Iterator pointing to element to be erased.
1778*38fd1498Szrj * @return An iterator pointing to the next element (or end()).
1779*38fd1498Szrj *
1780*38fd1498Szrj * This function will erase the element at the given position and thus
1781*38fd1498Szrj * shorten the %deque by one.
1782*38fd1498Szrj *
1783*38fd1498Szrj * The user is cautioned that
1784*38fd1498Szrj * this function only erases the element, and that if the element is
1785*38fd1498Szrj * itself a pointer, the pointed-to memory is not touched in any way.
1786*38fd1498Szrj * Managing the pointer is the user's responsibility.
1787*38fd1498Szrj */
1788*38fd1498Szrj iterator
1789*38fd1498Szrj #if __cplusplus >= 201103L
1790*38fd1498Szrj erase(const_iterator __position)
1791*38fd1498Szrj #else
1792*38fd1498Szrj erase(iterator __position)
1793*38fd1498Szrj #endif
1794*38fd1498Szrj { return _M_erase(__position._M_const_cast()); }
1795*38fd1498Szrj
1796*38fd1498Szrj /**
1797*38fd1498Szrj * @brief Remove a range of elements.
1798*38fd1498Szrj * @param __first Iterator pointing to the first element to be erased.
1799*38fd1498Szrj * @param __last Iterator pointing to one past the last element to be
1800*38fd1498Szrj * erased.
1801*38fd1498Szrj * @return An iterator pointing to the element pointed to by @a last
1802*38fd1498Szrj * prior to erasing (or end()).
1803*38fd1498Szrj *
1804*38fd1498Szrj * This function will erase the elements in the range
1805*38fd1498Szrj * [__first,__last) and shorten the %deque accordingly.
1806*38fd1498Szrj *
1807*38fd1498Szrj * The user is cautioned that
1808*38fd1498Szrj * this function only erases the elements, and that if the elements
1809*38fd1498Szrj * themselves are pointers, the pointed-to memory is not touched in any
1810*38fd1498Szrj * way. Managing the pointer is the user's responsibility.
1811*38fd1498Szrj */
1812*38fd1498Szrj iterator
1813*38fd1498Szrj #if __cplusplus >= 201103L
1814*38fd1498Szrj erase(const_iterator __first, const_iterator __last)
1815*38fd1498Szrj #else
1816*38fd1498Szrj erase(iterator __first, iterator __last)
1817*38fd1498Szrj #endif
1818*38fd1498Szrj { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1819*38fd1498Szrj
1820*38fd1498Szrj /**
1821*38fd1498Szrj * @brief Swaps data with another %deque.
1822*38fd1498Szrj * @param __x A %deque of the same element and allocator types.
1823*38fd1498Szrj *
1824*38fd1498Szrj * This exchanges the elements between two deques in constant time.
1825*38fd1498Szrj * (Four pointers, so it should be quite fast.)
1826*38fd1498Szrj * Note that the global std::swap() function is specialized such that
1827*38fd1498Szrj * std::swap(d1,d2) will feed to this function.
1828*38fd1498Szrj *
1829*38fd1498Szrj * Whether the allocators are swapped depends on the allocator traits.
1830*38fd1498Szrj */
1831*38fd1498Szrj void
1832*38fd1498Szrj swap(deque& __x) _GLIBCXX_NOEXCEPT
1833*38fd1498Szrj {
1834*38fd1498Szrj #if __cplusplus >= 201103L
1835*38fd1498Szrj __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1836*38fd1498Szrj || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
1837*38fd1498Szrj #endif
1838*38fd1498Szrj _M_impl._M_swap_data(__x._M_impl);
1839*38fd1498Szrj _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1840*38fd1498Szrj __x._M_get_Tp_allocator());
1841*38fd1498Szrj }
1842*38fd1498Szrj
1843*38fd1498Szrj /**
1844*38fd1498Szrj * Erases all the elements. Note that this function only erases the
1845*38fd1498Szrj * elements, and that if the elements themselves are pointers, the
1846*38fd1498Szrj * pointed-to memory is not touched in any way. Managing the pointer is
1847*38fd1498Szrj * the user's responsibility.
1848*38fd1498Szrj */
1849*38fd1498Szrj void
1850*38fd1498Szrj clear() _GLIBCXX_NOEXCEPT
1851*38fd1498Szrj { _M_erase_at_end(begin()); }
1852*38fd1498Szrj
1853*38fd1498Szrj protected:
1854*38fd1498Szrj // Internal constructor functions follow.
1855*38fd1498Szrj
1856*38fd1498Szrj // called by the range constructor to implement [23.1.1]/9
1857*38fd1498Szrj
1858*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
1859*38fd1498Szrj // 438. Ambiguity in the "do the right thing" clause
1860*38fd1498Szrj template<typename _Integer>
1861*38fd1498Szrj void
1862*38fd1498Szrj _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1863*38fd1498Szrj {
1864*38fd1498Szrj _M_initialize_map(static_cast<size_type>(__n));
1865*38fd1498Szrj _M_fill_initialize(__x);
1866*38fd1498Szrj }
1867*38fd1498Szrj
1868*38fd1498Szrj // called by the range constructor to implement [23.1.1]/9
1869*38fd1498Szrj template<typename _InputIterator>
1870*38fd1498Szrj void
1871*38fd1498Szrj _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1872*38fd1498Szrj __false_type)
1873*38fd1498Szrj {
1874*38fd1498Szrj _M_range_initialize(__first, __last,
1875*38fd1498Szrj std::__iterator_category(__first));
1876*38fd1498Szrj }
1877*38fd1498Szrj
1878*38fd1498Szrj // called by the second initialize_dispatch above
1879*38fd1498Szrj //@{
1880*38fd1498Szrj /**
1881*38fd1498Szrj * @brief Fills the deque with whatever is in [first,last).
1882*38fd1498Szrj * @param __first An input iterator.
1883*38fd1498Szrj * @param __last An input iterator.
1884*38fd1498Szrj * @return Nothing.
1885*38fd1498Szrj *
1886*38fd1498Szrj * If the iterators are actually forward iterators (or better), then the
1887*38fd1498Szrj * memory layout can be done all at once. Else we move forward using
1888*38fd1498Szrj * push_back on each value from the iterator.
1889*38fd1498Szrj */
1890*38fd1498Szrj template<typename _InputIterator>
1891*38fd1498Szrj void
1892*38fd1498Szrj _M_range_initialize(_InputIterator __first, _InputIterator __last,
1893*38fd1498Szrj std::input_iterator_tag);
1894*38fd1498Szrj
1895*38fd1498Szrj // called by the second initialize_dispatch above
1896*38fd1498Szrj template<typename _ForwardIterator>
1897*38fd1498Szrj void
1898*38fd1498Szrj _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1899*38fd1498Szrj std::forward_iterator_tag);
1900*38fd1498Szrj //@}
1901*38fd1498Szrj
1902*38fd1498Szrj /**
1903*38fd1498Szrj * @brief Fills the %deque with copies of value.
1904*38fd1498Szrj * @param __value Initial value.
1905*38fd1498Szrj * @return Nothing.
1906*38fd1498Szrj * @pre _M_start and _M_finish have already been initialized,
1907*38fd1498Szrj * but none of the %deque's elements have yet been constructed.
1908*38fd1498Szrj *
1909*38fd1498Szrj * This function is called only when the user provides an explicit size
1910*38fd1498Szrj * (with or without an explicit exemplar value).
1911*38fd1498Szrj */
1912*38fd1498Szrj void
1913*38fd1498Szrj _M_fill_initialize(const value_type& __value);
1914*38fd1498Szrj
1915*38fd1498Szrj #if __cplusplus >= 201103L
1916*38fd1498Szrj // called by deque(n).
1917*38fd1498Szrj void
1918*38fd1498Szrj _M_default_initialize();
1919*38fd1498Szrj #endif
1920*38fd1498Szrj
1921*38fd1498Szrj // Internal assign functions follow. The *_aux functions do the actual
1922*38fd1498Szrj // assignment work for the range versions.
1923*38fd1498Szrj
1924*38fd1498Szrj // called by the range assign to implement [23.1.1]/9
1925*38fd1498Szrj
1926*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
1927*38fd1498Szrj // 438. Ambiguity in the "do the right thing" clause
1928*38fd1498Szrj template<typename _Integer>
1929*38fd1498Szrj void
1930*38fd1498Szrj _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1931*38fd1498Szrj { _M_fill_assign(__n, __val); }
1932*38fd1498Szrj
1933*38fd1498Szrj // called by the range assign to implement [23.1.1]/9
1934*38fd1498Szrj template<typename _InputIterator>
1935*38fd1498Szrj void
1936*38fd1498Szrj _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1937*38fd1498Szrj __false_type)
1938*38fd1498Szrj { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1939*38fd1498Szrj
1940*38fd1498Szrj // called by the second assign_dispatch above
1941*38fd1498Szrj template<typename _InputIterator>
1942*38fd1498Szrj void
1943*38fd1498Szrj _M_assign_aux(_InputIterator __first, _InputIterator __last,
1944*38fd1498Szrj std::input_iterator_tag);
1945*38fd1498Szrj
1946*38fd1498Szrj // called by the second assign_dispatch above
1947*38fd1498Szrj template<typename _ForwardIterator>
1948*38fd1498Szrj void
1949*38fd1498Szrj _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1950*38fd1498Szrj std::forward_iterator_tag)
1951*38fd1498Szrj {
1952*38fd1498Szrj const size_type __len = std::distance(__first, __last);
1953*38fd1498Szrj if (__len > size())
1954*38fd1498Szrj {
1955*38fd1498Szrj _ForwardIterator __mid = __first;
1956*38fd1498Szrj std::advance(__mid, size());
1957*38fd1498Szrj std::copy(__first, __mid, begin());
1958*38fd1498Szrj _M_range_insert_aux(end(), __mid, __last,
1959*38fd1498Szrj std::__iterator_category(__first));
1960*38fd1498Szrj }
1961*38fd1498Szrj else
1962*38fd1498Szrj _M_erase_at_end(std::copy(__first, __last, begin()));
1963*38fd1498Szrj }
1964*38fd1498Szrj
1965*38fd1498Szrj // Called by assign(n,t), and the range assign when it turns out
1966*38fd1498Szrj // to be the same thing.
1967*38fd1498Szrj void
1968*38fd1498Szrj _M_fill_assign(size_type __n, const value_type& __val)
1969*38fd1498Szrj {
1970*38fd1498Szrj if (__n > size())
1971*38fd1498Szrj {
1972*38fd1498Szrj std::fill(begin(), end(), __val);
1973*38fd1498Szrj _M_fill_insert(end(), __n - size(), __val);
1974*38fd1498Szrj }
1975*38fd1498Szrj else
1976*38fd1498Szrj {
1977*38fd1498Szrj _M_erase_at_end(begin() + difference_type(__n));
1978*38fd1498Szrj std::fill(begin(), end(), __val);
1979*38fd1498Szrj }
1980*38fd1498Szrj }
1981*38fd1498Szrj
1982*38fd1498Szrj //@{
1983*38fd1498Szrj /// Helper functions for push_* and pop_*.
1984*38fd1498Szrj #if __cplusplus < 201103L
1985*38fd1498Szrj void _M_push_back_aux(const value_type&);
1986*38fd1498Szrj
1987*38fd1498Szrj void _M_push_front_aux(const value_type&);
1988*38fd1498Szrj #else
1989*38fd1498Szrj template<typename... _Args>
1990*38fd1498Szrj void _M_push_back_aux(_Args&&... __args);
1991*38fd1498Szrj
1992*38fd1498Szrj template<typename... _Args>
1993*38fd1498Szrj void _M_push_front_aux(_Args&&... __args);
1994*38fd1498Szrj #endif
1995*38fd1498Szrj
1996*38fd1498Szrj void _M_pop_back_aux();
1997*38fd1498Szrj
1998*38fd1498Szrj void _M_pop_front_aux();
1999*38fd1498Szrj //@}
2000*38fd1498Szrj
2001*38fd1498Szrj // Internal insert functions follow. The *_aux functions do the actual
2002*38fd1498Szrj // insertion work when all shortcuts fail.
2003*38fd1498Szrj
2004*38fd1498Szrj // called by the range insert to implement [23.1.1]/9
2005*38fd1498Szrj
2006*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS
2007*38fd1498Szrj // 438. Ambiguity in the "do the right thing" clause
2008*38fd1498Szrj template<typename _Integer>
2009*38fd1498Szrj void
2010*38fd1498Szrj _M_insert_dispatch(iterator __pos,
2011*38fd1498Szrj _Integer __n, _Integer __x, __true_type)
2012*38fd1498Szrj { _M_fill_insert(__pos, __n, __x); }
2013*38fd1498Szrj
2014*38fd1498Szrj // called by the range insert to implement [23.1.1]/9
2015*38fd1498Szrj template<typename _InputIterator>
2016*38fd1498Szrj void
2017*38fd1498Szrj _M_insert_dispatch(iterator __pos,
2018*38fd1498Szrj _InputIterator __first, _InputIterator __last,
2019*38fd1498Szrj __false_type)
2020*38fd1498Szrj {
2021*38fd1498Szrj _M_range_insert_aux(__pos, __first, __last,
2022*38fd1498Szrj std::__iterator_category(__first));
2023*38fd1498Szrj }
2024*38fd1498Szrj
2025*38fd1498Szrj // called by the second insert_dispatch above
2026*38fd1498Szrj template<typename _InputIterator>
2027*38fd1498Szrj void
2028*38fd1498Szrj _M_range_insert_aux(iterator __pos, _InputIterator __first,
2029*38fd1498Szrj _InputIterator __last, std::input_iterator_tag);
2030*38fd1498Szrj
2031*38fd1498Szrj // called by the second insert_dispatch above
2032*38fd1498Szrj template<typename _ForwardIterator>
2033*38fd1498Szrj void
2034*38fd1498Szrj _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
2035*38fd1498Szrj _ForwardIterator __last, std::forward_iterator_tag);
2036*38fd1498Szrj
2037*38fd1498Szrj // Called by insert(p,n,x), and the range insert when it turns out to be
2038*38fd1498Szrj // the same thing. Can use fill functions in optimal situations,
2039*38fd1498Szrj // otherwise passes off to insert_aux(p,n,x).
2040*38fd1498Szrj void
2041*38fd1498Szrj _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
2042*38fd1498Szrj
2043*38fd1498Szrj // called by insert(p,x)
2044*38fd1498Szrj #if __cplusplus < 201103L
2045*38fd1498Szrj iterator
2046*38fd1498Szrj _M_insert_aux(iterator __pos, const value_type& __x);
2047*38fd1498Szrj #else
2048*38fd1498Szrj template<typename... _Args>
2049*38fd1498Szrj iterator
2050*38fd1498Szrj _M_insert_aux(iterator __pos, _Args&&... __args);
2051*38fd1498Szrj #endif
2052*38fd1498Szrj
2053*38fd1498Szrj // called by insert(p,n,x) via fill_insert
2054*38fd1498Szrj void
2055*38fd1498Szrj _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
2056*38fd1498Szrj
2057*38fd1498Szrj // called by range_insert_aux for forward iterators
2058*38fd1498Szrj template<typename _ForwardIterator>
2059*38fd1498Szrj void
2060*38fd1498Szrj _M_insert_aux(iterator __pos,
2061*38fd1498Szrj _ForwardIterator __first, _ForwardIterator __last,
2062*38fd1498Szrj size_type __n);
2063*38fd1498Szrj
2064*38fd1498Szrj
2065*38fd1498Szrj // Internal erase functions follow.
2066*38fd1498Szrj
2067*38fd1498Szrj void
2068*38fd1498Szrj _M_destroy_data_aux(iterator __first, iterator __last);
2069*38fd1498Szrj
2070*38fd1498Szrj // Called by ~deque().
2071*38fd1498Szrj // NB: Doesn't deallocate the nodes.
2072*38fd1498Szrj template<typename _Alloc1>
2073*38fd1498Szrj void
2074*38fd1498Szrj _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
2075*38fd1498Szrj { _M_destroy_data_aux(__first, __last); }
2076*38fd1498Szrj
2077*38fd1498Szrj void
2078*38fd1498Szrj _M_destroy_data(iterator __first, iterator __last,
2079*38fd1498Szrj const std::allocator<_Tp>&)
2080*38fd1498Szrj {
2081*38fd1498Szrj if (!__has_trivial_destructor(value_type))
2082*38fd1498Szrj _M_destroy_data_aux(__first, __last);
2083*38fd1498Szrj }
2084*38fd1498Szrj
2085*38fd1498Szrj // Called by erase(q1, q2).
2086*38fd1498Szrj void
2087*38fd1498Szrj _M_erase_at_begin(iterator __pos)
2088*38fd1498Szrj {
2089*38fd1498Szrj _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
2090*38fd1498Szrj _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
2091*38fd1498Szrj this->_M_impl._M_start = __pos;
2092*38fd1498Szrj }
2093*38fd1498Szrj
2094*38fd1498Szrj // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2095*38fd1498Szrj // _M_fill_assign, operator=.
2096*38fd1498Szrj void
2097*38fd1498Szrj _M_erase_at_end(iterator __pos)
2098*38fd1498Szrj {
2099*38fd1498Szrj _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
2100*38fd1498Szrj _M_destroy_nodes(__pos._M_node + 1,
2101*38fd1498Szrj this->_M_impl._M_finish._M_node + 1);
2102*38fd1498Szrj this->_M_impl._M_finish = __pos;
2103*38fd1498Szrj }
2104*38fd1498Szrj
2105*38fd1498Szrj iterator
2106*38fd1498Szrj _M_erase(iterator __pos);
2107*38fd1498Szrj
2108*38fd1498Szrj iterator
2109*38fd1498Szrj _M_erase(iterator __first, iterator __last);
2110*38fd1498Szrj
2111*38fd1498Szrj #if __cplusplus >= 201103L
2112*38fd1498Szrj // Called by resize(sz).
2113*38fd1498Szrj void
2114*38fd1498Szrj _M_default_append(size_type __n);
2115*38fd1498Szrj
2116*38fd1498Szrj bool
2117*38fd1498Szrj _M_shrink_to_fit();
2118*38fd1498Szrj #endif
2119*38fd1498Szrj
2120*38fd1498Szrj //@{
2121*38fd1498Szrj /// Memory-handling helpers for the previous internal insert functions.
2122*38fd1498Szrj iterator
2123*38fd1498Szrj _M_reserve_elements_at_front(size_type __n)
2124*38fd1498Szrj {
2125*38fd1498Szrj const size_type __vacancies = this->_M_impl._M_start._M_cur
2126*38fd1498Szrj - this->_M_impl._M_start._M_first;
2127*38fd1498Szrj if (__n > __vacancies)
2128*38fd1498Szrj _M_new_elements_at_front(__n - __vacancies);
2129*38fd1498Szrj return this->_M_impl._M_start - difference_type(__n);
2130*38fd1498Szrj }
2131*38fd1498Szrj
2132*38fd1498Szrj iterator
2133*38fd1498Szrj _M_reserve_elements_at_back(size_type __n)
2134*38fd1498Szrj {
2135*38fd1498Szrj const size_type __vacancies = (this->_M_impl._M_finish._M_last
2136*38fd1498Szrj - this->_M_impl._M_finish._M_cur) - 1;
2137*38fd1498Szrj if (__n > __vacancies)
2138*38fd1498Szrj _M_new_elements_at_back(__n - __vacancies);
2139*38fd1498Szrj return this->_M_impl._M_finish + difference_type(__n);
2140*38fd1498Szrj }
2141*38fd1498Szrj
2142*38fd1498Szrj void
2143*38fd1498Szrj _M_new_elements_at_front(size_type __new_elements);
2144*38fd1498Szrj
2145*38fd1498Szrj void
2146*38fd1498Szrj _M_new_elements_at_back(size_type __new_elements);
2147*38fd1498Szrj //@}
2148*38fd1498Szrj
2149*38fd1498Szrj
2150*38fd1498Szrj //@{
2151*38fd1498Szrj /**
2152*38fd1498Szrj * @brief Memory-handling helpers for the major %map.
2153*38fd1498Szrj *
2154*38fd1498Szrj * Makes sure the _M_map has space for new nodes. Does not
2155*38fd1498Szrj * actually add the nodes. Can invalidate _M_map pointers.
2156*38fd1498Szrj * (And consequently, %deque iterators.)
2157*38fd1498Szrj */
2158*38fd1498Szrj void
2159*38fd1498Szrj _M_reserve_map_at_back(size_type __nodes_to_add = 1)
2160*38fd1498Szrj {
2161*38fd1498Szrj if (__nodes_to_add + 1 > this->_M_impl._M_map_size
2162*38fd1498Szrj - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
2163*38fd1498Szrj _M_reallocate_map(__nodes_to_add, false);
2164*38fd1498Szrj }
2165*38fd1498Szrj
2166*38fd1498Szrj void
2167*38fd1498Szrj _M_reserve_map_at_front(size_type __nodes_to_add = 1)
2168*38fd1498Szrj {
2169*38fd1498Szrj if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
2170*38fd1498Szrj - this->_M_impl._M_map))
2171*38fd1498Szrj _M_reallocate_map(__nodes_to_add, true);
2172*38fd1498Szrj }
2173*38fd1498Szrj
2174*38fd1498Szrj void
2175*38fd1498Szrj _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
2176*38fd1498Szrj //@}
2177*38fd1498Szrj
2178*38fd1498Szrj #if __cplusplus >= 201103L
2179*38fd1498Szrj // Constant-time, nothrow move assignment when source object's memory
2180*38fd1498Szrj // can be moved because the allocators are equal.
2181*38fd1498Szrj void
2182*38fd1498Szrj _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
2183*38fd1498Szrj {
2184*38fd1498Szrj this->_M_impl._M_swap_data(__x._M_impl);
2185*38fd1498Szrj __x.clear();
2186*38fd1498Szrj std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
2187*38fd1498Szrj }
2188*38fd1498Szrj
2189*38fd1498Szrj // When the allocators are not equal the operation could throw, because
2190*38fd1498Szrj // we might need to allocate a new map for __x after moving from it
2191*38fd1498Szrj // or we might need to allocate new elements for *this.
2192*38fd1498Szrj void
2193*38fd1498Szrj _M_move_assign1(deque&& __x, /* always equal: */ false_type)
2194*38fd1498Szrj {
2195*38fd1498Szrj constexpr bool __move_storage =
2196*38fd1498Szrj _Alloc_traits::_S_propagate_on_move_assign();
2197*38fd1498Szrj _M_move_assign2(std::move(__x), __bool_constant<__move_storage>());
2198*38fd1498Szrj }
2199*38fd1498Szrj
2200*38fd1498Szrj // Destroy all elements and deallocate all memory, then replace
2201*38fd1498Szrj // with elements created from __args.
2202*38fd1498Szrj template<typename... _Args>
2203*38fd1498Szrj void
2204*38fd1498Szrj _M_replace_map(_Args&&... __args)
2205*38fd1498Szrj {
2206*38fd1498Szrj // Create new data first, so if allocation fails there are no effects.
2207*38fd1498Szrj deque __newobj(std::forward<_Args>(__args)...);
2208*38fd1498Szrj // Free existing storage using existing allocator.
2209*38fd1498Szrj clear();
2210*38fd1498Szrj _M_deallocate_node(*begin()._M_node); // one node left after clear()
2211*38fd1498Szrj _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
2212*38fd1498Szrj this->_M_impl._M_map = nullptr;
2213*38fd1498Szrj this->_M_impl._M_map_size = 0;
2214*38fd1498Szrj // Take ownership of replacement memory.
2215*38fd1498Szrj this->_M_impl._M_swap_data(__newobj._M_impl);
2216*38fd1498Szrj }
2217*38fd1498Szrj
2218*38fd1498Szrj // Do move assignment when the allocator propagates.
2219*38fd1498Szrj void
2220*38fd1498Szrj _M_move_assign2(deque&& __x, /* propagate: */ true_type)
2221*38fd1498Szrj {
2222*38fd1498Szrj // Make a copy of the original allocator state.
2223*38fd1498Szrj auto __alloc = __x._M_get_Tp_allocator();
2224*38fd1498Szrj // The allocator propagates so storage can be moved from __x,
2225*38fd1498Szrj // leaving __x in a valid empty state with a moved-from allocator.
2226*38fd1498Szrj _M_replace_map(std::move(__x));
2227*38fd1498Szrj // Move the corresponding allocator state too.
2228*38fd1498Szrj _M_get_Tp_allocator() = std::move(__alloc);
2229*38fd1498Szrj }
2230*38fd1498Szrj
2231*38fd1498Szrj // Do move assignment when it may not be possible to move source
2232*38fd1498Szrj // object's memory, resulting in a linear-time operation.
2233*38fd1498Szrj void
2234*38fd1498Szrj _M_move_assign2(deque&& __x, /* propagate: */ false_type)
2235*38fd1498Szrj {
2236*38fd1498Szrj if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2237*38fd1498Szrj {
2238*38fd1498Szrj // The allocators are equal so storage can be moved from __x,
2239*38fd1498Szrj // leaving __x in a valid empty state with its current allocator.
2240*38fd1498Szrj _M_replace_map(std::move(__x), __x.get_allocator());
2241*38fd1498Szrj }
2242*38fd1498Szrj else
2243*38fd1498Szrj {
2244*38fd1498Szrj // The rvalue's allocator cannot be moved and is not equal,
2245*38fd1498Szrj // so we need to individually move each element.
2246*38fd1498Szrj _M_assign_aux(std::__make_move_if_noexcept_iterator(__x.begin()),
2247*38fd1498Szrj std::__make_move_if_noexcept_iterator(__x.end()),
2248*38fd1498Szrj std::random_access_iterator_tag());
2249*38fd1498Szrj __x.clear();
2250*38fd1498Szrj }
2251*38fd1498Szrj }
2252*38fd1498Szrj #endif
2253*38fd1498Szrj };
2254*38fd1498Szrj
2255*38fd1498Szrj #if __cpp_deduction_guides >= 201606
2256*38fd1498Szrj template<typename _InputIterator, typename _ValT
2257*38fd1498Szrj = typename iterator_traits<_InputIterator>::value_type,
2258*38fd1498Szrj typename _Allocator = allocator<_ValT>,
2259*38fd1498Szrj typename = _RequireInputIter<_InputIterator>,
2260*38fd1498Szrj typename = _RequireAllocator<_Allocator>>
2261*38fd1498Szrj deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
2262*38fd1498Szrj -> deque<_ValT, _Allocator>;
2263*38fd1498Szrj #endif
2264*38fd1498Szrj
2265*38fd1498Szrj /**
2266*38fd1498Szrj * @brief Deque equality comparison.
2267*38fd1498Szrj * @param __x A %deque.
2268*38fd1498Szrj * @param __y A %deque of the same type as @a __x.
2269*38fd1498Szrj * @return True iff the size and elements of the deques are equal.
2270*38fd1498Szrj *
2271*38fd1498Szrj * This is an equivalence relation. It is linear in the size of the
2272*38fd1498Szrj * deques. Deques are considered equivalent if their sizes are equal,
2273*38fd1498Szrj * and if corresponding elements compare equal.
2274*38fd1498Szrj */
2275*38fd1498Szrj template<typename _Tp, typename _Alloc>
2276*38fd1498Szrj inline bool
2277*38fd1498Szrj operator==(const deque<_Tp, _Alloc>& __x,
2278*38fd1498Szrj const deque<_Tp, _Alloc>& __y)
2279*38fd1498Szrj { return __x.size() == __y.size()
2280*38fd1498Szrj && std::equal(__x.begin(), __x.end(), __y.begin()); }
2281*38fd1498Szrj
2282*38fd1498Szrj /**
2283*38fd1498Szrj * @brief Deque ordering relation.
2284*38fd1498Szrj * @param __x A %deque.
2285*38fd1498Szrj * @param __y A %deque of the same type as @a __x.
2286*38fd1498Szrj * @return True iff @a x is lexicographically less than @a __y.
2287*38fd1498Szrj *
2288*38fd1498Szrj * This is a total ordering relation. It is linear in the size of the
2289*38fd1498Szrj * deques. The elements must be comparable with @c <.
2290*38fd1498Szrj *
2291*38fd1498Szrj * See std::lexicographical_compare() for how the determination is made.
2292*38fd1498Szrj */
2293*38fd1498Szrj template<typename _Tp, typename _Alloc>
2294*38fd1498Szrj inline bool
2295*38fd1498Szrj operator<(const deque<_Tp, _Alloc>& __x,
2296*38fd1498Szrj const deque<_Tp, _Alloc>& __y)
2297*38fd1498Szrj { return std::lexicographical_compare(__x.begin(), __x.end(),
2298*38fd1498Szrj __y.begin(), __y.end()); }
2299*38fd1498Szrj
2300*38fd1498Szrj /// Based on operator==
2301*38fd1498Szrj template<typename _Tp, typename _Alloc>
2302*38fd1498Szrj inline bool
2303*38fd1498Szrj operator!=(const deque<_Tp, _Alloc>& __x,
2304*38fd1498Szrj const deque<_Tp, _Alloc>& __y)
2305*38fd1498Szrj { return !(__x == __y); }
2306*38fd1498Szrj
2307*38fd1498Szrj /// Based on operator<
2308*38fd1498Szrj template<typename _Tp, typename _Alloc>
2309*38fd1498Szrj inline bool
2310*38fd1498Szrj operator>(const deque<_Tp, _Alloc>& __x,
2311*38fd1498Szrj const deque<_Tp, _Alloc>& __y)
2312*38fd1498Szrj { return __y < __x; }
2313*38fd1498Szrj
2314*38fd1498Szrj /// Based on operator<
2315*38fd1498Szrj template<typename _Tp, typename _Alloc>
2316*38fd1498Szrj inline bool
2317*38fd1498Szrj operator<=(const deque<_Tp, _Alloc>& __x,
2318*38fd1498Szrj const deque<_Tp, _Alloc>& __y)
2319*38fd1498Szrj { return !(__y < __x); }
2320*38fd1498Szrj
2321*38fd1498Szrj /// Based on operator<
2322*38fd1498Szrj template<typename _Tp, typename _Alloc>
2323*38fd1498Szrj inline bool
2324*38fd1498Szrj operator>=(const deque<_Tp, _Alloc>& __x,
2325*38fd1498Szrj const deque<_Tp, _Alloc>& __y)
2326*38fd1498Szrj { return !(__x < __y); }
2327*38fd1498Szrj
2328*38fd1498Szrj /// See std::deque::swap().
2329*38fd1498Szrj template<typename _Tp, typename _Alloc>
2330*38fd1498Szrj inline void
2331*38fd1498Szrj swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
2332*38fd1498Szrj _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2333*38fd1498Szrj { __x.swap(__y); }
2334*38fd1498Szrj
2335*38fd1498Szrj #undef _GLIBCXX_DEQUE_BUF_SIZE
2336*38fd1498Szrj
2337*38fd1498Szrj _GLIBCXX_END_NAMESPACE_CONTAINER
2338*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
2339*38fd1498Szrj } // namespace std
2340*38fd1498Szrj
2341*38fd1498Szrj #endif /* _STL_DEQUE_H */
2342