1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/concept_check.h>
66 #include <bits/stl_iterator_base_types.h>
67 #include <bits/stl_iterator_base_funcs.h>
69 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
73 * @brief This function controls the size of memory nodes.
74 * @param size The size of an element.
75 * @return The number (not byte size) of elements per node.
77 * This function started off as a compiler kludge from SGI, but seems to
78 * be a useful wrapper around a repeated constant expression. The '512' is
79 * tuneable (and no other code needs to change), but no investigation has
80 * been done since inheriting the SGI code.
84 __deque_buf_size(size_t __size)
85 { return __size < 512 ? size_t(512 / __size) : size_t(1); }
89 * @brief A deque::iterator.
91 * Quite a bit of intelligence here. Much of the functionality of
92 * deque is actually passed off to this class. A deque holds two
93 * of these internally, marking its valid range. Access to
94 * elements is done as offsets of either of those two, relying on
95 * operator overloading in this class.
98 * 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) {}
128 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
130 _Deque_iterator(const iterator& __x)
131 : _M_cur(__x._M_cur), _M_first(__x._M_first),
132 _M_last(__x._M_last), _M_node(__x._M_node) {}
146 if (_M_cur == _M_last)
148 _M_set_node(_M_node + 1);
165 if (_M_cur == _M_first)
167 _M_set_node(_M_node - 1);
183 operator+=(difference_type __n)
185 const difference_type __offset = __n + (_M_cur - _M_first);
186 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
190 const difference_type __node_offset =
191 __offset > 0 ? __offset / difference_type(_S_buffer_size())
192 : -difference_type((-__offset - 1)
193 / _S_buffer_size()) - 1;
194 _M_set_node(_M_node + __node_offset);
195 _M_cur = _M_first + (__offset - __node_offset
196 * difference_type(_S_buffer_size()));
202 operator+(difference_type __n) const
209 operator-=(difference_type __n)
210 { return *this += -__n; }
213 operator-(difference_type __n) const
220 operator[](difference_type __n) const
221 { return *(*this + __n); }
224 * Prepares to traverse new_node. Sets everything except
225 * _M_cur, which should therefore be set by the caller
226 * 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*>& __first,
356 const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value);
360 * Deque base class. This class provides the unified face for %deque's
361 * allocation. This class's constructor and destructor allocate and
362 * deallocate (but do not initialize) storage. This makes %exception
365 * Nothing in this class ever constructs or destroys an actual Tp element.
366 * (Deque handles that itself.) Only/All memory management is performed
370 template<typename _Tp, typename _Alloc>
374 typedef _Alloc allocator_type;
377 get_allocator() const
378 { return allocator_type(_M_get_Tp_allocator()); }
380 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
381 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
385 { _M_initialize_map(0); }
387 _Deque_base(const allocator_type& __a, size_t __num_elements)
389 { _M_initialize_map(__num_elements); }
391 _Deque_base(const allocator_type& __a)
398 //This struct encapsulates the implementation of the std::deque
399 //standard container and at the same time makes use of the EBO
400 //for empty allocators.
401 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
403 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
406 : public _Tp_alloc_type
414 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
415 _M_start(), _M_finish()
418 _Deque_impl(const _Tp_alloc_type& __a)
419 : _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
420 _M_start(), _M_finish()
425 _M_get_Tp_allocator()
426 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
428 const _Tp_alloc_type&
429 _M_get_Tp_allocator() const
430 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
433 _M_get_map_allocator() const
434 { return _Map_alloc_type(_M_get_Tp_allocator()); }
439 return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
443 _M_deallocate_node(_Tp* __p)
445 _M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
449 _M_allocate_map(size_t __n)
450 { return _M_get_map_allocator().allocate(__n); }
453 _M_deallocate_map(_Tp** __p, size_t __n)
454 { _M_get_map_allocator().deallocate(__p, __n); }
457 void _M_initialize_map(size_t);
458 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
459 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
460 enum { _S_initial_map_size = 8 };
465 template<typename _Tp, typename _Alloc>
466 _Deque_base<_Tp, _Alloc>::
469 if (this->_M_impl._M_map)
471 _M_destroy_nodes(this->_M_impl._M_start._M_node,
472 this->_M_impl._M_finish._M_node + 1);
473 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
479 * @brief Layout storage.
480 * @param num_elements The count of T's for which to allocate space
484 * The initial underlying memory layout is a bit complicated...
487 template<typename _Tp, typename _Alloc>
489 _Deque_base<_Tp, _Alloc>::
490 _M_initialize_map(size_t __num_elements)
492 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
495 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
496 size_t(__num_nodes + 2));
497 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
499 // For "small" maps (needing less than _M_map_size nodes), allocation
500 // starts in the middle elements and grows outwards. So nstart may be
501 // the beginning of _M_map, but for small maps it may be as far in as
504 _Tp** __nstart = (this->_M_impl._M_map
505 + (this->_M_impl._M_map_size - __num_nodes) / 2);
506 _Tp** __nfinish = __nstart + __num_nodes;
509 { _M_create_nodes(__nstart, __nfinish); }
512 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
513 this->_M_impl._M_map = 0;
514 this->_M_impl._M_map_size = 0;
515 __throw_exception_again;
518 this->_M_impl._M_start._M_set_node(__nstart);
519 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
520 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
521 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
523 % __deque_buf_size(sizeof(_Tp)));
526 template<typename _Tp, typename _Alloc>
528 _Deque_base<_Tp, _Alloc>::
529 _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
534 for (__cur = __nstart; __cur < __nfinish; ++__cur)
535 *__cur = this->_M_allocate_node();
539 _M_destroy_nodes(__nstart, __cur);
540 __throw_exception_again;
544 template<typename _Tp, typename _Alloc>
546 _Deque_base<_Tp, _Alloc>::
547 _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
549 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
550 _M_deallocate_node(*__n);
554 * @brief A standard container using fixed-size memory allocation and
555 * constant-time manipulation of elements at either end.
557 * @ingroup Containers
560 * Meets the requirements of a <a href="tables.html#65">container</a>, a
561 * <a href="tables.html#66">reversible container</a>, and a
562 * <a href="tables.html#67">sequence</a>, including the
563 * <a href="tables.html#68">optional sequence requirements</a>.
565 * In previous HP/SGI versions of deque, there was an extra template
566 * parameter so users could control the node size. This extension turned
567 * out to violate the C++ standard (it can be detected using template
568 * template parameters), and it was removed.
571 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
574 * - size_t _M_map_size
575 * - iterator _M_start, _M_finish
577 * map_size is at least 8. %map is an array of map_size
578 * pointers-to-"nodes". (The name %map has nothing to do with the
579 * std::map class, and "nodes" should not be confused with
580 * std::list's usage of "node".)
582 * A "node" has no specific type name as such, but it is referred
583 * to as "node" in this file. It is a simple array-of-Tp. If Tp
584 * is very large, there will be one Tp element per node (i.e., an
585 * "array" of one). For non-huge Tp's, node size is inversely
586 * related to Tp size: the larger the Tp, the fewer Tp's will fit
587 * in a node. The goal here is to keep the total size of a node
588 * relatively small and constant over different Tp's, to improve
589 * allocator efficiency.
591 * Not every pointer in the %map array will point to a node. If
592 * the initial number of elements in the deque is small, the
593 * /middle/ %map pointers will be valid, and the ones at the edges
594 * will be unused. This same situation will arise as the %map
595 * grows: available %map pointers, if any, will be on the ends. As
596 * new nodes are created, only a subset of the %map's pointers need
597 * to be copied "outward".
600 * - For any nonsingular iterator i:
601 * - i.node points to a member of the %map array. (Yes, you read that
602 * correctly: i.node does not actually point to a node.) The member of
603 * the %map array is what actually points to the node.
604 * - i.first == *(i.node) (This points to the node (first Tp element).)
605 * - i.last == i.first + node_size
606 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
607 * the implication of this is that i.cur is always a dereferenceable
608 * pointer, even if i is a past-the-end iterator.
609 * - Start and Finish are always nonsingular iterators. NOTE: this
610 * means that an empty deque must have one node, a deque with <N
611 * elements (where N is the node buffer size) must have one node, a
612 * deque with N through (2N-1) elements must have two nodes, etc.
613 * - For every node other than start.node and finish.node, every
614 * element in the node is an initialized object. If start.node ==
615 * finish.node, then [start.cur, finish.cur) are initialized
616 * objects, and the elements outside that range are uninitialized
617 * storage. Otherwise, [start.cur, start.last) and [finish.first,
618 * finish.cur) are initialized objects, and [start.first, start.cur)
619 * and [finish.cur, finish.last) are uninitialized storage.
620 * - [%map, %map + map_size) is a valid, non-empty range.
621 * - [start.node, finish.node] is a valid range contained within
622 * [%map, %map + map_size).
623 * - A pointer in the range [%map, %map + map_size) points to an allocated
624 * node if and only if the pointer is in the range
625 * [start.node, finish.node].
627 * Here's the magic: nothing in deque is "aware" of the discontiguous
630 * The memory setup and layout occurs in the parent, _Base, and the iterator
631 * class is entirely responsible for "leaping" from one node to the next.
632 * All the implementation routines for deque itself work only through the
633 * start and finish iterators. This keeps the routines simple and sane,
634 * 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 heirarchy.
680 * May be accessed via _M_impl.*
683 using _Base::_M_impl;
686 // [23.2.1.1] construct/copy/destroy
687 // (assign() and get_allocator() are also listed in this section)
689 * @brief Default constructor creates no elements.
695 deque(const allocator_type& __a)
699 * @brief Create a %deque with copies of an exemplar element.
700 * @param n The number of elements to initially create.
701 * @param value An element to copy.
703 * This constructor fills the %deque with @a n copies of @a value.
706 deque(size_type __n, const value_type& __value = value_type(),
707 const allocator_type& __a = allocator_type())
709 { _M_fill_initialize(__value); }
712 * @brief %Deque copy constructor.
713 * @param x A %deque of identical element and allocator types.
715 * The newly-created %deque uses a copy of the allocation object used
718 deque(const deque& __x)
719 : _Base(__x._M_get_Tp_allocator(), __x.size())
720 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
721 this->_M_impl._M_start,
722 _M_get_Tp_allocator()); }
725 * @brief Builds a %deque from a range.
726 * @param first An input iterator.
727 * @param last An input iterator.
729 * Create a %deque consisting of copies of the elements from [first,
732 * If the iterators are forward, bidirectional, or random-access, then
733 * this will call the elements' copy constructor N times (where N is
734 * distance(first,last)) and do no memory reallocation. But if only
735 * input iterators are used, then this will do at most 2N calls to the
736 * copy constructor, and logN memory reallocations.
738 template<typename _InputIterator>
739 deque(_InputIterator __first, _InputIterator __last,
740 const allocator_type& __a = allocator_type())
743 // Check whether it's an integral type. If so, it's not an iterator.
744 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
745 _M_initialize_dispatch(__first, __last, _Integral());
749 * The dtor only erases the elements, and note that if the elements
750 * themselves are pointers, the pointed-to memory is not touched in any
751 * way. Managing the pointer is the user's responsibilty.
754 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
757 * @brief %Deque assignment operator.
758 * @param x A %deque of identical element and allocator types.
760 * All the elements of @a x are copied, but unlike the copy constructor,
761 * the allocator object is not copied.
764 operator=(const deque& __x);
767 * @brief Assigns a given value to a %deque.
768 * @param n Number of elements to be assigned.
769 * @param val Value to be assigned.
771 * This function fills a %deque with @a n copies of the given
772 * value. Note that the assignment completely changes the
773 * %deque and that the resulting %deque's size is the same as
774 * the number of elements assigned. Old data may be lost.
777 assign(size_type __n, const value_type& __val)
778 { _M_fill_assign(__n, __val); }
781 * @brief Assigns a range to a %deque.
782 * @param first An input iterator.
783 * @param last An input iterator.
785 * This function fills a %deque with copies of the elements in the
786 * range [first,last).
788 * Note that the assignment completely changes the %deque and that the
789 * resulting %deque's size is the same as the number of elements
790 * assigned. Old data may be lost.
792 template<typename _InputIterator>
794 assign(_InputIterator __first, _InputIterator __last)
796 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
797 _M_assign_dispatch(__first, __last, _Integral());
800 /// Get a copy of the memory allocation object.
802 get_allocator() const
803 { return _Base::get_allocator(); }
807 * Returns a read/write iterator that points to the first element in the
808 * %deque. Iteration is done in ordinary element order.
812 { return this->_M_impl._M_start; }
815 * Returns a read-only (constant) iterator that points to the first
816 * element in the %deque. Iteration is done in ordinary element order.
820 { return this->_M_impl._M_start; }
823 * Returns a read/write iterator that points one past the last
824 * element in the %deque. Iteration is done in ordinary
829 { return this->_M_impl._M_finish; }
832 * Returns a read-only (constant) iterator that points one past
833 * the last element in the %deque. Iteration is done in
834 * ordinary element order.
838 { return this->_M_impl._M_finish; }
841 * Returns a read/write reverse iterator that points to the
842 * last element in the %deque. Iteration is done in reverse
847 { return reverse_iterator(this->_M_impl._M_finish); }
850 * Returns a read-only (constant) reverse iterator that points
851 * to the last element in the %deque. Iteration is done in
852 * reverse element order.
854 const_reverse_iterator
856 { return const_reverse_iterator(this->_M_impl._M_finish); }
859 * Returns a read/write reverse iterator that points to one
860 * before the first element in the %deque. Iteration is done
861 * in reverse element order.
865 { return reverse_iterator(this->_M_impl._M_start); }
868 * Returns a read-only (constant) reverse iterator that points
869 * to one before the first element in the %deque. Iteration is
870 * done in reverse element order.
872 const_reverse_iterator
874 { return const_reverse_iterator(this->_M_impl._M_start); }
876 // [23.2.1.2] capacity
877 /** Returns the number of elements in the %deque. */
880 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
882 /** Returns the size() of the largest possible %deque. */
885 { return _M_get_Tp_allocator().max_size(); }
888 * @brief Resizes the %deque to the specified number of elements.
889 * @param new_size Number of elements the %deque should contain.
890 * @param x Data with which new elements should be populated.
892 * This function will %resize the %deque to the specified
893 * number of elements. If the number is smaller than the
894 * %deque's current size the %deque is truncated, otherwise the
895 * %deque is extended and new elements are populated with given
899 resize(size_type __new_size, value_type __x = value_type())
901 const size_type __len = size();
902 if (__new_size < __len)
903 _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size));
905 insert(this->_M_impl._M_finish, __new_size - __len, __x);
909 * Returns true if the %deque is empty. (Thus begin() would
914 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
918 * @brief Subscript access to the data contained in the %deque.
919 * @param n The index of the element for which data should be
921 * @return Read/write reference to data.
923 * This operator allows for easy, array-style, data access.
924 * Note that data access with this operator is unchecked and
925 * out_of_range lookups are not defined. (For checked lookups
929 operator[](size_type __n)
930 { return this->_M_impl._M_start[difference_type(__n)]; }
933 * @brief Subscript access to the data contained in the %deque.
934 * @param n The index of the element for which data should be
936 * @return Read-only (constant) reference to data.
938 * This operator allows for easy, array-style, data access.
939 * Note that data access with this operator is unchecked and
940 * out_of_range lookups are not defined. (For checked lookups
944 operator[](size_type __n) const
945 { return this->_M_impl._M_start[difference_type(__n)]; }
948 /// @if maint Safety check used only from at(). @endif
950 _M_range_check(size_type __n) const
952 if (__n >= this->size())
953 __throw_out_of_range(__N("deque::_M_range_check"));
958 * @brief Provides access to the data contained in the %deque.
959 * @param n The index of the element for which data should be
961 * @return Read/write reference to data.
962 * @throw std::out_of_range If @a n is an invalid index.
964 * This function provides for safer data access. The parameter
965 * is first checked that it is in the range of the deque. The
966 * function throws out_of_range if the check fails.
976 * @brief Provides access to the data contained in the %deque.
977 * @param n The index of the element for which data should be
979 * @return Read-only (constant) reference to data.
980 * @throw std::out_of_range If @a n is an invalid index.
982 * This function provides for safer data access. The parameter is first
983 * checked that it is in the range of the deque. The function throws
984 * out_of_range if the check fails.
987 at(size_type __n) const
994 * Returns a read/write reference to the data at the first
995 * element of the %deque.
1002 * Returns a read-only (constant) reference to the data at the first
1003 * element of the %deque.
1007 { return *begin(); }
1010 * Returns a read/write reference to the data at the last element of the
1016 iterator __tmp = end();
1022 * Returns a read-only (constant) reference to the data at the last
1023 * element of the %deque.
1028 const_iterator __tmp = end();
1033 // [23.2.1.2] modifiers
1035 * @brief Add data to the front of the %deque.
1036 * @param x Data to be added.
1038 * This is a typical stack operation. The function creates an
1039 * element at the front of the %deque and assigns the given
1040 * data to it. Due to the nature of a %deque this operation
1041 * can be done in constant time.
1044 push_front(const value_type& __x)
1046 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1048 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1049 --this->_M_impl._M_start._M_cur;
1052 _M_push_front_aux(__x);
1056 * @brief Add data to the end of the %deque.
1057 * @param x Data to be added.
1059 * This is a typical stack operation. The function creates an
1060 * element at the end of the %deque and assigns the given data
1061 * to it. Due to the nature of a %deque this operation can be
1062 * done in constant time.
1065 push_back(const value_type& __x)
1067 if (this->_M_impl._M_finish._M_cur
1068 != this->_M_impl._M_finish._M_last - 1)
1070 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1071 ++this->_M_impl._M_finish._M_cur;
1074 _M_push_back_aux(__x);
1078 * @brief Removes first element.
1080 * This is a typical stack operation. It shrinks the %deque by one.
1082 * Note that no data is returned, and if the first element's data is
1083 * needed, it should be retrieved before pop_front() is called.
1088 if (this->_M_impl._M_start._M_cur
1089 != this->_M_impl._M_start._M_last - 1)
1091 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1092 ++this->_M_impl._M_start._M_cur;
1099 * @brief Removes last element.
1101 * This is a typical stack operation. It shrinks the %deque by one.
1103 * Note that no data is returned, and if the last element's data is
1104 * needed, it should be retrieved before pop_back() is called.
1109 if (this->_M_impl._M_finish._M_cur
1110 != this->_M_impl._M_finish._M_first)
1112 --this->_M_impl._M_finish._M_cur;
1113 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1120 * @brief Inserts given value into %deque before specified iterator.
1121 * @param position An iterator into the %deque.
1122 * @param x Data to be inserted.
1123 * @return An iterator that points to the inserted data.
1125 * This function will insert a copy of the given value before the
1126 * specified location.
1129 insert(iterator __position, const value_type& __x);
1132 * @brief Inserts a number of copies of given data into the %deque.
1133 * @param position An iterator into the %deque.
1134 * @param n Number of elements to be inserted.
1135 * @param x Data to be inserted.
1137 * This function will insert a specified number of copies of the given
1138 * data before the location specified by @a position.
1141 insert(iterator __position, size_type __n, const value_type& __x)
1142 { _M_fill_insert(__position, __n, __x); }
1145 * @brief Inserts a range into the %deque.
1146 * @param position An iterator into the %deque.
1147 * @param first An input iterator.
1148 * @param last An input iterator.
1150 * This function will insert copies of the data in the range
1151 * [first,last) into the %deque before the location specified
1152 * by @a pos. This is known as "range insert."
1154 template<typename _InputIterator>
1156 insert(iterator __position, _InputIterator __first,
1157 _InputIterator __last)
1159 // Check whether it's an integral type. If so, it's not an iterator.
1160 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1161 _M_insert_dispatch(__position, __first, __last, _Integral());
1165 * @brief Remove element at given position.
1166 * @param position Iterator pointing to element to be erased.
1167 * @return An iterator pointing to the next element (or end()).
1169 * This function will erase the element at the given position and thus
1170 * shorten the %deque by one.
1172 * The user is cautioned that
1173 * this function only erases the element, and that if the element is
1174 * itself a pointer, the pointed-to memory is not touched in any way.
1175 * Managing the pointer is the user's responsibilty.
1178 erase(iterator __position);
1181 * @brief Remove a range of elements.
1182 * @param first Iterator pointing to the first element to be erased.
1183 * @param last Iterator pointing to one past the last element to be
1185 * @return An iterator pointing to the element pointed to by @a last
1186 * prior to erasing (or end()).
1188 * This function will erase the elements in the range [first,last) and
1189 * shorten the %deque accordingly.
1191 * The user is cautioned that
1192 * this function only erases the elements, and that if the elements
1193 * themselves are pointers, the pointed-to memory is not touched in any
1194 * way. Managing the pointer is the user's responsibilty.
1197 erase(iterator __first, iterator __last);
1200 * @brief Swaps data with another %deque.
1201 * @param x A %deque of the same element and allocator types.
1203 * This exchanges the elements between two deques in constant time.
1204 * (Four pointers, so it should be quite fast.)
1205 * Note that the global std::swap() function is specialized such that
1206 * std::swap(d1,d2) will feed to this function.
1211 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1212 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1213 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1214 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1216 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1217 // 431. Swapping containers with unequal allocators.
1218 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1219 __x._M_get_Tp_allocator());
1223 * Erases all the elements. Note that this function only erases the
1224 * elements, and that if the elements themselves are pointers, the
1225 * pointed-to memory is not touched in any way. Managing the pointer is
1226 * the user's responsibilty.
1230 { _M_erase_at_end(begin()); }
1233 // Internal constructor functions follow.
1235 // called by the range constructor to implement [23.1.1]/9
1236 template<typename _Integer>
1238 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1240 _M_initialize_map(__n);
1241 _M_fill_initialize(__x);
1244 // called by the range constructor to implement [23.1.1]/9
1245 template<typename _InputIterator>
1247 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1250 typedef typename std::iterator_traits<_InputIterator>::
1251 iterator_category _IterCategory;
1252 _M_range_initialize(__first, __last, _IterCategory());
1255 // called by the second initialize_dispatch above
1259 * @brief Fills the deque with whatever is in [first,last).
1260 * @param first An input iterator.
1261 * @param last An input iterator.
1264 * If the iterators are actually forward iterators (or better), then the
1265 * memory layout can be done all at once. Else we move forward using
1266 * push_back on each value from the iterator.
1269 template<typename _InputIterator>
1271 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1272 std::input_iterator_tag);
1274 // called by the second initialize_dispatch above
1275 template<typename _ForwardIterator>
1277 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1278 std::forward_iterator_tag);
1283 * @brief Fills the %deque with copies of value.
1284 * @param value Initial value.
1286 * @pre _M_start and _M_finish have already been initialized,
1287 * but none of the %deque's elements have yet been constructed.
1289 * This function is called only when the user provides an explicit size
1290 * (with or without an explicit exemplar value).
1294 _M_fill_initialize(const value_type& __value);
1296 // Internal assign functions follow. The *_aux functions do the actual
1297 // assignment work for the range versions.
1299 // called by the range assign to implement [23.1.1]/9
1300 template<typename _Integer>
1302 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1304 _M_fill_assign(static_cast<size_type>(__n),
1305 static_cast<value_type>(__val));
1308 // called by the range assign to implement [23.1.1]/9
1309 template<typename _InputIterator>
1311 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1314 typedef typename std::iterator_traits<_InputIterator>::
1315 iterator_category _IterCategory;
1316 _M_assign_aux(__first, __last, _IterCategory());
1319 // called by the second assign_dispatch above
1320 template<typename _InputIterator>
1322 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1323 std::input_iterator_tag);
1325 // called by the second assign_dispatch above
1326 template<typename _ForwardIterator>
1328 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1329 std::forward_iterator_tag)
1331 const size_type __len = std::distance(__first, __last);
1334 _ForwardIterator __mid = __first;
1335 std::advance(__mid, size());
1336 std::copy(__first, __mid, begin());
1337 insert(end(), __mid, __last);
1340 _M_erase_at_end(std::copy(__first, __last, begin()));
1343 // Called by assign(n,t), and the range assign when it turns out
1344 // to be the same thing.
1346 _M_fill_assign(size_type __n, const value_type& __val)
1350 std::fill(begin(), end(), __val);
1351 insert(end(), __n - size(), __val);
1355 _M_erase_at_end(begin() + difference_type(__n));
1356 std::fill(begin(), end(), __val);
1363 * @brief Helper functions for push_* and pop_*.
1366 void _M_push_back_aux(const value_type&);
1368 void _M_push_front_aux(const value_type&);
1370 void _M_pop_back_aux();
1372 void _M_pop_front_aux();
1375 // Internal insert functions follow. The *_aux functions do the actual
1376 // insertion work when all shortcuts fail.
1378 // called by the range insert to implement [23.1.1]/9
1379 template<typename _Integer>
1381 _M_insert_dispatch(iterator __pos,
1382 _Integer __n, _Integer __x, __true_type)
1384 _M_fill_insert(__pos, static_cast<size_type>(__n),
1385 static_cast<value_type>(__x));
1388 // called by the range insert to implement [23.1.1]/9
1389 template<typename _InputIterator>
1391 _M_insert_dispatch(iterator __pos,
1392 _InputIterator __first, _InputIterator __last,
1395 typedef typename std::iterator_traits<_InputIterator>::
1396 iterator_category _IterCategory;
1397 _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1400 // called by the second insert_dispatch above
1401 template<typename _InputIterator>
1403 _M_range_insert_aux(iterator __pos, _InputIterator __first,
1404 _InputIterator __last, std::input_iterator_tag);
1406 // called by the second insert_dispatch above
1407 template<typename _ForwardIterator>
1409 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1410 _ForwardIterator __last, std::forward_iterator_tag);
1412 // Called by insert(p,n,x), and the range insert when it turns out to be
1413 // the same thing. Can use fill functions in optimal situations,
1414 // otherwise passes off to insert_aux(p,n,x).
1416 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1418 // called by insert(p,x)
1420 _M_insert_aux(iterator __pos, const value_type& __x);
1422 // called by insert(p,n,x) via fill_insert
1424 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1426 // called by range_insert_aux for forward iterators
1427 template<typename _ForwardIterator>
1429 _M_insert_aux(iterator __pos,
1430 _ForwardIterator __first, _ForwardIterator __last,
1434 // Internal erase functions follow.
1437 _M_destroy_data_aux(iterator __first, iterator __last);
1440 _M_destroy_data_dispatch(iterator, iterator, __true_type) { }
1443 _M_destroy_data_dispatch(iterator __first, iterator __last, __false_type)
1444 { _M_destroy_data_aux(__first, __last); }
1446 // Called by ~deque().
1447 // NB: Doesn't deallocate the nodes.
1448 template<typename _Alloc1>
1450 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
1451 { _M_destroy_data_aux(__first, __last); }
1454 _M_destroy_data(iterator __first, iterator __last,
1455 const std::allocator<_Tp>&)
1457 typedef typename std::__is_scalar<value_type>::__type
1458 _Has_trivial_destructor;
1459 _M_destroy_data_dispatch(__first, __last, _Has_trivial_destructor());
1462 // Called by erase(q1, q2).
1464 _M_erase_at_begin(iterator __pos)
1466 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
1467 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
1468 this->_M_impl._M_start = __pos;
1471 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1472 // _M_fill_assign, operator=.
1474 _M_erase_at_end(iterator __pos)
1476 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
1477 _M_destroy_nodes(__pos._M_node + 1,
1478 this->_M_impl._M_finish._M_node + 1);
1479 this->_M_impl._M_finish = __pos;
1485 * @brief Memory-handling helpers for the previous internal insert
1490 _M_reserve_elements_at_front(size_type __n)
1492 const size_type __vacancies = this->_M_impl._M_start._M_cur
1493 - this->_M_impl._M_start._M_first;
1494 if (__n > __vacancies)
1495 _M_new_elements_at_front(__n - __vacancies);
1496 return this->_M_impl._M_start - difference_type(__n);
1500 _M_reserve_elements_at_back(size_type __n)
1502 const size_type __vacancies = (this->_M_impl._M_finish._M_last
1503 - this->_M_impl._M_finish._M_cur) - 1;
1504 if (__n > __vacancies)
1505 _M_new_elements_at_back(__n - __vacancies);
1506 return this->_M_impl._M_finish + difference_type(__n);
1510 _M_new_elements_at_front(size_type __new_elements);
1513 _M_new_elements_at_back(size_type __new_elements);
1520 * @brief Memory-handling helpers for the major %map.
1522 * Makes sure the _M_map has space for new nodes. Does not
1523 * actually add the nodes. Can invalidate _M_map pointers.
1524 * (And consequently, %deque iterators.)
1528 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
1530 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1531 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1532 _M_reallocate_map(__nodes_to_add, false);
1536 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
1538 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1539 - this->_M_impl._M_map))
1540 _M_reallocate_map(__nodes_to_add, true);
1544 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1550 * @brief Deque equality comparison.
1551 * @param x A %deque.
1552 * @param y A %deque of the same type as @a x.
1553 * @return True iff the size and elements of the deques are equal.
1555 * This is an equivalence relation. It is linear in the size of the
1556 * deques. Deques are considered equivalent if their sizes are equal,
1557 * and if corresponding elements compare equal.
1559 template<typename _Tp, typename _Alloc>
1561 operator==(const deque<_Tp, _Alloc>& __x,
1562 const deque<_Tp, _Alloc>& __y)
1563 { return __x.size() == __y.size()
1564 && std::equal(__x.begin(), __x.end(), __y.begin()); }
1567 * @brief Deque ordering relation.
1568 * @param x A %deque.
1569 * @param y A %deque of the same type as @a x.
1570 * @return True iff @a x is lexicographically less than @a y.
1572 * This is a total ordering relation. It is linear in the size of the
1573 * deques. The elements must be comparable with @c <.
1575 * See std::lexicographical_compare() for how the determination is made.
1577 template<typename _Tp, typename _Alloc>
1579 operator<(const deque<_Tp, _Alloc>& __x,
1580 const deque<_Tp, _Alloc>& __y)
1581 { return std::lexicographical_compare(__x.begin(), __x.end(),
1582 __y.begin(), __y.end()); }
1584 /// Based on operator==
1585 template<typename _Tp, typename _Alloc>
1587 operator!=(const deque<_Tp, _Alloc>& __x,
1588 const deque<_Tp, _Alloc>& __y)
1589 { return !(__x == __y); }
1591 /// Based on operator<
1592 template<typename _Tp, typename _Alloc>
1594 operator>(const deque<_Tp, _Alloc>& __x,
1595 const deque<_Tp, _Alloc>& __y)
1596 { return __y < __x; }
1598 /// Based on operator<
1599 template<typename _Tp, typename _Alloc>
1601 operator<=(const deque<_Tp, _Alloc>& __x,
1602 const deque<_Tp, _Alloc>& __y)
1603 { return !(__y < __x); }
1605 /// Based on operator<
1606 template<typename _Tp, typename _Alloc>
1608 operator>=(const deque<_Tp, _Alloc>& __x,
1609 const deque<_Tp, _Alloc>& __y)
1610 { return !(__x < __y); }
1612 /// See std::deque::swap().
1613 template<typename _Tp, typename _Alloc>
1615 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1618 _GLIBCXX_END_NESTED_NAMESPACE
1620 #endif /* _DEQUE_H */