1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001-2013 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()
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
)
129 : _M_cur(__x
), _M_first(*__y
),
130 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
133 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
135 _Deque_iterator(const iterator
& __x
)
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
141 { return iterator(_M_cur
, _M_node
); }
155 if (_M_cur
== _M_last
)
157 _M_set_node(_M_node
+ 1);
174 if (_M_cur
== _M_first
)
176 _M_set_node(_M_node
- 1);
192 operator+=(difference_type __n
)
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
218 operator-=(difference_type __n
)
219 { return *this += -__n
; }
222 operator-(difference_type __n
) const
229 operator[](difference_type __n
) const
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
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
)
359 { return __x
+ __n
; }
361 template<typename _Tp
>
363 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
364 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
366 template<typename _Tp
>
367 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
368 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
369 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
370 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
372 template<typename _Tp
>
373 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
374 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
375 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
376 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
377 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
378 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
381 template<typename _Tp
>
382 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
383 copy_backward(_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_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
390 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
391 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
392 { return std::copy_backward(_Deque_iterator
<_Tp
,
393 const _Tp
&, const _Tp
*>(__first
),
395 const _Tp
&, const _Tp
*>(__last
),
398 #if __cplusplus >= 201103L
399 template<typename _Tp
>
400 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
401 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
402 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
403 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
405 template<typename _Tp
>
406 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
407 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
408 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
409 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
410 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
411 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
414 template<typename _Tp
>
415 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
416 move_backward(_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_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
423 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
424 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
425 { return std::move_backward(_Deque_iterator
<_Tp
,
426 const _Tp
&, const _Tp
*>(__first
),
428 const _Tp
&, const _Tp
*>(__last
),
433 * Deque base class. This class provides the unified face for %deque's
434 * allocation. This class's constructor and destructor allocate and
435 * deallocate (but do not initialize) storage. This makes %exception
438 * Nothing in this class ever constructs or destroys an actual Tp element.
439 * (Deque handles that itself.) Only/All memory management is performed
442 template<typename _Tp
, typename _Alloc
>
446 typedef _Alloc allocator_type
;
449 get_allocator() const _GLIBCXX_NOEXCEPT
450 { return allocator_type(_M_get_Tp_allocator()); }
452 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
453 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
457 { _M_initialize_map(0); }
459 _Deque_base(size_t __num_elements
)
461 { _M_initialize_map(__num_elements
); }
463 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
465 { _M_initialize_map(__num_elements
); }
467 _Deque_base(const allocator_type
& __a
)
471 #if __cplusplus >= 201103L
472 _Deque_base(_Deque_base
&& __x
)
473 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
475 _M_initialize_map(0);
476 if (__x
._M_impl
._M_map
)
478 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
479 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
480 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
481 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
489 //This struct encapsulates the implementation of the std::deque
490 //standard container and at the same time makes use of the EBO
491 //for empty allocators.
492 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
494 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
497 : public _Tp_alloc_type
505 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
506 _M_start(), _M_finish()
509 _Deque_impl(const _Tp_alloc_type
& __a
)
510 : _Tp_alloc_type(__a
), _M_map(0), _M_map_size(0),
511 _M_start(), _M_finish()
514 #if __cplusplus >= 201103L
515 _Deque_impl(_Tp_alloc_type
&& __a
)
516 : _Tp_alloc_type(std::move(__a
)), _M_map(0), _M_map_size(0),
517 _M_start(), _M_finish()
523 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
524 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
526 const _Tp_alloc_type
&
527 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
528 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
531 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
532 { return _Map_alloc_type(_M_get_Tp_allocator()); }
537 return _M_impl
._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp
)));
541 _M_deallocate_node(_Tp
* __p
)
543 _M_impl
._Tp_alloc_type::deallocate(__p
, __deque_buf_size(sizeof(_Tp
)));
547 _M_allocate_map(size_t __n
)
548 { return _M_get_map_allocator().allocate(__n
); }
551 _M_deallocate_map(_Tp
** __p
, size_t __n
)
552 { _M_get_map_allocator().deallocate(__p
, __n
); }
555 void _M_initialize_map(size_t);
556 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
557 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
558 enum { _S_initial_map_size
= 8 };
563 template<typename _Tp
, typename _Alloc
>
564 _Deque_base
<_Tp
, _Alloc
>::
567 if (this->_M_impl
._M_map
)
569 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
570 this->_M_impl
._M_finish
._M_node
+ 1);
571 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
576 * @brief Layout storage.
577 * @param __num_elements The count of T's for which to allocate space
581 * The initial underlying memory layout is a bit complicated...
583 template<typename _Tp
, typename _Alloc
>
585 _Deque_base
<_Tp
, _Alloc
>::
586 _M_initialize_map(size_t __num_elements
)
588 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
591 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
592 size_t(__num_nodes
+ 2));
593 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
595 // For "small" maps (needing less than _M_map_size nodes), allocation
596 // starts in the middle elements and grows outwards. So nstart may be
597 // the beginning of _M_map, but for small maps it may be as far in as
600 _Tp
** __nstart
= (this->_M_impl
._M_map
601 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
602 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
605 { _M_create_nodes(__nstart
, __nfinish
); }
608 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
609 this->_M_impl
._M_map
= 0;
610 this->_M_impl
._M_map_size
= 0;
611 __throw_exception_again
;
614 this->_M_impl
._M_start
._M_set_node(__nstart
);
615 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
616 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
617 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
619 % __deque_buf_size(sizeof(_Tp
)));
622 template<typename _Tp
, typename _Alloc
>
624 _Deque_base
<_Tp
, _Alloc
>::
625 _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
630 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
631 *__cur
= this->_M_allocate_node();
635 _M_destroy_nodes(__nstart
, __cur
);
636 __throw_exception_again
;
640 template<typename _Tp
, typename _Alloc
>
642 _Deque_base
<_Tp
, _Alloc
>::
643 _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
645 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
646 _M_deallocate_node(*__n
);
650 * @brief A standard container using fixed-size memory allocation and
651 * constant-time manipulation of elements at either end.
655 * @tparam _Tp Type of element.
656 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
658 * Meets the requirements of a <a href="tables.html#65">container</a>, a
659 * <a href="tables.html#66">reversible container</a>, and a
660 * <a href="tables.html#67">sequence</a>, including the
661 * <a href="tables.html#68">optional sequence requirements</a>.
663 * In previous HP/SGI versions of deque, there was an extra template
664 * parameter so users could control the node size. This extension turned
665 * out to violate the C++ standard (it can be detected using template
666 * template parameters), and it was removed.
668 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
671 * - size_t _M_map_size
672 * - iterator _M_start, _M_finish
674 * map_size is at least 8. %map is an array of map_size
675 * pointers-to-@a nodes. (The name %map has nothing to do with the
676 * std::map class, and @b nodes should not be confused with
677 * std::list's usage of @a node.)
679 * A @a node has no specific type name as such, but it is referred
680 * to as @a node in this file. It is a simple array-of-Tp. If Tp
681 * is very large, there will be one Tp element per node (i.e., an
682 * @a array of one). For non-huge Tp's, node size is inversely
683 * related to Tp size: the larger the Tp, the fewer Tp's will fit
684 * in a node. The goal here is to keep the total size of a node
685 * relatively small and constant over different Tp's, to improve
686 * allocator efficiency.
688 * Not every pointer in the %map array will point to a node. If
689 * the initial number of elements in the deque is small, the
690 * /middle/ %map pointers will be valid, and the ones at the edges
691 * will be unused. This same situation will arise as the %map
692 * grows: available %map pointers, if any, will be on the ends. As
693 * new nodes are created, only a subset of the %map's pointers need
694 * to be copied @a outward.
697 * - For any nonsingular iterator i:
698 * - i.node points to a member of the %map array. (Yes, you read that
699 * correctly: i.node does not actually point to a node.) The member of
700 * the %map array is what actually points to the node.
701 * - i.first == *(i.node) (This points to the node (first Tp element).)
702 * - i.last == i.first + node_size
703 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
704 * the implication of this is that i.cur is always a dereferenceable
705 * pointer, even if i is a past-the-end iterator.
706 * - Start and Finish are always nonsingular iterators. NOTE: this
707 * means that an empty deque must have one node, a deque with <N
708 * elements (where N is the node buffer size) must have one node, a
709 * deque with N through (2N-1) elements must have two nodes, etc.
710 * - For every node other than start.node and finish.node, every
711 * element in the node is an initialized object. If start.node ==
712 * finish.node, then [start.cur, finish.cur) are initialized
713 * objects, and the elements outside that range are uninitialized
714 * storage. Otherwise, [start.cur, start.last) and [finish.first,
715 * finish.cur) are initialized objects, and [start.first, start.cur)
716 * and [finish.cur, finish.last) are uninitialized storage.
717 * - [%map, %map + map_size) is a valid, non-empty range.
718 * - [start.node, finish.node] is a valid range contained within
719 * [%map, %map + map_size).
720 * - A pointer in the range [%map, %map + map_size) points to an allocated
721 * node if and only if the pointer is in the range
722 * [start.node, finish.node].
724 * Here's the magic: nothing in deque is @b aware of the discontiguous
727 * The memory setup and layout occurs in the parent, _Base, and the iterator
728 * class is entirely responsible for @a leaping from one node to the next.
729 * All the implementation routines for deque itself work only through the
730 * start and finish iterators. This keeps the routines simple and sane,
731 * and we can use other standard algorithms as well.
733 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
734 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
736 // concept requirements
737 typedef typename
_Alloc::value_type _Alloc_value_type
;
738 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
739 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
741 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
742 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
745 typedef _Tp value_type
;
746 typedef typename
_Tp_alloc_type::pointer pointer
;
747 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
748 typedef typename
_Tp_alloc_type::reference reference
;
749 typedef typename
_Tp_alloc_type::const_reference const_reference
;
750 typedef typename
_Base::iterator iterator
;
751 typedef typename
_Base::const_iterator const_iterator
;
752 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
753 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
754 typedef size_t size_type
;
755 typedef ptrdiff_t difference_type
;
756 typedef _Alloc allocator_type
;
759 typedef pointer
* _Map_pointer
;
761 static size_t _S_buffer_size()
762 { return __deque_buf_size(sizeof(_Tp
)); }
764 // Functions controlling memory layout, and nothing else.
765 using _Base::_M_initialize_map
;
766 using _Base::_M_create_nodes
;
767 using _Base::_M_destroy_nodes
;
768 using _Base::_M_allocate_node
;
769 using _Base::_M_deallocate_node
;
770 using _Base::_M_allocate_map
;
771 using _Base::_M_deallocate_map
;
772 using _Base::_M_get_Tp_allocator
;
775 * A total of four data members accumulated down the hierarchy.
776 * May be accessed via _M_impl.*
778 using _Base::_M_impl
;
781 // [23.2.1.1] construct/copy/destroy
782 // (assign() and get_allocator() are also listed in this section)
784 * @brief Default constructor creates no elements.
790 * @brief Creates a %deque with no elements.
791 * @param __a An allocator object.
794 deque(const allocator_type
& __a
)
797 #if __cplusplus >= 201103L
799 * @brief Creates a %deque with default constructed elements.
800 * @param __n The number of elements to initially create.
802 * This constructor fills the %deque with @a n default
803 * constructed elements.
808 { _M_default_initialize(); }
811 * @brief Creates a %deque with copies of an exemplar element.
812 * @param __n The number of elements to initially create.
813 * @param __value An element to copy.
814 * @param __a An allocator.
816 * This constructor fills the %deque with @a __n copies of @a __value.
818 deque(size_type __n
, const value_type
& __value
,
819 const allocator_type
& __a
= allocator_type())
821 { _M_fill_initialize(__value
); }
824 * @brief Creates a %deque with copies of an exemplar element.
825 * @param __n The number of elements to initially create.
826 * @param __value An element to copy.
827 * @param __a An allocator.
829 * This constructor fills the %deque with @a __n copies of @a __value.
832 deque(size_type __n
, const value_type
& __value
= value_type(),
833 const allocator_type
& __a
= allocator_type())
835 { _M_fill_initialize(__value
); }
839 * @brief %Deque copy constructor.
840 * @param __x A %deque of identical element and allocator types.
842 * The newly-created %deque uses a copy of the allocation object used
845 deque(const deque
& __x
)
846 : _Base(__x
._M_get_Tp_allocator(), __x
.size())
847 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
848 this->_M_impl
._M_start
,
849 _M_get_Tp_allocator()); }
851 #if __cplusplus >= 201103L
853 * @brief %Deque move constructor.
854 * @param __x A %deque of identical element and allocator types.
856 * The newly-created %deque contains the exact contents of @a __x.
857 * The contents of @a __x are a valid, but unspecified %deque.
860 : _Base(std::move(__x
)) { }
863 * @brief Builds a %deque from an initializer list.
864 * @param __l An initializer_list.
865 * @param __a An allocator object.
867 * Create a %deque consisting of copies of the elements in the
868 * initializer_list @a __l.
870 * This will call the element type's copy constructor N times
871 * (where N is __l.size()) and do no memory reallocation.
873 deque(initializer_list
<value_type
> __l
,
874 const allocator_type
& __a
= allocator_type())
877 _M_range_initialize(__l
.begin(), __l
.end(),
878 random_access_iterator_tag());
883 * @brief Builds a %deque from a range.
884 * @param __first An input iterator.
885 * @param __last An input iterator.
886 * @param __a An allocator object.
888 * Create a %deque consisting of copies of the elements from [__first,
891 * If the iterators are forward, bidirectional, or random-access, then
892 * this will call the elements' copy constructor N times (where N is
893 * distance(__first,__last)) and do no memory reallocation. But if only
894 * input iterators are used, then this will do at most 2N calls to the
895 * copy constructor, and logN memory reallocations.
897 #if __cplusplus >= 201103L
898 template<typename _InputIterator
,
899 typename
= std::_RequireInputIter
<_InputIterator
>>
900 deque(_InputIterator __first
, _InputIterator __last
,
901 const allocator_type
& __a
= allocator_type())
903 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
905 template<typename _InputIterator
>
906 deque(_InputIterator __first
, _InputIterator __last
,
907 const allocator_type
& __a
= allocator_type())
910 // Check whether it's an integral type. If so, it's not an iterator.
911 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
912 _M_initialize_dispatch(__first
, __last
, _Integral());
917 * The dtor only erases the elements, and note that if the elements
918 * themselves are pointers, the pointed-to memory is not touched in any
919 * way. Managing the pointer is the user's responsibility.
921 ~deque() _GLIBCXX_NOEXCEPT
922 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
925 * @brief %Deque assignment operator.
926 * @param __x A %deque of identical element and allocator types.
928 * All the elements of @a x are copied, but unlike the copy constructor,
929 * the allocator object is not copied.
932 operator=(const deque
& __x
);
934 #if __cplusplus >= 201103L
936 * @brief %Deque move assignment operator.
937 * @param __x A %deque of identical element and allocator types.
939 * The contents of @a __x are moved into this deque (without copying).
940 * @a __x is a valid, but unspecified %deque.
943 operator=(deque
&& __x
)
953 * @brief Assigns an initializer list to a %deque.
954 * @param __l An initializer_list.
956 * This function fills a %deque with copies of the elements in the
957 * initializer_list @a __l.
959 * Note that the assignment completely changes the %deque and that the
960 * resulting %deque's size is the same as the number of elements
961 * assigned. Old data may be lost.
964 operator=(initializer_list
<value_type
> __l
)
966 this->assign(__l
.begin(), __l
.end());
972 * @brief Assigns a given value to a %deque.
973 * @param __n Number of elements to be assigned.
974 * @param __val Value to be assigned.
976 * This function fills a %deque with @a n copies of the given
977 * value. Note that the assignment completely changes the
978 * %deque and that the resulting %deque's size is the same as
979 * the number of elements assigned. Old data may be lost.
982 assign(size_type __n
, const value_type
& __val
)
983 { _M_fill_assign(__n
, __val
); }
986 * @brief Assigns a range to a %deque.
987 * @param __first An input iterator.
988 * @param __last An input iterator.
990 * This function fills a %deque with copies of the elements in the
991 * range [__first,__last).
993 * Note that the assignment completely changes the %deque and that the
994 * resulting %deque's size is the same as the number of elements
995 * assigned. Old data may be lost.
997 #if __cplusplus >= 201103L
998 template<typename _InputIterator
,
999 typename
= std::_RequireInputIter
<_InputIterator
>>
1001 assign(_InputIterator __first
, _InputIterator __last
)
1002 { _M_assign_dispatch(__first
, __last
, __false_type()); }
1004 template<typename _InputIterator
>
1006 assign(_InputIterator __first
, _InputIterator __last
)
1008 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1009 _M_assign_dispatch(__first
, __last
, _Integral());
1013 #if __cplusplus >= 201103L
1015 * @brief Assigns an initializer list to a %deque.
1016 * @param __l An initializer_list.
1018 * This function fills a %deque with copies of the elements in the
1019 * initializer_list @a __l.
1021 * Note that the assignment completely changes the %deque and that the
1022 * resulting %deque's size is the same as the number of elements
1023 * assigned. Old data may be lost.
1026 assign(initializer_list
<value_type
> __l
)
1027 { this->assign(__l
.begin(), __l
.end()); }
1030 /// Get a copy of the memory allocation object.
1032 get_allocator() const _GLIBCXX_NOEXCEPT
1033 { return _Base::get_allocator(); }
1037 * Returns a read/write iterator that points to the first element in the
1038 * %deque. Iteration is done in ordinary element order.
1041 begin() _GLIBCXX_NOEXCEPT
1042 { return this->_M_impl
._M_start
; }
1045 * Returns a read-only (constant) iterator that points to the first
1046 * element in the %deque. Iteration is done in ordinary element order.
1049 begin() const _GLIBCXX_NOEXCEPT
1050 { return this->_M_impl
._M_start
; }
1053 * Returns a read/write iterator that points one past the last
1054 * element in the %deque. Iteration is done in ordinary
1058 end() _GLIBCXX_NOEXCEPT
1059 { return this->_M_impl
._M_finish
; }
1062 * Returns a read-only (constant) iterator that points one past
1063 * the last element in the %deque. Iteration is done in
1064 * ordinary element order.
1067 end() const _GLIBCXX_NOEXCEPT
1068 { return this->_M_impl
._M_finish
; }
1071 * Returns a read/write reverse iterator that points to the
1072 * last element in the %deque. Iteration is done in reverse
1076 rbegin() _GLIBCXX_NOEXCEPT
1077 { return reverse_iterator(this->_M_impl
._M_finish
); }
1080 * Returns a read-only (constant) reverse iterator that points
1081 * to the last element in the %deque. Iteration is done in
1082 * reverse element order.
1084 const_reverse_iterator
1085 rbegin() const _GLIBCXX_NOEXCEPT
1086 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1089 * Returns a read/write reverse iterator that points to one
1090 * before the first element in the %deque. Iteration is done
1091 * in reverse element order.
1094 rend() _GLIBCXX_NOEXCEPT
1095 { return reverse_iterator(this->_M_impl
._M_start
); }
1098 * Returns a read-only (constant) reverse iterator that points
1099 * to one before the first element in the %deque. Iteration is
1100 * done in reverse element order.
1102 const_reverse_iterator
1103 rend() const _GLIBCXX_NOEXCEPT
1104 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1106 #if __cplusplus >= 201103L
1108 * Returns a read-only (constant) iterator that points to the first
1109 * element in the %deque. Iteration is done in ordinary element order.
1112 cbegin() const noexcept
1113 { return this->_M_impl
._M_start
; }
1116 * Returns a read-only (constant) iterator that points one past
1117 * the last element in the %deque. Iteration is done in
1118 * ordinary element order.
1121 cend() const noexcept
1122 { return this->_M_impl
._M_finish
; }
1125 * Returns a read-only (constant) reverse iterator that points
1126 * to the last element in the %deque. Iteration is done in
1127 * reverse element order.
1129 const_reverse_iterator
1130 crbegin() const noexcept
1131 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1134 * Returns a read-only (constant) reverse iterator that points
1135 * to one before the first element in the %deque. Iteration is
1136 * done in reverse element order.
1138 const_reverse_iterator
1139 crend() const noexcept
1140 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1143 // [23.2.1.2] capacity
1144 /** Returns the number of elements in the %deque. */
1146 size() const _GLIBCXX_NOEXCEPT
1147 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1149 /** Returns the size() of the largest possible %deque. */
1151 max_size() const _GLIBCXX_NOEXCEPT
1152 { return _M_get_Tp_allocator().max_size(); }
1154 #if __cplusplus >= 201103L
1156 * @brief Resizes the %deque to the specified number of elements.
1157 * @param __new_size Number of elements the %deque should contain.
1159 * This function will %resize the %deque to the specified
1160 * number of elements. If the number is smaller than the
1161 * %deque's current size the %deque is truncated, otherwise
1162 * default constructed elements are appended.
1165 resize(size_type __new_size
)
1167 const size_type __len
= size();
1168 if (__new_size
> __len
)
1169 _M_default_append(__new_size
- __len
);
1170 else if (__new_size
< __len
)
1171 _M_erase_at_end(this->_M_impl
._M_start
1172 + difference_type(__new_size
));
1176 * @brief Resizes the %deque to the specified number of elements.
1177 * @param __new_size Number of elements the %deque should contain.
1178 * @param __x Data with which new elements should be populated.
1180 * This function will %resize the %deque to the specified
1181 * number of elements. If the number is smaller than the
1182 * %deque's current size the %deque is truncated, otherwise the
1183 * %deque is extended and new elements are populated with given
1187 resize(size_type __new_size
, const value_type
& __x
)
1189 const size_type __len
= size();
1190 if (__new_size
> __len
)
1191 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1192 else if (__new_size
< __len
)
1193 _M_erase_at_end(this->_M_impl
._M_start
1194 + difference_type(__new_size
));
1198 * @brief Resizes the %deque to the specified number of elements.
1199 * @param __new_size Number of elements the %deque should contain.
1200 * @param __x Data with which new elements should be populated.
1202 * This function will %resize the %deque to the specified
1203 * number of elements. If the number is smaller than the
1204 * %deque's current size the %deque is truncated, otherwise the
1205 * %deque is extended and new elements are populated with given
1209 resize(size_type __new_size
, value_type __x
= value_type())
1211 const size_type __len
= size();
1212 if (__new_size
> __len
)
1213 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1214 else if (__new_size
< __len
)
1215 _M_erase_at_end(this->_M_impl
._M_start
1216 + difference_type(__new_size
));
1220 #if __cplusplus >= 201103L
1221 /** A non-binding request to reduce memory use. */
1224 { _M_shrink_to_fit(); }
1228 * Returns true if the %deque is empty. (Thus begin() would
1232 empty() const _GLIBCXX_NOEXCEPT
1233 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1237 * @brief Subscript access to the data contained in the %deque.
1238 * @param __n The index of the element for which data should be
1240 * @return Read/write reference to data.
1242 * This operator allows for easy, array-style, data access.
1243 * Note that data access with this operator is unchecked and
1244 * out_of_range lookups are not defined. (For checked lookups
1248 operator[](size_type __n
)
1249 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1252 * @brief Subscript access to the data contained in the %deque.
1253 * @param __n The index of the element for which data should be
1255 * @return Read-only (constant) reference to data.
1257 * This operator allows for easy, array-style, data access.
1258 * Note that data access with this operator is unchecked and
1259 * out_of_range lookups are not defined. (For checked lookups
1263 operator[](size_type __n
) const
1264 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1267 /// Safety check used only from at().
1269 _M_range_check(size_type __n
) const
1271 if (__n
>= this->size())
1272 __throw_out_of_range(__N("deque::_M_range_check"));
1277 * @brief Provides access to the data contained in the %deque.
1278 * @param __n The index of the element for which data should be
1280 * @return Read/write reference to data.
1281 * @throw std::out_of_range If @a __n is an invalid index.
1283 * This function provides for safer data access. The parameter
1284 * is first checked that it is in the range of the deque. The
1285 * function throws out_of_range if the check fails.
1290 _M_range_check(__n
);
1291 return (*this)[__n
];
1295 * @brief Provides access to the data contained in the %deque.
1296 * @param __n The index of the element for which data should be
1298 * @return Read-only (constant) reference to data.
1299 * @throw std::out_of_range If @a __n is an invalid index.
1301 * This function provides for safer data access. The parameter is first
1302 * checked that it is in the range of the deque. The function throws
1303 * out_of_range if the check fails.
1306 at(size_type __n
) const
1308 _M_range_check(__n
);
1309 return (*this)[__n
];
1313 * Returns a read/write reference to the data at the first
1314 * element of the %deque.
1318 { return *begin(); }
1321 * Returns a read-only (constant) reference to the data at the first
1322 * element of the %deque.
1326 { return *begin(); }
1329 * Returns a read/write reference to the data at the last element of the
1335 iterator __tmp
= end();
1341 * Returns a read-only (constant) reference to the data at the last
1342 * element of the %deque.
1347 const_iterator __tmp
= end();
1352 // [23.2.1.2] modifiers
1354 * @brief Add data to the front of the %deque.
1355 * @param __x Data to be added.
1357 * This is a typical stack operation. The function creates an
1358 * element at the front of the %deque and assigns the given
1359 * data to it. Due to the nature of a %deque this operation
1360 * can be done in constant time.
1363 push_front(const value_type
& __x
)
1365 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1367 this->_M_impl
.construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
1368 --this->_M_impl
._M_start
._M_cur
;
1371 _M_push_front_aux(__x
);
1374 #if __cplusplus >= 201103L
1376 push_front(value_type
&& __x
)
1377 { emplace_front(std::move(__x
)); }
1379 template<typename
... _Args
>
1381 emplace_front(_Args
&&... __args
);
1385 * @brief Add data to the end of the %deque.
1386 * @param __x Data to be added.
1388 * This is a typical stack operation. The function creates an
1389 * element at the end of the %deque and assigns the given data
1390 * to it. Due to the nature of a %deque this operation can be
1391 * done in constant time.
1394 push_back(const value_type
& __x
)
1396 if (this->_M_impl
._M_finish
._M_cur
1397 != this->_M_impl
._M_finish
._M_last
- 1)
1399 this->_M_impl
.construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1400 ++this->_M_impl
._M_finish
._M_cur
;
1403 _M_push_back_aux(__x
);
1406 #if __cplusplus >= 201103L
1408 push_back(value_type
&& __x
)
1409 { emplace_back(std::move(__x
)); }
1411 template<typename
... _Args
>
1413 emplace_back(_Args
&&... __args
);
1417 * @brief Removes first element.
1419 * This is a typical stack operation. It shrinks the %deque by one.
1421 * Note that no data is returned, and if the first element's data is
1422 * needed, it should be retrieved before pop_front() is called.
1427 if (this->_M_impl
._M_start
._M_cur
1428 != this->_M_impl
._M_start
._M_last
- 1)
1430 this->_M_impl
.destroy(this->_M_impl
._M_start
._M_cur
);
1431 ++this->_M_impl
._M_start
._M_cur
;
1438 * @brief Removes last element.
1440 * This is a typical stack operation. It shrinks the %deque by one.
1442 * Note that no data is returned, and if the last element's data is
1443 * needed, it should be retrieved before pop_back() is called.
1448 if (this->_M_impl
._M_finish
._M_cur
1449 != this->_M_impl
._M_finish
._M_first
)
1451 --this->_M_impl
._M_finish
._M_cur
;
1452 this->_M_impl
.destroy(this->_M_impl
._M_finish
._M_cur
);
1458 #if __cplusplus >= 201103L
1460 * @brief Inserts an object in %deque before specified iterator.
1461 * @param __position An iterator into the %deque.
1462 * @param __args Arguments.
1463 * @return An iterator that points to the inserted data.
1465 * This function will insert an object of type T constructed
1466 * with T(std::forward<Args>(args)...) before the specified location.
1468 template<typename
... _Args
>
1470 emplace(iterator __position
, _Args
&&... __args
);
1474 * @brief Inserts given value into %deque before specified iterator.
1475 * @param __position An iterator into the %deque.
1476 * @param __x Data to be inserted.
1477 * @return An iterator that points to the inserted data.
1479 * This function will insert a copy of the given value before the
1480 * specified location.
1483 insert(iterator __position
, const value_type
& __x
);
1485 #if __cplusplus >= 201103L
1487 * @brief Inserts given rvalue into %deque before specified iterator.
1488 * @param __position An iterator into the %deque.
1489 * @param __x Data to be inserted.
1490 * @return An iterator that points to the inserted data.
1492 * This function will insert a copy of the given rvalue before the
1493 * specified location.
1496 insert(iterator __position
, value_type
&& __x
)
1497 { return emplace(__position
, std::move(__x
)); }
1500 * @brief Inserts an initializer list into the %deque.
1501 * @param __p An iterator into the %deque.
1502 * @param __l An initializer_list.
1504 * This function will insert copies of the data in the
1505 * initializer_list @a __l into the %deque before the location
1506 * specified by @a __p. This is known as <em>list insert</em>.
1509 insert(iterator __p
, initializer_list
<value_type
> __l
)
1510 { this->insert(__p
, __l
.begin(), __l
.end()); }
1514 * @brief Inserts a number of copies of given data into the %deque.
1515 * @param __position An iterator into the %deque.
1516 * @param __n Number of elements to be inserted.
1517 * @param __x Data to be inserted.
1519 * This function will insert a specified number of copies of the given
1520 * data before the location specified by @a __position.
1523 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1524 { _M_fill_insert(__position
, __n
, __x
); }
1527 * @brief Inserts a range into the %deque.
1528 * @param __position An iterator into the %deque.
1529 * @param __first An input iterator.
1530 * @param __last An input iterator.
1532 * This function will insert copies of the data in the range
1533 * [__first,__last) into the %deque before the location specified
1534 * by @a __position. This is known as <em>range insert</em>.
1536 #if __cplusplus >= 201103L
1537 template<typename _InputIterator
,
1538 typename
= std::_RequireInputIter
<_InputIterator
>>
1540 insert(iterator __position
, _InputIterator __first
,
1541 _InputIterator __last
)
1542 { _M_insert_dispatch(__position
, __first
, __last
, __false_type()); }
1544 template<typename _InputIterator
>
1546 insert(iterator __position
, _InputIterator __first
,
1547 _InputIterator __last
)
1549 // Check whether it's an integral type. If so, it's not an iterator.
1550 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1551 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1556 * @brief Remove element at given position.
1557 * @param __position Iterator pointing to element to be erased.
1558 * @return An iterator pointing to the next element (or end()).
1560 * This function will erase the element at the given position and thus
1561 * shorten the %deque by one.
1563 * The user is cautioned that
1564 * this function only erases the element, and that if the element is
1565 * itself a pointer, the pointed-to memory is not touched in any way.
1566 * Managing the pointer is the user's responsibility.
1569 #if __cplusplus >= 201103L
1570 erase(const_iterator __position
)
1571 { return _M_erase(__position
._M_const_cast()); }
1573 erase(iterator __position
)
1574 { return _M_erase(__position
); }
1578 * @brief Remove a range of elements.
1579 * @param __first Iterator pointing to the first element to be erased.
1580 * @param __last Iterator pointing to one past the last element to be
1582 * @return An iterator pointing to the element pointed to by @a last
1583 * prior to erasing (or end()).
1585 * This function will erase the elements in the range
1586 * [__first,__last) and shorten the %deque accordingly.
1588 * The user is cautioned that
1589 * this function only erases the elements, and that if the elements
1590 * themselves are pointers, the pointed-to memory is not touched in any
1591 * way. Managing the pointer is the user's responsibility.
1594 #if __cplusplus >= 201103L
1595 erase(const_iterator __first
, const_iterator __last
)
1596 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1598 erase(iterator __first
, iterator __last
)
1599 { return _M_erase(__first
, __last
); }
1603 * @brief Swaps data with another %deque.
1604 * @param __x A %deque of the same element and allocator types.
1606 * This exchanges the elements between two deques in constant time.
1607 * (Four pointers, so it should be quite fast.)
1608 * Note that the global std::swap() function is specialized such that
1609 * std::swap(d1,d2) will feed to this function.
1614 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1615 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1616 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1617 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1619 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1620 // 431. Swapping containers with unequal allocators.
1621 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1622 __x
._M_get_Tp_allocator());
1626 * Erases all the elements. Note that this function only erases the
1627 * elements, and that if the elements themselves are pointers, the
1628 * pointed-to memory is not touched in any way. Managing the pointer is
1629 * the user's responsibility.
1632 clear() _GLIBCXX_NOEXCEPT
1633 { _M_erase_at_end(begin()); }
1636 // Internal constructor functions follow.
1638 // called by the range constructor to implement [23.1.1]/9
1640 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1641 // 438. Ambiguity in the "do the right thing" clause
1642 template<typename _Integer
>
1644 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1646 _M_initialize_map(static_cast<size_type
>(__n
));
1647 _M_fill_initialize(__x
);
1650 // called by the range constructor to implement [23.1.1]/9
1651 template<typename _InputIterator
>
1653 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1656 typedef typename
std::iterator_traits
<_InputIterator
>::
1657 iterator_category _IterCategory
;
1658 _M_range_initialize(__first
, __last
, _IterCategory());
1661 // called by the second initialize_dispatch above
1664 * @brief Fills the deque with whatever is in [first,last).
1665 * @param __first An input iterator.
1666 * @param __last An input iterator.
1669 * If the iterators are actually forward iterators (or better), then the
1670 * memory layout can be done all at once. Else we move forward using
1671 * push_back on each value from the iterator.
1673 template<typename _InputIterator
>
1675 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1676 std::input_iterator_tag
);
1678 // called by the second initialize_dispatch above
1679 template<typename _ForwardIterator
>
1681 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1682 std::forward_iterator_tag
);
1686 * @brief Fills the %deque with copies of value.
1687 * @param __value Initial value.
1689 * @pre _M_start and _M_finish have already been initialized,
1690 * but none of the %deque's elements have yet been constructed.
1692 * This function is called only when the user provides an explicit size
1693 * (with or without an explicit exemplar value).
1696 _M_fill_initialize(const value_type
& __value
);
1698 #if __cplusplus >= 201103L
1699 // called by deque(n).
1701 _M_default_initialize();
1704 // Internal assign functions follow. The *_aux functions do the actual
1705 // assignment work for the range versions.
1707 // called by the range assign to implement [23.1.1]/9
1709 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1710 // 438. Ambiguity in the "do the right thing" clause
1711 template<typename _Integer
>
1713 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1714 { _M_fill_assign(__n
, __val
); }
1716 // called by the range assign to implement [23.1.1]/9
1717 template<typename _InputIterator
>
1719 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1722 typedef typename
std::iterator_traits
<_InputIterator
>::
1723 iterator_category _IterCategory
;
1724 _M_assign_aux(__first
, __last
, _IterCategory());
1727 // called by the second assign_dispatch above
1728 template<typename _InputIterator
>
1730 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1731 std::input_iterator_tag
);
1733 // called by the second assign_dispatch above
1734 template<typename _ForwardIterator
>
1736 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1737 std::forward_iterator_tag
)
1739 const size_type __len
= std::distance(__first
, __last
);
1742 _ForwardIterator __mid
= __first
;
1743 std::advance(__mid
, size());
1744 std::copy(__first
, __mid
, begin());
1745 insert(end(), __mid
, __last
);
1748 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1751 // Called by assign(n,t), and the range assign when it turns out
1752 // to be the same thing.
1754 _M_fill_assign(size_type __n
, const value_type
& __val
)
1758 std::fill(begin(), end(), __val
);
1759 insert(end(), __n
- size(), __val
);
1763 _M_erase_at_end(begin() + difference_type(__n
));
1764 std::fill(begin(), end(), __val
);
1769 /// Helper functions for push_* and pop_*.
1770 #if __cplusplus < 201103L
1771 void _M_push_back_aux(const value_type
&);
1773 void _M_push_front_aux(const value_type
&);
1775 template<typename
... _Args
>
1776 void _M_push_back_aux(_Args
&&... __args
);
1778 template<typename
... _Args
>
1779 void _M_push_front_aux(_Args
&&... __args
);
1782 void _M_pop_back_aux();
1784 void _M_pop_front_aux();
1787 // Internal insert functions follow. The *_aux functions do the actual
1788 // insertion work when all shortcuts fail.
1790 // called by the range insert to implement [23.1.1]/9
1792 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1793 // 438. Ambiguity in the "do the right thing" clause
1794 template<typename _Integer
>
1796 _M_insert_dispatch(iterator __pos
,
1797 _Integer __n
, _Integer __x
, __true_type
)
1798 { _M_fill_insert(__pos
, __n
, __x
); }
1800 // called by the range insert to implement [23.1.1]/9
1801 template<typename _InputIterator
>
1803 _M_insert_dispatch(iterator __pos
,
1804 _InputIterator __first
, _InputIterator __last
,
1807 typedef typename
std::iterator_traits
<_InputIterator
>::
1808 iterator_category _IterCategory
;
1809 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1812 // called by the second insert_dispatch above
1813 template<typename _InputIterator
>
1815 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1816 _InputIterator __last
, std::input_iterator_tag
);
1818 // called by the second insert_dispatch above
1819 template<typename _ForwardIterator
>
1821 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1822 _ForwardIterator __last
, std::forward_iterator_tag
);
1824 // Called by insert(p,n,x), and the range insert when it turns out to be
1825 // the same thing. Can use fill functions in optimal situations,
1826 // otherwise passes off to insert_aux(p,n,x).
1828 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1830 // called by insert(p,x)
1831 #if __cplusplus < 201103L
1833 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1835 template<typename
... _Args
>
1837 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
1840 // called by insert(p,n,x) via fill_insert
1842 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1844 // called by range_insert_aux for forward iterators
1845 template<typename _ForwardIterator
>
1847 _M_insert_aux(iterator __pos
,
1848 _ForwardIterator __first
, _ForwardIterator __last
,
1852 // Internal erase functions follow.
1855 _M_destroy_data_aux(iterator __first
, iterator __last
);
1857 // Called by ~deque().
1858 // NB: Doesn't deallocate the nodes.
1859 template<typename _Alloc1
>
1861 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
1862 { _M_destroy_data_aux(__first
, __last
); }
1865 _M_destroy_data(iterator __first
, iterator __last
,
1866 const std::allocator
<_Tp
>&)
1868 if (!__has_trivial_destructor(value_type
))
1869 _M_destroy_data_aux(__first
, __last
);
1872 // Called by erase(q1, q2).
1874 _M_erase_at_begin(iterator __pos
)
1876 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
1877 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
1878 this->_M_impl
._M_start
= __pos
;
1881 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1882 // _M_fill_assign, operator=.
1884 _M_erase_at_end(iterator __pos
)
1886 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
1887 _M_destroy_nodes(__pos
._M_node
+ 1,
1888 this->_M_impl
._M_finish
._M_node
+ 1);
1889 this->_M_impl
._M_finish
= __pos
;
1893 _M_erase(iterator __pos
);
1896 _M_erase(iterator __first
, iterator __last
);
1898 #if __cplusplus >= 201103L
1899 // Called by resize(sz).
1901 _M_default_append(size_type __n
);
1908 /// Memory-handling helpers for the previous internal insert functions.
1910 _M_reserve_elements_at_front(size_type __n
)
1912 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1913 - this->_M_impl
._M_start
._M_first
;
1914 if (__n
> __vacancies
)
1915 _M_new_elements_at_front(__n
- __vacancies
);
1916 return this->_M_impl
._M_start
- difference_type(__n
);
1920 _M_reserve_elements_at_back(size_type __n
)
1922 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1923 - this->_M_impl
._M_finish
._M_cur
) - 1;
1924 if (__n
> __vacancies
)
1925 _M_new_elements_at_back(__n
- __vacancies
);
1926 return this->_M_impl
._M_finish
+ difference_type(__n
);
1930 _M_new_elements_at_front(size_type __new_elements
);
1933 _M_new_elements_at_back(size_type __new_elements
);
1939 * @brief Memory-handling helpers for the major %map.
1941 * Makes sure the _M_map has space for new nodes. Does not
1942 * actually add the nodes. Can invalidate _M_map pointers.
1943 * (And consequently, %deque iterators.)
1946 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
1948 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1949 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
1950 _M_reallocate_map(__nodes_to_add
, false);
1954 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
1956 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
1957 - this->_M_impl
._M_map
))
1958 _M_reallocate_map(__nodes_to_add
, true);
1962 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
1968 * @brief Deque equality comparison.
1969 * @param __x A %deque.
1970 * @param __y A %deque of the same type as @a __x.
1971 * @return True iff the size and elements of the deques are equal.
1973 * This is an equivalence relation. It is linear in the size of the
1974 * deques. Deques are considered equivalent if their sizes are equal,
1975 * and if corresponding elements compare equal.
1977 template<typename _Tp
, typename _Alloc
>
1979 operator==(const deque
<_Tp
, _Alloc
>& __x
,
1980 const deque
<_Tp
, _Alloc
>& __y
)
1981 { return __x
.size() == __y
.size()
1982 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
1985 * @brief Deque ordering relation.
1986 * @param __x A %deque.
1987 * @param __y A %deque of the same type as @a __x.
1988 * @return True iff @a x is lexicographically less than @a __y.
1990 * This is a total ordering relation. It is linear in the size of the
1991 * deques. The elements must be comparable with @c <.
1993 * See std::lexicographical_compare() for how the determination is made.
1995 template<typename _Tp
, typename _Alloc
>
1997 operator<(const deque
<_Tp
, _Alloc
>& __x
,
1998 const deque
<_Tp
, _Alloc
>& __y
)
1999 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2000 __y
.begin(), __y
.end()); }
2002 /// Based on operator==
2003 template<typename _Tp
, typename _Alloc
>
2005 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
2006 const deque
<_Tp
, _Alloc
>& __y
)
2007 { return !(__x
== __y
); }
2009 /// Based on operator<
2010 template<typename _Tp
, typename _Alloc
>
2012 operator>(const deque
<_Tp
, _Alloc
>& __x
,
2013 const deque
<_Tp
, _Alloc
>& __y
)
2014 { return __y
< __x
; }
2016 /// Based on operator<
2017 template<typename _Tp
, typename _Alloc
>
2019 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
2020 const deque
<_Tp
, _Alloc
>& __y
)
2021 { return !(__y
< __x
); }
2023 /// Based on operator<
2024 template<typename _Tp
, typename _Alloc
>
2026 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
2027 const deque
<_Tp
, _Alloc
>& __y
)
2028 { return !(__x
< __y
); }
2030 /// See std::deque::swap().
2031 template<typename _Tp
, typename _Alloc
>
2033 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
2036 #undef _GLIBCXX_DEQUE_BUF_SIZE
2038 _GLIBCXX_END_NAMESPACE_CONTAINER
2041 #endif /* _STL_DEQUE_H */