Fix handling of an empty filename at end of a path
[official-gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
blob58a01c894c0569c6ffa8e246ee59d3b9b033bf92
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;
496 typedef typename _Alloc_traits::size_type size_type;
498 allocator_type
499 get_allocator() const _GLIBCXX_NOEXCEPT
500 { return allocator_type(_M_get_Tp_allocator()); }
502 typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator;
503 typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator;
505 _Deque_base()
506 : _M_impl()
507 { _M_initialize_map(0); }
509 _Deque_base(size_t __num_elements)
510 : _M_impl()
511 { _M_initialize_map(__num_elements); }
513 _Deque_base(const allocator_type& __a, size_t __num_elements)
514 : _M_impl(__a)
515 { _M_initialize_map(__num_elements); }
517 _Deque_base(const allocator_type& __a)
518 : _M_impl(__a)
519 { /* Caller must initialize map. */ }
521 #if __cplusplus >= 201103L
522 _Deque_base(_Deque_base&& __x, false_type)
523 : _M_impl(__x._M_move_impl())
526 _Deque_base(_Deque_base&& __x, true_type)
527 : _M_impl(std::move(__x._M_get_Tp_allocator()))
529 _M_initialize_map(0);
530 if (__x._M_impl._M_map)
531 this->_M_impl._M_swap_data(__x._M_impl);
534 _Deque_base(_Deque_base&& __x)
535 : _Deque_base(std::move(__x), typename _Alloc_traits::is_always_equal{})
538 _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_type __n)
539 : _M_impl(__a)
541 if (__x.get_allocator() == __a)
543 if (__x._M_impl._M_map)
545 _M_initialize_map(0);
546 this->_M_impl._M_swap_data(__x._M_impl);
549 else
551 _M_initialize_map(__n);
554 #endif
556 ~_Deque_base() _GLIBCXX_NOEXCEPT;
558 protected:
559 typedef typename iterator::_Map_pointer _Map_pointer;
561 //This struct encapsulates the implementation of the std::deque
562 //standard container and at the same time makes use of the EBO
563 //for empty allocators.
564 struct _Deque_impl
565 : public _Tp_alloc_type
567 _Map_pointer _M_map;
568 size_t _M_map_size;
569 iterator _M_start;
570 iterator _M_finish;
572 _Deque_impl()
573 : _Tp_alloc_type(), _M_map(), _M_map_size(0),
574 _M_start(), _M_finish()
577 _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
578 : _Tp_alloc_type(__a), _M_map(), _M_map_size(0),
579 _M_start(), _M_finish()
582 #if __cplusplus >= 201103L
583 _Deque_impl(_Deque_impl&&) = default;
585 _Deque_impl(_Tp_alloc_type&& __a) noexcept
586 : _Tp_alloc_type(std::move(__a)), _M_map(), _M_map_size(0),
587 _M_start(), _M_finish()
589 #endif
591 void _M_swap_data(_Deque_impl& __x) _GLIBCXX_NOEXCEPT
593 using std::swap;
594 swap(this->_M_start, __x._M_start);
595 swap(this->_M_finish, __x._M_finish);
596 swap(this->_M_map, __x._M_map);
597 swap(this->_M_map_size, __x._M_map_size);
601 _Tp_alloc_type&
602 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
603 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
605 const _Tp_alloc_type&
606 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
607 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
609 _Map_alloc_type
610 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
611 { return _Map_alloc_type(_M_get_Tp_allocator()); }
613 _Ptr
614 _M_allocate_node()
616 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
617 return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
620 void
621 _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
623 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
624 _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
627 _Map_pointer
628 _M_allocate_map(size_t __n)
630 _Map_alloc_type __map_alloc = _M_get_map_allocator();
631 return _Map_alloc_traits::allocate(__map_alloc, __n);
634 void
635 _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
637 _Map_alloc_type __map_alloc = _M_get_map_allocator();
638 _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
641 protected:
642 void _M_initialize_map(size_t);
643 void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
644 void _M_destroy_nodes(_Map_pointer __nstart,
645 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
646 enum { _S_initial_map_size = 8 };
648 _Deque_impl _M_impl;
650 #if __cplusplus >= 201103L
651 private:
652 _Deque_impl
653 _M_move_impl()
655 if (!_M_impl._M_map)
656 return std::move(_M_impl);
658 // Create a copy of the current allocator.
659 _Tp_alloc_type __alloc{_M_get_Tp_allocator()};
660 // Put that copy in a moved-from state.
661 _Tp_alloc_type __sink __attribute((__unused__)) {std::move(__alloc)};
662 // Create an empty map that allocates using the moved-from allocator.
663 _Deque_base __empty{__alloc};
664 __empty._M_initialize_map(0);
665 // Now safe to modify current allocator and perform non-throwing swaps.
666 _Deque_impl __ret{std::move(_M_get_Tp_allocator())};
667 _M_impl._M_swap_data(__ret);
668 _M_impl._M_swap_data(__empty._M_impl);
669 return __ret;
671 #endif
674 template<typename _Tp, typename _Alloc>
675 _Deque_base<_Tp, _Alloc>::
676 ~_Deque_base() _GLIBCXX_NOEXCEPT
678 if (this->_M_impl._M_map)
680 _M_destroy_nodes(this->_M_impl._M_start._M_node,
681 this->_M_impl._M_finish._M_node + 1);
682 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
687 * @brief Layout storage.
688 * @param __num_elements The count of T's for which to allocate space
689 * at first.
690 * @return Nothing.
692 * The initial underlying memory layout is a bit complicated...
694 template<typename _Tp, typename _Alloc>
695 void
696 _Deque_base<_Tp, _Alloc>::
697 _M_initialize_map(size_t __num_elements)
699 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
700 + 1);
702 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
703 size_t(__num_nodes + 2));
704 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
706 // For "small" maps (needing less than _M_map_size nodes), allocation
707 // starts in the middle elements and grows outwards. So nstart may be
708 // the beginning of _M_map, but for small maps it may be as far in as
709 // _M_map+3.
711 _Map_pointer __nstart = (this->_M_impl._M_map
712 + (this->_M_impl._M_map_size - __num_nodes) / 2);
713 _Map_pointer __nfinish = __nstart + __num_nodes;
715 __try
716 { _M_create_nodes(__nstart, __nfinish); }
717 __catch(...)
719 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
720 this->_M_impl._M_map = _Map_pointer();
721 this->_M_impl._M_map_size = 0;
722 __throw_exception_again;
725 this->_M_impl._M_start._M_set_node(__nstart);
726 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
727 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
728 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
729 + __num_elements
730 % __deque_buf_size(sizeof(_Tp)));
733 template<typename _Tp, typename _Alloc>
734 void
735 _Deque_base<_Tp, _Alloc>::
736 _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
738 _Map_pointer __cur;
739 __try
741 for (__cur = __nstart; __cur < __nfinish; ++__cur)
742 *__cur = this->_M_allocate_node();
744 __catch(...)
746 _M_destroy_nodes(__nstart, __cur);
747 __throw_exception_again;
751 template<typename _Tp, typename _Alloc>
752 void
753 _Deque_base<_Tp, _Alloc>::
754 _M_destroy_nodes(_Map_pointer __nstart,
755 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
757 for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
758 _M_deallocate_node(*__n);
762 * @brief A standard container using fixed-size memory allocation and
763 * constant-time manipulation of elements at either end.
765 * @ingroup sequences
767 * @tparam _Tp Type of element.
768 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
770 * Meets the requirements of a <a href="tables.html#65">container</a>, a
771 * <a href="tables.html#66">reversible container</a>, and a
772 * <a href="tables.html#67">sequence</a>, including the
773 * <a href="tables.html#68">optional sequence requirements</a>.
775 * In previous HP/SGI versions of deque, there was an extra template
776 * parameter so users could control the node size. This extension turned
777 * out to violate the C++ standard (it can be detected using template
778 * template parameters), and it was removed.
780 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
782 * - Tp** _M_map
783 * - size_t _M_map_size
784 * - iterator _M_start, _M_finish
786 * map_size is at least 8. %map is an array of map_size
787 * pointers-to-@a nodes. (The name %map has nothing to do with the
788 * std::map class, and @b nodes should not be confused with
789 * std::list's usage of @a node.)
791 * A @a node has no specific type name as such, but it is referred
792 * to as @a node in this file. It is a simple array-of-Tp. If Tp
793 * is very large, there will be one Tp element per node (i.e., an
794 * @a array of one). For non-huge Tp's, node size is inversely
795 * related to Tp size: the larger the Tp, the fewer Tp's will fit
796 * in a node. The goal here is to keep the total size of a node
797 * relatively small and constant over different Tp's, to improve
798 * allocator efficiency.
800 * Not every pointer in the %map array will point to a node. If
801 * the initial number of elements in the deque is small, the
802 * /middle/ %map pointers will be valid, and the ones at the edges
803 * will be unused. This same situation will arise as the %map
804 * grows: available %map pointers, if any, will be on the ends. As
805 * new nodes are created, only a subset of the %map's pointers need
806 * to be copied @a outward.
808 * Class invariants:
809 * - For any nonsingular iterator i:
810 * - i.node points to a member of the %map array. (Yes, you read that
811 * correctly: i.node does not actually point to a node.) The member of
812 * the %map array is what actually points to the node.
813 * - i.first == *(i.node) (This points to the node (first Tp element).)
814 * - i.last == i.first + node_size
815 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
816 * the implication of this is that i.cur is always a dereferenceable
817 * pointer, even if i is a past-the-end iterator.
818 * - Start and Finish are always nonsingular iterators. NOTE: this
819 * means that an empty deque must have one node, a deque with <N
820 * elements (where N is the node buffer size) must have one node, a
821 * deque with N through (2N-1) elements must have two nodes, etc.
822 * - For every node other than start.node and finish.node, every
823 * element in the node is an initialized object. If start.node ==
824 * finish.node, then [start.cur, finish.cur) are initialized
825 * objects, and the elements outside that range are uninitialized
826 * storage. Otherwise, [start.cur, start.last) and [finish.first,
827 * finish.cur) are initialized objects, and [start.first, start.cur)
828 * and [finish.cur, finish.last) are uninitialized storage.
829 * - [%map, %map + map_size) is a valid, non-empty range.
830 * - [start.node, finish.node] is a valid range contained within
831 * [%map, %map + map_size).
832 * - A pointer in the range [%map, %map + map_size) points to an allocated
833 * node if and only if the pointer is in the range
834 * [start.node, finish.node].
836 * Here's the magic: nothing in deque is @b aware of the discontiguous
837 * storage!
839 * The memory setup and layout occurs in the parent, _Base, and the iterator
840 * class is entirely responsible for @a leaping from one node to the next.
841 * All the implementation routines for deque itself work only through the
842 * start and finish iterators. This keeps the routines simple and sane,
843 * and we can use other standard algorithms as well.
845 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
846 class deque : protected _Deque_base<_Tp, _Alloc>
848 #ifdef _GLIBCXX_CONCEPT_CHECKS
849 // concept requirements
850 typedef typename _Alloc::value_type _Alloc_value_type;
851 # if __cplusplus < 201103L
852 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
853 # endif
854 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
855 #endif
857 #if __cplusplus >= 201103L
858 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
859 "std::deque must have a non-const, non-volatile value_type");
860 # ifdef __STRICT_ANSI__
861 static_assert(is_same<typename _Alloc::value_type, _Tp>::value,
862 "std::deque must have the same value_type as its allocator");
863 # endif
864 #endif
866 typedef _Deque_base<_Tp, _Alloc> _Base;
867 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
868 typedef typename _Base::_Alloc_traits _Alloc_traits;
869 typedef typename _Base::_Map_pointer _Map_pointer;
871 public:
872 typedef _Tp value_type;
873 typedef typename _Alloc_traits::pointer pointer;
874 typedef typename _Alloc_traits::const_pointer const_pointer;
875 typedef typename _Alloc_traits::reference reference;
876 typedef typename _Alloc_traits::const_reference const_reference;
877 typedef typename _Base::iterator iterator;
878 typedef typename _Base::const_iterator const_iterator;
879 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
880 typedef std::reverse_iterator<iterator> reverse_iterator;
881 typedef size_t size_type;
882 typedef ptrdiff_t difference_type;
883 typedef _Alloc allocator_type;
885 protected:
886 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
887 { return __deque_buf_size(sizeof(_Tp)); }
889 // Functions controlling memory layout, and nothing else.
890 using _Base::_M_initialize_map;
891 using _Base::_M_create_nodes;
892 using _Base::_M_destroy_nodes;
893 using _Base::_M_allocate_node;
894 using _Base::_M_deallocate_node;
895 using _Base::_M_allocate_map;
896 using _Base::_M_deallocate_map;
897 using _Base::_M_get_Tp_allocator;
900 * A total of four data members accumulated down the hierarchy.
901 * May be accessed via _M_impl.*
903 using _Base::_M_impl;
905 public:
906 // [23.2.1.1] construct/copy/destroy
907 // (assign() and get_allocator() are also listed in this section)
910 * @brief Creates a %deque with no elements.
912 deque() : _Base() { }
915 * @brief Creates a %deque with no elements.
916 * @param __a An allocator object.
918 explicit
919 deque(const allocator_type& __a)
920 : _Base(__a, 0) { }
922 #if __cplusplus >= 201103L
924 * @brief Creates a %deque with default constructed elements.
925 * @param __n The number of elements to initially create.
926 * @param __a An allocator.
928 * This constructor fills the %deque with @a n default
929 * constructed elements.
931 explicit
932 deque(size_type __n, const allocator_type& __a = allocator_type())
933 : _Base(__a, __n)
934 { _M_default_initialize(); }
937 * @brief Creates a %deque with copies of an exemplar element.
938 * @param __n The number of elements to initially create.
939 * @param __value An element to copy.
940 * @param __a An allocator.
942 * This constructor fills the %deque with @a __n copies of @a __value.
944 deque(size_type __n, const value_type& __value,
945 const allocator_type& __a = allocator_type())
946 : _Base(__a, __n)
947 { _M_fill_initialize(__value); }
948 #else
950 * @brief Creates a %deque with copies of an exemplar element.
951 * @param __n The number of elements to initially create.
952 * @param __value An element to copy.
953 * @param __a An allocator.
955 * This constructor fills the %deque with @a __n copies of @a __value.
957 explicit
958 deque(size_type __n, const value_type& __value = value_type(),
959 const allocator_type& __a = allocator_type())
960 : _Base(__a, __n)
961 { _M_fill_initialize(__value); }
962 #endif
965 * @brief %Deque copy constructor.
966 * @param __x A %deque of identical element and allocator types.
968 * The newly-created %deque uses a copy of the allocator object used
969 * by @a __x (unless the allocator traits dictate a different object).
971 deque(const deque& __x)
972 : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
973 __x.size())
974 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
975 this->_M_impl._M_start,
976 _M_get_Tp_allocator()); }
978 #if __cplusplus >= 201103L
980 * @brief %Deque move constructor.
981 * @param __x A %deque of identical element and allocator types.
983 * The newly-created %deque contains the exact contents of @a __x.
984 * The contents of @a __x are a valid, but unspecified %deque.
986 deque(deque&& __x)
987 : _Base(std::move(__x)) { }
989 /// Copy constructor with alternative allocator
990 deque(const deque& __x, const allocator_type& __a)
991 : _Base(__a, __x.size())
992 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
993 this->_M_impl._M_start,
994 _M_get_Tp_allocator()); }
996 /// Move constructor with alternative allocator
997 deque(deque&& __x, const allocator_type& __a)
998 : _Base(std::move(__x), __a, __x.size())
1000 if (__x.get_allocator() != __a)
1002 std::__uninitialized_move_a(__x.begin(), __x.end(),
1003 this->_M_impl._M_start,
1004 _M_get_Tp_allocator());
1005 __x.clear();
1010 * @brief Builds a %deque from an initializer list.
1011 * @param __l An initializer_list.
1012 * @param __a An allocator object.
1014 * Create a %deque consisting of copies of the elements in the
1015 * initializer_list @a __l.
1017 * This will call the element type's copy constructor N times
1018 * (where N is __l.size()) and do no memory reallocation.
1020 deque(initializer_list<value_type> __l,
1021 const allocator_type& __a = allocator_type())
1022 : _Base(__a)
1024 _M_range_initialize(__l.begin(), __l.end(),
1025 random_access_iterator_tag());
1027 #endif
1030 * @brief Builds a %deque from a range.
1031 * @param __first An input iterator.
1032 * @param __last An input iterator.
1033 * @param __a An allocator object.
1035 * Create a %deque consisting of copies of the elements from [__first,
1036 * __last).
1038 * If the iterators are forward, bidirectional, or random-access, then
1039 * this will call the elements' copy constructor N times (where N is
1040 * distance(__first,__last)) and do no memory reallocation. But if only
1041 * input iterators are used, then this will do at most 2N calls to the
1042 * copy constructor, and logN memory reallocations.
1044 #if __cplusplus >= 201103L
1045 template<typename _InputIterator,
1046 typename = std::_RequireInputIter<_InputIterator>>
1047 deque(_InputIterator __first, _InputIterator __last,
1048 const allocator_type& __a = allocator_type())
1049 : _Base(__a)
1050 { _M_initialize_dispatch(__first, __last, __false_type()); }
1051 #else
1052 template<typename _InputIterator>
1053 deque(_InputIterator __first, _InputIterator __last,
1054 const allocator_type& __a = allocator_type())
1055 : _Base(__a)
1057 // Check whether it's an integral type. If so, it's not an iterator.
1058 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1059 _M_initialize_dispatch(__first, __last, _Integral());
1061 #endif
1064 * The dtor only erases the elements, and note that if the elements
1065 * themselves are pointers, the pointed-to memory is not touched in any
1066 * way. Managing the pointer is the user's responsibility.
1068 ~deque()
1069 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
1072 * @brief %Deque assignment operator.
1073 * @param __x A %deque of identical element and allocator types.
1075 * All the elements of @a x are copied.
1077 * The newly-created %deque uses a copy of the allocator object used
1078 * by @a __x (unless the allocator traits dictate a different object).
1080 deque&
1081 operator=(const deque& __x);
1083 #if __cplusplus >= 201103L
1085 * @brief %Deque move assignment operator.
1086 * @param __x A %deque of identical element and allocator types.
1088 * The contents of @a __x are moved into this deque (without copying,
1089 * if the allocators permit it).
1090 * @a __x is a valid, but unspecified %deque.
1092 deque&
1093 operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
1095 using __always_equal = typename _Alloc_traits::is_always_equal;
1096 _M_move_assign1(std::move(__x), __always_equal{});
1097 return *this;
1101 * @brief Assigns an initializer list to a %deque.
1102 * @param __l An initializer_list.
1104 * This function fills a %deque with copies of the elements in the
1105 * initializer_list @a __l.
1107 * Note that the assignment completely changes the %deque and that the
1108 * resulting %deque's size is the same as the number of elements
1109 * assigned.
1111 deque&
1112 operator=(initializer_list<value_type> __l)
1114 _M_assign_aux(__l.begin(), __l.end(),
1115 random_access_iterator_tag());
1116 return *this;
1118 #endif
1121 * @brief Assigns a given value to a %deque.
1122 * @param __n Number of elements to be assigned.
1123 * @param __val Value to be assigned.
1125 * This function fills a %deque with @a n copies of the given
1126 * value. Note that the assignment completely changes the
1127 * %deque and that the resulting %deque's size is the same as
1128 * the number of elements assigned.
1130 void
1131 assign(size_type __n, const value_type& __val)
1132 { _M_fill_assign(__n, __val); }
1135 * @brief Assigns a range to a %deque.
1136 * @param __first An input iterator.
1137 * @param __last An input iterator.
1139 * This function fills a %deque with copies of the elements in the
1140 * range [__first,__last).
1142 * Note that the assignment completely changes the %deque and that the
1143 * resulting %deque's size is the same as the number of elements
1144 * assigned.
1146 #if __cplusplus >= 201103L
1147 template<typename _InputIterator,
1148 typename = std::_RequireInputIter<_InputIterator>>
1149 void
1150 assign(_InputIterator __first, _InputIterator __last)
1151 { _M_assign_dispatch(__first, __last, __false_type()); }
1152 #else
1153 template<typename _InputIterator>
1154 void
1155 assign(_InputIterator __first, _InputIterator __last)
1157 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1158 _M_assign_dispatch(__first, __last, _Integral());
1160 #endif
1162 #if __cplusplus >= 201103L
1164 * @brief Assigns an initializer list to a %deque.
1165 * @param __l An initializer_list.
1167 * This function fills a %deque with copies of the elements in the
1168 * initializer_list @a __l.
1170 * Note that the assignment completely changes the %deque and that the
1171 * resulting %deque's size is the same as the number of elements
1172 * assigned.
1174 void
1175 assign(initializer_list<value_type> __l)
1176 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
1177 #endif
1179 /// Get a copy of the memory allocation object.
1180 allocator_type
1181 get_allocator() const _GLIBCXX_NOEXCEPT
1182 { return _Base::get_allocator(); }
1184 // iterators
1186 * Returns a read/write iterator that points to the first element in the
1187 * %deque. Iteration is done in ordinary element order.
1189 iterator
1190 begin() _GLIBCXX_NOEXCEPT
1191 { return this->_M_impl._M_start; }
1194 * Returns a read-only (constant) iterator that points to the first
1195 * element in the %deque. Iteration is done in ordinary element order.
1197 const_iterator
1198 begin() const _GLIBCXX_NOEXCEPT
1199 { return this->_M_impl._M_start; }
1202 * Returns a read/write iterator that points one past the last
1203 * element in the %deque. Iteration is done in ordinary
1204 * element order.
1206 iterator
1207 end() _GLIBCXX_NOEXCEPT
1208 { return this->_M_impl._M_finish; }
1211 * Returns a read-only (constant) iterator that points one past
1212 * the last element in the %deque. Iteration is done in
1213 * ordinary element order.
1215 const_iterator
1216 end() const _GLIBCXX_NOEXCEPT
1217 { return this->_M_impl._M_finish; }
1220 * Returns a read/write reverse iterator that points to the
1221 * last element in the %deque. Iteration is done in reverse
1222 * element order.
1224 reverse_iterator
1225 rbegin() _GLIBCXX_NOEXCEPT
1226 { return reverse_iterator(this->_M_impl._M_finish); }
1229 * Returns a read-only (constant) reverse iterator that points
1230 * to the last element in the %deque. Iteration is done in
1231 * reverse element order.
1233 const_reverse_iterator
1234 rbegin() const _GLIBCXX_NOEXCEPT
1235 { return const_reverse_iterator(this->_M_impl._M_finish); }
1238 * Returns a read/write reverse iterator that points to one
1239 * before the first element in the %deque. Iteration is done
1240 * in reverse element order.
1242 reverse_iterator
1243 rend() _GLIBCXX_NOEXCEPT
1244 { return reverse_iterator(this->_M_impl._M_start); }
1247 * Returns a read-only (constant) reverse iterator that points
1248 * to one before the first element in the %deque. Iteration is
1249 * done in reverse element order.
1251 const_reverse_iterator
1252 rend() const _GLIBCXX_NOEXCEPT
1253 { return const_reverse_iterator(this->_M_impl._M_start); }
1255 #if __cplusplus >= 201103L
1257 * Returns a read-only (constant) iterator that points to the first
1258 * element in the %deque. Iteration is done in ordinary element order.
1260 const_iterator
1261 cbegin() const noexcept
1262 { return this->_M_impl._M_start; }
1265 * Returns a read-only (constant) iterator that points one past
1266 * the last element in the %deque. Iteration is done in
1267 * ordinary element order.
1269 const_iterator
1270 cend() const noexcept
1271 { return this->_M_impl._M_finish; }
1274 * Returns a read-only (constant) reverse iterator that points
1275 * to the last element in the %deque. Iteration is done in
1276 * reverse element order.
1278 const_reverse_iterator
1279 crbegin() const noexcept
1280 { return const_reverse_iterator(this->_M_impl._M_finish); }
1283 * Returns a read-only (constant) reverse iterator that points
1284 * to one before the first element in the %deque. Iteration is
1285 * done in reverse element order.
1287 const_reverse_iterator
1288 crend() const noexcept
1289 { return const_reverse_iterator(this->_M_impl._M_start); }
1290 #endif
1292 // [23.2.1.2] capacity
1293 /** Returns the number of elements in the %deque. */
1294 size_type
1295 size() const _GLIBCXX_NOEXCEPT
1296 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1298 /** Returns the size() of the largest possible %deque. */
1299 size_type
1300 max_size() const _GLIBCXX_NOEXCEPT
1301 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
1303 #if __cplusplus >= 201103L
1305 * @brief Resizes the %deque to the specified number of elements.
1306 * @param __new_size Number of elements the %deque should contain.
1308 * This function will %resize the %deque to the specified
1309 * number of elements. If the number is smaller than the
1310 * %deque's current size the %deque is truncated, otherwise
1311 * default constructed elements are appended.
1313 void
1314 resize(size_type __new_size)
1316 const size_type __len = size();
1317 if (__new_size > __len)
1318 _M_default_append(__new_size - __len);
1319 else if (__new_size < __len)
1320 _M_erase_at_end(this->_M_impl._M_start
1321 + difference_type(__new_size));
1325 * @brief Resizes the %deque to the specified number of elements.
1326 * @param __new_size Number of elements the %deque should contain.
1327 * @param __x Data with which new elements should be populated.
1329 * This function will %resize the %deque to the specified
1330 * number of elements. If the number is smaller than the
1331 * %deque's current size the %deque is truncated, otherwise the
1332 * %deque is extended and new elements are populated with given
1333 * data.
1335 void
1336 resize(size_type __new_size, const value_type& __x)
1338 const size_type __len = size();
1339 if (__new_size > __len)
1340 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1341 else if (__new_size < __len)
1342 _M_erase_at_end(this->_M_impl._M_start
1343 + difference_type(__new_size));
1345 #else
1347 * @brief Resizes the %deque to the specified number of elements.
1348 * @param __new_size Number of elements the %deque should contain.
1349 * @param __x Data with which new elements should be populated.
1351 * This function will %resize the %deque to the specified
1352 * number of elements. If the number is smaller than the
1353 * %deque's current size the %deque is truncated, otherwise the
1354 * %deque is extended and new elements are populated with given
1355 * data.
1357 void
1358 resize(size_type __new_size, value_type __x = value_type())
1360 const size_type __len = size();
1361 if (__new_size > __len)
1362 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1363 else if (__new_size < __len)
1364 _M_erase_at_end(this->_M_impl._M_start
1365 + difference_type(__new_size));
1367 #endif
1369 #if __cplusplus >= 201103L
1370 /** A non-binding request to reduce memory use. */
1371 void
1372 shrink_to_fit() noexcept
1373 { _M_shrink_to_fit(); }
1374 #endif
1377 * Returns true if the %deque is empty. (Thus begin() would
1378 * equal end().)
1380 bool
1381 empty() const _GLIBCXX_NOEXCEPT
1382 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1384 // element access
1386 * @brief Subscript access to the data contained in the %deque.
1387 * @param __n The index of the element for which data should be
1388 * accessed.
1389 * @return Read/write reference to data.
1391 * This operator allows for easy, array-style, data access.
1392 * Note that data access with this operator is unchecked and
1393 * out_of_range lookups are not defined. (For checked lookups
1394 * see at().)
1396 reference
1397 operator[](size_type __n) _GLIBCXX_NOEXCEPT
1399 __glibcxx_requires_subscript(__n);
1400 return this->_M_impl._M_start[difference_type(__n)];
1404 * @brief Subscript access to the data contained in the %deque.
1405 * @param __n The index of the element for which data should be
1406 * accessed.
1407 * @return Read-only (constant) reference to data.
1409 * This operator allows for easy, array-style, data access.
1410 * Note that data access with this operator is unchecked and
1411 * out_of_range lookups are not defined. (For checked lookups
1412 * see at().)
1414 const_reference
1415 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1417 __glibcxx_requires_subscript(__n);
1418 return this->_M_impl._M_start[difference_type(__n)];
1421 protected:
1422 /// Safety check used only from at().
1423 void
1424 _M_range_check(size_type __n) const
1426 if (__n >= this->size())
1427 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1428 "(which is %zu)>= this->size() "
1429 "(which is %zu)"),
1430 __n, this->size());
1433 public:
1435 * @brief Provides access to the data contained in the %deque.
1436 * @param __n The index of the element for which data should be
1437 * accessed.
1438 * @return Read/write reference to data.
1439 * @throw std::out_of_range If @a __n is an invalid index.
1441 * This function provides for safer data access. The parameter
1442 * is first checked that it is in the range of the deque. The
1443 * function throws out_of_range if the check fails.
1445 reference
1446 at(size_type __n)
1448 _M_range_check(__n);
1449 return (*this)[__n];
1453 * @brief Provides access to the data contained in the %deque.
1454 * @param __n The index of the element for which data should be
1455 * accessed.
1456 * @return Read-only (constant) reference to data.
1457 * @throw std::out_of_range If @a __n is an invalid index.
1459 * This function provides for safer data access. The parameter is first
1460 * checked that it is in the range of the deque. The function throws
1461 * out_of_range if the check fails.
1463 const_reference
1464 at(size_type __n) const
1466 _M_range_check(__n);
1467 return (*this)[__n];
1471 * Returns a read/write reference to the data at the first
1472 * element of the %deque.
1474 reference
1475 front() _GLIBCXX_NOEXCEPT
1477 __glibcxx_requires_nonempty();
1478 return *begin();
1482 * Returns a read-only (constant) reference to the data at the first
1483 * element of the %deque.
1485 const_reference
1486 front() const _GLIBCXX_NOEXCEPT
1488 __glibcxx_requires_nonempty();
1489 return *begin();
1493 * Returns a read/write reference to the data at the last element of the
1494 * %deque.
1496 reference
1497 back() _GLIBCXX_NOEXCEPT
1499 __glibcxx_requires_nonempty();
1500 iterator __tmp = end();
1501 --__tmp;
1502 return *__tmp;
1506 * Returns a read-only (constant) reference to the data at the last
1507 * element of the %deque.
1509 const_reference
1510 back() const _GLIBCXX_NOEXCEPT
1512 __glibcxx_requires_nonempty();
1513 const_iterator __tmp = end();
1514 --__tmp;
1515 return *__tmp;
1518 // [23.2.1.2] modifiers
1520 * @brief Add data to the front of the %deque.
1521 * @param __x Data to be added.
1523 * This is a typical stack operation. The function creates an
1524 * element at the front of the %deque and assigns the given
1525 * data to it. Due to the nature of a %deque this operation
1526 * can be done in constant time.
1528 void
1529 push_front(const value_type& __x)
1531 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1533 _Alloc_traits::construct(this->_M_impl,
1534 this->_M_impl._M_start._M_cur - 1,
1535 __x);
1536 --this->_M_impl._M_start._M_cur;
1538 else
1539 _M_push_front_aux(__x);
1542 #if __cplusplus >= 201103L
1543 void
1544 push_front(value_type&& __x)
1545 { emplace_front(std::move(__x)); }
1547 template<typename... _Args>
1548 #if __cplusplus > 201402L
1549 reference
1550 #else
1551 void
1552 #endif
1553 emplace_front(_Args&&... __args);
1554 #endif
1557 * @brief Add data to the end of the %deque.
1558 * @param __x Data to be added.
1560 * This is a typical stack operation. The function creates an
1561 * element at the end of the %deque and assigns the given data
1562 * to it. Due to the nature of a %deque this operation can be
1563 * done in constant time.
1565 void
1566 push_back(const value_type& __x)
1568 if (this->_M_impl._M_finish._M_cur
1569 != this->_M_impl._M_finish._M_last - 1)
1571 _Alloc_traits::construct(this->_M_impl,
1572 this->_M_impl._M_finish._M_cur, __x);
1573 ++this->_M_impl._M_finish._M_cur;
1575 else
1576 _M_push_back_aux(__x);
1579 #if __cplusplus >= 201103L
1580 void
1581 push_back(value_type&& __x)
1582 { emplace_back(std::move(__x)); }
1584 template<typename... _Args>
1585 #if __cplusplus > 201402L
1586 reference
1587 #else
1588 void
1589 #endif
1590 emplace_back(_Args&&... __args);
1591 #endif
1594 * @brief Removes first element.
1596 * This is a typical stack operation. It shrinks the %deque by one.
1598 * Note that no data is returned, and if the first element's data is
1599 * needed, it should be retrieved before pop_front() is called.
1601 void
1602 pop_front() _GLIBCXX_NOEXCEPT
1604 __glibcxx_requires_nonempty();
1605 if (this->_M_impl._M_start._M_cur
1606 != this->_M_impl._M_start._M_last - 1)
1608 _Alloc_traits::destroy(this->_M_impl,
1609 this->_M_impl._M_start._M_cur);
1610 ++this->_M_impl._M_start._M_cur;
1612 else
1613 _M_pop_front_aux();
1617 * @brief Removes last element.
1619 * This is a typical stack operation. It shrinks the %deque by one.
1621 * Note that no data is returned, and if the last element's data is
1622 * needed, it should be retrieved before pop_back() is called.
1624 void
1625 pop_back() _GLIBCXX_NOEXCEPT
1627 __glibcxx_requires_nonempty();
1628 if (this->_M_impl._M_finish._M_cur
1629 != this->_M_impl._M_finish._M_first)
1631 --this->_M_impl._M_finish._M_cur;
1632 _Alloc_traits::destroy(this->_M_impl,
1633 this->_M_impl._M_finish._M_cur);
1635 else
1636 _M_pop_back_aux();
1639 #if __cplusplus >= 201103L
1641 * @brief Inserts an object in %deque before specified iterator.
1642 * @param __position A const_iterator into the %deque.
1643 * @param __args Arguments.
1644 * @return An iterator that points to the inserted data.
1646 * This function will insert an object of type T constructed
1647 * with T(std::forward<Args>(args)...) before the specified location.
1649 template<typename... _Args>
1650 iterator
1651 emplace(const_iterator __position, _Args&&... __args);
1654 * @brief Inserts given value into %deque before specified iterator.
1655 * @param __position A const_iterator into the %deque.
1656 * @param __x Data to be inserted.
1657 * @return An iterator that points to the inserted data.
1659 * This function will insert a copy of the given value before the
1660 * specified location.
1662 iterator
1663 insert(const_iterator __position, const value_type& __x);
1664 #else
1666 * @brief Inserts given value into %deque before specified iterator.
1667 * @param __position An iterator into the %deque.
1668 * @param __x Data to be inserted.
1669 * @return An iterator that points to the inserted data.
1671 * This function will insert a copy of the given value before the
1672 * specified location.
1674 iterator
1675 insert(iterator __position, const value_type& __x);
1676 #endif
1678 #if __cplusplus >= 201103L
1680 * @brief Inserts given rvalue into %deque before specified iterator.
1681 * @param __position A const_iterator into the %deque.
1682 * @param __x Data to be inserted.
1683 * @return An iterator that points to the inserted data.
1685 * This function will insert a copy of the given rvalue before the
1686 * specified location.
1688 iterator
1689 insert(const_iterator __position, value_type&& __x)
1690 { return emplace(__position, std::move(__x)); }
1693 * @brief Inserts an initializer list into the %deque.
1694 * @param __p An iterator into the %deque.
1695 * @param __l An initializer_list.
1697 * This function will insert copies of the data in the
1698 * initializer_list @a __l into the %deque before the location
1699 * specified by @a __p. This is known as <em>list insert</em>.
1701 iterator
1702 insert(const_iterator __p, initializer_list<value_type> __l)
1704 auto __offset = __p - cbegin();
1705 _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(),
1706 std::random_access_iterator_tag());
1707 return begin() + __offset;
1709 #endif
1711 #if __cplusplus >= 201103L
1713 * @brief Inserts a number of copies of given data into the %deque.
1714 * @param __position A const_iterator into the %deque.
1715 * @param __n Number of elements to be inserted.
1716 * @param __x Data to be inserted.
1717 * @return An iterator that points to the inserted data.
1719 * This function will insert a specified number of copies of the given
1720 * data before the location specified by @a __position.
1722 iterator
1723 insert(const_iterator __position, size_type __n, const value_type& __x)
1725 difference_type __offset = __position - cbegin();
1726 _M_fill_insert(__position._M_const_cast(), __n, __x);
1727 return begin() + __offset;
1729 #else
1731 * @brief Inserts a number of copies of given data into the %deque.
1732 * @param __position An iterator into the %deque.
1733 * @param __n Number of elements to be inserted.
1734 * @param __x Data to be inserted.
1736 * This function will insert a specified number of copies of the given
1737 * data before the location specified by @a __position.
1739 void
1740 insert(iterator __position, size_type __n, const value_type& __x)
1741 { _M_fill_insert(__position, __n, __x); }
1742 #endif
1744 #if __cplusplus >= 201103L
1746 * @brief Inserts a range into the %deque.
1747 * @param __position A const_iterator into the %deque.
1748 * @param __first An input iterator.
1749 * @param __last An input iterator.
1750 * @return An iterator that points to the inserted data.
1752 * This function will insert copies of the data in the range
1753 * [__first,__last) into the %deque before the location specified
1754 * by @a __position. This is known as <em>range insert</em>.
1756 template<typename _InputIterator,
1757 typename = std::_RequireInputIter<_InputIterator>>
1758 iterator
1759 insert(const_iterator __position, _InputIterator __first,
1760 _InputIterator __last)
1762 difference_type __offset = __position - cbegin();
1763 _M_insert_dispatch(__position._M_const_cast(),
1764 __first, __last, __false_type());
1765 return begin() + __offset;
1767 #else
1769 * @brief Inserts a range into the %deque.
1770 * @param __position An iterator into the %deque.
1771 * @param __first An input iterator.
1772 * @param __last An input iterator.
1774 * This function will insert copies of the data in the range
1775 * [__first,__last) into the %deque before the location specified
1776 * by @a __position. This is known as <em>range insert</em>.
1778 template<typename _InputIterator>
1779 void
1780 insert(iterator __position, _InputIterator __first,
1781 _InputIterator __last)
1783 // Check whether it's an integral type. If so, it's not an iterator.
1784 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1785 _M_insert_dispatch(__position, __first, __last, _Integral());
1787 #endif
1790 * @brief Remove element at given position.
1791 * @param __position Iterator pointing to element to be erased.
1792 * @return An iterator pointing to the next element (or end()).
1794 * This function will erase the element at the given position and thus
1795 * shorten the %deque by one.
1797 * The user is cautioned that
1798 * this function only erases the element, and that if the element is
1799 * itself a pointer, the pointed-to memory is not touched in any way.
1800 * Managing the pointer is the user's responsibility.
1802 iterator
1803 #if __cplusplus >= 201103L
1804 erase(const_iterator __position)
1805 #else
1806 erase(iterator __position)
1807 #endif
1808 { return _M_erase(__position._M_const_cast()); }
1811 * @brief Remove a range of elements.
1812 * @param __first Iterator pointing to the first element to be erased.
1813 * @param __last Iterator pointing to one past the last element to be
1814 * erased.
1815 * @return An iterator pointing to the element pointed to by @a last
1816 * prior to erasing (or end()).
1818 * This function will erase the elements in the range
1819 * [__first,__last) and shorten the %deque accordingly.
1821 * The user is cautioned that
1822 * this function only erases the elements, and that if the elements
1823 * themselves are pointers, the pointed-to memory is not touched in any
1824 * way. Managing the pointer is the user's responsibility.
1826 iterator
1827 #if __cplusplus >= 201103L
1828 erase(const_iterator __first, const_iterator __last)
1829 #else
1830 erase(iterator __first, iterator __last)
1831 #endif
1832 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1835 * @brief Swaps data with another %deque.
1836 * @param __x A %deque of the same element and allocator types.
1838 * This exchanges the elements between two deques in constant time.
1839 * (Four pointers, so it should be quite fast.)
1840 * Note that the global std::swap() function is specialized such that
1841 * std::swap(d1,d2) will feed to this function.
1843 * Whether the allocators are swapped depends on the allocator traits.
1845 void
1846 swap(deque& __x) _GLIBCXX_NOEXCEPT
1848 #if __cplusplus >= 201103L
1849 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1850 || _M_get_Tp_allocator() == __x._M_get_Tp_allocator());
1851 #endif
1852 _M_impl._M_swap_data(__x._M_impl);
1853 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1854 __x._M_get_Tp_allocator());
1858 * Erases all the elements. Note that this function only erases the
1859 * elements, and that if the elements themselves are pointers, the
1860 * pointed-to memory is not touched in any way. Managing the pointer is
1861 * the user's responsibility.
1863 void
1864 clear() _GLIBCXX_NOEXCEPT
1865 { _M_erase_at_end(begin()); }
1867 protected:
1868 // Internal constructor functions follow.
1870 // called by the range constructor to implement [23.1.1]/9
1872 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1873 // 438. Ambiguity in the "do the right thing" clause
1874 template<typename _Integer>
1875 void
1876 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1878 _M_initialize_map(static_cast<size_type>(__n));
1879 _M_fill_initialize(__x);
1882 // called by the range constructor to implement [23.1.1]/9
1883 template<typename _InputIterator>
1884 void
1885 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1886 __false_type)
1888 _M_range_initialize(__first, __last,
1889 std::__iterator_category(__first));
1892 // called by the second initialize_dispatch above
1893 //@{
1895 * @brief Fills the deque with whatever is in [first,last).
1896 * @param __first An input iterator.
1897 * @param __last An input iterator.
1898 * @return Nothing.
1900 * If the iterators are actually forward iterators (or better), then the
1901 * memory layout can be done all at once. Else we move forward using
1902 * push_back on each value from the iterator.
1904 template<typename _InputIterator>
1905 void
1906 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1907 std::input_iterator_tag);
1909 // called by the second initialize_dispatch above
1910 template<typename _ForwardIterator>
1911 void
1912 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1913 std::forward_iterator_tag);
1914 //@}
1917 * @brief Fills the %deque with copies of value.
1918 * @param __value Initial value.
1919 * @return Nothing.
1920 * @pre _M_start and _M_finish have already been initialized,
1921 * but none of the %deque's elements have yet been constructed.
1923 * This function is called only when the user provides an explicit size
1924 * (with or without an explicit exemplar value).
1926 void
1927 _M_fill_initialize(const value_type& __value);
1929 #if __cplusplus >= 201103L
1930 // called by deque(n).
1931 void
1932 _M_default_initialize();
1933 #endif
1935 // Internal assign functions follow. The *_aux functions do the actual
1936 // assignment work for the range versions.
1938 // called by the range assign to implement [23.1.1]/9
1940 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1941 // 438. Ambiguity in the "do the right thing" clause
1942 template<typename _Integer>
1943 void
1944 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1945 { _M_fill_assign(__n, __val); }
1947 // called by the range assign to implement [23.1.1]/9
1948 template<typename _InputIterator>
1949 void
1950 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1951 __false_type)
1952 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1954 // called by the second assign_dispatch above
1955 template<typename _InputIterator>
1956 void
1957 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1958 std::input_iterator_tag);
1960 // called by the second assign_dispatch above
1961 template<typename _ForwardIterator>
1962 void
1963 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1964 std::forward_iterator_tag)
1966 const size_type __len = std::distance(__first, __last);
1967 if (__len > size())
1969 _ForwardIterator __mid = __first;
1970 std::advance(__mid, size());
1971 std::copy(__first, __mid, begin());
1972 _M_range_insert_aux(end(), __mid, __last,
1973 std::__iterator_category(__first));
1975 else
1976 _M_erase_at_end(std::copy(__first, __last, begin()));
1979 // Called by assign(n,t), and the range assign when it turns out
1980 // to be the same thing.
1981 void
1982 _M_fill_assign(size_type __n, const value_type& __val)
1984 if (__n > size())
1986 std::fill(begin(), end(), __val);
1987 _M_fill_insert(end(), __n - size(), __val);
1989 else
1991 _M_erase_at_end(begin() + difference_type(__n));
1992 std::fill(begin(), end(), __val);
1996 //@{
1997 /// Helper functions for push_* and pop_*.
1998 #if __cplusplus < 201103L
1999 void _M_push_back_aux(const value_type&);
2001 void _M_push_front_aux(const value_type&);
2002 #else
2003 template<typename... _Args>
2004 void _M_push_back_aux(_Args&&... __args);
2006 template<typename... _Args>
2007 void _M_push_front_aux(_Args&&... __args);
2008 #endif
2010 void _M_pop_back_aux();
2012 void _M_pop_front_aux();
2013 //@}
2015 // Internal insert functions follow. The *_aux functions do the actual
2016 // insertion work when all shortcuts fail.
2018 // called by the range insert to implement [23.1.1]/9
2020 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2021 // 438. Ambiguity in the "do the right thing" clause
2022 template<typename _Integer>
2023 void
2024 _M_insert_dispatch(iterator __pos,
2025 _Integer __n, _Integer __x, __true_type)
2026 { _M_fill_insert(__pos, __n, __x); }
2028 // called by the range insert to implement [23.1.1]/9
2029 template<typename _InputIterator>
2030 void
2031 _M_insert_dispatch(iterator __pos,
2032 _InputIterator __first, _InputIterator __last,
2033 __false_type)
2035 _M_range_insert_aux(__pos, __first, __last,
2036 std::__iterator_category(__first));
2039 // called by the second insert_dispatch above
2040 template<typename _InputIterator>
2041 void
2042 _M_range_insert_aux(iterator __pos, _InputIterator __first,
2043 _InputIterator __last, std::input_iterator_tag);
2045 // called by the second insert_dispatch above
2046 template<typename _ForwardIterator>
2047 void
2048 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
2049 _ForwardIterator __last, std::forward_iterator_tag);
2051 // Called by insert(p,n,x), and the range insert when it turns out to be
2052 // the same thing. Can use fill functions in optimal situations,
2053 // otherwise passes off to insert_aux(p,n,x).
2054 void
2055 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
2057 // called by insert(p,x)
2058 #if __cplusplus < 201103L
2059 iterator
2060 _M_insert_aux(iterator __pos, const value_type& __x);
2061 #else
2062 template<typename... _Args>
2063 iterator
2064 _M_insert_aux(iterator __pos, _Args&&... __args);
2065 #endif
2067 // called by insert(p,n,x) via fill_insert
2068 void
2069 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
2071 // called by range_insert_aux for forward iterators
2072 template<typename _ForwardIterator>
2073 void
2074 _M_insert_aux(iterator __pos,
2075 _ForwardIterator __first, _ForwardIterator __last,
2076 size_type __n);
2079 // Internal erase functions follow.
2081 void
2082 _M_destroy_data_aux(iterator __first, iterator __last);
2084 // Called by ~deque().
2085 // NB: Doesn't deallocate the nodes.
2086 template<typename _Alloc1>
2087 void
2088 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
2089 { _M_destroy_data_aux(__first, __last); }
2091 void
2092 _M_destroy_data(iterator __first, iterator __last,
2093 const std::allocator<_Tp>&)
2095 if (!__has_trivial_destructor(value_type))
2096 _M_destroy_data_aux(__first, __last);
2099 // Called by erase(q1, q2).
2100 void
2101 _M_erase_at_begin(iterator __pos)
2103 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
2104 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
2105 this->_M_impl._M_start = __pos;
2108 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2109 // _M_fill_assign, operator=.
2110 void
2111 _M_erase_at_end(iterator __pos)
2113 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
2114 _M_destroy_nodes(__pos._M_node + 1,
2115 this->_M_impl._M_finish._M_node + 1);
2116 this->_M_impl._M_finish = __pos;
2119 iterator
2120 _M_erase(iterator __pos);
2122 iterator
2123 _M_erase(iterator __first, iterator __last);
2125 #if __cplusplus >= 201103L
2126 // Called by resize(sz).
2127 void
2128 _M_default_append(size_type __n);
2130 bool
2131 _M_shrink_to_fit();
2132 #endif
2134 //@{
2135 /// Memory-handling helpers for the previous internal insert functions.
2136 iterator
2137 _M_reserve_elements_at_front(size_type __n)
2139 const size_type __vacancies = this->_M_impl._M_start._M_cur
2140 - this->_M_impl._M_start._M_first;
2141 if (__n > __vacancies)
2142 _M_new_elements_at_front(__n - __vacancies);
2143 return this->_M_impl._M_start - difference_type(__n);
2146 iterator
2147 _M_reserve_elements_at_back(size_type __n)
2149 const size_type __vacancies = (this->_M_impl._M_finish._M_last
2150 - this->_M_impl._M_finish._M_cur) - 1;
2151 if (__n > __vacancies)
2152 _M_new_elements_at_back(__n - __vacancies);
2153 return this->_M_impl._M_finish + difference_type(__n);
2156 void
2157 _M_new_elements_at_front(size_type __new_elements);
2159 void
2160 _M_new_elements_at_back(size_type __new_elements);
2161 //@}
2164 //@{
2166 * @brief Memory-handling helpers for the major %map.
2168 * Makes sure the _M_map has space for new nodes. Does not
2169 * actually add the nodes. Can invalidate _M_map pointers.
2170 * (And consequently, %deque iterators.)
2172 void
2173 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
2175 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
2176 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
2177 _M_reallocate_map(__nodes_to_add, false);
2180 void
2181 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
2183 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
2184 - this->_M_impl._M_map))
2185 _M_reallocate_map(__nodes_to_add, true);
2188 void
2189 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
2190 //@}
2192 #if __cplusplus >= 201103L
2193 // Constant-time, nothrow move assignment when source object's memory
2194 // can be moved because the allocators are equal.
2195 void
2196 _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
2198 this->_M_impl._M_swap_data(__x._M_impl);
2199 __x.clear();
2200 std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
2203 // When the allocators are not equal the operation could throw, because
2204 // we might need to allocate a new map for __x after moving from it
2205 // or we might need to allocate new elements for *this.
2206 void
2207 _M_move_assign1(deque&& __x, /* always equal: */ false_type)
2209 constexpr bool __move_storage =
2210 _Alloc_traits::_S_propagate_on_move_assign();
2211 _M_move_assign2(std::move(__x), __bool_constant<__move_storage>());
2214 // Destroy all elements and deallocate all memory, then replace
2215 // with elements created from __args.
2216 template<typename... _Args>
2217 void
2218 _M_replace_map(_Args&&... __args)
2220 // Create new data first, so if allocation fails there are no effects.
2221 deque __newobj(std::forward<_Args>(__args)...);
2222 // Free existing storage using existing allocator.
2223 clear();
2224 _M_deallocate_node(*begin()._M_node); // one node left after clear()
2225 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
2226 this->_M_impl._M_map = nullptr;
2227 this->_M_impl._M_map_size = 0;
2228 // Take ownership of replacement memory.
2229 this->_M_impl._M_swap_data(__newobj._M_impl);
2232 // Do move assignment when the allocator propagates.
2233 void
2234 _M_move_assign2(deque&& __x, /* propagate: */ true_type)
2236 // Make a copy of the original allocator state.
2237 auto __alloc = __x._M_get_Tp_allocator();
2238 // The allocator propagates so storage can be moved from __x,
2239 // leaving __x in a valid empty state with a moved-from allocator.
2240 _M_replace_map(std::move(__x));
2241 // Move the corresponding allocator state too.
2242 _M_get_Tp_allocator() = std::move(__alloc);
2245 // Do move assignment when it may not be possible to move source
2246 // object's memory, resulting in a linear-time operation.
2247 void
2248 _M_move_assign2(deque&& __x, /* propagate: */ false_type)
2250 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2252 // The allocators are equal so storage can be moved from __x,
2253 // leaving __x in a valid empty state with its current allocator.
2254 _M_replace_map(std::move(__x), __x.get_allocator());
2256 else
2258 // The rvalue's allocator cannot be moved and is not equal,
2259 // so we need to individually move each element.
2260 _M_assign_aux(std::__make_move_if_noexcept_iterator(__x.begin()),
2261 std::__make_move_if_noexcept_iterator(__x.end()),
2262 std::random_access_iterator_tag());
2263 __x.clear();
2266 #endif
2269 #if __cpp_deduction_guides >= 201606
2270 template<typename _InputIterator, typename _ValT
2271 = typename iterator_traits<_InputIterator>::value_type,
2272 typename _Allocator = allocator<_ValT>,
2273 typename = _RequireInputIter<_InputIterator>,
2274 typename = _RequireAllocator<_Allocator>>
2275 deque(_InputIterator, _InputIterator, _Allocator = _Allocator())
2276 -> deque<_ValT, _Allocator>;
2277 #endif
2280 * @brief Deque equality comparison.
2281 * @param __x A %deque.
2282 * @param __y A %deque of the same type as @a __x.
2283 * @return True iff the size and elements of the deques are equal.
2285 * This is an equivalence relation. It is linear in the size of the
2286 * deques. Deques are considered equivalent if their sizes are equal,
2287 * and if corresponding elements compare equal.
2289 template<typename _Tp, typename _Alloc>
2290 inline bool
2291 operator==(const deque<_Tp, _Alloc>& __x,
2292 const deque<_Tp, _Alloc>& __y)
2293 { return __x.size() == __y.size()
2294 && std::equal(__x.begin(), __x.end(), __y.begin()); }
2297 * @brief Deque ordering relation.
2298 * @param __x A %deque.
2299 * @param __y A %deque of the same type as @a __x.
2300 * @return True iff @a x is lexicographically less than @a __y.
2302 * This is a total ordering relation. It is linear in the size of the
2303 * deques. The elements must be comparable with @c <.
2305 * See std::lexicographical_compare() for how the determination is made.
2307 template<typename _Tp, typename _Alloc>
2308 inline bool
2309 operator<(const deque<_Tp, _Alloc>& __x,
2310 const deque<_Tp, _Alloc>& __y)
2311 { return std::lexicographical_compare(__x.begin(), __x.end(),
2312 __y.begin(), __y.end()); }
2314 /// Based on operator==
2315 template<typename _Tp, typename _Alloc>
2316 inline bool
2317 operator!=(const deque<_Tp, _Alloc>& __x,
2318 const deque<_Tp, _Alloc>& __y)
2319 { return !(__x == __y); }
2321 /// Based on operator<
2322 template<typename _Tp, typename _Alloc>
2323 inline bool
2324 operator>(const deque<_Tp, _Alloc>& __x,
2325 const deque<_Tp, _Alloc>& __y)
2326 { return __y < __x; }
2328 /// Based on operator<
2329 template<typename _Tp, typename _Alloc>
2330 inline bool
2331 operator<=(const deque<_Tp, _Alloc>& __x,
2332 const deque<_Tp, _Alloc>& __y)
2333 { return !(__y < __x); }
2335 /// Based on operator<
2336 template<typename _Tp, typename _Alloc>
2337 inline bool
2338 operator>=(const deque<_Tp, _Alloc>& __x,
2339 const deque<_Tp, _Alloc>& __y)
2340 { return !(__x < __y); }
2342 /// See std::deque::swap().
2343 template<typename _Tp, typename _Alloc>
2344 inline void
2345 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
2346 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2347 { __x.swap(__y); }
2349 #undef _GLIBCXX_DEQUE_BUF_SIZE
2351 _GLIBCXX_END_NAMESPACE_CONTAINER
2352 _GLIBCXX_END_NAMESPACE_VERSION
2353 } // namespace std
2355 #endif /* _STL_DEQUE_H */