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 namespace std
_GLIBCXX_VISIBILITY(default)
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
71 * @brief This function controls the size of memory nodes.
72 * @param __size The size of an element.
73 * @return The number (not byte size) of elements per node.
75 * This function started off as a compiler kludge from SGI, but
76 * seems to be a useful wrapper around a repeated constant
77 * expression. The @b 512 is tunable (and no other code needs to
78 * change), but no investigation has been done since inheriting the
79 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
80 * you are doing, however: changing it breaks the binary
84 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
85 #define _GLIBCXX_DEQUE_BUF_SIZE 512
88 _GLIBCXX_CONSTEXPR
inline size_t
89 __deque_buf_size(size_t __size
)
90 { return (__size
< _GLIBCXX_DEQUE_BUF_SIZE
91 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE
/ __size
) : size_t(1)); }
95 * @brief A deque::iterator.
97 * Quite a bit of intelligence here. Much of the functionality of
98 * deque is actually passed off to this class. A deque holds two
99 * of these internally, marking its valid range. Access to
100 * elements is done as offsets of either of those two, relying on
101 * operator overloading in this class.
103 * All the functions are op overloads except for _M_set_node.
105 template<typename _Tp
, typename _Ref
, typename _Ptr
>
106 struct _Deque_iterator
108 #if __cplusplus < 201103L
109 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
110 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
111 typedef _Tp
* _Elt_pointer
;
112 typedef _Tp
** _Map_pointer
;
115 template<typename _Up
>
116 using __ptr_to
= typename pointer_traits
<_Ptr
>::template rebind
<_Up
>;
117 template<typename _CvTp
>
118 using __iter
= _Deque_iterator
<_Tp
, _CvTp
&, __ptr_to
<_CvTp
>>;
120 typedef __iter
<_Tp
> iterator
;
121 typedef __iter
<const _Tp
> const_iterator
;
122 typedef __ptr_to
<_Tp
> _Elt_pointer
;
123 typedef __ptr_to
<_Elt_pointer
> _Map_pointer
;
126 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
127 { return __deque_buf_size(sizeof(_Tp
)); }
129 typedef std::random_access_iterator_tag iterator_category
;
130 typedef _Tp value_type
;
131 typedef _Ptr pointer
;
132 typedef _Ref reference
;
133 typedef size_t size_type
;
134 typedef ptrdiff_t difference_type
;
135 typedef _Deque_iterator _Self
;
138 _Elt_pointer _M_first
;
139 _Elt_pointer _M_last
;
140 _Map_pointer _M_node
;
142 _Deque_iterator(_Elt_pointer __x
, _Map_pointer __y
) _GLIBCXX_NOEXCEPT
143 : _M_cur(__x
), _M_first(*__y
),
144 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
146 _Deque_iterator() _GLIBCXX_NOEXCEPT
147 : _M_cur(), _M_first(), _M_last(), _M_node() { }
149 _Deque_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
150 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
151 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
154 _M_const_cast() const _GLIBCXX_NOEXCEPT
155 { return iterator(_M_cur
, _M_node
); }
158 operator*() const _GLIBCXX_NOEXCEPT
162 operator->() const _GLIBCXX_NOEXCEPT
166 operator++() _GLIBCXX_NOEXCEPT
169 if (_M_cur
== _M_last
)
171 _M_set_node(_M_node
+ 1);
178 operator++(int) _GLIBCXX_NOEXCEPT
186 operator--() _GLIBCXX_NOEXCEPT
188 if (_M_cur
== _M_first
)
190 _M_set_node(_M_node
- 1);
198 operator--(int) _GLIBCXX_NOEXCEPT
206 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
208 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
209 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
213 const difference_type __node_offset
=
214 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
215 : -difference_type((-__offset
- 1)
216 / _S_buffer_size()) - 1;
217 _M_set_node(_M_node
+ __node_offset
);
218 _M_cur
= _M_first
+ (__offset
- __node_offset
219 * difference_type(_S_buffer_size()));
225 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
232 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
233 { return *this += -__n
; }
236 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
243 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
244 { return *(*this + __n
); }
247 * Prepares to traverse new_node. Sets everything except
248 * _M_cur, which should therefore be set by the caller
249 * immediately afterwards, based on _M_first and _M_last.
252 _M_set_node(_Map_pointer __new_node
) _GLIBCXX_NOEXCEPT
254 _M_node
= __new_node
;
255 _M_first
= *__new_node
;
256 _M_last
= _M_first
+ difference_type(_S_buffer_size());
260 // Note: we also provide overloads whose operands are of the same type in
261 // order to avoid ambiguous overload resolution when std::rel_ops operators
262 // are in scope (for additional details, see libstdc++/3628)
263 template<typename _Tp
, typename _Ref
, typename _Ptr
>
265 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
266 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
267 { return __x
._M_cur
== __y
._M_cur
; }
269 template<typename _Tp
, typename _RefL
, typename _PtrL
,
270 typename _RefR
, typename _PtrR
>
272 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
273 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
274 { return __x
._M_cur
== __y
._M_cur
; }
276 template<typename _Tp
, typename _Ref
, typename _Ptr
>
278 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
279 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
280 { return !(__x
== __y
); }
282 template<typename _Tp
, typename _RefL
, typename _PtrL
,
283 typename _RefR
, typename _PtrR
>
285 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
286 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
287 { return !(__x
== __y
); }
289 template<typename _Tp
, typename _Ref
, typename _Ptr
>
291 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
292 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
293 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
294 : (__x
._M_node
< __y
._M_node
); }
296 template<typename _Tp
, typename _RefL
, typename _PtrL
,
297 typename _RefR
, typename _PtrR
>
299 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
300 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
301 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
302 : (__x
._M_node
< __y
._M_node
); }
304 template<typename _Tp
, typename _Ref
, typename _Ptr
>
306 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
307 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
308 { return __y
< __x
; }
310 template<typename _Tp
, typename _RefL
, typename _PtrL
,
311 typename _RefR
, typename _PtrR
>
313 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
314 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
315 { return __y
< __x
; }
317 template<typename _Tp
, typename _Ref
, typename _Ptr
>
319 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
320 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
321 { return !(__y
< __x
); }
323 template<typename _Tp
, typename _RefL
, typename _PtrL
,
324 typename _RefR
, typename _PtrR
>
326 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
327 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
328 { return !(__y
< __x
); }
330 template<typename _Tp
, typename _Ref
, typename _Ptr
>
332 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
333 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
334 { return !(__x
< __y
); }
336 template<typename _Tp
, typename _RefL
, typename _PtrL
,
337 typename _RefR
, typename _PtrR
>
339 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
340 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
341 { return !(__x
< __y
); }
343 // _GLIBCXX_RESOLVE_LIB_DEFECTS
344 // According to the resolution of DR179 not only the various comparison
345 // operators but also operator- must accept mixed iterator/const_iterator
347 template<typename _Tp
, typename _Ref
, typename _Ptr
>
348 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
349 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
350 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
352 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
353 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
354 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
355 + (__y
._M_last
- __y
._M_cur
);
358 template<typename _Tp
, typename _RefL
, typename _PtrL
,
359 typename _RefR
, typename _PtrR
>
360 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
361 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
362 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
364 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
365 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
366 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
367 + (__y
._M_last
- __y
._M_cur
);
370 template<typename _Tp
, typename _Ref
, typename _Ptr
>
371 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
372 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
374 { return __x
+ __n
; }
376 template<typename _Tp
>
378 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
379 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
381 template<typename _Tp
>
382 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
383 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
384 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
385 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
387 template<typename _Tp
>
388 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
389 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
390 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
391 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
392 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
393 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
396 template<typename _Tp
>
397 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
398 copy_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
399 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
400 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
402 template<typename _Tp
>
403 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
404 copy_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
405 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
406 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
407 { return std::copy_backward(_Deque_iterator
<_Tp
,
408 const _Tp
&, const _Tp
*>(__first
),
410 const _Tp
&, const _Tp
*>(__last
),
413 #if __cplusplus >= 201103L
414 template<typename _Tp
>
415 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
416 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
417 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
418 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
420 template<typename _Tp
>
421 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
422 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
423 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
424 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
425 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
426 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
429 template<typename _Tp
>
430 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
431 move_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
432 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
433 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
435 template<typename _Tp
>
436 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
437 move_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
438 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
439 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
440 { return std::move_backward(_Deque_iterator
<_Tp
,
441 const _Tp
&, const _Tp
*>(__first
),
443 const _Tp
&, const _Tp
*>(__last
),
448 * Deque base class. This class provides the unified face for %deque's
449 * allocation. This class's constructor and destructor allocate and
450 * deallocate (but do not initialize) storage. This makes %exception
453 * Nothing in this class ever constructs or destroys an actual Tp element.
454 * (Deque handles that itself.) Only/All memory management is performed
457 template<typename _Tp
, typename _Alloc
>
461 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
462 rebind
<_Tp
>::other _Tp_alloc_type
;
463 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Alloc_traits
;
465 #if __cplusplus < 201103L
467 typedef const _Tp
* _Ptr_const
;
469 typedef typename
_Alloc_traits::pointer _Ptr
;
470 typedef typename
_Alloc_traits::const_pointer _Ptr_const
;
473 typedef typename
_Alloc_traits::template rebind
<_Ptr
>::other
475 typedef __gnu_cxx::__alloc_traits
<_Map_alloc_type
> _Map_alloc_traits
;
478 typedef _Alloc allocator_type
;
479 typedef typename
_Alloc_traits::size_type size_type
;
482 get_allocator() const _GLIBCXX_NOEXCEPT
483 { return allocator_type(_M_get_Tp_allocator()); }
485 typedef _Deque_iterator
<_Tp
, _Tp
&, _Ptr
> iterator
;
486 typedef _Deque_iterator
<_Tp
, const _Tp
&, _Ptr_const
> const_iterator
;
490 { _M_initialize_map(0); }
492 _Deque_base(size_t __num_elements
)
494 { _M_initialize_map(__num_elements
); }
496 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
498 { _M_initialize_map(__num_elements
); }
500 _Deque_base(const allocator_type
& __a
)
502 { /* Caller must initialize map. */ }
504 #if __cplusplus >= 201103L
505 _Deque_base(_Deque_base
&& __x
, false_type
)
506 : _M_impl(__x
._M_move_impl())
509 _Deque_base(_Deque_base
&& __x
, true_type
)
510 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
512 _M_initialize_map(0);
513 if (__x
._M_impl
._M_map
)
514 this->_M_impl
._M_swap_data(__x
._M_impl
);
517 _Deque_base(_Deque_base
&& __x
)
518 : _Deque_base(std::move(__x
), typename
_Alloc_traits::is_always_equal
{})
521 _Deque_base(_Deque_base
&& __x
, const allocator_type
& __a
, size_type __n
)
524 if (__x
.get_allocator() == __a
)
526 if (__x
._M_impl
._M_map
)
528 _M_initialize_map(0);
529 this->_M_impl
._M_swap_data(__x
._M_impl
);
534 _M_initialize_map(__n
);
539 ~_Deque_base() _GLIBCXX_NOEXCEPT
;
542 typedef typename
iterator::_Map_pointer _Map_pointer
;
544 //This struct encapsulates the implementation of the std::deque
545 //standard container and at the same time makes use of the EBO
546 //for empty allocators.
548 : public _Tp_alloc_type
556 : _Tp_alloc_type(), _M_map(), _M_map_size(0),
557 _M_start(), _M_finish()
560 _Deque_impl(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
561 : _Tp_alloc_type(__a
), _M_map(), _M_map_size(0),
562 _M_start(), _M_finish()
565 #if __cplusplus >= 201103L
566 _Deque_impl(_Deque_impl
&&) = default;
568 _Deque_impl(_Tp_alloc_type
&& __a
) noexcept
569 : _Tp_alloc_type(std::move(__a
)), _M_map(), _M_map_size(0),
570 _M_start(), _M_finish()
574 void _M_swap_data(_Deque_impl
& __x
) _GLIBCXX_NOEXCEPT
577 swap(this->_M_start
, __x
._M_start
);
578 swap(this->_M_finish
, __x
._M_finish
);
579 swap(this->_M_map
, __x
._M_map
);
580 swap(this->_M_map_size
, __x
._M_map_size
);
585 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
586 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
588 const _Tp_alloc_type
&
589 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
590 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
593 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
594 { return _Map_alloc_type(_M_get_Tp_allocator()); }
599 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Traits
;
600 return _Traits::allocate(_M_impl
, __deque_buf_size(sizeof(_Tp
)));
604 _M_deallocate_node(_Ptr __p
) _GLIBCXX_NOEXCEPT
606 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Traits
;
607 _Traits::deallocate(_M_impl
, __p
, __deque_buf_size(sizeof(_Tp
)));
611 _M_allocate_map(size_t __n
)
613 _Map_alloc_type __map_alloc
= _M_get_map_allocator();
614 return _Map_alloc_traits::allocate(__map_alloc
, __n
);
618 _M_deallocate_map(_Map_pointer __p
, size_t __n
) _GLIBCXX_NOEXCEPT
620 _Map_alloc_type __map_alloc
= _M_get_map_allocator();
621 _Map_alloc_traits::deallocate(__map_alloc
, __p
, __n
);
625 void _M_initialize_map(size_t);
626 void _M_create_nodes(_Map_pointer __nstart
, _Map_pointer __nfinish
);
627 void _M_destroy_nodes(_Map_pointer __nstart
,
628 _Map_pointer __nfinish
) _GLIBCXX_NOEXCEPT
;
629 enum { _S_initial_map_size
= 8 };
633 #if __cplusplus >= 201103L
639 return std::move(_M_impl
);
641 // Create a copy of the current allocator.
642 _Tp_alloc_type __alloc
{_M_get_Tp_allocator()};
643 // Put that copy in a moved-from state.
644 _Tp_alloc_type __sink
__attribute((__unused__
)) {std::move(__alloc
)};
645 // Create an empty map that allocates using the moved-from allocator.
646 _Deque_base __empty
{__alloc
};
647 __empty
._M_initialize_map(0);
648 // Now safe to modify current allocator and perform non-throwing swaps.
649 _Deque_impl __ret
{std::move(_M_get_Tp_allocator())};
650 _M_impl
._M_swap_data(__ret
);
651 _M_impl
._M_swap_data(__empty
._M_impl
);
657 template<typename _Tp
, typename _Alloc
>
658 _Deque_base
<_Tp
, _Alloc
>::
659 ~_Deque_base() _GLIBCXX_NOEXCEPT
661 if (this->_M_impl
._M_map
)
663 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
664 this->_M_impl
._M_finish
._M_node
+ 1);
665 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
670 * @brief Layout storage.
671 * @param __num_elements The count of T's for which to allocate space
675 * The initial underlying memory layout is a bit complicated...
677 template<typename _Tp
, typename _Alloc
>
679 _Deque_base
<_Tp
, _Alloc
>::
680 _M_initialize_map(size_t __num_elements
)
682 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
685 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
686 size_t(__num_nodes
+ 2));
687 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
689 // For "small" maps (needing less than _M_map_size nodes), allocation
690 // starts in the middle elements and grows outwards. So nstart may be
691 // the beginning of _M_map, but for small maps it may be as far in as
694 _Map_pointer __nstart
= (this->_M_impl
._M_map
695 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
696 _Map_pointer __nfinish
= __nstart
+ __num_nodes
;
699 { _M_create_nodes(__nstart
, __nfinish
); }
702 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
703 this->_M_impl
._M_map
= _Map_pointer();
704 this->_M_impl
._M_map_size
= 0;
705 __throw_exception_again
;
708 this->_M_impl
._M_start
._M_set_node(__nstart
);
709 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
710 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
711 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
713 % __deque_buf_size(sizeof(_Tp
)));
716 template<typename _Tp
, typename _Alloc
>
718 _Deque_base
<_Tp
, _Alloc
>::
719 _M_create_nodes(_Map_pointer __nstart
, _Map_pointer __nfinish
)
724 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
725 *__cur
= this->_M_allocate_node();
729 _M_destroy_nodes(__nstart
, __cur
);
730 __throw_exception_again
;
734 template<typename _Tp
, typename _Alloc
>
736 _Deque_base
<_Tp
, _Alloc
>::
737 _M_destroy_nodes(_Map_pointer __nstart
,
738 _Map_pointer __nfinish
) _GLIBCXX_NOEXCEPT
740 for (_Map_pointer __n
= __nstart
; __n
< __nfinish
; ++__n
)
741 _M_deallocate_node(*__n
);
745 * @brief A standard container using fixed-size memory allocation and
746 * constant-time manipulation of elements at either end.
750 * @tparam _Tp Type of element.
751 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
753 * Meets the requirements of a <a href="tables.html#65">container</a>, a
754 * <a href="tables.html#66">reversible container</a>, and a
755 * <a href="tables.html#67">sequence</a>, including the
756 * <a href="tables.html#68">optional sequence requirements</a>.
758 * In previous HP/SGI versions of deque, there was an extra template
759 * parameter so users could control the node size. This extension turned
760 * out to violate the C++ standard (it can be detected using template
761 * template parameters), and it was removed.
763 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
766 * - size_t _M_map_size
767 * - iterator _M_start, _M_finish
769 * map_size is at least 8. %map is an array of map_size
770 * pointers-to-@a nodes. (The name %map has nothing to do with the
771 * std::map class, and @b nodes should not be confused with
772 * std::list's usage of @a node.)
774 * A @a node has no specific type name as such, but it is referred
775 * to as @a node in this file. It is a simple array-of-Tp. If Tp
776 * is very large, there will be one Tp element per node (i.e., an
777 * @a array of one). For non-huge Tp's, node size is inversely
778 * related to Tp size: the larger the Tp, the fewer Tp's will fit
779 * in a node. The goal here is to keep the total size of a node
780 * relatively small and constant over different Tp's, to improve
781 * allocator efficiency.
783 * Not every pointer in the %map array will point to a node. If
784 * the initial number of elements in the deque is small, the
785 * /middle/ %map pointers will be valid, and the ones at the edges
786 * will be unused. This same situation will arise as the %map
787 * grows: available %map pointers, if any, will be on the ends. As
788 * new nodes are created, only a subset of the %map's pointers need
789 * to be copied @a outward.
792 * - For any nonsingular iterator i:
793 * - i.node points to a member of the %map array. (Yes, you read that
794 * correctly: i.node does not actually point to a node.) The member of
795 * the %map array is what actually points to the node.
796 * - i.first == *(i.node) (This points to the node (first Tp element).)
797 * - i.last == i.first + node_size
798 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
799 * the implication of this is that i.cur is always a dereferenceable
800 * pointer, even if i is a past-the-end iterator.
801 * - Start and Finish are always nonsingular iterators. NOTE: this
802 * means that an empty deque must have one node, a deque with <N
803 * elements (where N is the node buffer size) must have one node, a
804 * deque with N through (2N-1) elements must have two nodes, etc.
805 * - For every node other than start.node and finish.node, every
806 * element in the node is an initialized object. If start.node ==
807 * finish.node, then [start.cur, finish.cur) are initialized
808 * objects, and the elements outside that range are uninitialized
809 * storage. Otherwise, [start.cur, start.last) and [finish.first,
810 * finish.cur) are initialized objects, and [start.first, start.cur)
811 * and [finish.cur, finish.last) are uninitialized storage.
812 * - [%map, %map + map_size) is a valid, non-empty range.
813 * - [start.node, finish.node] is a valid range contained within
814 * [%map, %map + map_size).
815 * - A pointer in the range [%map, %map + map_size) points to an allocated
816 * node if and only if the pointer is in the range
817 * [start.node, finish.node].
819 * Here's the magic: nothing in deque is @b aware of the discontiguous
822 * The memory setup and layout occurs in the parent, _Base, and the iterator
823 * class is entirely responsible for @a leaping from one node to the next.
824 * All the implementation routines for deque itself work only through the
825 * start and finish iterators. This keeps the routines simple and sane,
826 * and we can use other standard algorithms as well.
828 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
829 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
831 // concept requirements
832 typedef typename
_Alloc::value_type _Alloc_value_type
;
833 #if __cplusplus < 201103L
834 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
836 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
838 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
839 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
840 typedef typename
_Base::_Alloc_traits _Alloc_traits
;
841 typedef typename
_Base::_Map_pointer _Map_pointer
;
844 typedef _Tp value_type
;
845 typedef typename
_Alloc_traits::pointer pointer
;
846 typedef typename
_Alloc_traits::const_pointer const_pointer
;
847 typedef typename
_Alloc_traits::reference reference
;
848 typedef typename
_Alloc_traits::const_reference const_reference
;
849 typedef typename
_Base::iterator iterator
;
850 typedef typename
_Base::const_iterator const_iterator
;
851 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
852 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
853 typedef size_t size_type
;
854 typedef ptrdiff_t difference_type
;
855 typedef _Alloc allocator_type
;
858 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
859 { return __deque_buf_size(sizeof(_Tp
)); }
861 // Functions controlling memory layout, and nothing else.
862 using _Base::_M_initialize_map
;
863 using _Base::_M_create_nodes
;
864 using _Base::_M_destroy_nodes
;
865 using _Base::_M_allocate_node
;
866 using _Base::_M_deallocate_node
;
867 using _Base::_M_allocate_map
;
868 using _Base::_M_deallocate_map
;
869 using _Base::_M_get_Tp_allocator
;
872 * A total of four data members accumulated down the hierarchy.
873 * May be accessed via _M_impl.*
875 using _Base::_M_impl
;
878 // [23.2.1.1] construct/copy/destroy
879 // (assign() and get_allocator() are also listed in this section)
882 * @brief Creates a %deque with no elements.
884 deque() : _Base() { }
887 * @brief Creates a %deque with no elements.
888 * @param __a An allocator object.
891 deque(const allocator_type
& __a
)
894 #if __cplusplus >= 201103L
896 * @brief Creates a %deque with default constructed elements.
897 * @param __n The number of elements to initially create.
898 * @param __a An allocator.
900 * This constructor fills the %deque with @a n default
901 * constructed elements.
904 deque(size_type __n
, const allocator_type
& __a
= allocator_type())
906 { _M_default_initialize(); }
909 * @brief Creates a %deque with copies of an exemplar element.
910 * @param __n The number of elements to initially create.
911 * @param __value An element to copy.
912 * @param __a An allocator.
914 * This constructor fills the %deque with @a __n copies of @a __value.
916 deque(size_type __n
, const value_type
& __value
,
917 const allocator_type
& __a
= allocator_type())
919 { _M_fill_initialize(__value
); }
922 * @brief Creates a %deque with copies of an exemplar element.
923 * @param __n The number of elements to initially create.
924 * @param __value An element to copy.
925 * @param __a An allocator.
927 * This constructor fills the %deque with @a __n copies of @a __value.
930 deque(size_type __n
, const value_type
& __value
= value_type(),
931 const allocator_type
& __a
= allocator_type())
933 { _M_fill_initialize(__value
); }
937 * @brief %Deque copy constructor.
938 * @param __x A %deque of identical element and allocator types.
940 * The newly-created %deque uses a copy of the allocation object used
943 deque(const deque
& __x
)
944 : _Base(_Alloc_traits::_S_select_on_copy(__x
._M_get_Tp_allocator()),
946 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
947 this->_M_impl
._M_start
,
948 _M_get_Tp_allocator()); }
950 #if __cplusplus >= 201103L
952 * @brief %Deque move constructor.
953 * @param __x A %deque of identical element and allocator types.
955 * The newly-created %deque contains the exact contents of @a __x.
956 * The contents of @a __x are a valid, but unspecified %deque.
959 : _Base(std::move(__x
)) { }
961 /// Copy constructor with alternative allocator
962 deque(const deque
& __x
, const allocator_type
& __a
)
963 : _Base(__a
, __x
.size())
964 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
965 this->_M_impl
._M_start
,
966 _M_get_Tp_allocator()); }
968 /// Move constructor with alternative allocator
969 deque(deque
&& __x
, const allocator_type
& __a
)
970 : _Base(std::move(__x
), __a
, __x
.size())
972 if (__x
.get_allocator() != __a
)
974 std::__uninitialized_move_a(__x
.begin(), __x
.end(),
975 this->_M_impl
._M_start
,
976 _M_get_Tp_allocator());
982 * @brief Builds a %deque from an initializer list.
983 * @param __l An initializer_list.
984 * @param __a An allocator object.
986 * Create a %deque consisting of copies of the elements in the
987 * initializer_list @a __l.
989 * This will call the element type's copy constructor N times
990 * (where N is __l.size()) and do no memory reallocation.
992 deque(initializer_list
<value_type
> __l
,
993 const allocator_type
& __a
= allocator_type())
996 _M_range_initialize(__l
.begin(), __l
.end(),
997 random_access_iterator_tag());
1002 * @brief Builds a %deque from a range.
1003 * @param __first An input iterator.
1004 * @param __last An input iterator.
1005 * @param __a An allocator object.
1007 * Create a %deque consisting of copies of the elements from [__first,
1010 * If the iterators are forward, bidirectional, or random-access, then
1011 * this will call the elements' copy constructor N times (where N is
1012 * distance(__first,__last)) and do no memory reallocation. But if only
1013 * input iterators are used, then this will do at most 2N calls to the
1014 * copy constructor, and logN memory reallocations.
1016 #if __cplusplus >= 201103L
1017 template<typename _InputIterator
,
1018 typename
= std::_RequireInputIter
<_InputIterator
>>
1019 deque(_InputIterator __first
, _InputIterator __last
,
1020 const allocator_type
& __a
= allocator_type())
1022 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
1024 template<typename _InputIterator
>
1025 deque(_InputIterator __first
, _InputIterator __last
,
1026 const allocator_type
& __a
= allocator_type())
1029 // Check whether it's an integral type. If so, it's not an iterator.
1030 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1031 _M_initialize_dispatch(__first
, __last
, _Integral());
1036 * The dtor only erases the elements, and note that if the elements
1037 * themselves are pointers, the pointed-to memory is not touched in any
1038 * way. Managing the pointer is the user's responsibility.
1041 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
1044 * @brief %Deque assignment operator.
1045 * @param __x A %deque of identical element and allocator types.
1047 * All the elements of @a x are copied, but unlike the copy constructor,
1048 * the allocator object is not copied.
1051 operator=(const deque
& __x
);
1053 #if __cplusplus >= 201103L
1055 * @brief %Deque move assignment operator.
1056 * @param __x A %deque of identical element and allocator types.
1058 * The contents of @a __x are moved into this deque (without copying,
1059 * if the allocators permit it).
1060 * @a __x is a valid, but unspecified %deque.
1063 operator=(deque
&& __x
) noexcept(_Alloc_traits::_S_always_equal())
1065 using __always_equal
= typename
_Alloc_traits::is_always_equal
;
1066 _M_move_assign1(std::move(__x
), __always_equal
{});
1071 * @brief Assigns an initializer list to a %deque.
1072 * @param __l An initializer_list.
1074 * This function fills a %deque with copies of the elements in the
1075 * initializer_list @a __l.
1077 * Note that the assignment completely changes the %deque and that the
1078 * resulting %deque's size is the same as the number of elements
1079 * assigned. Old data may be lost.
1082 operator=(initializer_list
<value_type
> __l
)
1084 this->assign(__l
.begin(), __l
.end());
1090 * @brief Assigns a given value to a %deque.
1091 * @param __n Number of elements to be assigned.
1092 * @param __val Value to be assigned.
1094 * This function fills a %deque with @a n copies of the given
1095 * value. Note that the assignment completely changes the
1096 * %deque and that the resulting %deque's size is the same as
1097 * the number of elements assigned. Old data may be lost.
1100 assign(size_type __n
, const value_type
& __val
)
1101 { _M_fill_assign(__n
, __val
); }
1104 * @brief Assigns a range to a %deque.
1105 * @param __first An input iterator.
1106 * @param __last An input iterator.
1108 * This function fills a %deque with copies of the elements in the
1109 * range [__first,__last).
1111 * Note that the assignment completely changes the %deque and that the
1112 * resulting %deque's size is the same as the number of elements
1113 * assigned. Old data may be lost.
1115 #if __cplusplus >= 201103L
1116 template<typename _InputIterator
,
1117 typename
= std::_RequireInputIter
<_InputIterator
>>
1119 assign(_InputIterator __first
, _InputIterator __last
)
1120 { _M_assign_dispatch(__first
, __last
, __false_type()); }
1122 template<typename _InputIterator
>
1124 assign(_InputIterator __first
, _InputIterator __last
)
1126 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1127 _M_assign_dispatch(__first
, __last
, _Integral());
1131 #if __cplusplus >= 201103L
1133 * @brief Assigns an initializer list to a %deque.
1134 * @param __l An initializer_list.
1136 * This function fills a %deque with copies of the elements in the
1137 * initializer_list @a __l.
1139 * Note that the assignment completely changes the %deque and that the
1140 * resulting %deque's size is the same as the number of elements
1141 * assigned. Old data may be lost.
1144 assign(initializer_list
<value_type
> __l
)
1145 { this->assign(__l
.begin(), __l
.end()); }
1148 /// Get a copy of the memory allocation object.
1150 get_allocator() const _GLIBCXX_NOEXCEPT
1151 { return _Base::get_allocator(); }
1155 * Returns a read/write iterator that points to the first element in the
1156 * %deque. Iteration is done in ordinary element order.
1159 begin() _GLIBCXX_NOEXCEPT
1160 { return this->_M_impl
._M_start
; }
1163 * Returns a read-only (constant) iterator that points to the first
1164 * element in the %deque. Iteration is done in ordinary element order.
1167 begin() const _GLIBCXX_NOEXCEPT
1168 { return this->_M_impl
._M_start
; }
1171 * Returns a read/write iterator that points one past the last
1172 * element in the %deque. Iteration is done in ordinary
1176 end() _GLIBCXX_NOEXCEPT
1177 { return this->_M_impl
._M_finish
; }
1180 * Returns a read-only (constant) iterator that points one past
1181 * the last element in the %deque. Iteration is done in
1182 * ordinary element order.
1185 end() const _GLIBCXX_NOEXCEPT
1186 { return this->_M_impl
._M_finish
; }
1189 * Returns a read/write reverse iterator that points to the
1190 * last element in the %deque. Iteration is done in reverse
1194 rbegin() _GLIBCXX_NOEXCEPT
1195 { return reverse_iterator(this->_M_impl
._M_finish
); }
1198 * Returns a read-only (constant) reverse iterator that points
1199 * to the last element in the %deque. Iteration is done in
1200 * reverse element order.
1202 const_reverse_iterator
1203 rbegin() const _GLIBCXX_NOEXCEPT
1204 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1207 * Returns a read/write reverse iterator that points to one
1208 * before the first element in the %deque. Iteration is done
1209 * in reverse element order.
1212 rend() _GLIBCXX_NOEXCEPT
1213 { return reverse_iterator(this->_M_impl
._M_start
); }
1216 * Returns a read-only (constant) reverse iterator that points
1217 * to one before the first element in the %deque. Iteration is
1218 * done in reverse element order.
1220 const_reverse_iterator
1221 rend() const _GLIBCXX_NOEXCEPT
1222 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1224 #if __cplusplus >= 201103L
1226 * Returns a read-only (constant) iterator that points to the first
1227 * element in the %deque. Iteration is done in ordinary element order.
1230 cbegin() const noexcept
1231 { return this->_M_impl
._M_start
; }
1234 * Returns a read-only (constant) iterator that points one past
1235 * the last element in the %deque. Iteration is done in
1236 * ordinary element order.
1239 cend() const noexcept
1240 { return this->_M_impl
._M_finish
; }
1243 * Returns a read-only (constant) reverse iterator that points
1244 * to the last element in the %deque. Iteration is done in
1245 * reverse element order.
1247 const_reverse_iterator
1248 crbegin() const noexcept
1249 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1252 * Returns a read-only (constant) reverse iterator that points
1253 * to one before the first element in the %deque. Iteration is
1254 * done in reverse element order.
1256 const_reverse_iterator
1257 crend() const noexcept
1258 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1261 // [23.2.1.2] capacity
1262 /** Returns the number of elements in the %deque. */
1264 size() const _GLIBCXX_NOEXCEPT
1265 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1267 /** Returns the size() of the largest possible %deque. */
1269 max_size() const _GLIBCXX_NOEXCEPT
1270 { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
1272 #if __cplusplus >= 201103L
1274 * @brief Resizes the %deque to the specified number of elements.
1275 * @param __new_size Number of elements the %deque should contain.
1277 * This function will %resize the %deque to the specified
1278 * number of elements. If the number is smaller than the
1279 * %deque's current size the %deque is truncated, otherwise
1280 * default constructed elements are appended.
1283 resize(size_type __new_size
)
1285 const size_type __len
= size();
1286 if (__new_size
> __len
)
1287 _M_default_append(__new_size
- __len
);
1288 else if (__new_size
< __len
)
1289 _M_erase_at_end(this->_M_impl
._M_start
1290 + difference_type(__new_size
));
1294 * @brief Resizes the %deque to the specified number of elements.
1295 * @param __new_size Number of elements the %deque should contain.
1296 * @param __x Data with which new elements should be populated.
1298 * This function will %resize the %deque to the specified
1299 * number of elements. If the number is smaller than the
1300 * %deque's current size the %deque is truncated, otherwise the
1301 * %deque is extended and new elements are populated with given
1305 resize(size_type __new_size
, const value_type
& __x
)
1307 const size_type __len
= size();
1308 if (__new_size
> __len
)
1309 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1310 else if (__new_size
< __len
)
1311 _M_erase_at_end(this->_M_impl
._M_start
1312 + difference_type(__new_size
));
1316 * @brief Resizes the %deque to the specified number of elements.
1317 * @param __new_size Number of elements the %deque should contain.
1318 * @param __x Data with which new elements should be populated.
1320 * This function will %resize the %deque to the specified
1321 * number of elements. If the number is smaller than the
1322 * %deque's current size the %deque is truncated, otherwise the
1323 * %deque is extended and new elements are populated with given
1327 resize(size_type __new_size
, value_type __x
= value_type())
1329 const size_type __len
= size();
1330 if (__new_size
> __len
)
1331 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1332 else if (__new_size
< __len
)
1333 _M_erase_at_end(this->_M_impl
._M_start
1334 + difference_type(__new_size
));
1338 #if __cplusplus >= 201103L
1339 /** A non-binding request to reduce memory use. */
1341 shrink_to_fit() noexcept
1342 { _M_shrink_to_fit(); }
1346 * Returns true if the %deque is empty. (Thus begin() would
1350 empty() const _GLIBCXX_NOEXCEPT
1351 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1355 * @brief Subscript access to the data contained in the %deque.
1356 * @param __n The index of the element for which data should be
1358 * @return Read/write reference to data.
1360 * This operator allows for easy, array-style, data access.
1361 * Note that data access with this operator is unchecked and
1362 * out_of_range lookups are not defined. (For checked lookups
1366 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1367 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1370 * @brief Subscript access to the data contained in the %deque.
1371 * @param __n The index of the element for which data should be
1373 * @return Read-only (constant) reference to data.
1375 * This operator allows for easy, array-style, data access.
1376 * Note that data access with this operator is unchecked and
1377 * out_of_range lookups are not defined. (For checked lookups
1381 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1382 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1385 /// Safety check used only from at().
1387 _M_range_check(size_type __n
) const
1389 if (__n
>= this->size())
1390 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1391 "(which is %zu)>= this->size() "
1398 * @brief Provides access to the data contained in the %deque.
1399 * @param __n The index of the element for which data should be
1401 * @return Read/write reference to data.
1402 * @throw std::out_of_range If @a __n is an invalid index.
1404 * This function provides for safer data access. The parameter
1405 * is first checked that it is in the range of the deque. The
1406 * function throws out_of_range if the check fails.
1411 _M_range_check(__n
);
1412 return (*this)[__n
];
1416 * @brief Provides access to the data contained in the %deque.
1417 * @param __n The index of the element for which data should be
1419 * @return Read-only (constant) reference to data.
1420 * @throw std::out_of_range If @a __n is an invalid index.
1422 * This function provides for safer data access. The parameter is first
1423 * checked that it is in the range of the deque. The function throws
1424 * out_of_range if the check fails.
1427 at(size_type __n
) const
1429 _M_range_check(__n
);
1430 return (*this)[__n
];
1434 * Returns a read/write reference to the data at the first
1435 * element of the %deque.
1438 front() _GLIBCXX_NOEXCEPT
1439 { return *begin(); }
1442 * Returns a read-only (constant) reference to the data at the first
1443 * element of the %deque.
1446 front() const _GLIBCXX_NOEXCEPT
1447 { return *begin(); }
1450 * Returns a read/write reference to the data at the last element of the
1454 back() _GLIBCXX_NOEXCEPT
1456 iterator __tmp
= end();
1462 * Returns a read-only (constant) reference to the data at the last
1463 * element of the %deque.
1466 back() const _GLIBCXX_NOEXCEPT
1468 const_iterator __tmp
= end();
1473 // [23.2.1.2] modifiers
1475 * @brief Add data to the front of the %deque.
1476 * @param __x Data to be added.
1478 * This is a typical stack operation. The function creates an
1479 * element at the front of the %deque and assigns the given
1480 * data to it. Due to the nature of a %deque this operation
1481 * can be done in constant time.
1484 push_front(const value_type
& __x
)
1486 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1488 _Alloc_traits::construct(this->_M_impl
,
1489 this->_M_impl
._M_start
._M_cur
- 1,
1491 --this->_M_impl
._M_start
._M_cur
;
1494 _M_push_front_aux(__x
);
1497 #if __cplusplus >= 201103L
1499 push_front(value_type
&& __x
)
1500 { emplace_front(std::move(__x
)); }
1502 template<typename
... _Args
>
1504 emplace_front(_Args
&&... __args
);
1508 * @brief Add data to the end of the %deque.
1509 * @param __x Data to be added.
1511 * This is a typical stack operation. The function creates an
1512 * element at the end of the %deque and assigns the given data
1513 * to it. Due to the nature of a %deque this operation can be
1514 * done in constant time.
1517 push_back(const value_type
& __x
)
1519 if (this->_M_impl
._M_finish
._M_cur
1520 != this->_M_impl
._M_finish
._M_last
- 1)
1522 _Alloc_traits::construct(this->_M_impl
,
1523 this->_M_impl
._M_finish
._M_cur
, __x
);
1524 ++this->_M_impl
._M_finish
._M_cur
;
1527 _M_push_back_aux(__x
);
1530 #if __cplusplus >= 201103L
1532 push_back(value_type
&& __x
)
1533 { emplace_back(std::move(__x
)); }
1535 template<typename
... _Args
>
1537 emplace_back(_Args
&&... __args
);
1541 * @brief Removes first element.
1543 * This is a typical stack operation. It shrinks the %deque by one.
1545 * Note that no data is returned, and if the first element's data is
1546 * needed, it should be retrieved before pop_front() is called.
1549 pop_front() _GLIBCXX_NOEXCEPT
1551 if (this->_M_impl
._M_start
._M_cur
1552 != this->_M_impl
._M_start
._M_last
- 1)
1554 _Alloc_traits::destroy(this->_M_impl
,
1555 this->_M_impl
._M_start
._M_cur
);
1556 ++this->_M_impl
._M_start
._M_cur
;
1563 * @brief Removes last element.
1565 * This is a typical stack operation. It shrinks the %deque by one.
1567 * Note that no data is returned, and if the last element's data is
1568 * needed, it should be retrieved before pop_back() is called.
1571 pop_back() _GLIBCXX_NOEXCEPT
1573 if (this->_M_impl
._M_finish
._M_cur
1574 != this->_M_impl
._M_finish
._M_first
)
1576 --this->_M_impl
._M_finish
._M_cur
;
1577 _Alloc_traits::destroy(this->_M_impl
,
1578 this->_M_impl
._M_finish
._M_cur
);
1584 #if __cplusplus >= 201103L
1586 * @brief Inserts an object in %deque before specified iterator.
1587 * @param __position A const_iterator into the %deque.
1588 * @param __args Arguments.
1589 * @return An iterator that points to the inserted data.
1591 * This function will insert an object of type T constructed
1592 * with T(std::forward<Args>(args)...) before the specified location.
1594 template<typename
... _Args
>
1596 emplace(const_iterator __position
, _Args
&&... __args
);
1599 * @brief Inserts given value into %deque before specified iterator.
1600 * @param __position A const_iterator into the %deque.
1601 * @param __x Data to be inserted.
1602 * @return An iterator that points to the inserted data.
1604 * This function will insert a copy of the given value before the
1605 * specified location.
1608 insert(const_iterator __position
, const value_type
& __x
);
1611 * @brief Inserts given value into %deque before specified iterator.
1612 * @param __position An iterator into the %deque.
1613 * @param __x Data to be inserted.
1614 * @return An iterator that points to the inserted data.
1616 * This function will insert a copy of the given value before the
1617 * specified location.
1620 insert(iterator __position
, const value_type
& __x
);
1623 #if __cplusplus >= 201103L
1625 * @brief Inserts given rvalue into %deque before specified iterator.
1626 * @param __position A const_iterator into the %deque.
1627 * @param __x Data to be inserted.
1628 * @return An iterator that points to the inserted data.
1630 * This function will insert a copy of the given rvalue before the
1631 * specified location.
1634 insert(const_iterator __position
, value_type
&& __x
)
1635 { return emplace(__position
, std::move(__x
)); }
1638 * @brief Inserts an initializer list into the %deque.
1639 * @param __p An iterator into the %deque.
1640 * @param __l An initializer_list.
1642 * This function will insert copies of the data in the
1643 * initializer_list @a __l into the %deque before the location
1644 * specified by @a __p. This is known as <em>list insert</em>.
1647 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1648 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1651 #if __cplusplus >= 201103L
1653 * @brief Inserts a number of copies of given data into the %deque.
1654 * @param __position A const_iterator into the %deque.
1655 * @param __n Number of elements to be inserted.
1656 * @param __x Data to be inserted.
1657 * @return An iterator that points to the inserted data.
1659 * This function will insert a specified number of copies of the given
1660 * data before the location specified by @a __position.
1663 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1665 difference_type __offset
= __position
- cbegin();
1666 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1667 return begin() + __offset
;
1671 * @brief Inserts a number of copies of given data into the %deque.
1672 * @param __position An iterator into the %deque.
1673 * @param __n Number of elements to be inserted.
1674 * @param __x Data to be inserted.
1676 * This function will insert a specified number of copies of the given
1677 * data before the location specified by @a __position.
1680 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1681 { _M_fill_insert(__position
, __n
, __x
); }
1684 #if __cplusplus >= 201103L
1686 * @brief Inserts a range into the %deque.
1687 * @param __position A const_iterator into the %deque.
1688 * @param __first An input iterator.
1689 * @param __last An input iterator.
1690 * @return An iterator that points to the inserted data.
1692 * This function will insert copies of the data in the range
1693 * [__first,__last) into the %deque before the location specified
1694 * by @a __position. This is known as <em>range insert</em>.
1696 template<typename _InputIterator
,
1697 typename
= std::_RequireInputIter
<_InputIterator
>>
1699 insert(const_iterator __position
, _InputIterator __first
,
1700 _InputIterator __last
)
1702 difference_type __offset
= __position
- cbegin();
1703 _M_insert_dispatch(__position
._M_const_cast(),
1704 __first
, __last
, __false_type());
1705 return begin() + __offset
;
1709 * @brief Inserts a range into the %deque.
1710 * @param __position An iterator into the %deque.
1711 * @param __first An input iterator.
1712 * @param __last An input iterator.
1714 * This function will insert copies of the data in the range
1715 * [__first,__last) into the %deque before the location specified
1716 * by @a __position. This is known as <em>range insert</em>.
1718 template<typename _InputIterator
>
1720 insert(iterator __position
, _InputIterator __first
,
1721 _InputIterator __last
)
1723 // Check whether it's an integral type. If so, it's not an iterator.
1724 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1725 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1730 * @brief Remove element at given position.
1731 * @param __position Iterator pointing to element to be erased.
1732 * @return An iterator pointing to the next element (or end()).
1734 * This function will erase the element at the given position and thus
1735 * shorten the %deque by one.
1737 * The user is cautioned that
1738 * this function only erases the element, and that if the element is
1739 * itself a pointer, the pointed-to memory is not touched in any way.
1740 * Managing the pointer is the user's responsibility.
1743 #if __cplusplus >= 201103L
1744 erase(const_iterator __position
)
1746 erase(iterator __position
)
1748 { return _M_erase(__position
._M_const_cast()); }
1751 * @brief Remove a range of elements.
1752 * @param __first Iterator pointing to the first element to be erased.
1753 * @param __last Iterator pointing to one past the last element to be
1755 * @return An iterator pointing to the element pointed to by @a last
1756 * prior to erasing (or end()).
1758 * This function will erase the elements in the range
1759 * [__first,__last) and shorten the %deque accordingly.
1761 * The user is cautioned that
1762 * this function only erases the elements, and that if the elements
1763 * themselves are pointers, the pointed-to memory is not touched in any
1764 * way. Managing the pointer is the user's responsibility.
1767 #if __cplusplus >= 201103L
1768 erase(const_iterator __first
, const_iterator __last
)
1770 erase(iterator __first
, iterator __last
)
1772 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1775 * @brief Swaps data with another %deque.
1776 * @param __x A %deque of the same element and allocator types.
1778 * This exchanges the elements between two deques in constant time.
1779 * (Four pointers, so it should be quite fast.)
1780 * Note that the global std::swap() function is specialized such that
1781 * std::swap(d1,d2) will feed to this function.
1784 swap(deque
& __x
) _GLIBCXX_NOEXCEPT
1786 _M_impl
._M_swap_data(__x
._M_impl
);
1787 _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1788 __x
._M_get_Tp_allocator());
1792 * Erases all the elements. Note that this function only erases the
1793 * elements, and that if the elements themselves are pointers, the
1794 * pointed-to memory is not touched in any way. Managing the pointer is
1795 * the user's responsibility.
1798 clear() _GLIBCXX_NOEXCEPT
1799 { _M_erase_at_end(begin()); }
1802 // Internal constructor functions follow.
1804 // called by the range constructor to implement [23.1.1]/9
1806 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1807 // 438. Ambiguity in the "do the right thing" clause
1808 template<typename _Integer
>
1810 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1812 _M_initialize_map(static_cast<size_type
>(__n
));
1813 _M_fill_initialize(__x
);
1816 // called by the range constructor to implement [23.1.1]/9
1817 template<typename _InputIterator
>
1819 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1822 typedef typename
std::iterator_traits
<_InputIterator
>::
1823 iterator_category _IterCategory
;
1824 _M_range_initialize(__first
, __last
, _IterCategory());
1827 // called by the second initialize_dispatch above
1830 * @brief Fills the deque with whatever is in [first,last).
1831 * @param __first An input iterator.
1832 * @param __last An input iterator.
1835 * If the iterators are actually forward iterators (or better), then the
1836 * memory layout can be done all at once. Else we move forward using
1837 * push_back on each value from the iterator.
1839 template<typename _InputIterator
>
1841 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1842 std::input_iterator_tag
);
1844 // called by the second initialize_dispatch above
1845 template<typename _ForwardIterator
>
1847 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1848 std::forward_iterator_tag
);
1852 * @brief Fills the %deque with copies of value.
1853 * @param __value Initial value.
1855 * @pre _M_start and _M_finish have already been initialized,
1856 * but none of the %deque's elements have yet been constructed.
1858 * This function is called only when the user provides an explicit size
1859 * (with or without an explicit exemplar value).
1862 _M_fill_initialize(const value_type
& __value
);
1864 #if __cplusplus >= 201103L
1865 // called by deque(n).
1867 _M_default_initialize();
1870 // Internal assign functions follow. The *_aux functions do the actual
1871 // assignment work for the range versions.
1873 // called by the range assign to implement [23.1.1]/9
1875 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1876 // 438. Ambiguity in the "do the right thing" clause
1877 template<typename _Integer
>
1879 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1880 { _M_fill_assign(__n
, __val
); }
1882 // called by the range assign to implement [23.1.1]/9
1883 template<typename _InputIterator
>
1885 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1888 typedef typename
std::iterator_traits
<_InputIterator
>::
1889 iterator_category _IterCategory
;
1890 _M_assign_aux(__first
, __last
, _IterCategory());
1893 // called by the second assign_dispatch above
1894 template<typename _InputIterator
>
1896 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1897 std::input_iterator_tag
);
1899 // called by the second assign_dispatch above
1900 template<typename _ForwardIterator
>
1902 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1903 std::forward_iterator_tag
)
1905 const size_type __len
= std::distance(__first
, __last
);
1908 _ForwardIterator __mid
= __first
;
1909 std::advance(__mid
, size());
1910 std::copy(__first
, __mid
, begin());
1911 insert(end(), __mid
, __last
);
1914 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1917 // Called by assign(n,t), and the range assign when it turns out
1918 // to be the same thing.
1920 _M_fill_assign(size_type __n
, const value_type
& __val
)
1924 std::fill(begin(), end(), __val
);
1925 insert(end(), __n
- size(), __val
);
1929 _M_erase_at_end(begin() + difference_type(__n
));
1930 std::fill(begin(), end(), __val
);
1935 /// Helper functions for push_* and pop_*.
1936 #if __cplusplus < 201103L
1937 void _M_push_back_aux(const value_type
&);
1939 void _M_push_front_aux(const value_type
&);
1941 template<typename
... _Args
>
1942 void _M_push_back_aux(_Args
&&... __args
);
1944 template<typename
... _Args
>
1945 void _M_push_front_aux(_Args
&&... __args
);
1948 void _M_pop_back_aux();
1950 void _M_pop_front_aux();
1953 // Internal insert functions follow. The *_aux functions do the actual
1954 // insertion work when all shortcuts fail.
1956 // called by the range insert to implement [23.1.1]/9
1958 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1959 // 438. Ambiguity in the "do the right thing" clause
1960 template<typename _Integer
>
1962 _M_insert_dispatch(iterator __pos
,
1963 _Integer __n
, _Integer __x
, __true_type
)
1964 { _M_fill_insert(__pos
, __n
, __x
); }
1966 // called by the range insert to implement [23.1.1]/9
1967 template<typename _InputIterator
>
1969 _M_insert_dispatch(iterator __pos
,
1970 _InputIterator __first
, _InputIterator __last
,
1973 typedef typename
std::iterator_traits
<_InputIterator
>::
1974 iterator_category _IterCategory
;
1975 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1978 // called by the second insert_dispatch above
1979 template<typename _InputIterator
>
1981 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1982 _InputIterator __last
, std::input_iterator_tag
);
1984 // called by the second insert_dispatch above
1985 template<typename _ForwardIterator
>
1987 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1988 _ForwardIterator __last
, std::forward_iterator_tag
);
1990 // Called by insert(p,n,x), and the range insert when it turns out to be
1991 // the same thing. Can use fill functions in optimal situations,
1992 // otherwise passes off to insert_aux(p,n,x).
1994 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1996 // called by insert(p,x)
1997 #if __cplusplus < 201103L
1999 _M_insert_aux(iterator __pos
, const value_type
& __x
);
2001 template<typename
... _Args
>
2003 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
2006 // called by insert(p,n,x) via fill_insert
2008 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
2010 // called by range_insert_aux for forward iterators
2011 template<typename _ForwardIterator
>
2013 _M_insert_aux(iterator __pos
,
2014 _ForwardIterator __first
, _ForwardIterator __last
,
2018 // Internal erase functions follow.
2021 _M_destroy_data_aux(iterator __first
, iterator __last
);
2023 // Called by ~deque().
2024 // NB: Doesn't deallocate the nodes.
2025 template<typename _Alloc1
>
2027 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
2028 { _M_destroy_data_aux(__first
, __last
); }
2031 _M_destroy_data(iterator __first
, iterator __last
,
2032 const std::allocator
<_Tp
>&)
2034 if (!__has_trivial_destructor(value_type
))
2035 _M_destroy_data_aux(__first
, __last
);
2038 // Called by erase(q1, q2).
2040 _M_erase_at_begin(iterator __pos
)
2042 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
2043 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
2044 this->_M_impl
._M_start
= __pos
;
2047 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2048 // _M_fill_assign, operator=.
2050 _M_erase_at_end(iterator __pos
)
2052 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
2053 _M_destroy_nodes(__pos
._M_node
+ 1,
2054 this->_M_impl
._M_finish
._M_node
+ 1);
2055 this->_M_impl
._M_finish
= __pos
;
2059 _M_erase(iterator __pos
);
2062 _M_erase(iterator __first
, iterator __last
);
2064 #if __cplusplus >= 201103L
2065 // Called by resize(sz).
2067 _M_default_append(size_type __n
);
2074 /// Memory-handling helpers for the previous internal insert functions.
2076 _M_reserve_elements_at_front(size_type __n
)
2078 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
2079 - this->_M_impl
._M_start
._M_first
;
2080 if (__n
> __vacancies
)
2081 _M_new_elements_at_front(__n
- __vacancies
);
2082 return this->_M_impl
._M_start
- difference_type(__n
);
2086 _M_reserve_elements_at_back(size_type __n
)
2088 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
2089 - this->_M_impl
._M_finish
._M_cur
) - 1;
2090 if (__n
> __vacancies
)
2091 _M_new_elements_at_back(__n
- __vacancies
);
2092 return this->_M_impl
._M_finish
+ difference_type(__n
);
2096 _M_new_elements_at_front(size_type __new_elements
);
2099 _M_new_elements_at_back(size_type __new_elements
);
2105 * @brief Memory-handling helpers for the major %map.
2107 * Makes sure the _M_map has space for new nodes. Does not
2108 * actually add the nodes. Can invalidate _M_map pointers.
2109 * (And consequently, %deque iterators.)
2112 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
2114 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
2115 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
2116 _M_reallocate_map(__nodes_to_add
, false);
2120 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
2122 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
2123 - this->_M_impl
._M_map
))
2124 _M_reallocate_map(__nodes_to_add
, true);
2128 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
2131 #if __cplusplus >= 201103L
2132 // Constant-time, nothrow move assignment when source object's memory
2133 // can be moved because the allocators are equal.
2135 _M_move_assign1(deque
&& __x
, /* always equal: */ true_type
) noexcept
2137 this->_M_impl
._M_swap_data(__x
._M_impl
);
2139 std::__alloc_on_move(_M_get_Tp_allocator(), __x
._M_get_Tp_allocator());
2142 // When the allocators are not equal the operation could throw, because
2143 // we might need to allocate a new map for __x after moving from it
2144 // or we might need to allocate new elements for *this.
2146 _M_move_assign1(deque
&& __x
, /* always equal: */ false_type
)
2148 constexpr bool __move_storage
=
2149 _Alloc_traits::_S_propagate_on_move_assign();
2150 _M_move_assign2(std::move(__x
), __bool_constant
<__move_storage
>());
2153 // Destroy all elements and deallocate all memory, then replace
2154 // with elements created from __args.
2155 template<typename
... _Args
>
2157 _M_replace_map(_Args
&&... __args
)
2159 // Create new data first, so if allocation fails there are no effects.
2160 deque
__newobj(std::forward
<_Args
>(__args
)...);
2161 // Free existing storage using existing allocator.
2163 _M_deallocate_node(*begin()._M_node
); // one node left after clear()
2164 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
2165 this->_M_impl
._M_map
= nullptr;
2166 this->_M_impl
._M_map_size
= 0;
2167 // Take ownership of replacement memory.
2168 this->_M_impl
._M_swap_data(__newobj
._M_impl
);
2171 // Do move assignment when the allocator propagates.
2173 _M_move_assign2(deque
&& __x
, /* propagate: */ true_type
)
2175 // Make a copy of the original allocator state.
2176 auto __alloc
= __x
._M_get_Tp_allocator();
2177 // The allocator propagates so storage can be moved from __x,
2178 // leaving __x in a valid empty state with a moved-from allocator.
2179 _M_replace_map(std::move(__x
));
2180 // Move the corresponding allocator state too.
2181 _M_get_Tp_allocator() = std::move(__alloc
);
2184 // Do move assignment when it may not be possible to move source
2185 // object's memory, resulting in a linear-time operation.
2187 _M_move_assign2(deque
&& __x
, /* propagate: */ false_type
)
2189 if (__x
._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2191 // The allocators are equal so storage can be moved from __x,
2192 // leaving __x in a valid empty state with its current allocator.
2193 _M_replace_map(std::move(__x
), __x
.get_allocator());
2197 // The rvalue's allocator cannot be moved and is not equal,
2198 // so we need to individually move each element.
2199 this->assign(std::__make_move_if_noexcept_iterator(__x
.begin()),
2200 std::__make_move_if_noexcept_iterator(__x
.end()));
2209 * @brief Deque equality comparison.
2210 * @param __x A %deque.
2211 * @param __y A %deque of the same type as @a __x.
2212 * @return True iff the size and elements of the deques are equal.
2214 * This is an equivalence relation. It is linear in the size of the
2215 * deques. Deques are considered equivalent if their sizes are equal,
2216 * and if corresponding elements compare equal.
2218 template<typename _Tp
, typename _Alloc
>
2220 operator==(const deque
<_Tp
, _Alloc
>& __x
,
2221 const deque
<_Tp
, _Alloc
>& __y
)
2222 { return __x
.size() == __y
.size()
2223 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
2226 * @brief Deque ordering relation.
2227 * @param __x A %deque.
2228 * @param __y A %deque of the same type as @a __x.
2229 * @return True iff @a x is lexicographically less than @a __y.
2231 * This is a total ordering relation. It is linear in the size of the
2232 * deques. The elements must be comparable with @c <.
2234 * See std::lexicographical_compare() for how the determination is made.
2236 template<typename _Tp
, typename _Alloc
>
2238 operator<(const deque
<_Tp
, _Alloc
>& __x
,
2239 const deque
<_Tp
, _Alloc
>& __y
)
2240 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2241 __y
.begin(), __y
.end()); }
2243 /// Based on operator==
2244 template<typename _Tp
, typename _Alloc
>
2246 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
2247 const deque
<_Tp
, _Alloc
>& __y
)
2248 { return !(__x
== __y
); }
2250 /// Based on operator<
2251 template<typename _Tp
, typename _Alloc
>
2253 operator>(const deque
<_Tp
, _Alloc
>& __x
,
2254 const deque
<_Tp
, _Alloc
>& __y
)
2255 { return __y
< __x
; }
2257 /// Based on operator<
2258 template<typename _Tp
, typename _Alloc
>
2260 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
2261 const deque
<_Tp
, _Alloc
>& __y
)
2262 { return !(__y
< __x
); }
2264 /// Based on operator<
2265 template<typename _Tp
, typename _Alloc
>
2267 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
2268 const deque
<_Tp
, _Alloc
>& __y
)
2269 { return !(__x
< __y
); }
2271 /// See std::deque::swap().
2272 template<typename _Tp
, typename _Alloc
>
2274 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
2275 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2278 #undef _GLIBCXX_DEQUE_BUF_SIZE
2280 _GLIBCXX_END_NAMESPACE_CONTAINER
2283 #endif /* _STL_DEQUE_H */