2016-06-15 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
blobf63ae4c0ba5b845e23cfa30c440cce714e290968
1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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 namespace std _GLIBCXX_VISIBILITY(default)
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
70 /**
71 * @brief This function controls the size of memory nodes.
72 * @param __size The size of an element.
73 * @return The number (not byte size) of elements per node.
75 * This function started off as a compiler kludge from SGI, but
76 * seems to be a useful wrapper around a repeated constant
77 * expression. The @b 512 is tunable (and no other code needs to
78 * change), but no investigation has been done since inheriting the
79 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
80 * you are doing, however: changing it breaks the binary
81 * compatibility!!
84 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
85 #define _GLIBCXX_DEQUE_BUF_SIZE 512
86 #endif
88 _GLIBCXX_CONSTEXPR inline size_t
89 __deque_buf_size(size_t __size)
90 { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
91 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
94 /**
95 * @brief A deque::iterator.
97 * Quite a bit of intelligence here. Much of the functionality of
98 * deque is actually passed off to this class. A deque holds two
99 * of these internally, marking its valid range. Access to
100 * elements is done as offsets of either of those two, relying on
101 * operator overloading in this class.
103 * All the functions are op overloads except for _M_set_node.
105 template<typename _Tp, typename _Ref, typename _Ptr>
106 struct _Deque_iterator
108 #if __cplusplus < 201103L
109 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
110 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
111 typedef _Tp* _Elt_pointer;
112 typedef _Tp** _Map_pointer;
113 #else
114 private:
115 template<typename _Up>
116 using __ptr_to = typename pointer_traits<_Ptr>::template rebind<_Up>;
117 template<typename _CvTp>
118 using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_to<_CvTp>>;
119 public:
120 typedef __iter<_Tp> iterator;
121 typedef __iter<const _Tp> const_iterator;
122 typedef __ptr_to<_Tp> _Elt_pointer;
123 typedef __ptr_to<_Elt_pointer> _Map_pointer;
124 #endif
126 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
127 { return __deque_buf_size(sizeof(_Tp)); }
129 typedef std::random_access_iterator_tag iterator_category;
130 typedef _Tp value_type;
131 typedef _Ptr pointer;
132 typedef _Ref reference;
133 typedef size_t size_type;
134 typedef ptrdiff_t difference_type;
135 typedef _Deque_iterator _Self;
137 _Elt_pointer _M_cur;
138 _Elt_pointer _M_first;
139 _Elt_pointer _M_last;
140 _Map_pointer _M_node;
142 _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT
143 : _M_cur(__x), _M_first(*__y),
144 _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
146 _Deque_iterator() _GLIBCXX_NOEXCEPT
147 : _M_cur(), _M_first(), _M_last(), _M_node() { }
149 _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
150 : _M_cur(__x._M_cur), _M_first(__x._M_first),
151 _M_last(__x._M_last), _M_node(__x._M_node) { }
153 iterator
154 _M_const_cast() const _GLIBCXX_NOEXCEPT
155 { return iterator(_M_cur, _M_node); }
157 reference
158 operator*() const _GLIBCXX_NOEXCEPT
159 { return *_M_cur; }
161 pointer
162 operator->() const _GLIBCXX_NOEXCEPT
163 { return _M_cur; }
165 _Self&
166 operator++() _GLIBCXX_NOEXCEPT
168 ++_M_cur;
169 if (_M_cur == _M_last)
171 _M_set_node(_M_node + 1);
172 _M_cur = _M_first;
174 return *this;
177 _Self
178 operator++(int) _GLIBCXX_NOEXCEPT
180 _Self __tmp = *this;
181 ++*this;
182 return __tmp;
185 _Self&
186 operator--() _GLIBCXX_NOEXCEPT
188 if (_M_cur == _M_first)
190 _M_set_node(_M_node - 1);
191 _M_cur = _M_last;
193 --_M_cur;
194 return *this;
197 _Self
198 operator--(int) _GLIBCXX_NOEXCEPT
200 _Self __tmp = *this;
201 --*this;
202 return __tmp;
205 _Self&
206 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
208 const difference_type __offset = __n + (_M_cur - _M_first);
209 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
210 _M_cur += __n;
211 else
213 const difference_type __node_offset =
214 __offset > 0 ? __offset / difference_type(_S_buffer_size())
215 : -difference_type((-__offset - 1)
216 / _S_buffer_size()) - 1;
217 _M_set_node(_M_node + __node_offset);
218 _M_cur = _M_first + (__offset - __node_offset
219 * difference_type(_S_buffer_size()));
221 return *this;
224 _Self
225 operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
227 _Self __tmp = *this;
228 return __tmp += __n;
231 _Self&
232 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
233 { return *this += -__n; }
235 _Self
236 operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
238 _Self __tmp = *this;
239 return __tmp -= __n;
242 reference
243 operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
244 { return *(*this + __n); }
246 /**
247 * Prepares to traverse new_node. Sets everything except
248 * _M_cur, which should therefore be set by the caller
249 * immediately afterwards, based on _M_first and _M_last.
251 void
252 _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT
254 _M_node = __new_node;
255 _M_first = *__new_node;
256 _M_last = _M_first + difference_type(_S_buffer_size());
260 // Note: we also provide overloads whose operands are of the same type in
261 // order to avoid ambiguous overload resolution when std::rel_ops operators
262 // are in scope (for additional details, see libstdc++/3628)
263 template<typename _Tp, typename _Ref, typename _Ptr>
264 inline bool
265 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
266 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
267 { return __x._M_cur == __y._M_cur; }
269 template<typename _Tp, typename _RefL, typename _PtrL,
270 typename _RefR, typename _PtrR>
271 inline bool
272 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
273 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
274 { return __x._M_cur == __y._M_cur; }
276 template<typename _Tp, typename _Ref, typename _Ptr>
277 inline bool
278 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
279 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
280 { return !(__x == __y); }
282 template<typename _Tp, typename _RefL, typename _PtrL,
283 typename _RefR, typename _PtrR>
284 inline bool
285 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
286 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
287 { return !(__x == __y); }
289 template<typename _Tp, typename _Ref, typename _Ptr>
290 inline bool
291 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
292 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
293 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
294 : (__x._M_node < __y._M_node); }
296 template<typename _Tp, typename _RefL, typename _PtrL,
297 typename _RefR, typename _PtrR>
298 inline bool
299 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
300 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
301 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
302 : (__x._M_node < __y._M_node); }
304 template<typename _Tp, typename _Ref, typename _Ptr>
305 inline bool
306 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
307 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
308 { return __y < __x; }
310 template<typename _Tp, typename _RefL, typename _PtrL,
311 typename _RefR, typename _PtrR>
312 inline bool
313 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
314 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
315 { return __y < __x; }
317 template<typename _Tp, typename _Ref, typename _Ptr>
318 inline bool
319 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
320 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
321 { return !(__y < __x); }
323 template<typename _Tp, typename _RefL, typename _PtrL,
324 typename _RefR, typename _PtrR>
325 inline bool
326 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
327 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
328 { return !(__y < __x); }
330 template<typename _Tp, typename _Ref, typename _Ptr>
331 inline bool
332 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
333 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
334 { return !(__x < __y); }
336 template<typename _Tp, typename _RefL, typename _PtrL,
337 typename _RefR, typename _PtrR>
338 inline bool
339 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
340 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
341 { return !(__x < __y); }
343 // _GLIBCXX_RESOLVE_LIB_DEFECTS
344 // According to the resolution of DR179 not only the various comparison
345 // operators but also operator- must accept mixed iterator/const_iterator
346 // parameters.
347 template<typename _Tp, typename _Ref, typename _Ptr>
348 inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
349 operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
350 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
352 return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
353 (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
354 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
355 + (__y._M_last - __y._M_cur);
358 template<typename _Tp, typename _RefL, typename _PtrL,
359 typename _RefR, typename _PtrR>
360 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
361 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
362 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
364 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
365 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
366 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
367 + (__y._M_last - __y._M_cur);
370 template<typename _Tp, typename _Ref, typename _Ptr>
371 inline _Deque_iterator<_Tp, _Ref, _Ptr>
372 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
373 _GLIBCXX_NOEXCEPT
374 { return __x + __n; }
376 template<typename _Tp>
377 void
378 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&,
379 const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&);
381 template<typename _Tp>
382 _Deque_iterator<_Tp, _Tp&, _Tp*>
383 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
384 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
385 _Deque_iterator<_Tp, _Tp&, _Tp*>);
387 template<typename _Tp>
388 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
389 copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
390 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
391 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
392 { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
393 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
394 __result); }
396 template<typename _Tp>
397 _Deque_iterator<_Tp, _Tp&, _Tp*>
398 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
399 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
400 _Deque_iterator<_Tp, _Tp&, _Tp*>);
402 template<typename _Tp>
403 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
404 copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
405 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
406 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
407 { return std::copy_backward(_Deque_iterator<_Tp,
408 const _Tp&, const _Tp*>(__first),
409 _Deque_iterator<_Tp,
410 const _Tp&, const _Tp*>(__last),
411 __result); }
413 #if __cplusplus >= 201103L
414 template<typename _Tp>
415 _Deque_iterator<_Tp, _Tp&, _Tp*>
416 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
417 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
418 _Deque_iterator<_Tp, _Tp&, _Tp*>);
420 template<typename _Tp>
421 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
422 move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
423 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
424 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
425 { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
426 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
427 __result); }
429 template<typename _Tp>
430 _Deque_iterator<_Tp, _Tp&, _Tp*>
431 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
432 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
433 _Deque_iterator<_Tp, _Tp&, _Tp*>);
435 template<typename _Tp>
436 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
437 move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
438 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
439 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
440 { return std::move_backward(_Deque_iterator<_Tp,
441 const _Tp&, const _Tp*>(__first),
442 _Deque_iterator<_Tp,
443 const _Tp&, const _Tp*>(__last),
444 __result); }
445 #endif
448 * Deque base class. This class provides the unified face for %deque's
449 * allocation. This class's constructor and destructor allocate and
450 * deallocate (but do not initialize) storage. This makes %exception
451 * safety easier.
453 * Nothing in this class ever constructs or destroys an actual Tp element.
454 * (Deque handles that itself.) Only/All memory management is performed
455 * here.
457 template<typename _Tp, typename _Alloc>
458 class _Deque_base
460 protected:
461 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
462 rebind<_Tp>::other _Tp_alloc_type;
463 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Alloc_traits;
465 #if __cplusplus < 201103L
466 typedef _Tp* _Ptr;
467 typedef const _Tp* _Ptr_const;
468 #else
469 typedef typename _Alloc_traits::pointer _Ptr;
470 typedef typename _Alloc_traits::const_pointer _Ptr_const;
471 #endif
473 typedef typename _Alloc_traits::template rebind<_Ptr>::other
474 _Map_alloc_type;
475 typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits;
477 public:
478 typedef _Alloc allocator_type;
479 typedef typename _Alloc_traits::size_type size_type;
481 allocator_type
482 get_allocator() const _GLIBCXX_NOEXCEPT
483 { return allocator_type(_M_get_Tp_allocator()); }
485 typedef _Deque_iterator<_Tp, _Tp&, _Ptr> iterator;
486 typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const> const_iterator;
488 _Deque_base()
489 : _M_impl()
490 { _M_initialize_map(0); }
492 _Deque_base(size_t __num_elements)
493 : _M_impl()
494 { _M_initialize_map(__num_elements); }
496 _Deque_base(const allocator_type& __a, size_t __num_elements)
497 : _M_impl(__a)
498 { _M_initialize_map(__num_elements); }
500 _Deque_base(const allocator_type& __a)
501 : _M_impl(__a)
502 { /* Caller must initialize map. */ }
504 #if __cplusplus >= 201103L
505 _Deque_base(_Deque_base&& __x, false_type)
506 : _M_impl(__x._M_move_impl())
509 _Deque_base(_Deque_base&& __x, true_type)
510 : _M_impl(std::move(__x._M_get_Tp_allocator()))
512 _M_initialize_map(0);
513 if (__x._M_impl._M_map)
514 this->_M_impl._M_swap_data(__x._M_impl);
517 _Deque_base(_Deque_base&& __x)
518 : _Deque_base(std::move(__x), typename _Alloc_traits::is_always_equal{})
521 _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_type __n)
522 : _M_impl(__a)
524 if (__x.get_allocator() == __a)
526 if (__x._M_impl._M_map)
528 _M_initialize_map(0);
529 this->_M_impl._M_swap_data(__x._M_impl);
532 else
534 _M_initialize_map(__n);
537 #endif
539 ~_Deque_base() _GLIBCXX_NOEXCEPT;
541 protected:
542 typedef typename iterator::_Map_pointer _Map_pointer;
544 //This struct encapsulates the implementation of the std::deque
545 //standard container and at the same time makes use of the EBO
546 //for empty allocators.
547 struct _Deque_impl
548 : public _Tp_alloc_type
550 _Map_pointer _M_map;
551 size_t _M_map_size;
552 iterator _M_start;
553 iterator _M_finish;
555 _Deque_impl()
556 : _Tp_alloc_type(), _M_map(), _M_map_size(0),
557 _M_start(), _M_finish()
560 _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
561 : _Tp_alloc_type(__a), _M_map(), _M_map_size(0),
562 _M_start(), _M_finish()
565 #if __cplusplus >= 201103L
566 _Deque_impl(_Deque_impl&&) = default;
568 _Deque_impl(_Tp_alloc_type&& __a) noexcept
569 : _Tp_alloc_type(std::move(__a)), _M_map(), _M_map_size(0),
570 _M_start(), _M_finish()
572 #endif
574 void _M_swap_data(_Deque_impl& __x) _GLIBCXX_NOEXCEPT
576 using std::swap;
577 swap(this->_M_start, __x._M_start);
578 swap(this->_M_finish, __x._M_finish);
579 swap(this->_M_map, __x._M_map);
580 swap(this->_M_map_size, __x._M_map_size);
584 _Tp_alloc_type&
585 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
586 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
588 const _Tp_alloc_type&
589 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
590 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
592 _Map_alloc_type
593 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
594 { return _Map_alloc_type(_M_get_Tp_allocator()); }
596 _Ptr
597 _M_allocate_node()
599 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
600 return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
603 void
604 _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
606 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
607 _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
610 _Map_pointer
611 _M_allocate_map(size_t __n)
613 _Map_alloc_type __map_alloc = _M_get_map_allocator();
614 return _Map_alloc_traits::allocate(__map_alloc, __n);
617 void
618 _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
620 _Map_alloc_type __map_alloc = _M_get_map_allocator();
621 _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
624 protected:
625 void _M_initialize_map(size_t);
626 void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
627 void _M_destroy_nodes(_Map_pointer __nstart,
628 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
629 enum { _S_initial_map_size = 8 };
631 _Deque_impl _M_impl;
633 #if __cplusplus >= 201103L
634 private:
635 _Deque_impl
636 _M_move_impl()
638 if (!_M_impl._M_map)
639 return std::move(_M_impl);
641 // Create a copy of the current allocator.
642 _Tp_alloc_type __alloc{_M_get_Tp_allocator()};
643 // Put that copy in a moved-from state.
644 _Tp_alloc_type __sink __attribute((__unused__)) {std::move(__alloc)};
645 // Create an empty map that allocates using the moved-from allocator.
646 _Deque_base __empty{__alloc};
647 __empty._M_initialize_map(0);
648 // Now safe to modify current allocator and perform non-throwing swaps.
649 _Deque_impl __ret{std::move(_M_get_Tp_allocator())};
650 _M_impl._M_swap_data(__ret);
651 _M_impl._M_swap_data(__empty._M_impl);
652 return __ret;
654 #endif
657 template<typename _Tp, typename _Alloc>
658 _Deque_base<_Tp, _Alloc>::
659 ~_Deque_base() _GLIBCXX_NOEXCEPT
661 if (this->_M_impl._M_map)
663 _M_destroy_nodes(this->_M_impl._M_start._M_node,
664 this->_M_impl._M_finish._M_node + 1);
665 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
670 * @brief Layout storage.
671 * @param __num_elements The count of T's for which to allocate space
672 * at first.
673 * @return Nothing.
675 * The initial underlying memory layout is a bit complicated...
677 template<typename _Tp, typename _Alloc>
678 void
679 _Deque_base<_Tp, _Alloc>::
680 _M_initialize_map(size_t __num_elements)
682 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
683 + 1);
685 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
686 size_t(__num_nodes + 2));
687 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
689 // For "small" maps (needing less than _M_map_size nodes), allocation
690 // starts in the middle elements and grows outwards. So nstart may be
691 // the beginning of _M_map, but for small maps it may be as far in as
692 // _M_map+3.
694 _Map_pointer __nstart = (this->_M_impl._M_map
695 + (this->_M_impl._M_map_size - __num_nodes) / 2);
696 _Map_pointer __nfinish = __nstart + __num_nodes;
698 __try
699 { _M_create_nodes(__nstart, __nfinish); }
700 __catch(...)
702 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
703 this->_M_impl._M_map = _Map_pointer();
704 this->_M_impl._M_map_size = 0;
705 __throw_exception_again;
708 this->_M_impl._M_start._M_set_node(__nstart);
709 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
710 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
711 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
712 + __num_elements
713 % __deque_buf_size(sizeof(_Tp)));
716 template<typename _Tp, typename _Alloc>
717 void
718 _Deque_base<_Tp, _Alloc>::
719 _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
721 _Map_pointer __cur;
722 __try
724 for (__cur = __nstart; __cur < __nfinish; ++__cur)
725 *__cur = this->_M_allocate_node();
727 __catch(...)
729 _M_destroy_nodes(__nstart, __cur);
730 __throw_exception_again;
734 template<typename _Tp, typename _Alloc>
735 void
736 _Deque_base<_Tp, _Alloc>::
737 _M_destroy_nodes(_Map_pointer __nstart,
738 _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
740 for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
741 _M_deallocate_node(*__n);
745 * @brief A standard container using fixed-size memory allocation and
746 * constant-time manipulation of elements at either end.
748 * @ingroup sequences
750 * @tparam _Tp Type of element.
751 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
753 * Meets the requirements of a <a href="tables.html#65">container</a>, a
754 * <a href="tables.html#66">reversible container</a>, and a
755 * <a href="tables.html#67">sequence</a>, including the
756 * <a href="tables.html#68">optional sequence requirements</a>.
758 * In previous HP/SGI versions of deque, there was an extra template
759 * parameter so users could control the node size. This extension turned
760 * out to violate the C++ standard (it can be detected using template
761 * template parameters), and it was removed.
763 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
765 * - Tp** _M_map
766 * - size_t _M_map_size
767 * - iterator _M_start, _M_finish
769 * map_size is at least 8. %map is an array of map_size
770 * pointers-to-@a nodes. (The name %map has nothing to do with the
771 * std::map class, and @b nodes should not be confused with
772 * std::list's usage of @a node.)
774 * A @a node has no specific type name as such, but it is referred
775 * to as @a node in this file. It is a simple array-of-Tp. If Tp
776 * is very large, there will be one Tp element per node (i.e., an
777 * @a array of one). For non-huge Tp's, node size is inversely
778 * related to Tp size: the larger the Tp, the fewer Tp's will fit
779 * in a node. The goal here is to keep the total size of a node
780 * relatively small and constant over different Tp's, to improve
781 * allocator efficiency.
783 * Not every pointer in the %map array will point to a node. If
784 * the initial number of elements in the deque is small, the
785 * /middle/ %map pointers will be valid, and the ones at the edges
786 * will be unused. This same situation will arise as the %map
787 * grows: available %map pointers, if any, will be on the ends. As
788 * new nodes are created, only a subset of the %map's pointers need
789 * to be copied @a outward.
791 * Class invariants:
792 * - For any nonsingular iterator i:
793 * - i.node points to a member of the %map array. (Yes, you read that
794 * correctly: i.node does not actually point to a node.) The member of
795 * the %map array is what actually points to the node.
796 * - i.first == *(i.node) (This points to the node (first Tp element).)
797 * - i.last == i.first + node_size
798 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
799 * the implication of this is that i.cur is always a dereferenceable
800 * pointer, even if i is a past-the-end iterator.
801 * - Start and Finish are always nonsingular iterators. NOTE: this
802 * means that an empty deque must have one node, a deque with <N
803 * elements (where N is the node buffer size) must have one node, a
804 * deque with N through (2N-1) elements must have two nodes, etc.
805 * - For every node other than start.node and finish.node, every
806 * element in the node is an initialized object. If start.node ==
807 * finish.node, then [start.cur, finish.cur) are initialized
808 * objects, and the elements outside that range are uninitialized
809 * storage. Otherwise, [start.cur, start.last) and [finish.first,
810 * finish.cur) are initialized objects, and [start.first, start.cur)
811 * and [finish.cur, finish.last) are uninitialized storage.
812 * - [%map, %map + map_size) is a valid, non-empty range.
813 * - [start.node, finish.node] is a valid range contained within
814 * [%map, %map + map_size).
815 * - A pointer in the range [%map, %map + map_size) points to an allocated
816 * node if and only if the pointer is in the range
817 * [start.node, finish.node].
819 * Here's the magic: nothing in deque is @b aware of the discontiguous
820 * storage!
822 * The memory setup and layout occurs in the parent, _Base, and the iterator
823 * class is entirely responsible for @a leaping from one node to the next.
824 * All the implementation routines for deque itself work only through the
825 * start and finish iterators. This keeps the routines simple and sane,
826 * and we can use other standard algorithms as well.
828 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
829 class deque : protected _Deque_base<_Tp, _Alloc>
831 // concept requirements
832 typedef typename _Alloc::value_type _Alloc_value_type;
833 #if __cplusplus < 201103L
834 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
835 #endif
836 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
838 typedef _Deque_base<_Tp, _Alloc> _Base;
839 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
840 typedef typename _Base::_Alloc_traits _Alloc_traits;
841 typedef typename _Base::_Map_pointer _Map_pointer;
843 public:
844 typedef _Tp value_type;
845 typedef typename _Alloc_traits::pointer pointer;
846 typedef typename _Alloc_traits::const_pointer const_pointer;
847 typedef typename _Alloc_traits::reference reference;
848 typedef typename _Alloc_traits::const_reference const_reference;
849 typedef typename _Base::iterator iterator;
850 typedef typename _Base::const_iterator const_iterator;
851 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
852 typedef std::reverse_iterator<iterator> reverse_iterator;
853 typedef size_t size_type;
854 typedef ptrdiff_t difference_type;
855 typedef _Alloc allocator_type;
857 protected:
858 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
859 { return __deque_buf_size(sizeof(_Tp)); }
861 // Functions controlling memory layout, and nothing else.
862 using _Base::_M_initialize_map;
863 using _Base::_M_create_nodes;
864 using _Base::_M_destroy_nodes;
865 using _Base::_M_allocate_node;
866 using _Base::_M_deallocate_node;
867 using _Base::_M_allocate_map;
868 using _Base::_M_deallocate_map;
869 using _Base::_M_get_Tp_allocator;
871 /**
872 * A total of four data members accumulated down the hierarchy.
873 * May be accessed via _M_impl.*
875 using _Base::_M_impl;
877 public:
878 // [23.2.1.1] construct/copy/destroy
879 // (assign() and get_allocator() are also listed in this section)
882 * @brief Creates a %deque with no elements.
884 deque() : _Base() { }
887 * @brief Creates a %deque with no elements.
888 * @param __a An allocator object.
890 explicit
891 deque(const allocator_type& __a)
892 : _Base(__a, 0) { }
894 #if __cplusplus >= 201103L
896 * @brief Creates a %deque with default constructed elements.
897 * @param __n The number of elements to initially create.
898 * @param __a An allocator.
900 * This constructor fills the %deque with @a n default
901 * constructed elements.
903 explicit
904 deque(size_type __n, const allocator_type& __a = allocator_type())
905 : _Base(__a, __n)
906 { _M_default_initialize(); }
909 * @brief Creates a %deque with copies of an exemplar element.
910 * @param __n The number of elements to initially create.
911 * @param __value An element to copy.
912 * @param __a An allocator.
914 * This constructor fills the %deque with @a __n copies of @a __value.
916 deque(size_type __n, const value_type& __value,
917 const allocator_type& __a = allocator_type())
918 : _Base(__a, __n)
919 { _M_fill_initialize(__value); }
920 #else
922 * @brief Creates a %deque with copies of an exemplar element.
923 * @param __n The number of elements to initially create.
924 * @param __value An element to copy.
925 * @param __a An allocator.
927 * This constructor fills the %deque with @a __n copies of @a __value.
929 explicit
930 deque(size_type __n, const value_type& __value = value_type(),
931 const allocator_type& __a = allocator_type())
932 : _Base(__a, __n)
933 { _M_fill_initialize(__value); }
934 #endif
937 * @brief %Deque copy constructor.
938 * @param __x A %deque of identical element and allocator types.
940 * The newly-created %deque uses a copy of the allocation object used
941 * by @a __x.
943 deque(const deque& __x)
944 : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
945 __x.size())
946 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
947 this->_M_impl._M_start,
948 _M_get_Tp_allocator()); }
950 #if __cplusplus >= 201103L
952 * @brief %Deque move constructor.
953 * @param __x A %deque of identical element and allocator types.
955 * The newly-created %deque contains the exact contents of @a __x.
956 * The contents of @a __x are a valid, but unspecified %deque.
958 deque(deque&& __x)
959 : _Base(std::move(__x)) { }
961 /// Copy constructor with alternative allocator
962 deque(const deque& __x, const allocator_type& __a)
963 : _Base(__a, __x.size())
964 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
965 this->_M_impl._M_start,
966 _M_get_Tp_allocator()); }
968 /// Move constructor with alternative allocator
969 deque(deque&& __x, const allocator_type& __a)
970 : _Base(std::move(__x), __a, __x.size())
972 if (__x.get_allocator() != __a)
974 std::__uninitialized_move_a(__x.begin(), __x.end(),
975 this->_M_impl._M_start,
976 _M_get_Tp_allocator());
977 __x.clear();
982 * @brief Builds a %deque from an initializer list.
983 * @param __l An initializer_list.
984 * @param __a An allocator object.
986 * Create a %deque consisting of copies of the elements in the
987 * initializer_list @a __l.
989 * This will call the element type's copy constructor N times
990 * (where N is __l.size()) and do no memory reallocation.
992 deque(initializer_list<value_type> __l,
993 const allocator_type& __a = allocator_type())
994 : _Base(__a)
996 _M_range_initialize(__l.begin(), __l.end(),
997 random_access_iterator_tag());
999 #endif
1002 * @brief Builds a %deque from a range.
1003 * @param __first An input iterator.
1004 * @param __last An input iterator.
1005 * @param __a An allocator object.
1007 * Create a %deque consisting of copies of the elements from [__first,
1008 * __last).
1010 * If the iterators are forward, bidirectional, or random-access, then
1011 * this will call the elements' copy constructor N times (where N is
1012 * distance(__first,__last)) and do no memory reallocation. But if only
1013 * input iterators are used, then this will do at most 2N calls to the
1014 * copy constructor, and logN memory reallocations.
1016 #if __cplusplus >= 201103L
1017 template<typename _InputIterator,
1018 typename = std::_RequireInputIter<_InputIterator>>
1019 deque(_InputIterator __first, _InputIterator __last,
1020 const allocator_type& __a = allocator_type())
1021 : _Base(__a)
1022 { _M_initialize_dispatch(__first, __last, __false_type()); }
1023 #else
1024 template<typename _InputIterator>
1025 deque(_InputIterator __first, _InputIterator __last,
1026 const allocator_type& __a = allocator_type())
1027 : _Base(__a)
1029 // Check whether it's an integral type. If so, it's not an iterator.
1030 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1031 _M_initialize_dispatch(__first, __last, _Integral());
1033 #endif
1036 * The dtor only erases the elements, and note that if the elements
1037 * themselves are pointers, the pointed-to memory is not touched in any
1038 * way. Managing the pointer is the user's responsibility.
1040 ~deque()
1041 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
1044 * @brief %Deque assignment operator.
1045 * @param __x A %deque of identical element and allocator types.
1047 * All the elements of @a x are copied, but unlike the copy constructor,
1048 * the allocator object is not copied.
1050 deque&
1051 operator=(const deque& __x);
1053 #if __cplusplus >= 201103L
1055 * @brief %Deque move assignment operator.
1056 * @param __x A %deque of identical element and allocator types.
1058 * The contents of @a __x are moved into this deque (without copying,
1059 * if the allocators permit it).
1060 * @a __x is a valid, but unspecified %deque.
1062 deque&
1063 operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
1065 using __always_equal = typename _Alloc_traits::is_always_equal;
1066 _M_move_assign1(std::move(__x), __always_equal{});
1067 return *this;
1071 * @brief Assigns an initializer list to a %deque.
1072 * @param __l An initializer_list.
1074 * This function fills a %deque with copies of the elements in the
1075 * initializer_list @a __l.
1077 * Note that the assignment completely changes the %deque and that the
1078 * resulting %deque's size is the same as the number of elements
1079 * assigned. Old data may be lost.
1081 deque&
1082 operator=(initializer_list<value_type> __l)
1084 _M_assign_aux(__l.begin(), __l.end(),
1085 random_access_iterator_tag());
1086 return *this;
1088 #endif
1091 * @brief Assigns a given value to a %deque.
1092 * @param __n Number of elements to be assigned.
1093 * @param __val Value to be assigned.
1095 * This function fills a %deque with @a n copies of the given
1096 * value. Note that the assignment completely changes the
1097 * %deque and that the resulting %deque's size is the same as
1098 * the number of elements assigned. Old data may be lost.
1100 void
1101 assign(size_type __n, const value_type& __val)
1102 { _M_fill_assign(__n, __val); }
1105 * @brief Assigns a range to a %deque.
1106 * @param __first An input iterator.
1107 * @param __last An input iterator.
1109 * This function fills a %deque with copies of the elements in the
1110 * range [__first,__last).
1112 * Note that the assignment completely changes the %deque and that the
1113 * resulting %deque's size is the same as the number of elements
1114 * assigned. Old data may be lost.
1116 #if __cplusplus >= 201103L
1117 template<typename _InputIterator,
1118 typename = std::_RequireInputIter<_InputIterator>>
1119 void
1120 assign(_InputIterator __first, _InputIterator __last)
1121 { _M_assign_dispatch(__first, __last, __false_type()); }
1122 #else
1123 template<typename _InputIterator>
1124 void
1125 assign(_InputIterator __first, _InputIterator __last)
1127 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1128 _M_assign_dispatch(__first, __last, _Integral());
1130 #endif
1132 #if __cplusplus >= 201103L
1134 * @brief Assigns an initializer list to a %deque.
1135 * @param __l An initializer_list.
1137 * This function fills a %deque with copies of the elements in the
1138 * initializer_list @a __l.
1140 * Note that the assignment completely changes the %deque and that the
1141 * resulting %deque's size is the same as the number of elements
1142 * assigned. Old data may be lost.
1144 void
1145 assign(initializer_list<value_type> __l)
1146 { _M_assign_aux(__l.begin(), __l.end(), random_access_iterator_tag()); }
1147 #endif
1149 /// Get a copy of the memory allocation object.
1150 allocator_type
1151 get_allocator() const _GLIBCXX_NOEXCEPT
1152 { return _Base::get_allocator(); }
1154 // iterators
1156 * Returns a read/write iterator that points to the first element in the
1157 * %deque. Iteration is done in ordinary element order.
1159 iterator
1160 begin() _GLIBCXX_NOEXCEPT
1161 { return this->_M_impl._M_start; }
1164 * Returns a read-only (constant) iterator that points to the first
1165 * element in the %deque. Iteration is done in ordinary element order.
1167 const_iterator
1168 begin() const _GLIBCXX_NOEXCEPT
1169 { return this->_M_impl._M_start; }
1172 * Returns a read/write iterator that points one past the last
1173 * element in the %deque. Iteration is done in ordinary
1174 * element order.
1176 iterator
1177 end() _GLIBCXX_NOEXCEPT
1178 { return this->_M_impl._M_finish; }
1181 * Returns a read-only (constant) iterator that points one past
1182 * the last element in the %deque. Iteration is done in
1183 * ordinary element order.
1185 const_iterator
1186 end() const _GLIBCXX_NOEXCEPT
1187 { return this->_M_impl._M_finish; }
1190 * Returns a read/write reverse iterator that points to the
1191 * last element in the %deque. Iteration is done in reverse
1192 * element order.
1194 reverse_iterator
1195 rbegin() _GLIBCXX_NOEXCEPT
1196 { return reverse_iterator(this->_M_impl._M_finish); }
1199 * Returns a read-only (constant) reverse iterator that points
1200 * to the last element in the %deque. Iteration is done in
1201 * reverse element order.
1203 const_reverse_iterator
1204 rbegin() const _GLIBCXX_NOEXCEPT
1205 { return const_reverse_iterator(this->_M_impl._M_finish); }
1208 * Returns a read/write reverse iterator that points to one
1209 * before the first element in the %deque. Iteration is done
1210 * in reverse element order.
1212 reverse_iterator
1213 rend() _GLIBCXX_NOEXCEPT
1214 { return reverse_iterator(this->_M_impl._M_start); }
1217 * Returns a read-only (constant) reverse iterator that points
1218 * to one before the first element in the %deque. Iteration is
1219 * done in reverse element order.
1221 const_reverse_iterator
1222 rend() const _GLIBCXX_NOEXCEPT
1223 { return const_reverse_iterator(this->_M_impl._M_start); }
1225 #if __cplusplus >= 201103L
1227 * Returns a read-only (constant) iterator that points to the first
1228 * element in the %deque. Iteration is done in ordinary element order.
1230 const_iterator
1231 cbegin() const noexcept
1232 { return this->_M_impl._M_start; }
1235 * Returns a read-only (constant) iterator that points one past
1236 * the last element in the %deque. Iteration is done in
1237 * ordinary element order.
1239 const_iterator
1240 cend() const noexcept
1241 { return this->_M_impl._M_finish; }
1244 * Returns a read-only (constant) reverse iterator that points
1245 * to the last element in the %deque. Iteration is done in
1246 * reverse element order.
1248 const_reverse_iterator
1249 crbegin() const noexcept
1250 { return const_reverse_iterator(this->_M_impl._M_finish); }
1253 * Returns a read-only (constant) reverse iterator that points
1254 * to one before the first element in the %deque. Iteration is
1255 * done in reverse element order.
1257 const_reverse_iterator
1258 crend() const noexcept
1259 { return const_reverse_iterator(this->_M_impl._M_start); }
1260 #endif
1262 // [23.2.1.2] capacity
1263 /** Returns the number of elements in the %deque. */
1264 size_type
1265 size() const _GLIBCXX_NOEXCEPT
1266 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1268 /** Returns the size() of the largest possible %deque. */
1269 size_type
1270 max_size() const _GLIBCXX_NOEXCEPT
1271 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
1273 #if __cplusplus >= 201103L
1275 * @brief Resizes the %deque to the specified number of elements.
1276 * @param __new_size Number of elements the %deque should contain.
1278 * This function will %resize the %deque to the specified
1279 * number of elements. If the number is smaller than the
1280 * %deque's current size the %deque is truncated, otherwise
1281 * default constructed elements are appended.
1283 void
1284 resize(size_type __new_size)
1286 const size_type __len = size();
1287 if (__new_size > __len)
1288 _M_default_append(__new_size - __len);
1289 else if (__new_size < __len)
1290 _M_erase_at_end(this->_M_impl._M_start
1291 + difference_type(__new_size));
1295 * @brief Resizes the %deque to the specified number of elements.
1296 * @param __new_size Number of elements the %deque should contain.
1297 * @param __x Data with which new elements should be populated.
1299 * This function will %resize the %deque to the specified
1300 * number of elements. If the number is smaller than the
1301 * %deque's current size the %deque is truncated, otherwise the
1302 * %deque is extended and new elements are populated with given
1303 * data.
1305 void
1306 resize(size_type __new_size, const value_type& __x)
1308 const size_type __len = size();
1309 if (__new_size > __len)
1310 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1311 else if (__new_size < __len)
1312 _M_erase_at_end(this->_M_impl._M_start
1313 + difference_type(__new_size));
1315 #else
1317 * @brief Resizes the %deque to the specified number of elements.
1318 * @param __new_size Number of elements the %deque should contain.
1319 * @param __x Data with which new elements should be populated.
1321 * This function will %resize the %deque to the specified
1322 * number of elements. If the number is smaller than the
1323 * %deque's current size the %deque is truncated, otherwise the
1324 * %deque is extended and new elements are populated with given
1325 * data.
1327 void
1328 resize(size_type __new_size, value_type __x = value_type())
1330 const size_type __len = size();
1331 if (__new_size > __len)
1332 _M_fill_insert(this->_M_impl._M_finish, __new_size - __len, __x);
1333 else if (__new_size < __len)
1334 _M_erase_at_end(this->_M_impl._M_start
1335 + difference_type(__new_size));
1337 #endif
1339 #if __cplusplus >= 201103L
1340 /** A non-binding request to reduce memory use. */
1341 void
1342 shrink_to_fit() noexcept
1343 { _M_shrink_to_fit(); }
1344 #endif
1347 * Returns true if the %deque is empty. (Thus begin() would
1348 * equal end().)
1350 bool
1351 empty() const _GLIBCXX_NOEXCEPT
1352 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1354 // element access
1356 * @brief Subscript access to the data contained in the %deque.
1357 * @param __n The index of the element for which data should be
1358 * accessed.
1359 * @return Read/write reference to data.
1361 * This operator allows for easy, array-style, data access.
1362 * Note that data access with this operator is unchecked and
1363 * out_of_range lookups are not defined. (For checked lookups
1364 * see at().)
1366 reference
1367 operator[](size_type __n) _GLIBCXX_NOEXCEPT
1368 { return this->_M_impl._M_start[difference_type(__n)]; }
1371 * @brief Subscript access to the data contained in the %deque.
1372 * @param __n The index of the element for which data should be
1373 * accessed.
1374 * @return Read-only (constant) reference to data.
1376 * This operator allows for easy, array-style, data access.
1377 * Note that data access with this operator is unchecked and
1378 * out_of_range lookups are not defined. (For checked lookups
1379 * see at().)
1381 const_reference
1382 operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1383 { return this->_M_impl._M_start[difference_type(__n)]; }
1385 protected:
1386 /// Safety check used only from at().
1387 void
1388 _M_range_check(size_type __n) const
1390 if (__n >= this->size())
1391 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1392 "(which is %zu)>= this->size() "
1393 "(which is %zu)"),
1394 __n, this->size());
1397 public:
1399 * @brief Provides access to the data contained in the %deque.
1400 * @param __n The index of the element for which data should be
1401 * accessed.
1402 * @return Read/write reference to data.
1403 * @throw std::out_of_range If @a __n is an invalid index.
1405 * This function provides for safer data access. The parameter
1406 * is first checked that it is in the range of the deque. The
1407 * function throws out_of_range if the check fails.
1409 reference
1410 at(size_type __n)
1412 _M_range_check(__n);
1413 return (*this)[__n];
1417 * @brief Provides access to the data contained in the %deque.
1418 * @param __n The index of the element for which data should be
1419 * accessed.
1420 * @return Read-only (constant) reference to data.
1421 * @throw std::out_of_range If @a __n is an invalid index.
1423 * This function provides for safer data access. The parameter is first
1424 * checked that it is in the range of the deque. The function throws
1425 * out_of_range if the check fails.
1427 const_reference
1428 at(size_type __n) const
1430 _M_range_check(__n);
1431 return (*this)[__n];
1435 * Returns a read/write reference to the data at the first
1436 * element of the %deque.
1438 reference
1439 front() _GLIBCXX_NOEXCEPT
1440 { return *begin(); }
1443 * Returns a read-only (constant) reference to the data at the first
1444 * element of the %deque.
1446 const_reference
1447 front() const _GLIBCXX_NOEXCEPT
1448 { return *begin(); }
1451 * Returns a read/write reference to the data at the last element of the
1452 * %deque.
1454 reference
1455 back() _GLIBCXX_NOEXCEPT
1457 iterator __tmp = end();
1458 --__tmp;
1459 return *__tmp;
1463 * Returns a read-only (constant) reference to the data at the last
1464 * element of the %deque.
1466 const_reference
1467 back() const _GLIBCXX_NOEXCEPT
1469 const_iterator __tmp = end();
1470 --__tmp;
1471 return *__tmp;
1474 // [23.2.1.2] modifiers
1476 * @brief Add data to the front of the %deque.
1477 * @param __x Data to be added.
1479 * This is a typical stack operation. The function creates an
1480 * element at the front of the %deque and assigns the given
1481 * data to it. Due to the nature of a %deque this operation
1482 * can be done in constant time.
1484 void
1485 push_front(const value_type& __x)
1487 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1489 _Alloc_traits::construct(this->_M_impl,
1490 this->_M_impl._M_start._M_cur - 1,
1491 __x);
1492 --this->_M_impl._M_start._M_cur;
1494 else
1495 _M_push_front_aux(__x);
1498 #if __cplusplus >= 201103L
1499 void
1500 push_front(value_type&& __x)
1501 { emplace_front(std::move(__x)); }
1503 template<typename... _Args>
1504 void
1505 emplace_front(_Args&&... __args);
1506 #endif
1509 * @brief Add data to the end of the %deque.
1510 * @param __x Data to be added.
1512 * This is a typical stack operation. The function creates an
1513 * element at the end of the %deque and assigns the given data
1514 * to it. Due to the nature of a %deque this operation can be
1515 * done in constant time.
1517 void
1518 push_back(const value_type& __x)
1520 if (this->_M_impl._M_finish._M_cur
1521 != this->_M_impl._M_finish._M_last - 1)
1523 _Alloc_traits::construct(this->_M_impl,
1524 this->_M_impl._M_finish._M_cur, __x);
1525 ++this->_M_impl._M_finish._M_cur;
1527 else
1528 _M_push_back_aux(__x);
1531 #if __cplusplus >= 201103L
1532 void
1533 push_back(value_type&& __x)
1534 { emplace_back(std::move(__x)); }
1536 template<typename... _Args>
1537 void
1538 emplace_back(_Args&&... __args);
1539 #endif
1542 * @brief Removes first element.
1544 * This is a typical stack operation. It shrinks the %deque by one.
1546 * Note that no data is returned, and if the first element's data is
1547 * needed, it should be retrieved before pop_front() is called.
1549 void
1550 pop_front() _GLIBCXX_NOEXCEPT
1552 if (this->_M_impl._M_start._M_cur
1553 != this->_M_impl._M_start._M_last - 1)
1555 _Alloc_traits::destroy(this->_M_impl,
1556 this->_M_impl._M_start._M_cur);
1557 ++this->_M_impl._M_start._M_cur;
1559 else
1560 _M_pop_front_aux();
1564 * @brief Removes last element.
1566 * This is a typical stack operation. It shrinks the %deque by one.
1568 * Note that no data is returned, and if the last element's data is
1569 * needed, it should be retrieved before pop_back() is called.
1571 void
1572 pop_back() _GLIBCXX_NOEXCEPT
1574 if (this->_M_impl._M_finish._M_cur
1575 != this->_M_impl._M_finish._M_first)
1577 --this->_M_impl._M_finish._M_cur;
1578 _Alloc_traits::destroy(this->_M_impl,
1579 this->_M_impl._M_finish._M_cur);
1581 else
1582 _M_pop_back_aux();
1585 #if __cplusplus >= 201103L
1587 * @brief Inserts an object in %deque before specified iterator.
1588 * @param __position A const_iterator into the %deque.
1589 * @param __args Arguments.
1590 * @return An iterator that points to the inserted data.
1592 * This function will insert an object of type T constructed
1593 * with T(std::forward<Args>(args)...) before the specified location.
1595 template<typename... _Args>
1596 iterator
1597 emplace(const_iterator __position, _Args&&... __args);
1600 * @brief Inserts given value into %deque before specified iterator.
1601 * @param __position A const_iterator into the %deque.
1602 * @param __x Data to be inserted.
1603 * @return An iterator that points to the inserted data.
1605 * This function will insert a copy of the given value before the
1606 * specified location.
1608 iterator
1609 insert(const_iterator __position, const value_type& __x);
1610 #else
1612 * @brief Inserts given value into %deque before specified iterator.
1613 * @param __position An iterator into the %deque.
1614 * @param __x Data to be inserted.
1615 * @return An iterator that points to the inserted data.
1617 * This function will insert a copy of the given value before the
1618 * specified location.
1620 iterator
1621 insert(iterator __position, const value_type& __x);
1622 #endif
1624 #if __cplusplus >= 201103L
1626 * @brief Inserts given rvalue into %deque before specified iterator.
1627 * @param __position A const_iterator into the %deque.
1628 * @param __x Data to be inserted.
1629 * @return An iterator that points to the inserted data.
1631 * This function will insert a copy of the given rvalue before the
1632 * specified location.
1634 iterator
1635 insert(const_iterator __position, value_type&& __x)
1636 { return emplace(__position, std::move(__x)); }
1639 * @brief Inserts an initializer list into the %deque.
1640 * @param __p An iterator into the %deque.
1641 * @param __l An initializer_list.
1643 * This function will insert copies of the data in the
1644 * initializer_list @a __l into the %deque before the location
1645 * specified by @a __p. This is known as <em>list insert</em>.
1647 iterator
1648 insert(const_iterator __p, initializer_list<value_type> __l)
1650 auto __offset = __p - cbegin();
1651 _M_range_insert_aux(__p._M_const_cast(), __l.begin(), __l.end(),
1652 std::random_access_iterator_tag());
1653 return begin() + __offset;
1655 #endif
1657 #if __cplusplus >= 201103L
1659 * @brief Inserts a number of copies of given data into the %deque.
1660 * @param __position A const_iterator into the %deque.
1661 * @param __n Number of elements to be inserted.
1662 * @param __x Data to be inserted.
1663 * @return An iterator that points to the inserted data.
1665 * This function will insert a specified number of copies of the given
1666 * data before the location specified by @a __position.
1668 iterator
1669 insert(const_iterator __position, size_type __n, const value_type& __x)
1671 difference_type __offset = __position - cbegin();
1672 _M_fill_insert(__position._M_const_cast(), __n, __x);
1673 return begin() + __offset;
1675 #else
1677 * @brief Inserts a number of copies of given data into the %deque.
1678 * @param __position An iterator into the %deque.
1679 * @param __n Number of elements to be inserted.
1680 * @param __x Data to be inserted.
1682 * This function will insert a specified number of copies of the given
1683 * data before the location specified by @a __position.
1685 void
1686 insert(iterator __position, size_type __n, const value_type& __x)
1687 { _M_fill_insert(__position, __n, __x); }
1688 #endif
1690 #if __cplusplus >= 201103L
1692 * @brief Inserts a range into the %deque.
1693 * @param __position A const_iterator into the %deque.
1694 * @param __first An input iterator.
1695 * @param __last An input iterator.
1696 * @return An iterator that points to the inserted data.
1698 * This function will insert copies of the data in the range
1699 * [__first,__last) into the %deque before the location specified
1700 * by @a __position. This is known as <em>range insert</em>.
1702 template<typename _InputIterator,
1703 typename = std::_RequireInputIter<_InputIterator>>
1704 iterator
1705 insert(const_iterator __position, _InputIterator __first,
1706 _InputIterator __last)
1708 difference_type __offset = __position - cbegin();
1709 _M_insert_dispatch(__position._M_const_cast(),
1710 __first, __last, __false_type());
1711 return begin() + __offset;
1713 #else
1715 * @brief Inserts a range into the %deque.
1716 * @param __position An iterator into the %deque.
1717 * @param __first An input iterator.
1718 * @param __last An input iterator.
1720 * This function will insert copies of the data in the range
1721 * [__first,__last) into the %deque before the location specified
1722 * by @a __position. This is known as <em>range insert</em>.
1724 template<typename _InputIterator>
1725 void
1726 insert(iterator __position, _InputIterator __first,
1727 _InputIterator __last)
1729 // Check whether it's an integral type. If so, it's not an iterator.
1730 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1731 _M_insert_dispatch(__position, __first, __last, _Integral());
1733 #endif
1736 * @brief Remove element at given position.
1737 * @param __position Iterator pointing to element to be erased.
1738 * @return An iterator pointing to the next element (or end()).
1740 * This function will erase the element at the given position and thus
1741 * shorten the %deque by one.
1743 * The user is cautioned that
1744 * this function only erases the element, and that if the element is
1745 * itself a pointer, the pointed-to memory is not touched in any way.
1746 * Managing the pointer is the user's responsibility.
1748 iterator
1749 #if __cplusplus >= 201103L
1750 erase(const_iterator __position)
1751 #else
1752 erase(iterator __position)
1753 #endif
1754 { return _M_erase(__position._M_const_cast()); }
1757 * @brief Remove a range of elements.
1758 * @param __first Iterator pointing to the first element to be erased.
1759 * @param __last Iterator pointing to one past the last element to be
1760 * erased.
1761 * @return An iterator pointing to the element pointed to by @a last
1762 * prior to erasing (or end()).
1764 * This function will erase the elements in the range
1765 * [__first,__last) and shorten the %deque accordingly.
1767 * The user is cautioned that
1768 * this function only erases the elements, and that if the elements
1769 * themselves are pointers, the pointed-to memory is not touched in any
1770 * way. Managing the pointer is the user's responsibility.
1772 iterator
1773 #if __cplusplus >= 201103L
1774 erase(const_iterator __first, const_iterator __last)
1775 #else
1776 erase(iterator __first, iterator __last)
1777 #endif
1778 { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1781 * @brief Swaps data with another %deque.
1782 * @param __x A %deque of the same element and allocator types.
1784 * This exchanges the elements between two deques in constant time.
1785 * (Four pointers, so it should be quite fast.)
1786 * Note that the global std::swap() function is specialized such that
1787 * std::swap(d1,d2) will feed to this function.
1789 void
1790 swap(deque& __x) _GLIBCXX_NOEXCEPT
1792 _M_impl._M_swap_data(__x._M_impl);
1793 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1794 __x._M_get_Tp_allocator());
1798 * Erases all the elements. Note that this function only erases the
1799 * elements, and that if the elements themselves are pointers, the
1800 * pointed-to memory is not touched in any way. Managing the pointer is
1801 * the user's responsibility.
1803 void
1804 clear() _GLIBCXX_NOEXCEPT
1805 { _M_erase_at_end(begin()); }
1807 protected:
1808 // Internal constructor functions follow.
1810 // called by the range constructor to implement [23.1.1]/9
1812 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1813 // 438. Ambiguity in the "do the right thing" clause
1814 template<typename _Integer>
1815 void
1816 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1818 _M_initialize_map(static_cast<size_type>(__n));
1819 _M_fill_initialize(__x);
1822 // called by the range constructor to implement [23.1.1]/9
1823 template<typename _InputIterator>
1824 void
1825 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1826 __false_type)
1828 _M_range_initialize(__first, __last,
1829 std::__iterator_category(__first));
1832 // called by the second initialize_dispatch above
1833 //@{
1835 * @brief Fills the deque with whatever is in [first,last).
1836 * @param __first An input iterator.
1837 * @param __last An input iterator.
1838 * @return Nothing.
1840 * If the iterators are actually forward iterators (or better), then the
1841 * memory layout can be done all at once. Else we move forward using
1842 * push_back on each value from the iterator.
1844 template<typename _InputIterator>
1845 void
1846 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1847 std::input_iterator_tag);
1849 // called by the second initialize_dispatch above
1850 template<typename _ForwardIterator>
1851 void
1852 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1853 std::forward_iterator_tag);
1854 //@}
1857 * @brief Fills the %deque with copies of value.
1858 * @param __value Initial value.
1859 * @return Nothing.
1860 * @pre _M_start and _M_finish have already been initialized,
1861 * but none of the %deque's elements have yet been constructed.
1863 * This function is called only when the user provides an explicit size
1864 * (with or without an explicit exemplar value).
1866 void
1867 _M_fill_initialize(const value_type& __value);
1869 #if __cplusplus >= 201103L
1870 // called by deque(n).
1871 void
1872 _M_default_initialize();
1873 #endif
1875 // Internal assign functions follow. The *_aux functions do the actual
1876 // assignment work for the range versions.
1878 // called by the range assign to implement [23.1.1]/9
1880 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1881 // 438. Ambiguity in the "do the right thing" clause
1882 template<typename _Integer>
1883 void
1884 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1885 { _M_fill_assign(__n, __val); }
1887 // called by the range assign to implement [23.1.1]/9
1888 template<typename _InputIterator>
1889 void
1890 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1891 __false_type)
1892 { _M_assign_aux(__first, __last, std::__iterator_category(__first)); }
1894 // called by the second assign_dispatch above
1895 template<typename _InputIterator>
1896 void
1897 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1898 std::input_iterator_tag);
1900 // called by the second assign_dispatch above
1901 template<typename _ForwardIterator>
1902 void
1903 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1904 std::forward_iterator_tag)
1906 const size_type __len = std::distance(__first, __last);
1907 if (__len > size())
1909 _ForwardIterator __mid = __first;
1910 std::advance(__mid, size());
1911 std::copy(__first, __mid, begin());
1912 _M_range_insert_aux(end(), __mid, __last,
1913 std::__iterator_category(__first));
1915 else
1916 _M_erase_at_end(std::copy(__first, __last, begin()));
1919 // Called by assign(n,t), and the range assign when it turns out
1920 // to be the same thing.
1921 void
1922 _M_fill_assign(size_type __n, const value_type& __val)
1924 if (__n > size())
1926 std::fill(begin(), end(), __val);
1927 _M_fill_insert(end(), __n - size(), __val);
1929 else
1931 _M_erase_at_end(begin() + difference_type(__n));
1932 std::fill(begin(), end(), __val);
1936 //@{
1937 /// Helper functions for push_* and pop_*.
1938 #if __cplusplus < 201103L
1939 void _M_push_back_aux(const value_type&);
1941 void _M_push_front_aux(const value_type&);
1942 #else
1943 template<typename... _Args>
1944 void _M_push_back_aux(_Args&&... __args);
1946 template<typename... _Args>
1947 void _M_push_front_aux(_Args&&... __args);
1948 #endif
1950 void _M_pop_back_aux();
1952 void _M_pop_front_aux();
1953 //@}
1955 // Internal insert functions follow. The *_aux functions do the actual
1956 // insertion work when all shortcuts fail.
1958 // called by the range insert to implement [23.1.1]/9
1960 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1961 // 438. Ambiguity in the "do the right thing" clause
1962 template<typename _Integer>
1963 void
1964 _M_insert_dispatch(iterator __pos,
1965 _Integer __n, _Integer __x, __true_type)
1966 { _M_fill_insert(__pos, __n, __x); }
1968 // called by the range insert to implement [23.1.1]/9
1969 template<typename _InputIterator>
1970 void
1971 _M_insert_dispatch(iterator __pos,
1972 _InputIterator __first, _InputIterator __last,
1973 __false_type)
1975 _M_range_insert_aux(__pos, __first, __last,
1976 std::__iterator_category(__first));
1979 // called by the second insert_dispatch above
1980 template<typename _InputIterator>
1981 void
1982 _M_range_insert_aux(iterator __pos, _InputIterator __first,
1983 _InputIterator __last, std::input_iterator_tag);
1985 // called by the second insert_dispatch above
1986 template<typename _ForwardIterator>
1987 void
1988 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1989 _ForwardIterator __last, std::forward_iterator_tag);
1991 // Called by insert(p,n,x), and the range insert when it turns out to be
1992 // the same thing. Can use fill functions in optimal situations,
1993 // otherwise passes off to insert_aux(p,n,x).
1994 void
1995 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1997 // called by insert(p,x)
1998 #if __cplusplus < 201103L
1999 iterator
2000 _M_insert_aux(iterator __pos, const value_type& __x);
2001 #else
2002 template<typename... _Args>
2003 iterator
2004 _M_insert_aux(iterator __pos, _Args&&... __args);
2005 #endif
2007 // called by insert(p,n,x) via fill_insert
2008 void
2009 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
2011 // called by range_insert_aux for forward iterators
2012 template<typename _ForwardIterator>
2013 void
2014 _M_insert_aux(iterator __pos,
2015 _ForwardIterator __first, _ForwardIterator __last,
2016 size_type __n);
2019 // Internal erase functions follow.
2021 void
2022 _M_destroy_data_aux(iterator __first, iterator __last);
2024 // Called by ~deque().
2025 // NB: Doesn't deallocate the nodes.
2026 template<typename _Alloc1>
2027 void
2028 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
2029 { _M_destroy_data_aux(__first, __last); }
2031 void
2032 _M_destroy_data(iterator __first, iterator __last,
2033 const std::allocator<_Tp>&)
2035 if (!__has_trivial_destructor(value_type))
2036 _M_destroy_data_aux(__first, __last);
2039 // Called by erase(q1, q2).
2040 void
2041 _M_erase_at_begin(iterator __pos)
2043 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
2044 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
2045 this->_M_impl._M_start = __pos;
2048 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2049 // _M_fill_assign, operator=.
2050 void
2051 _M_erase_at_end(iterator __pos)
2053 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
2054 _M_destroy_nodes(__pos._M_node + 1,
2055 this->_M_impl._M_finish._M_node + 1);
2056 this->_M_impl._M_finish = __pos;
2059 iterator
2060 _M_erase(iterator __pos);
2062 iterator
2063 _M_erase(iterator __first, iterator __last);
2065 #if __cplusplus >= 201103L
2066 // Called by resize(sz).
2067 void
2068 _M_default_append(size_type __n);
2070 bool
2071 _M_shrink_to_fit();
2072 #endif
2074 //@{
2075 /// Memory-handling helpers for the previous internal insert functions.
2076 iterator
2077 _M_reserve_elements_at_front(size_type __n)
2079 const size_type __vacancies = this->_M_impl._M_start._M_cur
2080 - this->_M_impl._M_start._M_first;
2081 if (__n > __vacancies)
2082 _M_new_elements_at_front(__n - __vacancies);
2083 return this->_M_impl._M_start - difference_type(__n);
2086 iterator
2087 _M_reserve_elements_at_back(size_type __n)
2089 const size_type __vacancies = (this->_M_impl._M_finish._M_last
2090 - this->_M_impl._M_finish._M_cur) - 1;
2091 if (__n > __vacancies)
2092 _M_new_elements_at_back(__n - __vacancies);
2093 return this->_M_impl._M_finish + difference_type(__n);
2096 void
2097 _M_new_elements_at_front(size_type __new_elements);
2099 void
2100 _M_new_elements_at_back(size_type __new_elements);
2101 //@}
2104 //@{
2106 * @brief Memory-handling helpers for the major %map.
2108 * Makes sure the _M_map has space for new nodes. Does not
2109 * actually add the nodes. Can invalidate _M_map pointers.
2110 * (And consequently, %deque iterators.)
2112 void
2113 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
2115 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
2116 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
2117 _M_reallocate_map(__nodes_to_add, false);
2120 void
2121 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
2123 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
2124 - this->_M_impl._M_map))
2125 _M_reallocate_map(__nodes_to_add, true);
2128 void
2129 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
2130 //@}
2132 #if __cplusplus >= 201103L
2133 // Constant-time, nothrow move assignment when source object's memory
2134 // can be moved because the allocators are equal.
2135 void
2136 _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
2138 this->_M_impl._M_swap_data(__x._M_impl);
2139 __x.clear();
2140 std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
2143 // When the allocators are not equal the operation could throw, because
2144 // we might need to allocate a new map for __x after moving from it
2145 // or we might need to allocate new elements for *this.
2146 void
2147 _M_move_assign1(deque&& __x, /* always equal: */ false_type)
2149 constexpr bool __move_storage =
2150 _Alloc_traits::_S_propagate_on_move_assign();
2151 _M_move_assign2(std::move(__x), __bool_constant<__move_storage>());
2154 // Destroy all elements and deallocate all memory, then replace
2155 // with elements created from __args.
2156 template<typename... _Args>
2157 void
2158 _M_replace_map(_Args&&... __args)
2160 // Create new data first, so if allocation fails there are no effects.
2161 deque __newobj(std::forward<_Args>(__args)...);
2162 // Free existing storage using existing allocator.
2163 clear();
2164 _M_deallocate_node(*begin()._M_node); // one node left after clear()
2165 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
2166 this->_M_impl._M_map = nullptr;
2167 this->_M_impl._M_map_size = 0;
2168 // Take ownership of replacement memory.
2169 this->_M_impl._M_swap_data(__newobj._M_impl);
2172 // Do move assignment when the allocator propagates.
2173 void
2174 _M_move_assign2(deque&& __x, /* propagate: */ true_type)
2176 // Make a copy of the original allocator state.
2177 auto __alloc = __x._M_get_Tp_allocator();
2178 // The allocator propagates so storage can be moved from __x,
2179 // leaving __x in a valid empty state with a moved-from allocator.
2180 _M_replace_map(std::move(__x));
2181 // Move the corresponding allocator state too.
2182 _M_get_Tp_allocator() = std::move(__alloc);
2185 // Do move assignment when it may not be possible to move source
2186 // object's memory, resulting in a linear-time operation.
2187 void
2188 _M_move_assign2(deque&& __x, /* propagate: */ false_type)
2190 if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2192 // The allocators are equal so storage can be moved from __x,
2193 // leaving __x in a valid empty state with its current allocator.
2194 _M_replace_map(std::move(__x), __x.get_allocator());
2196 else
2198 // The rvalue's allocator cannot be moved and is not equal,
2199 // so we need to individually move each element.
2200 _M_assign_aux(std::__make_move_if_noexcept_iterator(__x.begin()),
2201 std::__make_move_if_noexcept_iterator(__x.end()),
2202 std::random_access_iterator_tag());
2203 __x.clear();
2206 #endif
2211 * @brief Deque equality comparison.
2212 * @param __x A %deque.
2213 * @param __y A %deque of the same type as @a __x.
2214 * @return True iff the size and elements of the deques are equal.
2216 * This is an equivalence relation. It is linear in the size of the
2217 * deques. Deques are considered equivalent if their sizes are equal,
2218 * and if corresponding elements compare equal.
2220 template<typename _Tp, typename _Alloc>
2221 inline bool
2222 operator==(const deque<_Tp, _Alloc>& __x,
2223 const deque<_Tp, _Alloc>& __y)
2224 { return __x.size() == __y.size()
2225 && std::equal(__x.begin(), __x.end(), __y.begin()); }
2228 * @brief Deque ordering relation.
2229 * @param __x A %deque.
2230 * @param __y A %deque of the same type as @a __x.
2231 * @return True iff @a x is lexicographically less than @a __y.
2233 * This is a total ordering relation. It is linear in the size of the
2234 * deques. The elements must be comparable with @c <.
2236 * See std::lexicographical_compare() for how the determination is made.
2238 template<typename _Tp, typename _Alloc>
2239 inline bool
2240 operator<(const deque<_Tp, _Alloc>& __x,
2241 const deque<_Tp, _Alloc>& __y)
2242 { return std::lexicographical_compare(__x.begin(), __x.end(),
2243 __y.begin(), __y.end()); }
2245 /// Based on operator==
2246 template<typename _Tp, typename _Alloc>
2247 inline bool
2248 operator!=(const deque<_Tp, _Alloc>& __x,
2249 const deque<_Tp, _Alloc>& __y)
2250 { return !(__x == __y); }
2252 /// Based on operator<
2253 template<typename _Tp, typename _Alloc>
2254 inline bool
2255 operator>(const deque<_Tp, _Alloc>& __x,
2256 const deque<_Tp, _Alloc>& __y)
2257 { return __y < __x; }
2259 /// Based on operator<
2260 template<typename _Tp, typename _Alloc>
2261 inline bool
2262 operator<=(const deque<_Tp, _Alloc>& __x,
2263 const deque<_Tp, _Alloc>& __y)
2264 { return !(__y < __x); }
2266 /// Based on operator<
2267 template<typename _Tp, typename _Alloc>
2268 inline bool
2269 operator>=(const deque<_Tp, _Alloc>& __x,
2270 const deque<_Tp, _Alloc>& __y)
2271 { return !(__x < __y); }
2273 /// See std::deque::swap().
2274 template<typename _Tp, typename _Alloc>
2275 inline void
2276 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
2277 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2278 { __x.swap(__y); }
2280 #undef _GLIBCXX_DEQUE_BUF_SIZE
2282 _GLIBCXX_END_NAMESPACE_CONTAINER
2283 } // namespace std
2285 #endif /* _STL_DEQUE_H */