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