1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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.
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
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 seems to
73 * be a useful wrapper around a repeated constant expression. The '512' is
74 * tunable (and no other code needs to change), but no investigation has
75 * been done since inheriting the SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE
76 * only if you know what you are doing, however: changing it breaks the
77 * binary compatibility!!
80 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
81 #define _GLIBCXX_DEQUE_BUF_SIZE 512
85 __deque_buf_size(size_t __size
)
86 { return (__size
< _GLIBCXX_DEQUE_BUF_SIZE
87 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE
/ __size
) : size_t(1)); }
91 * @brief A deque::iterator.
93 * Quite a bit of intelligence here. Much of the functionality of
94 * deque is actually passed off to this class. A deque holds two
95 * of these internally, marking its valid range. Access to
96 * elements is done as offsets of either of those two, relying on
97 * operator overloading in this class.
99 * All the functions are op overloads except for _M_set_node.
101 template<typename _Tp
, typename _Ref
, typename _Ptr
>
102 struct _Deque_iterator
104 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
105 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
107 static size_t _S_buffer_size()
108 { return __deque_buf_size(sizeof(_Tp
)); }
110 typedef std::random_access_iterator_tag iterator_category
;
111 typedef _Tp value_type
;
112 typedef _Ptr pointer
;
113 typedef _Ref reference
;
114 typedef size_t size_type
;
115 typedef ptrdiff_t difference_type
;
116 typedef _Tp
** _Map_pointer
;
117 typedef _Deque_iterator _Self
;
122 _Map_pointer _M_node
;
124 _Deque_iterator(_Tp
* __x
, _Map_pointer __y
)
125 : _M_cur(__x
), _M_first(*__y
),
126 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
129 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
131 _Deque_iterator(const iterator
& __x
)
132 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
133 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
147 if (_M_cur
== _M_last
)
149 _M_set_node(_M_node
+ 1);
166 if (_M_cur
== _M_first
)
168 _M_set_node(_M_node
- 1);
184 operator+=(difference_type __n
)
186 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
187 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
191 const difference_type __node_offset
=
192 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
193 : -difference_type((-__offset
- 1)
194 / _S_buffer_size()) - 1;
195 _M_set_node(_M_node
+ __node_offset
);
196 _M_cur
= _M_first
+ (__offset
- __node_offset
197 * difference_type(_S_buffer_size()));
203 operator+(difference_type __n
) const
210 operator-=(difference_type __n
)
211 { return *this += -__n
; }
214 operator-(difference_type __n
) const
221 operator[](difference_type __n
) const
222 { return *(*this + __n
); }
225 * Prepares to traverse new_node. Sets everything except
226 * _M_cur, which should therefore be set by the caller
227 * immediately afterwards, based on _M_first and _M_last.
230 _M_set_node(_Map_pointer __new_node
)
232 _M_node
= __new_node
;
233 _M_first
= *__new_node
;
234 _M_last
= _M_first
+ difference_type(_S_buffer_size());
238 // Note: we also provide overloads whose operands are of the same type in
239 // order to avoid ambiguous overload resolution when std::rel_ops operators
240 // are in scope (for additional details, see libstdc++/3628)
241 template<typename _Tp
, typename _Ref
, typename _Ptr
>
243 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
244 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
245 { return __x
._M_cur
== __y
._M_cur
; }
247 template<typename _Tp
, typename _RefL
, typename _PtrL
,
248 typename _RefR
, typename _PtrR
>
250 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
251 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
252 { return __x
._M_cur
== __y
._M_cur
; }
254 template<typename _Tp
, typename _Ref
, typename _Ptr
>
256 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
257 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
258 { return !(__x
== __y
); }
260 template<typename _Tp
, typename _RefL
, typename _PtrL
,
261 typename _RefR
, typename _PtrR
>
263 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
264 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
265 { return !(__x
== __y
); }
267 template<typename _Tp
, typename _Ref
, typename _Ptr
>
269 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
270 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
271 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
272 : (__x
._M_node
< __y
._M_node
); }
274 template<typename _Tp
, typename _RefL
, typename _PtrL
,
275 typename _RefR
, typename _PtrR
>
277 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
278 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
279 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
280 : (__x
._M_node
< __y
._M_node
); }
282 template<typename _Tp
, typename _Ref
, typename _Ptr
>
284 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
285 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
286 { return __y
< __x
; }
288 template<typename _Tp
, typename _RefL
, typename _PtrL
,
289 typename _RefR
, typename _PtrR
>
291 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
292 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
293 { return __y
< __x
; }
295 template<typename _Tp
, typename _Ref
, typename _Ptr
>
297 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
298 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
299 { return !(__y
< __x
); }
301 template<typename _Tp
, typename _RefL
, typename _PtrL
,
302 typename _RefR
, typename _PtrR
>
304 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
305 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
306 { return !(__y
< __x
); }
308 template<typename _Tp
, typename _Ref
, typename _Ptr
>
310 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
311 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
312 { return !(__x
< __y
); }
314 template<typename _Tp
, typename _RefL
, typename _PtrL
,
315 typename _RefR
, typename _PtrR
>
317 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
318 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
319 { return !(__x
< __y
); }
321 // _GLIBCXX_RESOLVE_LIB_DEFECTS
322 // According to the resolution of DR179 not only the various comparison
323 // operators but also operator- must accept mixed iterator/const_iterator
325 template<typename _Tp
, typename _Ref
, typename _Ptr
>
326 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
327 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
328 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
330 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
331 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
332 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
333 + (__y
._M_last
- __y
._M_cur
);
336 template<typename _Tp
, typename _RefL
, typename _PtrL
,
337 typename _RefR
, typename _PtrR
>
338 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
339 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
340 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
342 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
343 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
344 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
345 + (__y
._M_last
- __y
._M_cur
);
348 template<typename _Tp
, typename _Ref
, typename _Ptr
>
349 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
350 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
351 { return __x
+ __n
; }
353 template<typename _Tp
>
355 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
356 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
358 template<typename _Tp
>
359 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
360 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
361 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
362 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
364 template<typename _Tp
>
365 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
366 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
367 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
368 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
369 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
370 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
373 #ifdef __GXX_EXPERIMENTAL_CXX0X__
374 template<typename _Tp
>
375 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
376 move(_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 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
383 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
384 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
385 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
386 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
391 * Deque base class. This class provides the unified face for %deque's
392 * allocation. This class's constructor and destructor allocate and
393 * deallocate (but do not initialize) storage. This makes %exception
396 * Nothing in this class ever constructs or destroys an actual Tp element.
397 * (Deque handles that itself.) Only/All memory management is performed
400 template<typename _Tp
, typename _Alloc
>
404 typedef _Alloc allocator_type
;
407 get_allocator() const
408 { return allocator_type(_M_get_Tp_allocator()); }
410 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
411 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
415 { _M_initialize_map(0); }
417 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
419 { _M_initialize_map(__num_elements
); }
421 _Deque_base(const allocator_type
& __a
)
425 #ifdef __GXX_EXPERIMENTAL_CXX0X__
426 _Deque_base(_Deque_base
&& __x
)
427 : _M_impl(__x
._M_get_Tp_allocator())
429 _M_initialize_map(0);
430 if (__x
._M_impl
._M_map
)
432 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
433 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
434 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
435 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
443 //This struct encapsulates the implementation of the std::deque
444 //standard container and at the same time makes use of the EBO
445 //for empty allocators.
446 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
448 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
451 : public _Tp_alloc_type
459 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
460 _M_start(), _M_finish()
463 _Deque_impl(const _Tp_alloc_type
& __a
)
464 : _Tp_alloc_type(__a
), _M_map(0), _M_map_size(0),
465 _M_start(), _M_finish()
470 _M_get_Tp_allocator()
471 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
473 const _Tp_alloc_type
&
474 _M_get_Tp_allocator() const
475 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
478 _M_get_map_allocator() const
479 { return _Map_alloc_type(_M_get_Tp_allocator()); }
484 return _M_impl
._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp
)));
488 _M_deallocate_node(_Tp
* __p
)
490 _M_impl
._Tp_alloc_type::deallocate(__p
, __deque_buf_size(sizeof(_Tp
)));
494 _M_allocate_map(size_t __n
)
495 { return _M_get_map_allocator().allocate(__n
); }
498 _M_deallocate_map(_Tp
** __p
, size_t __n
)
499 { _M_get_map_allocator().deallocate(__p
, __n
); }
502 void _M_initialize_map(size_t);
503 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
504 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
505 enum { _S_initial_map_size
= 8 };
510 template<typename _Tp
, typename _Alloc
>
511 _Deque_base
<_Tp
, _Alloc
>::
514 if (this->_M_impl
._M_map
)
516 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
517 this->_M_impl
._M_finish
._M_node
+ 1);
518 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
523 * @brief Layout storage.
524 * @param num_elements The count of T's for which to allocate space
528 * The initial underlying memory layout is a bit complicated...
530 template<typename _Tp
, typename _Alloc
>
532 _Deque_base
<_Tp
, _Alloc
>::
533 _M_initialize_map(size_t __num_elements
)
535 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
538 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
539 size_t(__num_nodes
+ 2));
540 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
542 // For "small" maps (needing less than _M_map_size nodes), allocation
543 // starts in the middle elements and grows outwards. So nstart may be
544 // the beginning of _M_map, but for small maps it may be as far in as
547 _Tp
** __nstart
= (this->_M_impl
._M_map
548 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
549 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
552 { _M_create_nodes(__nstart
, __nfinish
); }
555 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
556 this->_M_impl
._M_map
= 0;
557 this->_M_impl
._M_map_size
= 0;
558 __throw_exception_again
;
561 this->_M_impl
._M_start
._M_set_node(__nstart
);
562 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
563 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
564 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
566 % __deque_buf_size(sizeof(_Tp
)));
569 template<typename _Tp
, typename _Alloc
>
571 _Deque_base
<_Tp
, _Alloc
>::
572 _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
577 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
578 *__cur
= this->_M_allocate_node();
582 _M_destroy_nodes(__nstart
, __cur
);
583 __throw_exception_again
;
587 template<typename _Tp
, typename _Alloc
>
589 _Deque_base
<_Tp
, _Alloc
>::
590 _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
592 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
593 _M_deallocate_node(*__n
);
597 * @brief A standard container using fixed-size memory allocation and
598 * constant-time manipulation of elements at either end.
602 * Meets the requirements of a <a href="tables.html#65">container</a>, a
603 * <a href="tables.html#66">reversible container</a>, and a
604 * <a href="tables.html#67">sequence</a>, including the
605 * <a href="tables.html#68">optional sequence requirements</a>.
607 * In previous HP/SGI versions of deque, there was an extra template
608 * parameter so users could control the node size. This extension turned
609 * out to violate the C++ standard (it can be detected using template
610 * template parameters), and it was removed.
612 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
615 * - size_t _M_map_size
616 * - iterator _M_start, _M_finish
618 * map_size is at least 8. %map is an array of map_size
619 * pointers-to-"nodes". (The name %map has nothing to do with the
620 * std::map class, and "nodes" should not be confused with
621 * std::list's usage of "node".)
623 * A "node" has no specific type name as such, but it is referred
624 * to as "node" in this file. It is a simple array-of-Tp. If Tp
625 * is very large, there will be one Tp element per node (i.e., an
626 * "array" of one). For non-huge Tp's, node size is inversely
627 * related to Tp size: the larger the Tp, the fewer Tp's will fit
628 * in a node. The goal here is to keep the total size of a node
629 * relatively small and constant over different Tp's, to improve
630 * allocator efficiency.
632 * Not every pointer in the %map array will point to a node. If
633 * the initial number of elements in the deque is small, the
634 * /middle/ %map pointers will be valid, and the ones at the edges
635 * will be unused. This same situation will arise as the %map
636 * grows: available %map pointers, if any, will be on the ends. As
637 * new nodes are created, only a subset of the %map's pointers need
638 * to be copied "outward".
641 * - For any nonsingular iterator i:
642 * - i.node points to a member of the %map array. (Yes, you read that
643 * correctly: i.node does not actually point to a node.) The member of
644 * the %map array is what actually points to the node.
645 * - i.first == *(i.node) (This points to the node (first Tp element).)
646 * - i.last == i.first + node_size
647 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
648 * the implication of this is that i.cur is always a dereferenceable
649 * pointer, even if i is a past-the-end iterator.
650 * - Start and Finish are always nonsingular iterators. NOTE: this
651 * means that an empty deque must have one node, a deque with <N
652 * elements (where N is the node buffer size) must have one node, a
653 * deque with N through (2N-1) elements must have two nodes, etc.
654 * - For every node other than start.node and finish.node, every
655 * element in the node is an initialized object. If start.node ==
656 * finish.node, then [start.cur, finish.cur) are initialized
657 * objects, and the elements outside that range are uninitialized
658 * storage. Otherwise, [start.cur, start.last) and [finish.first,
659 * finish.cur) are initialized objects, and [start.first, start.cur)
660 * and [finish.cur, finish.last) are uninitialized storage.
661 * - [%map, %map + map_size) is a valid, non-empty range.
662 * - [start.node, finish.node] is a valid range contained within
663 * [%map, %map + map_size).
664 * - A pointer in the range [%map, %map + map_size) points to an allocated
665 * node if and only if the pointer is in the range
666 * [start.node, finish.node].
668 * Here's the magic: nothing in deque is "aware" of the discontiguous
671 * The memory setup and layout occurs in the parent, _Base, and the iterator
672 * class is entirely responsible for "leaping" from one node to the next.
673 * All the implementation routines for deque itself work only through the
674 * start and finish iterators. This keeps the routines simple and sane,
675 * and we can use other standard algorithms as well.
677 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
678 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
680 // concept requirements
681 typedef typename
_Alloc::value_type _Alloc_value_type
;
682 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
683 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
685 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
686 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
689 typedef _Tp value_type
;
690 typedef typename
_Tp_alloc_type::pointer pointer
;
691 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
692 typedef typename
_Tp_alloc_type::reference reference
;
693 typedef typename
_Tp_alloc_type::const_reference const_reference
;
694 typedef typename
_Base::iterator iterator
;
695 typedef typename
_Base::const_iterator const_iterator
;
696 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
697 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
698 typedef size_t size_type
;
699 typedef ptrdiff_t difference_type
;
700 typedef _Alloc allocator_type
;
703 typedef pointer
* _Map_pointer
;
705 static size_t _S_buffer_size()
706 { return __deque_buf_size(sizeof(_Tp
)); }
708 // Functions controlling memory layout, and nothing else.
709 using _Base::_M_initialize_map
;
710 using _Base::_M_create_nodes
;
711 using _Base::_M_destroy_nodes
;
712 using _Base::_M_allocate_node
;
713 using _Base::_M_deallocate_node
;
714 using _Base::_M_allocate_map
;
715 using _Base::_M_deallocate_map
;
716 using _Base::_M_get_Tp_allocator
;
719 * A total of four data members accumulated down the hierarchy.
720 * May be accessed via _M_impl.*
722 using _Base::_M_impl
;
725 // [23.2.1.1] construct/copy/destroy
726 // (assign() and get_allocator() are also listed in this section)
728 * @brief Default constructor creates no elements.
734 * @brief Creates a %deque with no elements.
735 * @param a An allocator object.
738 deque(const allocator_type
& __a
)
742 * @brief Creates a %deque with copies of an exemplar element.
743 * @param n The number of elements to initially create.
744 * @param value An element to copy.
745 * @param a An allocator.
747 * This constructor fills the %deque with @a n copies of @a value.
750 deque(size_type __n
, const value_type
& __value
= value_type(),
751 const allocator_type
& __a
= allocator_type())
753 { _M_fill_initialize(__value
); }
756 * @brief %Deque copy constructor.
757 * @param x A %deque of identical element and allocator types.
759 * The newly-created %deque uses a copy of the allocation object used
762 deque(const deque
& __x
)
763 : _Base(__x
._M_get_Tp_allocator(), __x
.size())
764 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
765 this->_M_impl
._M_start
,
766 _M_get_Tp_allocator()); }
768 #ifdef __GXX_EXPERIMENTAL_CXX0X__
770 * @brief %Deque move constructor.
771 * @param x A %deque of identical element and allocator types.
773 * The newly-created %deque contains the exact contents of @a x.
774 * The contents of @a x are a valid, but unspecified %deque.
777 : _Base(std::forward
<_Base
>(__x
)) { }
780 * @brief Builds a %deque from an initializer list.
781 * @param l An initializer_list.
782 * @param a An allocator object.
784 * Create a %deque consisting of copies of the elements in the
785 * initializer_list @a l.
787 * This will call the element type's copy constructor N times
788 * (where N is l.size()) and do no memory reallocation.
790 deque(initializer_list
<value_type
> __l
,
791 const allocator_type
& __a
= allocator_type())
794 _M_range_initialize(__l
.begin(), __l
.end(),
795 random_access_iterator_tag());
800 * @brief Builds a %deque from a range.
801 * @param first An input iterator.
802 * @param last An input iterator.
803 * @param a An allocator object.
805 * Create a %deque consisting of copies of the elements from [first,
808 * If the iterators are forward, bidirectional, or random-access, then
809 * this will call the elements' copy constructor N times (where N is
810 * distance(first,last)) and do no memory reallocation. But if only
811 * input iterators are used, then this will do at most 2N calls to the
812 * copy constructor, and logN memory reallocations.
814 template<typename _InputIterator
>
815 deque(_InputIterator __first
, _InputIterator __last
,
816 const allocator_type
& __a
= allocator_type())
819 // Check whether it's an integral type. If so, it's not an iterator.
820 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
821 _M_initialize_dispatch(__first
, __last
, _Integral());
825 * The dtor only erases the elements, and note that if the elements
826 * themselves are pointers, the pointed-to memory is not touched in any
827 * way. Managing the pointer is the user's responsibility.
830 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
833 * @brief %Deque assignment operator.
834 * @param x A %deque of identical element and allocator types.
836 * All the elements of @a x are copied, but unlike the copy constructor,
837 * the allocator object is not copied.
840 operator=(const deque
& __x
);
842 #ifdef __GXX_EXPERIMENTAL_CXX0X__
844 * @brief %Deque move assignment operator.
845 * @param x A %deque of identical element and allocator types.
847 * The contents of @a x are moved into this deque (without copying).
848 * @a x is a valid, but unspecified %deque.
851 operator=(deque
&& __x
)
860 * @brief Assigns an initializer list to a %deque.
861 * @param l An initializer_list.
863 * This function fills a %deque with copies of the elements in the
864 * initializer_list @a l.
866 * Note that the assignment completely changes the %deque and that the
867 * resulting %deque's size is the same as the number of elements
868 * assigned. Old data may be lost.
871 operator=(initializer_list
<value_type
> __l
)
873 this->assign(__l
.begin(), __l
.end());
879 * @brief Assigns a given value to a %deque.
880 * @param n Number of elements to be assigned.
881 * @param val Value to be assigned.
883 * This function fills a %deque with @a n copies of the given
884 * value. Note that the assignment completely changes the
885 * %deque and that the resulting %deque's size is the same as
886 * the number of elements assigned. Old data may be lost.
889 assign(size_type __n
, const value_type
& __val
)
890 { _M_fill_assign(__n
, __val
); }
893 * @brief Assigns a range to a %deque.
894 * @param first An input iterator.
895 * @param last An input iterator.
897 * This function fills a %deque with copies of the elements in the
898 * range [first,last).
900 * Note that the assignment completely changes the %deque and that the
901 * resulting %deque's size is the same as the number of elements
902 * assigned. Old data may be lost.
904 template<typename _InputIterator
>
906 assign(_InputIterator __first
, _InputIterator __last
)
908 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
909 _M_assign_dispatch(__first
, __last
, _Integral());
912 #ifdef __GXX_EXPERIMENTAL_CXX0X__
914 * @brief Assigns an initializer list to a %deque.
915 * @param l An initializer_list.
917 * This function fills a %deque with copies of the elements in the
918 * initializer_list @a l.
920 * Note that the assignment completely changes the %deque and that the
921 * resulting %deque's size is the same as the number of elements
922 * assigned. Old data may be lost.
925 assign(initializer_list
<value_type
> __l
)
926 { this->assign(__l
.begin(), __l
.end()); }
929 /// Get a copy of the memory allocation object.
931 get_allocator() const
932 { return _Base::get_allocator(); }
936 * Returns a read/write iterator that points to the first element in the
937 * %deque. Iteration is done in ordinary element order.
941 { return this->_M_impl
._M_start
; }
944 * Returns a read-only (constant) iterator that points to the first
945 * element in the %deque. Iteration is done in ordinary element order.
949 { return this->_M_impl
._M_start
; }
952 * Returns a read/write iterator that points one past the last
953 * element in the %deque. Iteration is done in ordinary
958 { return this->_M_impl
._M_finish
; }
961 * Returns a read-only (constant) iterator that points one past
962 * the last element in the %deque. Iteration is done in
963 * ordinary element order.
967 { return this->_M_impl
._M_finish
; }
970 * Returns a read/write reverse iterator that points to the
971 * last element in the %deque. Iteration is done in reverse
976 { return reverse_iterator(this->_M_impl
._M_finish
); }
979 * Returns a read-only (constant) reverse iterator that points
980 * to the last element in the %deque. Iteration is done in
981 * reverse element order.
983 const_reverse_iterator
985 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
988 * Returns a read/write reverse iterator that points to one
989 * before the first element in the %deque. Iteration is done
990 * in reverse element order.
994 { return reverse_iterator(this->_M_impl
._M_start
); }
997 * Returns a read-only (constant) reverse iterator that points
998 * to one before the first element in the %deque. Iteration is
999 * done in reverse element order.
1001 const_reverse_iterator
1003 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1005 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1007 * Returns a read-only (constant) iterator that points to the first
1008 * element in the %deque. Iteration is done in ordinary element order.
1012 { return this->_M_impl
._M_start
; }
1015 * Returns a read-only (constant) iterator that points one past
1016 * the last element in the %deque. Iteration is done in
1017 * ordinary element order.
1021 { return this->_M_impl
._M_finish
; }
1024 * Returns a read-only (constant) reverse iterator that points
1025 * to the last element in the %deque. Iteration is done in
1026 * reverse element order.
1028 const_reverse_iterator
1030 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1033 * Returns a read-only (constant) reverse iterator that points
1034 * to one before the first element in the %deque. Iteration is
1035 * done in reverse element order.
1037 const_reverse_iterator
1039 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1042 // [23.2.1.2] capacity
1043 /** Returns the number of elements in the %deque. */
1046 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1048 /** Returns the size() of the largest possible %deque. */
1051 { return _M_get_Tp_allocator().max_size(); }
1054 * @brief Resizes the %deque to the specified number of elements.
1055 * @param new_size Number of elements the %deque should contain.
1056 * @param x Data with which new elements should be populated.
1058 * This function will %resize the %deque to the specified
1059 * number of elements. If the number is smaller than the
1060 * %deque's current size the %deque is truncated, otherwise the
1061 * %deque is extended and new elements are populated with given
1065 resize(size_type __new_size
, value_type __x
= value_type())
1067 const size_type __len
= size();
1068 if (__new_size
< __len
)
1069 _M_erase_at_end(this->_M_impl
._M_start
+ difference_type(__new_size
));
1071 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1075 * Returns true if the %deque is empty. (Thus begin() would
1080 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1084 * @brief Subscript access to the data contained in the %deque.
1085 * @param n The index of the element for which data should be
1087 * @return Read/write reference to data.
1089 * This operator allows for easy, array-style, data access.
1090 * Note that data access with this operator is unchecked and
1091 * out_of_range lookups are not defined. (For checked lookups
1095 operator[](size_type __n
)
1096 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1099 * @brief Subscript access to the data contained in the %deque.
1100 * @param n The index of the element for which data should be
1102 * @return Read-only (constant) reference to data.
1104 * This operator allows for easy, array-style, data access.
1105 * Note that data access with this operator is unchecked and
1106 * out_of_range lookups are not defined. (For checked lookups
1110 operator[](size_type __n
) const
1111 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1114 /// Safety check used only from at().
1116 _M_range_check(size_type __n
) const
1118 if (__n
>= this->size())
1119 __throw_out_of_range(__N("deque::_M_range_check"));
1124 * @brief Provides access to the data contained in the %deque.
1125 * @param n The index of the element for which data should be
1127 * @return Read/write reference to data.
1128 * @throw std::out_of_range If @a n is an invalid index.
1130 * This function provides for safer data access. The parameter
1131 * is first checked that it is in the range of the deque. The
1132 * function throws out_of_range if the check fails.
1137 _M_range_check(__n
);
1138 return (*this)[__n
];
1142 * @brief Provides access to the data contained in the %deque.
1143 * @param n The index of the element for which data should be
1145 * @return Read-only (constant) reference to data.
1146 * @throw std::out_of_range If @a n is an invalid index.
1148 * This function provides for safer data access. The parameter is first
1149 * checked that it is in the range of the deque. The function throws
1150 * out_of_range if the check fails.
1153 at(size_type __n
) const
1155 _M_range_check(__n
);
1156 return (*this)[__n
];
1160 * Returns a read/write reference to the data at the first
1161 * element of the %deque.
1165 { return *begin(); }
1168 * Returns a read-only (constant) reference to the data at the first
1169 * element of the %deque.
1173 { return *begin(); }
1176 * Returns a read/write reference to the data at the last element of the
1182 iterator __tmp
= end();
1188 * Returns a read-only (constant) reference to the data at the last
1189 * element of the %deque.
1194 const_iterator __tmp
= end();
1199 // [23.2.1.2] modifiers
1201 * @brief Add data to the front of the %deque.
1202 * @param x Data to be added.
1204 * This is a typical stack operation. The function creates an
1205 * element at the front of the %deque and assigns the given
1206 * data to it. Due to the nature of a %deque this operation
1207 * can be done in constant time.
1210 push_front(const value_type
& __x
)
1212 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1214 this->_M_impl
.construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
1215 --this->_M_impl
._M_start
._M_cur
;
1218 _M_push_front_aux(__x
);
1221 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1223 push_front(value_type
&& __x
)
1224 { emplace_front(std::move(__x
)); }
1226 template<typename
... _Args
>
1228 emplace_front(_Args
&&... __args
);
1232 * @brief Add data to the end of the %deque.
1233 * @param x Data to be added.
1235 * This is a typical stack operation. The function creates an
1236 * element at the end of the %deque and assigns the given data
1237 * to it. Due to the nature of a %deque this operation can be
1238 * done in constant time.
1241 push_back(const value_type
& __x
)
1243 if (this->_M_impl
._M_finish
._M_cur
1244 != this->_M_impl
._M_finish
._M_last
- 1)
1246 this->_M_impl
.construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1247 ++this->_M_impl
._M_finish
._M_cur
;
1250 _M_push_back_aux(__x
);
1253 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1255 push_back(value_type
&& __x
)
1256 { emplace_back(std::move(__x
)); }
1258 template<typename
... _Args
>
1260 emplace_back(_Args
&&... __args
);
1264 * @brief Removes first element.
1266 * This is a typical stack operation. It shrinks the %deque by one.
1268 * Note that no data is returned, and if the first element's data is
1269 * needed, it should be retrieved before pop_front() is called.
1274 if (this->_M_impl
._M_start
._M_cur
1275 != this->_M_impl
._M_start
._M_last
- 1)
1277 this->_M_impl
.destroy(this->_M_impl
._M_start
._M_cur
);
1278 ++this->_M_impl
._M_start
._M_cur
;
1285 * @brief Removes last element.
1287 * This is a typical stack operation. It shrinks the %deque by one.
1289 * Note that no data is returned, and if the last element's data is
1290 * needed, it should be retrieved before pop_back() is called.
1295 if (this->_M_impl
._M_finish
._M_cur
1296 != this->_M_impl
._M_finish
._M_first
)
1298 --this->_M_impl
._M_finish
._M_cur
;
1299 this->_M_impl
.destroy(this->_M_impl
._M_finish
._M_cur
);
1305 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1307 * @brief Inserts an object in %deque before specified iterator.
1308 * @param position An iterator into the %deque.
1309 * @param args Arguments.
1310 * @return An iterator that points to the inserted data.
1312 * This function will insert an object of type T constructed
1313 * with T(std::forward<Args>(args)...) before the specified location.
1315 template<typename
... _Args
>
1317 emplace(iterator __position
, _Args
&&... __args
);
1321 * @brief Inserts given value into %deque before specified iterator.
1322 * @param position An iterator into the %deque.
1323 * @param x Data to be inserted.
1324 * @return An iterator that points to the inserted data.
1326 * This function will insert a copy of the given value before the
1327 * specified location.
1330 insert(iterator __position
, const value_type
& __x
);
1332 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1334 * @brief Inserts given rvalue into %deque before specified iterator.
1335 * @param position An iterator into the %deque.
1336 * @param x Data to be inserted.
1337 * @return An iterator that points to the inserted data.
1339 * This function will insert a copy of the given rvalue before the
1340 * specified location.
1343 insert(iterator __position
, value_type
&& __x
)
1344 { return emplace(__position
, std::move(__x
)); }
1347 * @brief Inserts an initializer list into the %deque.
1348 * @param p An iterator into the %deque.
1349 * @param l An initializer_list.
1351 * This function will insert copies of the data in the
1352 * initializer_list @a l into the %deque before the location
1353 * specified by @a p. This is known as "list insert."
1356 insert(iterator __p
, initializer_list
<value_type
> __l
)
1357 { this->insert(__p
, __l
.begin(), __l
.end()); }
1361 * @brief Inserts a number of copies of given data into the %deque.
1362 * @param position An iterator into the %deque.
1363 * @param n Number of elements to be inserted.
1364 * @param x Data to be inserted.
1366 * This function will insert a specified number of copies of the given
1367 * data before the location specified by @a position.
1370 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1371 { _M_fill_insert(__position
, __n
, __x
); }
1374 * @brief Inserts a range into the %deque.
1375 * @param position An iterator into the %deque.
1376 * @param first An input iterator.
1377 * @param last An input iterator.
1379 * This function will insert copies of the data in the range
1380 * [first,last) into the %deque before the location specified
1381 * by @a pos. This is known as "range insert."
1383 template<typename _InputIterator
>
1385 insert(iterator __position
, _InputIterator __first
,
1386 _InputIterator __last
)
1388 // Check whether it's an integral type. If so, it's not an iterator.
1389 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1390 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1394 * @brief Remove element at given position.
1395 * @param position Iterator pointing to element to be erased.
1396 * @return An iterator pointing to the next element (or end()).
1398 * This function will erase the element at the given position and thus
1399 * shorten the %deque by one.
1401 * The user is cautioned that
1402 * this function only erases the element, and that if the element is
1403 * itself a pointer, the pointed-to memory is not touched in any way.
1404 * Managing the pointer is the user's responsibility.
1407 erase(iterator __position
);
1410 * @brief Remove a range of elements.
1411 * @param first Iterator pointing to the first element to be erased.
1412 * @param last Iterator pointing to one past the last element to be
1414 * @return An iterator pointing to the element pointed to by @a last
1415 * prior to erasing (or end()).
1417 * This function will erase the elements in the range [first,last) and
1418 * shorten the %deque accordingly.
1420 * The user is cautioned that
1421 * this function only erases the elements, and that if the elements
1422 * themselves are pointers, the pointed-to memory is not touched in any
1423 * way. Managing the pointer is the user's responsibility.
1426 erase(iterator __first
, iterator __last
);
1429 * @brief Swaps data with another %deque.
1430 * @param x A %deque of the same element and allocator types.
1432 * This exchanges the elements between two deques in constant time.
1433 * (Four pointers, so it should be quite fast.)
1434 * Note that the global std::swap() function is specialized such that
1435 * std::swap(d1,d2) will feed to this function.
1440 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1441 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1442 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1443 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1445 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1446 // 431. Swapping containers with unequal allocators.
1447 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1448 __x
._M_get_Tp_allocator());
1452 * Erases all the elements. Note that this function only erases the
1453 * elements, and that if the elements themselves are pointers, the
1454 * pointed-to memory is not touched in any way. Managing the pointer is
1455 * the user's responsibility.
1459 { _M_erase_at_end(begin()); }
1462 // Internal constructor functions follow.
1464 // called by the range constructor to implement [23.1.1]/9
1466 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1467 // 438. Ambiguity in the "do the right thing" clause
1468 template<typename _Integer
>
1470 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1472 _M_initialize_map(static_cast<size_type
>(__n
));
1473 _M_fill_initialize(__x
);
1476 // called by the range constructor to implement [23.1.1]/9
1477 template<typename _InputIterator
>
1479 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1482 typedef typename
std::iterator_traits
<_InputIterator
>::
1483 iterator_category _IterCategory
;
1484 _M_range_initialize(__first
, __last
, _IterCategory());
1487 // called by the second initialize_dispatch above
1490 * @brief Fills the deque with whatever is in [first,last).
1491 * @param first An input iterator.
1492 * @param last An input iterator.
1495 * If the iterators are actually forward iterators (or better), then the
1496 * memory layout can be done all at once. Else we move forward using
1497 * push_back on each value from the iterator.
1499 template<typename _InputIterator
>
1501 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1502 std::input_iterator_tag
);
1504 // called by the second initialize_dispatch above
1505 template<typename _ForwardIterator
>
1507 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1508 std::forward_iterator_tag
);
1512 * @brief Fills the %deque with copies of value.
1513 * @param value Initial value.
1515 * @pre _M_start and _M_finish have already been initialized,
1516 * but none of the %deque's elements have yet been constructed.
1518 * This function is called only when the user provides an explicit size
1519 * (with or without an explicit exemplar value).
1522 _M_fill_initialize(const value_type
& __value
);
1524 // Internal assign functions follow. The *_aux functions do the actual
1525 // assignment work for the range versions.
1527 // called by the range assign to implement [23.1.1]/9
1529 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1530 // 438. Ambiguity in the "do the right thing" clause
1531 template<typename _Integer
>
1533 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1534 { _M_fill_assign(__n
, __val
); }
1536 // called by the range assign to implement [23.1.1]/9
1537 template<typename _InputIterator
>
1539 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1542 typedef typename
std::iterator_traits
<_InputIterator
>::
1543 iterator_category _IterCategory
;
1544 _M_assign_aux(__first
, __last
, _IterCategory());
1547 // called by the second assign_dispatch above
1548 template<typename _InputIterator
>
1550 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1551 std::input_iterator_tag
);
1553 // called by the second assign_dispatch above
1554 template<typename _ForwardIterator
>
1556 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1557 std::forward_iterator_tag
)
1559 const size_type __len
= std::distance(__first
, __last
);
1562 _ForwardIterator __mid
= __first
;
1563 std::advance(__mid
, size());
1564 std::copy(__first
, __mid
, begin());
1565 insert(end(), __mid
, __last
);
1568 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1571 // Called by assign(n,t), and the range assign when it turns out
1572 // to be the same thing.
1574 _M_fill_assign(size_type __n
, const value_type
& __val
)
1578 std::fill(begin(), end(), __val
);
1579 insert(end(), __n
- size(), __val
);
1583 _M_erase_at_end(begin() + difference_type(__n
));
1584 std::fill(begin(), end(), __val
);
1589 /// Helper functions for push_* and pop_*.
1590 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1591 void _M_push_back_aux(const value_type
&);
1593 void _M_push_front_aux(const value_type
&);
1595 template<typename
... _Args
>
1596 void _M_push_back_aux(_Args
&&... __args
);
1598 template<typename
... _Args
>
1599 void _M_push_front_aux(_Args
&&... __args
);
1602 void _M_pop_back_aux();
1604 void _M_pop_front_aux();
1607 // Internal insert functions follow. The *_aux functions do the actual
1608 // insertion work when all shortcuts fail.
1610 // called by the range insert to implement [23.1.1]/9
1612 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1613 // 438. Ambiguity in the "do the right thing" clause
1614 template<typename _Integer
>
1616 _M_insert_dispatch(iterator __pos
,
1617 _Integer __n
, _Integer __x
, __true_type
)
1618 { _M_fill_insert(__pos
, __n
, __x
); }
1620 // called by the range insert to implement [23.1.1]/9
1621 template<typename _InputIterator
>
1623 _M_insert_dispatch(iterator __pos
,
1624 _InputIterator __first
, _InputIterator __last
,
1627 typedef typename
std::iterator_traits
<_InputIterator
>::
1628 iterator_category _IterCategory
;
1629 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1632 // called by the second insert_dispatch above
1633 template<typename _InputIterator
>
1635 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1636 _InputIterator __last
, std::input_iterator_tag
);
1638 // called by the second insert_dispatch above
1639 template<typename _ForwardIterator
>
1641 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1642 _ForwardIterator __last
, std::forward_iterator_tag
);
1644 // Called by insert(p,n,x), and the range insert when it turns out to be
1645 // the same thing. Can use fill functions in optimal situations,
1646 // otherwise passes off to insert_aux(p,n,x).
1648 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1650 // called by insert(p,x)
1651 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1653 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1655 template<typename
... _Args
>
1657 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
1660 // called by insert(p,n,x) via fill_insert
1662 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1664 // called by range_insert_aux for forward iterators
1665 template<typename _ForwardIterator
>
1667 _M_insert_aux(iterator __pos
,
1668 _ForwardIterator __first
, _ForwardIterator __last
,
1672 // Internal erase functions follow.
1675 _M_destroy_data_aux(iterator __first
, iterator __last
);
1677 // Called by ~deque().
1678 // NB: Doesn't deallocate the nodes.
1679 template<typename _Alloc1
>
1681 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
1682 { _M_destroy_data_aux(__first
, __last
); }
1685 _M_destroy_data(iterator __first
, iterator __last
,
1686 const std::allocator
<_Tp
>&)
1688 if (!__has_trivial_destructor(value_type
))
1689 _M_destroy_data_aux(__first
, __last
);
1692 // Called by erase(q1, q2).
1694 _M_erase_at_begin(iterator __pos
)
1696 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
1697 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
1698 this->_M_impl
._M_start
= __pos
;
1701 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1702 // _M_fill_assign, operator=.
1704 _M_erase_at_end(iterator __pos
)
1706 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
1707 _M_destroy_nodes(__pos
._M_node
+ 1,
1708 this->_M_impl
._M_finish
._M_node
+ 1);
1709 this->_M_impl
._M_finish
= __pos
;
1713 /// Memory-handling helpers for the previous internal insert functions.
1715 _M_reserve_elements_at_front(size_type __n
)
1717 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1718 - this->_M_impl
._M_start
._M_first
;
1719 if (__n
> __vacancies
)
1720 _M_new_elements_at_front(__n
- __vacancies
);
1721 return this->_M_impl
._M_start
- difference_type(__n
);
1725 _M_reserve_elements_at_back(size_type __n
)
1727 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1728 - this->_M_impl
._M_finish
._M_cur
) - 1;
1729 if (__n
> __vacancies
)
1730 _M_new_elements_at_back(__n
- __vacancies
);
1731 return this->_M_impl
._M_finish
+ difference_type(__n
);
1735 _M_new_elements_at_front(size_type __new_elements
);
1738 _M_new_elements_at_back(size_type __new_elements
);
1744 * @brief Memory-handling helpers for the major %map.
1746 * Makes sure the _M_map has space for new nodes. Does not
1747 * actually add the nodes. Can invalidate _M_map pointers.
1748 * (And consequently, %deque iterators.)
1751 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
1753 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1754 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
1755 _M_reallocate_map(__nodes_to_add
, false);
1759 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
1761 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
1762 - this->_M_impl
._M_map
))
1763 _M_reallocate_map(__nodes_to_add
, true);
1767 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
1773 * @brief Deque equality comparison.
1774 * @param x A %deque.
1775 * @param y A %deque of the same type as @a x.
1776 * @return True iff the size and elements of the deques are equal.
1778 * This is an equivalence relation. It is linear in the size of the
1779 * deques. Deques are considered equivalent if their sizes are equal,
1780 * and if corresponding elements compare equal.
1782 template<typename _Tp
, typename _Alloc
>
1784 operator==(const deque
<_Tp
, _Alloc
>& __x
,
1785 const deque
<_Tp
, _Alloc
>& __y
)
1786 { return __x
.size() == __y
.size()
1787 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
1790 * @brief Deque ordering relation.
1791 * @param x A %deque.
1792 * @param y A %deque of the same type as @a x.
1793 * @return True iff @a x is lexicographically less than @a y.
1795 * This is a total ordering relation. It is linear in the size of the
1796 * deques. The elements must be comparable with @c <.
1798 * See std::lexicographical_compare() for how the determination is made.
1800 template<typename _Tp
, typename _Alloc
>
1802 operator<(const deque
<_Tp
, _Alloc
>& __x
,
1803 const deque
<_Tp
, _Alloc
>& __y
)
1804 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1805 __y
.begin(), __y
.end()); }
1807 /// Based on operator==
1808 template<typename _Tp
, typename _Alloc
>
1810 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
1811 const deque
<_Tp
, _Alloc
>& __y
)
1812 { return !(__x
== __y
); }
1814 /// Based on operator<
1815 template<typename _Tp
, typename _Alloc
>
1817 operator>(const deque
<_Tp
, _Alloc
>& __x
,
1818 const deque
<_Tp
, _Alloc
>& __y
)
1819 { return __y
< __x
; }
1821 /// Based on operator<
1822 template<typename _Tp
, typename _Alloc
>
1824 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
1825 const deque
<_Tp
, _Alloc
>& __y
)
1826 { return !(__y
< __x
); }
1828 /// Based on operator<
1829 template<typename _Tp
, typename _Alloc
>
1831 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
1832 const deque
<_Tp
, _Alloc
>& __y
)
1833 { return !(__x
< __y
); }
1835 /// See std::deque::swap().
1836 template<typename _Tp
, typename _Alloc
>
1838 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
1841 #undef _GLIBCXX_DEQUE_BUF_SIZE
1843 _GLIBCXX_END_NESTED_NAMESPACE
1845 #endif /* _STL_DEQUE_H */