Merged with trunk at revision 155767
[official-gcc.git] / libstdc++-v3 / include / bits / stl_deque.h
blob7e60b658b65cf02c10beba215d7cc1477ee0b472
1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file stl_deque.h
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
57 #ifndef _STL_DEQUE_H
58 #define _STL_DEQUE_H 1
60 #include <bits/concept_check.h>
61 #include <bits/stl_iterator_base_types.h>
62 #include <bits/stl_iterator_base_funcs.h>
63 #include <initializer_list>
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
67 /**
68 * @brief This function controls the size of memory nodes.
69 * @param size The size of an element.
70 * @return The number (not byte size) of elements per node.
72 * This function started off as a compiler kludge from SGI, but seems to
73 * be a useful wrapper around a repeated constant expression. The '512' is
74 * tunable (and no other code needs to change), but no investigation has
75 * been done since inheriting the SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE
76 * only if you know what you are doing, however: changing it breaks the
77 * binary compatibility!!
80 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
81 #define _GLIBCXX_DEQUE_BUF_SIZE 512
82 #endif
84 inline size_t
85 __deque_buf_size(size_t __size)
86 { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
87 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
90 /**
91 * @brief A deque::iterator.
93 * Quite a bit of intelligence here. Much of the functionality of
94 * deque is actually passed off to this class. A deque holds two
95 * of these internally, marking its valid range. Access to
96 * elements is done as offsets of either of those two, relying on
97 * operator overloading in this class.
99 * All the functions are op overloads except for _M_set_node.
101 template<typename _Tp, typename _Ref, typename _Ptr>
102 struct _Deque_iterator
104 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
105 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
107 static size_t _S_buffer_size()
108 { return __deque_buf_size(sizeof(_Tp)); }
110 typedef std::random_access_iterator_tag iterator_category;
111 typedef _Tp value_type;
112 typedef _Ptr pointer;
113 typedef _Ref reference;
114 typedef size_t size_type;
115 typedef ptrdiff_t difference_type;
116 typedef _Tp** _Map_pointer;
117 typedef _Deque_iterator _Self;
119 _Tp* _M_cur;
120 _Tp* _M_first;
121 _Tp* _M_last;
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()
129 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
131 _Deque_iterator(const iterator& __x)
132 : _M_cur(__x._M_cur), _M_first(__x._M_first),
133 _M_last(__x._M_last), _M_node(__x._M_node) { }
135 reference
136 operator*() const
137 { return *_M_cur; }
139 pointer
140 operator->() const
141 { return _M_cur; }
143 _Self&
144 operator++()
146 ++_M_cur;
147 if (_M_cur == _M_last)
149 _M_set_node(_M_node + 1);
150 _M_cur = _M_first;
152 return *this;
155 _Self
156 operator++(int)
158 _Self __tmp = *this;
159 ++*this;
160 return __tmp;
163 _Self&
164 operator--()
166 if (_M_cur == _M_first)
168 _M_set_node(_M_node - 1);
169 _M_cur = _M_last;
171 --_M_cur;
172 return *this;
175 _Self
176 operator--(int)
178 _Self __tmp = *this;
179 --*this;
180 return __tmp;
183 _Self&
184 operator+=(difference_type __n)
186 const difference_type __offset = __n + (_M_cur - _M_first);
187 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
188 _M_cur += __n;
189 else
191 const difference_type __node_offset =
192 __offset > 0 ? __offset / difference_type(_S_buffer_size())
193 : -difference_type((-__offset - 1)
194 / _S_buffer_size()) - 1;
195 _M_set_node(_M_node + __node_offset);
196 _M_cur = _M_first + (__offset - __node_offset
197 * difference_type(_S_buffer_size()));
199 return *this;
202 _Self
203 operator+(difference_type __n) const
205 _Self __tmp = *this;
206 return __tmp += __n;
209 _Self&
210 operator-=(difference_type __n)
211 { return *this += -__n; }
213 _Self
214 operator-(difference_type __n) const
216 _Self __tmp = *this;
217 return __tmp -= __n;
220 reference
221 operator[](difference_type __n) const
222 { return *(*this + __n); }
224 /**
225 * Prepares to traverse new_node. Sets everything except
226 * _M_cur, which should therefore be set by the caller
227 * immediately afterwards, based on _M_first and _M_last.
229 void
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>
242 inline bool
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>
249 inline bool
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>
255 inline bool
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>
262 inline bool
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>
268 inline bool
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>
276 inline bool
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>
283 inline bool
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>
290 inline bool
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>
296 inline bool
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>
303 inline bool
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>
309 inline bool
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>
316 inline bool
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
324 // parameters.
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>
354 void
355 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&,
356 const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&);
358 template<typename _Tp>
359 _Deque_iterator<_Tp, _Tp&, _Tp*>
360 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
361 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
362 _Deque_iterator<_Tp, _Tp&, _Tp*>);
364 template<typename _Tp>
365 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
366 copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
367 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
368 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
369 { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
370 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
371 __result); }
373 template<typename _Tp>
374 _Deque_iterator<_Tp, _Tp&, _Tp*>
375 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
376 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
377 _Deque_iterator<_Tp, _Tp&, _Tp*>);
379 template<typename _Tp>
380 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
381 copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
382 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
383 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
384 { return std::copy_backward(_Deque_iterator<_Tp,
385 const _Tp&, const _Tp*>(__first),
386 _Deque_iterator<_Tp,
387 const _Tp&, const _Tp*>(__last),
388 __result); }
390 #ifdef __GXX_EXPERIMENTAL_CXX0X__
391 template<typename _Tp>
392 _Deque_iterator<_Tp, _Tp&, _Tp*>
393 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
394 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
395 _Deque_iterator<_Tp, _Tp&, _Tp*>);
397 template<typename _Tp>
398 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
399 move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
400 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
401 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
402 { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
403 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
404 __result); }
406 template<typename _Tp>
407 _Deque_iterator<_Tp, _Tp&, _Tp*>
408 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
409 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
410 _Deque_iterator<_Tp, _Tp&, _Tp*>);
412 template<typename _Tp>
413 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
414 move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
415 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
416 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
417 { return std::move_backward(_Deque_iterator<_Tp,
418 const _Tp&, const _Tp*>(__first),
419 _Deque_iterator<_Tp,
420 const _Tp&, const _Tp*>(__last),
421 __result); }
422 #endif
425 * Deque base class. This class provides the unified face for %deque's
426 * allocation. This class's constructor and destructor allocate and
427 * deallocate (but do not initialize) storage. This makes %exception
428 * safety easier.
430 * Nothing in this class ever constructs or destroys an actual Tp element.
431 * (Deque handles that itself.) Only/All memory management is performed
432 * here.
434 template<typename _Tp, typename _Alloc>
435 class _Deque_base
437 public:
438 typedef _Alloc allocator_type;
440 allocator_type
441 get_allocator() const
442 { return allocator_type(_M_get_Tp_allocator()); }
444 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
445 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
447 _Deque_base()
448 : _M_impl()
449 { _M_initialize_map(0); }
451 _Deque_base(const allocator_type& __a, size_t __num_elements)
452 : _M_impl(__a)
453 { _M_initialize_map(__num_elements); }
455 _Deque_base(const allocator_type& __a)
456 : _M_impl(__a)
459 #ifdef __GXX_EXPERIMENTAL_CXX0X__
460 _Deque_base(_Deque_base&& __x)
461 : _M_impl(__x._M_get_Tp_allocator())
463 _M_initialize_map(0);
464 if (__x._M_impl._M_map)
466 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
467 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
468 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
469 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
472 #endif
474 ~_Deque_base();
476 protected:
477 //This struct encapsulates the implementation of the std::deque
478 //standard container and at the same time makes use of the EBO
479 //for empty allocators.
480 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
482 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
484 struct _Deque_impl
485 : public _Tp_alloc_type
487 _Tp** _M_map;
488 size_t _M_map_size;
489 iterator _M_start;
490 iterator _M_finish;
492 _Deque_impl()
493 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
494 _M_start(), _M_finish()
497 _Deque_impl(const _Tp_alloc_type& __a)
498 : _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
499 _M_start(), _M_finish()
503 _Tp_alloc_type&
504 _M_get_Tp_allocator()
505 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
507 const _Tp_alloc_type&
508 _M_get_Tp_allocator() const
509 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
511 _Map_alloc_type
512 _M_get_map_allocator() const
513 { return _Map_alloc_type(_M_get_Tp_allocator()); }
515 _Tp*
516 _M_allocate_node()
518 return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
521 void
522 _M_deallocate_node(_Tp* __p)
524 _M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
527 _Tp**
528 _M_allocate_map(size_t __n)
529 { return _M_get_map_allocator().allocate(__n); }
531 void
532 _M_deallocate_map(_Tp** __p, size_t __n)
533 { _M_get_map_allocator().deallocate(__p, __n); }
535 protected:
536 void _M_initialize_map(size_t);
537 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
538 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
539 enum { _S_initial_map_size = 8 };
541 _Deque_impl _M_impl;
544 template<typename _Tp, typename _Alloc>
545 _Deque_base<_Tp, _Alloc>::
546 ~_Deque_base()
548 if (this->_M_impl._M_map)
550 _M_destroy_nodes(this->_M_impl._M_start._M_node,
551 this->_M_impl._M_finish._M_node + 1);
552 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
557 * @brief Layout storage.
558 * @param num_elements The count of T's for which to allocate space
559 * at first.
560 * @return Nothing.
562 * The initial underlying memory layout is a bit complicated...
564 template<typename _Tp, typename _Alloc>
565 void
566 _Deque_base<_Tp, _Alloc>::
567 _M_initialize_map(size_t __num_elements)
569 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
570 + 1);
572 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
573 size_t(__num_nodes + 2));
574 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
576 // For "small" maps (needing less than _M_map_size nodes), allocation
577 // starts in the middle elements and grows outwards. So nstart may be
578 // the beginning of _M_map, but for small maps it may be as far in as
579 // _M_map+3.
581 _Tp** __nstart = (this->_M_impl._M_map
582 + (this->_M_impl._M_map_size - __num_nodes) / 2);
583 _Tp** __nfinish = __nstart + __num_nodes;
585 __try
586 { _M_create_nodes(__nstart, __nfinish); }
587 __catch(...)
589 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
590 this->_M_impl._M_map = 0;
591 this->_M_impl._M_map_size = 0;
592 __throw_exception_again;
595 this->_M_impl._M_start._M_set_node(__nstart);
596 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
597 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
598 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
599 + __num_elements
600 % __deque_buf_size(sizeof(_Tp)));
603 template<typename _Tp, typename _Alloc>
604 void
605 _Deque_base<_Tp, _Alloc>::
606 _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
608 _Tp** __cur;
609 __try
611 for (__cur = __nstart; __cur < __nfinish; ++__cur)
612 *__cur = this->_M_allocate_node();
614 __catch(...)
616 _M_destroy_nodes(__nstart, __cur);
617 __throw_exception_again;
621 template<typename _Tp, typename _Alloc>
622 void
623 _Deque_base<_Tp, _Alloc>::
624 _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
626 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
627 _M_deallocate_node(*__n);
631 * @brief A standard container using fixed-size memory allocation and
632 * constant-time manipulation of elements at either end.
634 * @ingroup sequences
636 * Meets the requirements of a <a href="tables.html#65">container</a>, a
637 * <a href="tables.html#66">reversible container</a>, and a
638 * <a href="tables.html#67">sequence</a>, including the
639 * <a href="tables.html#68">optional sequence requirements</a>.
641 * In previous HP/SGI versions of deque, there was an extra template
642 * parameter so users could control the node size. This extension turned
643 * out to violate the C++ standard (it can be detected using template
644 * template parameters), and it was removed.
646 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
648 * - Tp** _M_map
649 * - size_t _M_map_size
650 * - iterator _M_start, _M_finish
652 * map_size is at least 8. %map is an array of map_size
653 * pointers-to-"nodes". (The name %map has nothing to do with the
654 * std::map class, and "nodes" should not be confused with
655 * std::list's usage of "node".)
657 * A "node" has no specific type name as such, but it is referred
658 * to as "node" in this file. It is a simple array-of-Tp. If Tp
659 * is very large, there will be one Tp element per node (i.e., an
660 * "array" of one). For non-huge Tp's, node size is inversely
661 * related to Tp size: the larger the Tp, the fewer Tp's will fit
662 * in a node. The goal here is to keep the total size of a node
663 * relatively small and constant over different Tp's, to improve
664 * allocator efficiency.
666 * Not every pointer in the %map array will point to a node. If
667 * the initial number of elements in the deque is small, the
668 * /middle/ %map pointers will be valid, and the ones at the edges
669 * will be unused. This same situation will arise as the %map
670 * grows: available %map pointers, if any, will be on the ends. As
671 * new nodes are created, only a subset of the %map's pointers need
672 * to be copied "outward".
674 * Class invariants:
675 * - For any nonsingular iterator i:
676 * - i.node points to a member of the %map array. (Yes, you read that
677 * correctly: i.node does not actually point to a node.) The member of
678 * the %map array is what actually points to the node.
679 * - i.first == *(i.node) (This points to the node (first Tp element).)
680 * - i.last == i.first + node_size
681 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
682 * the implication of this is that i.cur is always a dereferenceable
683 * pointer, even if i is a past-the-end iterator.
684 * - Start and Finish are always nonsingular iterators. NOTE: this
685 * means that an empty deque must have one node, a deque with <N
686 * elements (where N is the node buffer size) must have one node, a
687 * deque with N through (2N-1) elements must have two nodes, etc.
688 * - For every node other than start.node and finish.node, every
689 * element in the node is an initialized object. If start.node ==
690 * finish.node, then [start.cur, finish.cur) are initialized
691 * objects, and the elements outside that range are uninitialized
692 * storage. Otherwise, [start.cur, start.last) and [finish.first,
693 * finish.cur) are initialized objects, and [start.first, start.cur)
694 * and [finish.cur, finish.last) are uninitialized storage.
695 * - [%map, %map + map_size) is a valid, non-empty range.
696 * - [start.node, finish.node] is a valid range contained within
697 * [%map, %map + map_size).
698 * - A pointer in the range [%map, %map + map_size) points to an allocated
699 * node if and only if the pointer is in the range
700 * [start.node, finish.node].
702 * Here's the magic: nothing in deque is "aware" of the discontiguous
703 * storage!
705 * The memory setup and layout occurs in the parent, _Base, and the iterator
706 * class is entirely responsible for "leaping" from one node to the next.
707 * All the implementation routines for deque itself work only through the
708 * start and finish iterators. This keeps the routines simple and sane,
709 * and we can use other standard algorithms as well.
711 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
712 class deque : protected _Deque_base<_Tp, _Alloc>
714 // concept requirements
715 typedef typename _Alloc::value_type _Alloc_value_type;
716 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
717 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
719 typedef _Deque_base<_Tp, _Alloc> _Base;
720 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
722 public:
723 typedef _Tp value_type;
724 typedef typename _Tp_alloc_type::pointer pointer;
725 typedef typename _Tp_alloc_type::const_pointer const_pointer;
726 typedef typename _Tp_alloc_type::reference reference;
727 typedef typename _Tp_alloc_type::const_reference const_reference;
728 typedef typename _Base::iterator iterator;
729 typedef typename _Base::const_iterator const_iterator;
730 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
731 typedef std::reverse_iterator<iterator> reverse_iterator;
732 typedef size_t size_type;
733 typedef ptrdiff_t difference_type;
734 typedef _Alloc allocator_type;
736 protected:
737 typedef pointer* _Map_pointer;
739 static size_t _S_buffer_size()
740 { return __deque_buf_size(sizeof(_Tp)); }
742 // Functions controlling memory layout, and nothing else.
743 using _Base::_M_initialize_map;
744 using _Base::_M_create_nodes;
745 using _Base::_M_destroy_nodes;
746 using _Base::_M_allocate_node;
747 using _Base::_M_deallocate_node;
748 using _Base::_M_allocate_map;
749 using _Base::_M_deallocate_map;
750 using _Base::_M_get_Tp_allocator;
752 /**
753 * A total of four data members accumulated down the hierarchy.
754 * May be accessed via _M_impl.*
756 using _Base::_M_impl;
758 public:
759 // [23.2.1.1] construct/copy/destroy
760 // (assign() and get_allocator() are also listed in this section)
762 * @brief Default constructor creates no elements.
764 deque()
765 : _Base() { }
768 * @brief Creates a %deque with no elements.
769 * @param a An allocator object.
771 explicit
772 deque(const allocator_type& __a)
773 : _Base(__a, 0) { }
776 * @brief Creates a %deque with copies of an exemplar element.
777 * @param n The number of elements to initially create.
778 * @param value An element to copy.
779 * @param a An allocator.
781 * This constructor fills the %deque with @a n copies of @a value.
783 explicit
784 deque(size_type __n, const value_type& __value = value_type(),
785 const allocator_type& __a = allocator_type())
786 : _Base(__a, __n)
787 { _M_fill_initialize(__value); }
790 * @brief %Deque copy constructor.
791 * @param x A %deque of identical element and allocator types.
793 * The newly-created %deque uses a copy of the allocation object used
794 * by @a x.
796 deque(const deque& __x)
797 : _Base(__x._M_get_Tp_allocator(), __x.size())
798 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
799 this->_M_impl._M_start,
800 _M_get_Tp_allocator()); }
802 #ifdef __GXX_EXPERIMENTAL_CXX0X__
804 * @brief %Deque move constructor.
805 * @param x A %deque of identical element and allocator types.
807 * The newly-created %deque contains the exact contents of @a x.
808 * The contents of @a x are a valid, but unspecified %deque.
810 deque(deque&& __x)
811 : _Base(std::forward<_Base>(__x)) { }
814 * @brief Builds a %deque from an initializer list.
815 * @param l An initializer_list.
816 * @param a An allocator object.
818 * Create a %deque consisting of copies of the elements in the
819 * initializer_list @a l.
821 * This will call the element type's copy constructor N times
822 * (where N is l.size()) and do no memory reallocation.
824 deque(initializer_list<value_type> __l,
825 const allocator_type& __a = allocator_type())
826 : _Base(__a)
828 _M_range_initialize(__l.begin(), __l.end(),
829 random_access_iterator_tag());
831 #endif
834 * @brief Builds a %deque from a range.
835 * @param first An input iterator.
836 * @param last An input iterator.
837 * @param a An allocator object.
839 * Create a %deque consisting of copies of the elements from [first,
840 * last).
842 * If the iterators are forward, bidirectional, or random-access, then
843 * this will call the elements' copy constructor N times (where N is
844 * distance(first,last)) and do no memory reallocation. But if only
845 * input iterators are used, then this will do at most 2N calls to the
846 * copy constructor, and logN memory reallocations.
848 template<typename _InputIterator>
849 deque(_InputIterator __first, _InputIterator __last,
850 const allocator_type& __a = allocator_type())
851 : _Base(__a)
853 // Check whether it's an integral type. If so, it's not an iterator.
854 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
855 _M_initialize_dispatch(__first, __last, _Integral());
859 * The dtor only erases the elements, and note that if the elements
860 * themselves are pointers, the pointed-to memory is not touched in any
861 * way. Managing the pointer is the user's responsibility.
863 ~deque()
864 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
867 * @brief %Deque assignment operator.
868 * @param x A %deque of identical element and allocator types.
870 * All the elements of @a x are copied, but unlike the copy constructor,
871 * the allocator object is not copied.
873 deque&
874 operator=(const deque& __x);
876 #ifdef __GXX_EXPERIMENTAL_CXX0X__
878 * @brief %Deque move assignment operator.
879 * @param x A %deque of identical element and allocator types.
881 * The contents of @a x are moved into this deque (without copying).
882 * @a x is a valid, but unspecified %deque.
884 deque&
885 operator=(deque&& __x)
887 // NB: DR 1204.
888 // NB: DR 675.
889 this->clear();
890 this->swap(__x);
891 return *this;
895 * @brief Assigns an initializer list to a %deque.
896 * @param l An initializer_list.
898 * This function fills a %deque with copies of the elements in the
899 * initializer_list @a l.
901 * Note that the assignment completely changes the %deque and that the
902 * resulting %deque's size is the same as the number of elements
903 * assigned. Old data may be lost.
905 deque&
906 operator=(initializer_list<value_type> __l)
908 this->assign(__l.begin(), __l.end());
909 return *this;
911 #endif
914 * @brief Assigns a given value to a %deque.
915 * @param n Number of elements to be assigned.
916 * @param val Value to be assigned.
918 * This function fills a %deque with @a n copies of the given
919 * value. Note that the assignment completely changes the
920 * %deque and that the resulting %deque's size is the same as
921 * the number of elements assigned. Old data may be lost.
923 void
924 assign(size_type __n, const value_type& __val)
925 { _M_fill_assign(__n, __val); }
928 * @brief Assigns a range to a %deque.
929 * @param first An input iterator.
930 * @param last An input iterator.
932 * This function fills a %deque with copies of the elements in the
933 * range [first,last).
935 * Note that the assignment completely changes the %deque and that the
936 * resulting %deque's size is the same as the number of elements
937 * assigned. Old data may be lost.
939 template<typename _InputIterator>
940 void
941 assign(_InputIterator __first, _InputIterator __last)
943 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
944 _M_assign_dispatch(__first, __last, _Integral());
947 #ifdef __GXX_EXPERIMENTAL_CXX0X__
949 * @brief Assigns an initializer list to a %deque.
950 * @param l An initializer_list.
952 * This function fills a %deque with copies of the elements in the
953 * initializer_list @a l.
955 * Note that the assignment completely changes the %deque and that the
956 * resulting %deque's size is the same as the number of elements
957 * assigned. Old data may be lost.
959 void
960 assign(initializer_list<value_type> __l)
961 { this->assign(__l.begin(), __l.end()); }
962 #endif
964 /// Get a copy of the memory allocation object.
965 allocator_type
966 get_allocator() const
967 { return _Base::get_allocator(); }
969 // iterators
971 * Returns a read/write iterator that points to the first element in the
972 * %deque. Iteration is done in ordinary element order.
974 iterator
975 begin()
976 { return this->_M_impl._M_start; }
979 * Returns a read-only (constant) iterator that points to the first
980 * element in the %deque. Iteration is done in ordinary element order.
982 const_iterator
983 begin() const
984 { return this->_M_impl._M_start; }
987 * Returns a read/write iterator that points one past the last
988 * element in the %deque. Iteration is done in ordinary
989 * element order.
991 iterator
992 end()
993 { return this->_M_impl._M_finish; }
996 * Returns a read-only (constant) iterator that points one past
997 * the last element in the %deque. Iteration is done in
998 * ordinary element order.
1000 const_iterator
1001 end() const
1002 { return this->_M_impl._M_finish; }
1005 * Returns a read/write reverse iterator that points to the
1006 * last element in the %deque. Iteration is done in reverse
1007 * element order.
1009 reverse_iterator
1010 rbegin()
1011 { return reverse_iterator(this->_M_impl._M_finish); }
1014 * Returns a read-only (constant) reverse iterator that points
1015 * to the last element in the %deque. Iteration is done in
1016 * reverse element order.
1018 const_reverse_iterator
1019 rbegin() const
1020 { return const_reverse_iterator(this->_M_impl._M_finish); }
1023 * Returns a read/write reverse iterator that points to one
1024 * before the first element in the %deque. Iteration is done
1025 * in reverse element order.
1027 reverse_iterator
1028 rend()
1029 { return reverse_iterator(this->_M_impl._M_start); }
1032 * Returns a read-only (constant) reverse iterator that points
1033 * to one before the first element in the %deque. Iteration is
1034 * done in reverse element order.
1036 const_reverse_iterator
1037 rend() const
1038 { return const_reverse_iterator(this->_M_impl._M_start); }
1040 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1042 * Returns a read-only (constant) iterator that points to the first
1043 * element in the %deque. Iteration is done in ordinary element order.
1045 const_iterator
1046 cbegin() const
1047 { return this->_M_impl._M_start; }
1050 * Returns a read-only (constant) iterator that points one past
1051 * the last element in the %deque. Iteration is done in
1052 * ordinary element order.
1054 const_iterator
1055 cend() const
1056 { return this->_M_impl._M_finish; }
1059 * Returns a read-only (constant) reverse iterator that points
1060 * to the last element in the %deque. Iteration is done in
1061 * reverse element order.
1063 const_reverse_iterator
1064 crbegin() const
1065 { return const_reverse_iterator(this->_M_impl._M_finish); }
1068 * Returns a read-only (constant) reverse iterator that points
1069 * to one before the first element in the %deque. Iteration is
1070 * done in reverse element order.
1072 const_reverse_iterator
1073 crend() const
1074 { return const_reverse_iterator(this->_M_impl._M_start); }
1075 #endif
1077 // [23.2.1.2] capacity
1078 /** Returns the number of elements in the %deque. */
1079 size_type
1080 size() const
1081 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1083 /** Returns the size() of the largest possible %deque. */
1084 size_type
1085 max_size() const
1086 { return _M_get_Tp_allocator().max_size(); }
1089 * @brief Resizes the %deque to the specified number of elements.
1090 * @param new_size Number of elements the %deque should contain.
1091 * @param x Data with which new elements should be populated.
1093 * This function will %resize the %deque to the specified
1094 * number of elements. If the number is smaller than the
1095 * %deque's current size the %deque is truncated, otherwise the
1096 * %deque is extended and new elements are populated with given
1097 * data.
1099 void
1100 resize(size_type __new_size, value_type __x = value_type())
1102 const size_type __len = size();
1103 if (__new_size < __len)
1104 _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size));
1105 else
1106 insert(this->_M_impl._M_finish, __new_size - __len, __x);
1109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1110 /** A non-binding request to reduce memory use. */
1111 void
1112 shrink_to_fit()
1113 { std::__shrink_to_fit<deque>::_S_do_it(*this); }
1114 #endif
1117 * Returns true if the %deque is empty. (Thus begin() would
1118 * equal end().)
1120 bool
1121 empty() const
1122 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1124 // element access
1126 * @brief Subscript access to the data contained in the %deque.
1127 * @param n The index of the element for which data should be
1128 * accessed.
1129 * @return Read/write reference to data.
1131 * This operator allows for easy, array-style, data access.
1132 * Note that data access with this operator is unchecked and
1133 * out_of_range lookups are not defined. (For checked lookups
1134 * see at().)
1136 reference
1137 operator[](size_type __n)
1138 { return this->_M_impl._M_start[difference_type(__n)]; }
1141 * @brief Subscript access to the data contained in the %deque.
1142 * @param n The index of the element for which data should be
1143 * accessed.
1144 * @return Read-only (constant) reference to data.
1146 * This operator allows for easy, array-style, data access.
1147 * Note that data access with this operator is unchecked and
1148 * out_of_range lookups are not defined. (For checked lookups
1149 * see at().)
1151 const_reference
1152 operator[](size_type __n) const
1153 { return this->_M_impl._M_start[difference_type(__n)]; }
1155 protected:
1156 /// Safety check used only from at().
1157 void
1158 _M_range_check(size_type __n) const
1160 if (__n >= this->size())
1161 __throw_out_of_range(__N("deque::_M_range_check"));
1164 public:
1166 * @brief Provides access to the data contained in the %deque.
1167 * @param n The index of the element for which data should be
1168 * accessed.
1169 * @return Read/write reference to data.
1170 * @throw std::out_of_range If @a n is an invalid index.
1172 * This function provides for safer data access. The parameter
1173 * is first checked that it is in the range of the deque. The
1174 * function throws out_of_range if the check fails.
1176 reference
1177 at(size_type __n)
1179 _M_range_check(__n);
1180 return (*this)[__n];
1184 * @brief Provides access to the data contained in the %deque.
1185 * @param n The index of the element for which data should be
1186 * accessed.
1187 * @return Read-only (constant) reference to data.
1188 * @throw std::out_of_range If @a n is an invalid index.
1190 * This function provides for safer data access. The parameter is first
1191 * checked that it is in the range of the deque. The function throws
1192 * out_of_range if the check fails.
1194 const_reference
1195 at(size_type __n) const
1197 _M_range_check(__n);
1198 return (*this)[__n];
1202 * Returns a read/write reference to the data at the first
1203 * element of the %deque.
1205 reference
1206 front()
1207 { return *begin(); }
1210 * Returns a read-only (constant) reference to the data at the first
1211 * element of the %deque.
1213 const_reference
1214 front() const
1215 { return *begin(); }
1218 * Returns a read/write reference to the data at the last element of the
1219 * %deque.
1221 reference
1222 back()
1224 iterator __tmp = end();
1225 --__tmp;
1226 return *__tmp;
1230 * Returns a read-only (constant) reference to the data at the last
1231 * element of the %deque.
1233 const_reference
1234 back() const
1236 const_iterator __tmp = end();
1237 --__tmp;
1238 return *__tmp;
1241 // [23.2.1.2] modifiers
1243 * @brief Add data to the front of the %deque.
1244 * @param x Data to be added.
1246 * This is a typical stack operation. The function creates an
1247 * element at the front of the %deque and assigns the given
1248 * data to it. Due to the nature of a %deque this operation
1249 * can be done in constant time.
1251 void
1252 push_front(const value_type& __x)
1254 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1256 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1257 --this->_M_impl._M_start._M_cur;
1259 else
1260 _M_push_front_aux(__x);
1263 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1264 void
1265 push_front(value_type&& __x)
1266 { emplace_front(std::move(__x)); }
1268 template<typename... _Args>
1269 void
1270 emplace_front(_Args&&... __args);
1271 #endif
1274 * @brief Add data to the end of the %deque.
1275 * @param x Data to be added.
1277 * This is a typical stack operation. The function creates an
1278 * element at the end of the %deque and assigns the given data
1279 * to it. Due to the nature of a %deque this operation can be
1280 * done in constant time.
1282 void
1283 push_back(const value_type& __x)
1285 if (this->_M_impl._M_finish._M_cur
1286 != this->_M_impl._M_finish._M_last - 1)
1288 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1289 ++this->_M_impl._M_finish._M_cur;
1291 else
1292 _M_push_back_aux(__x);
1295 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1296 void
1297 push_back(value_type&& __x)
1298 { emplace_back(std::move(__x)); }
1300 template<typename... _Args>
1301 void
1302 emplace_back(_Args&&... __args);
1303 #endif
1306 * @brief Removes first element.
1308 * This is a typical stack operation. It shrinks the %deque by one.
1310 * Note that no data is returned, and if the first element's data is
1311 * needed, it should be retrieved before pop_front() is called.
1313 void
1314 pop_front()
1316 if (this->_M_impl._M_start._M_cur
1317 != this->_M_impl._M_start._M_last - 1)
1319 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1320 ++this->_M_impl._M_start._M_cur;
1322 else
1323 _M_pop_front_aux();
1327 * @brief Removes last element.
1329 * This is a typical stack operation. It shrinks the %deque by one.
1331 * Note that no data is returned, and if the last element's data is
1332 * needed, it should be retrieved before pop_back() is called.
1334 void
1335 pop_back()
1337 if (this->_M_impl._M_finish._M_cur
1338 != this->_M_impl._M_finish._M_first)
1340 --this->_M_impl._M_finish._M_cur;
1341 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1343 else
1344 _M_pop_back_aux();
1347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1349 * @brief Inserts an object in %deque before specified iterator.
1350 * @param position An iterator into the %deque.
1351 * @param args Arguments.
1352 * @return An iterator that points to the inserted data.
1354 * This function will insert an object of type T constructed
1355 * with T(std::forward<Args>(args)...) before the specified location.
1357 template<typename... _Args>
1358 iterator
1359 emplace(iterator __position, _Args&&... __args);
1360 #endif
1363 * @brief Inserts given value into %deque before specified iterator.
1364 * @param position An iterator into the %deque.
1365 * @param x Data to be inserted.
1366 * @return An iterator that points to the inserted data.
1368 * This function will insert a copy of the given value before the
1369 * specified location.
1371 iterator
1372 insert(iterator __position, const value_type& __x);
1374 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1376 * @brief Inserts given rvalue into %deque before specified iterator.
1377 * @param position An iterator into the %deque.
1378 * @param x Data to be inserted.
1379 * @return An iterator that points to the inserted data.
1381 * This function will insert a copy of the given rvalue before the
1382 * specified location.
1384 iterator
1385 insert(iterator __position, value_type&& __x)
1386 { return emplace(__position, std::move(__x)); }
1389 * @brief Inserts an initializer list into the %deque.
1390 * @param p An iterator into the %deque.
1391 * @param l An initializer_list.
1393 * This function will insert copies of the data in the
1394 * initializer_list @a l into the %deque before the location
1395 * specified by @a p. This is known as "list insert."
1397 void
1398 insert(iterator __p, initializer_list<value_type> __l)
1399 { this->insert(__p, __l.begin(), __l.end()); }
1400 #endif
1403 * @brief Inserts a number of copies of given data into the %deque.
1404 * @param position An iterator into the %deque.
1405 * @param n Number of elements to be inserted.
1406 * @param x Data to be inserted.
1408 * This function will insert a specified number of copies of the given
1409 * data before the location specified by @a position.
1411 void
1412 insert(iterator __position, size_type __n, const value_type& __x)
1413 { _M_fill_insert(__position, __n, __x); }
1416 * @brief Inserts a range into the %deque.
1417 * @param position An iterator into the %deque.
1418 * @param first An input iterator.
1419 * @param last An input iterator.
1421 * This function will insert copies of the data in the range
1422 * [first,last) into the %deque before the location specified
1423 * by @a pos. This is known as "range insert."
1425 template<typename _InputIterator>
1426 void
1427 insert(iterator __position, _InputIterator __first,
1428 _InputIterator __last)
1430 // Check whether it's an integral type. If so, it's not an iterator.
1431 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1432 _M_insert_dispatch(__position, __first, __last, _Integral());
1436 * @brief Remove element at given position.
1437 * @param position Iterator pointing to element to be erased.
1438 * @return An iterator pointing to the next element (or end()).
1440 * This function will erase the element at the given position and thus
1441 * shorten the %deque by one.
1443 * The user is cautioned that
1444 * this function only erases the element, and that if the element is
1445 * itself a pointer, the pointed-to memory is not touched in any way.
1446 * Managing the pointer is the user's responsibility.
1448 iterator
1449 erase(iterator __position);
1452 * @brief Remove a range of elements.
1453 * @param first Iterator pointing to the first element to be erased.
1454 * @param last Iterator pointing to one past the last element to be
1455 * erased.
1456 * @return An iterator pointing to the element pointed to by @a last
1457 * prior to erasing (or end()).
1459 * This function will erase the elements in the range [first,last) and
1460 * shorten the %deque accordingly.
1462 * The user is cautioned that
1463 * this function only erases the elements, and that if the elements
1464 * themselves are pointers, the pointed-to memory is not touched in any
1465 * way. Managing the pointer is the user's responsibility.
1467 iterator
1468 erase(iterator __first, iterator __last);
1471 * @brief Swaps data with another %deque.
1472 * @param x A %deque of the same element and allocator types.
1474 * This exchanges the elements between two deques in constant time.
1475 * (Four pointers, so it should be quite fast.)
1476 * Note that the global std::swap() function is specialized such that
1477 * std::swap(d1,d2) will feed to this function.
1479 void
1480 swap(deque& __x)
1482 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1483 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1484 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1485 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1487 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1488 // 431. Swapping containers with unequal allocators.
1489 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1490 __x._M_get_Tp_allocator());
1494 * Erases all the elements. Note that this function only erases the
1495 * elements, and that if the elements themselves are pointers, the
1496 * pointed-to memory is not touched in any way. Managing the pointer is
1497 * the user's responsibility.
1499 void
1500 clear()
1501 { _M_erase_at_end(begin()); }
1503 protected:
1504 // Internal constructor functions follow.
1506 // called by the range constructor to implement [23.1.1]/9
1508 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1509 // 438. Ambiguity in the "do the right thing" clause
1510 template<typename _Integer>
1511 void
1512 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1514 _M_initialize_map(static_cast<size_type>(__n));
1515 _M_fill_initialize(__x);
1518 // called by the range constructor to implement [23.1.1]/9
1519 template<typename _InputIterator>
1520 void
1521 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1522 __false_type)
1524 typedef typename std::iterator_traits<_InputIterator>::
1525 iterator_category _IterCategory;
1526 _M_range_initialize(__first, __last, _IterCategory());
1529 // called by the second initialize_dispatch above
1530 //@{
1532 * @brief Fills the deque with whatever is in [first,last).
1533 * @param first An input iterator.
1534 * @param last An input iterator.
1535 * @return Nothing.
1537 * If the iterators are actually forward iterators (or better), then the
1538 * memory layout can be done all at once. Else we move forward using
1539 * push_back on each value from the iterator.
1541 template<typename _InputIterator>
1542 void
1543 _M_range_initialize(_InputIterator __first, _InputIterator __last,
1544 std::input_iterator_tag);
1546 // called by the second initialize_dispatch above
1547 template<typename _ForwardIterator>
1548 void
1549 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1550 std::forward_iterator_tag);
1551 //@}
1554 * @brief Fills the %deque with copies of value.
1555 * @param value Initial value.
1556 * @return Nothing.
1557 * @pre _M_start and _M_finish have already been initialized,
1558 * but none of the %deque's elements have yet been constructed.
1560 * This function is called only when the user provides an explicit size
1561 * (with or without an explicit exemplar value).
1563 void
1564 _M_fill_initialize(const value_type& __value);
1566 // Internal assign functions follow. The *_aux functions do the actual
1567 // assignment work for the range versions.
1569 // called by the range assign to implement [23.1.1]/9
1571 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1572 // 438. Ambiguity in the "do the right thing" clause
1573 template<typename _Integer>
1574 void
1575 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1576 { _M_fill_assign(__n, __val); }
1578 // called by the range assign to implement [23.1.1]/9
1579 template<typename _InputIterator>
1580 void
1581 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1582 __false_type)
1584 typedef typename std::iterator_traits<_InputIterator>::
1585 iterator_category _IterCategory;
1586 _M_assign_aux(__first, __last, _IterCategory());
1589 // called by the second assign_dispatch above
1590 template<typename _InputIterator>
1591 void
1592 _M_assign_aux(_InputIterator __first, _InputIterator __last,
1593 std::input_iterator_tag);
1595 // called by the second assign_dispatch above
1596 template<typename _ForwardIterator>
1597 void
1598 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1599 std::forward_iterator_tag)
1601 const size_type __len = std::distance(__first, __last);
1602 if (__len > size())
1604 _ForwardIterator __mid = __first;
1605 std::advance(__mid, size());
1606 std::copy(__first, __mid, begin());
1607 insert(end(), __mid, __last);
1609 else
1610 _M_erase_at_end(std::copy(__first, __last, begin()));
1613 // Called by assign(n,t), and the range assign when it turns out
1614 // to be the same thing.
1615 void
1616 _M_fill_assign(size_type __n, const value_type& __val)
1618 if (__n > size())
1620 std::fill(begin(), end(), __val);
1621 insert(end(), __n - size(), __val);
1623 else
1625 _M_erase_at_end(begin() + difference_type(__n));
1626 std::fill(begin(), end(), __val);
1630 //@{
1631 /// Helper functions for push_* and pop_*.
1632 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1633 void _M_push_back_aux(const value_type&);
1635 void _M_push_front_aux(const value_type&);
1636 #else
1637 template<typename... _Args>
1638 void _M_push_back_aux(_Args&&... __args);
1640 template<typename... _Args>
1641 void _M_push_front_aux(_Args&&... __args);
1642 #endif
1644 void _M_pop_back_aux();
1646 void _M_pop_front_aux();
1647 //@}
1649 // Internal insert functions follow. The *_aux functions do the actual
1650 // insertion work when all shortcuts fail.
1652 // called by the range insert to implement [23.1.1]/9
1654 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1655 // 438. Ambiguity in the "do the right thing" clause
1656 template<typename _Integer>
1657 void
1658 _M_insert_dispatch(iterator __pos,
1659 _Integer __n, _Integer __x, __true_type)
1660 { _M_fill_insert(__pos, __n, __x); }
1662 // called by the range insert to implement [23.1.1]/9
1663 template<typename _InputIterator>
1664 void
1665 _M_insert_dispatch(iterator __pos,
1666 _InputIterator __first, _InputIterator __last,
1667 __false_type)
1669 typedef typename std::iterator_traits<_InputIterator>::
1670 iterator_category _IterCategory;
1671 _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1674 // called by the second insert_dispatch above
1675 template<typename _InputIterator>
1676 void
1677 _M_range_insert_aux(iterator __pos, _InputIterator __first,
1678 _InputIterator __last, std::input_iterator_tag);
1680 // called by the second insert_dispatch above
1681 template<typename _ForwardIterator>
1682 void
1683 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1684 _ForwardIterator __last, std::forward_iterator_tag);
1686 // Called by insert(p,n,x), and the range insert when it turns out to be
1687 // the same thing. Can use fill functions in optimal situations,
1688 // otherwise passes off to insert_aux(p,n,x).
1689 void
1690 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1692 // called by insert(p,x)
1693 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1694 iterator
1695 _M_insert_aux(iterator __pos, const value_type& __x);
1696 #else
1697 template<typename... _Args>
1698 iterator
1699 _M_insert_aux(iterator __pos, _Args&&... __args);
1700 #endif
1702 // called by insert(p,n,x) via fill_insert
1703 void
1704 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1706 // called by range_insert_aux for forward iterators
1707 template<typename _ForwardIterator>
1708 void
1709 _M_insert_aux(iterator __pos,
1710 _ForwardIterator __first, _ForwardIterator __last,
1711 size_type __n);
1714 // Internal erase functions follow.
1716 void
1717 _M_destroy_data_aux(iterator __first, iterator __last);
1719 // Called by ~deque().
1720 // NB: Doesn't deallocate the nodes.
1721 template<typename _Alloc1>
1722 void
1723 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
1724 { _M_destroy_data_aux(__first, __last); }
1726 void
1727 _M_destroy_data(iterator __first, iterator __last,
1728 const std::allocator<_Tp>&)
1730 if (!__has_trivial_destructor(value_type))
1731 _M_destroy_data_aux(__first, __last);
1734 // Called by erase(q1, q2).
1735 void
1736 _M_erase_at_begin(iterator __pos)
1738 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
1739 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
1740 this->_M_impl._M_start = __pos;
1743 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1744 // _M_fill_assign, operator=.
1745 void
1746 _M_erase_at_end(iterator __pos)
1748 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
1749 _M_destroy_nodes(__pos._M_node + 1,
1750 this->_M_impl._M_finish._M_node + 1);
1751 this->_M_impl._M_finish = __pos;
1754 //@{
1755 /// Memory-handling helpers for the previous internal insert functions.
1756 iterator
1757 _M_reserve_elements_at_front(size_type __n)
1759 const size_type __vacancies = this->_M_impl._M_start._M_cur
1760 - this->_M_impl._M_start._M_first;
1761 if (__n > __vacancies)
1762 _M_new_elements_at_front(__n - __vacancies);
1763 return this->_M_impl._M_start - difference_type(__n);
1766 iterator
1767 _M_reserve_elements_at_back(size_type __n)
1769 const size_type __vacancies = (this->_M_impl._M_finish._M_last
1770 - this->_M_impl._M_finish._M_cur) - 1;
1771 if (__n > __vacancies)
1772 _M_new_elements_at_back(__n - __vacancies);
1773 return this->_M_impl._M_finish + difference_type(__n);
1776 void
1777 _M_new_elements_at_front(size_type __new_elements);
1779 void
1780 _M_new_elements_at_back(size_type __new_elements);
1781 //@}
1784 //@{
1786 * @brief Memory-handling helpers for the major %map.
1788 * Makes sure the _M_map has space for new nodes. Does not
1789 * actually add the nodes. Can invalidate _M_map pointers.
1790 * (And consequently, %deque iterators.)
1792 void
1793 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
1795 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1796 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1797 _M_reallocate_map(__nodes_to_add, false);
1800 void
1801 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
1803 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1804 - this->_M_impl._M_map))
1805 _M_reallocate_map(__nodes_to_add, true);
1808 void
1809 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1810 //@}
1815 * @brief Deque equality comparison.
1816 * @param x A %deque.
1817 * @param y A %deque of the same type as @a x.
1818 * @return True iff the size and elements of the deques are equal.
1820 * This is an equivalence relation. It is linear in the size of the
1821 * deques. Deques are considered equivalent if their sizes are equal,
1822 * and if corresponding elements compare equal.
1824 template<typename _Tp, typename _Alloc>
1825 inline bool
1826 operator==(const deque<_Tp, _Alloc>& __x,
1827 const deque<_Tp, _Alloc>& __y)
1828 { return __x.size() == __y.size()
1829 && std::equal(__x.begin(), __x.end(), __y.begin()); }
1832 * @brief Deque ordering relation.
1833 * @param x A %deque.
1834 * @param y A %deque of the same type as @a x.
1835 * @return True iff @a x is lexicographically less than @a y.
1837 * This is a total ordering relation. It is linear in the size of the
1838 * deques. The elements must be comparable with @c <.
1840 * See std::lexicographical_compare() for how the determination is made.
1842 template<typename _Tp, typename _Alloc>
1843 inline bool
1844 operator<(const deque<_Tp, _Alloc>& __x,
1845 const deque<_Tp, _Alloc>& __y)
1846 { return std::lexicographical_compare(__x.begin(), __x.end(),
1847 __y.begin(), __y.end()); }
1849 /// Based on operator==
1850 template<typename _Tp, typename _Alloc>
1851 inline bool
1852 operator!=(const deque<_Tp, _Alloc>& __x,
1853 const deque<_Tp, _Alloc>& __y)
1854 { return !(__x == __y); }
1856 /// Based on operator<
1857 template<typename _Tp, typename _Alloc>
1858 inline bool
1859 operator>(const deque<_Tp, _Alloc>& __x,
1860 const deque<_Tp, _Alloc>& __y)
1861 { return __y < __x; }
1863 /// Based on operator<
1864 template<typename _Tp, typename _Alloc>
1865 inline bool
1866 operator<=(const deque<_Tp, _Alloc>& __x,
1867 const deque<_Tp, _Alloc>& __y)
1868 { return !(__y < __x); }
1870 /// Based on operator<
1871 template<typename _Tp, typename _Alloc>
1872 inline bool
1873 operator>=(const deque<_Tp, _Alloc>& __x,
1874 const deque<_Tp, _Alloc>& __y)
1875 { return !(__x < __y); }
1877 /// See std::deque::swap().
1878 template<typename _Tp, typename _Alloc>
1879 inline void
1880 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1881 { __x.swap(__y); }
1883 #undef _GLIBCXX_DEQUE_BUF_SIZE
1885 _GLIBCXX_END_NESTED_NAMESPACE
1887 #endif /* _STL_DEQUE_H */