Merge from trunk @ 138209
[official-gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
blob070fc07890994e65a005760c52abc0b98761cb9e
1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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.
33 * Copyright (c) 1994
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.
45 * Copyright (c) 1997
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.
57 /** @file stl_deque.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _STL_DEQUE_H
63 #define _STL_DEQUE_H 1
65 #include <bits/concept_check.h>
66 #include <bits/stl_iterator_base_types.h>
67 #include <bits/stl_iterator_base_funcs.h>
68 #include <initializer_list>
70 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
72 /**
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 * tunable (and no other code needs to change), but no investigation has
80 * been done since inheriting the SGI code.
82 inline size_t
83 __deque_buf_size(size_t __size)
84 { return __size < 512 ? size_t(512 / __size) : size_t(1); }
87 /**
88 * @brief A deque::iterator.
90 * Quite a bit of intelligence here. Much of the functionality of
91 * deque is actually passed off to this class. A deque holds two
92 * of these internally, marking its valid range. Access to
93 * elements is done as offsets of either of those two, relying on
94 * operator overloading in this class.
96 * All the functions are op overloads except for _M_set_node.
98 template<typename _Tp, typename _Ref, typename _Ptr>
99 struct _Deque_iterator
101 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
102 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
104 static size_t _S_buffer_size()
105 { return __deque_buf_size(sizeof(_Tp)); }
107 typedef std::random_access_iterator_tag iterator_category;
108 typedef _Tp value_type;
109 typedef _Ptr pointer;
110 typedef _Ref reference;
111 typedef size_t size_type;
112 typedef ptrdiff_t difference_type;
113 typedef _Tp** _Map_pointer;
114 typedef _Deque_iterator _Self;
116 _Tp* _M_cur;
117 _Tp* _M_first;
118 _Tp* _M_last;
119 _Map_pointer _M_node;
121 _Deque_iterator(_Tp* __x, _Map_pointer __y)
122 : _M_cur(__x), _M_first(*__y),
123 _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
125 _Deque_iterator()
126 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
128 _Deque_iterator(const iterator& __x)
129 : _M_cur(__x._M_cur), _M_first(__x._M_first),
130 _M_last(__x._M_last), _M_node(__x._M_node) { }
132 reference
133 operator*() const
134 { return *_M_cur; }
136 pointer
137 operator->() const
138 { return _M_cur; }
140 _Self&
141 operator++()
143 ++_M_cur;
144 if (_M_cur == _M_last)
146 _M_set_node(_M_node + 1);
147 _M_cur = _M_first;
149 return *this;
152 _Self
153 operator++(int)
155 _Self __tmp = *this;
156 ++*this;
157 return __tmp;
160 _Self&
161 operator--()
163 if (_M_cur == _M_first)
165 _M_set_node(_M_node - 1);
166 _M_cur = _M_last;
168 --_M_cur;
169 return *this;
172 _Self
173 operator--(int)
175 _Self __tmp = *this;
176 --*this;
177 return __tmp;
180 _Self&
181 operator+=(difference_type __n)
183 const difference_type __offset = __n + (_M_cur - _M_first);
184 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
185 _M_cur += __n;
186 else
188 const difference_type __node_offset =
189 __offset > 0 ? __offset / difference_type(_S_buffer_size())
190 : -difference_type((-__offset - 1)
191 / _S_buffer_size()) - 1;
192 _M_set_node(_M_node + __node_offset);
193 _M_cur = _M_first + (__offset - __node_offset
194 * difference_type(_S_buffer_size()));
196 return *this;
199 _Self
200 operator+(difference_type __n) const
202 _Self __tmp = *this;
203 return __tmp += __n;
206 _Self&
207 operator-=(difference_type __n)
208 { return *this += -__n; }
210 _Self
211 operator-(difference_type __n) const
213 _Self __tmp = *this;
214 return __tmp -= __n;
217 reference
218 operator[](difference_type __n) const
219 { return *(*this + __n); }
221 /**
222 * Prepares to traverse new_node. Sets everything except
223 * _M_cur, which should therefore be set by the caller
224 * immediately afterwards, based on _M_first and _M_last.
226 void
227 _M_set_node(_Map_pointer __new_node)
229 _M_node = __new_node;
230 _M_first = *__new_node;
231 _M_last = _M_first + difference_type(_S_buffer_size());
235 // Note: we also provide overloads whose operands are of the same type in
236 // order to avoid ambiguous overload resolution when std::rel_ops operators
237 // are in scope (for additional details, see libstdc++/3628)
238 template<typename _Tp, typename _Ref, typename _Ptr>
239 inline bool
240 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
241 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
242 { return __x._M_cur == __y._M_cur; }
244 template<typename _Tp, typename _RefL, typename _PtrL,
245 typename _RefR, typename _PtrR>
246 inline bool
247 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
248 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
249 { return __x._M_cur == __y._M_cur; }
251 template<typename _Tp, typename _Ref, typename _Ptr>
252 inline bool
253 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
254 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
255 { return !(__x == __y); }
257 template<typename _Tp, typename _RefL, typename _PtrL,
258 typename _RefR, typename _PtrR>
259 inline bool
260 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
261 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
262 { return !(__x == __y); }
264 template<typename _Tp, typename _Ref, typename _Ptr>
265 inline bool
266 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
267 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
268 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
269 : (__x._M_node < __y._M_node); }
271 template<typename _Tp, typename _RefL, typename _PtrL,
272 typename _RefR, typename _PtrR>
273 inline bool
274 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
275 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
276 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
277 : (__x._M_node < __y._M_node); }
279 template<typename _Tp, typename _Ref, typename _Ptr>
280 inline bool
281 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
282 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
283 { return __y < __x; }
285 template<typename _Tp, typename _RefL, typename _PtrL,
286 typename _RefR, typename _PtrR>
287 inline bool
288 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
289 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
290 { return __y < __x; }
292 template<typename _Tp, typename _Ref, typename _Ptr>
293 inline bool
294 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
295 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
296 { return !(__y < __x); }
298 template<typename _Tp, typename _RefL, typename _PtrL,
299 typename _RefR, typename _PtrR>
300 inline bool
301 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
302 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
303 { return !(__y < __x); }
305 template<typename _Tp, typename _Ref, typename _Ptr>
306 inline bool
307 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
308 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
309 { return !(__x < __y); }
311 template<typename _Tp, typename _RefL, typename _PtrL,
312 typename _RefR, typename _PtrR>
313 inline bool
314 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
315 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
316 { return !(__x < __y); }
318 // _GLIBCXX_RESOLVE_LIB_DEFECTS
319 // According to the resolution of DR179 not only the various comparison
320 // operators but also operator- must accept mixed iterator/const_iterator
321 // parameters.
322 template<typename _Tp, typename _Ref, typename _Ptr>
323 inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
324 operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
325 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
327 return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
328 (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
329 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
330 + (__y._M_last - __y._M_cur);
333 template<typename _Tp, typename _RefL, typename _PtrL,
334 typename _RefR, typename _PtrR>
335 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
336 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
337 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
339 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
340 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
341 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
342 + (__y._M_last - __y._M_cur);
345 template<typename _Tp, typename _Ref, typename _Ptr>
346 inline _Deque_iterator<_Tp, _Ref, _Ptr>
347 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
348 { return __x + __n; }
350 template<typename _Tp>
351 void
352 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
353 const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value);
356 * Deque base class. This class provides the unified face for %deque's
357 * allocation. This class's constructor and destructor allocate and
358 * deallocate (but do not initialize) storage. This makes %exception
359 * safety easier.
361 * Nothing in this class ever constructs or destroys an actual Tp element.
362 * (Deque handles that itself.) Only/All memory management is performed
363 * here.
365 template<typename _Tp, typename _Alloc>
366 class _Deque_base
368 public:
369 typedef _Alloc allocator_type;
371 allocator_type
372 get_allocator() const
373 { return allocator_type(_M_get_Tp_allocator()); }
375 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
376 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
378 _Deque_base()
379 : _M_impl()
380 { _M_initialize_map(0); }
382 _Deque_base(const allocator_type& __a, size_t __num_elements)
383 : _M_impl(__a)
384 { _M_initialize_map(__num_elements); }
386 _Deque_base(const allocator_type& __a)
387 : _M_impl(__a)
390 #ifdef __GXX_EXPERIMENTAL_CXX0X__
391 _Deque_base(_Deque_base&& __x)
392 : _M_impl(__x._M_get_Tp_allocator())
394 _M_initialize_map(0);
395 if (__x._M_impl._M_map)
397 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
398 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
399 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
400 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
403 #endif
405 ~_Deque_base();
407 protected:
408 //This struct encapsulates the implementation of the std::deque
409 //standard container and at the same time makes use of the EBO
410 //for empty allocators.
411 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
413 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
415 struct _Deque_impl
416 : public _Tp_alloc_type
418 _Tp** _M_map;
419 size_t _M_map_size;
420 iterator _M_start;
421 iterator _M_finish;
423 _Deque_impl()
424 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
425 _M_start(), _M_finish()
428 _Deque_impl(const _Tp_alloc_type& __a)
429 : _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
430 _M_start(), _M_finish()
434 _Tp_alloc_type&
435 _M_get_Tp_allocator()
436 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
438 const _Tp_alloc_type&
439 _M_get_Tp_allocator() const
440 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
442 _Map_alloc_type
443 _M_get_map_allocator() const
444 { return _Map_alloc_type(_M_get_Tp_allocator()); }
446 _Tp*
447 _M_allocate_node()
449 return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
452 void
453 _M_deallocate_node(_Tp* __p)
455 _M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
458 _Tp**
459 _M_allocate_map(size_t __n)
460 { return _M_get_map_allocator().allocate(__n); }
462 void
463 _M_deallocate_map(_Tp** __p, size_t __n)
464 { _M_get_map_allocator().deallocate(__p, __n); }
466 protected:
467 void _M_initialize_map(size_t);
468 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
469 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
470 enum { _S_initial_map_size = 8 };
472 _Deque_impl _M_impl;
475 template<typename _Tp, typename _Alloc>
476 _Deque_base<_Tp, _Alloc>::
477 ~_Deque_base()
479 if (this->_M_impl._M_map)
481 _M_destroy_nodes(this->_M_impl._M_start._M_node,
482 this->_M_impl._M_finish._M_node + 1);
483 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
488 * @brief Layout storage.
489 * @param num_elements The count of T's for which to allocate space
490 * at first.
491 * @return Nothing.
493 * The initial underlying memory layout is a bit complicated...
495 template<typename _Tp, typename _Alloc>
496 void
497 _Deque_base<_Tp, _Alloc>::
498 _M_initialize_map(size_t __num_elements)
500 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
501 + 1);
503 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
504 size_t(__num_nodes + 2));
505 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
507 // For "small" maps (needing less than _M_map_size nodes), allocation
508 // starts in the middle elements and grows outwards. So nstart may be
509 // the beginning of _M_map, but for small maps it may be as far in as
510 // _M_map+3.
512 _Tp** __nstart = (this->_M_impl._M_map
513 + (this->_M_impl._M_map_size - __num_nodes) / 2);
514 _Tp** __nfinish = __nstart + __num_nodes;
517 { _M_create_nodes(__nstart, __nfinish); }
518 catch(...)
520 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
521 this->_M_impl._M_map = 0;
522 this->_M_impl._M_map_size = 0;
523 __throw_exception_again;
526 this->_M_impl._M_start._M_set_node(__nstart);
527 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
528 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
529 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
530 + __num_elements
531 % __deque_buf_size(sizeof(_Tp)));
534 template<typename _Tp, typename _Alloc>
535 void
536 _Deque_base<_Tp, _Alloc>::
537 _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
539 _Tp** __cur;
542 for (__cur = __nstart; __cur < __nfinish; ++__cur)
543 *__cur = this->_M_allocate_node();
545 catch(...)
547 _M_destroy_nodes(__nstart, __cur);
548 __throw_exception_again;
552 template<typename _Tp, typename _Alloc>
553 void
554 _Deque_base<_Tp, _Alloc>::
555 _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
557 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
558 _M_deallocate_node(*__n);
562 * @brief A standard container using fixed-size memory allocation and
563 * constant-time manipulation of elements at either end.
565 * @ingroup Containers
566 * @ingroup Sequences
568 * Meets the requirements of a <a href="tables.html#65">container</a>, a
569 * <a href="tables.html#66">reversible container</a>, and a
570 * <a href="tables.html#67">sequence</a>, including the
571 * <a href="tables.html#68">optional sequence requirements</a>.
573 * In previous HP/SGI versions of deque, there was an extra template
574 * parameter so users could control the node size. This extension turned
575 * out to violate the C++ standard (it can be detected using template
576 * template parameters), and it was removed.
578 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
580 * - Tp** _M_map
581 * - size_t _M_map_size
582 * - iterator _M_start, _M_finish
584 * map_size is at least 8. %map is an array of map_size
585 * pointers-to-"nodes". (The name %map has nothing to do with the
586 * std::map class, and "nodes" should not be confused with
587 * std::list's usage of "node".)
589 * A "node" has no specific type name as such, but it is referred
590 * to as "node" in this file. It is a simple array-of-Tp. If Tp
591 * is very large, there will be one Tp element per node (i.e., an
592 * "array" of one). For non-huge Tp's, node size is inversely
593 * related to Tp size: the larger the Tp, the fewer Tp's will fit
594 * in a node. The goal here is to keep the total size of a node
595 * relatively small and constant over different Tp's, to improve
596 * allocator efficiency.
598 * Not every pointer in the %map array will point to a node. If
599 * the initial number of elements in the deque is small, the
600 * /middle/ %map pointers will be valid, and the ones at the edges
601 * will be unused. This same situation will arise as the %map
602 * grows: available %map pointers, if any, will be on the ends. As
603 * new nodes are created, only a subset of the %map's pointers need
604 * to be copied "outward".
606 * Class invariants:
607 * - For any nonsingular iterator i:
608 * - i.node points to a member of the %map array. (Yes, you read that
609 * correctly: i.node does not actually point to a node.) The member of
610 * the %map array is what actually points to the node.
611 * - i.first == *(i.node) (This points to the node (first Tp element).)
612 * - i.last == i.first + node_size
613 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
614 * the implication of this is that i.cur is always a dereferenceable
615 * pointer, even if i is a past-the-end iterator.
616 * - Start and Finish are always nonsingular iterators. NOTE: this
617 * means that an empty deque must have one node, a deque with <N
618 * elements (where N is the node buffer size) must have one node, a
619 * deque with N through (2N-1) elements must have two nodes, etc.
620 * - For every node other than start.node and finish.node, every
621 * element in the node is an initialized object. If start.node ==
622 * finish.node, then [start.cur, finish.cur) are initialized
623 * objects, and the elements outside that range are uninitialized
624 * storage. Otherwise, [start.cur, start.last) and [finish.first,
625 * finish.cur) are initialized objects, and [start.first, start.cur)
626 * and [finish.cur, finish.last) are uninitialized storage.
627 * - [%map, %map + map_size) is a valid, non-empty range.
628 * - [start.node, finish.node] is a valid range contained within
629 * [%map, %map + map_size).
630 * - A pointer in the range [%map, %map + map_size) points to an allocated
631 * node if and only if the pointer is in the range
632 * [start.node, finish.node].
634 * Here's the magic: nothing in deque is "aware" of the discontiguous
635 * storage!
637 * The memory setup and layout occurs in the parent, _Base, and the iterator
638 * class is entirely responsible for "leaping" from one node to the next.
639 * All the implementation routines for deque itself work only through the
640 * start and finish iterators. This keeps the routines simple and sane,
641 * and we can use other standard algorithms as well.
643 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
644 class deque : protected _Deque_base<_Tp, _Alloc>
646 // concept requirements
647 typedef typename _Alloc::value_type _Alloc_value_type;
648 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
649 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
651 typedef _Deque_base<_Tp, _Alloc> _Base;
652 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
654 public:
655 typedef _Tp value_type;
656 typedef typename _Tp_alloc_type::pointer pointer;
657 typedef typename _Tp_alloc_type::const_pointer const_pointer;
658 typedef typename _Tp_alloc_type::reference reference;
659 typedef typename _Tp_alloc_type::const_reference const_reference;
660 typedef typename _Base::iterator iterator;
661 typedef typename _Base::const_iterator const_iterator;
662 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
663 typedef std::reverse_iterator<iterator> reverse_iterator;
664 typedef size_t size_type;
665 typedef ptrdiff_t difference_type;
666 typedef _Alloc allocator_type;
668 protected:
669 typedef pointer* _Map_pointer;
671 static size_t _S_buffer_size()
672 { return __deque_buf_size(sizeof(_Tp)); }
674 // Functions controlling memory layout, and nothing else.
675 using _Base::_M_initialize_map;
676 using _Base::_M_create_nodes;
677 using _Base::_M_destroy_nodes;
678 using _Base::_M_allocate_node;
679 using _Base::_M_deallocate_node;
680 using _Base::_M_allocate_map;
681 using _Base::_M_deallocate_map;
682 using _Base::_M_get_Tp_allocator;
684 /**
685 * A total of four data members accumulated down the hierarchy.
686 * May be accessed via _M_impl.*
688 using _Base::_M_impl;
690 public:
691 // [23.2.1.1] construct/copy/destroy
692 // (assign() and get_allocator() are also listed in this section)
694 * @brief Default constructor creates no elements.
696 deque()
697 : _Base() { }
700 * @brief Creates a %deque with no elements.
701 * @param a An allocator object.
703 explicit
704 deque(const allocator_type& __a)
705 : _Base(__a, 0) { }
708 * @brief Creates a %deque with copies of an exemplar element.
709 * @param n The number of elements to initially create.
710 * @param value An element to copy.
711 * @param a An allocator.
713 * This constructor fills the %deque with @a n copies of @a value.
715 explicit
716 deque(size_type __n, const value_type& __value = value_type(),
717 const allocator_type& __a = allocator_type())
718 : _Base(__a, __n)
719 { _M_fill_initialize(__value); }
722 * @brief %Deque copy constructor.
723 * @param x A %deque of identical element and allocator types.
725 * The newly-created %deque uses a copy of the allocation object used
726 * by @a x.
728 deque(const deque& __x)
729 : _Base(__x._M_get_Tp_allocator(), __x.size())
730 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
731 this->_M_impl._M_start,
732 _M_get_Tp_allocator()); }
734 #ifdef __GXX_EXPERIMENTAL_CXX0X__
736 * @brief %Deque move constructor.
737 * @param x A %deque of identical element and allocator types.
739 * The newly-created %deque contains the exact contents of @a x.
740 * The contents of @a x are a valid, but unspecified %deque.
742 deque(deque&& __x)
743 : _Base(std::forward<_Base>(__x)) { }
746 * @brief Builds a %deque from an initializer list.
747 * @param l An initializer_list.
748 * @param a An allocator object.
750 * Create a %deque consisting of copies of the elements in the
751 * initializer_list @a l.
753 * This will call the element type's copy constructor N times
754 * (where N is l.size()) and do no memory reallocation.
756 deque(initializer_list<value_type> __l,
757 const allocator_type& __a = allocator_type())
758 : _Base(__a)
760 _M_range_initialize(__l.begin(), __l.end(),
761 random_access_iterator_tag());
763 #endif
766 * @brief Builds a %deque from a range.
767 * @param first An input iterator.
768 * @param last An input iterator.
769 * @param a An allocator object.
771 * Create a %deque consisting of copies of the elements from [first,
772 * last).
774 * If the iterators are forward, bidirectional, or random-access, then
775 * this will call the elements' copy constructor N times (where N is
776 * distance(first,last)) and do no memory reallocation. But if only
777 * input iterators are used, then this will do at most 2N calls to the
778 * copy constructor, and logN memory reallocations.
780 template<typename _InputIterator>
781 deque(_InputIterator __first, _InputIterator __last,
782 const allocator_type& __a = allocator_type())
783 : _Base(__a)
785 // Check whether it's an integral type. If so, it's not an iterator.
786 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
787 _M_initialize_dispatch(__first, __last, _Integral());
791 * The dtor only erases the elements, and note that if the elements
792 * themselves are pointers, the pointed-to memory is not touched in any
793 * way. Managing the pointer is the user's responsibility.
795 ~deque()
796 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
799 * @brief %Deque assignment operator.
800 * @param x A %deque of identical element and allocator types.
802 * All the elements of @a x are copied, but unlike the copy constructor,
803 * the allocator object is not copied.
805 deque&
806 operator=(const deque& __x);
808 #ifdef __GXX_EXPERIMENTAL_CXX0X__
810 * @brief %Deque move assignment operator.
811 * @param x A %deque of identical element and allocator types.
813 * The contents of @a x are moved into this deque (without copying).
814 * @a x is a valid, but unspecified %deque.
816 deque&
817 operator=(deque&& __x)
819 // NB: DR 675.
820 this->clear();
821 this->swap(__x);
822 return *this;
826 * @brief Assigns an initializer list to a %deque.
827 * @param l An initializer_list.
829 * This function fills a %deque with copies of the elements in the
830 * initializer_list @a l.
832 * Note that the assignment completely changes the %deque and that the
833 * resulting %deque's size is the same as the number of elements
834 * assigned. Old data may be lost.
836 deque&
837 operator=(initializer_list<value_type> __l)
839 this->assign(__l.begin(), __l.end());
840 return *this;
842 #endif
845 * @brief Assigns a given value to a %deque.
846 * @param n Number of elements to be assigned.
847 * @param val Value to be assigned.
849 * This function fills a %deque with @a n copies of the given
850 * value. Note that the assignment completely changes the
851 * %deque and that the resulting %deque's size is the same as
852 * the number of elements assigned. Old data may be lost.
854 void
855 assign(size_type __n, const value_type& __val)
856 { _M_fill_assign(__n, __val); }
859 * @brief Assigns a range to a %deque.
860 * @param first An input iterator.
861 * @param last An input iterator.
863 * This function fills a %deque with copies of the elements in the
864 * range [first,last).
866 * Note that the assignment completely changes the %deque and that the
867 * resulting %deque's size is the same as the number of elements
868 * assigned. Old data may be lost.
870 template<typename _InputIterator>
871 void
872 assign(_InputIterator __first, _InputIterator __last)
874 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
875 _M_assign_dispatch(__first, __last, _Integral());
878 #ifdef __GXX_EXPERIMENTAL_CXX0X__
880 * @brief Assigns an initializer list to a %deque.
881 * @param l An initializer_list.
883 * This function fills a %deque with copies of the elements in the
884 * initializer_list @a l.
886 * Note that the assignment completely changes the %deque and that the
887 * resulting %deque's size is the same as the number of elements
888 * assigned. Old data may be lost.
890 void
891 assign(initializer_list<value_type> __l)
892 { this->assign(__l.begin(), __l.end()); }
893 #endif
895 /// Get a copy of the memory allocation object.
896 allocator_type
897 get_allocator() const
898 { return _Base::get_allocator(); }
900 // iterators
902 * Returns a read/write iterator that points to the first element in the
903 * %deque. Iteration is done in ordinary element order.
905 iterator
906 begin()
907 { return this->_M_impl._M_start; }
910 * Returns a read-only (constant) iterator that points to the first
911 * element in the %deque. Iteration is done in ordinary element order.
913 const_iterator
914 begin() const
915 { return this->_M_impl._M_start; }
918 * Returns a read/write iterator that points one past the last
919 * element in the %deque. Iteration is done in ordinary
920 * element order.
922 iterator
923 end()
924 { return this->_M_impl._M_finish; }
927 * Returns a read-only (constant) iterator that points one past
928 * the last element in the %deque. Iteration is done in
929 * ordinary element order.
931 const_iterator
932 end() const
933 { return this->_M_impl._M_finish; }
936 * Returns a read/write reverse iterator that points to the
937 * last element in the %deque. Iteration is done in reverse
938 * element order.
940 reverse_iterator
941 rbegin()
942 { return reverse_iterator(this->_M_impl._M_finish); }
945 * Returns a read-only (constant) reverse iterator that points
946 * to the last element in the %deque. Iteration is done in
947 * reverse element order.
949 const_reverse_iterator
950 rbegin() const
951 { return const_reverse_iterator(this->_M_impl._M_finish); }
954 * Returns a read/write reverse iterator that points to one
955 * before the first element in the %deque. Iteration is done
956 * in reverse element order.
958 reverse_iterator
959 rend()
960 { return reverse_iterator(this->_M_impl._M_start); }
963 * Returns a read-only (constant) reverse iterator that points
964 * to one before the first element in the %deque. Iteration is
965 * done in reverse element order.
967 const_reverse_iterator
968 rend() const
969 { return const_reverse_iterator(this->_M_impl._M_start); }
971 #ifdef __GXX_EXPERIMENTAL_CXX0X__
973 * Returns a read-only (constant) iterator that points to the first
974 * element in the %deque. Iteration is done in ordinary element order.
976 const_iterator
977 cbegin() const
978 { return this->_M_impl._M_start; }
981 * Returns a read-only (constant) iterator that points one past
982 * the last element in the %deque. Iteration is done in
983 * ordinary element order.
985 const_iterator
986 cend() const
987 { return this->_M_impl._M_finish; }
990 * Returns a read-only (constant) reverse iterator that points
991 * to the last element in the %deque. Iteration is done in
992 * reverse element order.
994 const_reverse_iterator
995 crbegin() const
996 { return const_reverse_iterator(this->_M_impl._M_finish); }
999 * Returns a read-only (constant) reverse iterator that points
1000 * to one before the first element in the %deque. Iteration is
1001 * done in reverse element order.
1003 const_reverse_iterator
1004 crend() const
1005 { return const_reverse_iterator(this->_M_impl._M_start); }
1006 #endif
1008 // [23.2.1.2] capacity
1009 /** Returns the number of elements in the %deque. */
1010 size_type
1011 size() const
1012 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1014 /** Returns the size() of the largest possible %deque. */
1015 size_type
1016 max_size() const
1017 { return _M_get_Tp_allocator().max_size(); }
1020 * @brief Resizes the %deque to the specified number of elements.
1021 * @param new_size Number of elements the %deque should contain.
1022 * @param x Data with which new elements should be populated.
1024 * This function will %resize the %deque to the specified
1025 * number of elements. If the number is smaller than the
1026 * %deque's current size the %deque is truncated, otherwise the
1027 * %deque is extended and new elements are populated with given
1028 * data.
1030 void
1031 resize(size_type __new_size, value_type __x = value_type())
1033 const size_type __len = size();
1034 if (__new_size < __len)
1035 _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size));
1036 else
1037 insert(this->_M_impl._M_finish, __new_size - __len, __x);
1041 * Returns true if the %deque is empty. (Thus begin() would
1042 * equal end().)
1044 bool
1045 empty() const
1046 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1048 // element access
1050 * @brief Subscript access to the data contained in the %deque.
1051 * @param n The index of the element for which data should be
1052 * accessed.
1053 * @return Read/write reference to data.
1055 * This operator allows for easy, array-style, data access.
1056 * Note that data access with this operator is unchecked and
1057 * out_of_range lookups are not defined. (For checked lookups
1058 * see at().)
1060 reference
1061 operator[](size_type __n)
1062 { return this->_M_impl._M_start[difference_type(__n)]; }
1065 * @brief Subscript access to the data contained in the %deque.
1066 * @param n The index of the element for which data should be
1067 * accessed.
1068 * @return Read-only (constant) reference to data.
1070 * This operator allows for easy, array-style, data access.
1071 * Note that data access with this operator is unchecked and
1072 * out_of_range lookups are not defined. (For checked lookups
1073 * see at().)
1075 const_reference
1076 operator[](size_type __n) const
1077 { return this->_M_impl._M_start[difference_type(__n)]; }
1079 protected:
1080 /// Safety check used only from at().
1081 void
1082 _M_range_check(size_type __n) const
1084 if (__n >= this->size())
1085 __throw_out_of_range(__N("deque::_M_range_check"));
1088 public:
1090 * @brief Provides access to the data contained in the %deque.
1091 * @param n The index of the element for which data should be
1092 * accessed.
1093 * @return Read/write reference to data.
1094 * @throw std::out_of_range If @a n is an invalid index.
1096 * This function provides for safer data access. The parameter
1097 * is first checked that it is in the range of the deque. The
1098 * function throws out_of_range if the check fails.
1100 reference
1101 at(size_type __n)
1103 _M_range_check(__n);
1104 return (*this)[__n];
1108 * @brief Provides access to the data contained in the %deque.
1109 * @param n The index of the element for which data should be
1110 * accessed.
1111 * @return Read-only (constant) reference to data.
1112 * @throw std::out_of_range If @a n is an invalid index.
1114 * This function provides for safer data access. The parameter is first
1115 * checked that it is in the range of the deque. The function throws
1116 * out_of_range if the check fails.
1118 const_reference
1119 at(size_type __n) const
1121 _M_range_check(__n);
1122 return (*this)[__n];
1126 * Returns a read/write reference to the data at the first
1127 * element of the %deque.
1129 reference
1130 front()
1131 { return *begin(); }
1134 * Returns a read-only (constant) reference to the data at the first
1135 * element of the %deque.
1137 const_reference
1138 front() const
1139 { return *begin(); }
1142 * Returns a read/write reference to the data at the last element of the
1143 * %deque.
1145 reference
1146 back()
1148 iterator __tmp = end();
1149 --__tmp;
1150 return *__tmp;
1154 * Returns a read-only (constant) reference to the data at the last
1155 * element of the %deque.
1157 const_reference
1158 back() const
1160 const_iterator __tmp = end();
1161 --__tmp;
1162 return *__tmp;
1165 // [23.2.1.2] modifiers
1167 * @brief Add data to the front of the %deque.
1168 * @param x Data to be added.
1170 * This is a typical stack operation. The function creates an
1171 * element at the front of the %deque and assigns the given
1172 * data to it. Due to the nature of a %deque this operation
1173 * can be done in constant time.
1175 void
1176 push_front(const value_type& __x)
1178 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1180 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1181 --this->_M_impl._M_start._M_cur;
1183 else
1184 _M_push_front_aux(__x);
1187 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1188 void
1189 push_front(value_type&& __x)
1190 { emplace_front(std::move(__x)); }
1192 template<typename... _Args>
1193 void
1194 emplace_front(_Args&&... __args);
1195 #endif
1198 * @brief Add data to the end of the %deque.
1199 * @param x Data to be added.
1201 * This is a typical stack operation. The function creates an
1202 * element at the end of the %deque and assigns the given data
1203 * to it. Due to the nature of a %deque this operation can be
1204 * done in constant time.
1206 void
1207 push_back(const value_type& __x)
1209 if (this->_M_impl._M_finish._M_cur
1210 != this->_M_impl._M_finish._M_last - 1)
1212 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1213 ++this->_M_impl._M_finish._M_cur;
1215 else
1216 _M_push_back_aux(__x);
1219 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1220 void
1221 push_back(value_type&& __x)
1222 { emplace_back(std::move(__x)); }
1224 template<typename... _Args>
1225 void
1226 emplace_back(_Args&&... __args);
1227 #endif
1230 * @brief Removes first element.
1232 * This is a typical stack operation. It shrinks the %deque by one.
1234 * Note that no data is returned, and if the first element's data is
1235 * needed, it should be retrieved before pop_front() is called.
1237 void
1238 pop_front()
1240 if (this->_M_impl._M_start._M_cur
1241 != this->_M_impl._M_start._M_last - 1)
1243 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1244 ++this->_M_impl._M_start._M_cur;
1246 else
1247 _M_pop_front_aux();
1251 * @brief Removes last element.
1253 * This is a typical stack operation. It shrinks the %deque by one.
1255 * Note that no data is returned, and if the last element's data is
1256 * needed, it should be retrieved before pop_back() is called.
1258 void
1259 pop_back()
1261 if (this->_M_impl._M_finish._M_cur
1262 != this->_M_impl._M_finish._M_first)
1264 --this->_M_impl._M_finish._M_cur;
1265 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1267 else
1268 _M_pop_back_aux();
1271 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1273 * @brief Inserts an object in %deque before specified iterator.
1274 * @param position An iterator into the %deque.
1275 * @param args Arguments.
1276 * @return An iterator that points to the inserted data.
1278 * This function will insert an object of type T constructed
1279 * with T(std::forward<Args>(args)...) before the specified location.
1281 template<typename... _Args>
1282 iterator
1283 emplace(iterator __position, _Args&&... __args);
1284 #endif
1287 * @brief Inserts given value into %deque before specified iterator.
1288 * @param position An iterator into the %deque.
1289 * @param x Data to be inserted.
1290 * @return An iterator that points to the inserted data.
1292 * This function will insert a copy of the given value before the
1293 * specified location.
1295 iterator
1296 insert(iterator __position, const value_type& __x);
1298 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1300 * @brief Inserts given rvalue into %deque before specified iterator.
1301 * @param position An iterator into the %deque.
1302 * @param x Data to be inserted.
1303 * @return An iterator that points to the inserted data.
1305 * This function will insert a copy of the given rvalue before the
1306 * specified location.
1308 iterator
1309 insert(iterator __position, value_type&& __x)
1310 { return emplace(__position, std::move(__x)); }
1313 * @brief Inserts an initializer list into the %deque.
1314 * @param p An iterator into the %deque.
1315 * @param l An initializer_list.
1317 * This function will insert copies of the data in the
1318 * initializer_list @a l into the %deque before the location
1319 * specified by @a p. This is known as "list insert."
1321 void
1322 insert(iterator __p, initializer_list<value_type> __l)
1323 { this->insert(__p, __l.begin(), __l.end()); }
1324 #endif
1327 * @brief Inserts a number of copies of given data into the %deque.
1328 * @param position An iterator into the %deque.
1329 * @param n Number of elements to be inserted.
1330 * @param x Data to be inserted.
1332 * This function will insert a specified number of copies of the given
1333 * data before the location specified by @a position.
1335 void
1336 insert(iterator __position, size_type __n, const value_type& __x)
1337 { _M_fill_insert(__position, __n, __x); }
1340 * @brief Inserts a range into the %deque.
1341 * @param position An iterator into the %deque.
1342 * @param first An input iterator.
1343 * @param last An input iterator.
1345 * This function will insert copies of the data in the range
1346 * [first,last) into the %deque before the location specified
1347 * by @a pos. This is known as "range insert."
1349 template<typename _InputIterator>
1350 void
1351 insert(iterator __position, _InputIterator __first,
1352 _InputIterator __last)
1354 // Check whether it's an integral type. If so, it's not an iterator.
1355 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1356 _M_insert_dispatch(__position, __first, __last, _Integral());
1360 * @brief Remove element at given position.
1361 * @param position Iterator pointing to element to be erased.
1362 * @return An iterator pointing to the next element (or end()).
1364 * This function will erase the element at the given position and thus
1365 * shorten the %deque by one.
1367 * The user is cautioned that
1368 * this function only erases the element, and that if the element is
1369 * itself a pointer, the pointed-to memory is not touched in any way.
1370 * Managing the pointer is the user's responsibility.
1372 iterator
1373 erase(iterator __position);
1376 * @brief Remove a range of elements.
1377 * @param first Iterator pointing to the first element to be erased.
1378 * @param last Iterator pointing to one past the last element to be
1379 * erased.
1380 * @return An iterator pointing to the element pointed to by @a last
1381 * prior to erasing (or end()).
1383 * This function will erase the elements in the range [first,last) and
1384 * shorten the %deque accordingly.
1386 * The user is cautioned that
1387 * this function only erases the elements, and that if the elements
1388 * themselves are pointers, the pointed-to memory is not touched in any
1389 * way. Managing the pointer is the user's responsibility.
1391 iterator
1392 erase(iterator __first, iterator __last);
1395 * @brief Swaps data with another %deque.
1396 * @param x A %deque of the same element and allocator types.
1398 * This exchanges the elements between two deques in constant time.
1399 * (Four pointers, so it should be quite fast.)
1400 * Note that the global std::swap() function is specialized such that
1401 * std::swap(d1,d2) will feed to this function.
1403 void
1404 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1405 swap(deque&& __x)
1406 #else
1407 swap(deque& __x)
1408 #endif
1410 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1411 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1412 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1413 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1415 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1416 // 431. Swapping containers with unequal allocators.
1417 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1418 __x._M_get_Tp_allocator());
1422 * Erases all the elements. Note that this function only erases the
1423 * elements, and that if the elements themselves are pointers, the
1424 * pointed-to memory is not touched in any way. Managing the pointer is
1425 * the user's responsibility.
1427 void
1428 clear()
1429 { _M_erase_at_end(begin()); }
1431 protected:
1432 // Internal constructor functions follow.
1434 // called by the range constructor to implement [23.1.1]/9
1436 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1437 // 438. Ambiguity in the "do the right thing" clause
1438 template<typename _Integer>
1439 void
1440 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1442 _M_initialize_map(static_cast<size_type>(__n));
1443 _M_fill_initialize(__x);
1446 // called by the range constructor to implement [23.1.1]/9
1447 template<typename _InputIterator>
1448 void
1449 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1450 __false_type)
1452 typedef typename std::iterator_traits<_InputIterator>::
1453 iterator_category _IterCategory;
1454 _M_range_initialize(__first, __last, _IterCategory());
1457 // called by the second initialize_dispatch above
1458 //@{
1460 * @brief Fills the deque with whatever is in [first,last).
1461 * @param first An input iterator.
1462 * @param last An input iterator.
1463 * @return Nothing.
1465 * If the iterators are actually forward iterators (or better), then the
1466 * memory layout can be done all at once. Else we move forward using
1467 * push_back on each value from the iterator.
1469 template<typename _InputIterator>
1470 void
1471 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1472 std::input_iterator_tag);
1474 // called by the second initialize_dispatch above
1475 template<typename _ForwardIterator>
1476 void
1477 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1478 std::forward_iterator_tag);
1479 //@}
1482 * @brief Fills the %deque with copies of value.
1483 * @param value Initial value.
1484 * @return Nothing.
1485 * @pre _M_start and _M_finish have already been initialized,
1486 * but none of the %deque's elements have yet been constructed.
1488 * This function is called only when the user provides an explicit size
1489 * (with or without an explicit exemplar value).
1491 void
1492 _M_fill_initialize(const value_type& __value);
1494 // Internal assign functions follow. The *_aux functions do the actual
1495 // assignment work for the range versions.
1497 // called by the range assign to implement [23.1.1]/9
1499 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1500 // 438. Ambiguity in the "do the right thing" clause
1501 template<typename _Integer>
1502 void
1503 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1504 { _M_fill_assign(__n, __val); }
1506 // called by the range assign to implement [23.1.1]/9
1507 template<typename _InputIterator>
1508 void
1509 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1510 __false_type)
1512 typedef typename std::iterator_traits<_InputIterator>::
1513 iterator_category _IterCategory;
1514 _M_assign_aux(__first, __last, _IterCategory());
1517 // called by the second assign_dispatch above
1518 template<typename _InputIterator>
1519 void
1520 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1521 std::input_iterator_tag);
1523 // called by the second assign_dispatch above
1524 template<typename _ForwardIterator>
1525 void
1526 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1527 std::forward_iterator_tag)
1529 const size_type __len = std::distance(__first, __last);
1530 if (__len > size())
1532 _ForwardIterator __mid = __first;
1533 std::advance(__mid, size());
1534 std::copy(__first, __mid, begin());
1535 insert(end(), __mid, __last);
1537 else
1538 _M_erase_at_end(std::copy(__first, __last, begin()));
1541 // Called by assign(n,t), and the range assign when it turns out
1542 // to be the same thing.
1543 void
1544 _M_fill_assign(size_type __n, const value_type& __val)
1546 if (__n > size())
1548 std::fill(begin(), end(), __val);
1549 insert(end(), __n - size(), __val);
1551 else
1553 _M_erase_at_end(begin() + difference_type(__n));
1554 std::fill(begin(), end(), __val);
1558 //@{
1559 /// Helper functions for push_* and pop_*.
1560 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1561 void _M_push_back_aux(const value_type&);
1563 void _M_push_front_aux(const value_type&);
1564 #else
1565 template<typename... _Args>
1566 void _M_push_back_aux(_Args&&... __args);
1568 template<typename... _Args>
1569 void _M_push_front_aux(_Args&&... __args);
1570 #endif
1572 void _M_pop_back_aux();
1574 void _M_pop_front_aux();
1575 //@}
1577 // Internal insert functions follow. The *_aux functions do the actual
1578 // insertion work when all shortcuts fail.
1580 // called by the range insert to implement [23.1.1]/9
1582 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1583 // 438. Ambiguity in the "do the right thing" clause
1584 template<typename _Integer>
1585 void
1586 _M_insert_dispatch(iterator __pos,
1587 _Integer __n, _Integer __x, __true_type)
1588 { _M_fill_insert(__pos, __n, __x); }
1590 // called by the range insert to implement [23.1.1]/9
1591 template<typename _InputIterator>
1592 void
1593 _M_insert_dispatch(iterator __pos,
1594 _InputIterator __first, _InputIterator __last,
1595 __false_type)
1597 typedef typename std::iterator_traits<_InputIterator>::
1598 iterator_category _IterCategory;
1599 _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1602 // called by the second insert_dispatch above
1603 template<typename _InputIterator>
1604 void
1605 _M_range_insert_aux(iterator __pos, _InputIterator __first,
1606 _InputIterator __last, std::input_iterator_tag);
1608 // called by the second insert_dispatch above
1609 template<typename _ForwardIterator>
1610 void
1611 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1612 _ForwardIterator __last, std::forward_iterator_tag);
1614 // Called by insert(p,n,x), and the range insert when it turns out to be
1615 // the same thing. Can use fill functions in optimal situations,
1616 // otherwise passes off to insert_aux(p,n,x).
1617 void
1618 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1620 // called by insert(p,x)
1621 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1622 iterator
1623 _M_insert_aux(iterator __pos, const value_type& __x);
1624 #else
1625 template<typename... _Args>
1626 iterator
1627 _M_insert_aux(iterator __pos, _Args&&... __args);
1628 #endif
1630 // called by insert(p,n,x) via fill_insert
1631 void
1632 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1634 // called by range_insert_aux for forward iterators
1635 template<typename _ForwardIterator>
1636 void
1637 _M_insert_aux(iterator __pos,
1638 _ForwardIterator __first, _ForwardIterator __last,
1639 size_type __n);
1642 // Internal erase functions follow.
1644 void
1645 _M_destroy_data_aux(iterator __first, iterator __last);
1647 // Called by ~deque().
1648 // NB: Doesn't deallocate the nodes.
1649 template<typename _Alloc1>
1650 void
1651 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
1652 { _M_destroy_data_aux(__first, __last); }
1654 void
1655 _M_destroy_data(iterator __first, iterator __last,
1656 const std::allocator<_Tp>&)
1658 if (!__has_trivial_destructor(value_type))
1659 _M_destroy_data_aux(__first, __last);
1662 // Called by erase(q1, q2).
1663 void
1664 _M_erase_at_begin(iterator __pos)
1666 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
1667 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
1668 this->_M_impl._M_start = __pos;
1671 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1672 // _M_fill_assign, operator=.
1673 void
1674 _M_erase_at_end(iterator __pos)
1676 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
1677 _M_destroy_nodes(__pos._M_node + 1,
1678 this->_M_impl._M_finish._M_node + 1);
1679 this->_M_impl._M_finish = __pos;
1682 //@{
1683 /// Memory-handling helpers for the previous internal insert functions.
1684 iterator
1685 _M_reserve_elements_at_front(size_type __n)
1687 const size_type __vacancies = this->_M_impl._M_start._M_cur
1688 - this->_M_impl._M_start._M_first;
1689 if (__n > __vacancies)
1690 _M_new_elements_at_front(__n - __vacancies);
1691 return this->_M_impl._M_start - difference_type(__n);
1694 iterator
1695 _M_reserve_elements_at_back(size_type __n)
1697 const size_type __vacancies = (this->_M_impl._M_finish._M_last
1698 - this->_M_impl._M_finish._M_cur) - 1;
1699 if (__n > __vacancies)
1700 _M_new_elements_at_back(__n - __vacancies);
1701 return this->_M_impl._M_finish + difference_type(__n);
1704 void
1705 _M_new_elements_at_front(size_type __new_elements);
1707 void
1708 _M_new_elements_at_back(size_type __new_elements);
1709 //@}
1712 //@{
1714 * @brief Memory-handling helpers for the major %map.
1716 * Makes sure the _M_map has space for new nodes. Does not
1717 * actually add the nodes. Can invalidate _M_map pointers.
1718 * (And consequently, %deque iterators.)
1720 void
1721 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
1723 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1724 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1725 _M_reallocate_map(__nodes_to_add, false);
1728 void
1729 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
1731 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1732 - this->_M_impl._M_map))
1733 _M_reallocate_map(__nodes_to_add, true);
1736 void
1737 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1738 //@}
1743 * @brief Deque equality comparison.
1744 * @param x A %deque.
1745 * @param y A %deque of the same type as @a x.
1746 * @return True iff the size and elements of the deques are equal.
1748 * This is an equivalence relation. It is linear in the size of the
1749 * deques. Deques are considered equivalent if their sizes are equal,
1750 * and if corresponding elements compare equal.
1752 template<typename _Tp, typename _Alloc>
1753 inline bool
1754 operator==(const deque<_Tp, _Alloc>& __x,
1755 const deque<_Tp, _Alloc>& __y)
1756 { return __x.size() == __y.size()
1757 && std::equal(__x.begin(), __x.end(), __y.begin()); }
1760 * @brief Deque ordering relation.
1761 * @param x A %deque.
1762 * @param y A %deque of the same type as @a x.
1763 * @return True iff @a x is lexicographically less than @a y.
1765 * This is a total ordering relation. It is linear in the size of the
1766 * deques. The elements must be comparable with @c <.
1768 * See std::lexicographical_compare() for how the determination is made.
1770 template<typename _Tp, typename _Alloc>
1771 inline bool
1772 operator<(const deque<_Tp, _Alloc>& __x,
1773 const deque<_Tp, _Alloc>& __y)
1774 { return std::lexicographical_compare(__x.begin(), __x.end(),
1775 __y.begin(), __y.end()); }
1777 /// Based on operator==
1778 template<typename _Tp, typename _Alloc>
1779 inline bool
1780 operator!=(const deque<_Tp, _Alloc>& __x,
1781 const deque<_Tp, _Alloc>& __y)
1782 { return !(__x == __y); }
1784 /// Based on operator<
1785 template<typename _Tp, typename _Alloc>
1786 inline bool
1787 operator>(const deque<_Tp, _Alloc>& __x,
1788 const deque<_Tp, _Alloc>& __y)
1789 { return __y < __x; }
1791 /// Based on operator<
1792 template<typename _Tp, typename _Alloc>
1793 inline bool
1794 operator<=(const deque<_Tp, _Alloc>& __x,
1795 const deque<_Tp, _Alloc>& __y)
1796 { return !(__y < __x); }
1798 /// Based on operator<
1799 template<typename _Tp, typename _Alloc>
1800 inline bool
1801 operator>=(const deque<_Tp, _Alloc>& __x,
1802 const deque<_Tp, _Alloc>& __y)
1803 { return !(__x < __y); }
1805 /// See std::deque::swap().
1806 template<typename _Tp, typename _Alloc>
1807 inline void
1808 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1809 { __x.swap(__y); }
1811 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1812 template<typename _Tp, typename _Alloc>
1813 inline void
1814 swap(deque<_Tp,_Alloc>&& __x, deque<_Tp,_Alloc>& __y)
1815 { __x.swap(__y); }
1817 template<typename _Tp, typename _Alloc>
1818 inline void
1819 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>&& __y)
1820 { __x.swap(__y); }
1821 #endif
1823 _GLIBCXX_END_NESTED_NAMESPACE
1825 #endif /* _STL_DEQUE_H */