PR libstdc++/87278 restore support for std::make_shared<volatile T>()
[official-gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
blob555be16dcd5663f5c5b6b1a1278012b6acc5506d
1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001-2018 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.
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.
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.
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/>.
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1997
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
56 #ifndef _STL_DEQUE_H
57 #define _STL_DEQUE_H 1
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
66 #include <debug/assertions.h>
68 namespace std _GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
73 /**
74 * @brief This function controls the size of memory nodes.
75 * @param __size The size of an element.
76 * @return The number (not byte size) of elements per node.
78 * This function started off as a compiler kludge from SGI, but
79 * seems to be a useful wrapper around a repeated constant
80 * expression. The @b 512 is tunable (and no other code needs to
81 * change), but no investigation has been done since inheriting the
82 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
83 * you are doing, however: changing it breaks the binary
84 * compatibility!!
87 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
88 #define _GLIBCXX_DEQUE_BUF_SIZE 512
89 #endif
91 _GLIBCXX_CONSTEXPR inline size_t
92 __deque_buf_size(size_t __size)
93 { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
94 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
97 /**
98 * @brief A deque::iterator.
100 * Quite a bit of intelligence here. Much of the functionality of
101 * deque is actually passed off to this class. A deque holds two
102 * of these internally, marking its valid range. Access to
103 * elements is done as offsets of either of those two, relying on
104 * operator overloading in this class.
106 * All the functions are op overloads except for _M_set_node.
108 template<typename _Tp, typename _Ref, typename _Ptr>
109 struct _Deque_iterator
111 #if __cplusplus < 201103L
112 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
113 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
114 typedef _Tp* _Elt_pointer;
115 typedef _Tp** _Map_pointer;
116 #else
117 private:
118 template<typename _Up>
119 using __ptr_to = typename pointer_traits<_Ptr>::template rebind<_Up>;
120 template<typename _CvTp>
121 using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_to<_CvTp>>;
122 public:
123 typedef __iter<_Tp> iterator;
124 typedef __iter<const _Tp> const_iterator;
125 typedef __ptr_to<_Tp> _Elt_pointer;
126 typedef __ptr_to<_Elt_pointer> _Map_pointer;
127 #endif
129 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
130 { return __deque_buf_size(sizeof(_Tp)); }
132 typedef std::random_access_iterator_tag iterator_category;
133 typedef _Tp value_type;
134 typedef _Ptr pointer;
135 typedef _Ref reference;
136 typedef size_t size_type;
137 typedef ptrdiff_t difference_type;
138 typedef _Deque_iterator _Self;
140 _Elt_pointer _M_cur;
141 _Elt_pointer _M_first;
142 _Elt_pointer _M_last;
143 _Map_pointer _M_node;
145 _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT
146 : _M_cur(__x), _M_first(*__y),
147 _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
149 _Deque_iterator() _GLIBCXX_NOEXCEPT
150 : _M_cur(), _M_first(), _M_last(), _M_node() { }
152 #if __cplusplus < 201103L
153 // Conversion from iterator to const_iterator.
154 _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
155 : _M_cur(__x._M_cur), _M_first(__x._M_first),
156 _M_last(__x._M_last), _M_node(__x._M_node) { }
157 #else
158 // Conversion from iterator to const_iterator.
159 template<typename _Iter,
160 typename = _Require<is_same<_Self, const_iterator>,
161 is_same<_Iter, iterator>>>
162 _Deque_iterator(const _Iter& __x) noexcept
163 : _M_cur(__x._M_cur), _M_first(__x._M_first),
164 _M_last(__x._M_last), _M_node(__x._M_node) { }
166 _Deque_iterator(const _Deque_iterator&) = default;
167 _Deque_iterator& operator=(const _Deque_iterator&) = default;
168 #endif
170 iterator
171 _M_const_cast() const _GLIBCXX_NOEXCEPT
172 { return iterator(_M_cur, _M_node); }
174 reference
175 operator*() const _GLIBCXX_NOEXCEPT
176 { return *_M_cur; }
178 pointer
179 operator->() const _GLIBCXX_NOEXCEPT
180 { return _M_cur; }
182 _Self&
183 operator++() _GLIBCXX_NOEXCEPT
185 ++_M_cur;
186 if (_M_cur == _M_last)
188 _M_set_node(_M_node + 1);
189 _M_cur = _M_first;
191 return *this;
194 _Self
195 operator++(int) _GLIBCXX_NOEXCEPT
197 _Self __tmp = *this;
198 ++*this;
199 return __tmp;
202 _Self&
203 operator--() _GLIBCXX_NOEXCEPT
205 if (_M_cur == _M_first)
207 _M_set_node(_M_node - 1);
208 _M_cur = _M_last;
210 --_M_cur;
211 return *this;
214 _Self
215 operator--(int) _GLIBCXX_NOEXCEPT
217 _Self __tmp = *this;
218 --*this;
219 return __tmp;
222 _Self&
223 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
225 const difference_type __offset = __n + (_M_cur - _M_first);
226 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
227 _M_cur += __n;
228 else
230 const difference_type __node_offset =
231 __offset > 0 ? __offset / difference_type(_S_buffer_size())
232 : -difference_type((-__offset - 1)
233 / _S_buffer_size()) - 1;
234 _M_set_node(_M_node + __node_offset);
235 _M_cur = _M_first + (__offset - __node_offset
236 * difference_type(_S_buffer_size()));
238 return *this;
241 _Self
242 operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
244 _Self __tmp = *this;
245 return __tmp += __n;
248 _Self&
249 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
250 { return *this += -__n; }
252 _Self
253 operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
255 _Self __tmp = *this;
256 return __tmp -= __n;
259 reference
260 operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
261 { return *(*this + __n); }
264 * Prepares to traverse new_node. Sets everything except
265 * _M_cur, which should therefore be set by the caller
266 * immediately afterwards, based on _M_first and _M_last.
268 void
269 _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT
271 _M_node = __new_node;
272 _M_first = *__new_node;
273 _M_last = _M_first + difference_type(_S_buffer_size());
277 // Note: we also provide overloads whose operands are of the same type in
278 // order to avoid ambiguous overload resolution when std::rel_ops operators
279 // are in scope (for additional details, see libstdc++/3628)
280 template<typename _Tp, typename _Ref, typename _Ptr>
281 inline bool
282 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
283 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
284 { return __x._M_cur == __y._M_cur; }
286 template<typename _Tp, typename _RefL, typename _PtrL,
287 typename _RefR, typename _PtrR>
288 inline bool
289 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
290 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
291 { return __x._M_cur == __y._M_cur; }
293 template<typename _Tp, typename _Ref, typename _Ptr>
294 inline bool
295 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
296 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
297 { return !(__x == __y); }
299 template<typename _Tp, typename _RefL, typename _PtrL,
300 typename _RefR, typename _PtrR>
301 inline bool
302 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
303 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
304 { return !(__x == __y); }
306 template<typename _Tp, typename _Ref, typename _Ptr>
307 inline bool
308 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
309 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
310 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
311 : (__x._M_node < __y._M_node); }
313 template<typename _Tp, typename _RefL, typename _PtrL,
314 typename _RefR, typename _PtrR>
315 inline bool
316 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
317 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
318 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
319 : (__x._M_node < __y._M_node); }
321 template<typename _Tp, typename _Ref, typename _Ptr>
322 inline bool
323 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
324 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
325 { return __y < __x; }
327 template<typename _Tp, typename _RefL, typename _PtrL,
328 typename _RefR, typename _PtrR>
329 inline bool
330 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
331 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
332 { return __y < __x; }
334 template<typename _Tp, typename _Ref, typename _Ptr>
335 inline bool
336 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
337 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
338 { return !(__y < __x); }
340 template<typename _Tp, typename _RefL, typename _PtrL,
341 typename _RefR, typename _PtrR>
342 inline bool
343 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
344 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
345 { return !(__y < __x); }
347 template<typename _Tp, typename _Ref, typename _Ptr>
348 inline bool
349 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
350 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
351 { return !(__x < __y); }
353 template<typename _Tp, typename _RefL, typename _PtrL,
354 typename _RefR, typename _PtrR>
355 inline bool
356 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
357 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
358 { return !(__x < __y); }
360 // _GLIBCXX_RESOLVE_LIB_DEFECTS
361 // According to the resolution of DR179 not only the various comparison
362 // operators but also operator- must accept mixed iterator/const_iterator
363 // parameters.
364 template<typename _Tp, typename _Ref, typename _Ptr>
365 inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
366 operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
367 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
369 return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
370 (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
371 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
372 + (__y._M_last - __y._M_cur);
375 template<typename _Tp, typename _RefL, typename _PtrL,
376 typename _RefR, typename _PtrR>
377 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
378 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
379 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
381 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
382 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
383 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
384 + (__y._M_last - __y._M_cur);
387 template<typename _Tp, typename _Ref, typename _Ptr>
388 inline _Deque_iterator<_Tp, _Ref, _Ptr>
389 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
390 _GLIBCXX_NOEXCEPT
391 { return __x + __n; }
393 template<typename _Tp>
394 void
395 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&,
396 const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&);
398 template<typename _Tp>
399 _Deque_iterator<_Tp, _Tp&, _Tp*>
400 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
401 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
402 _Deque_iterator<_Tp, _Tp&, _Tp*>);
404 template<typename _Tp>
405 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
406 copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
407 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
408 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
409 { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
410 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
411 __result); }
413 template<typename _Tp>
414 _Deque_iterator<_Tp, _Tp&, _Tp*>
415 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
416 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
417 _Deque_iterator<_Tp, _Tp&, _Tp*>);
419 template<typename _Tp>
420 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
421 copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
422 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
423 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
424 { return std::copy_backward(_Deque_iterator<_Tp,
425 const _Tp&, const _Tp*>(__first),
426 _Deque_iterator<_Tp,
427 const _Tp&, const _Tp*>(__last),
428 __result); }
430 #if __cplusplus >= 201103L
431 template<typename _Tp>
432 _Deque_iterator<_Tp, _Tp&, _Tp*>
433 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
434 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
435 _Deque_iterator<_Tp, _Tp&, _Tp*>);
437 template<typename _Tp>
438 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
439 move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
440 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
441 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
442 { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
443 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
444 __result); }
446 template<typename _Tp>
447 _Deque_iterator<_Tp, _Tp&, _Tp*>
448 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
449 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
450 _Deque_iterator<_Tp, _Tp&, _Tp*>);
452 template<typename _Tp>
453 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
454 move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
455 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
456 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
457 { return std::move_backward(_Deque_iterator<_Tp,
458 const _Tp&, const _Tp*>(__first),
459 _Deque_iterator<_Tp,
460 const _Tp&, const _Tp*>(__last),
461 __result); }
462 #endif
465 * Deque base class. This class provides the unified face for %deque's
466 * allocation. This class's constructor and destructor allocate and
467 * deallocate (but do not initialize) storage. This makes %exception
468 * safety easier.
470 * Nothing in this class ever constructs or destroys an actual Tp element.
471 * (Deque handles that itself.) Only/All memory management is performed
472 * here.
474 template<typename _Tp, typename _Alloc>
475 class _Deque_base
477 protected:
478 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
479 rebind<_Tp>::other _Tp_alloc_type;
480 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
482 #if __cplusplus < 201103L
483 typedef _Tp* _Ptr;
484 typedef const _Tp* _Ptr_const;
485 #else
486 typedef typename _Alloc_traits::pointer _Ptr;
487 typedef typename _Alloc_traits::const_pointer _Ptr_const;
488 #endif
490 typedef typename _Alloc_traits::template rebind<_Ptr>::other
491 _Map_alloc_type;
492 typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits;
494 public:
495 typedef _Alloc allocator_type;
497 allocator_type
498 get_allocator() const _GLIBCXX_NOEXCEPT
499 { return allocator_type(_M_get_Tp_allocator()); }
501 typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator;
502 typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator;
504 _Deque_base()
505 : _M_impl()
506 { _M_initialize_map(0); }
508 _Deque_base(size_t __num_elements)
509 : _M_impl()
510 { _M_initialize_map(__num_elements); }
512 _Deque_base(const allocator_type& __a, size_t __num_elements)
513 : _M_impl(__a)
514 { _M_initialize_map(__num_elements); }
516 _Deque_base(const allocator_type& __a)
517 : _M_impl(__a)
518 { /* Caller must initialize map. */ }
520 #if __cplusplus >= 201103L
521 _Deque_base(_Deque_base&& __x, false_type)
522 : _M_impl(__x._M_move_impl())
525 _Deque_base(_Deque_base&& __x, true_type)
526 : _M_impl(std::move(__x._M_get_Tp_allocator()))
528 _M_initialize_map(0);
529 if (__x._M_impl._M_map)
530 this->_M_impl._M_swap_data(__x._M_impl);
533 _Deque_base(_Deque_base&& __x)
534 : _Deque_base(std::move(__x), typename _Alloc_traits::is_always_equal{})
537 _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_t __n)
538 : _M_impl(__a)
540 if (__x.get_allocator() == __a)
542 if (__x._M_impl._M_map)
544 _M_initialize_map(0);
545 this->_M_impl._M_swap_data(__x._M_impl);
548 else
550 _M_initialize_map(__n);
553 #endif
555 ~_Deque_base() _GLIBCXX_NOEXCEPT;
557 protected:
558 typedef typename iterator::_Map_pointer _Map_pointer;
560 //This struct encapsulates the implementation of the std::deque
561 //standard container and at the same time makes use of the EBO
562 //for empty allocators.
563 struct _Deque_impl
564 : public _Tp_alloc_type
566 _Map_pointer _M_map;
567 size_t _M_map_size;
568 iterator _M_start;
569 iterator _M_finish;
571 _Deque_impl()
572 : _Tp_alloc_type(), _M_map(), _M_map_size(0),
573 _M_start(), _M_finish()
576 _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
577 : _Tp_alloc_type(__a), _M_map(), _M_map_size(0),
578 _M_start(), _M_finish()
581 #if __cplusplus >= 201103L
582 _Deque_impl(_Deque_impl&&) = default;
584 _Deque_impl(_Tp_alloc_type&& __a) noexcept
585 : _Tp_alloc_type(std::move(__a)), _M_map(), _M_map_size(0),
586 _M_start(), _M_finish()
588 #endif
590 void _M_swap_data(_Deque_impl& __x) _GLIBCXX_NOEXCEPT
592 using std::swap;
593 swap(this->_M_start, __x._M_start);
594 swap(this->_M_finish, __x._M_finish);
595 swap(this->_M_map, __x._M_map);
596 swap(this->_M_map_size, __x._M_map_size);
600 _Tp_alloc_type&
601 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
602 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
604 const _Tp_alloc_type&
605 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
606 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
608 _Map_alloc_type
609 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
610 { return _Map_alloc_type(_M_get_Tp_allocator()); }
612 _Ptr
613 _M_allocate_node()
615 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
616 return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
619 void
620 _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
622 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
623 _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
626 _Map_pointer
627 _M_allocate_map(size_t __n)
629 _Map_alloc_type __map_alloc = _M_get_map_allocator();
630 return _Map_alloc_traits::allocate(__map_alloc, __n);
633 void
634 _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
636 _Map_alloc_type __map_alloc = _M_get_map_allocator();
637 _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
640 protected:
641 void _M_initialize_map(size_t);
642 void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
643 void _M_destroy_nodes(_Map_pointer __nstart,
644 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
645 enum { _S_initial_map_size = 8 };
647 _Deque_impl _M_impl;
649 #if __cplusplus >= 201103L
650 private:
651 _Deque_impl
652 _M_move_impl()
654 if (!_M_impl._M_map)
655 return std::move(_M_impl);
657 // Create a copy of the current allocator.
658 _Tp_alloc_type __alloc{_M_get_Tp_allocator()};
659 // Put that copy in a moved-from state.
660 _Tp_alloc_type __sink __attribute((__unused__)) {std::move(__alloc)};
661 // Create an empty map that allocates using the moved-from allocator.
662 _Deque_base __empty{__alloc};
663 __empty._M_initialize_map(0);
664 // Now safe to modify current allocator and perform non-throwing swaps.
665 _Deque_impl __ret{std::move(_M_get_Tp_allocator())};
666 _M_impl._M_swap_data(__ret);
667 _M_impl._M_swap_data(__empty._M_impl);
668 return __ret;
670 #endif
673 template<typename _Tp, typename _Alloc>
674 _Deque_base<_Tp, _Alloc>::
675 ~_Deque_base() _GLIBCXX_NOEXCEPT
677 if (this->_M_impl._M_map)
679 _M_destroy_nodes(this->_M_impl._M_start._M_node,
680 this->_M_impl._M_finish._M_node + 1);
681 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
686 * @brief Layout storage.
687 * @param __num_elements The count of T's for which to allocate space
688 * at first.
689 * @return Nothing.
691 * The initial underlying memory layout is a bit complicated...
693 template<typename _Tp, typename _Alloc>
694 void
695 _Deque_base<_Tp, _Alloc>::
696 _M_initialize_map(size_t __num_elements)
698 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
699 + 1);
701 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
702 size_t(__num_nodes + 2));
703 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
705 // For "small" maps (needing less than _M_map_size nodes), allocation
706 // starts in the middle elements and grows outwards. So nstart may be
707 // the beginning of _M_map, but for small maps it may be as far in as
708 // _M_map+3.
710 _Map_pointer __nstart = (this->_M_impl._M_map
711 + (this->_M_impl._M_map_size - __num_nodes) / 2);
712 _Map_pointer __nfinish = __nstart + __num_nodes;
714 __try
715 { _M_create_nodes(__nstart, __nfinish); }
716 __catch(...)
718 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
719 this->_M_impl._M_map = _Map_pointer();
720 this->_M_impl._M_map_size = 0;
721 __throw_exception_again;
724 this->_M_impl._M_start._M_set_node(__nstart);
725 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
726 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
727 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
728 + __num_elements
729 % __deque_buf_size(sizeof(_Tp)));
732 template<typename _Tp, typename _Alloc>
733 void
734 _Deque_base<_Tp, _Alloc>::
735 _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
737 _Map_pointer __cur;
738 __try
740 for (__cur = __nstart; __cur < __nfinish; ++__cur)
741 *__cur = this->_M_allocate_node();
743 __catch(...)
745 _M_destroy_nodes(__nstart, __cur);
746 __throw_exception_again;
750 template<typename _Tp, typename _Alloc>
751 void
752 _Deque_base<_Tp, _Alloc>::
753 _M_destroy_nodes(_Map_pointer __nstart,
754 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
756 for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
757 _M_deallocate_node(*__n);
761 * @brief A standard container using fixed-size memory allocation and
762 * constant-time manipulation of elements at either end.
764 * @ingroup sequences
766 * @tparam _Tp Type of element.
767 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
769 * Meets the requirements of a <a href="tables.html#65">container</a>, a
770 * <a href="tables.html#66">reversible container</a>, and a
771 * <a href="tables.html#67">sequence</a>, including the
772 * <a href="tables.html#68">optional sequence requirements</a>.
774 * In previous HP/SGI versions of deque, there was an extra template
775 * parameter so users could control the node size. This extension turned
776 * out to violate the C++ standard (it can be detected using template
777 * template parameters), and it was removed.
779 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
781 * - Tp** _M_map
782 * - size_t _M_map_size
783 * - iterator _M_start, _M_finish
785 * map_size is at least 8. %map is an array of map_size
786 * pointers-to-@a nodes. (The name %map has nothing to do with the
787 * std::map class, and @b nodes should not be confused with
788 * std::list's usage of @a node.)
790 * A @a node has no specific type name as such, but it is referred
791 * to as @a node in this file. It is a simple array-of-Tp. If Tp
792 * is very large, there will be one Tp element per node (i.e., an
793 * @a array of one). For non-huge Tp's, node size is inversely
794 * related to Tp size: the larger the Tp, the fewer Tp's will fit
795 * in a node. The goal here is to keep the total size of a node
796 * relatively small and constant over different Tp's, to improve
797 * allocator efficiency.
799 * Not every pointer in the %map array will point to a node. If
800 * the initial number of elements in the deque is small, the
801 * /middle/ %map pointers will be valid, and the ones at the edges
802 * will be unused. This same situation will arise as the %map
803 * grows: available %map pointers, if any, will be on the ends. As
804 * new nodes are created, only a subset of the %map's pointers need
805 * to be copied @a outward.
807 * Class invariants:
808 * - For any nonsingular iterator i:
809 * - i.node points to a member of the %map array. (Yes, you read that
810 * correctly: i.node does not actually point to a node.) The member of
811 * the %map array is what actually points to the node.
812 * - i.first == *(i.node) (This points to the node (first Tp element).)
813 * - i.last == i.first + node_size
814 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
815 * the implication of this is that i.cur is always a dereferenceable
816 * pointer, even if i is a past-the-end iterator.
817 * - Start and Finish are always nonsingular iterators. NOTE: this
818 * means that an empty deque must have one node, a deque with <N
819 * elements (where N is the node buffer size) must have one node, a
820 * deque with N through (2N-1) elements must have two nodes, etc.
821 * - For every node other than start.node and finish.node, every
822 * element in the node is an initialized object. If start.node ==
823 * finish.node, then [start.cur, finish.cur) are initialized
824 * objects, and the elements outside that range are uninitialized
825 * storage. Otherwise, [start.cur, start.last) and [finish.first,
826 * finish.cur) are initialized objects, and [start.first, start.cur)
827 * and [finish.cur, finish.last) are uninitialized storage.
828 * - [%map, %map + map_size) is a valid, non-empty range.
829 * - [start.node, finish.node] is a valid range contained within
830 * [%map, %map + map_size).
831 * - A pointer in the range [%map, %map + map_size) points to an allocated
832 * node if and only if the pointer is in the range
833 * [start.node, finish.node].
835 * Here's the magic: nothing in deque is @b aware of the discontiguous
836 * storage!
838 * The memory setup and layout occurs in the parent, _Base, and the iterator
839 * class is entirely responsible for @a leaping from one node to the next.
840 * All the implementation routines for deque itself work only through the
841 * start and finish iterators. This keeps the routines simple and sane,
842 * and we can use other standard algorithms as well.
844 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
845 class deque : protected _Deque_base<_Tp, _Alloc>
847 #ifdef _GLIBCXX_CONCEPT_CHECKS
848 // concept requirements
849 typedef typename _Alloc::value_type _Alloc_value_type;
850 # if __cplusplus < 201103L
851 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
852 # endif
853 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
854 #endif
856 #if __cplusplus >= 201103L
857 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
858 "std::deque must have a non-const, non-volatile value_type");
859 # ifdef __STRICT_ANSI__
860 static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
861 "std::deque must have the same value_type as its allocator");
862 # endif
863 #endif
865 typedef _Deque_base<_Tp, _Alloc> _Base;
866 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
867 typedef typename _Base::_Alloc_traits _Alloc_traits;
868 typedef typename _Base::_Map_pointer _Map_pointer;
870 public:
871 typedef _Tp value_type;
872 typedef typename _Alloc_traits::pointer pointer;
873 typedef typename _Alloc_traits::const_pointer const_pointer;
874 typedef typename _Alloc_traits::reference reference;
875 typedef typename _Alloc_traits::const_reference const_reference;
876 typedef typename _Base::iterator iterator;
877 typedef typename _Base::const_iterator const_iterator;
878 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
879 typedef std::reverse_iterator<iterator> reverse_iterator;
880 typedef size_t size_type;
881 typedef ptrdiff_t difference_type;
882 typedef _Alloc allocator_type;
884 protected:
885 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
886 { return __deque_buf_size(sizeof(_Tp)); }
888 // Functions controlling memory layout, and nothing else.
889 using _Base::_M_initialize_map;
890 using _Base::_M_create_nodes;
891 using _Base::_M_destroy_nodes;
892 using _Base::_M_allocate_node;
893 using _Base::_M_deallocate_node;
894 using _Base::_M_allocate_map;
895 using _Base::_M_deallocate_map;
896 using _Base::_M_get_Tp_allocator;
899 * A total of four data members accumulated down the hierarchy.
900 * May be accessed via _M_impl.*
902 using _Base::_M_impl;
904 public:
905 // [23.2.1.1] construct/copy/destroy
906 // (assign() and get_allocator() are also listed in this section)
909 * @brief Creates a %deque with no elements.
911 deque() : _Base() { }
914 * @brief Creates a %deque with no elements.
915 * @param __a An allocator object.
917 explicit
918 deque(const allocator_type& __a)
919 : _Base(__a, 0) { }
921 #if __cplusplus >= 201103L
923 * @brief Creates a %deque with default constructed elements.
924 * @param __n The number of elements to initially create.
925 * @param __a An allocator.
927 * This constructor fills the %deque with @a n default
928 * constructed elements.
930 explicit
931 deque(size_type __n, const allocator_type& __a = allocator_type())
932 : _Base(__a, _S_check_init_len(__n, __a))
933 { _M_default_initialize(); }
936 * @brief Creates a %deque with copies of an exemplar element.
937 * @param __n The number of elements to initially create.
938 * @param __value An element to copy.
939 * @param __a An allocator.
941 * This constructor fills the %deque with @a __n copies of @a __value.
943 deque(size_type __n, const value_type& __value,
944 const allocator_type& __a = allocator_type())
945 : _Base(__a, _S_check_init_len(__n, __a))
946 { _M_fill_initialize(__value); }
947 #else
949 * @brief Creates a %deque with copies of an exemplar element.
950 * @param __n The number of elements to initially create.
951 * @param __value An element to copy.
952 * @param __a An allocator.
954 * This constructor fills the %deque with @a __n copies of @a __value.
956 explicit
957 deque(size_type __n, const value_type& __value = value_type(),
958 const allocator_type& __a = allocator_type())
959 : _Base(__a, _S_check_init_len(__n, __a))
960 { _M_fill_initialize(__value); }
961 #endif
964 * @brief %Deque copy constructor.
965 * @param __x A %deque of identical element and allocator types.
967 * The newly-created %deque uses a copy of the allocator object used
968 * by @a __x (unless the allocator traits dictate a different object).
970 deque(const deque& __x)
971 : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
972 __x.size())
973 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
974 this->_M_impl._M_start,
975 _M_get_Tp_allocator()); }
977 #if __cplusplus >= 201103L
979 * @brief %Deque move constructor.
980 * @param __x A %deque of identical element and allocator types.
982 * The newly-created %deque contains the exact contents of @a __x.
983 * The contents of @a __x are a valid, but unspecified %deque.
985 deque(deque&& __x)
986 : _Base(std::move(__x)) { }
988 /// Copy constructor with alternative allocator
989 deque(const deque& __x, const allocator_type& __a)
990 : _Base(__a, __x.size())
991 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
992 this->_M_impl._M_start,
993 _M_get_Tp_allocator()); }
995 /// Move constructor with alternative allocator
996 deque(deque&& __x, const allocator_type& __a)
997 : _Base(std::move(__x), __a, __x.size())
999 if (__x.get_allocator() != __a)
1001 std::__uninitialized_move_a(__x.begin(), __x.end(),
1002 this->_M_impl._M_start,
1003 _M_get_Tp_allocator());
1004 __x.clear();
1009 * @brief Builds a %deque from an initializer list.
1010 * @param __l An initializer_list.
1011 * @param __a An allocator object.
1013 * Create a %deque consisting of copies of the elements in the
1014 * initializer_list @a __l.
1016 * This will call the element type's copy constructor N times
1017 * (where N is __l.size()) and do no memory reallocation.
1019 deque(initializer_list<value_type> __l,
1020 const allocator_type& __a = allocator_type())
1021 : _Base(__a)
1023 _M_range_initialize(__l.begin(), __l.end(),
1024 random_access_iterator_tag());
1026 #endif
1029 * @brief Builds a %deque from a range.
1030 * @param __first An input iterator.
1031 * @param __last An input iterator.
1032 * @param __a An allocator object.
1034 * Create a %deque consisting of copies of the elements from [__first,
1035 * __last).
1037 * If the iterators are forward, bidirectional, or random-access, then
1038 * this will call the elements' copy constructor N times (where N is
1039 * distance(__first,__last)) and do no memory reallocation. But if only
1040 * input iterators are used, then this will do at most 2N calls to the
1041 * copy constructor, and logN memory reallocations.
1043 #if __cplusplus >= 201103L
1044 template<typename _InputIterator,
1045 typename = std::_RequireInputIter<_InputIterator>>
1046 deque(_InputIterator __first, _InputIterator __last,
1047 const allocator_type& __a = allocator_type())
1048 : _Base(__a)
1049 { _M_initialize_dispatch(__first, __last, __false_type()); }
1050 #else
1051 template<typename _InputIterator>
1052 deque(_InputIterator __first, _InputIterator __last,
1053 const allocator_type& __a = allocator_type())
1054 : _Base(__a)
1056 // Check whether it's an integral type. If so, it's not an iterator.
1057 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1058 _M_initialize_dispatch(__first, __last, _Integral());
1060 #endif
1063 * The dtor only erases the elements, and note that if the elements
1064 * themselves are pointers, the pointed-to memory is not touched in any
1065 * way. Managing the pointer is the user's responsibility.
1067 ~deque()
1068 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
1071 * @brief %Deque assignment operator.
1072 * @param __x A %deque of identical element and allocator types.
1074 * All the elements of @a x are copied.
1076 * The newly-created %deque uses a copy of the allocator object used
1077 * by @a __x (unless the allocator traits dictate a different object).
1079 deque&
1080 operator=(const deque& __x);
1082 #if __cplusplus >= 201103L
1084 * @brief %Deque move assignment operator.
1085 * @param __x A %deque of identical element and allocator types.
1087 * The contents of @a __x are moved into this deque (without copying,
1088 * if the allocators permit it).
1089 * @a __x is a valid, but unspecified %deque.
1091 deque&
1092 operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
1094 using __always_equal = typename _Alloc_traits::is_always_equal;
1095 _M_move_assign1(std::move(__x), __always_equal{});
1096 return *this;
1100 * @brief Assigns an initializer list to a %deque.
1101 * @param __l An initializer_list.
1103 * This function fills a %deque with copies of the elements in the
1104 * initializer_list @a __l.
1106 * Note that the assignment completely changes the %deque and that the
1107 * resulting %deque's size is the same as the number of elements
1108 * assigned.
1110 deque&
1111 operator=(initializer_list<value_type> __l)
1113 _M_assign_aux(__l.begin(), __l.end(),
1114 random_access_iterator_tag());
1115 return *this;
1117 #endif
1120 * @brief Assigns a given value to a %deque.
1121 * @param __n Number of elements to be assigned.
1122 * @param __val Value to be assigned.
1124 * This function fills a %deque with @a n copies of the given
1125 * value. Note that the assignment completely changes the
1126 * %deque and that the resulting %deque's size is the same as
1127 * the number of elements assigned.
1129 void
1130 assign(size_type __n, const value_type& __val)
1131 { _M_fill_assign(__n, __val); }
1134 * @brief Assigns a range to a %deque.
1135 * @param __first An input iterator.
1136 * @param __last An input iterator.
1138 * This function fills a %deque with copies of the elements in the
1139 * range [__first,__last).
1141 * Note that the assignment completely changes the %deque and that the
1142 * resulting %deque's size is the same as the number of elements
1143 * assigned.
1145 #if __cplusplus >= 201103L
1146 template<typename _InputIterator,
1147 typename = std::_RequireInputIter<_InputIterator>>
1148 void
1149 assign(_InputIterator __first, _InputIterator __last)
1150 { _M_assign_dispatch(__first, __last, __false_type()); }
1151 #else
1152 template<typename _InputIterator>
1153 void
1154 assign(_InputIterator __first, _InputIterator __last)
1156 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1157 _M_assign_dispatch(__first, __last, _Integral());
1159 #endif
1161 #if __cplusplus >= 201103L
1163 * @brief Assigns an initializer list to a %deque.
1164 * @param __l An initializer_list.
1166 * This function fills a %deque with copies of the elements in the
1167 * initializer_list @a __l.
1169 * Note that the assignment completely changes the %deque and that the
1170 * resulting %deque's size is the same as the number of elements
1171 * assigned.
1173 void
1174 assign(initializer_list<value_type> __l)
1175 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
1176 #endif
1178 /// Get a copy of the memory allocation object.
1179 allocator_type
1180 get_allocator() const _GLIBCXX_NOEXCEPT
1181 { return _Base::get_allocator(); }
1183 // iterators
1185 * Returns a read/write iterator that points to the first element in the
1186 * %deque. Iteration is done in ordinary element order.
1188 iterator
1189 begin() _GLIBCXX_NOEXCEPT
1190 { return this->_M_impl._M_start; }
1193 * Returns a read-only (constant) iterator that points to the first
1194 * element in the %deque. Iteration is done in ordinary element order.
1196 const_iterator
1197 begin() const _GLIBCXX_NOEXCEPT
1198 { return this->_M_impl._M_start; }
1201 * Returns a read/write iterator that points one past the last
1202 * element in the %deque. Iteration is done in ordinary
1203 * element order.
1205 iterator
1206 end() _GLIBCXX_NOEXCEPT
1207 { return this->_M_impl._M_finish; }
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.
1214 const_iterator
1215 end() const _GLIBCXX_NOEXCEPT
1216 { return this->_M_impl._M_finish; }
1219 * Returns a read/write reverse iterator that points to the
1220 * last element in the %deque. Iteration is done in reverse
1221 * element order.
1223 reverse_iterator
1224 rbegin() _GLIBCXX_NOEXCEPT
1225 { return reverse_iterator(this->_M_impl._M_finish); }
1228 * Returns a read-only (constant) reverse iterator that points
1229 * to the last element in the %deque. Iteration is done in
1230 * reverse element order.
1232 const_reverse_iterator
1233 rbegin() const _GLIBCXX_NOEXCEPT
1234 { return const_reverse_iterator(this->_M_impl._M_finish); }
1237 * Returns a read/write reverse iterator that points to one
1238 * before the first element in the %deque. Iteration is done
1239 * in reverse element order.
1241 reverse_iterator
1242 rend() _GLIBCXX_NOEXCEPT
1243 { return reverse_iterator(this->_M_impl._M_start); }
1246 * Returns a read-only (constant) reverse iterator that points
1247 * to one before the first element in the %deque. Iteration is
1248 * done in reverse element order.
1250 const_reverse_iterator
1251 rend() const _GLIBCXX_NOEXCEPT
1252 { return const_reverse_iterator(this->_M_impl._M_start); }
1254 #if __cplusplus >= 201103L
1256 * Returns a read-only (constant) iterator that points to the first
1257 * element in the %deque. Iteration is done in ordinary element order.
1259 const_iterator
1260 cbegin() const noexcept
1261 { return this->_M_impl._M_start; }
1264 * Returns a read-only (constant) iterator that points one past
1265 * the last element in the %deque. Iteration is done in
1266 * ordinary element order.
1268 const_iterator
1269 cend() const noexcept
1270 { return this->_M_impl._M_finish; }
1273 * Returns a read-only (constant) reverse iterator that points
1274 * to the last element in the %deque. Iteration is done in
1275 * reverse element order.
1277 const_reverse_iterator
1278 crbegin() const noexcept
1279 { return const_reverse_iterator(this->_M_impl._M_finish); }
1282 * Returns a read-only (constant) reverse iterator that points
1283 * to one before the first element in the %deque. Iteration is
1284 * done in reverse element order.
1286 const_reverse_iterator
1287 crend() const noexcept
1288 { return const_reverse_iterator(this->_M_impl._M_start); }
1289 #endif
1291 // [23.2.1.2] capacity
1292 /** Returns the number of elements in the %deque. */
1293 size_type
1294 size() const _GLIBCXX_NOEXCEPT
1295 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1297 /** Returns the size() of the largest possible %deque. */
1298 size_type
1299 max_size() const _GLIBCXX_NOEXCEPT
1300 { return _S_max_size(_M_get_Tp_allocator()); }
1302 #if __cplusplus >= 201103L
1304 * @brief Resizes the %deque to the specified number of elements.
1305 * @param __new_size Number of elements the %deque should contain.
1307 * This function will %resize the %deque to the specified
1308 * number of elements. If the number is smaller than the
1309 * %deque's current size the %deque is truncated, otherwise
1310 * default constructed elements are appended.
1312 void
1313 resize(size_type __new_size)
1315 const size_type __len = size();
1316 if (__new_size > __len)
1317 _M_default_append(__new_size - __len);
1318 else if (__new_size < __len)
1319 _M_erase_at_end(this->_M_impl._M_start
1320 + difference_type(__new_size));
1324 * @brief Resizes the %deque to the specified number of elements.
1325 * @param __new_size Number of elements the %deque should contain.
1326 * @param __x Data with which new elements should be populated.
1328 * This function will %resize the %deque to the specified
1329 * number of elements. If the number is smaller than the
1330 * %deque's current size the %deque is truncated, otherwise the
1331 * %deque is extended and new elements are populated with given
1332 * data.
1334 void
1335 resize(size_type __new_size, const value_type& __x)
1337 const size_type __len = size();
1338 if (__new_size > __len)
1339 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1340 else if (__new_size < __len)
1341 _M_erase_at_end(this->_M_impl._M_start
1342 + difference_type(__new_size));
1344 #else
1346 * @brief Resizes the %deque to the specified number of elements.
1347 * @param __new_size Number of elements the %deque should contain.
1348 * @param __x Data with which new elements should be populated.
1350 * This function will %resize the %deque to the specified
1351 * number of elements. If the number is smaller than the
1352 * %deque's current size the %deque is truncated, otherwise the
1353 * %deque is extended and new elements are populated with given
1354 * data.
1356 void
1357 resize(size_type __new_size, value_type __x = value_type())
1359 const size_type __len = size();
1360 if (__new_size > __len)
1361 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1362 else if (__new_size < __len)
1363 _M_erase_at_end(this->_M_impl._M_start
1364 + difference_type(__new_size));
1366 #endif
1368 #if __cplusplus >= 201103L
1369 /** A non-binding request to reduce memory use. */
1370 void
1371 shrink_to_fit() noexcept
1372 { _M_shrink_to_fit(); }
1373 #endif
1376 * Returns true if the %deque is empty. (Thus begin() would
1377 * equal end().)
1379 bool
1380 empty() const _GLIBCXX_NOEXCEPT
1381 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1383 // element access
1385 * @brief Subscript access to the data contained in the %deque.
1386 * @param __n The index of the element for which data should be
1387 * accessed.
1388 * @return Read/write reference to data.
1390 * This operator allows for easy, array-style, data access.
1391 * Note that data access with this operator is unchecked and
1392 * out_of_range lookups are not defined. (For checked lookups
1393 * see at().)
1395 reference
1396 operator[](size_type __n) _GLIBCXX_NOEXCEPT
1398 __glibcxx_requires_subscript(__n);
1399 return this->_M_impl._M_start[difference_type(__n)];
1403 * @brief Subscript access to the data contained in the %deque.
1404 * @param __n The index of the element for which data should be
1405 * accessed.
1406 * @return Read-only (constant) reference to data.
1408 * This operator allows for easy, array-style, data access.
1409 * Note that data access with this operator is unchecked and
1410 * out_of_range lookups are not defined. (For checked lookups
1411 * see at().)
1413 const_reference
1414 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1416 __glibcxx_requires_subscript(__n);
1417 return this->_M_impl._M_start[difference_type(__n)];
1420 protected:
1421 /// Safety check used only from at().
1422 void
1423 _M_range_check(size_type __n) const
1425 if (__n >= this->size())
1426 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1427 "(which is %zu)>= this->size() "
1428 "(which is %zu)"),
1429 __n, this->size());
1432 public:
1434 * @brief Provides access to the data contained in the %deque.
1435 * @param __n The index of the element for which data should be
1436 * accessed.
1437 * @return Read/write reference to data.
1438 * @throw std::out_of_range If @a __n is an invalid index.
1440 * This function provides for safer data access. The parameter
1441 * is first checked that it is in the range of the deque. The
1442 * function throws out_of_range if the check fails.
1444 reference
1445 at(size_type __n)
1447 _M_range_check(__n);
1448 return (*this)[__n];
1452 * @brief Provides access to the data contained in the %deque.
1453 * @param __n The index of the element for which data should be
1454 * accessed.
1455 * @return Read-only (constant) reference to data.
1456 * @throw std::out_of_range If @a __n is an invalid index.
1458 * This function provides for safer data access. The parameter is first
1459 * checked that it is in the range of the deque. The function throws
1460 * out_of_range if the check fails.
1462 const_reference
1463 at(size_type __n) const
1465 _M_range_check(__n);
1466 return (*this)[__n];
1470 * Returns a read/write reference to the data at the first
1471 * element of the %deque.
1473 reference
1474 front() _GLIBCXX_NOEXCEPT
1476 __glibcxx_requires_nonempty();
1477 return *begin();
1481 * Returns a read-only (constant) reference to the data at the first
1482 * element of the %deque.
1484 const_reference
1485 front() const _GLIBCXX_NOEXCEPT
1487 __glibcxx_requires_nonempty();
1488 return *begin();
1492 * Returns a read/write reference to the data at the last element of the
1493 * %deque.
1495 reference
1496 back() _GLIBCXX_NOEXCEPT
1498 __glibcxx_requires_nonempty();
1499 iterator __tmp = end();
1500 --__tmp;
1501 return *__tmp;
1505 * Returns a read-only (constant) reference to the data at the last
1506 * element of the %deque.
1508 const_reference
1509 back() const _GLIBCXX_NOEXCEPT
1511 __glibcxx_requires_nonempty();
1512 const_iterator __tmp = end();
1513 --__tmp;
1514 return *__tmp;
1517 // [23.2.1.2] modifiers
1519 * @brief Add data to the front of the %deque.
1520 * @param __x Data to be added.
1522 * This is a typical stack operation. The function creates an
1523 * element at the front of the %deque and assigns the given
1524 * data to it. Due to the nature of a %deque this operation
1525 * can be done in constant time.
1527 void
1528 push_front(const value_type& __x)
1530 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1532 _Alloc_traits::construct(this->_M_impl,
1533 this->_M_impl._M_start._M_cur - 1,
1534 __x);
1535 --this->_M_impl._M_start._M_cur;
1537 else
1538 _M_push_front_aux(__x);
1541 #if __cplusplus >= 201103L
1542 void
1543 push_front(value_type&& __x)
1544 { emplace_front(std::move(__x)); }
1546 template<typename... _Args>
1547 #if __cplusplus > 201402L
1548 reference
1549 #else
1550 void
1551 #endif
1552 emplace_front(_Args&&... __args);
1553 #endif
1556 * @brief Add data to the end of the %deque.
1557 * @param __x Data to be added.
1559 * This is a typical stack operation. The function creates an
1560 * element at the end of the %deque and assigns the given data
1561 * to it. Due to the nature of a %deque this operation can be
1562 * done in constant time.
1564 void
1565 push_back(const value_type& __x)
1567 if (this->_M_impl._M_finish._M_cur
1568 != this->_M_impl._M_finish._M_last - 1)
1570 _Alloc_traits::construct(this->_M_impl,
1571 this->_M_impl._M_finish._M_cur, __x);
1572 ++this->_M_impl._M_finish._M_cur;
1574 else
1575 _M_push_back_aux(__x);
1578 #if __cplusplus >= 201103L
1579 void
1580 push_back(value_type&& __x)
1581 { emplace_back(std::move(__x)); }
1583 template<typename... _Args>
1584 #if __cplusplus > 201402L
1585 reference
1586 #else
1587 void
1588 #endif
1589 emplace_back(_Args&&... __args);
1590 #endif
1593 * @brief Removes first element.
1595 * This is a typical stack operation. It shrinks the %deque by one.
1597 * Note that no data is returned, and if the first element's data is
1598 * needed, it should be retrieved before pop_front() is called.
1600 void
1601 pop_front() _GLIBCXX_NOEXCEPT
1603 __glibcxx_requires_nonempty();
1604 if (this->_M_impl._M_start._M_cur
1605 != this->_M_impl._M_start._M_last - 1)
1607 _Alloc_traits::destroy(this->_M_impl,
1608 this->_M_impl._M_start._M_cur);
1609 ++this->_M_impl._M_start._M_cur;
1611 else
1612 _M_pop_front_aux();
1616 * @brief Removes last element.
1618 * This is a typical stack operation. It shrinks the %deque by one.
1620 * Note that no data is returned, and if the last element's data is
1621 * needed, it should be retrieved before pop_back() is called.
1623 void
1624 pop_back() _GLIBCXX_NOEXCEPT
1626 __glibcxx_requires_nonempty();
1627 if (this->_M_impl._M_finish._M_cur
1628 != this->_M_impl._M_finish._M_first)
1630 --this->_M_impl._M_finish._M_cur;
1631 _Alloc_traits::destroy(this->_M_impl,
1632 this->_M_impl._M_finish._M_cur);
1634 else
1635 _M_pop_back_aux();
1638 #if __cplusplus >= 201103L
1640 * @brief Inserts an object in %deque before specified iterator.
1641 * @param __position A const_iterator into the %deque.
1642 * @param __args Arguments.
1643 * @return An iterator that points to the inserted data.
1645 * This function will insert an object of type T constructed
1646 * with T(std::forward<Args>(args)...) before the specified location.
1648 template<typename... _Args>
1649 iterator
1650 emplace(const_iterator __position, _Args&&... __args);
1653 * @brief Inserts given value into %deque before specified iterator.
1654 * @param __position A const_iterator into the %deque.
1655 * @param __x Data to be inserted.
1656 * @return An iterator that points to the inserted data.
1658 * This function will insert a copy of the given value before the
1659 * specified location.
1661 iterator
1662 insert(const_iterator __position, const value_type& __x);
1663 #else
1665 * @brief Inserts given value into %deque before specified iterator.
1666 * @param __position An iterator into the %deque.
1667 * @param __x Data to be inserted.
1668 * @return An iterator that points to the inserted data.
1670 * This function will insert a copy of the given value before the
1671 * specified location.
1673 iterator
1674 insert(iterator __position, const value_type& __x);
1675 #endif
1677 #if __cplusplus >= 201103L
1679 * @brief Inserts given rvalue into %deque before specified iterator.
1680 * @param __position A const_iterator into the %deque.
1681 * @param __x Data to be inserted.
1682 * @return An iterator that points to the inserted data.
1684 * This function will insert a copy of the given rvalue before the
1685 * specified location.
1687 iterator
1688 insert(const_iterator __position, value_type&& __x)
1689 { return emplace(__position, std::move(__x)); }
1692 * @brief Inserts an initializer list into the %deque.
1693 * @param __p An iterator into the %deque.
1694 * @param __l An initializer_list.
1696 * This function will insert copies of the data in the
1697 * initializer_list @a __l into the %deque before the location
1698 * specified by @a __p. This is known as <em>list insert</em>.
1700 iterator
1701 insert(const_iterator __p, initializer_list<value_type> __l)
1703 auto __offset = __p - cbegin();
1704 _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(),
1705 std::random_access_iterator_tag());
1706 return begin() + __offset;
1708 #endif
1710 #if __cplusplus >= 201103L
1712 * @brief Inserts a number of copies of given data into the %deque.
1713 * @param __position A const_iterator into the %deque.
1714 * @param __n Number of elements to be inserted.
1715 * @param __x Data to be inserted.
1716 * @return An iterator that points to the inserted data.
1718 * This function will insert a specified number of copies of the given
1719 * data before the location specified by @a __position.
1721 iterator
1722 insert(const_iterator __position, size_type __n, const value_type& __x)
1724 difference_type __offset = __position - cbegin();
1725 _M_fill_insert(__position._M_const_cast(), __n, __x);
1726 return begin() + __offset;
1728 #else
1730 * @brief Inserts a number of copies of given data into the %deque.
1731 * @param __position An iterator into the %deque.
1732 * @param __n Number of elements to be inserted.
1733 * @param __x Data to be inserted.
1735 * This function will insert a specified number of copies of the given
1736 * data before the location specified by @a __position.
1738 void
1739 insert(iterator __position, size_type __n, const value_type& __x)
1740 { _M_fill_insert(__position, __n, __x); }
1741 #endif
1743 #if __cplusplus >= 201103L
1745 * @brief Inserts a range into the %deque.
1746 * @param __position A const_iterator into the %deque.
1747 * @param __first An input iterator.
1748 * @param __last An input iterator.
1749 * @return An iterator that points to the inserted data.
1751 * This function will insert copies of the data in the range
1752 * [__first,__last) into the %deque before the location specified
1753 * by @a __position. This is known as <em>range insert</em>.
1755 template<typename _InputIterator,
1756 typename = std::_RequireInputIter<_InputIterator>>
1757 iterator
1758 insert(const_iterator __position, _InputIterator __first,
1759 _InputIterator __last)
1761 difference_type __offset = __position - cbegin();
1762 _M_insert_dispatch(__position._M_const_cast(),
1763 __first, __last, __false_type());
1764 return begin() + __offset;
1766 #else
1768 * @brief Inserts a range into the %deque.
1769 * @param __position An iterator into the %deque.
1770 * @param __first An input iterator.
1771 * @param __last An input iterator.
1773 * This function will insert copies of the data in the range
1774 * [__first,__last) into the %deque before the location specified
1775 * by @a __position. This is known as <em>range insert</em>.
1777 template<typename _InputIterator>
1778 void
1779 insert(iterator __position, _InputIterator __first,
1780 _InputIterator __last)
1782 // Check whether it's an integral type. If so, it's not an iterator.
1783 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1784 _M_insert_dispatch(__position, __first, __last, _Integral());
1786 #endif
1789 * @brief Remove element at given position.
1790 * @param __position Iterator pointing to element to be erased.
1791 * @return An iterator pointing to the next element (or end()).
1793 * This function will erase the element at the given position and thus
1794 * shorten the %deque by one.
1796 * The user is cautioned that
1797 * this function only erases the element, and that if the element is
1798 * itself a pointer, the pointed-to memory is not touched in any way.
1799 * Managing the pointer is the user's responsibility.
1801 iterator
1802 #if __cplusplus >= 201103L
1803 erase(const_iterator __position)
1804 #else
1805 erase(iterator __position)
1806 #endif
1807 { return _M_erase(__position._M_const_cast()); }
1810 * @brief Remove a range of elements.
1811 * @param __first Iterator pointing to the first element to be erased.
1812 * @param __last Iterator pointing to one past the last element to be
1813 * erased.
1814 * @return An iterator pointing to the element pointed to by @a last
1815 * prior to erasing (or end()).
1817 * This function will erase the elements in the range
1818 * [__first,__last) and shorten the %deque accordingly.
1820 * The user is cautioned that
1821 * this function only erases the elements, and that if the elements
1822 * themselves are pointers, the pointed-to memory is not touched in any
1823 * way. Managing the pointer is the user's responsibility.
1825 iterator
1826 #if __cplusplus >= 201103L
1827 erase(const_iterator __first, const_iterator __last)
1828 #else
1829 erase(iterator __first, iterator __last)
1830 #endif
1831 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1834 * @brief Swaps data with another %deque.
1835 * @param __x A %deque of the same element and allocator types.
1837 * This exchanges the elements between two deques in constant time.
1838 * (Four pointers, so it should be quite fast.)
1839 * Note that the global std::swap() function is specialized such that
1840 * std::swap(d1,d2) will feed to this function.
1842 * Whether the allocators are swapped depends on the allocator traits.
1844 void
1845 swap(deque& __x) _GLIBCXX_NOEXCEPT
1847 #if __cplusplus >= 201103L
1848 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1849 || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
1850 #endif
1851 _M_impl._M_swap_data(__x._M_impl);
1852 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1853 __x._M_get_Tp_allocator());
1857 * Erases all the elements. Note that this function only erases the
1858 * elements, and that if the elements themselves are pointers, the
1859 * pointed-to memory is not touched in any way. Managing the pointer is
1860 * the user's responsibility.
1862 void
1863 clear() _GLIBCXX_NOEXCEPT
1864 { _M_erase_at_end(begin()); }
1866 protected:
1867 // Internal constructor functions follow.
1869 // called by the range constructor to implement [23.1.1]/9
1871 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1872 // 438. Ambiguity in the "do the right thing" clause
1873 template<typename _Integer>
1874 void
1875 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1877 _M_initialize_map(_S_check_init_len(static_cast<size_type>(__n),
1878 _M_get_Tp_allocator()));
1879 _M_fill_initialize(__x);
1882 static size_t
1883 _S_check_init_len(size_t __n, const allocator_type& __a)
1885 if (__n > _S_max_size(__a))
1886 __throw_length_error(
1887 __N("cannot create std::deque larger than max_size()"));
1888 return __n;
1891 static size_type
1892 _S_max_size(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
1894 const size_t __diffmax = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max;
1895 const size_t __allocmax = _Alloc_traits::max_size(__a);
1896 return (std::min)(__diffmax, __allocmax);
1899 // called by the range constructor to implement [23.1.1]/9
1900 template<typename _InputIterator>
1901 void
1902 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1903 __false_type)
1905 _M_range_initialize(__first, __last,
1906 std::__iterator_category(__first));
1909 // called by the second initialize_dispatch above
1910 //@{
1912 * @brief Fills the deque with whatever is in [first,last).
1913 * @param __first An input iterator.
1914 * @param __last An input iterator.
1915 * @return Nothing.
1917 * If the iterators are actually forward iterators (or better), then the
1918 * memory layout can be done all at once. Else we move forward using
1919 * push_back on each value from the iterator.
1921 template<typename _InputIterator>
1922 void
1923 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1924 std::input_iterator_tag);
1926 // called by the second initialize_dispatch above
1927 template<typename _ForwardIterator>
1928 void
1929 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1930 std::forward_iterator_tag);
1931 //@}
1934 * @brief Fills the %deque with copies of value.
1935 * @param __value Initial value.
1936 * @return Nothing.
1937 * @pre _M_start and _M_finish have already been initialized,
1938 * but none of the %deque's elements have yet been constructed.
1940 * This function is called only when the user provides an explicit size
1941 * (with or without an explicit exemplar value).
1943 void
1944 _M_fill_initialize(const value_type& __value);
1946 #if __cplusplus >= 201103L
1947 // called by deque(n).
1948 void
1949 _M_default_initialize();
1950 #endif
1952 // Internal assign functions follow. The *_aux functions do the actual
1953 // assignment work for the range versions.
1955 // called by the range assign to implement [23.1.1]/9
1957 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1958 // 438. Ambiguity in the "do the right thing" clause
1959 template<typename _Integer>
1960 void
1961 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1962 { _M_fill_assign(__n, __val); }
1964 // called by the range assign to implement [23.1.1]/9
1965 template<typename _InputIterator>
1966 void
1967 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1968 __false_type)
1969 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1971 // called by the second assign_dispatch above
1972 template<typename _InputIterator>
1973 void
1974 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1975 std::input_iterator_tag);
1977 // called by the second assign_dispatch above
1978 template<typename _ForwardIterator>
1979 void
1980 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1981 std::forward_iterator_tag)
1983 const size_type __len = std::distance(__first, __last);
1984 if (__len > size())
1986 _ForwardIterator __mid = __first;
1987 std::advance(__mid, size());
1988 std::copy(__first, __mid, begin());
1989 _M_range_insert_aux(end(), __mid, __last,
1990 std::__iterator_category(__first));
1992 else
1993 _M_erase_at_end(std::copy(__first, __last, begin()));
1996 // Called by assign(n,t), and the range assign when it turns out
1997 // to be the same thing.
1998 void
1999 _M_fill_assign(size_type __n, const value_type& __val)
2001 if (__n > size())
2003 std::fill(begin(), end(), __val);
2004 _M_fill_insert(end(), __n - size(), __val);
2006 else
2008 _M_erase_at_end(begin() + difference_type(__n));
2009 std::fill(begin(), end(), __val);
2013 //@{
2014 /// Helper functions for push_* and pop_*.
2015 #if __cplusplus < 201103L
2016 void _M_push_back_aux(const value_type&);
2018 void _M_push_front_aux(const value_type&);
2019 #else
2020 template<typename... _Args>
2021 void _M_push_back_aux(_Args&&... __args);
2023 template<typename... _Args>
2024 void _M_push_front_aux(_Args&&... __args);
2025 #endif
2027 void _M_pop_back_aux();
2029 void _M_pop_front_aux();
2030 //@}
2032 // Internal insert functions follow. The *_aux functions do the actual
2033 // insertion work when all shortcuts fail.
2035 // called by the range insert to implement [23.1.1]/9
2037 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2038 // 438. Ambiguity in the "do the right thing" clause
2039 template<typename _Integer>
2040 void
2041 _M_insert_dispatch(iterator __pos,
2042 _Integer __n, _Integer __x, __true_type)
2043 { _M_fill_insert(__pos, __n, __x); }
2045 // called by the range insert to implement [23.1.1]/9
2046 template<typename _InputIterator>
2047 void
2048 _M_insert_dispatch(iterator __pos,
2049 _InputIterator __first, _InputIterator __last,
2050 __false_type)
2052 _M_range_insert_aux(__pos, __first, __last,
2053 std::__iterator_category(__first));
2056 // called by the second insert_dispatch above
2057 template<typename _InputIterator>
2058 void
2059 _M_range_insert_aux(iterator __pos, _InputIterator __first,
2060 _InputIterator __last, std::input_iterator_tag);
2062 // called by the second insert_dispatch above
2063 template<typename _ForwardIterator>
2064 void
2065 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
2066 _ForwardIterator __last, std::forward_iterator_tag);
2068 // Called by insert(p,n,x), and the range insert when it turns out to be
2069 // the same thing. Can use fill functions in optimal situations,
2070 // otherwise passes off to insert_aux(p,n,x).
2071 void
2072 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
2074 // called by insert(p,x)
2075 #if __cplusplus < 201103L
2076 iterator
2077 _M_insert_aux(iterator __pos, const value_type& __x);
2078 #else
2079 template<typename... _Args>
2080 iterator
2081 _M_insert_aux(iterator __pos, _Args&&... __args);
2082 #endif
2084 // called by insert(p,n,x) via fill_insert
2085 void
2086 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
2088 // called by range_insert_aux for forward iterators
2089 template<typename _ForwardIterator>
2090 void
2091 _M_insert_aux(iterator __pos,
2092 _ForwardIterator __first, _ForwardIterator __last,
2093 size_type __n);
2096 // Internal erase functions follow.
2098 void
2099 _M_destroy_data_aux(iterator __first, iterator __last);
2101 // Called by ~deque().
2102 // NB: Doesn't deallocate the nodes.
2103 template<typename _Alloc1>
2104 void
2105 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
2106 { _M_destroy_data_aux(__first, __last); }
2108 void
2109 _M_destroy_data(iterator __first, iterator __last,
2110 const std::allocator<_Tp>&)
2112 if (!__has_trivial_destructor(value_type))
2113 _M_destroy_data_aux(__first, __last);
2116 // Called by erase(q1, q2).
2117 void
2118 _M_erase_at_begin(iterator __pos)
2120 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
2121 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
2122 this->_M_impl._M_start = __pos;
2125 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2126 // _M_fill_assign, operator=.
2127 void
2128 _M_erase_at_end(iterator __pos)
2130 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
2131 _M_destroy_nodes(__pos._M_node + 1,
2132 this->_M_impl._M_finish._M_node + 1);
2133 this->_M_impl._M_finish = __pos;
2136 iterator
2137 _M_erase(iterator __pos);
2139 iterator
2140 _M_erase(iterator __first, iterator __last);
2142 #if __cplusplus >= 201103L
2143 // Called by resize(sz).
2144 void
2145 _M_default_append(size_type __n);
2147 bool
2148 _M_shrink_to_fit();
2149 #endif
2151 //@{
2152 /// Memory-handling helpers for the previous internal insert functions.
2153 iterator
2154 _M_reserve_elements_at_front(size_type __n)
2156 const size_type __vacancies = this->_M_impl._M_start._M_cur
2157 - this->_M_impl._M_start._M_first;
2158 if (__n > __vacancies)
2159 _M_new_elements_at_front(__n - __vacancies);
2160 return this->_M_impl._M_start - difference_type(__n);
2163 iterator
2164 _M_reserve_elements_at_back(size_type __n)
2166 const size_type __vacancies = (this->_M_impl._M_finish._M_last
2167 - this->_M_impl._M_finish._M_cur) - 1;
2168 if (__n > __vacancies)
2169 _M_new_elements_at_back(__n - __vacancies);
2170 return this->_M_impl._M_finish + difference_type(__n);
2173 void
2174 _M_new_elements_at_front(size_type __new_elements);
2176 void
2177 _M_new_elements_at_back(size_type __new_elements);
2178 //@}
2181 //@{
2183 * @brief Memory-handling helpers for the major %map.
2185 * Makes sure the _M_map has space for new nodes. Does not
2186 * actually add the nodes. Can invalidate _M_map pointers.
2187 * (And consequently, %deque iterators.)
2189 void
2190 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
2192 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
2193 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
2194 _M_reallocate_map(__nodes_to_add, false);
2197 void
2198 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
2200 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
2201 - this->_M_impl._M_map))
2202 _M_reallocate_map(__nodes_to_add, true);
2205 void
2206 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
2207 //@}
2209 #if __cplusplus >= 201103L
2210 // Constant-time, nothrow move assignment when source object's memory
2211 // can be moved because the allocators are equal.
2212 void
2213 _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
2215 this->_M_impl._M_swap_data(__x._M_impl);
2216 __x.clear();
2217 std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
2220 // When the allocators are not equal the operation could throw, because
2221 // we might need to allocate a new map for __x after moving from it
2222 // or we might need to allocate new elements for *this.
2223 void
2224 _M_move_assign1(deque&& __x, /* always equal: */ false_type)
2226 constexpr bool __move_storage =
2227 _Alloc_traits::_S_propagate_on_move_assign();
2228 _M_move_assign2(std::move(__x), __bool_constant<__move_storage>());
2231 // Destroy all elements and deallocate all memory, then replace
2232 // with elements created from __args.
2233 template<typename... _Args>
2234 void
2235 _M_replace_map(_Args&&... __args)
2237 // Create new data first, so if allocation fails there are no effects.
2238 deque __newobj(std::forward<_Args>(__args)...);
2239 // Free existing storage using existing allocator.
2240 clear();
2241 _M_deallocate_node(*begin()._M_node); // one node left after clear()
2242 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
2243 this->_M_impl._M_map = nullptr;
2244 this->_M_impl._M_map_size = 0;
2245 // Take ownership of replacement memory.
2246 this->_M_impl._M_swap_data(__newobj._M_impl);
2249 // Do move assignment when the allocator propagates.
2250 void
2251 _M_move_assign2(deque&& __x, /* propagate: */ true_type)
2253 // Make a copy of the original allocator state.
2254 auto __alloc = __x._M_get_Tp_allocator();
2255 // The allocator propagates so storage can be moved from __x,
2256 // leaving __x in a valid empty state with a moved-from allocator.
2257 _M_replace_map(std::move(__x));
2258 // Move the corresponding allocator state too.
2259 _M_get_Tp_allocator() = std::move(__alloc);
2262 // Do move assignment when it may not be possible to move source
2263 // object's memory, resulting in a linear-time operation.
2264 void
2265 _M_move_assign2(deque&& __x, /* propagate: */ false_type)
2267 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2269 // The allocators are equal so storage can be moved from __x,
2270 // leaving __x in a valid empty state with its current allocator.
2271 _M_replace_map(std::move(__x), __x.get_allocator());
2273 else
2275 // The rvalue's allocator cannot be moved and is not equal,
2276 // so we need to individually move each element.
2277 _M_assign_aux(std::__make_move_if_noexcept_iterator(__x.begin()),
2278 std::__make_move_if_noexcept_iterator(__x.end()),
2279 std::random_access_iterator_tag());
2280 __x.clear();
2283 #endif
2286 #if __cpp_deduction_guides >= 201606
2287 template<typename _InputIterator, typename _ValT
2288 = typename iterator_traits<_InputIterator>::value_type,
2289 typename _Allocator = allocator<_ValT>,
2290 typename = _RequireInputIter<_InputIterator>,
2291 typename = _RequireAllocator<_Allocator>>
2292 deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
2293 -> deque<_ValT, _Allocator>;
2294 #endif
2297 * @brief Deque equality comparison.
2298 * @param __x A %deque.
2299 * @param __y A %deque of the same type as @a __x.
2300 * @return True iff the size and elements of the deques are equal.
2302 * This is an equivalence relation. It is linear in the size of the
2303 * deques. Deques are considered equivalent if their sizes are equal,
2304 * and if corresponding elements compare equal.
2306 template<typename _Tp, typename _Alloc>
2307 inline bool
2308 operator==(const deque<_Tp, _Alloc>& __x,
2309 const deque<_Tp, _Alloc>& __y)
2310 { return __x.size() == __y.size()
2311 && std::equal(__x.begin(), __x.end(), __y.begin()); }
2314 * @brief Deque ordering relation.
2315 * @param __x A %deque.
2316 * @param __y A %deque of the same type as @a __x.
2317 * @return True iff @a x is lexicographically less than @a __y.
2319 * This is a total ordering relation. It is linear in the size of the
2320 * deques. The elements must be comparable with @c <.
2322 * See std::lexicographical_compare() for how the determination is made.
2324 template<typename _Tp, typename _Alloc>
2325 inline bool
2326 operator<(const deque<_Tp, _Alloc>& __x,
2327 const deque<_Tp, _Alloc>& __y)
2328 { return std::lexicographical_compare(__x.begin(), __x.end(),
2329 __y.begin(), __y.end()); }
2331 /// Based on operator==
2332 template<typename _Tp, typename _Alloc>
2333 inline bool
2334 operator!=(const deque<_Tp, _Alloc>& __x,
2335 const deque<_Tp, _Alloc>& __y)
2336 { return !(__x == __y); }
2338 /// Based on operator<
2339 template<typename _Tp, typename _Alloc>
2340 inline bool
2341 operator>(const deque<_Tp, _Alloc>& __x,
2342 const deque<_Tp, _Alloc>& __y)
2343 { return __y < __x; }
2345 /// Based on operator<
2346 template<typename _Tp, typename _Alloc>
2347 inline bool
2348 operator<=(const deque<_Tp, _Alloc>& __x,
2349 const deque<_Tp, _Alloc>& __y)
2350 { return !(__y < __x); }
2352 /// Based on operator<
2353 template<typename _Tp, typename _Alloc>
2354 inline bool
2355 operator>=(const deque<_Tp, _Alloc>& __x,
2356 const deque<_Tp, _Alloc>& __y)
2357 { return !(__x < __y); }
2359 /// See std::deque::swap().
2360 template<typename _Tp, typename _Alloc>
2361 inline void
2362 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
2363 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2364 { __x.swap(__y); }
2366 #undef _GLIBCXX_DEQUE_BUF_SIZE
2368 _GLIBCXX_END_NAMESPACE_CONTAINER
2369 _GLIBCXX_END_NAMESPACE_VERSION
2370 } // namespace std
2372 #endif /* _STL_DEQUE_H */