1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file bits/stl_deque.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{deque}
58 #define _STL_DEQUE_H 1
60 #include <bits/concept_check.h>
61 #include <bits/stl_iterator_base_types.h>
62 #include <bits/stl_iterator_base_funcs.h>
63 #include <initializer_list>
65 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
70 * @brief This function controls the size of memory nodes.
71 * @param size The size of an element.
72 * @return The number (not byte size) of elements per node.
74 * This function started off as a compiler kludge from SGI, but
75 * seems to be a useful wrapper around a repeated constant
76 * expression. The @b 512 is tunable (and no other code needs to
77 * change), but no investigation has been done since inheriting the
78 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
79 * you are doing, however: changing it breaks the binary
83 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
84 #define _GLIBCXX_DEQUE_BUF_SIZE 512
88 __deque_buf_size(size_t __size
)
89 { return (__size
< _GLIBCXX_DEQUE_BUF_SIZE
90 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE
/ __size
) : size_t(1)); }
94 * @brief A deque::iterator.
96 * Quite a bit of intelligence here. Much of the functionality of
97 * deque is actually passed off to this class. A deque holds two
98 * of these internally, marking its valid range. Access to
99 * elements is done as offsets of either of those two, relying on
100 * operator overloading in this class.
102 * All the functions are op overloads except for _M_set_node.
104 template<typename _Tp
, typename _Ref
, typename _Ptr
>
105 struct _Deque_iterator
107 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
108 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
110 static size_t _S_buffer_size()
111 { return __deque_buf_size(sizeof(_Tp
)); }
113 typedef std::random_access_iterator_tag iterator_category
;
114 typedef _Tp value_type
;
115 typedef _Ptr pointer
;
116 typedef _Ref reference
;
117 typedef size_t size_type
;
118 typedef ptrdiff_t difference_type
;
119 typedef _Tp
** _Map_pointer
;
120 typedef _Deque_iterator _Self
;
125 _Map_pointer _M_node
;
127 _Deque_iterator(_Tp
* __x
, _Map_pointer __y
)
128 : _M_cur(__x
), _M_first(*__y
),
129 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
132 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
134 _Deque_iterator(const iterator
& __x
)
135 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
136 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
150 if (_M_cur
== _M_last
)
152 _M_set_node(_M_node
+ 1);
169 if (_M_cur
== _M_first
)
171 _M_set_node(_M_node
- 1);
187 operator+=(difference_type __n
)
189 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
190 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
194 const difference_type __node_offset
=
195 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
196 : -difference_type((-__offset
- 1)
197 / _S_buffer_size()) - 1;
198 _M_set_node(_M_node
+ __node_offset
);
199 _M_cur
= _M_first
+ (__offset
- __node_offset
200 * difference_type(_S_buffer_size()));
206 operator+(difference_type __n
) const
213 operator-=(difference_type __n
)
214 { return *this += -__n
; }
217 operator-(difference_type __n
) const
224 operator[](difference_type __n
) const
225 { return *(*this + __n
); }
228 * Prepares to traverse new_node. Sets everything except
229 * _M_cur, which should therefore be set by the caller
230 * immediately afterwards, based on _M_first and _M_last.
233 _M_set_node(_Map_pointer __new_node
)
235 _M_node
= __new_node
;
236 _M_first
= *__new_node
;
237 _M_last
= _M_first
+ difference_type(_S_buffer_size());
241 // Note: we also provide overloads whose operands are of the same type in
242 // order to avoid ambiguous overload resolution when std::rel_ops operators
243 // are in scope (for additional details, see libstdc++/3628)
244 template<typename _Tp
, typename _Ref
, typename _Ptr
>
246 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
247 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
248 { return __x
._M_cur
== __y
._M_cur
; }
250 template<typename _Tp
, typename _RefL
, typename _PtrL
,
251 typename _RefR
, typename _PtrR
>
253 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
254 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
255 { return __x
._M_cur
== __y
._M_cur
; }
257 template<typename _Tp
, typename _Ref
, typename _Ptr
>
259 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
260 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
261 { return !(__x
== __y
); }
263 template<typename _Tp
, typename _RefL
, typename _PtrL
,
264 typename _RefR
, typename _PtrR
>
266 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
267 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
268 { return !(__x
== __y
); }
270 template<typename _Tp
, typename _Ref
, typename _Ptr
>
272 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
273 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
274 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
275 : (__x
._M_node
< __y
._M_node
); }
277 template<typename _Tp
, typename _RefL
, typename _PtrL
,
278 typename _RefR
, typename _PtrR
>
280 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
281 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
282 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
283 : (__x
._M_node
< __y
._M_node
); }
285 template<typename _Tp
, typename _Ref
, typename _Ptr
>
287 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
288 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
289 { return __y
< __x
; }
291 template<typename _Tp
, typename _RefL
, typename _PtrL
,
292 typename _RefR
, typename _PtrR
>
294 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
295 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
296 { return __y
< __x
; }
298 template<typename _Tp
, typename _Ref
, typename _Ptr
>
300 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
301 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
302 { return !(__y
< __x
); }
304 template<typename _Tp
, typename _RefL
, typename _PtrL
,
305 typename _RefR
, typename _PtrR
>
307 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
308 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
309 { return !(__y
< __x
); }
311 template<typename _Tp
, typename _Ref
, typename _Ptr
>
313 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
314 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
315 { return !(__x
< __y
); }
317 template<typename _Tp
, typename _RefL
, typename _PtrL
,
318 typename _RefR
, typename _PtrR
>
320 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
321 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
322 { return !(__x
< __y
); }
324 // _GLIBCXX_RESOLVE_LIB_DEFECTS
325 // According to the resolution of DR179 not only the various comparison
326 // operators but also operator- must accept mixed iterator/const_iterator
328 template<typename _Tp
, typename _Ref
, typename _Ptr
>
329 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
330 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
331 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
333 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
334 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
335 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
336 + (__y
._M_last
- __y
._M_cur
);
339 template<typename _Tp
, typename _RefL
, typename _PtrL
,
340 typename _RefR
, typename _PtrR
>
341 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
342 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
343 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
345 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
346 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
347 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
348 + (__y
._M_last
- __y
._M_cur
);
351 template<typename _Tp
, typename _Ref
, typename _Ptr
>
352 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
353 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
354 { return __x
+ __n
; }
356 template<typename _Tp
>
358 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
359 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
361 template<typename _Tp
>
362 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
363 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
364 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
365 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
367 template<typename _Tp
>
368 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
369 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
370 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
371 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
372 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
373 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
376 template<typename _Tp
>
377 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
378 copy_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
379 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
380 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
382 template<typename _Tp
>
383 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
384 copy_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
385 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
386 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
387 { return std::copy_backward(_Deque_iterator
<_Tp
,
388 const _Tp
&, const _Tp
*>(__first
),
390 const _Tp
&, const _Tp
*>(__last
),
393 #ifdef __GXX_EXPERIMENTAL_CXX0X__
394 template<typename _Tp
>
395 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
396 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
397 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
398 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
400 template<typename _Tp
>
401 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
402 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
403 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
404 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
405 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
406 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
409 template<typename _Tp
>
410 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
411 move_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
412 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
413 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
415 template<typename _Tp
>
416 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
417 move_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
418 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
419 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
420 { return std::move_backward(_Deque_iterator
<_Tp
,
421 const _Tp
&, const _Tp
*>(__first
),
423 const _Tp
&, const _Tp
*>(__last
),
428 * Deque base class. This class provides the unified face for %deque's
429 * allocation. This class's constructor and destructor allocate and
430 * deallocate (but do not initialize) storage. This makes %exception
433 * Nothing in this class ever constructs or destroys an actual Tp element.
434 * (Deque handles that itself.) Only/All memory management is performed
437 template<typename _Tp
, typename _Alloc
>
441 typedef _Alloc allocator_type
;
444 get_allocator() const _GLIBCXX_NOEXCEPT
445 { return allocator_type(_M_get_Tp_allocator()); }
447 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
448 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
452 { _M_initialize_map(0); }
454 _Deque_base(size_t __num_elements
)
456 { _M_initialize_map(__num_elements
); }
458 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
460 { _M_initialize_map(__num_elements
); }
462 _Deque_base(const allocator_type
& __a
)
466 #ifdef __GXX_EXPERIMENTAL_CXX0X__
467 _Deque_base(_Deque_base
&& __x
)
468 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
470 _M_initialize_map(0);
471 if (__x
._M_impl
._M_map
)
473 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
474 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
475 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
476 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
484 //This struct encapsulates the implementation of the std::deque
485 //standard container and at the same time makes use of the EBO
486 //for empty allocators.
487 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
489 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
492 : public _Tp_alloc_type
500 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
501 _M_start(), _M_finish()
504 _Deque_impl(const _Tp_alloc_type
& __a
)
505 : _Tp_alloc_type(__a
), _M_map(0), _M_map_size(0),
506 _M_start(), _M_finish()
509 #ifdef __GXX_EXPERIMENTAL_CXX0X__
510 _Deque_impl(_Tp_alloc_type
&& __a
)
511 : _Tp_alloc_type(std::move(__a
)), _M_map(0), _M_map_size(0),
512 _M_start(), _M_finish()
518 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
519 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
521 const _Tp_alloc_type
&
522 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
523 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
526 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
527 { return _Map_alloc_type(_M_get_Tp_allocator()); }
532 return _M_impl
._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp
)));
536 _M_deallocate_node(_Tp
* __p
)
538 _M_impl
._Tp_alloc_type::deallocate(__p
, __deque_buf_size(sizeof(_Tp
)));
542 _M_allocate_map(size_t __n
)
543 { return _M_get_map_allocator().allocate(__n
); }
546 _M_deallocate_map(_Tp
** __p
, size_t __n
)
547 { _M_get_map_allocator().deallocate(__p
, __n
); }
550 void _M_initialize_map(size_t);
551 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
552 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
553 enum { _S_initial_map_size
= 8 };
558 template<typename _Tp
, typename _Alloc
>
559 _Deque_base
<_Tp
, _Alloc
>::
562 if (this->_M_impl
._M_map
)
564 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
565 this->_M_impl
._M_finish
._M_node
+ 1);
566 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
571 * @brief Layout storage.
572 * @param num_elements The count of T's for which to allocate space
576 * The initial underlying memory layout is a bit complicated...
578 template<typename _Tp
, typename _Alloc
>
580 _Deque_base
<_Tp
, _Alloc
>::
581 _M_initialize_map(size_t __num_elements
)
583 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
586 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
587 size_t(__num_nodes
+ 2));
588 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
590 // For "small" maps (needing less than _M_map_size nodes), allocation
591 // starts in the middle elements and grows outwards. So nstart may be
592 // the beginning of _M_map, but for small maps it may be as far in as
595 _Tp
** __nstart
= (this->_M_impl
._M_map
596 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
597 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
600 { _M_create_nodes(__nstart
, __nfinish
); }
603 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
604 this->_M_impl
._M_map
= 0;
605 this->_M_impl
._M_map_size
= 0;
606 __throw_exception_again
;
609 this->_M_impl
._M_start
._M_set_node(__nstart
);
610 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
611 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
612 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
614 % __deque_buf_size(sizeof(_Tp
)));
617 template<typename _Tp
, typename _Alloc
>
619 _Deque_base
<_Tp
, _Alloc
>::
620 _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
625 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
626 *__cur
= this->_M_allocate_node();
630 _M_destroy_nodes(__nstart
, __cur
);
631 __throw_exception_again
;
635 template<typename _Tp
, typename _Alloc
>
637 _Deque_base
<_Tp
, _Alloc
>::
638 _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
640 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
641 _M_deallocate_node(*__n
);
645 * @brief A standard container using fixed-size memory allocation and
646 * constant-time manipulation of elements at either end.
650 * Meets the requirements of a <a href="tables.html#65">container</a>, a
651 * <a href="tables.html#66">reversible container</a>, and a
652 * <a href="tables.html#67">sequence</a>, including the
653 * <a href="tables.html#68">optional sequence requirements</a>.
655 * In previous HP/SGI versions of deque, there was an extra template
656 * parameter so users could control the node size. This extension turned
657 * out to violate the C++ standard (it can be detected using template
658 * template parameters), and it was removed.
660 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
663 * - size_t _M_map_size
664 * - iterator _M_start, _M_finish
666 * map_size is at least 8. %map is an array of map_size
667 * pointers-to-@anodes. (The name %map has nothing to do with the
668 * std::map class, and @b nodes should not be confused with
669 * std::list's usage of @a node.)
671 * A @a node has no specific type name as such, but it is referred
672 * to as @a node in this file. It is a simple array-of-Tp. If Tp
673 * is very large, there will be one Tp element per node (i.e., an
674 * @a array of one). For non-huge Tp's, node size is inversely
675 * related to Tp size: the larger the Tp, the fewer Tp's will fit
676 * in a node. The goal here is to keep the total size of a node
677 * relatively small and constant over different Tp's, to improve
678 * allocator efficiency.
680 * Not every pointer in the %map array will point to a node. If
681 * the initial number of elements in the deque is small, the
682 * /middle/ %map pointers will be valid, and the ones at the edges
683 * will be unused. This same situation will arise as the %map
684 * grows: available %map pointers, if any, will be on the ends. As
685 * new nodes are created, only a subset of the %map's pointers need
686 * to be copied @a outward.
689 * - For any nonsingular iterator i:
690 * - i.node points to a member of the %map array. (Yes, you read that
691 * correctly: i.node does not actually point to a node.) The member of
692 * the %map array is what actually points to the node.
693 * - i.first == *(i.node) (This points to the node (first Tp element).)
694 * - i.last == i.first + node_size
695 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
696 * the implication of this is that i.cur is always a dereferenceable
697 * pointer, even if i is a past-the-end iterator.
698 * - Start and Finish are always nonsingular iterators. NOTE: this
699 * means that an empty deque must have one node, a deque with <N
700 * elements (where N is the node buffer size) must have one node, a
701 * deque with N through (2N-1) elements must have two nodes, etc.
702 * - For every node other than start.node and finish.node, every
703 * element in the node is an initialized object. If start.node ==
704 * finish.node, then [start.cur, finish.cur) are initialized
705 * objects, and the elements outside that range are uninitialized
706 * storage. Otherwise, [start.cur, start.last) and [finish.first,
707 * finish.cur) are initialized objects, and [start.first, start.cur)
708 * and [finish.cur, finish.last) are uninitialized storage.
709 * - [%map, %map + map_size) is a valid, non-empty range.
710 * - [start.node, finish.node] is a valid range contained within
711 * [%map, %map + map_size).
712 * - A pointer in the range [%map, %map + map_size) points to an allocated
713 * node if and only if the pointer is in the range
714 * [start.node, finish.node].
716 * Here's the magic: nothing in deque is @b aware of the discontiguous
719 * The memory setup and layout occurs in the parent, _Base, and the iterator
720 * class is entirely responsible for @a leaping from one node to the next.
721 * All the implementation routines for deque itself work only through the
722 * start and finish iterators. This keeps the routines simple and sane,
723 * and we can use other standard algorithms as well.
725 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
726 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
728 // concept requirements
729 typedef typename
_Alloc::value_type _Alloc_value_type
;
730 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
731 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
733 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
734 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
737 typedef _Tp value_type
;
738 typedef typename
_Tp_alloc_type::pointer pointer
;
739 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
740 typedef typename
_Tp_alloc_type::reference reference
;
741 typedef typename
_Tp_alloc_type::const_reference const_reference
;
742 typedef typename
_Base::iterator iterator
;
743 typedef typename
_Base::const_iterator const_iterator
;
744 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
745 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
746 typedef size_t size_type
;
747 typedef ptrdiff_t difference_type
;
748 typedef _Alloc allocator_type
;
751 typedef pointer
* _Map_pointer
;
753 static size_t _S_buffer_size()
754 { return __deque_buf_size(sizeof(_Tp
)); }
756 // Functions controlling memory layout, and nothing else.
757 using _Base::_M_initialize_map
;
758 using _Base::_M_create_nodes
;
759 using _Base::_M_destroy_nodes
;
760 using _Base::_M_allocate_node
;
761 using _Base::_M_deallocate_node
;
762 using _Base::_M_allocate_map
;
763 using _Base::_M_deallocate_map
;
764 using _Base::_M_get_Tp_allocator
;
767 * A total of four data members accumulated down the hierarchy.
768 * May be accessed via _M_impl.*
770 using _Base::_M_impl
;
773 // [23.2.1.1] construct/copy/destroy
774 // (assign() and get_allocator() are also listed in this section)
776 * @brief Default constructor creates no elements.
782 * @brief Creates a %deque with no elements.
783 * @param a An allocator object.
786 deque(const allocator_type
& __a
)
789 #ifdef __GXX_EXPERIMENTAL_CXX0X__
791 * @brief Creates a %deque with default constructed elements.
792 * @param n The number of elements to initially create.
794 * This constructor fills the %deque with @a n default
795 * constructed elements.
800 { _M_default_initialize(); }
803 * @brief Creates a %deque with copies of an exemplar element.
804 * @param n The number of elements to initially create.
805 * @param value An element to copy.
806 * @param a An allocator.
808 * This constructor fills the %deque with @a n copies of @a value.
810 deque(size_type __n
, const value_type
& __value
,
811 const allocator_type
& __a
= allocator_type())
813 { _M_fill_initialize(__value
); }
816 * @brief Creates a %deque with copies of an exemplar element.
817 * @param n The number of elements to initially create.
818 * @param value An element to copy.
819 * @param a An allocator.
821 * This constructor fills the %deque with @a n copies of @a value.
824 deque(size_type __n
, const value_type
& __value
= value_type(),
825 const allocator_type
& __a
= allocator_type())
827 { _M_fill_initialize(__value
); }
831 * @brief %Deque copy constructor.
832 * @param x A %deque of identical element and allocator types.
834 * The newly-created %deque uses a copy of the allocation object used
837 deque(const deque
& __x
)
838 : _Base(__x
._M_get_Tp_allocator(), __x
.size())
839 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
840 this->_M_impl
._M_start
,
841 _M_get_Tp_allocator()); }
843 #ifdef __GXX_EXPERIMENTAL_CXX0X__
845 * @brief %Deque move constructor.
846 * @param x A %deque of identical element and allocator types.
848 * The newly-created %deque contains the exact contents of @a x.
849 * The contents of @a x are a valid, but unspecified %deque.
852 : _Base(std::move(__x
)) { }
855 * @brief Builds a %deque from an initializer list.
856 * @param l An initializer_list.
857 * @param a An allocator object.
859 * Create a %deque consisting of copies of the elements in the
860 * initializer_list @a l.
862 * This will call the element type's copy constructor N times
863 * (where N is l.size()) and do no memory reallocation.
865 deque(initializer_list
<value_type
> __l
,
866 const allocator_type
& __a
= allocator_type())
869 _M_range_initialize(__l
.begin(), __l
.end(),
870 random_access_iterator_tag());
875 * @brief Builds a %deque from a range.
876 * @param first An input iterator.
877 * @param last An input iterator.
878 * @param a An allocator object.
880 * Create a %deque consisting of copies of the elements from [first,
883 * If the iterators are forward, bidirectional, or random-access, then
884 * this will call the elements' copy constructor N times (where N is
885 * distance(first,last)) and do no memory reallocation. But if only
886 * input iterators are used, then this will do at most 2N calls to the
887 * copy constructor, and logN memory reallocations.
889 template<typename _InputIterator
>
890 deque(_InputIterator __first
, _InputIterator __last
,
891 const allocator_type
& __a
= allocator_type())
894 // Check whether it's an integral type. If so, it's not an iterator.
895 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
896 _M_initialize_dispatch(__first
, __last
, _Integral());
900 * The dtor only erases the elements, and note that if the elements
901 * themselves are pointers, the pointed-to memory is not touched in any
902 * way. Managing the pointer is the user's responsibility.
904 ~deque() _GLIBCXX_NOEXCEPT
905 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
908 * @brief %Deque assignment operator.
909 * @param x A %deque of identical element and allocator types.
911 * All the elements of @a x are copied, but unlike the copy constructor,
912 * the allocator object is not copied.
915 operator=(const deque
& __x
);
917 #ifdef __GXX_EXPERIMENTAL_CXX0X__
919 * @brief %Deque move assignment operator.
920 * @param x A %deque of identical element and allocator types.
922 * The contents of @a x are moved into this deque (without copying).
923 * @a x is a valid, but unspecified %deque.
926 operator=(deque
&& __x
)
936 * @brief Assigns an initializer list to a %deque.
937 * @param l An initializer_list.
939 * This function fills a %deque with copies of the elements in the
940 * initializer_list @a l.
942 * Note that the assignment completely changes the %deque and that the
943 * resulting %deque's size is the same as the number of elements
944 * assigned. Old data may be lost.
947 operator=(initializer_list
<value_type
> __l
)
949 this->assign(__l
.begin(), __l
.end());
955 * @brief Assigns a given value to a %deque.
956 * @param n Number of elements to be assigned.
957 * @param val Value to be assigned.
959 * This function fills a %deque with @a n copies of the given
960 * value. Note that the assignment completely changes the
961 * %deque and that the resulting %deque's size is the same as
962 * the number of elements assigned. Old data may be lost.
965 assign(size_type __n
, const value_type
& __val
)
966 { _M_fill_assign(__n
, __val
); }
969 * @brief Assigns a range to a %deque.
970 * @param first An input iterator.
971 * @param last An input iterator.
973 * This function fills a %deque with copies of the elements in the
974 * range [first,last).
976 * Note that the assignment completely changes the %deque and that the
977 * resulting %deque's size is the same as the number of elements
978 * assigned. Old data may be lost.
980 template<typename _InputIterator
>
982 assign(_InputIterator __first
, _InputIterator __last
)
984 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
985 _M_assign_dispatch(__first
, __last
, _Integral());
988 #ifdef __GXX_EXPERIMENTAL_CXX0X__
990 * @brief Assigns an initializer list to a %deque.
991 * @param l An initializer_list.
993 * This function fills a %deque with copies of the elements in the
994 * initializer_list @a l.
996 * Note that the assignment completely changes the %deque and that the
997 * resulting %deque's size is the same as the number of elements
998 * assigned. Old data may be lost.
1001 assign(initializer_list
<value_type
> __l
)
1002 { this->assign(__l
.begin(), __l
.end()); }
1005 /// Get a copy of the memory allocation object.
1007 get_allocator() const _GLIBCXX_NOEXCEPT
1008 { return _Base::get_allocator(); }
1012 * Returns a read/write iterator that points to the first element in the
1013 * %deque. Iteration is done in ordinary element order.
1016 begin() _GLIBCXX_NOEXCEPT
1017 { return this->_M_impl
._M_start
; }
1020 * Returns a read-only (constant) iterator that points to the first
1021 * element in the %deque. Iteration is done in ordinary element order.
1024 begin() const _GLIBCXX_NOEXCEPT
1025 { return this->_M_impl
._M_start
; }
1028 * Returns a read/write iterator that points one past the last
1029 * element in the %deque. Iteration is done in ordinary
1033 end() _GLIBCXX_NOEXCEPT
1034 { return this->_M_impl
._M_finish
; }
1037 * Returns a read-only (constant) iterator that points one past
1038 * the last element in the %deque. Iteration is done in
1039 * ordinary element order.
1042 end() const _GLIBCXX_NOEXCEPT
1043 { return this->_M_impl
._M_finish
; }
1046 * Returns a read/write reverse iterator that points to the
1047 * last element in the %deque. Iteration is done in reverse
1051 rbegin() _GLIBCXX_NOEXCEPT
1052 { return reverse_iterator(this->_M_impl
._M_finish
); }
1055 * Returns a read-only (constant) reverse iterator that points
1056 * to the last element in the %deque. Iteration is done in
1057 * reverse element order.
1059 const_reverse_iterator
1060 rbegin() const _GLIBCXX_NOEXCEPT
1061 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1064 * Returns a read/write reverse iterator that points to one
1065 * before the first element in the %deque. Iteration is done
1066 * in reverse element order.
1069 rend() _GLIBCXX_NOEXCEPT
1070 { return reverse_iterator(this->_M_impl
._M_start
); }
1073 * Returns a read-only (constant) reverse iterator that points
1074 * to one before the first element in the %deque. Iteration is
1075 * done in reverse element order.
1077 const_reverse_iterator
1078 rend() const _GLIBCXX_NOEXCEPT
1079 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1081 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1083 * Returns a read-only (constant) iterator that points to the first
1084 * element in the %deque. Iteration is done in ordinary element order.
1087 cbegin() const noexcept
1088 { return this->_M_impl
._M_start
; }
1091 * Returns a read-only (constant) iterator that points one past
1092 * the last element in the %deque. Iteration is done in
1093 * ordinary element order.
1096 cend() const noexcept
1097 { return this->_M_impl
._M_finish
; }
1100 * Returns a read-only (constant) reverse iterator that points
1101 * to the last element in the %deque. Iteration is done in
1102 * reverse element order.
1104 const_reverse_iterator
1105 crbegin() const noexcept
1106 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1109 * Returns a read-only (constant) reverse iterator that points
1110 * to one before the first element in the %deque. Iteration is
1111 * done in reverse element order.
1113 const_reverse_iterator
1114 crend() const noexcept
1115 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1118 // [23.2.1.2] capacity
1119 /** Returns the number of elements in the %deque. */
1121 size() const _GLIBCXX_NOEXCEPT
1122 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1124 /** Returns the size() of the largest possible %deque. */
1126 max_size() const _GLIBCXX_NOEXCEPT
1127 { return _M_get_Tp_allocator().max_size(); }
1129 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1131 * @brief Resizes the %deque to the specified number of elements.
1132 * @param new_size Number of elements the %deque should contain.
1134 * This function will %resize the %deque to the specified
1135 * number of elements. If the number is smaller than the
1136 * %deque's current size the %deque is truncated, otherwise
1137 * default constructed elements are appended.
1140 resize(size_type __new_size
)
1142 const size_type __len
= size();
1143 if (__new_size
> __len
)
1144 _M_default_append(__new_size
- __len
);
1145 else if (__new_size
< __len
)
1146 _M_erase_at_end(this->_M_impl
._M_start
1147 + difference_type(__new_size
));
1151 * @brief Resizes the %deque to the specified number of elements.
1152 * @param new_size Number of elements the %deque should contain.
1153 * @param x Data with which new elements should be populated.
1155 * This function will %resize the %deque to the specified
1156 * number of elements. If the number is smaller than the
1157 * %deque's current size the %deque is truncated, otherwise the
1158 * %deque is extended and new elements are populated with given
1162 resize(size_type __new_size
, const value_type
& __x
)
1164 const size_type __len
= size();
1165 if (__new_size
> __len
)
1166 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1167 else if (__new_size
< __len
)
1168 _M_erase_at_end(this->_M_impl
._M_start
1169 + difference_type(__new_size
));
1173 * @brief Resizes the %deque to the specified number of elements.
1174 * @param new_size Number of elements the %deque should contain.
1175 * @param x Data with which new elements should be populated.
1177 * This function will %resize the %deque to the specified
1178 * number of elements. If the number is smaller than the
1179 * %deque's current size the %deque is truncated, otherwise the
1180 * %deque is extended and new elements are populated with given
1184 resize(size_type __new_size
, value_type __x
= value_type())
1186 const size_type __len
= size();
1187 if (__new_size
> __len
)
1188 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1189 else if (__new_size
< __len
)
1190 _M_erase_at_end(this->_M_impl
._M_start
1191 + difference_type(__new_size
));
1195 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1196 /** A non-binding request to reduce memory use. */
1199 { std::__shrink_to_fit(*this); }
1203 * Returns true if the %deque is empty. (Thus begin() would
1207 empty() const _GLIBCXX_NOEXCEPT
1208 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1212 * @brief Subscript access to the data contained in the %deque.
1213 * @param n The index of the element for which data should be
1215 * @return Read/write reference to data.
1217 * This operator allows for easy, array-style, data access.
1218 * Note that data access with this operator is unchecked and
1219 * out_of_range lookups are not defined. (For checked lookups
1223 operator[](size_type __n
)
1224 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1227 * @brief Subscript access to the data contained in the %deque.
1228 * @param n The index of the element for which data should be
1230 * @return Read-only (constant) reference to data.
1232 * This operator allows for easy, array-style, data access.
1233 * Note that data access with this operator is unchecked and
1234 * out_of_range lookups are not defined. (For checked lookups
1238 operator[](size_type __n
) const
1239 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1242 /// Safety check used only from at().
1244 _M_range_check(size_type __n
) const
1246 if (__n
>= this->size())
1247 __throw_out_of_range(__N("deque::_M_range_check"));
1252 * @brief Provides access to the data contained in the %deque.
1253 * @param n The index of the element for which data should be
1255 * @return Read/write reference to data.
1256 * @throw std::out_of_range If @a n is an invalid index.
1258 * This function provides for safer data access. The parameter
1259 * is first checked that it is in the range of the deque. The
1260 * function throws out_of_range if the check fails.
1265 _M_range_check(__n
);
1266 return (*this)[__n
];
1270 * @brief Provides access to the data contained in the %deque.
1271 * @param n The index of the element for which data should be
1273 * @return Read-only (constant) reference to data.
1274 * @throw std::out_of_range If @a n is an invalid index.
1276 * This function provides for safer data access. The parameter is first
1277 * checked that it is in the range of the deque. The function throws
1278 * out_of_range if the check fails.
1281 at(size_type __n
) const
1283 _M_range_check(__n
);
1284 return (*this)[__n
];
1288 * Returns a read/write reference to the data at the first
1289 * element of the %deque.
1293 { return *begin(); }
1296 * Returns a read-only (constant) reference to the data at the first
1297 * element of the %deque.
1301 { return *begin(); }
1304 * Returns a read/write reference to the data at the last element of the
1310 iterator __tmp
= end();
1316 * Returns a read-only (constant) reference to the data at the last
1317 * element of the %deque.
1322 const_iterator __tmp
= end();
1327 // [23.2.1.2] modifiers
1329 * @brief Add data to the front of the %deque.
1330 * @param x Data to be added.
1332 * This is a typical stack operation. The function creates an
1333 * element at the front of the %deque and assigns the given
1334 * data to it. Due to the nature of a %deque this operation
1335 * can be done in constant time.
1338 push_front(const value_type
& __x
)
1340 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1342 this->_M_impl
.construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
1343 --this->_M_impl
._M_start
._M_cur
;
1346 _M_push_front_aux(__x
);
1349 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1351 push_front(value_type
&& __x
)
1352 { emplace_front(std::move(__x
)); }
1354 template<typename
... _Args
>
1356 emplace_front(_Args
&&... __args
);
1360 * @brief Add data to the end of the %deque.
1361 * @param x Data to be added.
1363 * This is a typical stack operation. The function creates an
1364 * element at the end of the %deque and assigns the given data
1365 * to it. Due to the nature of a %deque this operation can be
1366 * done in constant time.
1369 push_back(const value_type
& __x
)
1371 if (this->_M_impl
._M_finish
._M_cur
1372 != this->_M_impl
._M_finish
._M_last
- 1)
1374 this->_M_impl
.construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1375 ++this->_M_impl
._M_finish
._M_cur
;
1378 _M_push_back_aux(__x
);
1381 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1383 push_back(value_type
&& __x
)
1384 { emplace_back(std::move(__x
)); }
1386 template<typename
... _Args
>
1388 emplace_back(_Args
&&... __args
);
1392 * @brief Removes first element.
1394 * This is a typical stack operation. It shrinks the %deque by one.
1396 * Note that no data is returned, and if the first element's data is
1397 * needed, it should be retrieved before pop_front() is called.
1402 if (this->_M_impl
._M_start
._M_cur
1403 != this->_M_impl
._M_start
._M_last
- 1)
1405 this->_M_impl
.destroy(this->_M_impl
._M_start
._M_cur
);
1406 ++this->_M_impl
._M_start
._M_cur
;
1413 * @brief Removes last element.
1415 * This is a typical stack operation. It shrinks the %deque by one.
1417 * Note that no data is returned, and if the last element's data is
1418 * needed, it should be retrieved before pop_back() is called.
1423 if (this->_M_impl
._M_finish
._M_cur
1424 != this->_M_impl
._M_finish
._M_first
)
1426 --this->_M_impl
._M_finish
._M_cur
;
1427 this->_M_impl
.destroy(this->_M_impl
._M_finish
._M_cur
);
1433 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1435 * @brief Inserts an object in %deque before specified iterator.
1436 * @param position An iterator into the %deque.
1437 * @param args Arguments.
1438 * @return An iterator that points to the inserted data.
1440 * This function will insert an object of type T constructed
1441 * with T(std::forward<Args>(args)...) before the specified location.
1443 template<typename
... _Args
>
1445 emplace(iterator __position
, _Args
&&... __args
);
1449 * @brief Inserts given value into %deque before specified iterator.
1450 * @param position An iterator into the %deque.
1451 * @param x Data to be inserted.
1452 * @return An iterator that points to the inserted data.
1454 * This function will insert a copy of the given value before the
1455 * specified location.
1458 insert(iterator __position
, const value_type
& __x
);
1460 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1462 * @brief Inserts given rvalue into %deque before specified iterator.
1463 * @param position An iterator into the %deque.
1464 * @param x Data to be inserted.
1465 * @return An iterator that points to the inserted data.
1467 * This function will insert a copy of the given rvalue before the
1468 * specified location.
1471 insert(iterator __position
, value_type
&& __x
)
1472 { return emplace(__position
, std::move(__x
)); }
1475 * @brief Inserts an initializer list into the %deque.
1476 * @param p An iterator into the %deque.
1477 * @param l An initializer_list.
1479 * This function will insert copies of the data in the
1480 * initializer_list @a l into the %deque before the location
1481 * specified by @a p. This is known as <em>list insert</em>.
1484 insert(iterator __p
, initializer_list
<value_type
> __l
)
1485 { this->insert(__p
, __l
.begin(), __l
.end()); }
1489 * @brief Inserts a number of copies of given data into the %deque.
1490 * @param position An iterator into the %deque.
1491 * @param n Number of elements to be inserted.
1492 * @param x Data to be inserted.
1494 * This function will insert a specified number of copies of the given
1495 * data before the location specified by @a position.
1498 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1499 { _M_fill_insert(__position
, __n
, __x
); }
1502 * @brief Inserts a range into the %deque.
1503 * @param position An iterator into the %deque.
1504 * @param first An input iterator.
1505 * @param last An input iterator.
1507 * This function will insert copies of the data in the range
1508 * [first,last) into the %deque before the location specified
1509 * by @a pos. This is known as <em>range insert</em>.
1511 template<typename _InputIterator
>
1513 insert(iterator __position
, _InputIterator __first
,
1514 _InputIterator __last
)
1516 // Check whether it's an integral type. If so, it's not an iterator.
1517 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1518 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1522 * @brief Remove element at given position.
1523 * @param position Iterator pointing to element to be erased.
1524 * @return An iterator pointing to the next element (or end()).
1526 * This function will erase the element at the given position and thus
1527 * shorten the %deque by one.
1529 * The user is cautioned that
1530 * this function only erases the element, and that if the element is
1531 * itself a pointer, the pointed-to memory is not touched in any way.
1532 * Managing the pointer is the user's responsibility.
1535 erase(iterator __position
);
1538 * @brief Remove a range of elements.
1539 * @param first Iterator pointing to the first element to be erased.
1540 * @param last Iterator pointing to one past the last element to be
1542 * @return An iterator pointing to the element pointed to by @a last
1543 * prior to erasing (or end()).
1545 * This function will erase the elements in the range [first,last) and
1546 * shorten the %deque accordingly.
1548 * The user is cautioned that
1549 * this function only erases the elements, and that if the elements
1550 * themselves are pointers, the pointed-to memory is not touched in any
1551 * way. Managing the pointer is the user's responsibility.
1554 erase(iterator __first
, iterator __last
);
1557 * @brief Swaps data with another %deque.
1558 * @param x A %deque of the same element and allocator types.
1560 * This exchanges the elements between two deques in constant time.
1561 * (Four pointers, so it should be quite fast.)
1562 * Note that the global std::swap() function is specialized such that
1563 * std::swap(d1,d2) will feed to this function.
1568 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1569 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1570 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1571 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1573 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1574 // 431. Swapping containers with unequal allocators.
1575 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1576 __x
._M_get_Tp_allocator());
1580 * Erases all the elements. Note that this function only erases the
1581 * elements, and that if the elements themselves are pointers, the
1582 * pointed-to memory is not touched in any way. Managing the pointer is
1583 * the user's responsibility.
1586 clear() _GLIBCXX_NOEXCEPT
1587 { _M_erase_at_end(begin()); }
1590 // Internal constructor functions follow.
1592 // called by the range constructor to implement [23.1.1]/9
1594 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1595 // 438. Ambiguity in the "do the right thing" clause
1596 template<typename _Integer
>
1598 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1600 _M_initialize_map(static_cast<size_type
>(__n
));
1601 _M_fill_initialize(__x
);
1604 // called by the range constructor to implement [23.1.1]/9
1605 template<typename _InputIterator
>
1607 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1610 typedef typename
std::iterator_traits
<_InputIterator
>::
1611 iterator_category _IterCategory
;
1612 _M_range_initialize(__first
, __last
, _IterCategory());
1615 // called by the second initialize_dispatch above
1618 * @brief Fills the deque with whatever is in [first,last).
1619 * @param first An input iterator.
1620 * @param last An input iterator.
1623 * If the iterators are actually forward iterators (or better), then the
1624 * memory layout can be done all at once. Else we move forward using
1625 * push_back on each value from the iterator.
1627 template<typename _InputIterator
>
1629 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1630 std::input_iterator_tag
);
1632 // called by the second initialize_dispatch above
1633 template<typename _ForwardIterator
>
1635 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1636 std::forward_iterator_tag
);
1640 * @brief Fills the %deque with copies of value.
1641 * @param value Initial value.
1643 * @pre _M_start and _M_finish have already been initialized,
1644 * but none of the %deque's elements have yet been constructed.
1646 * This function is called only when the user provides an explicit size
1647 * (with or without an explicit exemplar value).
1650 _M_fill_initialize(const value_type
& __value
);
1652 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1653 // called by deque(n).
1655 _M_default_initialize();
1658 // Internal assign functions follow. The *_aux functions do the actual
1659 // assignment work for the range versions.
1661 // called by the range assign to implement [23.1.1]/9
1663 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1664 // 438. Ambiguity in the "do the right thing" clause
1665 template<typename _Integer
>
1667 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1668 { _M_fill_assign(__n
, __val
); }
1670 // called by the range assign to implement [23.1.1]/9
1671 template<typename _InputIterator
>
1673 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1676 typedef typename
std::iterator_traits
<_InputIterator
>::
1677 iterator_category _IterCategory
;
1678 _M_assign_aux(__first
, __last
, _IterCategory());
1681 // called by the second assign_dispatch above
1682 template<typename _InputIterator
>
1684 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1685 std::input_iterator_tag
);
1687 // called by the second assign_dispatch above
1688 template<typename _ForwardIterator
>
1690 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1691 std::forward_iterator_tag
)
1693 const size_type __len
= std::distance(__first
, __last
);
1696 _ForwardIterator __mid
= __first
;
1697 std::advance(__mid
, size());
1698 std::copy(__first
, __mid
, begin());
1699 insert(end(), __mid
, __last
);
1702 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1705 // Called by assign(n,t), and the range assign when it turns out
1706 // to be the same thing.
1708 _M_fill_assign(size_type __n
, const value_type
& __val
)
1712 std::fill(begin(), end(), __val
);
1713 insert(end(), __n
- size(), __val
);
1717 _M_erase_at_end(begin() + difference_type(__n
));
1718 std::fill(begin(), end(), __val
);
1723 /// Helper functions for push_* and pop_*.
1724 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1725 void _M_push_back_aux(const value_type
&);
1727 void _M_push_front_aux(const value_type
&);
1729 template<typename
... _Args
>
1730 void _M_push_back_aux(_Args
&&... __args
);
1732 template<typename
... _Args
>
1733 void _M_push_front_aux(_Args
&&... __args
);
1736 void _M_pop_back_aux();
1738 void _M_pop_front_aux();
1741 // Internal insert functions follow. The *_aux functions do the actual
1742 // insertion work when all shortcuts fail.
1744 // called by the range insert to implement [23.1.1]/9
1746 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1747 // 438. Ambiguity in the "do the right thing" clause
1748 template<typename _Integer
>
1750 _M_insert_dispatch(iterator __pos
,
1751 _Integer __n
, _Integer __x
, __true_type
)
1752 { _M_fill_insert(__pos
, __n
, __x
); }
1754 // called by the range insert to implement [23.1.1]/9
1755 template<typename _InputIterator
>
1757 _M_insert_dispatch(iterator __pos
,
1758 _InputIterator __first
, _InputIterator __last
,
1761 typedef typename
std::iterator_traits
<_InputIterator
>::
1762 iterator_category _IterCategory
;
1763 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1766 // called by the second insert_dispatch above
1767 template<typename _InputIterator
>
1769 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1770 _InputIterator __last
, std::input_iterator_tag
);
1772 // called by the second insert_dispatch above
1773 template<typename _ForwardIterator
>
1775 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1776 _ForwardIterator __last
, std::forward_iterator_tag
);
1778 // Called by insert(p,n,x), and the range insert when it turns out to be
1779 // the same thing. Can use fill functions in optimal situations,
1780 // otherwise passes off to insert_aux(p,n,x).
1782 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1784 // called by insert(p,x)
1785 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1787 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1789 template<typename
... _Args
>
1791 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
1794 // called by insert(p,n,x) via fill_insert
1796 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1798 // called by range_insert_aux for forward iterators
1799 template<typename _ForwardIterator
>
1801 _M_insert_aux(iterator __pos
,
1802 _ForwardIterator __first
, _ForwardIterator __last
,
1806 // Internal erase functions follow.
1809 _M_destroy_data_aux(iterator __first
, iterator __last
);
1811 // Called by ~deque().
1812 // NB: Doesn't deallocate the nodes.
1813 template<typename _Alloc1
>
1815 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
1816 { _M_destroy_data_aux(__first
, __last
); }
1819 _M_destroy_data(iterator __first
, iterator __last
,
1820 const std::allocator
<_Tp
>&)
1822 if (!__has_trivial_destructor(value_type
))
1823 _M_destroy_data_aux(__first
, __last
);
1826 // Called by erase(q1, q2).
1828 _M_erase_at_begin(iterator __pos
)
1830 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
1831 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
1832 this->_M_impl
._M_start
= __pos
;
1835 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1836 // _M_fill_assign, operator=.
1838 _M_erase_at_end(iterator __pos
)
1840 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
1841 _M_destroy_nodes(__pos
._M_node
+ 1,
1842 this->_M_impl
._M_finish
._M_node
+ 1);
1843 this->_M_impl
._M_finish
= __pos
;
1846 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1847 // Called by resize(sz).
1849 _M_default_append(size_type __n
);
1853 /// Memory-handling helpers for the previous internal insert functions.
1855 _M_reserve_elements_at_front(size_type __n
)
1857 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1858 - this->_M_impl
._M_start
._M_first
;
1859 if (__n
> __vacancies
)
1860 _M_new_elements_at_front(__n
- __vacancies
);
1861 return this->_M_impl
._M_start
- difference_type(__n
);
1865 _M_reserve_elements_at_back(size_type __n
)
1867 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1868 - this->_M_impl
._M_finish
._M_cur
) - 1;
1869 if (__n
> __vacancies
)
1870 _M_new_elements_at_back(__n
- __vacancies
);
1871 return this->_M_impl
._M_finish
+ difference_type(__n
);
1875 _M_new_elements_at_front(size_type __new_elements
);
1878 _M_new_elements_at_back(size_type __new_elements
);
1884 * @brief Memory-handling helpers for the major %map.
1886 * Makes sure the _M_map has space for new nodes. Does not
1887 * actually add the nodes. Can invalidate _M_map pointers.
1888 * (And consequently, %deque iterators.)
1891 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
1893 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1894 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
1895 _M_reallocate_map(__nodes_to_add
, false);
1899 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
1901 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
1902 - this->_M_impl
._M_map
))
1903 _M_reallocate_map(__nodes_to_add
, true);
1907 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
1913 * @brief Deque equality comparison.
1914 * @param x A %deque.
1915 * @param y A %deque of the same type as @a x.
1916 * @return True iff the size and elements of the deques are equal.
1918 * This is an equivalence relation. It is linear in the size of the
1919 * deques. Deques are considered equivalent if their sizes are equal,
1920 * and if corresponding elements compare equal.
1922 template<typename _Tp
, typename _Alloc
>
1924 operator==(const deque
<_Tp
, _Alloc
>& __x
,
1925 const deque
<_Tp
, _Alloc
>& __y
)
1926 { return __x
.size() == __y
.size()
1927 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
1930 * @brief Deque ordering relation.
1931 * @param x A %deque.
1932 * @param y A %deque of the same type as @a x.
1933 * @return True iff @a x is lexicographically less than @a y.
1935 * This is a total ordering relation. It is linear in the size of the
1936 * deques. The elements must be comparable with @c <.
1938 * See std::lexicographical_compare() for how the determination is made.
1940 template<typename _Tp
, typename _Alloc
>
1942 operator<(const deque
<_Tp
, _Alloc
>& __x
,
1943 const deque
<_Tp
, _Alloc
>& __y
)
1944 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1945 __y
.begin(), __y
.end()); }
1947 /// Based on operator==
1948 template<typename _Tp
, typename _Alloc
>
1950 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
1951 const deque
<_Tp
, _Alloc
>& __y
)
1952 { return !(__x
== __y
); }
1954 /// Based on operator<
1955 template<typename _Tp
, typename _Alloc
>
1957 operator>(const deque
<_Tp
, _Alloc
>& __x
,
1958 const deque
<_Tp
, _Alloc
>& __y
)
1959 { return __y
< __x
; }
1961 /// Based on operator<
1962 template<typename _Tp
, typename _Alloc
>
1964 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
1965 const deque
<_Tp
, _Alloc
>& __y
)
1966 { return !(__y
< __x
); }
1968 /// Based on operator<
1969 template<typename _Tp
, typename _Alloc
>
1971 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
1972 const deque
<_Tp
, _Alloc
>& __y
)
1973 { return !(__x
< __y
); }
1975 /// See std::deque::swap().
1976 template<typename _Tp
, typename _Alloc
>
1978 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
1981 #undef _GLIBCXX_DEQUE_BUF_SIZE
1983 _GLIBCXX_END_NAMESPACE_CONTAINER
1986 #endif /* _STL_DEQUE_H */