1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // 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 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
68 * @brief This function controls the size of memory nodes.
69 * @param size The size of an element.
70 * @return The number (not byte size) of elements per node.
72 * This function started off as a compiler kludge from SGI, but
73 * seems to be a useful wrapper around a repeated constant
74 * expression. The @b 512 is tunable (and no other code needs to
75 * change), but no investigation has been done since inheriting the
76 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
77 * you are doing, however: changing it breaks the binary
81 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
82 #define _GLIBCXX_DEQUE_BUF_SIZE 512
86 __deque_buf_size(size_t __size
)
87 { return (__size
< _GLIBCXX_DEQUE_BUF_SIZE
88 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE
/ __size
) : size_t(1)); }
92 * @brief A deque::iterator.
94 * Quite a bit of intelligence here. Much of the functionality of
95 * deque is actually passed off to this class. A deque holds two
96 * of these internally, marking its valid range. Access to
97 * elements is done as offsets of either of those two, relying on
98 * operator overloading in this class.
100 * All the functions are op overloads except for _M_set_node.
102 template<typename _Tp
, typename _Ref
, typename _Ptr
>
103 struct _Deque_iterator
105 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
106 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
108 static size_t _S_buffer_size()
109 { return __deque_buf_size(sizeof(_Tp
)); }
111 typedef std::random_access_iterator_tag iterator_category
;
112 typedef _Tp value_type
;
113 typedef _Ptr pointer
;
114 typedef _Ref reference
;
115 typedef size_t size_type
;
116 typedef ptrdiff_t difference_type
;
117 typedef _Tp
** _Map_pointer
;
118 typedef _Deque_iterator _Self
;
123 _Map_pointer _M_node
;
125 _Deque_iterator(_Tp
* __x
, _Map_pointer __y
)
126 : _M_cur(__x
), _M_first(*__y
),
127 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
130 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
132 _Deque_iterator(const iterator
& __x
)
133 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
134 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
148 if (_M_cur
== _M_last
)
150 _M_set_node(_M_node
+ 1);
167 if (_M_cur
== _M_first
)
169 _M_set_node(_M_node
- 1);
185 operator+=(difference_type __n
)
187 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
188 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
192 const difference_type __node_offset
=
193 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
194 : -difference_type((-__offset
- 1)
195 / _S_buffer_size()) - 1;
196 _M_set_node(_M_node
+ __node_offset
);
197 _M_cur
= _M_first
+ (__offset
- __node_offset
198 * difference_type(_S_buffer_size()));
204 operator+(difference_type __n
) const
211 operator-=(difference_type __n
)
212 { return *this += -__n
; }
215 operator-(difference_type __n
) const
222 operator[](difference_type __n
) const
223 { return *(*this + __n
); }
226 * Prepares to traverse new_node. Sets everything except
227 * _M_cur, which should therefore be set by the caller
228 * immediately afterwards, based on _M_first and _M_last.
231 _M_set_node(_Map_pointer __new_node
)
233 _M_node
= __new_node
;
234 _M_first
= *__new_node
;
235 _M_last
= _M_first
+ difference_type(_S_buffer_size());
239 // Note: we also provide overloads whose operands are of the same type in
240 // order to avoid ambiguous overload resolution when std::rel_ops operators
241 // are in scope (for additional details, see libstdc++/3628)
242 template<typename _Tp
, typename _Ref
, typename _Ptr
>
244 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
245 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
246 { return __x
._M_cur
== __y
._M_cur
; }
248 template<typename _Tp
, typename _RefL
, typename _PtrL
,
249 typename _RefR
, typename _PtrR
>
251 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
252 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
253 { return __x
._M_cur
== __y
._M_cur
; }
255 template<typename _Tp
, typename _Ref
, typename _Ptr
>
257 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
258 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
259 { return !(__x
== __y
); }
261 template<typename _Tp
, typename _RefL
, typename _PtrL
,
262 typename _RefR
, typename _PtrR
>
264 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
265 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
266 { return !(__x
== __y
); }
268 template<typename _Tp
, typename _Ref
, typename _Ptr
>
270 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
271 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
272 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
273 : (__x
._M_node
< __y
._M_node
); }
275 template<typename _Tp
, typename _RefL
, typename _PtrL
,
276 typename _RefR
, typename _PtrR
>
278 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
279 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
280 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
281 : (__x
._M_node
< __y
._M_node
); }
283 template<typename _Tp
, typename _Ref
, typename _Ptr
>
285 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
286 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
287 { return __y
< __x
; }
289 template<typename _Tp
, typename _RefL
, typename _PtrL
,
290 typename _RefR
, typename _PtrR
>
292 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
293 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
294 { return __y
< __x
; }
296 template<typename _Tp
, typename _Ref
, typename _Ptr
>
298 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
299 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
300 { return !(__y
< __x
); }
302 template<typename _Tp
, typename _RefL
, typename _PtrL
,
303 typename _RefR
, typename _PtrR
>
305 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
306 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
307 { return !(__y
< __x
); }
309 template<typename _Tp
, typename _Ref
, typename _Ptr
>
311 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
312 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
313 { return !(__x
< __y
); }
315 template<typename _Tp
, typename _RefL
, typename _PtrL
,
316 typename _RefR
, typename _PtrR
>
318 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
319 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
320 { return !(__x
< __y
); }
322 // _GLIBCXX_RESOLVE_LIB_DEFECTS
323 // According to the resolution of DR179 not only the various comparison
324 // operators but also operator- must accept mixed iterator/const_iterator
326 template<typename _Tp
, typename _Ref
, typename _Ptr
>
327 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
328 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
329 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
331 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
332 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
333 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
334 + (__y
._M_last
- __y
._M_cur
);
337 template<typename _Tp
, typename _RefL
, typename _PtrL
,
338 typename _RefR
, typename _PtrR
>
339 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
340 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
341 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
343 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
344 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
345 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
346 + (__y
._M_last
- __y
._M_cur
);
349 template<typename _Tp
, typename _Ref
, typename _Ptr
>
350 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
351 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
352 { return __x
+ __n
; }
354 template<typename _Tp
>
356 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
357 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
359 template<typename _Tp
>
360 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
361 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
362 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
363 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
365 template<typename _Tp
>
366 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
367 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
368 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
369 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
370 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
371 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
374 template<typename _Tp
>
375 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
376 copy_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
377 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
378 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
380 template<typename _Tp
>
381 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
382 copy_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
383 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
384 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
385 { return std::copy_backward(_Deque_iterator
<_Tp
,
386 const _Tp
&, const _Tp
*>(__first
),
388 const _Tp
&, const _Tp
*>(__last
),
391 #ifdef __GXX_EXPERIMENTAL_CXX0X__
392 template<typename _Tp
>
393 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
394 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
395 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
396 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
398 template<typename _Tp
>
399 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
400 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
401 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
402 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
403 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
404 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
407 template<typename _Tp
>
408 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
409 move_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
410 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
411 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
413 template<typename _Tp
>
414 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
415 move_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
416 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
417 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
418 { return std::move_backward(_Deque_iterator
<_Tp
,
419 const _Tp
&, const _Tp
*>(__first
),
421 const _Tp
&, const _Tp
*>(__last
),
426 * Deque base class. This class provides the unified face for %deque's
427 * allocation. This class's constructor and destructor allocate and
428 * deallocate (but do not initialize) storage. This makes %exception
431 * Nothing in this class ever constructs or destroys an actual Tp element.
432 * (Deque handles that itself.) Only/All memory management is performed
435 template<typename _Tp
, typename _Alloc
>
439 typedef _Alloc allocator_type
;
442 get_allocator() const
443 { return allocator_type(_M_get_Tp_allocator()); }
445 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
446 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
450 { _M_initialize_map(0); }
452 _Deque_base(size_t __num_elements
)
454 { _M_initialize_map(__num_elements
); }
456 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
458 { _M_initialize_map(__num_elements
); }
460 _Deque_base(const allocator_type
& __a
)
464 #ifdef __GXX_EXPERIMENTAL_CXX0X__
465 _Deque_base(_Deque_base
&& __x
)
466 : _M_impl(__x
._M_get_Tp_allocator())
468 _M_initialize_map(0);
469 if (__x
._M_impl
._M_map
)
471 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
472 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
473 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
474 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
482 //This struct encapsulates the implementation of the std::deque
483 //standard container and at the same time makes use of the EBO
484 //for empty allocators.
485 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
487 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
490 : public _Tp_alloc_type
498 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
499 _M_start(), _M_finish()
502 _Deque_impl(const _Tp_alloc_type
& __a
)
503 : _Tp_alloc_type(__a
), _M_map(0), _M_map_size(0),
504 _M_start(), _M_finish()
509 _M_get_Tp_allocator()
510 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
512 const _Tp_alloc_type
&
513 _M_get_Tp_allocator() const
514 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
517 _M_get_map_allocator() const
518 { return _Map_alloc_type(_M_get_Tp_allocator()); }
523 return _M_impl
._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp
)));
527 _M_deallocate_node(_Tp
* __p
)
529 _M_impl
._Tp_alloc_type::deallocate(__p
, __deque_buf_size(sizeof(_Tp
)));
533 _M_allocate_map(size_t __n
)
534 { return _M_get_map_allocator().allocate(__n
); }
537 _M_deallocate_map(_Tp
** __p
, size_t __n
)
538 { _M_get_map_allocator().deallocate(__p
, __n
); }
541 void _M_initialize_map(size_t);
542 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
543 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
544 enum { _S_initial_map_size
= 8 };
549 template<typename _Tp
, typename _Alloc
>
550 _Deque_base
<_Tp
, _Alloc
>::
553 if (this->_M_impl
._M_map
)
555 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
556 this->_M_impl
._M_finish
._M_node
+ 1);
557 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
562 * @brief Layout storage.
563 * @param num_elements The count of T's for which to allocate space
567 * The initial underlying memory layout is a bit complicated...
569 template<typename _Tp
, typename _Alloc
>
571 _Deque_base
<_Tp
, _Alloc
>::
572 _M_initialize_map(size_t __num_elements
)
574 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
577 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
578 size_t(__num_nodes
+ 2));
579 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
581 // For "small" maps (needing less than _M_map_size nodes), allocation
582 // starts in the middle elements and grows outwards. So nstart may be
583 // the beginning of _M_map, but for small maps it may be as far in as
586 _Tp
** __nstart
= (this->_M_impl
._M_map
587 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
588 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
591 { _M_create_nodes(__nstart
, __nfinish
); }
594 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
595 this->_M_impl
._M_map
= 0;
596 this->_M_impl
._M_map_size
= 0;
597 __throw_exception_again
;
600 this->_M_impl
._M_start
._M_set_node(__nstart
);
601 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
602 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
603 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
605 % __deque_buf_size(sizeof(_Tp
)));
608 template<typename _Tp
, typename _Alloc
>
610 _Deque_base
<_Tp
, _Alloc
>::
611 _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
616 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
617 *__cur
= this->_M_allocate_node();
621 _M_destroy_nodes(__nstart
, __cur
);
622 __throw_exception_again
;
626 template<typename _Tp
, typename _Alloc
>
628 _Deque_base
<_Tp
, _Alloc
>::
629 _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
631 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
632 _M_deallocate_node(*__n
);
636 * @brief A standard container using fixed-size memory allocation and
637 * constant-time manipulation of elements at either end.
641 * Meets the requirements of a <a href="tables.html#65">container</a>, a
642 * <a href="tables.html#66">reversible container</a>, and a
643 * <a href="tables.html#67">sequence</a>, including the
644 * <a href="tables.html#68">optional sequence requirements</a>.
646 * In previous HP/SGI versions of deque, there was an extra template
647 * parameter so users could control the node size. This extension turned
648 * out to violate the C++ standard (it can be detected using template
649 * template parameters), and it was removed.
651 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
654 * - size_t _M_map_size
655 * - iterator _M_start, _M_finish
657 * map_size is at least 8. %map is an array of map_size
658 * pointers-to-@anodes. (The name %map has nothing to do with the
659 * std::map class, and @b nodes should not be confused with
660 * std::list's usage of @a node.)
662 * A @a node has no specific type name as such, but it is referred
663 * to as @a node in this file. It is a simple array-of-Tp. If Tp
664 * is very large, there will be one Tp element per node (i.e., an
665 * @a array of one). For non-huge Tp's, node size is inversely
666 * related to Tp size: the larger the Tp, the fewer Tp's will fit
667 * in a node. The goal here is to keep the total size of a node
668 * relatively small and constant over different Tp's, to improve
669 * allocator efficiency.
671 * Not every pointer in the %map array will point to a node. If
672 * the initial number of elements in the deque is small, the
673 * /middle/ %map pointers will be valid, and the ones at the edges
674 * will be unused. This same situation will arise as the %map
675 * grows: available %map pointers, if any, will be on the ends. As
676 * new nodes are created, only a subset of the %map's pointers need
677 * to be copied @a outward.
680 * - For any nonsingular iterator i:
681 * - i.node points to a member of the %map array. (Yes, you read that
682 * correctly: i.node does not actually point to a node.) The member of
683 * the %map array is what actually points to the node.
684 * - i.first == *(i.node) (This points to the node (first Tp element).)
685 * - i.last == i.first + node_size
686 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
687 * the implication of this is that i.cur is always a dereferenceable
688 * pointer, even if i is a past-the-end iterator.
689 * - Start and Finish are always nonsingular iterators. NOTE: this
690 * means that an empty deque must have one node, a deque with <N
691 * elements (where N is the node buffer size) must have one node, a
692 * deque with N through (2N-1) elements must have two nodes, etc.
693 * - For every node other than start.node and finish.node, every
694 * element in the node is an initialized object. If start.node ==
695 * finish.node, then [start.cur, finish.cur) are initialized
696 * objects, and the elements outside that range are uninitialized
697 * storage. Otherwise, [start.cur, start.last) and [finish.first,
698 * finish.cur) are initialized objects, and [start.first, start.cur)
699 * and [finish.cur, finish.last) are uninitialized storage.
700 * - [%map, %map + map_size) is a valid, non-empty range.
701 * - [start.node, finish.node] is a valid range contained within
702 * [%map, %map + map_size).
703 * - A pointer in the range [%map, %map + map_size) points to an allocated
704 * node if and only if the pointer is in the range
705 * [start.node, finish.node].
707 * Here's the magic: nothing in deque is @b aware of the discontiguous
710 * The memory setup and layout occurs in the parent, _Base, and the iterator
711 * class is entirely responsible for @a leaping from one node to the next.
712 * All the implementation routines for deque itself work only through the
713 * start and finish iterators. This keeps the routines simple and sane,
714 * and we can use other standard algorithms as well.
716 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
717 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
719 // concept requirements
720 typedef typename
_Alloc::value_type _Alloc_value_type
;
721 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
722 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
724 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
725 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
728 typedef _Tp value_type
;
729 typedef typename
_Tp_alloc_type::pointer pointer
;
730 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
731 typedef typename
_Tp_alloc_type::reference reference
;
732 typedef typename
_Tp_alloc_type::const_reference const_reference
;
733 typedef typename
_Base::iterator iterator
;
734 typedef typename
_Base::const_iterator const_iterator
;
735 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
736 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
737 typedef size_t size_type
;
738 typedef ptrdiff_t difference_type
;
739 typedef _Alloc allocator_type
;
742 typedef pointer
* _Map_pointer
;
744 static size_t _S_buffer_size()
745 { return __deque_buf_size(sizeof(_Tp
)); }
747 // Functions controlling memory layout, and nothing else.
748 using _Base::_M_initialize_map
;
749 using _Base::_M_create_nodes
;
750 using _Base::_M_destroy_nodes
;
751 using _Base::_M_allocate_node
;
752 using _Base::_M_deallocate_node
;
753 using _Base::_M_allocate_map
;
754 using _Base::_M_deallocate_map
;
755 using _Base::_M_get_Tp_allocator
;
758 * A total of four data members accumulated down the hierarchy.
759 * May be accessed via _M_impl.*
761 using _Base::_M_impl
;
764 // [23.2.1.1] construct/copy/destroy
765 // (assign() and get_allocator() are also listed in this section)
767 * @brief Default constructor creates no elements.
773 * @brief Creates a %deque with no elements.
774 * @param a An allocator object.
777 deque(const allocator_type
& __a
)
780 #ifdef __GXX_EXPERIMENTAL_CXX0X__
782 * @brief Creates a %deque with default constructed elements.
783 * @param n The number of elements to initially create.
785 * This constructor fills the %deque with @a n default
786 * constructed elements.
791 { _M_default_initialize(); }
794 * @brief Creates a %deque with copies of an exemplar element.
795 * @param n The number of elements to initially create.
796 * @param value An element to copy.
797 * @param a An allocator.
799 * This constructor fills the %deque with @a n copies of @a value.
801 deque(size_type __n
, const value_type
& __value
,
802 const allocator_type
& __a
= allocator_type())
804 { _M_fill_initialize(__value
); }
807 * @brief Creates a %deque with copies of an exemplar element.
808 * @param n The number of elements to initially create.
809 * @param value An element to copy.
810 * @param a An allocator.
812 * This constructor fills the %deque with @a n copies of @a value.
815 deque(size_type __n
, const value_type
& __value
= value_type(),
816 const allocator_type
& __a
= allocator_type())
818 { _M_fill_initialize(__value
); }
822 * @brief %Deque copy constructor.
823 * @param x A %deque of identical element and allocator types.
825 * The newly-created %deque uses a copy of the allocation object used
828 deque(const deque
& __x
)
829 : _Base(__x
._M_get_Tp_allocator(), __x
.size())
830 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
831 this->_M_impl
._M_start
,
832 _M_get_Tp_allocator()); }
834 #ifdef __GXX_EXPERIMENTAL_CXX0X__
836 * @brief %Deque move constructor.
837 * @param x A %deque of identical element and allocator types.
839 * The newly-created %deque contains the exact contents of @a x.
840 * The contents of @a x are a valid, but unspecified %deque.
843 : _Base(std::move(__x
)) { }
846 * @brief Builds a %deque from an initializer list.
847 * @param l An initializer_list.
848 * @param a An allocator object.
850 * Create a %deque consisting of copies of the elements in the
851 * initializer_list @a l.
853 * This will call the element type's copy constructor N times
854 * (where N is l.size()) and do no memory reallocation.
856 deque(initializer_list
<value_type
> __l
,
857 const allocator_type
& __a
= allocator_type())
860 _M_range_initialize(__l
.begin(), __l
.end(),
861 random_access_iterator_tag());
866 * @brief Builds a %deque from a range.
867 * @param first An input iterator.
868 * @param last An input iterator.
869 * @param a An allocator object.
871 * Create a %deque consisting of copies of the elements from [first,
874 * If the iterators are forward, bidirectional, or random-access, then
875 * this will call the elements' copy constructor N times (where N is
876 * distance(first,last)) and do no memory reallocation. But if only
877 * input iterators are used, then this will do at most 2N calls to the
878 * copy constructor, and logN memory reallocations.
880 template<typename _InputIterator
>
881 deque(_InputIterator __first
, _InputIterator __last
,
882 const allocator_type
& __a
= allocator_type())
885 // Check whether it's an integral type. If so, it's not an iterator.
886 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
887 _M_initialize_dispatch(__first
, __last
, _Integral());
891 * The dtor only erases the elements, and note that if the elements
892 * themselves are pointers, the pointed-to memory is not touched in any
893 * way. Managing the pointer is the user's responsibility.
896 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
899 * @brief %Deque assignment operator.
900 * @param x A %deque of identical element and allocator types.
902 * All the elements of @a x are copied, but unlike the copy constructor,
903 * the allocator object is not copied.
906 operator=(const deque
& __x
);
908 #ifdef __GXX_EXPERIMENTAL_CXX0X__
910 * @brief %Deque move assignment operator.
911 * @param x A %deque of identical element and allocator types.
913 * The contents of @a x are moved into this deque (without copying).
914 * @a x is a valid, but unspecified %deque.
917 operator=(deque
&& __x
)
927 * @brief Assigns an initializer list to a %deque.
928 * @param l An initializer_list.
930 * This function fills a %deque with copies of the elements in the
931 * initializer_list @a l.
933 * Note that the assignment completely changes the %deque and that the
934 * resulting %deque's size is the same as the number of elements
935 * assigned. Old data may be lost.
938 operator=(initializer_list
<value_type
> __l
)
940 this->assign(__l
.begin(), __l
.end());
946 * @brief Assigns a given value to a %deque.
947 * @param n Number of elements to be assigned.
948 * @param val Value to be assigned.
950 * This function fills a %deque with @a n copies of the given
951 * value. Note that the assignment completely changes the
952 * %deque and that the resulting %deque's size is the same as
953 * the number of elements assigned. Old data may be lost.
956 assign(size_type __n
, const value_type
& __val
)
957 { _M_fill_assign(__n
, __val
); }
960 * @brief Assigns a range to a %deque.
961 * @param first An input iterator.
962 * @param last An input iterator.
964 * This function fills a %deque with copies of the elements in the
965 * range [first,last).
967 * Note that the assignment completely changes the %deque and that the
968 * resulting %deque's size is the same as the number of elements
969 * assigned. Old data may be lost.
971 template<typename _InputIterator
>
973 assign(_InputIterator __first
, _InputIterator __last
)
975 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
976 _M_assign_dispatch(__first
, __last
, _Integral());
979 #ifdef __GXX_EXPERIMENTAL_CXX0X__
981 * @brief Assigns an initializer list to a %deque.
982 * @param l An initializer_list.
984 * This function fills a %deque with copies of the elements in the
985 * initializer_list @a l.
987 * Note that the assignment completely changes the %deque and that the
988 * resulting %deque's size is the same as the number of elements
989 * assigned. Old data may be lost.
992 assign(initializer_list
<value_type
> __l
)
993 { this->assign(__l
.begin(), __l
.end()); }
996 /// Get a copy of the memory allocation object.
998 get_allocator() const
999 { return _Base::get_allocator(); }
1003 * Returns a read/write iterator that points to the first element in the
1004 * %deque. Iteration is done in ordinary element order.
1008 { return this->_M_impl
._M_start
; }
1011 * Returns a read-only (constant) iterator that points to the first
1012 * element in the %deque. Iteration is done in ordinary element order.
1016 { return this->_M_impl
._M_start
; }
1019 * Returns a read/write iterator that points one past the last
1020 * element in the %deque. Iteration is done in ordinary
1025 { return this->_M_impl
._M_finish
; }
1028 * Returns a read-only (constant) iterator that points one past
1029 * the last element in the %deque. Iteration is done in
1030 * ordinary element order.
1034 { return this->_M_impl
._M_finish
; }
1037 * Returns a read/write reverse iterator that points to the
1038 * last element in the %deque. Iteration is done in reverse
1043 { return reverse_iterator(this->_M_impl
._M_finish
); }
1046 * Returns a read-only (constant) reverse iterator that points
1047 * to the last element in the %deque. Iteration is done in
1048 * reverse element order.
1050 const_reverse_iterator
1052 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1055 * Returns a read/write reverse iterator that points to one
1056 * before the first element in the %deque. Iteration is done
1057 * in reverse element order.
1061 { return reverse_iterator(this->_M_impl
._M_start
); }
1064 * Returns a read-only (constant) reverse iterator that points
1065 * to one before the first element in the %deque. Iteration is
1066 * done in reverse element order.
1068 const_reverse_iterator
1070 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1072 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1074 * Returns a read-only (constant) iterator that points to the first
1075 * element in the %deque. Iteration is done in ordinary element order.
1079 { return this->_M_impl
._M_start
; }
1082 * Returns a read-only (constant) iterator that points one past
1083 * the last element in the %deque. Iteration is done in
1084 * ordinary element order.
1088 { return this->_M_impl
._M_finish
; }
1091 * Returns a read-only (constant) reverse iterator that points
1092 * to the last element in the %deque. Iteration is done in
1093 * reverse element order.
1095 const_reverse_iterator
1097 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1100 * Returns a read-only (constant) reverse iterator that points
1101 * to one before the first element in the %deque. Iteration is
1102 * done in reverse element order.
1104 const_reverse_iterator
1106 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1109 // [23.2.1.2] capacity
1110 /** Returns the number of elements in the %deque. */
1113 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1115 /** Returns the size() of the largest possible %deque. */
1118 { return _M_get_Tp_allocator().max_size(); }
1120 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1122 * @brief Resizes the %deque to the specified number of elements.
1123 * @param new_size Number of elements the %deque should contain.
1125 * This function will %resize the %deque to the specified
1126 * number of elements. If the number is smaller than the
1127 * %deque's current size the %deque is truncated, otherwise
1128 * default constructed elements are appended.
1131 resize(size_type __new_size
)
1133 const size_type __len
= size();
1134 if (__new_size
> __len
)
1135 _M_default_append(__new_size
- __len
);
1136 else if (__new_size
< __len
)
1137 _M_erase_at_end(this->_M_impl
._M_start
1138 + difference_type(__new_size
));
1142 * @brief Resizes the %deque to the specified number of elements.
1143 * @param new_size Number of elements the %deque should contain.
1144 * @param x Data with which new elements should be populated.
1146 * This function will %resize the %deque to the specified
1147 * number of elements. If the number is smaller than the
1148 * %deque's current size the %deque is truncated, otherwise the
1149 * %deque is extended and new elements are populated with given
1153 resize(size_type __new_size
, const value_type
& __x
)
1155 const size_type __len
= size();
1156 if (__new_size
> __len
)
1157 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1158 else if (__new_size
< __len
)
1159 _M_erase_at_end(this->_M_impl
._M_start
1160 + difference_type(__new_size
));
1164 * @brief Resizes the %deque to the specified number of elements.
1165 * @param new_size Number of elements the %deque should contain.
1166 * @param x Data with which new elements should be populated.
1168 * This function will %resize the %deque to the specified
1169 * number of elements. If the number is smaller than the
1170 * %deque's current size the %deque is truncated, otherwise the
1171 * %deque is extended and new elements are populated with given
1175 resize(size_type __new_size
, value_type __x
= value_type())
1177 const size_type __len
= size();
1178 if (__new_size
> __len
)
1179 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1180 else if (__new_size
< __len
)
1181 _M_erase_at_end(this->_M_impl
._M_start
1182 + difference_type(__new_size
));
1186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1187 /** A non-binding request to reduce memory use. */
1190 { std::__shrink_to_fit
<deque
>::_S_do_it(*this); }
1194 * Returns true if the %deque is empty. (Thus begin() would
1199 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1203 * @brief Subscript access to the data contained in the %deque.
1204 * @param n The index of the element for which data should be
1206 * @return Read/write reference to data.
1208 * This operator allows for easy, array-style, data access.
1209 * Note that data access with this operator is unchecked and
1210 * out_of_range lookups are not defined. (For checked lookups
1214 operator[](size_type __n
)
1215 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1218 * @brief Subscript access to the data contained in the %deque.
1219 * @param n The index of the element for which data should be
1221 * @return Read-only (constant) reference to data.
1223 * This operator allows for easy, array-style, data access.
1224 * Note that data access with this operator is unchecked and
1225 * out_of_range lookups are not defined. (For checked lookups
1229 operator[](size_type __n
) const
1230 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1233 /// Safety check used only from at().
1235 _M_range_check(size_type __n
) const
1237 if (__n
>= this->size())
1238 __throw_out_of_range(__N("deque::_M_range_check"));
1243 * @brief Provides access to the data contained in the %deque.
1244 * @param n The index of the element for which data should be
1246 * @return Read/write reference to data.
1247 * @throw std::out_of_range If @a n is an invalid index.
1249 * This function provides for safer data access. The parameter
1250 * is first checked that it is in the range of the deque. The
1251 * function throws out_of_range if the check fails.
1256 _M_range_check(__n
);
1257 return (*this)[__n
];
1261 * @brief Provides access to the data contained in the %deque.
1262 * @param n The index of the element for which data should be
1264 * @return Read-only (constant) reference to data.
1265 * @throw std::out_of_range If @a n is an invalid index.
1267 * This function provides for safer data access. The parameter is first
1268 * checked that it is in the range of the deque. The function throws
1269 * out_of_range if the check fails.
1272 at(size_type __n
) const
1274 _M_range_check(__n
);
1275 return (*this)[__n
];
1279 * Returns a read/write reference to the data at the first
1280 * element of the %deque.
1284 { return *begin(); }
1287 * Returns a read-only (constant) reference to the data at the first
1288 * element of the %deque.
1292 { return *begin(); }
1295 * Returns a read/write reference to the data at the last element of the
1301 iterator __tmp
= end();
1307 * Returns a read-only (constant) reference to the data at the last
1308 * element of the %deque.
1313 const_iterator __tmp
= end();
1318 // [23.2.1.2] modifiers
1320 * @brief Add data to the front of the %deque.
1321 * @param x Data to be added.
1323 * This is a typical stack operation. The function creates an
1324 * element at the front of the %deque and assigns the given
1325 * data to it. Due to the nature of a %deque this operation
1326 * can be done in constant time.
1329 push_front(const value_type
& __x
)
1331 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1333 this->_M_impl
.construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
1334 --this->_M_impl
._M_start
._M_cur
;
1337 _M_push_front_aux(__x
);
1340 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1342 push_front(value_type
&& __x
)
1343 { emplace_front(std::move(__x
)); }
1345 template<typename
... _Args
>
1347 emplace_front(_Args
&&... __args
);
1351 * @brief Add data to the end of the %deque.
1352 * @param x Data to be added.
1354 * This is a typical stack operation. The function creates an
1355 * element at the end of the %deque and assigns the given data
1356 * to it. Due to the nature of a %deque this operation can be
1357 * done in constant time.
1360 push_back(const value_type
& __x
)
1362 if (this->_M_impl
._M_finish
._M_cur
1363 != this->_M_impl
._M_finish
._M_last
- 1)
1365 this->_M_impl
.construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1366 ++this->_M_impl
._M_finish
._M_cur
;
1369 _M_push_back_aux(__x
);
1372 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1374 push_back(value_type
&& __x
)
1375 { emplace_back(std::move(__x
)); }
1377 template<typename
... _Args
>
1379 emplace_back(_Args
&&... __args
);
1383 * @brief Removes first element.
1385 * This is a typical stack operation. It shrinks the %deque by one.
1387 * Note that no data is returned, and if the first element's data is
1388 * needed, it should be retrieved before pop_front() is called.
1393 if (this->_M_impl
._M_start
._M_cur
1394 != this->_M_impl
._M_start
._M_last
- 1)
1396 this->_M_impl
.destroy(this->_M_impl
._M_start
._M_cur
);
1397 ++this->_M_impl
._M_start
._M_cur
;
1404 * @brief Removes last element.
1406 * This is a typical stack operation. It shrinks the %deque by one.
1408 * Note that no data is returned, and if the last element's data is
1409 * needed, it should be retrieved before pop_back() is called.
1414 if (this->_M_impl
._M_finish
._M_cur
1415 != this->_M_impl
._M_finish
._M_first
)
1417 --this->_M_impl
._M_finish
._M_cur
;
1418 this->_M_impl
.destroy(this->_M_impl
._M_finish
._M_cur
);
1424 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1426 * @brief Inserts an object in %deque before specified iterator.
1427 * @param position An iterator into the %deque.
1428 * @param args Arguments.
1429 * @return An iterator that points to the inserted data.
1431 * This function will insert an object of type T constructed
1432 * with T(std::forward<Args>(args)...) before the specified location.
1434 template<typename
... _Args
>
1436 emplace(iterator __position
, _Args
&&... __args
);
1440 * @brief Inserts given value into %deque before specified iterator.
1441 * @param position An iterator into the %deque.
1442 * @param x Data to be inserted.
1443 * @return An iterator that points to the inserted data.
1445 * This function will insert a copy of the given value before the
1446 * specified location.
1449 insert(iterator __position
, const value_type
& __x
);
1451 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1453 * @brief Inserts given rvalue into %deque before specified iterator.
1454 * @param position An iterator into the %deque.
1455 * @param x Data to be inserted.
1456 * @return An iterator that points to the inserted data.
1458 * This function will insert a copy of the given rvalue before the
1459 * specified location.
1462 insert(iterator __position
, value_type
&& __x
)
1463 { return emplace(__position
, std::move(__x
)); }
1466 * @brief Inserts an initializer list into the %deque.
1467 * @param p An iterator into the %deque.
1468 * @param l An initializer_list.
1470 * This function will insert copies of the data in the
1471 * initializer_list @a l into the %deque before the location
1472 * specified by @a p. This is known as <em>list insert</em>.
1475 insert(iterator __p
, initializer_list
<value_type
> __l
)
1476 { this->insert(__p
, __l
.begin(), __l
.end()); }
1480 * @brief Inserts a number of copies of given data into the %deque.
1481 * @param position An iterator into the %deque.
1482 * @param n Number of elements to be inserted.
1483 * @param x Data to be inserted.
1485 * This function will insert a specified number of copies of the given
1486 * data before the location specified by @a position.
1489 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1490 { _M_fill_insert(__position
, __n
, __x
); }
1493 * @brief Inserts a range into the %deque.
1494 * @param position An iterator into the %deque.
1495 * @param first An input iterator.
1496 * @param last An input iterator.
1498 * This function will insert copies of the data in the range
1499 * [first,last) into the %deque before the location specified
1500 * by @a pos. This is known as <em>range insert</em>.
1502 template<typename _InputIterator
>
1504 insert(iterator __position
, _InputIterator __first
,
1505 _InputIterator __last
)
1507 // Check whether it's an integral type. If so, it's not an iterator.
1508 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1509 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1513 * @brief Remove element at given position.
1514 * @param position Iterator pointing to element to be erased.
1515 * @return An iterator pointing to the next element (or end()).
1517 * This function will erase the element at the given position and thus
1518 * shorten the %deque by one.
1520 * The user is cautioned that
1521 * this function only erases the element, and that if the element is
1522 * itself a pointer, the pointed-to memory is not touched in any way.
1523 * Managing the pointer is the user's responsibility.
1526 erase(iterator __position
);
1529 * @brief Remove a range of elements.
1530 * @param first Iterator pointing to the first element to be erased.
1531 * @param last Iterator pointing to one past the last element to be
1533 * @return An iterator pointing to the element pointed to by @a last
1534 * prior to erasing (or end()).
1536 * This function will erase the elements in the range [first,last) and
1537 * shorten the %deque accordingly.
1539 * The user is cautioned that
1540 * this function only erases the elements, and that if the elements
1541 * themselves are pointers, the pointed-to memory is not touched in any
1542 * way. Managing the pointer is the user's responsibility.
1545 erase(iterator __first
, iterator __last
);
1548 * @brief Swaps data with another %deque.
1549 * @param x A %deque of the same element and allocator types.
1551 * This exchanges the elements between two deques in constant time.
1552 * (Four pointers, so it should be quite fast.)
1553 * Note that the global std::swap() function is specialized such that
1554 * std::swap(d1,d2) will feed to this function.
1559 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1560 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1561 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1562 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1564 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1565 // 431. Swapping containers with unequal allocators.
1566 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1567 __x
._M_get_Tp_allocator());
1571 * Erases all the elements. Note that this function only erases the
1572 * elements, and that if the elements themselves are pointers, the
1573 * pointed-to memory is not touched in any way. Managing the pointer is
1574 * the user's responsibility.
1578 { _M_erase_at_end(begin()); }
1581 // Internal constructor functions follow.
1583 // called by the range constructor to implement [23.1.1]/9
1585 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1586 // 438. Ambiguity in the "do the right thing" clause
1587 template<typename _Integer
>
1589 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1591 _M_initialize_map(static_cast<size_type
>(__n
));
1592 _M_fill_initialize(__x
);
1595 // called by the range constructor to implement [23.1.1]/9
1596 template<typename _InputIterator
>
1598 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1601 typedef typename
std::iterator_traits
<_InputIterator
>::
1602 iterator_category _IterCategory
;
1603 _M_range_initialize(__first
, __last
, _IterCategory());
1606 // called by the second initialize_dispatch above
1609 * @brief Fills the deque with whatever is in [first,last).
1610 * @param first An input iterator.
1611 * @param last An input iterator.
1614 * If the iterators are actually forward iterators (or better), then the
1615 * memory layout can be done all at once. Else we move forward using
1616 * push_back on each value from the iterator.
1618 template<typename _InputIterator
>
1620 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1621 std::input_iterator_tag
);
1623 // called by the second initialize_dispatch above
1624 template<typename _ForwardIterator
>
1626 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1627 std::forward_iterator_tag
);
1631 * @brief Fills the %deque with copies of value.
1632 * @param value Initial value.
1634 * @pre _M_start and _M_finish have already been initialized,
1635 * but none of the %deque's elements have yet been constructed.
1637 * This function is called only when the user provides an explicit size
1638 * (with or without an explicit exemplar value).
1641 _M_fill_initialize(const value_type
& __value
);
1643 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1644 // called by deque(n).
1646 _M_default_initialize();
1649 // Internal assign functions follow. The *_aux functions do the actual
1650 // assignment work for the range versions.
1652 // called by the range assign to implement [23.1.1]/9
1654 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1655 // 438. Ambiguity in the "do the right thing" clause
1656 template<typename _Integer
>
1658 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1659 { _M_fill_assign(__n
, __val
); }
1661 // called by the range assign to implement [23.1.1]/9
1662 template<typename _InputIterator
>
1664 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1667 typedef typename
std::iterator_traits
<_InputIterator
>::
1668 iterator_category _IterCategory
;
1669 _M_assign_aux(__first
, __last
, _IterCategory());
1672 // called by the second assign_dispatch above
1673 template<typename _InputIterator
>
1675 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1676 std::input_iterator_tag
);
1678 // called by the second assign_dispatch above
1679 template<typename _ForwardIterator
>
1681 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1682 std::forward_iterator_tag
)
1684 const size_type __len
= std::distance(__first
, __last
);
1687 _ForwardIterator __mid
= __first
;
1688 std::advance(__mid
, size());
1689 std::copy(__first
, __mid
, begin());
1690 insert(end(), __mid
, __last
);
1693 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1696 // Called by assign(n,t), and the range assign when it turns out
1697 // to be the same thing.
1699 _M_fill_assign(size_type __n
, const value_type
& __val
)
1703 std::fill(begin(), end(), __val
);
1704 insert(end(), __n
- size(), __val
);
1708 _M_erase_at_end(begin() + difference_type(__n
));
1709 std::fill(begin(), end(), __val
);
1714 /// Helper functions for push_* and pop_*.
1715 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1716 void _M_push_back_aux(const value_type
&);
1718 void _M_push_front_aux(const value_type
&);
1720 template<typename
... _Args
>
1721 void _M_push_back_aux(_Args
&&... __args
);
1723 template<typename
... _Args
>
1724 void _M_push_front_aux(_Args
&&... __args
);
1727 void _M_pop_back_aux();
1729 void _M_pop_front_aux();
1732 // Internal insert functions follow. The *_aux functions do the actual
1733 // insertion work when all shortcuts fail.
1735 // called by the range insert to implement [23.1.1]/9
1737 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1738 // 438. Ambiguity in the "do the right thing" clause
1739 template<typename _Integer
>
1741 _M_insert_dispatch(iterator __pos
,
1742 _Integer __n
, _Integer __x
, __true_type
)
1743 { _M_fill_insert(__pos
, __n
, __x
); }
1745 // called by the range insert to implement [23.1.1]/9
1746 template<typename _InputIterator
>
1748 _M_insert_dispatch(iterator __pos
,
1749 _InputIterator __first
, _InputIterator __last
,
1752 typedef typename
std::iterator_traits
<_InputIterator
>::
1753 iterator_category _IterCategory
;
1754 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1757 // called by the second insert_dispatch above
1758 template<typename _InputIterator
>
1760 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1761 _InputIterator __last
, std::input_iterator_tag
);
1763 // called by the second insert_dispatch above
1764 template<typename _ForwardIterator
>
1766 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1767 _ForwardIterator __last
, std::forward_iterator_tag
);
1769 // Called by insert(p,n,x), and the range insert when it turns out to be
1770 // the same thing. Can use fill functions in optimal situations,
1771 // otherwise passes off to insert_aux(p,n,x).
1773 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1775 // called by insert(p,x)
1776 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1778 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1780 template<typename
... _Args
>
1782 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
1785 // called by insert(p,n,x) via fill_insert
1787 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1789 // called by range_insert_aux for forward iterators
1790 template<typename _ForwardIterator
>
1792 _M_insert_aux(iterator __pos
,
1793 _ForwardIterator __first
, _ForwardIterator __last
,
1797 // Internal erase functions follow.
1800 _M_destroy_data_aux(iterator __first
, iterator __last
);
1802 // Called by ~deque().
1803 // NB: Doesn't deallocate the nodes.
1804 template<typename _Alloc1
>
1806 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
1807 { _M_destroy_data_aux(__first
, __last
); }
1810 _M_destroy_data(iterator __first
, iterator __last
,
1811 const std::allocator
<_Tp
>&)
1813 if (!__has_trivial_destructor(value_type
))
1814 _M_destroy_data_aux(__first
, __last
);
1817 // Called by erase(q1, q2).
1819 _M_erase_at_begin(iterator __pos
)
1821 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
1822 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
1823 this->_M_impl
._M_start
= __pos
;
1826 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1827 // _M_fill_assign, operator=.
1829 _M_erase_at_end(iterator __pos
)
1831 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
1832 _M_destroy_nodes(__pos
._M_node
+ 1,
1833 this->_M_impl
._M_finish
._M_node
+ 1);
1834 this->_M_impl
._M_finish
= __pos
;
1837 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1838 // Called by resize(sz).
1840 _M_default_append(size_type __n
);
1844 /// Memory-handling helpers for the previous internal insert functions.
1846 _M_reserve_elements_at_front(size_type __n
)
1848 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1849 - this->_M_impl
._M_start
._M_first
;
1850 if (__n
> __vacancies
)
1851 _M_new_elements_at_front(__n
- __vacancies
);
1852 return this->_M_impl
._M_start
- difference_type(__n
);
1856 _M_reserve_elements_at_back(size_type __n
)
1858 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1859 - this->_M_impl
._M_finish
._M_cur
) - 1;
1860 if (__n
> __vacancies
)
1861 _M_new_elements_at_back(__n
- __vacancies
);
1862 return this->_M_impl
._M_finish
+ difference_type(__n
);
1866 _M_new_elements_at_front(size_type __new_elements
);
1869 _M_new_elements_at_back(size_type __new_elements
);
1875 * @brief Memory-handling helpers for the major %map.
1877 * Makes sure the _M_map has space for new nodes. Does not
1878 * actually add the nodes. Can invalidate _M_map pointers.
1879 * (And consequently, %deque iterators.)
1882 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
1884 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1885 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
1886 _M_reallocate_map(__nodes_to_add
, false);
1890 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
1892 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
1893 - this->_M_impl
._M_map
))
1894 _M_reallocate_map(__nodes_to_add
, true);
1898 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
1904 * @brief Deque equality comparison.
1905 * @param x A %deque.
1906 * @param y A %deque of the same type as @a x.
1907 * @return True iff the size and elements of the deques are equal.
1909 * This is an equivalence relation. It is linear in the size of the
1910 * deques. Deques are considered equivalent if their sizes are equal,
1911 * and if corresponding elements compare equal.
1913 template<typename _Tp
, typename _Alloc
>
1915 operator==(const deque
<_Tp
, _Alloc
>& __x
,
1916 const deque
<_Tp
, _Alloc
>& __y
)
1917 { return __x
.size() == __y
.size()
1918 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
1921 * @brief Deque ordering relation.
1922 * @param x A %deque.
1923 * @param y A %deque of the same type as @a x.
1924 * @return True iff @a x is lexicographically less than @a y.
1926 * This is a total ordering relation. It is linear in the size of the
1927 * deques. The elements must be comparable with @c <.
1929 * See std::lexicographical_compare() for how the determination is made.
1931 template<typename _Tp
, typename _Alloc
>
1933 operator<(const deque
<_Tp
, _Alloc
>& __x
,
1934 const deque
<_Tp
, _Alloc
>& __y
)
1935 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1936 __y
.begin(), __y
.end()); }
1938 /// Based on operator==
1939 template<typename _Tp
, typename _Alloc
>
1941 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
1942 const deque
<_Tp
, _Alloc
>& __y
)
1943 { return !(__x
== __y
); }
1945 /// Based on operator<
1946 template<typename _Tp
, typename _Alloc
>
1948 operator>(const deque
<_Tp
, _Alloc
>& __x
,
1949 const deque
<_Tp
, _Alloc
>& __y
)
1950 { return __y
< __x
; }
1952 /// Based on operator<
1953 template<typename _Tp
, typename _Alloc
>
1955 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
1956 const deque
<_Tp
, _Alloc
>& __y
)
1957 { return !(__y
< __x
); }
1959 /// Based on operator<
1960 template<typename _Tp
, typename _Alloc
>
1962 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
1963 const deque
<_Tp
, _Alloc
>& __y
)
1964 { return !(__x
< __y
); }
1966 /// See std::deque::swap().
1967 template<typename _Tp
, typename _Alloc
>
1969 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
1972 #undef _GLIBCXX_DEQUE_BUF_SIZE
1974 _GLIBCXX_END_NESTED_NAMESPACE
1976 #endif /* _STL_DEQUE_H */