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.
78 __deque_buf_size(size_t __size)
79 { return __size < 512 ? size_t(512 / __size) : size_t(1); }
83 * @brief A deque::iterator.
85 * Quite a bit of intelligence here. Much of the functionality of
86 * deque is actually passed off to this class. A deque holds two
87 * of these internally, marking its valid range. Access to
88 * elements is done as offsets of either of those two, relying on
89 * operator overloading in this class.
91 * All the functions are op overloads except for _M_set_node.
93 template<typename _Tp, typename _Ref, typename _Ptr>
94 struct _Deque_iterator
96 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
97 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
99 static size_t _S_buffer_size()
100 { return __deque_buf_size(sizeof(_Tp)); }
102 typedef std::random_access_iterator_tag iterator_category;
103 typedef _Tp value_type;
104 typedef _Ptr pointer;
105 typedef _Ref reference;
106 typedef size_t size_type;
107 typedef ptrdiff_t difference_type;
108 typedef _Tp** _Map_pointer;
109 typedef _Deque_iterator _Self;
114 _Map_pointer _M_node;
116 _Deque_iterator(_Tp* __x, _Map_pointer __y)
117 : _M_cur(__x), _M_first(*__y),
118 _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
121 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
123 _Deque_iterator(const iterator& __x)
124 : _M_cur(__x._M_cur), _M_first(__x._M_first),
125 _M_last(__x._M_last), _M_node(__x._M_node) { }
139 if (_M_cur == _M_last)
141 _M_set_node(_M_node + 1);
158 if (_M_cur == _M_first)
160 _M_set_node(_M_node - 1);
176 operator+=(difference_type __n)
178 const difference_type __offset = __n + (_M_cur - _M_first);
179 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
183 const difference_type __node_offset =
184 __offset > 0 ? __offset / difference_type(_S_buffer_size())
185 : -difference_type((-__offset - 1)
186 / _S_buffer_size()) - 1;
187 _M_set_node(_M_node + __node_offset);
188 _M_cur = _M_first + (__offset - __node_offset
189 * difference_type(_S_buffer_size()));
195 operator+(difference_type __n) const
202 operator-=(difference_type __n)
203 { return *this += -__n; }
206 operator-(difference_type __n) const
213 operator[](difference_type __n) const
214 { return *(*this + __n); }
217 * Prepares to traverse new_node. Sets everything except
218 * _M_cur, which should therefore be set by the caller
219 * immediately afterwards, based on _M_first and _M_last.
222 _M_set_node(_Map_pointer __new_node)
224 _M_node = __new_node;
225 _M_first = *__new_node;
226 _M_last = _M_first + difference_type(_S_buffer_size());
230 // Note: we also provide overloads whose operands are of the same type in
231 // order to avoid ambiguous overload resolution when std::rel_ops operators
232 // are in scope (for additional details, see libstdc++/3628)
233 template<typename _Tp, typename _Ref, typename _Ptr>
235 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
236 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
237 { return __x._M_cur == __y._M_cur; }
239 template<typename _Tp, typename _RefL, typename _PtrL,
240 typename _RefR, typename _PtrR>
242 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
243 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
244 { return __x._M_cur == __y._M_cur; }
246 template<typename _Tp, typename _Ref, typename _Ptr>
248 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
249 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
250 { return !(__x == __y); }
252 template<typename _Tp, typename _RefL, typename _PtrL,
253 typename _RefR, typename _PtrR>
255 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
256 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
257 { return !(__x == __y); }
259 template<typename _Tp, typename _Ref, typename _Ptr>
261 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
262 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
263 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
264 : (__x._M_node < __y._M_node); }
266 template<typename _Tp, typename _RefL, typename _PtrL,
267 typename _RefR, typename _PtrR>
269 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
270 const _Deque_iterator<_Tp, _RefR, _PtrR>& __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 _Ref, typename _Ptr>
276 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
277 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
278 { return __y < __x; }
280 template<typename _Tp, typename _RefL, typename _PtrL,
281 typename _RefR, typename _PtrR>
283 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
284 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
285 { return __y < __x; }
287 template<typename _Tp, typename _Ref, typename _Ptr>
289 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
290 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
291 { return !(__y < __x); }
293 template<typename _Tp, typename _RefL, typename _PtrL,
294 typename _RefR, typename _PtrR>
296 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
297 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
298 { return !(__y < __x); }
300 template<typename _Tp, typename _Ref, typename _Ptr>
302 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
303 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
304 { return !(__x < __y); }
306 template<typename _Tp, typename _RefL, typename _PtrL,
307 typename _RefR, typename _PtrR>
309 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
310 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
311 { return !(__x < __y); }
313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
314 // According to the resolution of DR179 not only the various comparison
315 // operators but also operator- must accept mixed iterator/const_iterator
317 template<typename _Tp, typename _Ref, typename _Ptr>
318 inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
319 operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
320 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
322 return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
323 (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
324 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
325 + (__y._M_last - __y._M_cur);
328 template<typename _Tp, typename _RefL, typename _PtrL,
329 typename _RefR, typename _PtrR>
330 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
331 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
332 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
334 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
335 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
336 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
337 + (__y._M_last - __y._M_cur);
340 template<typename _Tp, typename _Ref, typename _Ptr>
341 inline _Deque_iterator<_Tp, _Ref, _Ptr>
342 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
343 { return __x + __n; }
345 template<typename _Tp>
347 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
348 const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value);
351 * Deque base class. This class provides the unified face for %deque's
352 * allocation. This class's constructor and destructor allocate and
353 * deallocate (but do not initialize) storage. This makes %exception
356 * Nothing in this class ever constructs or destroys an actual Tp element.
357 * (Deque handles that itself.) Only/All memory management is performed
360 template<typename _Tp, typename _Alloc>
364 typedef _Alloc allocator_type;
367 get_allocator() const
368 { return allocator_type(_M_get_Tp_allocator()); }
370 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
371 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
375 { _M_initialize_map(0); }
377 _Deque_base(const allocator_type& __a, size_t __num_elements)
379 { _M_initialize_map(__num_elements); }
381 _Deque_base(const allocator_type& __a)
385 #ifdef __GXX_EXPERIMENTAL_CXX0X__
386 _Deque_base(_Deque_base&& __x)
387 : _M_impl(__x._M_get_Tp_allocator())
389 _M_initialize_map(0);
390 if (__x._M_impl._M_map)
392 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
393 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
394 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
395 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
403 //This struct encapsulates the implementation of the std::deque
404 //standard container and at the same time makes use of the EBO
405 //for empty allocators.
406 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
408 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
411 : public _Tp_alloc_type
419 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
420 _M_start(), _M_finish()
423 _Deque_impl(const _Tp_alloc_type& __a)
424 : _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
425 _M_start(), _M_finish()
430 _M_get_Tp_allocator()
431 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
433 const _Tp_alloc_type&
434 _M_get_Tp_allocator() const
435 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
438 _M_get_map_allocator() const
439 { return _Map_alloc_type(_M_get_Tp_allocator()); }
444 return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
448 _M_deallocate_node(_Tp* __p)
450 _M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
454 _M_allocate_map(size_t __n)
455 { return _M_get_map_allocator().allocate(__n); }
458 _M_deallocate_map(_Tp** __p, size_t __n)
459 { _M_get_map_allocator().deallocate(__p, __n); }
462 void _M_initialize_map(size_t);
463 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
464 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
465 enum { _S_initial_map_size = 8 };
470 template<typename _Tp, typename _Alloc>
471 _Deque_base<_Tp, _Alloc>::
474 if (this->_M_impl._M_map)
476 _M_destroy_nodes(this->_M_impl._M_start._M_node,
477 this->_M_impl._M_finish._M_node + 1);
478 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
483 * @brief Layout storage.
484 * @param num_elements The count of T's for which to allocate space
488 * The initial underlying memory layout is a bit complicated...
490 template<typename _Tp, typename _Alloc>
492 _Deque_base<_Tp, _Alloc>::
493 _M_initialize_map(size_t __num_elements)
495 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
498 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
499 size_t(__num_nodes + 2));
500 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
502 // For "small" maps (needing less than _M_map_size nodes), allocation
503 // starts in the middle elements and grows outwards. So nstart may be
504 // the beginning of _M_map, but for small maps it may be as far in as
507 _Tp** __nstart = (this->_M_impl._M_map
508 + (this->_M_impl._M_map_size - __num_nodes) / 2);
509 _Tp** __nfinish = __nstart + __num_nodes;
512 { _M_create_nodes(__nstart, __nfinish); }
515 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
516 this->_M_impl._M_map = 0;
517 this->_M_impl._M_map_size = 0;
518 __throw_exception_again;
521 this->_M_impl._M_start._M_set_node(__nstart);
522 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
523 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
524 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
526 % __deque_buf_size(sizeof(_Tp)));
529 template<typename _Tp, typename _Alloc>
531 _Deque_base<_Tp, _Alloc>::
532 _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
537 for (__cur = __nstart; __cur < __nfinish; ++__cur)
538 *__cur = this->_M_allocate_node();
542 _M_destroy_nodes(__nstart, __cur);
543 __throw_exception_again;
547 template<typename _Tp, typename _Alloc>
549 _Deque_base<_Tp, _Alloc>::
550 _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
552 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
553 _M_deallocate_node(*__n);
557 * @brief A standard container using fixed-size memory allocation and
558 * constant-time manipulation of elements at either end.
562 * Meets the requirements of a <a href="tables.html#65">container</a>, a
563 * <a href="tables.html#66">reversible container</a>, and a
564 * <a href="tables.html#67">sequence</a>, including the
565 * <a href="tables.html#68">optional sequence requirements</a>.
567 * In previous HP/SGI versions of deque, there was an extra template
568 * parameter so users could control the node size. This extension turned
569 * out to violate the C++ standard (it can be detected using template
570 * template parameters), and it was removed.
572 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
575 * - size_t _M_map_size
576 * - iterator _M_start, _M_finish
578 * map_size is at least 8. %map is an array of map_size
579 * pointers-to-"nodes". (The name %map has nothing to do with the
580 * std::map class, and "nodes" should not be confused with
581 * std::list's usage of "node".)
583 * A "node" has no specific type name as such, but it is referred
584 * to as "node" in this file. It is a simple array-of-Tp. If Tp
585 * is very large, there will be one Tp element per node (i.e., an
586 * "array" of one). For non-huge Tp's, node size is inversely
587 * related to Tp size: the larger the Tp, the fewer Tp's will fit
588 * in a node. The goal here is to keep the total size of a node
589 * relatively small and constant over different Tp's, to improve
590 * allocator efficiency.
592 * Not every pointer in the %map array will point to a node. If
593 * the initial number of elements in the deque is small, the
594 * /middle/ %map pointers will be valid, and the ones at the edges
595 * will be unused. This same situation will arise as the %map
596 * grows: available %map pointers, if any, will be on the ends. As
597 * new nodes are created, only a subset of the %map's pointers need
598 * to be copied "outward".
601 * - For any nonsingular iterator i:
602 * - i.node points to a member of the %map array. (Yes, you read that
603 * correctly: i.node does not actually point to a node.) The member of
604 * the %map array is what actually points to the node.
605 * - i.first == *(i.node) (This points to the node (first Tp element).)
606 * - i.last == i.first + node_size
607 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
608 * the implication of this is that i.cur is always a dereferenceable
609 * pointer, even if i is a past-the-end iterator.
610 * - Start and Finish are always nonsingular iterators. NOTE: this
611 * means that an empty deque must have one node, a deque with <N
612 * elements (where N is the node buffer size) must have one node, a
613 * deque with N through (2N-1) elements must have two nodes, etc.
614 * - For every node other than start.node and finish.node, every
615 * element in the node is an initialized object. If start.node ==
616 * finish.node, then [start.cur, finish.cur) are initialized
617 * objects, and the elements outside that range are uninitialized
618 * storage. Otherwise, [start.cur, start.last) and [finish.first,
619 * finish.cur) are initialized objects, and [start.first, start.cur)
620 * and [finish.cur, finish.last) are uninitialized storage.
621 * - [%map, %map + map_size) is a valid, non-empty range.
622 * - [start.node, finish.node] is a valid range contained within
623 * [%map, %map + map_size).
624 * - A pointer in the range [%map, %map + map_size) points to an allocated
625 * node if and only if the pointer is in the range
626 * [start.node, finish.node].
628 * Here's the magic: nothing in deque is "aware" of the discontiguous
631 * The memory setup and layout occurs in the parent, _Base, and the iterator
632 * class is entirely responsible for "leaping" from one node to the next.
633 * All the implementation routines for deque itself work only through the
634 * start and finish iterators. This keeps the routines simple and sane,
635 * and we can use other standard algorithms as well.
637 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
638 class deque : protected _Deque_base<_Tp, _Alloc>
640 // concept requirements
641 typedef typename _Alloc::value_type _Alloc_value_type;
642 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
643 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
645 typedef _Deque_base<_Tp, _Alloc> _Base;
646 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
649 typedef _Tp value_type;
650 typedef typename _Tp_alloc_type::pointer pointer;
651 typedef typename _Tp_alloc_type::const_pointer const_pointer;
652 typedef typename _Tp_alloc_type::reference reference;
653 typedef typename _Tp_alloc_type::const_reference const_reference;
654 typedef typename _Base::iterator iterator;
655 typedef typename _Base::const_iterator const_iterator;
656 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
657 typedef std::reverse_iterator<iterator> reverse_iterator;
658 typedef size_t size_type;
659 typedef ptrdiff_t difference_type;
660 typedef _Alloc allocator_type;
663 typedef pointer* _Map_pointer;
665 static size_t _S_buffer_size()
666 { return __deque_buf_size(sizeof(_Tp)); }
668 // Functions controlling memory layout, and nothing else.
669 using _Base::_M_initialize_map;
670 using _Base::_M_create_nodes;
671 using _Base::_M_destroy_nodes;
672 using _Base::_M_allocate_node;
673 using _Base::_M_deallocate_node;
674 using _Base::_M_allocate_map;
675 using _Base::_M_deallocate_map;
676 using _Base::_M_get_Tp_allocator;
679 * A total of four data members accumulated down the hierarchy.
680 * May be accessed via _M_impl.*
682 using _Base::_M_impl;
685 // [23.2.1.1] construct/copy/destroy
686 // (assign() and get_allocator() are also listed in this section)
688 * @brief Default constructor creates no elements.
694 * @brief Creates a %deque with no elements.
695 * @param a An allocator object.
698 deque(const allocator_type& __a)
702 * @brief Creates a %deque with copies of an exemplar element.
703 * @param n The number of elements to initially create.
704 * @param value An element to copy.
705 * @param a An allocator.
707 * This constructor fills the %deque with @a n copies of @a value.
710 deque(size_type __n, const value_type& __value = value_type(),
711 const allocator_type& __a = allocator_type())
713 { _M_fill_initialize(__value); }
716 * @brief %Deque copy constructor.
717 * @param x A %deque of identical element and allocator types.
719 * The newly-created %deque uses a copy of the allocation object used
722 deque(const deque& __x)
723 : _Base(__x._M_get_Tp_allocator(), __x.size())
724 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
725 this->_M_impl._M_start,
726 _M_get_Tp_allocator()); }
728 #ifdef __GXX_EXPERIMENTAL_CXX0X__
730 * @brief %Deque move constructor.
731 * @param x A %deque of identical element and allocator types.
733 * The newly-created %deque contains the exact contents of @a x.
734 * The contents of @a x are a valid, but unspecified %deque.
737 : _Base(std::forward<_Base>(__x)) { }
740 * @brief Builds a %deque from an initializer list.
741 * @param l An initializer_list.
742 * @param a An allocator object.
744 * Create a %deque consisting of copies of the elements in the
745 * initializer_list @a l.
747 * This will call the element type's copy constructor N times
748 * (where N is l.size()) and do no memory reallocation.
750 deque(initializer_list<value_type> __l,
751 const allocator_type& __a = allocator_type())
754 _M_range_initialize(__l.begin(), __l.end(),
755 random_access_iterator_tag());
760 * @brief Builds a %deque from a range.
761 * @param first An input iterator.
762 * @param last An input iterator.
763 * @param a An allocator object.
765 * Create a %deque consisting of copies of the elements from [first,
768 * If the iterators are forward, bidirectional, or random-access, then
769 * this will call the elements' copy constructor N times (where N is
770 * distance(first,last)) and do no memory reallocation. But if only
771 * input iterators are used, then this will do at most 2N calls to the
772 * copy constructor, and logN memory reallocations.
774 template<typename _InputIterator>
775 deque(_InputIterator __first, _InputIterator __last,
776 const allocator_type& __a = allocator_type())
779 // Check whether it's an integral type. If so, it's not an iterator.
780 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
781 _M_initialize_dispatch(__first, __last, _Integral());
785 * The dtor only erases the elements, and note that if the elements
786 * themselves are pointers, the pointed-to memory is not touched in any
787 * way. Managing the pointer is the user's responsibility.
790 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
793 * @brief %Deque assignment operator.
794 * @param x A %deque of identical element and allocator types.
796 * All the elements of @a x are copied, but unlike the copy constructor,
797 * the allocator object is not copied.
800 operator=(const deque& __x);
802 #ifdef __GXX_EXPERIMENTAL_CXX0X__
804 * @brief %Deque move assignment operator.
805 * @param x A %deque of identical element and allocator types.
807 * The contents of @a x are moved into this deque (without copying).
808 * @a x is a valid, but unspecified %deque.
811 operator=(deque&& __x)
820 * @brief Assigns an initializer list to a %deque.
821 * @param l An initializer_list.
823 * This function fills a %deque with copies of the elements in the
824 * initializer_list @a l.
826 * Note that the assignment completely changes the %deque and that the
827 * resulting %deque's size is the same as the number of elements
828 * assigned. Old data may be lost.
831 operator=(initializer_list<value_type> __l)
833 this->assign(__l.begin(), __l.end());
839 * @brief Assigns a given value to a %deque.
840 * @param n Number of elements to be assigned.
841 * @param val Value to be assigned.
843 * This function fills a %deque with @a n copies of the given
844 * value. Note that the assignment completely changes the
845 * %deque and that the resulting %deque's size is the same as
846 * the number of elements assigned. Old data may be lost.
849 assign(size_type __n, const value_type& __val)
850 { _M_fill_assign(__n, __val); }
853 * @brief Assigns a range to a %deque.
854 * @param first An input iterator.
855 * @param last An input iterator.
857 * This function fills a %deque with copies of the elements in the
858 * range [first,last).
860 * Note that the assignment completely changes the %deque and that the
861 * resulting %deque's size is the same as the number of elements
862 * assigned. Old data may be lost.
864 template<typename _InputIterator>
866 assign(_InputIterator __first, _InputIterator __last)
868 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
869 _M_assign_dispatch(__first, __last, _Integral());
872 #ifdef __GXX_EXPERIMENTAL_CXX0X__
874 * @brief Assigns an initializer list to a %deque.
875 * @param l An initializer_list.
877 * This function fills a %deque with copies of the elements in the
878 * initializer_list @a l.
880 * Note that the assignment completely changes the %deque and that the
881 * resulting %deque's size is the same as the number of elements
882 * assigned. Old data may be lost.
885 assign(initializer_list<value_type> __l)
886 { this->assign(__l.begin(), __l.end()); }
889 /// Get a copy of the memory allocation object.
891 get_allocator() const
892 { return _Base::get_allocator(); }
896 * Returns a read/write iterator that points to the first element in the
897 * %deque. Iteration is done in ordinary element order.
901 { return this->_M_impl._M_start; }
904 * Returns a read-only (constant) iterator that points to the first
905 * element in the %deque. Iteration is done in ordinary element order.
909 { return this->_M_impl._M_start; }
912 * Returns a read/write iterator that points one past the last
913 * element in the %deque. Iteration is done in ordinary
918 { return this->_M_impl._M_finish; }
921 * Returns a read-only (constant) iterator that points one past
922 * the last element in the %deque. Iteration is done in
923 * ordinary element order.
927 { return this->_M_impl._M_finish; }
930 * Returns a read/write reverse iterator that points to the
931 * last element in the %deque. Iteration is done in reverse
936 { return reverse_iterator(this->_M_impl._M_finish); }
939 * Returns a read-only (constant) reverse iterator that points
940 * to the last element in the %deque. Iteration is done in
941 * reverse element order.
943 const_reverse_iterator
945 { return const_reverse_iterator(this->_M_impl._M_finish); }
948 * Returns a read/write reverse iterator that points to one
949 * before the first element in the %deque. Iteration is done
950 * in reverse element order.
954 { return reverse_iterator(this->_M_impl._M_start); }
957 * Returns a read-only (constant) reverse iterator that points
958 * to one before the first element in the %deque. Iteration is
959 * done in reverse element order.
961 const_reverse_iterator
963 { return const_reverse_iterator(this->_M_impl._M_start); }
965 #ifdef __GXX_EXPERIMENTAL_CXX0X__
967 * Returns a read-only (constant) iterator that points to the first
968 * element in the %deque. Iteration is done in ordinary element order.
972 { return this->_M_impl._M_start; }
975 * Returns a read-only (constant) iterator that points one past
976 * the last element in the %deque. Iteration is done in
977 * ordinary element order.
981 { return this->_M_impl._M_finish; }
984 * Returns a read-only (constant) reverse iterator that points
985 * to the last element in the %deque. Iteration is done in
986 * reverse element order.
988 const_reverse_iterator
990 { return const_reverse_iterator(this->_M_impl._M_finish); }
993 * Returns a read-only (constant) reverse iterator that points
994 * to one before the first element in the %deque. Iteration is
995 * done in reverse element order.
997 const_reverse_iterator
999 { return const_reverse_iterator(this->_M_impl._M_start); }
1002 // [23.2.1.2] capacity
1003 /** Returns the number of elements in the %deque. */
1006 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1008 /** Returns the size() of the largest possible %deque. */
1011 { return _M_get_Tp_allocator().max_size(); }
1014 * @brief Resizes the %deque to the specified number of elements.
1015 * @param new_size Number of elements the %deque should contain.
1016 * @param x Data with which new elements should be populated.
1018 * This function will %resize the %deque to the specified
1019 * number of elements. If the number is smaller than the
1020 * %deque's current size the %deque is truncated, otherwise the
1021 * %deque is extended and new elements are populated with given
1025 resize(size_type __new_size, value_type __x = value_type())
1027 const size_type __len = size();
1028 if (__new_size < __len)
1029 _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size));
1031 insert(this->_M_impl._M_finish, __new_size - __len, __x);
1035 * Returns true if the %deque is empty. (Thus begin() would
1040 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1044 * @brief Subscript access to the data contained in the %deque.
1045 * @param n The index of the element for which data should be
1047 * @return Read/write reference to data.
1049 * This operator allows for easy, array-style, data access.
1050 * Note that data access with this operator is unchecked and
1051 * out_of_range lookups are not defined. (For checked lookups
1055 operator[](size_type __n)
1056 { return this->_M_impl._M_start[difference_type(__n)]; }
1059 * @brief Subscript access to the data contained in the %deque.
1060 * @param n The index of the element for which data should be
1062 * @return Read-only (constant) reference to data.
1064 * This operator allows for easy, array-style, data access.
1065 * Note that data access with this operator is unchecked and
1066 * out_of_range lookups are not defined. (For checked lookups
1070 operator[](size_type __n) const
1071 { return this->_M_impl._M_start[difference_type(__n)]; }
1074 /// Safety check used only from at().
1076 _M_range_check(size_type __n) const
1078 if (__n >= this->size())
1079 __throw_out_of_range(__N("deque::_M_range_check"));
1084 * @brief Provides 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.
1088 * @throw std::out_of_range If @a n is an invalid index.
1090 * This function provides for safer data access. The parameter
1091 * is first checked that it is in the range of the deque. The
1092 * function throws out_of_range if the check fails.
1097 _M_range_check(__n);
1098 return (*this)[__n];
1102 * @brief Provides access to the data contained in the %deque.
1103 * @param n The index of the element for which data should be
1105 * @return Read-only (constant) reference to data.
1106 * @throw std::out_of_range If @a n is an invalid index.
1108 * This function provides for safer data access. The parameter is first
1109 * checked that it is in the range of the deque. The function throws
1110 * out_of_range if the check fails.
1113 at(size_type __n) const
1115 _M_range_check(__n);
1116 return (*this)[__n];
1120 * Returns a read/write reference to the data at the first
1121 * element of the %deque.
1125 { return *begin(); }
1128 * Returns a read-only (constant) reference to the data at the first
1129 * element of the %deque.
1133 { return *begin(); }
1136 * Returns a read/write reference to the data at the last element of the
1142 iterator __tmp = end();
1148 * Returns a read-only (constant) reference to the data at the last
1149 * element of the %deque.
1154 const_iterator __tmp = end();
1159 // [23.2.1.2] modifiers
1161 * @brief Add data to the front of the %deque.
1162 * @param x Data to be added.
1164 * This is a typical stack operation. The function creates an
1165 * element at the front of the %deque and assigns the given
1166 * data to it. Due to the nature of a %deque this operation
1167 * can be done in constant time.
1170 push_front(const value_type& __x)
1172 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1174 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1175 --this->_M_impl._M_start._M_cur;
1178 _M_push_front_aux(__x);
1181 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1183 push_front(value_type&& __x)
1184 { emplace_front(std::move(__x)); }
1186 template<typename... _Args>
1188 emplace_front(_Args&&... __args);
1192 * @brief Add data to the end of the %deque.
1193 * @param x Data to be added.
1195 * This is a typical stack operation. The function creates an
1196 * element at the end of the %deque and assigns the given data
1197 * to it. Due to the nature of a %deque this operation can be
1198 * done in constant time.
1201 push_back(const value_type& __x)
1203 if (this->_M_impl._M_finish._M_cur
1204 != this->_M_impl._M_finish._M_last - 1)
1206 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1207 ++this->_M_impl._M_finish._M_cur;
1210 _M_push_back_aux(__x);
1213 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1215 push_back(value_type&& __x)
1216 { emplace_back(std::move(__x)); }
1218 template<typename... _Args>
1220 emplace_back(_Args&&... __args);
1224 * @brief Removes first element.
1226 * This is a typical stack operation. It shrinks the %deque by one.
1228 * Note that no data is returned, and if the first element's data is
1229 * needed, it should be retrieved before pop_front() is called.
1234 if (this->_M_impl._M_start._M_cur
1235 != this->_M_impl._M_start._M_last - 1)
1237 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1238 ++this->_M_impl._M_start._M_cur;
1245 * @brief Removes last element.
1247 * This is a typical stack operation. It shrinks the %deque by one.
1249 * Note that no data is returned, and if the last element's data is
1250 * needed, it should be retrieved before pop_back() is called.
1255 if (this->_M_impl._M_finish._M_cur
1256 != this->_M_impl._M_finish._M_first)
1258 --this->_M_impl._M_finish._M_cur;
1259 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1265 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1267 * @brief Inserts an object in %deque before specified iterator.
1268 * @param position An iterator into the %deque.
1269 * @param args Arguments.
1270 * @return An iterator that points to the inserted data.
1272 * This function will insert an object of type T constructed
1273 * with T(std::forward<Args>(args)...) before the specified location.
1275 template<typename... _Args>
1277 emplace(iterator __position, _Args&&... __args);
1281 * @brief Inserts given value into %deque before specified iterator.
1282 * @param position An iterator into the %deque.
1283 * @param x Data to be inserted.
1284 * @return An iterator that points to the inserted data.
1286 * This function will insert a copy of the given value before the
1287 * specified location.
1290 insert(iterator __position, const value_type& __x);
1292 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1294 * @brief Inserts given rvalue into %deque before specified iterator.
1295 * @param position An iterator into the %deque.
1296 * @param x Data to be inserted.
1297 * @return An iterator that points to the inserted data.
1299 * This function will insert a copy of the given rvalue before the
1300 * specified location.
1303 insert(iterator __position, value_type&& __x)
1304 { return emplace(__position, std::move(__x)); }
1307 * @brief Inserts an initializer list into the %deque.
1308 * @param p An iterator into the %deque.
1309 * @param l An initializer_list.
1311 * This function will insert copies of the data in the
1312 * initializer_list @a l into the %deque before the location
1313 * specified by @a p. This is known as "list insert."
1316 insert(iterator __p, initializer_list<value_type> __l)
1317 { this->insert(__p, __l.begin(), __l.end()); }
1321 * @brief Inserts a number of copies of given data into the %deque.
1322 * @param position An iterator into the %deque.
1323 * @param n Number of elements to be inserted.
1324 * @param x Data to be inserted.
1326 * This function will insert a specified number of copies of the given
1327 * data before the location specified by @a position.
1330 insert(iterator __position, size_type __n, const value_type& __x)
1331 { _M_fill_insert(__position, __n, __x); }
1334 * @brief Inserts a range into the %deque.
1335 * @param position An iterator into the %deque.
1336 * @param first An input iterator.
1337 * @param last An input iterator.
1339 * This function will insert copies of the data in the range
1340 * [first,last) into the %deque before the location specified
1341 * by @a pos. This is known as "range insert."
1343 template<typename _InputIterator>
1345 insert(iterator __position, _InputIterator __first,
1346 _InputIterator __last)
1348 // Check whether it's an integral type. If so, it's not an iterator.
1349 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1350 _M_insert_dispatch(__position, __first, __last, _Integral());
1354 * @brief Remove element at given position.
1355 * @param position Iterator pointing to element to be erased.
1356 * @return An iterator pointing to the next element (or end()).
1358 * This function will erase the element at the given position and thus
1359 * shorten the %deque by one.
1361 * The user is cautioned that
1362 * this function only erases the element, and that if the element is
1363 * itself a pointer, the pointed-to memory is not touched in any way.
1364 * Managing the pointer is the user's responsibility.
1367 erase(iterator __position);
1370 * @brief Remove a range of elements.
1371 * @param first Iterator pointing to the first element to be erased.
1372 * @param last Iterator pointing to one past the last element to be
1374 * @return An iterator pointing to the element pointed to by @a last
1375 * prior to erasing (or end()).
1377 * This function will erase the elements in the range [first,last) and
1378 * shorten the %deque accordingly.
1380 * The user is cautioned that
1381 * this function only erases the elements, and that if the elements
1382 * themselves are pointers, the pointed-to memory is not touched in any
1383 * way. Managing the pointer is the user's responsibility.
1386 erase(iterator __first, iterator __last);
1389 * @brief Swaps data with another %deque.
1390 * @param x A %deque of the same element and allocator types.
1392 * This exchanges the elements between two deques in constant time.
1393 * (Four pointers, so it should be quite fast.)
1394 * Note that the global std::swap() function is specialized such that
1395 * std::swap(d1,d2) will feed to this function.
1398 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1404 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1405 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1406 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1407 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1409 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1410 // 431. Swapping containers with unequal allocators.
1411 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1412 __x._M_get_Tp_allocator());
1416 * Erases all the elements. Note that this function only erases the
1417 * elements, and that if the elements themselves are pointers, the
1418 * pointed-to memory is not touched in any way. Managing the pointer is
1419 * the user's responsibility.
1423 { _M_erase_at_end(begin()); }
1426 // Internal constructor functions follow.
1428 // called by the range constructor to implement [23.1.1]/9
1430 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1431 // 438. Ambiguity in the "do the right thing" clause
1432 template<typename _Integer>
1434 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1436 _M_initialize_map(static_cast<size_type>(__n));
1437 _M_fill_initialize(__x);
1440 // called by the range constructor to implement [23.1.1]/9
1441 template<typename _InputIterator>
1443 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1446 typedef typename std::iterator_traits<_InputIterator>::
1447 iterator_category _IterCategory;
1448 _M_range_initialize(__first, __last, _IterCategory());
1451 // called by the second initialize_dispatch above
1454 * @brief Fills the deque with whatever is in [first,last).
1455 * @param first An input iterator.
1456 * @param last An input iterator.
1459 * If the iterators are actually forward iterators (or better), then the
1460 * memory layout can be done all at once. Else we move forward using
1461 * push_back on each value from the iterator.
1463 template<typename _InputIterator>
1465 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1466 std::input_iterator_tag);
1468 // called by the second initialize_dispatch above
1469 template<typename _ForwardIterator>
1471 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1472 std::forward_iterator_tag);
1476 * @brief Fills the %deque with copies of value.
1477 * @param value Initial value.
1479 * @pre _M_start and _M_finish have already been initialized,
1480 * but none of the %deque's elements have yet been constructed.
1482 * This function is called only when the user provides an explicit size
1483 * (with or without an explicit exemplar value).
1486 _M_fill_initialize(const value_type& __value);
1488 // Internal assign functions follow. The *_aux functions do the actual
1489 // assignment work for the range versions.
1491 // called by the range assign to implement [23.1.1]/9
1493 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1494 // 438. Ambiguity in the "do the right thing" clause
1495 template<typename _Integer>
1497 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1498 { _M_fill_assign(__n, __val); }
1500 // called by the range assign to implement [23.1.1]/9
1501 template<typename _InputIterator>
1503 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1506 typedef typename std::iterator_traits<_InputIterator>::
1507 iterator_category _IterCategory;
1508 _M_assign_aux(__first, __last, _IterCategory());
1511 // called by the second assign_dispatch above
1512 template<typename _InputIterator>
1514 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1515 std::input_iterator_tag);
1517 // called by the second assign_dispatch above
1518 template<typename _ForwardIterator>
1520 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1521 std::forward_iterator_tag)
1523 const size_type __len = std::distance(__first, __last);
1526 _ForwardIterator __mid = __first;
1527 std::advance(__mid, size());
1528 std::copy(__first, __mid, begin());
1529 insert(end(), __mid, __last);
1532 _M_erase_at_end(std::copy(__first, __last, begin()));
1535 // Called by assign(n,t), and the range assign when it turns out
1536 // to be the same thing.
1538 _M_fill_assign(size_type __n, const value_type& __val)
1542 std::fill(begin(), end(), __val);
1543 insert(end(), __n - size(), __val);
1547 _M_erase_at_end(begin() + difference_type(__n));
1548 std::fill(begin(), end(), __val);
1553 /// Helper functions for push_* and pop_*.
1554 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1555 void _M_push_back_aux(const value_type&);
1557 void _M_push_front_aux(const value_type&);
1559 template<typename... _Args>
1560 void _M_push_back_aux(_Args&&... __args);
1562 template<typename... _Args>
1563 void _M_push_front_aux(_Args&&... __args);
1566 void _M_pop_back_aux();
1568 void _M_pop_front_aux();
1571 // Internal insert functions follow. The *_aux functions do the actual
1572 // insertion work when all shortcuts fail.
1574 // called by the range insert to implement [23.1.1]/9
1576 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1577 // 438. Ambiguity in the "do the right thing" clause
1578 template<typename _Integer>
1580 _M_insert_dispatch(iterator __pos,
1581 _Integer __n, _Integer __x, __true_type)
1582 { _M_fill_insert(__pos, __n, __x); }
1584 // called by the range insert to implement [23.1.1]/9
1585 template<typename _InputIterator>
1587 _M_insert_dispatch(iterator __pos,
1588 _InputIterator __first, _InputIterator __last,
1591 typedef typename std::iterator_traits<_InputIterator>::
1592 iterator_category _IterCategory;
1593 _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1596 // called by the second insert_dispatch above
1597 template<typename _InputIterator>
1599 _M_range_insert_aux(iterator __pos, _InputIterator __first,
1600 _InputIterator __last, std::input_iterator_tag);
1602 // called by the second insert_dispatch above
1603 template<typename _ForwardIterator>
1605 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1606 _ForwardIterator __last, std::forward_iterator_tag);
1608 // Called by insert(p,n,x), and the range insert when it turns out to be
1609 // the same thing. Can use fill functions in optimal situations,
1610 // otherwise passes off to insert_aux(p,n,x).
1612 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1614 // called by insert(p,x)
1615 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1617 _M_insert_aux(iterator __pos, const value_type& __x);
1619 template<typename... _Args>
1621 _M_insert_aux(iterator __pos, _Args&&... __args);
1624 // called by insert(p,n,x) via fill_insert
1626 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1628 // called by range_insert_aux for forward iterators
1629 template<typename _ForwardIterator>
1631 _M_insert_aux(iterator __pos,
1632 _ForwardIterator __first, _ForwardIterator __last,
1636 // Internal erase functions follow.
1639 _M_destroy_data_aux(iterator __first, iterator __last);
1641 // Called by ~deque().
1642 // NB: Doesn't deallocate the nodes.
1643 template<typename _Alloc1>
1645 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
1646 { _M_destroy_data_aux(__first, __last); }
1649 _M_destroy_data(iterator __first, iterator __last,
1650 const std::allocator<_Tp>&)
1652 if (!__has_trivial_destructor(value_type))
1653 _M_destroy_data_aux(__first, __last);
1656 // Called by erase(q1, q2).
1658 _M_erase_at_begin(iterator __pos)
1660 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
1661 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
1662 this->_M_impl._M_start = __pos;
1665 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1666 // _M_fill_assign, operator=.
1668 _M_erase_at_end(iterator __pos)
1670 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
1671 _M_destroy_nodes(__pos._M_node + 1,
1672 this->_M_impl._M_finish._M_node + 1);
1673 this->_M_impl._M_finish = __pos;
1677 /// Memory-handling helpers for the previous internal insert functions.
1679 _M_reserve_elements_at_front(size_type __n)
1681 const size_type __vacancies = this->_M_impl._M_start._M_cur
1682 - this->_M_impl._M_start._M_first;
1683 if (__n > __vacancies)
1684 _M_new_elements_at_front(__n - __vacancies);
1685 return this->_M_impl._M_start - difference_type(__n);
1689 _M_reserve_elements_at_back(size_type __n)
1691 const size_type __vacancies = (this->_M_impl._M_finish._M_last
1692 - this->_M_impl._M_finish._M_cur) - 1;
1693 if (__n > __vacancies)
1694 _M_new_elements_at_back(__n - __vacancies);
1695 return this->_M_impl._M_finish + difference_type(__n);
1699 _M_new_elements_at_front(size_type __new_elements);
1702 _M_new_elements_at_back(size_type __new_elements);
1708 * @brief Memory-handling helpers for the major %map.
1710 * Makes sure the _M_map has space for new nodes. Does not
1711 * actually add the nodes. Can invalidate _M_map pointers.
1712 * (And consequently, %deque iterators.)
1715 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
1717 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1718 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1719 _M_reallocate_map(__nodes_to_add, false);
1723 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
1725 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1726 - this->_M_impl._M_map))
1727 _M_reallocate_map(__nodes_to_add, true);
1731 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1737 * @brief Deque equality comparison.
1738 * @param x A %deque.
1739 * @param y A %deque of the same type as @a x.
1740 * @return True iff the size and elements of the deques are equal.
1742 * This is an equivalence relation. It is linear in the size of the
1743 * deques. Deques are considered equivalent if their sizes are equal,
1744 * and if corresponding elements compare equal.
1746 template<typename _Tp, typename _Alloc>
1748 operator==(const deque<_Tp, _Alloc>& __x,
1749 const deque<_Tp, _Alloc>& __y)
1750 { return __x.size() == __y.size()
1751 && std::equal(__x.begin(), __x.end(), __y.begin()); }
1754 * @brief Deque ordering relation.
1755 * @param x A %deque.
1756 * @param y A %deque of the same type as @a x.
1757 * @return True iff @a x is lexicographically less than @a y.
1759 * This is a total ordering relation. It is linear in the size of the
1760 * deques. The elements must be comparable with @c <.
1762 * See std::lexicographical_compare() for how the determination is made.
1764 template<typename _Tp, typename _Alloc>
1766 operator<(const deque<_Tp, _Alloc>& __x,
1767 const deque<_Tp, _Alloc>& __y)
1768 { return std::lexicographical_compare(__x.begin(), __x.end(),
1769 __y.begin(), __y.end()); }
1771 /// Based on operator==
1772 template<typename _Tp, typename _Alloc>
1774 operator!=(const deque<_Tp, _Alloc>& __x,
1775 const deque<_Tp, _Alloc>& __y)
1776 { return !(__x == __y); }
1778 /// Based on operator<
1779 template<typename _Tp, typename _Alloc>
1781 operator>(const deque<_Tp, _Alloc>& __x,
1782 const deque<_Tp, _Alloc>& __y)
1783 { return __y < __x; }
1785 /// Based on operator<
1786 template<typename _Tp, typename _Alloc>
1788 operator<=(const deque<_Tp, _Alloc>& __x,
1789 const deque<_Tp, _Alloc>& __y)
1790 { return !(__y < __x); }
1792 /// Based on operator<
1793 template<typename _Tp, typename _Alloc>
1795 operator>=(const deque<_Tp, _Alloc>& __x,
1796 const deque<_Tp, _Alloc>& __y)
1797 { return !(__x < __y); }
1799 /// See std::deque::swap().
1800 template<typename _Tp, typename _Alloc>
1802 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1805 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1806 template<typename _Tp, typename _Alloc>
1808 swap(deque<_Tp,_Alloc>&& __x, deque<_Tp,_Alloc>& __y)
1811 template<typename _Tp, typename _Alloc>
1813 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>&& __y)
1817 _GLIBCXX_END_NESTED_NAMESPACE
1819 #endif /* _STL_DEQUE_H */