1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001-2014 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
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 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
109 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
111 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
112 { return __deque_buf_size(sizeof(_Tp
)); }
114 typedef std::random_access_iterator_tag iterator_category
;
115 typedef _Tp value_type
;
116 typedef _Ptr pointer
;
117 typedef _Ref reference
;
118 typedef size_t size_type
;
119 typedef ptrdiff_t difference_type
;
120 typedef _Tp
** _Map_pointer
;
121 typedef _Deque_iterator _Self
;
126 _Map_pointer _M_node
;
128 _Deque_iterator(_Tp
* __x
, _Map_pointer __y
) _GLIBCXX_NOEXCEPT
129 : _M_cur(__x
), _M_first(*__y
),
130 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
132 _Deque_iterator() _GLIBCXX_NOEXCEPT
133 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
135 _Deque_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
136 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
137 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
140 _M_const_cast() const _GLIBCXX_NOEXCEPT
141 { return iterator(_M_cur
, _M_node
); }
144 operator*() const _GLIBCXX_NOEXCEPT
148 operator->() const _GLIBCXX_NOEXCEPT
152 operator++() _GLIBCXX_NOEXCEPT
155 if (_M_cur
== _M_last
)
157 _M_set_node(_M_node
+ 1);
164 operator++(int) _GLIBCXX_NOEXCEPT
172 operator--() _GLIBCXX_NOEXCEPT
174 if (_M_cur
== _M_first
)
176 _M_set_node(_M_node
- 1);
184 operator--(int) _GLIBCXX_NOEXCEPT
192 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
194 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
195 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
199 const difference_type __node_offset
=
200 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
201 : -difference_type((-__offset
- 1)
202 / _S_buffer_size()) - 1;
203 _M_set_node(_M_node
+ __node_offset
);
204 _M_cur
= _M_first
+ (__offset
- __node_offset
205 * difference_type(_S_buffer_size()));
211 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
218 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
219 { return *this += -__n
; }
222 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
229 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
230 { return *(*this + __n
); }
233 * Prepares to traverse new_node. Sets everything except
234 * _M_cur, which should therefore be set by the caller
235 * immediately afterwards, based on _M_first and _M_last.
238 _M_set_node(_Map_pointer __new_node
) _GLIBCXX_NOEXCEPT
240 _M_node
= __new_node
;
241 _M_first
= *__new_node
;
242 _M_last
= _M_first
+ difference_type(_S_buffer_size());
246 // Note: we also provide overloads whose operands are of the same type in
247 // order to avoid ambiguous overload resolution when std::rel_ops operators
248 // are in scope (for additional details, see libstdc++/3628)
249 template<typename _Tp
, typename _Ref
, typename _Ptr
>
251 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
252 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
253 { return __x
._M_cur
== __y
._M_cur
; }
255 template<typename _Tp
, typename _RefL
, typename _PtrL
,
256 typename _RefR
, typename _PtrR
>
258 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
259 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
260 { return __x
._M_cur
== __y
._M_cur
; }
262 template<typename _Tp
, typename _Ref
, typename _Ptr
>
264 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
265 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
266 { return !(__x
== __y
); }
268 template<typename _Tp
, typename _RefL
, typename _PtrL
,
269 typename _RefR
, typename _PtrR
>
271 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
272 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
273 { return !(__x
== __y
); }
275 template<typename _Tp
, typename _Ref
, typename _Ptr
>
277 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
278 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
279 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
280 : (__x
._M_node
< __y
._M_node
); }
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
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
288 : (__x
._M_node
< __y
._M_node
); }
290 template<typename _Tp
, typename _Ref
, typename _Ptr
>
292 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
293 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
294 { return __y
< __x
; }
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 __y
< __x
; }
303 template<typename _Tp
, typename _Ref
, typename _Ptr
>
305 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
306 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
307 { return !(__y
< __x
); }
309 template<typename _Tp
, typename _RefL
, typename _PtrL
,
310 typename _RefR
, typename _PtrR
>
312 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
313 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
314 { return !(__y
< __x
); }
316 template<typename _Tp
, typename _Ref
, typename _Ptr
>
318 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
319 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
320 { return !(__x
< __y
); }
322 template<typename _Tp
, typename _RefL
, typename _PtrL
,
323 typename _RefR
, typename _PtrR
>
325 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
326 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
327 { return !(__x
< __y
); }
329 // _GLIBCXX_RESOLVE_LIB_DEFECTS
330 // According to the resolution of DR179 not only the various comparison
331 // operators but also operator- must accept mixed iterator/const_iterator
333 template<typename _Tp
, typename _Ref
, typename _Ptr
>
334 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
335 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
336 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
338 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
339 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
340 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
341 + (__y
._M_last
- __y
._M_cur
);
344 template<typename _Tp
, typename _RefL
, typename _PtrL
,
345 typename _RefR
, typename _PtrR
>
346 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
347 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
348 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
350 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
351 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
352 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
353 + (__y
._M_last
- __y
._M_cur
);
356 template<typename _Tp
, typename _Ref
, typename _Ptr
>
357 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
358 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
360 { return __x
+ __n
; }
362 template<typename _Tp
>
364 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
365 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
367 template<typename _Tp
>
368 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
369 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
370 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
371 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
373 template<typename _Tp
>
374 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
375 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
376 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
377 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
378 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
379 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
382 template<typename _Tp
>
383 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
384 copy_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
385 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
386 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
388 template<typename _Tp
>
389 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
390 copy_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
391 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
392 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
393 { return std::copy_backward(_Deque_iterator
<_Tp
,
394 const _Tp
&, const _Tp
*>(__first
),
396 const _Tp
&, const _Tp
*>(__last
),
399 #if __cplusplus >= 201103L
400 template<typename _Tp
>
401 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
402 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
403 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
404 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
406 template<typename _Tp
>
407 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
408 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
409 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
410 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
411 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
412 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
415 template<typename _Tp
>
416 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
417 move_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
418 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
419 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
421 template<typename _Tp
>
422 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
423 move_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
424 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
425 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
426 { return std::move_backward(_Deque_iterator
<_Tp
,
427 const _Tp
&, const _Tp
*>(__first
),
429 const _Tp
&, const _Tp
*>(__last
),
434 * Deque base class. This class provides the unified face for %deque's
435 * allocation. This class's constructor and destructor allocate and
436 * deallocate (but do not initialize) storage. This makes %exception
439 * Nothing in this class ever constructs or destroys an actual Tp element.
440 * (Deque handles that itself.) Only/All memory management is performed
443 template<typename _Tp
, typename _Alloc
>
447 typedef _Alloc allocator_type
;
450 get_allocator() const _GLIBCXX_NOEXCEPT
451 { return allocator_type(_M_get_Tp_allocator()); }
453 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
454 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
458 { _M_initialize_map(0); }
460 _Deque_base(size_t __num_elements
)
462 { _M_initialize_map(__num_elements
); }
464 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
466 { _M_initialize_map(__num_elements
); }
468 _Deque_base(const allocator_type
& __a
)
470 { _M_initialize_map(0); }
472 #if __cplusplus >= 201103L
473 _Deque_base(_Deque_base
&& __x
)
474 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
476 _M_initialize_map(0);
477 if (__x
._M_impl
._M_map
)
479 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
480 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
481 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
482 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
487 ~_Deque_base() _GLIBCXX_NOEXCEPT
;
490 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
492 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
494 //This struct encapsulates the implementation of the std::deque
495 //standard container and at the same time makes use of the EBO
496 //for empty allocators.
498 : public _Tp_alloc_type
506 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
507 _M_start(), _M_finish()
510 _Deque_impl(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
511 : _Tp_alloc_type(__a
), _M_map(0), _M_map_size(0),
512 _M_start(), _M_finish()
515 #if __cplusplus >= 201103L
516 _Deque_impl(_Tp_alloc_type
&& __a
) _GLIBCXX_NOEXCEPT
517 : _Tp_alloc_type(std::move(__a
)), _M_map(0), _M_map_size(0),
518 _M_start(), _M_finish()
524 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
525 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
527 const _Tp_alloc_type
&
528 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
529 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
532 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
533 { return _Map_alloc_type(_M_get_Tp_allocator()); }
538 return _M_impl
._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp
)));
542 _M_deallocate_node(_Tp
* __p
) _GLIBCXX_NOEXCEPT
544 _M_impl
._Tp_alloc_type::deallocate(__p
, __deque_buf_size(sizeof(_Tp
)));
548 _M_allocate_map(size_t __n
)
549 { return _M_get_map_allocator().allocate(__n
); }
552 _M_deallocate_map(_Tp
** __p
, size_t __n
) _GLIBCXX_NOEXCEPT
553 { _M_get_map_allocator().deallocate(__p
, __n
); }
556 void _M_initialize_map(size_t);
557 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
558 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
) _GLIBCXX_NOEXCEPT
;
559 enum { _S_initial_map_size
= 8 };
564 template<typename _Tp
, typename _Alloc
>
565 _Deque_base
<_Tp
, _Alloc
>::
566 ~_Deque_base() _GLIBCXX_NOEXCEPT
568 if (this->_M_impl
._M_map
)
570 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
571 this->_M_impl
._M_finish
._M_node
+ 1);
572 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
577 * @brief Layout storage.
578 * @param __num_elements The count of T's for which to allocate space
582 * The initial underlying memory layout is a bit complicated...
584 template<typename _Tp
, typename _Alloc
>
586 _Deque_base
<_Tp
, _Alloc
>::
587 _M_initialize_map(size_t __num_elements
)
589 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
592 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
593 size_t(__num_nodes
+ 2));
594 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
596 // For "small" maps (needing less than _M_map_size nodes), allocation
597 // starts in the middle elements and grows outwards. So nstart may be
598 // the beginning of _M_map, but for small maps it may be as far in as
601 _Tp
** __nstart
= (this->_M_impl
._M_map
602 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
603 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
606 { _M_create_nodes(__nstart
, __nfinish
); }
609 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
610 this->_M_impl
._M_map
= 0;
611 this->_M_impl
._M_map_size
= 0;
612 __throw_exception_again
;
615 this->_M_impl
._M_start
._M_set_node(__nstart
);
616 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
617 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
618 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
620 % __deque_buf_size(sizeof(_Tp
)));
623 template<typename _Tp
, typename _Alloc
>
625 _Deque_base
<_Tp
, _Alloc
>::
626 _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
631 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
632 *__cur
= this->_M_allocate_node();
636 _M_destroy_nodes(__nstart
, __cur
);
637 __throw_exception_again
;
641 template<typename _Tp
, typename _Alloc
>
643 _Deque_base
<_Tp
, _Alloc
>::
644 _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
) _GLIBCXX_NOEXCEPT
646 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
647 _M_deallocate_node(*__n
);
651 * @brief A standard container using fixed-size memory allocation and
652 * constant-time manipulation of elements at either end.
656 * @tparam _Tp Type of element.
657 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
659 * Meets the requirements of a <a href="tables.html#65">container</a>, a
660 * <a href="tables.html#66">reversible container</a>, and a
661 * <a href="tables.html#67">sequence</a>, including the
662 * <a href="tables.html#68">optional sequence requirements</a>.
664 * In previous HP/SGI versions of deque, there was an extra template
665 * parameter so users could control the node size. This extension turned
666 * out to violate the C++ standard (it can be detected using template
667 * template parameters), and it was removed.
669 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
672 * - size_t _M_map_size
673 * - iterator _M_start, _M_finish
675 * map_size is at least 8. %map is an array of map_size
676 * pointers-to-@a nodes. (The name %map has nothing to do with the
677 * std::map class, and @b nodes should not be confused with
678 * std::list's usage of @a node.)
680 * A @a node has no specific type name as such, but it is referred
681 * to as @a node in this file. It is a simple array-of-Tp. If Tp
682 * is very large, there will be one Tp element per node (i.e., an
683 * @a array of one). For non-huge Tp's, node size is inversely
684 * related to Tp size: the larger the Tp, the fewer Tp's will fit
685 * in a node. The goal here is to keep the total size of a node
686 * relatively small and constant over different Tp's, to improve
687 * allocator efficiency.
689 * Not every pointer in the %map array will point to a node. If
690 * the initial number of elements in the deque is small, the
691 * /middle/ %map pointers will be valid, and the ones at the edges
692 * will be unused. This same situation will arise as the %map
693 * grows: available %map pointers, if any, will be on the ends. As
694 * new nodes are created, only a subset of the %map's pointers need
695 * to be copied @a outward.
698 * - For any nonsingular iterator i:
699 * - i.node points to a member of the %map array. (Yes, you read that
700 * correctly: i.node does not actually point to a node.) The member of
701 * the %map array is what actually points to the node.
702 * - i.first == *(i.node) (This points to the node (first Tp element).)
703 * - i.last == i.first + node_size
704 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
705 * the implication of this is that i.cur is always a dereferenceable
706 * pointer, even if i is a past-the-end iterator.
707 * - Start and Finish are always nonsingular iterators. NOTE: this
708 * means that an empty deque must have one node, a deque with <N
709 * elements (where N is the node buffer size) must have one node, a
710 * deque with N through (2N-1) elements must have two nodes, etc.
711 * - For every node other than start.node and finish.node, every
712 * element in the node is an initialized object. If start.node ==
713 * finish.node, then [start.cur, finish.cur) are initialized
714 * objects, and the elements outside that range are uninitialized
715 * storage. Otherwise, [start.cur, start.last) and [finish.first,
716 * finish.cur) are initialized objects, and [start.first, start.cur)
717 * and [finish.cur, finish.last) are uninitialized storage.
718 * - [%map, %map + map_size) is a valid, non-empty range.
719 * - [start.node, finish.node] is a valid range contained within
720 * [%map, %map + map_size).
721 * - A pointer in the range [%map, %map + map_size) points to an allocated
722 * node if and only if the pointer is in the range
723 * [start.node, finish.node].
725 * Here's the magic: nothing in deque is @b aware of the discontiguous
728 * The memory setup and layout occurs in the parent, _Base, and the iterator
729 * class is entirely responsible for @a leaping from one node to the next.
730 * All the implementation routines for deque itself work only through the
731 * start and finish iterators. This keeps the routines simple and sane,
732 * and we can use other standard algorithms as well.
734 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
735 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
737 // concept requirements
738 typedef typename
_Alloc::value_type _Alloc_value_type
;
739 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
740 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
742 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
743 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
746 typedef _Tp value_type
;
747 typedef typename
_Tp_alloc_type::pointer pointer
;
748 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
749 typedef typename
_Tp_alloc_type::reference reference
;
750 typedef typename
_Tp_alloc_type::const_reference const_reference
;
751 typedef typename
_Base::iterator iterator
;
752 typedef typename
_Base::const_iterator const_iterator
;
753 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
754 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
755 typedef size_t size_type
;
756 typedef ptrdiff_t difference_type
;
757 typedef _Alloc allocator_type
;
760 typedef pointer
* _Map_pointer
;
762 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
763 { return __deque_buf_size(sizeof(_Tp
)); }
765 // Functions controlling memory layout, and nothing else.
766 using _Base::_M_initialize_map
;
767 using _Base::_M_create_nodes
;
768 using _Base::_M_destroy_nodes
;
769 using _Base::_M_allocate_node
;
770 using _Base::_M_deallocate_node
;
771 using _Base::_M_allocate_map
;
772 using _Base::_M_deallocate_map
;
773 using _Base::_M_get_Tp_allocator
;
776 * A total of four data members accumulated down the hierarchy.
777 * May be accessed via _M_impl.*
779 using _Base::_M_impl
;
782 // [23.2.1.1] construct/copy/destroy
783 // (assign() and get_allocator() are also listed in this section)
786 * @brief Creates a %deque with no elements.
788 deque() : _Base() { }
791 * @brief Creates a %deque with no elements.
792 * @param __a An allocator object.
795 deque(const allocator_type
& __a
)
798 #if __cplusplus >= 201103L
800 * @brief Creates a %deque with default constructed elements.
801 * @param __n The number of elements to initially create.
803 * This constructor fills the %deque with @a n default
804 * constructed elements.
809 { _M_default_initialize(); }
812 * @brief Creates a %deque with copies of an exemplar element.
813 * @param __n The number of elements to initially create.
814 * @param __value An element to copy.
815 * @param __a An allocator.
817 * This constructor fills the %deque with @a __n copies of @a __value.
819 deque(size_type __n
, const value_type
& __value
,
820 const allocator_type
& __a
= allocator_type())
822 { _M_fill_initialize(__value
); }
825 * @brief Creates a %deque with copies of an exemplar element.
826 * @param __n The number of elements to initially create.
827 * @param __value An element to copy.
828 * @param __a An allocator.
830 * This constructor fills the %deque with @a __n copies of @a __value.
833 deque(size_type __n
, const value_type
& __value
= value_type(),
834 const allocator_type
& __a
= allocator_type())
836 { _M_fill_initialize(__value
); }
840 * @brief %Deque copy constructor.
841 * @param __x A %deque of identical element and allocator types.
843 * The newly-created %deque uses a copy of the allocation object used
846 deque(const deque
& __x
)
847 : _Base(__x
._M_get_Tp_allocator(), __x
.size())
848 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
849 this->_M_impl
._M_start
,
850 _M_get_Tp_allocator()); }
852 #if __cplusplus >= 201103L
854 * @brief %Deque move constructor.
855 * @param __x A %deque of identical element and allocator types.
857 * The newly-created %deque contains the exact contents of @a __x.
858 * The contents of @a __x are a valid, but unspecified %deque.
861 : _Base(std::move(__x
)) { }
864 * @brief Builds a %deque from an initializer list.
865 * @param __l An initializer_list.
866 * @param __a An allocator object.
868 * Create a %deque consisting of copies of the elements in the
869 * initializer_list @a __l.
871 * This will call the element type's copy constructor N times
872 * (where N is __l.size()) and do no memory reallocation.
874 deque(initializer_list
<value_type
> __l
,
875 const allocator_type
& __a
= allocator_type())
878 _M_range_initialize(__l
.begin(), __l
.end(),
879 random_access_iterator_tag());
884 * @brief Builds a %deque from a range.
885 * @param __first An input iterator.
886 * @param __last An input iterator.
887 * @param __a An allocator object.
889 * Create a %deque consisting of copies of the elements from [__first,
892 * If the iterators are forward, bidirectional, or random-access, then
893 * this will call the elements' copy constructor N times (where N is
894 * distance(__first,__last)) and do no memory reallocation. But if only
895 * input iterators are used, then this will do at most 2N calls to the
896 * copy constructor, and logN memory reallocations.
898 #if __cplusplus >= 201103L
899 template<typename _InputIterator
,
900 typename
= std::_RequireInputIter
<_InputIterator
>>
901 deque(_InputIterator __first
, _InputIterator __last
,
902 const allocator_type
& __a
= allocator_type())
904 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
906 template<typename _InputIterator
>
907 deque(_InputIterator __first
, _InputIterator __last
,
908 const allocator_type
& __a
= allocator_type())
911 // Check whether it's an integral type. If so, it's not an iterator.
912 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
913 _M_initialize_dispatch(__first
, __last
, _Integral());
918 * The dtor only erases the elements, and note that if the elements
919 * themselves are pointers, the pointed-to memory is not touched in any
920 * way. Managing the pointer is the user's responsibility.
922 ~deque() _GLIBCXX_NOEXCEPT
923 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
926 * @brief %Deque assignment operator.
927 * @param __x A %deque of identical element and allocator types.
929 * All the elements of @a x are copied, but unlike the copy constructor,
930 * the allocator object is not copied.
933 operator=(const deque
& __x
);
935 #if __cplusplus >= 201103L
937 * @brief %Deque move assignment operator.
938 * @param __x A %deque of identical element and allocator types.
940 * The contents of @a __x are moved into this deque (without copying).
941 * @a __x is a valid, but unspecified %deque.
944 operator=(deque
&& __x
) noexcept
954 * @brief Assigns an initializer list to a %deque.
955 * @param __l An initializer_list.
957 * This function fills a %deque with copies of the elements in the
958 * initializer_list @a __l.
960 * Note that the assignment completely changes the %deque and that the
961 * resulting %deque's size is the same as the number of elements
962 * assigned. Old data may be lost.
965 operator=(initializer_list
<value_type
> __l
)
967 this->assign(__l
.begin(), __l
.end());
973 * @brief Assigns a given value to a %deque.
974 * @param __n Number of elements to be assigned.
975 * @param __val Value to be assigned.
977 * This function fills a %deque with @a n copies of the given
978 * value. Note that the assignment completely changes the
979 * %deque and that the resulting %deque's size is the same as
980 * the number of elements assigned. Old data may be lost.
983 assign(size_type __n
, const value_type
& __val
)
984 { _M_fill_assign(__n
, __val
); }
987 * @brief Assigns a range to a %deque.
988 * @param __first An input iterator.
989 * @param __last An input iterator.
991 * This function fills a %deque with copies of the elements in the
992 * range [__first,__last).
994 * Note that the assignment completely changes the %deque and that the
995 * resulting %deque's size is the same as the number of elements
996 * assigned. Old data may be lost.
998 #if __cplusplus >= 201103L
999 template<typename _InputIterator
,
1000 typename
= std::_RequireInputIter
<_InputIterator
>>
1002 assign(_InputIterator __first
, _InputIterator __last
)
1003 { _M_assign_dispatch(__first
, __last
, __false_type()); }
1005 template<typename _InputIterator
>
1007 assign(_InputIterator __first
, _InputIterator __last
)
1009 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1010 _M_assign_dispatch(__first
, __last
, _Integral());
1014 #if __cplusplus >= 201103L
1016 * @brief Assigns an initializer list to a %deque.
1017 * @param __l An initializer_list.
1019 * This function fills a %deque with copies of the elements in the
1020 * initializer_list @a __l.
1022 * Note that the assignment completely changes the %deque and that the
1023 * resulting %deque's size is the same as the number of elements
1024 * assigned. Old data may be lost.
1027 assign(initializer_list
<value_type
> __l
)
1028 { this->assign(__l
.begin(), __l
.end()); }
1031 /// Get a copy of the memory allocation object.
1033 get_allocator() const _GLIBCXX_NOEXCEPT
1034 { return _Base::get_allocator(); }
1038 * Returns a read/write iterator that points to the first element in the
1039 * %deque. Iteration is done in ordinary element order.
1042 begin() _GLIBCXX_NOEXCEPT
1043 { return this->_M_impl
._M_start
; }
1046 * Returns a read-only (constant) iterator that points to the first
1047 * element in the %deque. Iteration is done in ordinary element order.
1050 begin() const _GLIBCXX_NOEXCEPT
1051 { return this->_M_impl
._M_start
; }
1054 * Returns a read/write iterator that points one past the last
1055 * element in the %deque. Iteration is done in ordinary
1059 end() _GLIBCXX_NOEXCEPT
1060 { return this->_M_impl
._M_finish
; }
1063 * Returns a read-only (constant) iterator that points one past
1064 * the last element in the %deque. Iteration is done in
1065 * ordinary element order.
1068 end() const _GLIBCXX_NOEXCEPT
1069 { return this->_M_impl
._M_finish
; }
1072 * Returns a read/write reverse iterator that points to the
1073 * last element in the %deque. Iteration is done in reverse
1077 rbegin() _GLIBCXX_NOEXCEPT
1078 { return reverse_iterator(this->_M_impl
._M_finish
); }
1081 * Returns a read-only (constant) reverse iterator that points
1082 * to the last element in the %deque. Iteration is done in
1083 * reverse element order.
1085 const_reverse_iterator
1086 rbegin() const _GLIBCXX_NOEXCEPT
1087 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1090 * Returns a read/write reverse iterator that points to one
1091 * before the first element in the %deque. Iteration is done
1092 * in reverse element order.
1095 rend() _GLIBCXX_NOEXCEPT
1096 { return reverse_iterator(this->_M_impl
._M_start
); }
1099 * Returns a read-only (constant) reverse iterator that points
1100 * to one before the first element in the %deque. Iteration is
1101 * done in reverse element order.
1103 const_reverse_iterator
1104 rend() const _GLIBCXX_NOEXCEPT
1105 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1107 #if __cplusplus >= 201103L
1109 * Returns a read-only (constant) iterator that points to the first
1110 * element in the %deque. Iteration is done in ordinary element order.
1113 cbegin() const noexcept
1114 { return this->_M_impl
._M_start
; }
1117 * Returns a read-only (constant) iterator that points one past
1118 * the last element in the %deque. Iteration is done in
1119 * ordinary element order.
1122 cend() const noexcept
1123 { return this->_M_impl
._M_finish
; }
1126 * Returns a read-only (constant) reverse iterator that points
1127 * to the last element in the %deque. Iteration is done in
1128 * reverse element order.
1130 const_reverse_iterator
1131 crbegin() const noexcept
1132 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1135 * Returns a read-only (constant) reverse iterator that points
1136 * to one before the first element in the %deque. Iteration is
1137 * done in reverse element order.
1139 const_reverse_iterator
1140 crend() const noexcept
1141 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1144 // [23.2.1.2] capacity
1145 /** Returns the number of elements in the %deque. */
1147 size() const _GLIBCXX_NOEXCEPT
1148 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1150 /** Returns the size() of the largest possible %deque. */
1152 max_size() const _GLIBCXX_NOEXCEPT
1153 { return _M_get_Tp_allocator().max_size(); }
1155 #if __cplusplus >= 201103L
1157 * @brief Resizes the %deque to the specified number of elements.
1158 * @param __new_size Number of elements the %deque should contain.
1160 * This function will %resize the %deque to the specified
1161 * number of elements. If the number is smaller than the
1162 * %deque's current size the %deque is truncated, otherwise
1163 * default constructed elements are appended.
1166 resize(size_type __new_size
)
1168 const size_type __len
= size();
1169 if (__new_size
> __len
)
1170 _M_default_append(__new_size
- __len
);
1171 else if (__new_size
< __len
)
1172 _M_erase_at_end(this->_M_impl
._M_start
1173 + difference_type(__new_size
));
1177 * @brief Resizes the %deque to the specified number of elements.
1178 * @param __new_size Number of elements the %deque should contain.
1179 * @param __x Data with which new elements should be populated.
1181 * This function will %resize the %deque to the specified
1182 * number of elements. If the number is smaller than the
1183 * %deque's current size the %deque is truncated, otherwise the
1184 * %deque is extended and new elements are populated with given
1188 resize(size_type __new_size
, const value_type
& __x
)
1190 const size_type __len
= size();
1191 if (__new_size
> __len
)
1192 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1193 else if (__new_size
< __len
)
1194 _M_erase_at_end(this->_M_impl
._M_start
1195 + difference_type(__new_size
));
1199 * @brief Resizes the %deque to the specified number of elements.
1200 * @param __new_size Number of elements the %deque should contain.
1201 * @param __x Data with which new elements should be populated.
1203 * This function will %resize the %deque to the specified
1204 * number of elements. If the number is smaller than the
1205 * %deque's current size the %deque is truncated, otherwise the
1206 * %deque is extended and new elements are populated with given
1210 resize(size_type __new_size
, value_type __x
= value_type())
1212 const size_type __len
= size();
1213 if (__new_size
> __len
)
1214 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1215 else if (__new_size
< __len
)
1216 _M_erase_at_end(this->_M_impl
._M_start
1217 + difference_type(__new_size
));
1221 #if __cplusplus >= 201103L
1222 /** A non-binding request to reduce memory use. */
1224 shrink_to_fit() noexcept
1225 { _M_shrink_to_fit(); }
1229 * Returns true if the %deque is empty. (Thus begin() would
1233 empty() const _GLIBCXX_NOEXCEPT
1234 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1238 * @brief Subscript access to the data contained in the %deque.
1239 * @param __n The index of the element for which data should be
1241 * @return Read/write reference to data.
1243 * This operator allows for easy, array-style, data access.
1244 * Note that data access with this operator is unchecked and
1245 * out_of_range lookups are not defined. (For checked lookups
1249 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1250 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1253 * @brief Subscript access to the data contained in the %deque.
1254 * @param __n The index of the element for which data should be
1256 * @return Read-only (constant) reference to data.
1258 * This operator allows for easy, array-style, data access.
1259 * Note that data access with this operator is unchecked and
1260 * out_of_range lookups are not defined. (For checked lookups
1264 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1265 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1268 /// Safety check used only from at().
1270 _M_range_check(size_type __n
) const
1272 if (__n
>= this->size())
1273 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1274 "(which is %zu)>= this->size() "
1281 * @brief Provides access to the data contained in the %deque.
1282 * @param __n The index of the element for which data should be
1284 * @return Read/write reference to data.
1285 * @throw std::out_of_range If @a __n is an invalid index.
1287 * This function provides for safer data access. The parameter
1288 * is first checked that it is in the range of the deque. The
1289 * function throws out_of_range if the check fails.
1294 _M_range_check(__n
);
1295 return (*this)[__n
];
1299 * @brief Provides access to the data contained in the %deque.
1300 * @param __n The index of the element for which data should be
1302 * @return Read-only (constant) reference to data.
1303 * @throw std::out_of_range If @a __n is an invalid index.
1305 * This function provides for safer data access. The parameter is first
1306 * checked that it is in the range of the deque. The function throws
1307 * out_of_range if the check fails.
1310 at(size_type __n
) const
1312 _M_range_check(__n
);
1313 return (*this)[__n
];
1317 * Returns a read/write reference to the data at the first
1318 * element of the %deque.
1321 front() _GLIBCXX_NOEXCEPT
1322 { return *begin(); }
1325 * Returns a read-only (constant) reference to the data at the first
1326 * element of the %deque.
1329 front() const _GLIBCXX_NOEXCEPT
1330 { return *begin(); }
1333 * Returns a read/write reference to the data at the last element of the
1337 back() _GLIBCXX_NOEXCEPT
1339 iterator __tmp
= end();
1345 * Returns a read-only (constant) reference to the data at the last
1346 * element of the %deque.
1349 back() const _GLIBCXX_NOEXCEPT
1351 const_iterator __tmp
= end();
1356 // [23.2.1.2] modifiers
1358 * @brief Add data to the front of the %deque.
1359 * @param __x Data to be added.
1361 * This is a typical stack operation. The function creates an
1362 * element at the front of the %deque and assigns the given
1363 * data to it. Due to the nature of a %deque this operation
1364 * can be done in constant time.
1367 push_front(const value_type
& __x
)
1369 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1371 this->_M_impl
.construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
1372 --this->_M_impl
._M_start
._M_cur
;
1375 _M_push_front_aux(__x
);
1378 #if __cplusplus >= 201103L
1380 push_front(value_type
&& __x
)
1381 { emplace_front(std::move(__x
)); }
1383 template<typename
... _Args
>
1385 emplace_front(_Args
&&... __args
);
1389 * @brief Add data to the end of the %deque.
1390 * @param __x Data to be added.
1392 * This is a typical stack operation. The function creates an
1393 * element at the end of the %deque and assigns the given data
1394 * to it. Due to the nature of a %deque this operation can be
1395 * done in constant time.
1398 push_back(const value_type
& __x
)
1400 if (this->_M_impl
._M_finish
._M_cur
1401 != this->_M_impl
._M_finish
._M_last
- 1)
1403 this->_M_impl
.construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1404 ++this->_M_impl
._M_finish
._M_cur
;
1407 _M_push_back_aux(__x
);
1410 #if __cplusplus >= 201103L
1412 push_back(value_type
&& __x
)
1413 { emplace_back(std::move(__x
)); }
1415 template<typename
... _Args
>
1417 emplace_back(_Args
&&... __args
);
1421 * @brief Removes first element.
1423 * This is a typical stack operation. It shrinks the %deque by one.
1425 * Note that no data is returned, and if the first element's data is
1426 * needed, it should be retrieved before pop_front() is called.
1429 pop_front() _GLIBCXX_NOEXCEPT
1431 if (this->_M_impl
._M_start
._M_cur
1432 != this->_M_impl
._M_start
._M_last
- 1)
1434 this->_M_impl
.destroy(this->_M_impl
._M_start
._M_cur
);
1435 ++this->_M_impl
._M_start
._M_cur
;
1442 * @brief Removes last element.
1444 * This is a typical stack operation. It shrinks the %deque by one.
1446 * Note that no data is returned, and if the last element's data is
1447 * needed, it should be retrieved before pop_back() is called.
1450 pop_back() _GLIBCXX_NOEXCEPT
1452 if (this->_M_impl
._M_finish
._M_cur
1453 != this->_M_impl
._M_finish
._M_first
)
1455 --this->_M_impl
._M_finish
._M_cur
;
1456 this->_M_impl
.destroy(this->_M_impl
._M_finish
._M_cur
);
1462 #if __cplusplus >= 201103L
1464 * @brief Inserts an object in %deque before specified iterator.
1465 * @param __position A const_iterator into the %deque.
1466 * @param __args Arguments.
1467 * @return An iterator that points to the inserted data.
1469 * This function will insert an object of type T constructed
1470 * with T(std::forward<Args>(args)...) before the specified location.
1472 template<typename
... _Args
>
1474 emplace(const_iterator __position
, _Args
&&... __args
);
1477 * @brief Inserts given value into %deque before specified iterator.
1478 * @param __position A const_iterator into the %deque.
1479 * @param __x Data to be inserted.
1480 * @return An iterator that points to the inserted data.
1482 * This function will insert a copy of the given value before the
1483 * specified location.
1486 insert(const_iterator __position
, const value_type
& __x
);
1489 * @brief Inserts given value into %deque before specified iterator.
1490 * @param __position An iterator into the %deque.
1491 * @param __x Data to be inserted.
1492 * @return An iterator that points to the inserted data.
1494 * This function will insert a copy of the given value before the
1495 * specified location.
1498 insert(iterator __position
, const value_type
& __x
);
1501 #if __cplusplus >= 201103L
1503 * @brief Inserts given rvalue into %deque before specified iterator.
1504 * @param __position A const_iterator into the %deque.
1505 * @param __x Data to be inserted.
1506 * @return An iterator that points to the inserted data.
1508 * This function will insert a copy of the given rvalue before the
1509 * specified location.
1512 insert(const_iterator __position
, value_type
&& __x
)
1513 { return emplace(__position
, std::move(__x
)); }
1516 * @brief Inserts an initializer list into the %deque.
1517 * @param __p An iterator into the %deque.
1518 * @param __l An initializer_list.
1520 * This function will insert copies of the data in the
1521 * initializer_list @a __l into the %deque before the location
1522 * specified by @a __p. This is known as <em>list insert</em>.
1525 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1526 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1529 #if __cplusplus >= 201103L
1531 * @brief Inserts a number of copies of given data into the %deque.
1532 * @param __position A const_iterator into the %deque.
1533 * @param __n Number of elements to be inserted.
1534 * @param __x Data to be inserted.
1535 * @return An iterator that points to the inserted data.
1537 * This function will insert a specified number of copies of the given
1538 * data before the location specified by @a __position.
1541 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1543 difference_type __offset
= __position
- cbegin();
1544 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1545 return begin() + __offset
;
1549 * @brief Inserts a number of copies of given data into the %deque.
1550 * @param __position An iterator into the %deque.
1551 * @param __n Number of elements to be inserted.
1552 * @param __x Data to be inserted.
1554 * This function will insert a specified number of copies of the given
1555 * data before the location specified by @a __position.
1558 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1559 { _M_fill_insert(__position
, __n
, __x
); }
1562 #if __cplusplus >= 201103L
1564 * @brief Inserts a range into the %deque.
1565 * @param __position A const_iterator into the %deque.
1566 * @param __first An input iterator.
1567 * @param __last An input iterator.
1568 * @return An iterator that points to the inserted data.
1570 * This function will insert copies of the data in the range
1571 * [__first,__last) into the %deque before the location specified
1572 * by @a __position. This is known as <em>range insert</em>.
1574 template<typename _InputIterator
,
1575 typename
= std::_RequireInputIter
<_InputIterator
>>
1577 insert(const_iterator __position
, _InputIterator __first
,
1578 _InputIterator __last
)
1580 difference_type __offset
= __position
- cbegin();
1581 _M_insert_dispatch(__position
._M_const_cast(),
1582 __first
, __last
, __false_type());
1583 return begin() + __offset
;
1587 * @brief Inserts a range into the %deque.
1588 * @param __position An iterator into the %deque.
1589 * @param __first An input iterator.
1590 * @param __last An input iterator.
1592 * This function will insert copies of the data in the range
1593 * [__first,__last) into the %deque before the location specified
1594 * by @a __position. This is known as <em>range insert</em>.
1596 template<typename _InputIterator
>
1598 insert(iterator __position
, _InputIterator __first
,
1599 _InputIterator __last
)
1601 // Check whether it's an integral type. If so, it's not an iterator.
1602 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1603 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1608 * @brief Remove element at given position.
1609 * @param __position Iterator pointing to element to be erased.
1610 * @return An iterator pointing to the next element (or end()).
1612 * This function will erase the element at the given position and thus
1613 * shorten the %deque by one.
1615 * The user is cautioned that
1616 * this function only erases the element, and that if the element is
1617 * itself a pointer, the pointed-to memory is not touched in any way.
1618 * Managing the pointer is the user's responsibility.
1621 #if __cplusplus >= 201103L
1622 erase(const_iterator __position
)
1624 erase(iterator __position
)
1626 { return _M_erase(__position
._M_const_cast()); }
1629 * @brief Remove a range of elements.
1630 * @param __first Iterator pointing to the first element to be erased.
1631 * @param __last Iterator pointing to one past the last element to be
1633 * @return An iterator pointing to the element pointed to by @a last
1634 * prior to erasing (or end()).
1636 * This function will erase the elements in the range
1637 * [__first,__last) and shorten the %deque accordingly.
1639 * The user is cautioned that
1640 * this function only erases the elements, and that if the elements
1641 * themselves are pointers, the pointed-to memory is not touched in any
1642 * way. Managing the pointer is the user's responsibility.
1645 #if __cplusplus >= 201103L
1646 erase(const_iterator __first
, const_iterator __last
)
1648 erase(iterator __first
, iterator __last
)
1650 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1653 * @brief Swaps data with another %deque.
1654 * @param __x A %deque of the same element and allocator types.
1656 * This exchanges the elements between two deques in constant time.
1657 * (Four pointers, so it should be quite fast.)
1658 * Note that the global std::swap() function is specialized such that
1659 * std::swap(d1,d2) will feed to this function.
1662 swap(deque
& __x
) _GLIBCXX_NOEXCEPT
1664 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1665 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1666 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1667 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1669 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1670 // 431. Swapping containers with unequal allocators.
1671 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1672 __x
._M_get_Tp_allocator());
1676 * Erases all the elements. Note that this function only erases the
1677 * elements, and that if the elements themselves are pointers, the
1678 * pointed-to memory is not touched in any way. Managing the pointer is
1679 * the user's responsibility.
1682 clear() _GLIBCXX_NOEXCEPT
1683 { _M_erase_at_end(begin()); }
1686 // Internal constructor functions follow.
1688 // called by the range constructor to implement [23.1.1]/9
1690 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1691 // 438. Ambiguity in the "do the right thing" clause
1692 template<typename _Integer
>
1694 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1696 _M_initialize_map(static_cast<size_type
>(__n
));
1697 _M_fill_initialize(__x
);
1700 // called by the range constructor to implement [23.1.1]/9
1701 template<typename _InputIterator
>
1703 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1706 typedef typename
std::iterator_traits
<_InputIterator
>::
1707 iterator_category _IterCategory
;
1708 _M_range_initialize(__first
, __last
, _IterCategory());
1711 // called by the second initialize_dispatch above
1714 * @brief Fills the deque with whatever is in [first,last).
1715 * @param __first An input iterator.
1716 * @param __last An input iterator.
1719 * If the iterators are actually forward iterators (or better), then the
1720 * memory layout can be done all at once. Else we move forward using
1721 * push_back on each value from the iterator.
1723 template<typename _InputIterator
>
1725 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1726 std::input_iterator_tag
);
1728 // called by the second initialize_dispatch above
1729 template<typename _ForwardIterator
>
1731 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1732 std::forward_iterator_tag
);
1736 * @brief Fills the %deque with copies of value.
1737 * @param __value Initial value.
1739 * @pre _M_start and _M_finish have already been initialized,
1740 * but none of the %deque's elements have yet been constructed.
1742 * This function is called only when the user provides an explicit size
1743 * (with or without an explicit exemplar value).
1746 _M_fill_initialize(const value_type
& __value
);
1748 #if __cplusplus >= 201103L
1749 // called by deque(n).
1751 _M_default_initialize();
1754 // Internal assign functions follow. The *_aux functions do the actual
1755 // assignment work for the range versions.
1757 // called by the range assign to implement [23.1.1]/9
1759 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1760 // 438. Ambiguity in the "do the right thing" clause
1761 template<typename _Integer
>
1763 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1764 { _M_fill_assign(__n
, __val
); }
1766 // called by the range assign to implement [23.1.1]/9
1767 template<typename _InputIterator
>
1769 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1772 typedef typename
std::iterator_traits
<_InputIterator
>::
1773 iterator_category _IterCategory
;
1774 _M_assign_aux(__first
, __last
, _IterCategory());
1777 // called by the second assign_dispatch above
1778 template<typename _InputIterator
>
1780 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1781 std::input_iterator_tag
);
1783 // called by the second assign_dispatch above
1784 template<typename _ForwardIterator
>
1786 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1787 std::forward_iterator_tag
)
1789 const size_type __len
= std::distance(__first
, __last
);
1792 _ForwardIterator __mid
= __first
;
1793 std::advance(__mid
, size());
1794 std::copy(__first
, __mid
, begin());
1795 insert(end(), __mid
, __last
);
1798 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1801 // Called by assign(n,t), and the range assign when it turns out
1802 // to be the same thing.
1804 _M_fill_assign(size_type __n
, const value_type
& __val
)
1808 std::fill(begin(), end(), __val
);
1809 insert(end(), __n
- size(), __val
);
1813 _M_erase_at_end(begin() + difference_type(__n
));
1814 std::fill(begin(), end(), __val
);
1819 /// Helper functions for push_* and pop_*.
1820 #if __cplusplus < 201103L
1821 void _M_push_back_aux(const value_type
&);
1823 void _M_push_front_aux(const value_type
&);
1825 template<typename
... _Args
>
1826 void _M_push_back_aux(_Args
&&... __args
);
1828 template<typename
... _Args
>
1829 void _M_push_front_aux(_Args
&&... __args
);
1832 void _M_pop_back_aux();
1834 void _M_pop_front_aux();
1837 // Internal insert functions follow. The *_aux functions do the actual
1838 // insertion work when all shortcuts fail.
1840 // called by the range insert to implement [23.1.1]/9
1842 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1843 // 438. Ambiguity in the "do the right thing" clause
1844 template<typename _Integer
>
1846 _M_insert_dispatch(iterator __pos
,
1847 _Integer __n
, _Integer __x
, __true_type
)
1848 { _M_fill_insert(__pos
, __n
, __x
); }
1850 // called by the range insert to implement [23.1.1]/9
1851 template<typename _InputIterator
>
1853 _M_insert_dispatch(iterator __pos
,
1854 _InputIterator __first
, _InputIterator __last
,
1857 typedef typename
std::iterator_traits
<_InputIterator
>::
1858 iterator_category _IterCategory
;
1859 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1862 // called by the second insert_dispatch above
1863 template<typename _InputIterator
>
1865 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1866 _InputIterator __last
, std::input_iterator_tag
);
1868 // called by the second insert_dispatch above
1869 template<typename _ForwardIterator
>
1871 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1872 _ForwardIterator __last
, std::forward_iterator_tag
);
1874 // Called by insert(p,n,x), and the range insert when it turns out to be
1875 // the same thing. Can use fill functions in optimal situations,
1876 // otherwise passes off to insert_aux(p,n,x).
1878 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1880 // called by insert(p,x)
1881 #if __cplusplus < 201103L
1883 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1885 template<typename
... _Args
>
1887 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
1890 // called by insert(p,n,x) via fill_insert
1892 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1894 // called by range_insert_aux for forward iterators
1895 template<typename _ForwardIterator
>
1897 _M_insert_aux(iterator __pos
,
1898 _ForwardIterator __first
, _ForwardIterator __last
,
1902 // Internal erase functions follow.
1905 _M_destroy_data_aux(iterator __first
, iterator __last
);
1907 // Called by ~deque().
1908 // NB: Doesn't deallocate the nodes.
1909 template<typename _Alloc1
>
1911 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
1912 { _M_destroy_data_aux(__first
, __last
); }
1915 _M_destroy_data(iterator __first
, iterator __last
,
1916 const std::allocator
<_Tp
>&)
1918 if (!__has_trivial_destructor(value_type
))
1919 _M_destroy_data_aux(__first
, __last
);
1922 // Called by erase(q1, q2).
1924 _M_erase_at_begin(iterator __pos
)
1926 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
1927 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
1928 this->_M_impl
._M_start
= __pos
;
1931 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1932 // _M_fill_assign, operator=.
1934 _M_erase_at_end(iterator __pos
)
1936 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
1937 _M_destroy_nodes(__pos
._M_node
+ 1,
1938 this->_M_impl
._M_finish
._M_node
+ 1);
1939 this->_M_impl
._M_finish
= __pos
;
1943 _M_erase(iterator __pos
);
1946 _M_erase(iterator __first
, iterator __last
);
1948 #if __cplusplus >= 201103L
1949 // Called by resize(sz).
1951 _M_default_append(size_type __n
);
1958 /// Memory-handling helpers for the previous internal insert functions.
1960 _M_reserve_elements_at_front(size_type __n
)
1962 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1963 - this->_M_impl
._M_start
._M_first
;
1964 if (__n
> __vacancies
)
1965 _M_new_elements_at_front(__n
- __vacancies
);
1966 return this->_M_impl
._M_start
- difference_type(__n
);
1970 _M_reserve_elements_at_back(size_type __n
)
1972 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1973 - this->_M_impl
._M_finish
._M_cur
) - 1;
1974 if (__n
> __vacancies
)
1975 _M_new_elements_at_back(__n
- __vacancies
);
1976 return this->_M_impl
._M_finish
+ difference_type(__n
);
1980 _M_new_elements_at_front(size_type __new_elements
);
1983 _M_new_elements_at_back(size_type __new_elements
);
1989 * @brief Memory-handling helpers for the major %map.
1991 * Makes sure the _M_map has space for new nodes. Does not
1992 * actually add the nodes. Can invalidate _M_map pointers.
1993 * (And consequently, %deque iterators.)
1996 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
1998 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1999 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
2000 _M_reallocate_map(__nodes_to_add
, false);
2004 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
2006 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
2007 - this->_M_impl
._M_map
))
2008 _M_reallocate_map(__nodes_to_add
, true);
2012 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
2018 * @brief Deque equality comparison.
2019 * @param __x A %deque.
2020 * @param __y A %deque of the same type as @a __x.
2021 * @return True iff the size and elements of the deques are equal.
2023 * This is an equivalence relation. It is linear in the size of the
2024 * deques. Deques are considered equivalent if their sizes are equal,
2025 * and if corresponding elements compare equal.
2027 template<typename _Tp
, typename _Alloc
>
2029 operator==(const deque
<_Tp
, _Alloc
>& __x
,
2030 const deque
<_Tp
, _Alloc
>& __y
)
2031 { return __x
.size() == __y
.size()
2032 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
2035 * @brief Deque ordering relation.
2036 * @param __x A %deque.
2037 * @param __y A %deque of the same type as @a __x.
2038 * @return True iff @a x is lexicographically less than @a __y.
2040 * This is a total ordering relation. It is linear in the size of the
2041 * deques. The elements must be comparable with @c <.
2043 * See std::lexicographical_compare() for how the determination is made.
2045 template<typename _Tp
, typename _Alloc
>
2047 operator<(const deque
<_Tp
, _Alloc
>& __x
,
2048 const deque
<_Tp
, _Alloc
>& __y
)
2049 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2050 __y
.begin(), __y
.end()); }
2052 /// Based on operator==
2053 template<typename _Tp
, typename _Alloc
>
2055 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
2056 const deque
<_Tp
, _Alloc
>& __y
)
2057 { return !(__x
== __y
); }
2059 /// Based on operator<
2060 template<typename _Tp
, typename _Alloc
>
2062 operator>(const deque
<_Tp
, _Alloc
>& __x
,
2063 const deque
<_Tp
, _Alloc
>& __y
)
2064 { return __y
< __x
; }
2066 /// Based on operator<
2067 template<typename _Tp
, typename _Alloc
>
2069 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
2070 const deque
<_Tp
, _Alloc
>& __y
)
2071 { return !(__y
< __x
); }
2073 /// Based on operator<
2074 template<typename _Tp
, typename _Alloc
>
2076 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
2077 const deque
<_Tp
, _Alloc
>& __y
)
2078 { return !(__x
< __y
); }
2080 /// See std::deque::swap().
2081 template<typename _Tp
, typename _Alloc
>
2083 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
2086 #undef _GLIBCXX_DEQUE_BUF_SIZE
2088 _GLIBCXX_END_NAMESPACE_CONTAINER
2091 #endif /* _STL_DEQUE_H */