1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
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)
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/>.
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.
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}
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>
66 #include <debug/assertions.h>
68 namespace std
_GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
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
87 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
88 #define _GLIBCXX_DEQUE_BUF_SIZE 512
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)); }
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
;
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
>>;
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
;
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
;
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
) { }
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;
171 _M_const_cast() const _GLIBCXX_NOEXCEPT
172 { return iterator(_M_cur
, _M_node
); }
175 operator*() const _GLIBCXX_NOEXCEPT
179 operator->() const _GLIBCXX_NOEXCEPT
183 operator++() _GLIBCXX_NOEXCEPT
186 if (_M_cur
== _M_last
)
188 _M_set_node(_M_node
+ 1);
195 operator++(int) _GLIBCXX_NOEXCEPT
203 operator--() _GLIBCXX_NOEXCEPT
205 if (_M_cur
== _M_first
)
207 _M_set_node(_M_node
- 1);
215 operator--(int) _GLIBCXX_NOEXCEPT
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()))
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()));
242 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
249 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
250 { return *this += -__n
; }
253 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
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.
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
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
)
391 { return __x
+ __n
; }
393 template<typename _Tp
>
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
),
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
),
427 const _Tp
&, const _Tp
*>(__last
),
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
),
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
),
460 const _Tp
&, const _Tp
*>(__last
),
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
470 * Nothing in this class ever constructs or destroys an actual Tp element.
471 * (Deque handles that itself.) Only/All memory management is performed
474 template<typename _Tp
, typename _Alloc
>
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
484 typedef const _Tp
* _Ptr_const
;
486 typedef typename
_Alloc_traits::pointer _Ptr
;
487 typedef typename
_Alloc_traits::const_pointer _Ptr_const
;
490 typedef typename
_Alloc_traits::template rebind
<_Ptr
>::other
492 typedef __gnu_cxx::__alloc_traits
<_Map_alloc_type
> _Map_alloc_traits
;
495 typedef _Alloc allocator_type
;
496 typedef typename
_Alloc_traits::size_type size_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
;
507 { _M_initialize_map(0); }
509 _Deque_base(size_t __num_elements
)
511 { _M_initialize_map(__num_elements
); }
513 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
515 { _M_initialize_map(__num_elements
); }
517 _Deque_base(const allocator_type
& __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
)
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
);
551 _M_initialize_map(__n
);
556 ~_Deque_base() _GLIBCXX_NOEXCEPT
;
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.
565 : public _Tp_alloc_type
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()
591 void _M_swap_data(_Deque_impl
& __x
) _GLIBCXX_NOEXCEPT
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
);
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
); }
610 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
611 { return _Map_alloc_type(_M_get_Tp_allocator()); }
616 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Traits
;
617 return _Traits::allocate(_M_impl
, __deque_buf_size(sizeof(_Tp
)));
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
)));
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
);
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
);
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 };
650 #if __cplusplus >= 201103L
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
);
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
692 * The initial underlying memory layout is a bit complicated...
694 template<typename _Tp
, typename _Alloc
>
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
))
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
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
;
716 { _M_create_nodes(__nstart
, __nfinish
); }
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
730 % __deque_buf_size(sizeof(_Tp
)));
733 template<typename _Tp
, typename _Alloc
>
735 _Deque_base
<_Tp
, _Alloc
>::
736 _M_create_nodes(_Map_pointer __nstart
, _Map_pointer __nfinish
)
741 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
742 *__cur
= this->_M_allocate_node();
746 _M_destroy_nodes(__nstart
, __cur
);
747 __throw_exception_again
;
751 template<typename _Tp
, typename _Alloc
>
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.
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:
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.
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
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
)
854 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
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");
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
;
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
;
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
;
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.
919 deque(const allocator_type
& __a
)
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.
932 deque(size_type __n
, const allocator_type
& __a
= allocator_type())
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())
947 { _M_fill_initialize(__value
); }
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.
958 deque(size_type __n
, const value_type
& __value
= value_type(),
959 const allocator_type
& __a
= allocator_type())
961 { _M_fill_initialize(__value
); }
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()),
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.
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());
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())
1024 _M_range_initialize(__l
.begin(), __l
.end(),
1025 random_access_iterator_tag());
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,
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())
1050 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
1052 template<typename _InputIterator
>
1053 deque(_InputIterator __first
, _InputIterator __last
,
1054 const allocator_type
& __a
= allocator_type())
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());
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.
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).
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.
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
{});
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
1112 operator=(initializer_list
<value_type
> __l
)
1114 _M_assign_aux(__l
.begin(), __l
.end(),
1115 random_access_iterator_tag());
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.
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
1146 #if __cplusplus >= 201103L
1147 template<typename _InputIterator
,
1148 typename
= std::_RequireInputIter
<_InputIterator
>>
1150 assign(_InputIterator __first
, _InputIterator __last
)
1151 { _M_assign_dispatch(__first
, __last
, __false_type()); }
1153 template<typename _InputIterator
>
1155 assign(_InputIterator __first
, _InputIterator __last
)
1157 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1158 _M_assign_dispatch(__first
, __last
, _Integral());
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
1175 assign(initializer_list
<value_type
> __l
)
1176 { _M_assign_aux(__l
.begin(), __l
.end(), random_access_iterator_tag()); }
1179 /// Get a copy of the memory allocation object.
1181 get_allocator() const _GLIBCXX_NOEXCEPT
1182 { return _Base::get_allocator(); }
1186 * Returns a read/write iterator that points to the first element in the
1187 * %deque. Iteration is done in ordinary element order.
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.
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
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.
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
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.
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.
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.
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
); }
1292 // [23.2.1.2] capacity
1293 /** Returns the number of elements in the %deque. */
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. */
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.
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
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
));
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
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
));
1369 #if __cplusplus >= 201103L
1370 /** A non-binding request to reduce memory use. */
1372 shrink_to_fit() noexcept
1373 { _M_shrink_to_fit(); }
1377 * Returns true if the %deque is empty. (Thus begin() would
1381 empty() const _GLIBCXX_NOEXCEPT
1382 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1386 * @brief Subscript access to the data contained in the %deque.
1387 * @param __n The index of the element for which data should be
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
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
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
1415 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1417 __glibcxx_requires_subscript(__n
);
1418 return this->_M_impl
._M_start
[difference_type(__n
)];
1422 /// Safety check used only from at().
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() "
1435 * @brief Provides access to the data contained in the %deque.
1436 * @param __n The index of the element for which data should be
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.
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
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.
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.
1475 front() _GLIBCXX_NOEXCEPT
1477 __glibcxx_requires_nonempty();
1482 * Returns a read-only (constant) reference to the data at the first
1483 * element of the %deque.
1486 front() const _GLIBCXX_NOEXCEPT
1488 __glibcxx_requires_nonempty();
1493 * Returns a read/write reference to the data at the last element of the
1497 back() _GLIBCXX_NOEXCEPT
1499 __glibcxx_requires_nonempty();
1500 iterator __tmp
= end();
1506 * Returns a read-only (constant) reference to the data at the last
1507 * element of the %deque.
1510 back() const _GLIBCXX_NOEXCEPT
1512 __glibcxx_requires_nonempty();
1513 const_iterator __tmp
= end();
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.
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,
1536 --this->_M_impl
._M_start
._M_cur
;
1539 _M_push_front_aux(__x
);
1542 #if __cplusplus >= 201103L
1544 push_front(value_type
&& __x
)
1545 { emplace_front(std::move(__x
)); }
1547 template<typename
... _Args
>
1548 #if __cplusplus > 201402L
1553 emplace_front(_Args
&&... __args
);
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.
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
;
1576 _M_push_back_aux(__x
);
1579 #if __cplusplus >= 201103L
1581 push_back(value_type
&& __x
)
1582 { emplace_back(std::move(__x
)); }
1584 template<typename
... _Args
>
1585 #if __cplusplus > 201402L
1590 emplace_back(_Args
&&... __args
);
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.
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
;
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.
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
);
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
>
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.
1663 insert(const_iterator __position
, const value_type
& __x
);
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.
1675 insert(iterator __position
, const value_type
& __x
);
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.
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>.
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
;
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.
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
;
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.
1740 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1741 { _M_fill_insert(__position
, __n
, __x
); }
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
>>
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
;
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
>
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());
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.
1803 #if __cplusplus >= 201103L
1804 erase(const_iterator __position
)
1806 erase(iterator __position
)
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
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.
1827 #if __cplusplus >= 201103L
1828 erase(const_iterator __first
, const_iterator __last
)
1830 erase(iterator __first
, iterator __last
)
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.
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());
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.
1864 clear() _GLIBCXX_NOEXCEPT
1865 { _M_erase_at_end(begin()); }
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
>
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
>
1885 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1888 _M_range_initialize(__first
, __last
,
1889 std::__iterator_category(__first
));
1892 // called by the second initialize_dispatch above
1895 * @brief Fills the deque with whatever is in [first,last).
1896 * @param __first An input iterator.
1897 * @param __last An input iterator.
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
>
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
>
1912 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1913 std::forward_iterator_tag
);
1917 * @brief Fills the %deque with copies of value.
1918 * @param __value Initial value.
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).
1927 _M_fill_initialize(const value_type
& __value
);
1929 #if __cplusplus >= 201103L
1930 // called by deque(n).
1932 _M_default_initialize();
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
>
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
>
1950 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1952 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1954 // called by the second assign_dispatch above
1955 template<typename _InputIterator
>
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
>
1963 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1964 std::forward_iterator_tag
)
1966 const size_type __len
= std::distance(__first
, __last
);
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
));
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.
1982 _M_fill_assign(size_type __n
, const value_type
& __val
)
1986 std::fill(begin(), end(), __val
);
1987 _M_fill_insert(end(), __n
- size(), __val
);
1991 _M_erase_at_end(begin() + difference_type(__n
));
1992 std::fill(begin(), end(), __val
);
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
&);
2003 template<typename
... _Args
>
2004 void _M_push_back_aux(_Args
&&... __args
);
2006 template<typename
... _Args
>
2007 void _M_push_front_aux(_Args
&&... __args
);
2010 void _M_pop_back_aux();
2012 void _M_pop_front_aux();
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
>
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
>
2031 _M_insert_dispatch(iterator __pos
,
2032 _InputIterator __first
, _InputIterator __last
,
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
>
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
>
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).
2055 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
2057 // called by insert(p,x)
2058 #if __cplusplus < 201103L
2060 _M_insert_aux(iterator __pos
, const value_type
& __x
);
2062 template<typename
... _Args
>
2064 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
2067 // called by insert(p,n,x) via fill_insert
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
>
2074 _M_insert_aux(iterator __pos
,
2075 _ForwardIterator __first
, _ForwardIterator __last
,
2079 // Internal erase functions follow.
2082 _M_destroy_data_aux(iterator __first
, iterator __last
);
2084 // Called by ~deque().
2085 // NB: Doesn't deallocate the nodes.
2086 template<typename _Alloc1
>
2088 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
2089 { _M_destroy_data_aux(__first
, __last
); }
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).
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=.
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
;
2120 _M_erase(iterator __pos
);
2123 _M_erase(iterator __first
, iterator __last
);
2125 #if __cplusplus >= 201103L
2126 // Called by resize(sz).
2128 _M_default_append(size_type __n
);
2135 /// Memory-handling helpers for the previous internal insert functions.
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
);
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
);
2157 _M_new_elements_at_front(size_type __new_elements
);
2160 _M_new_elements_at_back(size_type __new_elements
);
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.)
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);
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);
2189 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
2192 #if __cplusplus >= 201103L
2193 // Constant-time, nothrow move assignment when source object's memory
2194 // can be moved because the allocators are equal.
2196 _M_move_assign1(deque
&& __x
, /* always equal: */ true_type
) noexcept
2198 this->_M_impl
._M_swap_data(__x
._M_impl
);
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.
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
>
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.
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.
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.
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());
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());
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
>;
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
>
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
>
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
>
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
>
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
>
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
>
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
>
2345 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
2346 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2349 #undef _GLIBCXX_DEQUE_BUF_SIZE
2351 _GLIBCXX_END_NAMESPACE_CONTAINER
2352 _GLIBCXX_END_NAMESPACE_VERSION
2355 #endif /* _STL_DEQUE_H */