1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
58 #define _STL_DEQUE_H 1
60 #include <bits/concept_check.h>
61 #include <bits/stl_iterator_base_types.h>
62 #include <bits/stl_iterator_base_funcs.h>
63 #include <initializer_list>
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
68 * @brief This function controls the size of memory nodes.
69 * @param size The size of an element.
70 * @return The number (not byte size) of elements per node.
72 * This function started off as a compiler kludge from SGI, but
73 * seems to be a useful wrapper around a repeated constant
74 * expression. The @b 512 is tunable (and no other code needs to
75 * change), but no investigation has been done since inheriting the
76 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
77 * you are doing, however: changing it breaks the binary
81 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
82 #define _GLIBCXX_DEQUE_BUF_SIZE 512
86 __deque_buf_size(size_t __size
)
87 { return (__size
< _GLIBCXX_DEQUE_BUF_SIZE
88 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE
/ __size
) : size_t(1)); }
92 * @brief A deque::iterator.
94 * Quite a bit of intelligence here. Much of the functionality of
95 * deque is actually passed off to this class. A deque holds two
96 * of these internally, marking its valid range. Access to
97 * elements is done as offsets of either of those two, relying on
98 * operator overloading in this class.
100 * All the functions are op overloads except for _M_set_node.
102 template<typename _Tp
, typename _Ref
, typename _Ptr
>
103 struct _Deque_iterator
105 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
106 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
108 static size_t _S_buffer_size()
109 { return __deque_buf_size(sizeof(_Tp
)); }
111 typedef std::random_access_iterator_tag iterator_category
;
112 typedef _Tp value_type
;
113 typedef _Ptr pointer
;
114 typedef _Ref reference
;
115 typedef size_t size_type
;
116 typedef ptrdiff_t difference_type
;
117 typedef _Tp
** _Map_pointer
;
118 typedef _Deque_iterator _Self
;
123 _Map_pointer _M_node
;
125 _Deque_iterator(_Tp
* __x
, _Map_pointer __y
)
126 : _M_cur(__x
), _M_first(*__y
),
127 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
130 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
132 _Deque_iterator(const iterator
& __x
)
133 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
134 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
148 if (_M_cur
== _M_last
)
150 _M_set_node(_M_node
+ 1);
167 if (_M_cur
== _M_first
)
169 _M_set_node(_M_node
- 1);
185 operator+=(difference_type __n
)
187 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
188 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
192 const difference_type __node_offset
=
193 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
194 : -difference_type((-__offset
- 1)
195 / _S_buffer_size()) - 1;
196 _M_set_node(_M_node
+ __node_offset
);
197 _M_cur
= _M_first
+ (__offset
- __node_offset
198 * difference_type(_S_buffer_size()));
204 operator+(difference_type __n
) const
211 operator-=(difference_type __n
)
212 { return *this += -__n
; }
215 operator-(difference_type __n
) const
222 operator[](difference_type __n
) const
223 { return *(*this + __n
); }
226 * Prepares to traverse new_node. Sets everything except
227 * _M_cur, which should therefore be set by the caller
228 * immediately afterwards, based on _M_first and _M_last.
231 _M_set_node(_Map_pointer __new_node
)
233 _M_node
= __new_node
;
234 _M_first
= *__new_node
;
235 _M_last
= _M_first
+ difference_type(_S_buffer_size());
239 // Note: we also provide overloads whose operands are of the same type in
240 // order to avoid ambiguous overload resolution when std::rel_ops operators
241 // are in scope (for additional details, see libstdc++/3628)
242 template<typename _Tp
, typename _Ref
, typename _Ptr
>
244 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
245 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
246 { return __x
._M_cur
== __y
._M_cur
; }
248 template<typename _Tp
, typename _RefL
, typename _PtrL
,
249 typename _RefR
, typename _PtrR
>
251 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
252 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
253 { return __x
._M_cur
== __y
._M_cur
; }
255 template<typename _Tp
, typename _Ref
, typename _Ptr
>
257 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
258 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
259 { return !(__x
== __y
); }
261 template<typename _Tp
, typename _RefL
, typename _PtrL
,
262 typename _RefR
, typename _PtrR
>
264 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
265 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
266 { return !(__x
== __y
); }
268 template<typename _Tp
, typename _Ref
, typename _Ptr
>
270 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
271 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
272 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
273 : (__x
._M_node
< __y
._M_node
); }
275 template<typename _Tp
, typename _RefL
, typename _PtrL
,
276 typename _RefR
, typename _PtrR
>
278 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
279 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
280 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
281 : (__x
._M_node
< __y
._M_node
); }
283 template<typename _Tp
, typename _Ref
, typename _Ptr
>
285 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
286 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
287 { return __y
< __x
; }
289 template<typename _Tp
, typename _RefL
, typename _PtrL
,
290 typename _RefR
, typename _PtrR
>
292 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
293 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
294 { return __y
< __x
; }
296 template<typename _Tp
, typename _Ref
, typename _Ptr
>
298 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
299 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
300 { return !(__y
< __x
); }
302 template<typename _Tp
, typename _RefL
, typename _PtrL
,
303 typename _RefR
, typename _PtrR
>
305 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
306 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
307 { return !(__y
< __x
); }
309 template<typename _Tp
, typename _Ref
, typename _Ptr
>
311 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
312 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
313 { return !(__x
< __y
); }
315 template<typename _Tp
, typename _RefL
, typename _PtrL
,
316 typename _RefR
, typename _PtrR
>
318 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
319 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
320 { return !(__x
< __y
); }
322 // _GLIBCXX_RESOLVE_LIB_DEFECTS
323 // According to the resolution of DR179 not only the various comparison
324 // operators but also operator- must accept mixed iterator/const_iterator
326 template<typename _Tp
, typename _Ref
, typename _Ptr
>
327 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
328 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
329 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
331 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
332 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
333 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
334 + (__y
._M_last
- __y
._M_cur
);
337 template<typename _Tp
, typename _RefL
, typename _PtrL
,
338 typename _RefR
, typename _PtrR
>
339 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
340 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
341 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
343 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
344 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
345 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
346 + (__y
._M_last
- __y
._M_cur
);
349 template<typename _Tp
, typename _Ref
, typename _Ptr
>
350 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
351 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
352 { return __x
+ __n
; }
354 template<typename _Tp
>
356 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
357 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
359 template<typename _Tp
>
360 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
361 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
362 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
363 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
365 template<typename _Tp
>
366 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
367 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
368 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
369 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
370 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
371 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
374 template<typename _Tp
>
375 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
376 copy_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
377 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
378 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
380 template<typename _Tp
>
381 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
382 copy_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
383 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
384 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
385 { return std::copy_backward(_Deque_iterator
<_Tp
,
386 const _Tp
&, const _Tp
*>(__first
),
388 const _Tp
&, const _Tp
*>(__last
),
391 #ifdef __GXX_EXPERIMENTAL_CXX0X__
392 template<typename _Tp
>
393 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
394 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
395 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
396 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
398 template<typename _Tp
>
399 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
400 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
401 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
402 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
403 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
404 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
407 template<typename _Tp
>
408 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
409 move_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
410 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
411 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
413 template<typename _Tp
>
414 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
415 move_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
416 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
417 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
418 { return std::move_backward(_Deque_iterator
<_Tp
,
419 const _Tp
&, const _Tp
*>(__first
),
421 const _Tp
&, const _Tp
*>(__last
),
426 * Deque base class. This class provides the unified face for %deque's
427 * allocation. This class's constructor and destructor allocate and
428 * deallocate (but do not initialize) storage. This makes %exception
431 * Nothing in this class ever constructs or destroys an actual Tp element.
432 * (Deque handles that itself.) Only/All memory management is performed
435 template<typename _Tp
, typename _Alloc
>
439 typedef _Alloc allocator_type
;
442 get_allocator() const
443 { return allocator_type(_M_get_Tp_allocator()); }
445 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
446 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
450 { _M_initialize_map(0); }
452 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
454 { _M_initialize_map(__num_elements
); }
456 _Deque_base(const allocator_type
& __a
)
460 #ifdef __GXX_EXPERIMENTAL_CXX0X__
461 _Deque_base(_Deque_base
&& __x
)
462 : _M_impl(__x
._M_get_Tp_allocator())
464 _M_initialize_map(0);
465 if (__x
._M_impl
._M_map
)
467 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
468 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
469 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
470 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
478 //This struct encapsulates the implementation of the std::deque
479 //standard container and at the same time makes use of the EBO
480 //for empty allocators.
481 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
483 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
486 : public _Tp_alloc_type
494 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
495 _M_start(), _M_finish()
498 _Deque_impl(const _Tp_alloc_type
& __a
)
499 : _Tp_alloc_type(__a
), _M_map(0), _M_map_size(0),
500 _M_start(), _M_finish()
505 _M_get_Tp_allocator()
506 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
508 const _Tp_alloc_type
&
509 _M_get_Tp_allocator() const
510 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
513 _M_get_map_allocator() const
514 { return _Map_alloc_type(_M_get_Tp_allocator()); }
519 return _M_impl
._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp
)));
523 _M_deallocate_node(_Tp
* __p
)
525 _M_impl
._Tp_alloc_type::deallocate(__p
, __deque_buf_size(sizeof(_Tp
)));
529 _M_allocate_map(size_t __n
)
530 { return _M_get_map_allocator().allocate(__n
); }
533 _M_deallocate_map(_Tp
** __p
, size_t __n
)
534 { _M_get_map_allocator().deallocate(__p
, __n
); }
537 void _M_initialize_map(size_t);
538 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
539 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
540 enum { _S_initial_map_size
= 8 };
545 template<typename _Tp
, typename _Alloc
>
546 _Deque_base
<_Tp
, _Alloc
>::
549 if (this->_M_impl
._M_map
)
551 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
552 this->_M_impl
._M_finish
._M_node
+ 1);
553 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
558 * @brief Layout storage.
559 * @param num_elements The count of T's for which to allocate space
563 * The initial underlying memory layout is a bit complicated...
565 template<typename _Tp
, typename _Alloc
>
567 _Deque_base
<_Tp
, _Alloc
>::
568 _M_initialize_map(size_t __num_elements
)
570 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
573 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
574 size_t(__num_nodes
+ 2));
575 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
577 // For "small" maps (needing less than _M_map_size nodes), allocation
578 // starts in the middle elements and grows outwards. So nstart may be
579 // the beginning of _M_map, but for small maps it may be as far in as
582 _Tp
** __nstart
= (this->_M_impl
._M_map
583 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
584 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
587 { _M_create_nodes(__nstart
, __nfinish
); }
590 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
591 this->_M_impl
._M_map
= 0;
592 this->_M_impl
._M_map_size
= 0;
593 __throw_exception_again
;
596 this->_M_impl
._M_start
._M_set_node(__nstart
);
597 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
598 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
599 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
601 % __deque_buf_size(sizeof(_Tp
)));
604 template<typename _Tp
, typename _Alloc
>
606 _Deque_base
<_Tp
, _Alloc
>::
607 _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
612 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
613 *__cur
= this->_M_allocate_node();
617 _M_destroy_nodes(__nstart
, __cur
);
618 __throw_exception_again
;
622 template<typename _Tp
, typename _Alloc
>
624 _Deque_base
<_Tp
, _Alloc
>::
625 _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
627 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
628 _M_deallocate_node(*__n
);
632 * @brief A standard container using fixed-size memory allocation and
633 * constant-time manipulation of elements at either end.
637 * Meets the requirements of a <a href="tables.html#65">container</a>, a
638 * <a href="tables.html#66">reversible container</a>, and a
639 * <a href="tables.html#67">sequence</a>, including the
640 * <a href="tables.html#68">optional sequence requirements</a>.
642 * In previous HP/SGI versions of deque, there was an extra template
643 * parameter so users could control the node size. This extension turned
644 * out to violate the C++ standard (it can be detected using template
645 * template parameters), and it was removed.
647 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
650 * - size_t _M_map_size
651 * - iterator _M_start, _M_finish
653 * map_size is at least 8. %map is an array of map_size
654 * pointers-to-@anodes. (The name %map has nothing to do with the
655 * std::map class, and @b nodes should not be confused with
656 * std::list's usage of @a node.)
658 * A @a node has no specific type name as such, but it is referred
659 * to as @a node in this file. It is a simple array-of-Tp. If Tp
660 * is very large, there will be one Tp element per node (i.e., an
661 * @a array of one). For non-huge Tp's, node size is inversely
662 * related to Tp size: the larger the Tp, the fewer Tp's will fit
663 * in a node. The goal here is to keep the total size of a node
664 * relatively small and constant over different Tp's, to improve
665 * allocator efficiency.
667 * Not every pointer in the %map array will point to a node. If
668 * the initial number of elements in the deque is small, the
669 * /middle/ %map pointers will be valid, and the ones at the edges
670 * will be unused. This same situation will arise as the %map
671 * grows: available %map pointers, if any, will be on the ends. As
672 * new nodes are created, only a subset of the %map's pointers need
673 * to be copied @a outward.
676 * - For any nonsingular iterator i:
677 * - i.node points to a member of the %map array. (Yes, you read that
678 * correctly: i.node does not actually point to a node.) The member of
679 * the %map array is what actually points to the node.
680 * - i.first == *(i.node) (This points to the node (first Tp element).)
681 * - i.last == i.first + node_size
682 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
683 * the implication of this is that i.cur is always a dereferenceable
684 * pointer, even if i is a past-the-end iterator.
685 * - Start and Finish are always nonsingular iterators. NOTE: this
686 * means that an empty deque must have one node, a deque with <N
687 * elements (where N is the node buffer size) must have one node, a
688 * deque with N through (2N-1) elements must have two nodes, etc.
689 * - For every node other than start.node and finish.node, every
690 * element in the node is an initialized object. If start.node ==
691 * finish.node, then [start.cur, finish.cur) are initialized
692 * objects, and the elements outside that range are uninitialized
693 * storage. Otherwise, [start.cur, start.last) and [finish.first,
694 * finish.cur) are initialized objects, and [start.first, start.cur)
695 * and [finish.cur, finish.last) are uninitialized storage.
696 * - [%map, %map + map_size) is a valid, non-empty range.
697 * - [start.node, finish.node] is a valid range contained within
698 * [%map, %map + map_size).
699 * - A pointer in the range [%map, %map + map_size) points to an allocated
700 * node if and only if the pointer is in the range
701 * [start.node, finish.node].
703 * Here's the magic: nothing in deque is @b aware of the discontiguous
706 * The memory setup and layout occurs in the parent, _Base, and the iterator
707 * class is entirely responsible for @a leaping from one node to the next.
708 * All the implementation routines for deque itself work only through the
709 * start and finish iterators. This keeps the routines simple and sane,
710 * and we can use other standard algorithms as well.
712 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
713 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
715 // concept requirements
716 typedef typename
_Alloc::value_type _Alloc_value_type
;
717 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
718 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
720 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
721 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
724 typedef _Tp value_type
;
725 typedef typename
_Tp_alloc_type::pointer pointer
;
726 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
727 typedef typename
_Tp_alloc_type::reference reference
;
728 typedef typename
_Tp_alloc_type::const_reference const_reference
;
729 typedef typename
_Base::iterator iterator
;
730 typedef typename
_Base::const_iterator const_iterator
;
731 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
732 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
733 typedef size_t size_type
;
734 typedef ptrdiff_t difference_type
;
735 typedef _Alloc allocator_type
;
738 typedef pointer
* _Map_pointer
;
740 static size_t _S_buffer_size()
741 { return __deque_buf_size(sizeof(_Tp
)); }
743 // Functions controlling memory layout, and nothing else.
744 using _Base::_M_initialize_map
;
745 using _Base::_M_create_nodes
;
746 using _Base::_M_destroy_nodes
;
747 using _Base::_M_allocate_node
;
748 using _Base::_M_deallocate_node
;
749 using _Base::_M_allocate_map
;
750 using _Base::_M_deallocate_map
;
751 using _Base::_M_get_Tp_allocator
;
754 * A total of four data members accumulated down the hierarchy.
755 * May be accessed via _M_impl.*
757 using _Base::_M_impl
;
760 // [23.2.1.1] construct/copy/destroy
761 // (assign() and get_allocator() are also listed in this section)
763 * @brief Default constructor creates no elements.
769 * @brief Creates a %deque with no elements.
770 * @param a An allocator object.
773 deque(const allocator_type
& __a
)
777 * @brief Creates a %deque with copies of an exemplar element.
778 * @param n The number of elements to initially create.
779 * @param value An element to copy.
780 * @param a An allocator.
782 * This constructor fills the %deque with @a n copies of @a value.
785 deque(size_type __n
, const value_type
& __value
= value_type(),
786 const allocator_type
& __a
= allocator_type())
788 { _M_fill_initialize(__value
); }
791 * @brief %Deque copy constructor.
792 * @param x A %deque of identical element and allocator types.
794 * The newly-created %deque uses a copy of the allocation object used
797 deque(const deque
& __x
)
798 : _Base(__x
._M_get_Tp_allocator(), __x
.size())
799 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
800 this->_M_impl
._M_start
,
801 _M_get_Tp_allocator()); }
803 #ifdef __GXX_EXPERIMENTAL_CXX0X__
805 * @brief %Deque move constructor.
806 * @param x A %deque of identical element and allocator types.
808 * The newly-created %deque contains the exact contents of @a x.
809 * The contents of @a x are a valid, but unspecified %deque.
812 : _Base(std::forward
<_Base
>(__x
)) { }
815 * @brief Builds a %deque from an initializer list.
816 * @param l An initializer_list.
817 * @param a An allocator object.
819 * Create a %deque consisting of copies of the elements in the
820 * initializer_list @a l.
822 * This will call the element type's copy constructor N times
823 * (where N is l.size()) and do no memory reallocation.
825 deque(initializer_list
<value_type
> __l
,
826 const allocator_type
& __a
= allocator_type())
829 _M_range_initialize(__l
.begin(), __l
.end(),
830 random_access_iterator_tag());
835 * @brief Builds a %deque from a range.
836 * @param first An input iterator.
837 * @param last An input iterator.
838 * @param a An allocator object.
840 * Create a %deque consisting of copies of the elements from [first,
843 * If the iterators are forward, bidirectional, or random-access, then
844 * this will call the elements' copy constructor N times (where N is
845 * distance(first,last)) and do no memory reallocation. But if only
846 * input iterators are used, then this will do at most 2N calls to the
847 * copy constructor, and logN memory reallocations.
849 template<typename _InputIterator
>
850 deque(_InputIterator __first
, _InputIterator __last
,
851 const allocator_type
& __a
= allocator_type())
854 // Check whether it's an integral type. If so, it's not an iterator.
855 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
856 _M_initialize_dispatch(__first
, __last
, _Integral());
860 * The dtor only erases the elements, and note that if the elements
861 * themselves are pointers, the pointed-to memory is not touched in any
862 * way. Managing the pointer is the user's responsibility.
865 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
868 * @brief %Deque assignment operator.
869 * @param x A %deque of identical element and allocator types.
871 * All the elements of @a x are copied, but unlike the copy constructor,
872 * the allocator object is not copied.
875 operator=(const deque
& __x
);
877 #ifdef __GXX_EXPERIMENTAL_CXX0X__
879 * @brief %Deque move assignment operator.
880 * @param x A %deque of identical element and allocator types.
882 * The contents of @a x are moved into this deque (without copying).
883 * @a x is a valid, but unspecified %deque.
886 operator=(deque
&& __x
)
896 * @brief Assigns an initializer list to a %deque.
897 * @param l An initializer_list.
899 * This function fills a %deque with copies of the elements in the
900 * initializer_list @a l.
902 * Note that the assignment completely changes the %deque and that the
903 * resulting %deque's size is the same as the number of elements
904 * assigned. Old data may be lost.
907 operator=(initializer_list
<value_type
> __l
)
909 this->assign(__l
.begin(), __l
.end());
915 * @brief Assigns a given value to a %deque.
916 * @param n Number of elements to be assigned.
917 * @param val Value to be assigned.
919 * This function fills a %deque with @a n copies of the given
920 * value. Note that the assignment completely changes the
921 * %deque and that the resulting %deque's size is the same as
922 * the number of elements assigned. Old data may be lost.
925 assign(size_type __n
, const value_type
& __val
)
926 { _M_fill_assign(__n
, __val
); }
929 * @brief Assigns a range to a %deque.
930 * @param first An input iterator.
931 * @param last An input iterator.
933 * This function fills a %deque with copies of the elements in the
934 * range [first,last).
936 * Note that the assignment completely changes the %deque and that the
937 * resulting %deque's size is the same as the number of elements
938 * assigned. Old data may be lost.
940 template<typename _InputIterator
>
942 assign(_InputIterator __first
, _InputIterator __last
)
944 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
945 _M_assign_dispatch(__first
, __last
, _Integral());
948 #ifdef __GXX_EXPERIMENTAL_CXX0X__
950 * @brief Assigns an initializer list to a %deque.
951 * @param l An initializer_list.
953 * This function fills a %deque with copies of the elements in the
954 * initializer_list @a l.
956 * Note that the assignment completely changes the %deque and that the
957 * resulting %deque's size is the same as the number of elements
958 * assigned. Old data may be lost.
961 assign(initializer_list
<value_type
> __l
)
962 { this->assign(__l
.begin(), __l
.end()); }
965 /// Get a copy of the memory allocation object.
967 get_allocator() const
968 { return _Base::get_allocator(); }
972 * Returns a read/write iterator that points to the first element in the
973 * %deque. Iteration is done in ordinary element order.
977 { return this->_M_impl
._M_start
; }
980 * Returns a read-only (constant) iterator that points to the first
981 * element in the %deque. Iteration is done in ordinary element order.
985 { return this->_M_impl
._M_start
; }
988 * Returns a read/write iterator that points one past the last
989 * element in the %deque. Iteration is done in ordinary
994 { return this->_M_impl
._M_finish
; }
997 * Returns a read-only (constant) iterator that points one past
998 * the last element in the %deque. Iteration is done in
999 * ordinary element order.
1003 { return this->_M_impl
._M_finish
; }
1006 * Returns a read/write reverse iterator that points to the
1007 * last element in the %deque. Iteration is done in reverse
1012 { return reverse_iterator(this->_M_impl
._M_finish
); }
1015 * Returns a read-only (constant) reverse iterator that points
1016 * to the last element in the %deque. Iteration is done in
1017 * reverse element order.
1019 const_reverse_iterator
1021 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1024 * Returns a read/write reverse iterator that points to one
1025 * before the first element in the %deque. Iteration is done
1026 * in reverse element order.
1030 { return reverse_iterator(this->_M_impl
._M_start
); }
1033 * Returns a read-only (constant) reverse iterator that points
1034 * to one before the first element in the %deque. Iteration is
1035 * done in reverse element order.
1037 const_reverse_iterator
1039 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1041 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1043 * Returns a read-only (constant) iterator that points to the first
1044 * element in the %deque. Iteration is done in ordinary element order.
1048 { return this->_M_impl
._M_start
; }
1051 * Returns a read-only (constant) iterator that points one past
1052 * the last element in the %deque. Iteration is done in
1053 * ordinary element order.
1057 { return this->_M_impl
._M_finish
; }
1060 * Returns a read-only (constant) reverse iterator that points
1061 * to the last element in the %deque. Iteration is done in
1062 * reverse element order.
1064 const_reverse_iterator
1066 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1069 * Returns a read-only (constant) reverse iterator that points
1070 * to one before the first element in the %deque. Iteration is
1071 * done in reverse element order.
1073 const_reverse_iterator
1075 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1078 // [23.2.1.2] capacity
1079 /** Returns the number of elements in the %deque. */
1082 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1084 /** Returns the size() of the largest possible %deque. */
1087 { return _M_get_Tp_allocator().max_size(); }
1090 * @brief Resizes the %deque to the specified number of elements.
1091 * @param new_size Number of elements the %deque should contain.
1092 * @param x Data with which new elements should be populated.
1094 * This function will %resize the %deque to the specified
1095 * number of elements. If the number is smaller than the
1096 * %deque's current size the %deque is truncated, otherwise the
1097 * %deque is extended and new elements are populated with given
1101 resize(size_type __new_size
, value_type __x
= value_type())
1103 const size_type __len
= size();
1104 if (__new_size
< __len
)
1105 _M_erase_at_end(this->_M_impl
._M_start
+ difference_type(__new_size
));
1107 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1110 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1111 /** A non-binding request to reduce memory use. */
1114 { std::__shrink_to_fit
<deque
>::_S_do_it(*this); }
1118 * Returns true if the %deque is empty. (Thus begin() would
1123 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1127 * @brief Subscript access to the data contained in the %deque.
1128 * @param n The index of the element for which data should be
1130 * @return Read/write reference to data.
1132 * This operator allows for easy, array-style, data access.
1133 * Note that data access with this operator is unchecked and
1134 * out_of_range lookups are not defined. (For checked lookups
1138 operator[](size_type __n
)
1139 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1142 * @brief Subscript access to the data contained in the %deque.
1143 * @param n The index of the element for which data should be
1145 * @return Read-only (constant) reference to data.
1147 * This operator allows for easy, array-style, data access.
1148 * Note that data access with this operator is unchecked and
1149 * out_of_range lookups are not defined. (For checked lookups
1153 operator[](size_type __n
) const
1154 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1157 /// Safety check used only from at().
1159 _M_range_check(size_type __n
) const
1161 if (__n
>= this->size())
1162 __throw_out_of_range(__N("deque::_M_range_check"));
1167 * @brief Provides access to the data contained in the %deque.
1168 * @param n The index of the element for which data should be
1170 * @return Read/write reference to data.
1171 * @throw std::out_of_range If @a n is an invalid index.
1173 * This function provides for safer data access. The parameter
1174 * is first checked that it is in the range of the deque. The
1175 * function throws out_of_range if the check fails.
1180 _M_range_check(__n
);
1181 return (*this)[__n
];
1185 * @brief Provides access to the data contained in the %deque.
1186 * @param n The index of the element for which data should be
1188 * @return Read-only (constant) reference to data.
1189 * @throw std::out_of_range If @a n is an invalid index.
1191 * This function provides for safer data access. The parameter is first
1192 * checked that it is in the range of the deque. The function throws
1193 * out_of_range if the check fails.
1196 at(size_type __n
) const
1198 _M_range_check(__n
);
1199 return (*this)[__n
];
1203 * Returns a read/write reference to the data at the first
1204 * element of the %deque.
1208 { return *begin(); }
1211 * Returns a read-only (constant) reference to the data at the first
1212 * element of the %deque.
1216 { return *begin(); }
1219 * Returns a read/write reference to the data at the last element of the
1225 iterator __tmp
= end();
1231 * Returns a read-only (constant) reference to the data at the last
1232 * element of the %deque.
1237 const_iterator __tmp
= end();
1242 // [23.2.1.2] modifiers
1244 * @brief Add data to the front of the %deque.
1245 * @param x Data to be added.
1247 * This is a typical stack operation. The function creates an
1248 * element at the front of the %deque and assigns the given
1249 * data to it. Due to the nature of a %deque this operation
1250 * can be done in constant time.
1253 push_front(const value_type
& __x
)
1255 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1257 this->_M_impl
.construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
1258 --this->_M_impl
._M_start
._M_cur
;
1261 _M_push_front_aux(__x
);
1264 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1266 push_front(value_type
&& __x
)
1267 { emplace_front(std::move(__x
)); }
1269 template<typename
... _Args
>
1271 emplace_front(_Args
&&... __args
);
1275 * @brief Add data to the end of the %deque.
1276 * @param x Data to be added.
1278 * This is a typical stack operation. The function creates an
1279 * element at the end of the %deque and assigns the given data
1280 * to it. Due to the nature of a %deque this operation can be
1281 * done in constant time.
1284 push_back(const value_type
& __x
)
1286 if (this->_M_impl
._M_finish
._M_cur
1287 != this->_M_impl
._M_finish
._M_last
- 1)
1289 this->_M_impl
.construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1290 ++this->_M_impl
._M_finish
._M_cur
;
1293 _M_push_back_aux(__x
);
1296 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1298 push_back(value_type
&& __x
)
1299 { emplace_back(std::move(__x
)); }
1301 template<typename
... _Args
>
1303 emplace_back(_Args
&&... __args
);
1307 * @brief Removes first element.
1309 * This is a typical stack operation. It shrinks the %deque by one.
1311 * Note that no data is returned, and if the first element's data is
1312 * needed, it should be retrieved before pop_front() is called.
1317 if (this->_M_impl
._M_start
._M_cur
1318 != this->_M_impl
._M_start
._M_last
- 1)
1320 this->_M_impl
.destroy(this->_M_impl
._M_start
._M_cur
);
1321 ++this->_M_impl
._M_start
._M_cur
;
1328 * @brief Removes last element.
1330 * This is a typical stack operation. It shrinks the %deque by one.
1332 * Note that no data is returned, and if the last element's data is
1333 * needed, it should be retrieved before pop_back() is called.
1338 if (this->_M_impl
._M_finish
._M_cur
1339 != this->_M_impl
._M_finish
._M_first
)
1341 --this->_M_impl
._M_finish
._M_cur
;
1342 this->_M_impl
.destroy(this->_M_impl
._M_finish
._M_cur
);
1348 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1350 * @brief Inserts an object in %deque before specified iterator.
1351 * @param position An iterator into the %deque.
1352 * @param args Arguments.
1353 * @return An iterator that points to the inserted data.
1355 * This function will insert an object of type T constructed
1356 * with T(std::forward<Args>(args)...) before the specified location.
1358 template<typename
... _Args
>
1360 emplace(iterator __position
, _Args
&&... __args
);
1364 * @brief Inserts given value into %deque before specified iterator.
1365 * @param position An iterator into the %deque.
1366 * @param x Data to be inserted.
1367 * @return An iterator that points to the inserted data.
1369 * This function will insert a copy of the given value before the
1370 * specified location.
1373 insert(iterator __position
, const value_type
& __x
);
1375 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1377 * @brief Inserts given rvalue into %deque before specified iterator.
1378 * @param position An iterator into the %deque.
1379 * @param x Data to be inserted.
1380 * @return An iterator that points to the inserted data.
1382 * This function will insert a copy of the given rvalue before the
1383 * specified location.
1386 insert(iterator __position
, value_type
&& __x
)
1387 { return emplace(__position
, std::move(__x
)); }
1390 * @brief Inserts an initializer list into the %deque.
1391 * @param p An iterator into the %deque.
1392 * @param l An initializer_list.
1394 * This function will insert copies of the data in the
1395 * initializer_list @a l into the %deque before the location
1396 * specified by @a p. This is known as <em>list insert</em>.
1399 insert(iterator __p
, initializer_list
<value_type
> __l
)
1400 { this->insert(__p
, __l
.begin(), __l
.end()); }
1404 * @brief Inserts a number of copies of given data into the %deque.
1405 * @param position An iterator into the %deque.
1406 * @param n Number of elements to be inserted.
1407 * @param x Data to be inserted.
1409 * This function will insert a specified number of copies of the given
1410 * data before the location specified by @a position.
1413 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1414 { _M_fill_insert(__position
, __n
, __x
); }
1417 * @brief Inserts a range into the %deque.
1418 * @param position An iterator into the %deque.
1419 * @param first An input iterator.
1420 * @param last An input iterator.
1422 * This function will insert copies of the data in the range
1423 * [first,last) into the %deque before the location specified
1424 * by @a pos. This is known as <em>range insert</em>.
1426 template<typename _InputIterator
>
1428 insert(iterator __position
, _InputIterator __first
,
1429 _InputIterator __last
)
1431 // Check whether it's an integral type. If so, it's not an iterator.
1432 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1433 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1437 * @brief Remove element at given position.
1438 * @param position Iterator pointing to element to be erased.
1439 * @return An iterator pointing to the next element (or end()).
1441 * This function will erase the element at the given position and thus
1442 * shorten the %deque by one.
1444 * The user is cautioned that
1445 * this function only erases the element, and that if the element is
1446 * itself a pointer, the pointed-to memory is not touched in any way.
1447 * Managing the pointer is the user's responsibility.
1450 erase(iterator __position
);
1453 * @brief Remove a range of elements.
1454 * @param first Iterator pointing to the first element to be erased.
1455 * @param last Iterator pointing to one past the last element to be
1457 * @return An iterator pointing to the element pointed to by @a last
1458 * prior to erasing (or end()).
1460 * This function will erase the elements in the range [first,last) and
1461 * shorten the %deque accordingly.
1463 * The user is cautioned that
1464 * this function only erases the elements, and that if the elements
1465 * themselves are pointers, the pointed-to memory is not touched in any
1466 * way. Managing the pointer is the user's responsibility.
1469 erase(iterator __first
, iterator __last
);
1472 * @brief Swaps data with another %deque.
1473 * @param x A %deque of the same element and allocator types.
1475 * This exchanges the elements between two deques in constant time.
1476 * (Four pointers, so it should be quite fast.)
1477 * Note that the global std::swap() function is specialized such that
1478 * std::swap(d1,d2) will feed to this function.
1483 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1484 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1485 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1486 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1488 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1489 // 431. Swapping containers with unequal allocators.
1490 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1491 __x
._M_get_Tp_allocator());
1495 * Erases all the elements. Note that this function only erases the
1496 * elements, and that if the elements themselves are pointers, the
1497 * pointed-to memory is not touched in any way. Managing the pointer is
1498 * the user's responsibility.
1502 { _M_erase_at_end(begin()); }
1505 // Internal constructor functions follow.
1507 // called by the range constructor to implement [23.1.1]/9
1509 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1510 // 438. Ambiguity in the "do the right thing" clause
1511 template<typename _Integer
>
1513 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1515 _M_initialize_map(static_cast<size_type
>(__n
));
1516 _M_fill_initialize(__x
);
1519 // called by the range constructor to implement [23.1.1]/9
1520 template<typename _InputIterator
>
1522 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1525 typedef typename
std::iterator_traits
<_InputIterator
>::
1526 iterator_category _IterCategory
;
1527 _M_range_initialize(__first
, __last
, _IterCategory());
1530 // called by the second initialize_dispatch above
1533 * @brief Fills the deque with whatever is in [first,last).
1534 * @param first An input iterator.
1535 * @param last An input iterator.
1538 * If the iterators are actually forward iterators (or better), then the
1539 * memory layout can be done all at once. Else we move forward using
1540 * push_back on each value from the iterator.
1542 template<typename _InputIterator
>
1544 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1545 std::input_iterator_tag
);
1547 // called by the second initialize_dispatch above
1548 template<typename _ForwardIterator
>
1550 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1551 std::forward_iterator_tag
);
1555 * @brief Fills the %deque with copies of value.
1556 * @param value Initial value.
1558 * @pre _M_start and _M_finish have already been initialized,
1559 * but none of the %deque's elements have yet been constructed.
1561 * This function is called only when the user provides an explicit size
1562 * (with or without an explicit exemplar value).
1565 _M_fill_initialize(const value_type
& __value
);
1567 // Internal assign functions follow. The *_aux functions do the actual
1568 // assignment work for the range versions.
1570 // called by the range assign to implement [23.1.1]/9
1572 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1573 // 438. Ambiguity in the "do the right thing" clause
1574 template<typename _Integer
>
1576 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1577 { _M_fill_assign(__n
, __val
); }
1579 // called by the range assign to implement [23.1.1]/9
1580 template<typename _InputIterator
>
1582 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1585 typedef typename
std::iterator_traits
<_InputIterator
>::
1586 iterator_category _IterCategory
;
1587 _M_assign_aux(__first
, __last
, _IterCategory());
1590 // called by the second assign_dispatch above
1591 template<typename _InputIterator
>
1593 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1594 std::input_iterator_tag
);
1596 // called by the second assign_dispatch above
1597 template<typename _ForwardIterator
>
1599 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1600 std::forward_iterator_tag
)
1602 const size_type __len
= std::distance(__first
, __last
);
1605 _ForwardIterator __mid
= __first
;
1606 std::advance(__mid
, size());
1607 std::copy(__first
, __mid
, begin());
1608 insert(end(), __mid
, __last
);
1611 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1614 // Called by assign(n,t), and the range assign when it turns out
1615 // to be the same thing.
1617 _M_fill_assign(size_type __n
, const value_type
& __val
)
1621 std::fill(begin(), end(), __val
);
1622 insert(end(), __n
- size(), __val
);
1626 _M_erase_at_end(begin() + difference_type(__n
));
1627 std::fill(begin(), end(), __val
);
1632 /// Helper functions for push_* and pop_*.
1633 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1634 void _M_push_back_aux(const value_type
&);
1636 void _M_push_front_aux(const value_type
&);
1638 template<typename
... _Args
>
1639 void _M_push_back_aux(_Args
&&... __args
);
1641 template<typename
... _Args
>
1642 void _M_push_front_aux(_Args
&&... __args
);
1645 void _M_pop_back_aux();
1647 void _M_pop_front_aux();
1650 // Internal insert functions follow. The *_aux functions do the actual
1651 // insertion work when all shortcuts fail.
1653 // called by the range insert to implement [23.1.1]/9
1655 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1656 // 438. Ambiguity in the "do the right thing" clause
1657 template<typename _Integer
>
1659 _M_insert_dispatch(iterator __pos
,
1660 _Integer __n
, _Integer __x
, __true_type
)
1661 { _M_fill_insert(__pos
, __n
, __x
); }
1663 // called by the range insert to implement [23.1.1]/9
1664 template<typename _InputIterator
>
1666 _M_insert_dispatch(iterator __pos
,
1667 _InputIterator __first
, _InputIterator __last
,
1670 typedef typename
std::iterator_traits
<_InputIterator
>::
1671 iterator_category _IterCategory
;
1672 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1675 // called by the second insert_dispatch above
1676 template<typename _InputIterator
>
1678 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1679 _InputIterator __last
, std::input_iterator_tag
);
1681 // called by the second insert_dispatch above
1682 template<typename _ForwardIterator
>
1684 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1685 _ForwardIterator __last
, std::forward_iterator_tag
);
1687 // Called by insert(p,n,x), and the range insert when it turns out to be
1688 // the same thing. Can use fill functions in optimal situations,
1689 // otherwise passes off to insert_aux(p,n,x).
1691 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1693 // called by insert(p,x)
1694 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1696 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1698 template<typename
... _Args
>
1700 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
1703 // called by insert(p,n,x) via fill_insert
1705 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1707 // called by range_insert_aux for forward iterators
1708 template<typename _ForwardIterator
>
1710 _M_insert_aux(iterator __pos
,
1711 _ForwardIterator __first
, _ForwardIterator __last
,
1715 // Internal erase functions follow.
1718 _M_destroy_data_aux(iterator __first
, iterator __last
);
1720 // Called by ~deque().
1721 // NB: Doesn't deallocate the nodes.
1722 template<typename _Alloc1
>
1724 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
1725 { _M_destroy_data_aux(__first
, __last
); }
1728 _M_destroy_data(iterator __first
, iterator __last
,
1729 const std::allocator
<_Tp
>&)
1731 if (!__has_trivial_destructor(value_type
))
1732 _M_destroy_data_aux(__first
, __last
);
1735 // Called by erase(q1, q2).
1737 _M_erase_at_begin(iterator __pos
)
1739 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
1740 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
1741 this->_M_impl
._M_start
= __pos
;
1744 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1745 // _M_fill_assign, operator=.
1747 _M_erase_at_end(iterator __pos
)
1749 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
1750 _M_destroy_nodes(__pos
._M_node
+ 1,
1751 this->_M_impl
._M_finish
._M_node
+ 1);
1752 this->_M_impl
._M_finish
= __pos
;
1756 /// Memory-handling helpers for the previous internal insert functions.
1758 _M_reserve_elements_at_front(size_type __n
)
1760 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1761 - this->_M_impl
._M_start
._M_first
;
1762 if (__n
> __vacancies
)
1763 _M_new_elements_at_front(__n
- __vacancies
);
1764 return this->_M_impl
._M_start
- difference_type(__n
);
1768 _M_reserve_elements_at_back(size_type __n
)
1770 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1771 - this->_M_impl
._M_finish
._M_cur
) - 1;
1772 if (__n
> __vacancies
)
1773 _M_new_elements_at_back(__n
- __vacancies
);
1774 return this->_M_impl
._M_finish
+ difference_type(__n
);
1778 _M_new_elements_at_front(size_type __new_elements
);
1781 _M_new_elements_at_back(size_type __new_elements
);
1787 * @brief Memory-handling helpers for the major %map.
1789 * Makes sure the _M_map has space for new nodes. Does not
1790 * actually add the nodes. Can invalidate _M_map pointers.
1791 * (And consequently, %deque iterators.)
1794 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
1796 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1797 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
1798 _M_reallocate_map(__nodes_to_add
, false);
1802 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
1804 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
1805 - this->_M_impl
._M_map
))
1806 _M_reallocate_map(__nodes_to_add
, true);
1810 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
1816 * @brief Deque equality comparison.
1817 * @param x A %deque.
1818 * @param y A %deque of the same type as @a x.
1819 * @return True iff the size and elements of the deques are equal.
1821 * This is an equivalence relation. It is linear in the size of the
1822 * deques. Deques are considered equivalent if their sizes are equal,
1823 * and if corresponding elements compare equal.
1825 template<typename _Tp
, typename _Alloc
>
1827 operator==(const deque
<_Tp
, _Alloc
>& __x
,
1828 const deque
<_Tp
, _Alloc
>& __y
)
1829 { return __x
.size() == __y
.size()
1830 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
1833 * @brief Deque ordering relation.
1834 * @param x A %deque.
1835 * @param y A %deque of the same type as @a x.
1836 * @return True iff @a x is lexicographically less than @a y.
1838 * This is a total ordering relation. It is linear in the size of the
1839 * deques. The elements must be comparable with @c <.
1841 * See std::lexicographical_compare() for how the determination is made.
1843 template<typename _Tp
, typename _Alloc
>
1845 operator<(const deque
<_Tp
, _Alloc
>& __x
,
1846 const deque
<_Tp
, _Alloc
>& __y
)
1847 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1848 __y
.begin(), __y
.end()); }
1850 /// Based on operator==
1851 template<typename _Tp
, typename _Alloc
>
1853 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
1854 const deque
<_Tp
, _Alloc
>& __y
)
1855 { return !(__x
== __y
); }
1857 /// Based on operator<
1858 template<typename _Tp
, typename _Alloc
>
1860 operator>(const deque
<_Tp
, _Alloc
>& __x
,
1861 const deque
<_Tp
, _Alloc
>& __y
)
1862 { return __y
< __x
; }
1864 /// Based on operator<
1865 template<typename _Tp
, typename _Alloc
>
1867 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
1868 const deque
<_Tp
, _Alloc
>& __y
)
1869 { return !(__y
< __x
); }
1871 /// Based on operator<
1872 template<typename _Tp
, typename _Alloc
>
1874 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
1875 const deque
<_Tp
, _Alloc
>& __y
)
1876 { return !(__x
< __y
); }
1878 /// See std::deque::swap().
1879 template<typename _Tp
, typename _Alloc
>
1881 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
1884 #undef _GLIBCXX_DEQUE_BUF_SIZE
1886 _GLIBCXX_END_NESTED_NAMESPACE
1888 #endif /* _STL_DEQUE_H */