1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001-2016 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_CONTAINER
73 * @brief This function controls the size of memory nodes.
74 * @param __size The size of an element.
75 * @return The number (not byte size) of elements per node.
77 * This function started off as a compiler kludge from SGI, but
78 * seems to be a useful wrapper around a repeated constant
79 * expression. The @b 512 is tunable (and no other code needs to
80 * change), but no investigation has been done since inheriting the
81 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
82 * you are doing, however: changing it breaks the binary
86 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
87 #define _GLIBCXX_DEQUE_BUF_SIZE 512
90 _GLIBCXX_CONSTEXPR
inline size_t
91 __deque_buf_size(size_t __size
)
92 { return (__size
< _GLIBCXX_DEQUE_BUF_SIZE
93 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE
/ __size
) : size_t(1)); }
97 * @brief A deque::iterator.
99 * Quite a bit of intelligence here. Much of the functionality of
100 * deque is actually passed off to this class. A deque holds two
101 * of these internally, marking its valid range. Access to
102 * elements is done as offsets of either of those two, relying on
103 * operator overloading in this class.
105 * All the functions are op overloads except for _M_set_node.
107 template<typename _Tp
, typename _Ref
, typename _Ptr
>
108 struct _Deque_iterator
110 #if __cplusplus < 201103L
111 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
112 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
113 typedef _Tp
* _Elt_pointer
;
114 typedef _Tp
** _Map_pointer
;
117 template<typename _Up
>
118 using __ptr_to
= typename pointer_traits
<_Ptr
>::template rebind
<_Up
>;
119 template<typename _CvTp
>
120 using __iter
= _Deque_iterator
<_Tp
, _CvTp
&, __ptr_to
<_CvTp
>>;
122 typedef __iter
<_Tp
> iterator
;
123 typedef __iter
<const _Tp
> const_iterator
;
124 typedef __ptr_to
<_Tp
> _Elt_pointer
;
125 typedef __ptr_to
<_Elt_pointer
> _Map_pointer
;
128 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
129 { return __deque_buf_size(sizeof(_Tp
)); }
131 typedef std::random_access_iterator_tag iterator_category
;
132 typedef _Tp value_type
;
133 typedef _Ptr pointer
;
134 typedef _Ref reference
;
135 typedef size_t size_type
;
136 typedef ptrdiff_t difference_type
;
137 typedef _Deque_iterator _Self
;
140 _Elt_pointer _M_first
;
141 _Elt_pointer _M_last
;
142 _Map_pointer _M_node
;
144 _Deque_iterator(_Elt_pointer __x
, _Map_pointer __y
) _GLIBCXX_NOEXCEPT
145 : _M_cur(__x
), _M_first(*__y
),
146 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
148 _Deque_iterator() _GLIBCXX_NOEXCEPT
149 : _M_cur(), _M_first(), _M_last(), _M_node() { }
151 _Deque_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
152 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
153 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
156 _M_const_cast() const _GLIBCXX_NOEXCEPT
157 { return iterator(_M_cur
, _M_node
); }
160 operator*() const _GLIBCXX_NOEXCEPT
164 operator->() const _GLIBCXX_NOEXCEPT
168 operator++() _GLIBCXX_NOEXCEPT
171 if (_M_cur
== _M_last
)
173 _M_set_node(_M_node
+ 1);
180 operator++(int) _GLIBCXX_NOEXCEPT
188 operator--() _GLIBCXX_NOEXCEPT
190 if (_M_cur
== _M_first
)
192 _M_set_node(_M_node
- 1);
200 operator--(int) _GLIBCXX_NOEXCEPT
208 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
210 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
211 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
215 const difference_type __node_offset
=
216 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
217 : -difference_type((-__offset
- 1)
218 / _S_buffer_size()) - 1;
219 _M_set_node(_M_node
+ __node_offset
);
220 _M_cur
= _M_first
+ (__offset
- __node_offset
221 * difference_type(_S_buffer_size()));
227 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
234 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
235 { return *this += -__n
; }
238 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
245 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
246 { return *(*this + __n
); }
249 * Prepares to traverse new_node. Sets everything except
250 * _M_cur, which should therefore be set by the caller
251 * immediately afterwards, based on _M_first and _M_last.
254 _M_set_node(_Map_pointer __new_node
) _GLIBCXX_NOEXCEPT
256 _M_node
= __new_node
;
257 _M_first
= *__new_node
;
258 _M_last
= _M_first
+ difference_type(_S_buffer_size());
262 // Note: we also provide overloads whose operands are of the same type in
263 // order to avoid ambiguous overload resolution when std::rel_ops operators
264 // are in scope (for additional details, see libstdc++/3628)
265 template<typename _Tp
, typename _Ref
, typename _Ptr
>
267 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
268 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
269 { return __x
._M_cur
== __y
._M_cur
; }
271 template<typename _Tp
, typename _RefL
, typename _PtrL
,
272 typename _RefR
, typename _PtrR
>
274 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
275 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
276 { return __x
._M_cur
== __y
._M_cur
; }
278 template<typename _Tp
, typename _Ref
, typename _Ptr
>
280 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
281 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
282 { return !(__x
== __y
); }
284 template<typename _Tp
, typename _RefL
, typename _PtrL
,
285 typename _RefR
, typename _PtrR
>
287 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
288 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
289 { return !(__x
== __y
); }
291 template<typename _Tp
, typename _Ref
, typename _Ptr
>
293 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
294 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
295 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
296 : (__x
._M_node
< __y
._M_node
); }
298 template<typename _Tp
, typename _RefL
, typename _PtrL
,
299 typename _RefR
, typename _PtrR
>
301 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
302 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
303 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
304 : (__x
._M_node
< __y
._M_node
); }
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 __y
< __x
; }
312 template<typename _Tp
, typename _RefL
, typename _PtrL
,
313 typename _RefR
, typename _PtrR
>
315 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
316 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
317 { return __y
< __x
; }
319 template<typename _Tp
, typename _Ref
, typename _Ptr
>
321 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
322 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
323 { return !(__y
< __x
); }
325 template<typename _Tp
, typename _RefL
, typename _PtrL
,
326 typename _RefR
, typename _PtrR
>
328 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
329 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
330 { return !(__y
< __x
); }
332 template<typename _Tp
, typename _Ref
, typename _Ptr
>
334 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
335 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
336 { return !(__x
< __y
); }
338 template<typename _Tp
, typename _RefL
, typename _PtrL
,
339 typename _RefR
, typename _PtrR
>
341 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
342 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
343 { return !(__x
< __y
); }
345 // _GLIBCXX_RESOLVE_LIB_DEFECTS
346 // According to the resolution of DR179 not only the various comparison
347 // operators but also operator- must accept mixed iterator/const_iterator
349 template<typename _Tp
, typename _Ref
, typename _Ptr
>
350 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
351 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
352 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
354 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
355 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
356 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
357 + (__y
._M_last
- __y
._M_cur
);
360 template<typename _Tp
, typename _RefL
, typename _PtrL
,
361 typename _RefR
, typename _PtrR
>
362 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
363 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
364 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
366 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
367 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
368 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
369 + (__y
._M_last
- __y
._M_cur
);
372 template<typename _Tp
, typename _Ref
, typename _Ptr
>
373 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
374 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
376 { return __x
+ __n
; }
378 template<typename _Tp
>
380 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
381 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
383 template<typename _Tp
>
384 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
385 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
386 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
387 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
389 template<typename _Tp
>
390 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
391 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
392 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
393 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
394 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
395 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
398 template<typename _Tp
>
399 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
400 copy_backward(_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_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
407 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
408 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
409 { return std::copy_backward(_Deque_iterator
<_Tp
,
410 const _Tp
&, const _Tp
*>(__first
),
412 const _Tp
&, const _Tp
*>(__last
),
415 #if __cplusplus >= 201103L
416 template<typename _Tp
>
417 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
418 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
419 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
420 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
422 template<typename _Tp
>
423 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
424 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
425 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
426 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
427 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
428 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
431 template<typename _Tp
>
432 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
433 move_backward(_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_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
440 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
441 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
442 { return std::move_backward(_Deque_iterator
<_Tp
,
443 const _Tp
&, const _Tp
*>(__first
),
445 const _Tp
&, const _Tp
*>(__last
),
450 * Deque base class. This class provides the unified face for %deque's
451 * allocation. This class's constructor and destructor allocate and
452 * deallocate (but do not initialize) storage. This makes %exception
455 * Nothing in this class ever constructs or destroys an actual Tp element.
456 * (Deque handles that itself.) Only/All memory management is performed
459 template<typename _Tp
, typename _Alloc
>
463 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
464 rebind
<_Tp
>::other _Tp_alloc_type
;
465 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
467 #if __cplusplus < 201103L
469 typedef const _Tp
* _Ptr_const
;
471 typedef typename
_Alloc_traits::pointer _Ptr
;
472 typedef typename
_Alloc_traits::const_pointer _Ptr_const
;
475 typedef typename
_Alloc_traits::template rebind
<_Ptr
>::other
477 typedef __gnu_cxx::__alloc_traits
<_Map_alloc_type
> _Map_alloc_traits
;
480 typedef _Alloc allocator_type
;
481 typedef typename
_Alloc_traits::size_type size_type
;
484 get_allocator() const _GLIBCXX_NOEXCEPT
485 { return allocator_type(_M_get_Tp_allocator()); }
487 typedef _Deque_iterator
<_Tp
, _Tp
&, _Ptr
> iterator
;
488 typedef _Deque_iterator
<_Tp
, const _Tp
&, _Ptr_const
> const_iterator
;
492 { _M_initialize_map(0); }
494 _Deque_base(size_t __num_elements
)
496 { _M_initialize_map(__num_elements
); }
498 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
500 { _M_initialize_map(__num_elements
); }
502 _Deque_base(const allocator_type
& __a
)
504 { /* Caller must initialize map. */ }
506 #if __cplusplus >= 201103L
507 _Deque_base(_Deque_base
&& __x
, false_type
)
508 : _M_impl(__x
._M_move_impl())
511 _Deque_base(_Deque_base
&& __x
, true_type
)
512 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
514 _M_initialize_map(0);
515 if (__x
._M_impl
._M_map
)
516 this->_M_impl
._M_swap_data(__x
._M_impl
);
519 _Deque_base(_Deque_base
&& __x
)
520 : _Deque_base(std::move(__x
), typename
_Alloc_traits::is_always_equal
{})
523 _Deque_base(_Deque_base
&& __x
, const allocator_type
& __a
, size_type __n
)
526 if (__x
.get_allocator() == __a
)
528 if (__x
._M_impl
._M_map
)
530 _M_initialize_map(0);
531 this->_M_impl
._M_swap_data(__x
._M_impl
);
536 _M_initialize_map(__n
);
541 ~_Deque_base() _GLIBCXX_NOEXCEPT
;
544 typedef typename
iterator::_Map_pointer _Map_pointer
;
546 //This struct encapsulates the implementation of the std::deque
547 //standard container and at the same time makes use of the EBO
548 //for empty allocators.
550 : public _Tp_alloc_type
558 : _Tp_alloc_type(), _M_map(), _M_map_size(0),
559 _M_start(), _M_finish()
562 _Deque_impl(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
563 : _Tp_alloc_type(__a
), _M_map(), _M_map_size(0),
564 _M_start(), _M_finish()
567 #if __cplusplus >= 201103L
568 _Deque_impl(_Deque_impl
&&) = default;
570 _Deque_impl(_Tp_alloc_type
&& __a
) noexcept
571 : _Tp_alloc_type(std::move(__a
)), _M_map(), _M_map_size(0),
572 _M_start(), _M_finish()
576 void _M_swap_data(_Deque_impl
& __x
) _GLIBCXX_NOEXCEPT
579 swap(this->_M_start
, __x
._M_start
);
580 swap(this->_M_finish
, __x
._M_finish
);
581 swap(this->_M_map
, __x
._M_map
);
582 swap(this->_M_map_size
, __x
._M_map_size
);
587 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
588 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
590 const _Tp_alloc_type
&
591 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
592 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
595 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
596 { return _Map_alloc_type(_M_get_Tp_allocator()); }
601 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Traits
;
602 return _Traits::allocate(_M_impl
, __deque_buf_size(sizeof(_Tp
)));
606 _M_deallocate_node(_Ptr __p
) _GLIBCXX_NOEXCEPT
608 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Traits
;
609 _Traits::deallocate(_M_impl
, __p
, __deque_buf_size(sizeof(_Tp
)));
613 _M_allocate_map(size_t __n
)
615 _Map_alloc_type __map_alloc
= _M_get_map_allocator();
616 return _Map_alloc_traits::allocate(__map_alloc
, __n
);
620 _M_deallocate_map(_Map_pointer __p
, size_t __n
) _GLIBCXX_NOEXCEPT
622 _Map_alloc_type __map_alloc
= _M_get_map_allocator();
623 _Map_alloc_traits::deallocate(__map_alloc
, __p
, __n
);
627 void _M_initialize_map(size_t);
628 void _M_create_nodes(_Map_pointer __nstart
, _Map_pointer __nfinish
);
629 void _M_destroy_nodes(_Map_pointer __nstart
,
630 _Map_pointer __nfinish
) _GLIBCXX_NOEXCEPT
;
631 enum { _S_initial_map_size
= 8 };
635 #if __cplusplus >= 201103L
641 return std::move(_M_impl
);
643 // Create a copy of the current allocator.
644 _Tp_alloc_type __alloc
{_M_get_Tp_allocator()};
645 // Put that copy in a moved-from state.
646 _Tp_alloc_type __sink
__attribute((__unused__
)) {std::move(__alloc
)};
647 // Create an empty map that allocates using the moved-from allocator.
648 _Deque_base __empty
{__alloc
};
649 __empty
._M_initialize_map(0);
650 // Now safe to modify current allocator and perform non-throwing swaps.
651 _Deque_impl __ret
{std::move(_M_get_Tp_allocator())};
652 _M_impl
._M_swap_data(__ret
);
653 _M_impl
._M_swap_data(__empty
._M_impl
);
659 template<typename _Tp
, typename _Alloc
>
660 _Deque_base
<_Tp
, _Alloc
>::
661 ~_Deque_base() _GLIBCXX_NOEXCEPT
663 if (this->_M_impl
._M_map
)
665 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
666 this->_M_impl
._M_finish
._M_node
+ 1);
667 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
672 * @brief Layout storage.
673 * @param __num_elements The count of T's for which to allocate space
677 * The initial underlying memory layout is a bit complicated...
679 template<typename _Tp
, typename _Alloc
>
681 _Deque_base
<_Tp
, _Alloc
>::
682 _M_initialize_map(size_t __num_elements
)
684 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
687 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
688 size_t(__num_nodes
+ 2));
689 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
691 // For "small" maps (needing less than _M_map_size nodes), allocation
692 // starts in the middle elements and grows outwards. So nstart may be
693 // the beginning of _M_map, but for small maps it may be as far in as
696 _Map_pointer __nstart
= (this->_M_impl
._M_map
697 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
698 _Map_pointer __nfinish
= __nstart
+ __num_nodes
;
701 { _M_create_nodes(__nstart
, __nfinish
); }
704 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
705 this->_M_impl
._M_map
= _Map_pointer();
706 this->_M_impl
._M_map_size
= 0;
707 __throw_exception_again
;
710 this->_M_impl
._M_start
._M_set_node(__nstart
);
711 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
712 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
713 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
715 % __deque_buf_size(sizeof(_Tp
)));
718 template<typename _Tp
, typename _Alloc
>
720 _Deque_base
<_Tp
, _Alloc
>::
721 _M_create_nodes(_Map_pointer __nstart
, _Map_pointer __nfinish
)
726 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
727 *__cur
= this->_M_allocate_node();
731 _M_destroy_nodes(__nstart
, __cur
);
732 __throw_exception_again
;
736 template<typename _Tp
, typename _Alloc
>
738 _Deque_base
<_Tp
, _Alloc
>::
739 _M_destroy_nodes(_Map_pointer __nstart
,
740 _Map_pointer __nfinish
) _GLIBCXX_NOEXCEPT
742 for (_Map_pointer __n
= __nstart
; __n
< __nfinish
; ++__n
)
743 _M_deallocate_node(*__n
);
747 * @brief A standard container using fixed-size memory allocation and
748 * constant-time manipulation of elements at either end.
752 * @tparam _Tp Type of element.
753 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
755 * Meets the requirements of a <a href="tables.html#65">container</a>, a
756 * <a href="tables.html#66">reversible container</a>, and a
757 * <a href="tables.html#67">sequence</a>, including the
758 * <a href="tables.html#68">optional sequence requirements</a>.
760 * In previous HP/SGI versions of deque, there was an extra template
761 * parameter so users could control the node size. This extension turned
762 * out to violate the C++ standard (it can be detected using template
763 * template parameters), and it was removed.
765 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
768 * - size_t _M_map_size
769 * - iterator _M_start, _M_finish
771 * map_size is at least 8. %map is an array of map_size
772 * pointers-to-@a nodes. (The name %map has nothing to do with the
773 * std::map class, and @b nodes should not be confused with
774 * std::list's usage of @a node.)
776 * A @a node has no specific type name as such, but it is referred
777 * to as @a node in this file. It is a simple array-of-Tp. If Tp
778 * is very large, there will be one Tp element per node (i.e., an
779 * @a array of one). For non-huge Tp's, node size is inversely
780 * related to Tp size: the larger the Tp, the fewer Tp's will fit
781 * in a node. The goal here is to keep the total size of a node
782 * relatively small and constant over different Tp's, to improve
783 * allocator efficiency.
785 * Not every pointer in the %map array will point to a node. If
786 * the initial number of elements in the deque is small, the
787 * /middle/ %map pointers will be valid, and the ones at the edges
788 * will be unused. This same situation will arise as the %map
789 * grows: available %map pointers, if any, will be on the ends. As
790 * new nodes are created, only a subset of the %map's pointers need
791 * to be copied @a outward.
794 * - For any nonsingular iterator i:
795 * - i.node points to a member of the %map array. (Yes, you read that
796 * correctly: i.node does not actually point to a node.) The member of
797 * the %map array is what actually points to the node.
798 * - i.first == *(i.node) (This points to the node (first Tp element).)
799 * - i.last == i.first + node_size
800 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
801 * the implication of this is that i.cur is always a dereferenceable
802 * pointer, even if i is a past-the-end iterator.
803 * - Start and Finish are always nonsingular iterators. NOTE: this
804 * means that an empty deque must have one node, a deque with <N
805 * elements (where N is the node buffer size) must have one node, a
806 * deque with N through (2N-1) elements must have two nodes, etc.
807 * - For every node other than start.node and finish.node, every
808 * element in the node is an initialized object. If start.node ==
809 * finish.node, then [start.cur, finish.cur) are initialized
810 * objects, and the elements outside that range are uninitialized
811 * storage. Otherwise, [start.cur, start.last) and [finish.first,
812 * finish.cur) are initialized objects, and [start.first, start.cur)
813 * and [finish.cur, finish.last) are uninitialized storage.
814 * - [%map, %map + map_size) is a valid, non-empty range.
815 * - [start.node, finish.node] is a valid range contained within
816 * [%map, %map + map_size).
817 * - A pointer in the range [%map, %map + map_size) points to an allocated
818 * node if and only if the pointer is in the range
819 * [start.node, finish.node].
821 * Here's the magic: nothing in deque is @b aware of the discontiguous
824 * The memory setup and layout occurs in the parent, _Base, and the iterator
825 * class is entirely responsible for @a leaping from one node to the next.
826 * All the implementation routines for deque itself work only through the
827 * start and finish iterators. This keeps the routines simple and sane,
828 * and we can use other standard algorithms as well.
830 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
831 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
833 // concept requirements
834 typedef typename
_Alloc::value_type _Alloc_value_type
;
835 #if __cplusplus < 201103L
836 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
838 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
840 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
841 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
842 typedef typename
_Base::_Alloc_traits _Alloc_traits
;
843 typedef typename
_Base::_Map_pointer _Map_pointer
;
846 typedef _Tp value_type
;
847 typedef typename
_Alloc_traits::pointer pointer
;
848 typedef typename
_Alloc_traits::const_pointer const_pointer
;
849 typedef typename
_Alloc_traits::reference reference
;
850 typedef typename
_Alloc_traits::const_reference const_reference
;
851 typedef typename
_Base::iterator iterator
;
852 typedef typename
_Base::const_iterator const_iterator
;
853 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
854 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
855 typedef size_t size_type
;
856 typedef ptrdiff_t difference_type
;
857 typedef _Alloc allocator_type
;
860 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
861 { return __deque_buf_size(sizeof(_Tp
)); }
863 // Functions controlling memory layout, and nothing else.
864 using _Base::_M_initialize_map
;
865 using _Base::_M_create_nodes
;
866 using _Base::_M_destroy_nodes
;
867 using _Base::_M_allocate_node
;
868 using _Base::_M_deallocate_node
;
869 using _Base::_M_allocate_map
;
870 using _Base::_M_deallocate_map
;
871 using _Base::_M_get_Tp_allocator
;
874 * A total of four data members accumulated down the hierarchy.
875 * May be accessed via _M_impl.*
877 using _Base::_M_impl
;
880 // [23.2.1.1] construct/copy/destroy
881 // (assign() and get_allocator() are also listed in this section)
884 * @brief Creates a %deque with no elements.
886 deque() : _Base() { }
889 * @brief Creates a %deque with no elements.
890 * @param __a An allocator object.
893 deque(const allocator_type
& __a
)
896 #if __cplusplus >= 201103L
898 * @brief Creates a %deque with default constructed elements.
899 * @param __n The number of elements to initially create.
900 * @param __a An allocator.
902 * This constructor fills the %deque with @a n default
903 * constructed elements.
906 deque(size_type __n
, const allocator_type
& __a
= allocator_type())
908 { _M_default_initialize(); }
911 * @brief Creates a %deque with copies of an exemplar element.
912 * @param __n The number of elements to initially create.
913 * @param __value An element to copy.
914 * @param __a An allocator.
916 * This constructor fills the %deque with @a __n copies of @a __value.
918 deque(size_type __n
, const value_type
& __value
,
919 const allocator_type
& __a
= allocator_type())
921 { _M_fill_initialize(__value
); }
924 * @brief Creates a %deque with copies of an exemplar element.
925 * @param __n The number of elements to initially create.
926 * @param __value An element to copy.
927 * @param __a An allocator.
929 * This constructor fills the %deque with @a __n copies of @a __value.
932 deque(size_type __n
, const value_type
& __value
= value_type(),
933 const allocator_type
& __a
= allocator_type())
935 { _M_fill_initialize(__value
); }
939 * @brief %Deque copy constructor.
940 * @param __x A %deque of identical element and allocator types.
942 * The newly-created %deque uses a copy of the allocator object used
943 * by @a __x (unless the allocator traits dictate a different object).
945 deque(const deque
& __x
)
946 : _Base(_Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()),
948 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
949 this->_M_impl
._M_start
,
950 _M_get_Tp_allocator()); }
952 #if __cplusplus >= 201103L
954 * @brief %Deque move constructor.
955 * @param __x A %deque of identical element and allocator types.
957 * The newly-created %deque contains the exact contents of @a __x.
958 * The contents of @a __x are a valid, but unspecified %deque.
961 : _Base(std::move(__x
)) { }
963 /// Copy constructor with alternative allocator
964 deque(const deque
& __x
, const allocator_type
& __a
)
965 : _Base(__a
, __x
.size())
966 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
967 this->_M_impl
._M_start
,
968 _M_get_Tp_allocator()); }
970 /// Move constructor with alternative allocator
971 deque(deque
&& __x
, const allocator_type
& __a
)
972 : _Base(std::move(__x
), __a
, __x
.size())
974 if (__x
.get_allocator() != __a
)
976 std::__uninitialized_move_a(__x
.begin(), __x
.end(),
977 this->_M_impl
._M_start
,
978 _M_get_Tp_allocator());
984 * @brief Builds a %deque from an initializer list.
985 * @param __l An initializer_list.
986 * @param __a An allocator object.
988 * Create a %deque consisting of copies of the elements in the
989 * initializer_list @a __l.
991 * This will call the element type's copy constructor N times
992 * (where N is __l.size()) and do no memory reallocation.
994 deque(initializer_list
<value_type
> __l
,
995 const allocator_type
& __a
= allocator_type())
998 _M_range_initialize(__l
.begin(), __l
.end(),
999 random_access_iterator_tag());
1004 * @brief Builds a %deque from a range.
1005 * @param __first An input iterator.
1006 * @param __last An input iterator.
1007 * @param __a An allocator object.
1009 * Create a %deque consisting of copies of the elements from [__first,
1012 * If the iterators are forward, bidirectional, or random-access, then
1013 * this will call the elements' copy constructor N times (where N is
1014 * distance(__first,__last)) and do no memory reallocation. But if only
1015 * input iterators are used, then this will do at most 2N calls to the
1016 * copy constructor, and logN memory reallocations.
1018 #if __cplusplus >= 201103L
1019 template<typename _InputIterator
,
1020 typename
= std::_RequireInputIter
<_InputIterator
>>
1021 deque(_InputIterator __first
, _InputIterator __last
,
1022 const allocator_type
& __a
= allocator_type())
1024 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
1026 template<typename _InputIterator
>
1027 deque(_InputIterator __first
, _InputIterator __last
,
1028 const allocator_type
& __a
= allocator_type())
1031 // Check whether it's an integral type. If so, it's not an iterator.
1032 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1033 _M_initialize_dispatch(__first
, __last
, _Integral());
1038 * The dtor only erases the elements, and note that if the elements
1039 * themselves are pointers, the pointed-to memory is not touched in any
1040 * way. Managing the pointer is the user's responsibility.
1043 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
1046 * @brief %Deque assignment operator.
1047 * @param __x A %deque of identical element and allocator types.
1049 * All the elements of @a x are copied.
1051 * The newly-created %deque uses a copy of the allocator object used
1052 * by @a __x (unless the allocator traits dictate a different object).
1055 operator=(const deque
& __x
);
1057 #if __cplusplus >= 201103L
1059 * @brief %Deque move assignment operator.
1060 * @param __x A %deque of identical element and allocator types.
1062 * The contents of @a __x are moved into this deque (without copying,
1063 * if the allocators permit it).
1064 * @a __x is a valid, but unspecified %deque.
1067 operator=(deque
&& __x
) noexcept(_Alloc_traits::_S_always_equal())
1069 using __always_equal
= typename
_Alloc_traits::is_always_equal
;
1070 _M_move_assign1(std::move(__x
), __always_equal
{});
1075 * @brief Assigns an initializer list to a %deque.
1076 * @param __l An initializer_list.
1078 * This function fills a %deque with copies of the elements in the
1079 * initializer_list @a __l.
1081 * Note that the assignment completely changes the %deque and that the
1082 * resulting %deque's size is the same as the number of elements
1086 operator=(initializer_list
<value_type
> __l
)
1088 _M_assign_aux(__l
.begin(), __l
.end(),
1089 random_access_iterator_tag());
1095 * @brief Assigns a given value to a %deque.
1096 * @param __n Number of elements to be assigned.
1097 * @param __val Value to be assigned.
1099 * This function fills a %deque with @a n copies of the given
1100 * value. Note that the assignment completely changes the
1101 * %deque and that the resulting %deque's size is the same as
1102 * the number of elements assigned.
1105 assign(size_type __n
, const value_type
& __val
)
1106 { _M_fill_assign(__n
, __val
); }
1109 * @brief Assigns a range to a %deque.
1110 * @param __first An input iterator.
1111 * @param __last An input iterator.
1113 * This function fills a %deque with copies of the elements in the
1114 * range [__first,__last).
1116 * Note that the assignment completely changes the %deque and that the
1117 * resulting %deque's size is the same as the number of elements
1120 #if __cplusplus >= 201103L
1121 template<typename _InputIterator
,
1122 typename
= std::_RequireInputIter
<_InputIterator
>>
1124 assign(_InputIterator __first
, _InputIterator __last
)
1125 { _M_assign_dispatch(__first
, __last
, __false_type()); }
1127 template<typename _InputIterator
>
1129 assign(_InputIterator __first
, _InputIterator __last
)
1131 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1132 _M_assign_dispatch(__first
, __last
, _Integral());
1136 #if __cplusplus >= 201103L
1138 * @brief Assigns an initializer list to a %deque.
1139 * @param __l An initializer_list.
1141 * This function fills a %deque with copies of the elements in the
1142 * initializer_list @a __l.
1144 * Note that the assignment completely changes the %deque and that the
1145 * resulting %deque's size is the same as the number of elements
1149 assign(initializer_list
<value_type
> __l
)
1150 { _M_assign_aux(__l
.begin(), __l
.end(), random_access_iterator_tag()); }
1153 /// Get a copy of the memory allocation object.
1155 get_allocator() const _GLIBCXX_NOEXCEPT
1156 { return _Base::get_allocator(); }
1160 * Returns a read/write iterator that points to the first element in the
1161 * %deque. Iteration is done in ordinary element order.
1164 begin() _GLIBCXX_NOEXCEPT
1165 { return this->_M_impl
._M_start
; }
1168 * Returns a read-only (constant) iterator that points to the first
1169 * element in the %deque. Iteration is done in ordinary element order.
1172 begin() const _GLIBCXX_NOEXCEPT
1173 { return this->_M_impl
._M_start
; }
1176 * Returns a read/write iterator that points one past the last
1177 * element in the %deque. Iteration is done in ordinary
1181 end() _GLIBCXX_NOEXCEPT
1182 { return this->_M_impl
._M_finish
; }
1185 * Returns a read-only (constant) iterator that points one past
1186 * the last element in the %deque. Iteration is done in
1187 * ordinary element order.
1190 end() const _GLIBCXX_NOEXCEPT
1191 { return this->_M_impl
._M_finish
; }
1194 * Returns a read/write reverse iterator that points to the
1195 * last element in the %deque. Iteration is done in reverse
1199 rbegin() _GLIBCXX_NOEXCEPT
1200 { return reverse_iterator(this->_M_impl
._M_finish
); }
1203 * Returns a read-only (constant) reverse iterator that points
1204 * to the last element in the %deque. Iteration is done in
1205 * reverse element order.
1207 const_reverse_iterator
1208 rbegin() const _GLIBCXX_NOEXCEPT
1209 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1212 * Returns a read/write reverse iterator that points to one
1213 * before the first element in the %deque. Iteration is done
1214 * in reverse element order.
1217 rend() _GLIBCXX_NOEXCEPT
1218 { return reverse_iterator(this->_M_impl
._M_start
); }
1221 * Returns a read-only (constant) reverse iterator that points
1222 * to one before the first element in the %deque. Iteration is
1223 * done in reverse element order.
1225 const_reverse_iterator
1226 rend() const _GLIBCXX_NOEXCEPT
1227 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1229 #if __cplusplus >= 201103L
1231 * Returns a read-only (constant) iterator that points to the first
1232 * element in the %deque. Iteration is done in ordinary element order.
1235 cbegin() const noexcept
1236 { return this->_M_impl
._M_start
; }
1239 * Returns a read-only (constant) iterator that points one past
1240 * the last element in the %deque. Iteration is done in
1241 * ordinary element order.
1244 cend() const noexcept
1245 { return this->_M_impl
._M_finish
; }
1248 * Returns a read-only (constant) reverse iterator that points
1249 * to the last element in the %deque. Iteration is done in
1250 * reverse element order.
1252 const_reverse_iterator
1253 crbegin() const noexcept
1254 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1257 * Returns a read-only (constant) reverse iterator that points
1258 * to one before the first element in the %deque. Iteration is
1259 * done in reverse element order.
1261 const_reverse_iterator
1262 crend() const noexcept
1263 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1266 // [23.2.1.2] capacity
1267 /** Returns the number of elements in the %deque. */
1269 size() const _GLIBCXX_NOEXCEPT
1270 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1272 /** Returns the size() of the largest possible %deque. */
1274 max_size() const _GLIBCXX_NOEXCEPT
1275 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
1277 #if __cplusplus >= 201103L
1279 * @brief Resizes the %deque to the specified number of elements.
1280 * @param __new_size Number of elements the %deque should contain.
1282 * This function will %resize the %deque to the specified
1283 * number of elements. If the number is smaller than the
1284 * %deque's current size the %deque is truncated, otherwise
1285 * default constructed elements are appended.
1288 resize(size_type __new_size
)
1290 const size_type __len
= size();
1291 if (__new_size
> __len
)
1292 _M_default_append(__new_size
- __len
);
1293 else if (__new_size
< __len
)
1294 _M_erase_at_end(this->_M_impl
._M_start
1295 + difference_type(__new_size
));
1299 * @brief Resizes the %deque to the specified number of elements.
1300 * @param __new_size Number of elements the %deque should contain.
1301 * @param __x Data with which new elements should be populated.
1303 * This function will %resize the %deque to the specified
1304 * number of elements. If the number is smaller than the
1305 * %deque's current size the %deque is truncated, otherwise the
1306 * %deque is extended and new elements are populated with given
1310 resize(size_type __new_size
, const value_type
& __x
)
1312 const size_type __len
= size();
1313 if (__new_size
> __len
)
1314 _M_fill_insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1315 else if (__new_size
< __len
)
1316 _M_erase_at_end(this->_M_impl
._M_start
1317 + difference_type(__new_size
));
1321 * @brief Resizes the %deque to the specified number of elements.
1322 * @param __new_size Number of elements the %deque should contain.
1323 * @param __x Data with which new elements should be populated.
1325 * This function will %resize the %deque to the specified
1326 * number of elements. If the number is smaller than the
1327 * %deque's current size the %deque is truncated, otherwise the
1328 * %deque is extended and new elements are populated with given
1332 resize(size_type __new_size
, value_type __x
= value_type())
1334 const size_type __len
= size();
1335 if (__new_size
> __len
)
1336 _M_fill_insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1337 else if (__new_size
< __len
)
1338 _M_erase_at_end(this->_M_impl
._M_start
1339 + difference_type(__new_size
));
1343 #if __cplusplus >= 201103L
1344 /** A non-binding request to reduce memory use. */
1346 shrink_to_fit() noexcept
1347 { _M_shrink_to_fit(); }
1351 * Returns true if the %deque is empty. (Thus begin() would
1355 empty() const _GLIBCXX_NOEXCEPT
1356 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1360 * @brief Subscript access to the data contained in the %deque.
1361 * @param __n The index of the element for which data should be
1363 * @return Read/write reference to data.
1365 * This operator allows for easy, array-style, data access.
1366 * Note that data access with this operator is unchecked and
1367 * out_of_range lookups are not defined. (For checked lookups
1371 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1373 __glibcxx_requires_subscript(__n
);
1374 return this->_M_impl
._M_start
[difference_type(__n
)];
1378 * @brief Subscript access to the data contained in the %deque.
1379 * @param __n The index of the element for which data should be
1381 * @return Read-only (constant) reference to data.
1383 * This operator allows for easy, array-style, data access.
1384 * Note that data access with this operator is unchecked and
1385 * out_of_range lookups are not defined. (For checked lookups
1389 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1391 __glibcxx_requires_subscript(__n
);
1392 return this->_M_impl
._M_start
[difference_type(__n
)];
1396 /// Safety check used only from at().
1398 _M_range_check(size_type __n
) const
1400 if (__n
>= this->size())
1401 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1402 "(which is %zu)>= this->size() "
1409 * @brief Provides access to the data contained in the %deque.
1410 * @param __n The index of the element for which data should be
1412 * @return Read/write reference to data.
1413 * @throw std::out_of_range If @a __n is an invalid index.
1415 * This function provides for safer data access. The parameter
1416 * is first checked that it is in the range of the deque. The
1417 * function throws out_of_range if the check fails.
1422 _M_range_check(__n
);
1423 return (*this)[__n
];
1427 * @brief Provides access to the data contained in the %deque.
1428 * @param __n The index of the element for which data should be
1430 * @return Read-only (constant) reference to data.
1431 * @throw std::out_of_range If @a __n is an invalid index.
1433 * This function provides for safer data access. The parameter is first
1434 * checked that it is in the range of the deque. The function throws
1435 * out_of_range if the check fails.
1438 at(size_type __n
) const
1440 _M_range_check(__n
);
1441 return (*this)[__n
];
1445 * Returns a read/write reference to the data at the first
1446 * element of the %deque.
1449 front() _GLIBCXX_NOEXCEPT
1451 __glibcxx_requires_nonempty();
1456 * Returns a read-only (constant) reference to the data at the first
1457 * element of the %deque.
1460 front() const _GLIBCXX_NOEXCEPT
1462 __glibcxx_requires_nonempty();
1467 * Returns a read/write reference to the data at the last element of the
1471 back() _GLIBCXX_NOEXCEPT
1473 __glibcxx_requires_nonempty();
1474 iterator __tmp
= end();
1480 * Returns a read-only (constant) reference to the data at the last
1481 * element of the %deque.
1484 back() const _GLIBCXX_NOEXCEPT
1486 __glibcxx_requires_nonempty();
1487 const_iterator __tmp
= end();
1492 // [23.2.1.2] modifiers
1494 * @brief Add data to the front of the %deque.
1495 * @param __x Data to be added.
1497 * This is a typical stack operation. The function creates an
1498 * element at the front of the %deque and assigns the given
1499 * data to it. Due to the nature of a %deque this operation
1500 * can be done in constant time.
1503 push_front(const value_type
& __x
)
1505 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1507 _Alloc_traits::construct(this->_M_impl
,
1508 this->_M_impl
._M_start
._M_cur
- 1,
1510 --this->_M_impl
._M_start
._M_cur
;
1513 _M_push_front_aux(__x
);
1516 #if __cplusplus >= 201103L
1518 push_front(value_type
&& __x
)
1519 { emplace_front(std::move(__x
)); }
1521 template<typename
... _Args
>
1523 emplace_front(_Args
&&... __args
);
1527 * @brief Add data to the end of the %deque.
1528 * @param __x Data to be added.
1530 * This is a typical stack operation. The function creates an
1531 * element at the end of the %deque and assigns the given data
1532 * to it. Due to the nature of a %deque this operation can be
1533 * done in constant time.
1536 push_back(const value_type
& __x
)
1538 if (this->_M_impl
._M_finish
._M_cur
1539 != this->_M_impl
._M_finish
._M_last
- 1)
1541 _Alloc_traits::construct(this->_M_impl
,
1542 this->_M_impl
._M_finish
._M_cur
, __x
);
1543 ++this->_M_impl
._M_finish
._M_cur
;
1546 _M_push_back_aux(__x
);
1549 #if __cplusplus >= 201103L
1551 push_back(value_type
&& __x
)
1552 { emplace_back(std::move(__x
)); }
1554 template<typename
... _Args
>
1556 emplace_back(_Args
&&... __args
);
1560 * @brief Removes first element.
1562 * This is a typical stack operation. It shrinks the %deque by one.
1564 * Note that no data is returned, and if the first element's data is
1565 * needed, it should be retrieved before pop_front() is called.
1568 pop_front() _GLIBCXX_NOEXCEPT
1570 __glibcxx_requires_nonempty();
1571 if (this->_M_impl
._M_start
._M_cur
1572 != this->_M_impl
._M_start
._M_last
- 1)
1574 _Alloc_traits::destroy(this->_M_impl
,
1575 this->_M_impl
._M_start
._M_cur
);
1576 ++this->_M_impl
._M_start
._M_cur
;
1583 * @brief Removes last element.
1585 * This is a typical stack operation. It shrinks the %deque by one.
1587 * Note that no data is returned, and if the last element's data is
1588 * needed, it should be retrieved before pop_back() is called.
1591 pop_back() _GLIBCXX_NOEXCEPT
1593 __glibcxx_requires_nonempty();
1594 if (this->_M_impl
._M_finish
._M_cur
1595 != this->_M_impl
._M_finish
._M_first
)
1597 --this->_M_impl
._M_finish
._M_cur
;
1598 _Alloc_traits::destroy(this->_M_impl
,
1599 this->_M_impl
._M_finish
._M_cur
);
1605 #if __cplusplus >= 201103L
1607 * @brief Inserts an object in %deque before specified iterator.
1608 * @param __position A const_iterator into the %deque.
1609 * @param __args Arguments.
1610 * @return An iterator that points to the inserted data.
1612 * This function will insert an object of type T constructed
1613 * with T(std::forward<Args>(args)...) before the specified location.
1615 template<typename
... _Args
>
1617 emplace(const_iterator __position
, _Args
&&... __args
);
1620 * @brief Inserts given value into %deque before specified iterator.
1621 * @param __position A const_iterator into the %deque.
1622 * @param __x Data to be inserted.
1623 * @return An iterator that points to the inserted data.
1625 * This function will insert a copy of the given value before the
1626 * specified location.
1629 insert(const_iterator __position
, const value_type
& __x
);
1632 * @brief Inserts given value into %deque before specified iterator.
1633 * @param __position An iterator into the %deque.
1634 * @param __x Data to be inserted.
1635 * @return An iterator that points to the inserted data.
1637 * This function will insert a copy of the given value before the
1638 * specified location.
1641 insert(iterator __position
, const value_type
& __x
);
1644 #if __cplusplus >= 201103L
1646 * @brief Inserts given rvalue into %deque before specified iterator.
1647 * @param __position A const_iterator into the %deque.
1648 * @param __x Data to be inserted.
1649 * @return An iterator that points to the inserted data.
1651 * This function will insert a copy of the given rvalue before the
1652 * specified location.
1655 insert(const_iterator __position
, value_type
&& __x
)
1656 { return emplace(__position
, std::move(__x
)); }
1659 * @brief Inserts an initializer list into the %deque.
1660 * @param __p An iterator into the %deque.
1661 * @param __l An initializer_list.
1663 * This function will insert copies of the data in the
1664 * initializer_list @a __l into the %deque before the location
1665 * specified by @a __p. This is known as <em>list insert</em>.
1668 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1670 auto __offset
= __p
- cbegin();
1671 _M_range_insert_aux(__p
._M_const_cast(), __l
.begin(), __l
.end(),
1672 std::random_access_iterator_tag());
1673 return begin() + __offset
;
1677 #if __cplusplus >= 201103L
1679 * @brief Inserts a number of copies of given data into the %deque.
1680 * @param __position A const_iterator into the %deque.
1681 * @param __n Number of elements to be inserted.
1682 * @param __x Data to be inserted.
1683 * @return An iterator that points to the inserted data.
1685 * This function will insert a specified number of copies of the given
1686 * data before the location specified by @a __position.
1689 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1691 difference_type __offset
= __position
- cbegin();
1692 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1693 return begin() + __offset
;
1697 * @brief Inserts a number of copies of given data into the %deque.
1698 * @param __position An iterator into the %deque.
1699 * @param __n Number of elements to be inserted.
1700 * @param __x Data to be inserted.
1702 * This function will insert a specified number of copies of the given
1703 * data before the location specified by @a __position.
1706 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1707 { _M_fill_insert(__position
, __n
, __x
); }
1710 #if __cplusplus >= 201103L
1712 * @brief Inserts a range into the %deque.
1713 * @param __position A const_iterator into the %deque.
1714 * @param __first An input iterator.
1715 * @param __last An input iterator.
1716 * @return An iterator that points to the inserted data.
1718 * This function will insert copies of the data in the range
1719 * [__first,__last) into the %deque before the location specified
1720 * by @a __position. This is known as <em>range insert</em>.
1722 template<typename _InputIterator
,
1723 typename
= std::_RequireInputIter
<_InputIterator
>>
1725 insert(const_iterator __position
, _InputIterator __first
,
1726 _InputIterator __last
)
1728 difference_type __offset
= __position
- cbegin();
1729 _M_insert_dispatch(__position
._M_const_cast(),
1730 __first
, __last
, __false_type());
1731 return begin() + __offset
;
1735 * @brief Inserts a range into the %deque.
1736 * @param __position An iterator into the %deque.
1737 * @param __first An input iterator.
1738 * @param __last An input iterator.
1740 * This function will insert copies of the data in the range
1741 * [__first,__last) into the %deque before the location specified
1742 * by @a __position. This is known as <em>range insert</em>.
1744 template<typename _InputIterator
>
1746 insert(iterator __position
, _InputIterator __first
,
1747 _InputIterator __last
)
1749 // Check whether it's an integral type. If so, it's not an iterator.
1750 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1751 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1756 * @brief Remove element at given position.
1757 * @param __position Iterator pointing to element to be erased.
1758 * @return An iterator pointing to the next element (or end()).
1760 * This function will erase the element at the given position and thus
1761 * shorten the %deque by one.
1763 * The user is cautioned that
1764 * this function only erases the element, and that if the element is
1765 * itself a pointer, the pointed-to memory is not touched in any way.
1766 * Managing the pointer is the user's responsibility.
1769 #if __cplusplus >= 201103L
1770 erase(const_iterator __position
)
1772 erase(iterator __position
)
1774 { return _M_erase(__position
._M_const_cast()); }
1777 * @brief Remove a range of elements.
1778 * @param __first Iterator pointing to the first element to be erased.
1779 * @param __last Iterator pointing to one past the last element to be
1781 * @return An iterator pointing to the element pointed to by @a last
1782 * prior to erasing (or end()).
1784 * This function will erase the elements in the range
1785 * [__first,__last) and shorten the %deque accordingly.
1787 * The user is cautioned that
1788 * this function only erases the elements, and that if the elements
1789 * themselves are pointers, the pointed-to memory is not touched in any
1790 * way. Managing the pointer is the user's responsibility.
1793 #if __cplusplus >= 201103L
1794 erase(const_iterator __first
, const_iterator __last
)
1796 erase(iterator __first
, iterator __last
)
1798 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1801 * @brief Swaps data with another %deque.
1802 * @param __x A %deque of the same element and allocator types.
1804 * This exchanges the elements between two deques in constant time.
1805 * (Four pointers, so it should be quite fast.)
1806 * Note that the global std::swap() function is specialized such that
1807 * std::swap(d1,d2) will feed to this function.
1809 * Whether the allocators are swapped depends on the allocator traits.
1812 swap(deque
& __x
) _GLIBCXX_NOEXCEPT
1814 #if __cplusplus >= 201103L
1815 __glibcxx_assert(_Alloc_traits::propagate_on_container_swap::value
1816 || _M_get_Tp_allocator() == __x
._M_get_Tp_allocator());
1818 _M_impl
._M_swap_data(__x
._M_impl
);
1819 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1820 __x
._M_get_Tp_allocator());
1824 * Erases all the elements. Note that this function only erases the
1825 * elements, and that if the elements themselves are pointers, the
1826 * pointed-to memory is not touched in any way. Managing the pointer is
1827 * the user's responsibility.
1830 clear() _GLIBCXX_NOEXCEPT
1831 { _M_erase_at_end(begin()); }
1834 // Internal constructor functions follow.
1836 // called by the range constructor to implement [23.1.1]/9
1838 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1839 // 438. Ambiguity in the "do the right thing" clause
1840 template<typename _Integer
>
1842 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1844 _M_initialize_map(static_cast<size_type
>(__n
));
1845 _M_fill_initialize(__x
);
1848 // called by the range constructor to implement [23.1.1]/9
1849 template<typename _InputIterator
>
1851 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1854 _M_range_initialize(__first
, __last
,
1855 std::__iterator_category(__first
));
1858 // called by the second initialize_dispatch above
1861 * @brief Fills the deque with whatever is in [first,last).
1862 * @param __first An input iterator.
1863 * @param __last An input iterator.
1866 * If the iterators are actually forward iterators (or better), then the
1867 * memory layout can be done all at once. Else we move forward using
1868 * push_back on each value from the iterator.
1870 template<typename _InputIterator
>
1872 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1873 std::input_iterator_tag
);
1875 // called by the second initialize_dispatch above
1876 template<typename _ForwardIterator
>
1878 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1879 std::forward_iterator_tag
);
1883 * @brief Fills the %deque with copies of value.
1884 * @param __value Initial value.
1886 * @pre _M_start and _M_finish have already been initialized,
1887 * but none of the %deque's elements have yet been constructed.
1889 * This function is called only when the user provides an explicit size
1890 * (with or without an explicit exemplar value).
1893 _M_fill_initialize(const value_type
& __value
);
1895 #if __cplusplus >= 201103L
1896 // called by deque(n).
1898 _M_default_initialize();
1901 // Internal assign functions follow. The *_aux functions do the actual
1902 // assignment work for the range versions.
1904 // called by the range assign to implement [23.1.1]/9
1906 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1907 // 438. Ambiguity in the "do the right thing" clause
1908 template<typename _Integer
>
1910 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1911 { _M_fill_assign(__n
, __val
); }
1913 // called by the range assign to implement [23.1.1]/9
1914 template<typename _InputIterator
>
1916 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1918 { _M_assign_aux(__first
, __last
, std::__iterator_category(__first
)); }
1920 // called by the second assign_dispatch above
1921 template<typename _InputIterator
>
1923 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1924 std::input_iterator_tag
);
1926 // called by the second assign_dispatch above
1927 template<typename _ForwardIterator
>
1929 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1930 std::forward_iterator_tag
)
1932 const size_type __len
= std::distance(__first
, __last
);
1935 _ForwardIterator __mid
= __first
;
1936 std::advance(__mid
, size());
1937 std::copy(__first
, __mid
, begin());
1938 _M_range_insert_aux(end(), __mid
, __last
,
1939 std::__iterator_category(__first
));
1942 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1945 // Called by assign(n,t), and the range assign when it turns out
1946 // to be the same thing.
1948 _M_fill_assign(size_type __n
, const value_type
& __val
)
1952 std::fill(begin(), end(), __val
);
1953 _M_fill_insert(end(), __n
- size(), __val
);
1957 _M_erase_at_end(begin() + difference_type(__n
));
1958 std::fill(begin(), end(), __val
);
1963 /// Helper functions for push_* and pop_*.
1964 #if __cplusplus < 201103L
1965 void _M_push_back_aux(const value_type
&);
1967 void _M_push_front_aux(const value_type
&);
1969 template<typename
... _Args
>
1970 void _M_push_back_aux(_Args
&&... __args
);
1972 template<typename
... _Args
>
1973 void _M_push_front_aux(_Args
&&... __args
);
1976 void _M_pop_back_aux();
1978 void _M_pop_front_aux();
1981 // Internal insert functions follow. The *_aux functions do the actual
1982 // insertion work when all shortcuts fail.
1984 // called by the range insert to implement [23.1.1]/9
1986 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1987 // 438. Ambiguity in the "do the right thing" clause
1988 template<typename _Integer
>
1990 _M_insert_dispatch(iterator __pos
,
1991 _Integer __n
, _Integer __x
, __true_type
)
1992 { _M_fill_insert(__pos
, __n
, __x
); }
1994 // called by the range insert to implement [23.1.1]/9
1995 template<typename _InputIterator
>
1997 _M_insert_dispatch(iterator __pos
,
1998 _InputIterator __first
, _InputIterator __last
,
2001 _M_range_insert_aux(__pos
, __first
, __last
,
2002 std::__iterator_category(__first
));
2005 // called by the second insert_dispatch above
2006 template<typename _InputIterator
>
2008 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
2009 _InputIterator __last
, std::input_iterator_tag
);
2011 // called by the second insert_dispatch above
2012 template<typename _ForwardIterator
>
2014 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
2015 _ForwardIterator __last
, std::forward_iterator_tag
);
2017 // Called by insert(p,n,x), and the range insert when it turns out to be
2018 // the same thing. Can use fill functions in optimal situations,
2019 // otherwise passes off to insert_aux(p,n,x).
2021 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
2023 // called by insert(p,x)
2024 #if __cplusplus < 201103L
2026 _M_insert_aux(iterator __pos
, const value_type
& __x
);
2028 template<typename
... _Args
>
2030 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
2033 // called by insert(p,n,x) via fill_insert
2035 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
2037 // called by range_insert_aux for forward iterators
2038 template<typename _ForwardIterator
>
2040 _M_insert_aux(iterator __pos
,
2041 _ForwardIterator __first
, _ForwardIterator __last
,
2045 // Internal erase functions follow.
2048 _M_destroy_data_aux(iterator __first
, iterator __last
);
2050 // Called by ~deque().
2051 // NB: Doesn't deallocate the nodes.
2052 template<typename _Alloc1
>
2054 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
2055 { _M_destroy_data_aux(__first
, __last
); }
2058 _M_destroy_data(iterator __first
, iterator __last
,
2059 const std::allocator
<_Tp
>&)
2061 if (!__has_trivial_destructor(value_type
))
2062 _M_destroy_data_aux(__first
, __last
);
2065 // Called by erase(q1, q2).
2067 _M_erase_at_begin(iterator __pos
)
2069 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
2070 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
2071 this->_M_impl
._M_start
= __pos
;
2074 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2075 // _M_fill_assign, operator=.
2077 _M_erase_at_end(iterator __pos
)
2079 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
2080 _M_destroy_nodes(__pos
._M_node
+ 1,
2081 this->_M_impl
._M_finish
._M_node
+ 1);
2082 this->_M_impl
._M_finish
= __pos
;
2086 _M_erase(iterator __pos
);
2089 _M_erase(iterator __first
, iterator __last
);
2091 #if __cplusplus >= 201103L
2092 // Called by resize(sz).
2094 _M_default_append(size_type __n
);
2101 /// Memory-handling helpers for the previous internal insert functions.
2103 _M_reserve_elements_at_front(size_type __n
)
2105 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
2106 - this->_M_impl
._M_start
._M_first
;
2107 if (__n
> __vacancies
)
2108 _M_new_elements_at_front(__n
- __vacancies
);
2109 return this->_M_impl
._M_start
- difference_type(__n
);
2113 _M_reserve_elements_at_back(size_type __n
)
2115 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
2116 - this->_M_impl
._M_finish
._M_cur
) - 1;
2117 if (__n
> __vacancies
)
2118 _M_new_elements_at_back(__n
- __vacancies
);
2119 return this->_M_impl
._M_finish
+ difference_type(__n
);
2123 _M_new_elements_at_front(size_type __new_elements
);
2126 _M_new_elements_at_back(size_type __new_elements
);
2132 * @brief Memory-handling helpers for the major %map.
2134 * Makes sure the _M_map has space for new nodes. Does not
2135 * actually add the nodes. Can invalidate _M_map pointers.
2136 * (And consequently, %deque iterators.)
2139 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
2141 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
2142 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
2143 _M_reallocate_map(__nodes_to_add
, false);
2147 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
2149 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
2150 - this->_M_impl
._M_map
))
2151 _M_reallocate_map(__nodes_to_add
, true);
2155 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
2158 #if __cplusplus >= 201103L
2159 // Constant-time, nothrow move assignment when source object's memory
2160 // can be moved because the allocators are equal.
2162 _M_move_assign1(deque
&& __x
, /* always equal: */ true_type
) noexcept
2164 this->_M_impl
._M_swap_data(__x
._M_impl
);
2166 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
2169 // When the allocators are not equal the operation could throw, because
2170 // we might need to allocate a new map for __x after moving from it
2171 // or we might need to allocate new elements for *this.
2173 _M_move_assign1(deque
&& __x
, /* always equal: */ false_type
)
2175 constexpr bool __move_storage
=
2176 _Alloc_traits::_S_propagate_on_move_assign();
2177 _M_move_assign2(std::move(__x
), __bool_constant
<__move_storage
>());
2180 // Destroy all elements and deallocate all memory, then replace
2181 // with elements created from __args.
2182 template<typename
... _Args
>
2184 _M_replace_map(_Args
&&... __args
)
2186 // Create new data first, so if allocation fails there are no effects.
2187 deque
__newobj(std::forward
<_Args
>(__args
)...);
2188 // Free existing storage using existing allocator.
2190 _M_deallocate_node(*begin()._M_node
); // one node left after clear()
2191 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
2192 this->_M_impl
._M_map
= nullptr;
2193 this->_M_impl
._M_map_size
= 0;
2194 // Take ownership of replacement memory.
2195 this->_M_impl
._M_swap_data(__newobj
._M_impl
);
2198 // Do move assignment when the allocator propagates.
2200 _M_move_assign2(deque
&& __x
, /* propagate: */ true_type
)
2202 // Make a copy of the original allocator state.
2203 auto __alloc
= __x
._M_get_Tp_allocator();
2204 // The allocator propagates so storage can be moved from __x,
2205 // leaving __x in a valid empty state with a moved-from allocator.
2206 _M_replace_map(std::move(__x
));
2207 // Move the corresponding allocator state too.
2208 _M_get_Tp_allocator() = std::move(__alloc
);
2211 // Do move assignment when it may not be possible to move source
2212 // object's memory, resulting in a linear-time operation.
2214 _M_move_assign2(deque
&& __x
, /* propagate: */ false_type
)
2216 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2218 // The allocators are equal so storage can be moved from __x,
2219 // leaving __x in a valid empty state with its current allocator.
2220 _M_replace_map(std::move(__x
), __x
.get_allocator());
2224 // The rvalue's allocator cannot be moved and is not equal,
2225 // so we need to individually move each element.
2226 _M_assign_aux(std::__make_move_if_noexcept_iterator(__x
.begin()),
2227 std::__make_move_if_noexcept_iterator(__x
.end()),
2228 std::random_access_iterator_tag());
2237 * @brief Deque equality comparison.
2238 * @param __x A %deque.
2239 * @param __y A %deque of the same type as @a __x.
2240 * @return True iff the size and elements of the deques are equal.
2242 * This is an equivalence relation. It is linear in the size of the
2243 * deques. Deques are considered equivalent if their sizes are equal,
2244 * and if corresponding elements compare equal.
2246 template<typename _Tp
, typename _Alloc
>
2248 operator==(const deque
<_Tp
, _Alloc
>& __x
,
2249 const deque
<_Tp
, _Alloc
>& __y
)
2250 { return __x
.size() == __y
.size()
2251 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
2254 * @brief Deque ordering relation.
2255 * @param __x A %deque.
2256 * @param __y A %deque of the same type as @a __x.
2257 * @return True iff @a x is lexicographically less than @a __y.
2259 * This is a total ordering relation. It is linear in the size of the
2260 * deques. The elements must be comparable with @c <.
2262 * See std::lexicographical_compare() for how the determination is made.
2264 template<typename _Tp
, typename _Alloc
>
2266 operator<(const deque
<_Tp
, _Alloc
>& __x
,
2267 const deque
<_Tp
, _Alloc
>& __y
)
2268 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2269 __y
.begin(), __y
.end()); }
2271 /// Based on operator==
2272 template<typename _Tp
, typename _Alloc
>
2274 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
2275 const deque
<_Tp
, _Alloc
>& __y
)
2276 { return !(__x
== __y
); }
2278 /// Based on operator<
2279 template<typename _Tp
, typename _Alloc
>
2281 operator>(const deque
<_Tp
, _Alloc
>& __x
,
2282 const deque
<_Tp
, _Alloc
>& __y
)
2283 { return __y
< __x
; }
2285 /// Based on operator<
2286 template<typename _Tp
, typename _Alloc
>
2288 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
2289 const deque
<_Tp
, _Alloc
>& __y
)
2290 { return !(__y
< __x
); }
2292 /// Based on operator<
2293 template<typename _Tp
, typename _Alloc
>
2295 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
2296 const deque
<_Tp
, _Alloc
>& __y
)
2297 { return !(__x
< __y
); }
2299 /// See std::deque::swap().
2300 template<typename _Tp
, typename _Alloc
>
2302 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
2303 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2306 #undef _GLIBCXX_DEQUE_BUF_SIZE
2308 _GLIBCXX_END_NAMESPACE_CONTAINER
2311 #endif /* _STL_DEQUE_H */