1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_deque.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{deque}
57 #define _STL_DEQUE_H 1
59 #include <bits/concept_check.h>
60 #include <bits/stl_iterator_base_types.h>
61 #include <bits/stl_iterator_base_funcs.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
66 namespace std
_GLIBCXX_VISIBILITY(default)
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
71 * @brief This function controls the size of memory nodes.
72 * @param __size The size of an element.
73 * @return The number (not byte size) of elements per node.
75 * This function started off as a compiler kludge from SGI, but
76 * seems to be a useful wrapper around a repeated constant
77 * expression. The @b 512 is tunable (and no other code needs to
78 * change), but no investigation has been done since inheriting the
79 * SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
80 * you are doing, however: changing it breaks the binary
84 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
85 #define _GLIBCXX_DEQUE_BUF_SIZE 512
89 __deque_buf_size(size_t __size
)
90 { return (__size
< _GLIBCXX_DEQUE_BUF_SIZE
91 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE
/ __size
) : size_t(1)); }
95 * @brief A deque::iterator.
97 * Quite a bit of intelligence here. Much of the functionality of
98 * deque is actually passed off to this class. A deque holds two
99 * of these internally, marking its valid range. Access to
100 * elements is done as offsets of either of those two, relying on
101 * operator overloading in this class.
103 * All the functions are op overloads except for _M_set_node.
105 template<typename _Tp
, typename _Ref
, typename _Ptr
>
106 struct _Deque_iterator
108 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
109 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
111 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
112 { return __deque_buf_size(sizeof(_Tp
)); }
114 typedef std::random_access_iterator_tag iterator_category
;
115 typedef _Tp value_type
;
116 typedef _Ptr pointer
;
117 typedef _Ref reference
;
118 typedef size_t size_type
;
119 typedef ptrdiff_t difference_type
;
120 typedef _Tp
** _Map_pointer
;
121 typedef _Deque_iterator _Self
;
126 _Map_pointer _M_node
;
128 _Deque_iterator(_Tp
* __x
, _Map_pointer __y
) _GLIBCXX_NOEXCEPT
129 : _M_cur(__x
), _M_first(*__y
),
130 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) { }
132 _Deque_iterator() _GLIBCXX_NOEXCEPT
133 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
135 _Deque_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
136 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
137 _M_last(__x
._M_last
), _M_node(__x
._M_node
) { }
140 _M_const_cast() const _GLIBCXX_NOEXCEPT
141 { return iterator(_M_cur
, _M_node
); }
144 operator*() const _GLIBCXX_NOEXCEPT
148 operator->() const _GLIBCXX_NOEXCEPT
152 operator++() _GLIBCXX_NOEXCEPT
155 if (_M_cur
== _M_last
)
157 _M_set_node(_M_node
+ 1);
164 operator++(int) _GLIBCXX_NOEXCEPT
172 operator--() _GLIBCXX_NOEXCEPT
174 if (_M_cur
== _M_first
)
176 _M_set_node(_M_node
- 1);
184 operator--(int) _GLIBCXX_NOEXCEPT
192 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
194 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
195 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
199 const difference_type __node_offset
=
200 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
201 : -difference_type((-__offset
- 1)
202 / _S_buffer_size()) - 1;
203 _M_set_node(_M_node
+ __node_offset
);
204 _M_cur
= _M_first
+ (__offset
- __node_offset
205 * difference_type(_S_buffer_size()));
211 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
218 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
219 { return *this += -__n
; }
222 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
229 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
230 { return *(*this + __n
); }
233 * Prepares to traverse new_node. Sets everything except
234 * _M_cur, which should therefore be set by the caller
235 * immediately afterwards, based on _M_first and _M_last.
238 _M_set_node(_Map_pointer __new_node
) _GLIBCXX_NOEXCEPT
240 _M_node
= __new_node
;
241 _M_first
= *__new_node
;
242 _M_last
= _M_first
+ difference_type(_S_buffer_size());
246 // Note: we also provide overloads whose operands are of the same type in
247 // order to avoid ambiguous overload resolution when std::rel_ops operators
248 // are in scope (for additional details, see libstdc++/3628)
249 template<typename _Tp
, typename _Ref
, typename _Ptr
>
251 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
252 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
253 { return __x
._M_cur
== __y
._M_cur
; }
255 template<typename _Tp
, typename _RefL
, typename _PtrL
,
256 typename _RefR
, typename _PtrR
>
258 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
259 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
260 { return __x
._M_cur
== __y
._M_cur
; }
262 template<typename _Tp
, typename _Ref
, typename _Ptr
>
264 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
265 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
266 { return !(__x
== __y
); }
268 template<typename _Tp
, typename _RefL
, typename _PtrL
,
269 typename _RefR
, typename _PtrR
>
271 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
272 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
273 { return !(__x
== __y
); }
275 template<typename _Tp
, typename _Ref
, typename _Ptr
>
277 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
278 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
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 _RefL
, typename _PtrL
,
283 typename _RefR
, typename _PtrR
>
285 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
286 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
287 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
288 : (__x
._M_node
< __y
._M_node
); }
290 template<typename _Tp
, typename _Ref
, typename _Ptr
>
292 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
293 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
294 { return __y
< __x
; }
296 template<typename _Tp
, typename _RefL
, typename _PtrL
,
297 typename _RefR
, typename _PtrR
>
299 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
300 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
301 { return __y
< __x
; }
303 template<typename _Tp
, typename _Ref
, typename _Ptr
>
305 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
306 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
307 { return !(__y
< __x
); }
309 template<typename _Tp
, typename _RefL
, typename _PtrL
,
310 typename _RefR
, typename _PtrR
>
312 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
313 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
314 { return !(__y
< __x
); }
316 template<typename _Tp
, typename _Ref
, typename _Ptr
>
318 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
319 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
320 { return !(__x
< __y
); }
322 template<typename _Tp
, typename _RefL
, typename _PtrL
,
323 typename _RefR
, typename _PtrR
>
325 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
326 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
327 { return !(__x
< __y
); }
329 // _GLIBCXX_RESOLVE_LIB_DEFECTS
330 // According to the resolution of DR179 not only the various comparison
331 // operators but also operator- must accept mixed iterator/const_iterator
333 template<typename _Tp
, typename _Ref
, typename _Ptr
>
334 inline typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
335 operator-(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
336 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
) _GLIBCXX_NOEXCEPT
338 return typename _Deque_iterator
<_Tp
, _Ref
, _Ptr
>::difference_type
339 (_Deque_iterator
<_Tp
, _Ref
, _Ptr
>::_S_buffer_size())
340 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
341 + (__y
._M_last
- __y
._M_cur
);
344 template<typename _Tp
, typename _RefL
, typename _PtrL
,
345 typename _RefR
, typename _PtrR
>
346 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
347 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
348 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
) _GLIBCXX_NOEXCEPT
350 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
351 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
352 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
353 + (__y
._M_last
- __y
._M_cur
);
356 template<typename _Tp
, typename _Ref
, typename _Ptr
>
357 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
358 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
360 { return __x
+ __n
; }
362 template<typename _Tp
>
364 fill(const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&,
365 const _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>&, const _Tp
&);
367 template<typename _Tp
>
368 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
369 copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
370 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
371 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
373 template<typename _Tp
>
374 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
375 copy(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
376 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
377 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
378 { return std::copy(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
379 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
382 template<typename _Tp
>
383 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
384 copy_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
385 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
386 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
388 template<typename _Tp
>
389 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
390 copy_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
391 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
392 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
393 { return std::copy_backward(_Deque_iterator
<_Tp
,
394 const _Tp
&, const _Tp
*>(__first
),
396 const _Tp
&, const _Tp
*>(__last
),
399 #if __cplusplus >= 201103L
400 template<typename _Tp
>
401 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
402 move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
403 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
404 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
406 template<typename _Tp
>
407 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
408 move(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
409 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
410 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
411 { return std::move(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__first
),
412 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>(__last
),
415 template<typename _Tp
>
416 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
417 move_backward(_Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
418 _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*>,
419 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>);
421 template<typename _Tp
>
422 inline _Deque_iterator
<_Tp
, _Tp
&, _Tp
*>
423 move_backward(_Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __first
,
424 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __last
,
425 _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> __result
)
426 { return std::move_backward(_Deque_iterator
<_Tp
,
427 const _Tp
&, const _Tp
*>(__first
),
429 const _Tp
&, const _Tp
*>(__last
),
434 * Deque base class. This class provides the unified face for %deque's
435 * allocation. This class's constructor and destructor allocate and
436 * deallocate (but do not initialize) storage. This makes %exception
439 * Nothing in this class ever constructs or destroys an actual Tp element.
440 * (Deque handles that itself.) Only/All memory management is performed
443 template<typename _Tp
, typename _Alloc
>
447 typedef _Alloc allocator_type
;
450 get_allocator() const _GLIBCXX_NOEXCEPT
451 { return allocator_type(_M_get_Tp_allocator()); }
453 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
454 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
458 { _M_initialize_map(0); }
460 _Deque_base(size_t __num_elements
)
462 { _M_initialize_map(__num_elements
); }
464 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
466 { _M_initialize_map(__num_elements
); }
468 _Deque_base(const allocator_type
& __a
)
470 { _M_initialize_map(0); }
472 #if __cplusplus >= 201103L
473 _Deque_base(_Deque_base
&& __x
)
474 : _M_impl(std::move(__x
._M_get_Tp_allocator()))
476 _M_initialize_map(0);
477 if (__x
._M_impl
._M_map
)
479 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
480 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
481 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
482 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
487 ~_Deque_base() _GLIBCXX_NOEXCEPT
;
490 //This struct encapsulates the implementation of the std::deque
491 //standard container and at the same time makes use of the EBO
492 //for empty allocators.
493 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
495 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
498 : public _Tp_alloc_type
506 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
507 _M_start(), _M_finish()
510 _Deque_impl(const _Tp_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
511 : _Tp_alloc_type(__a
), _M_map(0), _M_map_size(0),
512 _M_start(), _M_finish()
515 #if __cplusplus >= 201103L
516 _Deque_impl(_Tp_alloc_type
&& __a
) _GLIBCXX_NOEXCEPT
517 : _Tp_alloc_type(std::move(__a
)), _M_map(0), _M_map_size(0),
518 _M_start(), _M_finish()
524 _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
525 { return *static_cast<_Tp_alloc_type
*>(&this->_M_impl
); }
527 const _Tp_alloc_type
&
528 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
529 { return *static_cast<const _Tp_alloc_type
*>(&this->_M_impl
); }
532 _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
533 { return _Map_alloc_type(_M_get_Tp_allocator()); }
538 return _M_impl
._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp
)));
542 _M_deallocate_node(_Tp
* __p
) _GLIBCXX_NOEXCEPT
544 _M_impl
._Tp_alloc_type::deallocate(__p
, __deque_buf_size(sizeof(_Tp
)));
548 _M_allocate_map(size_t __n
)
549 { return _M_get_map_allocator().allocate(__n
); }
552 _M_deallocate_map(_Tp
** __p
, size_t __n
) _GLIBCXX_NOEXCEPT
553 { _M_get_map_allocator().deallocate(__p
, __n
); }
556 void _M_initialize_map(size_t);
557 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
558 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
) _GLIBCXX_NOEXCEPT
;
559 enum { _S_initial_map_size
= 8 };
564 template<typename _Tp
, typename _Alloc
>
565 _Deque_base
<_Tp
, _Alloc
>::
566 ~_Deque_base() _GLIBCXX_NOEXCEPT
568 if (this->_M_impl
._M_map
)
570 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
,
571 this->_M_impl
._M_finish
._M_node
+ 1);
572 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
577 * @brief Layout storage.
578 * @param __num_elements The count of T's for which to allocate space
582 * The initial underlying memory layout is a bit complicated...
584 template<typename _Tp
, typename _Alloc
>
586 _Deque_base
<_Tp
, _Alloc
>::
587 _M_initialize_map(size_t __num_elements
)
589 const size_t __num_nodes
= (__num_elements
/ __deque_buf_size(sizeof(_Tp
))
592 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
593 size_t(__num_nodes
+ 2));
594 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
596 // For "small" maps (needing less than _M_map_size nodes), allocation
597 // starts in the middle elements and grows outwards. So nstart may be
598 // the beginning of _M_map, but for small maps it may be as far in as
601 _Tp
** __nstart
= (this->_M_impl
._M_map
602 + (this->_M_impl
._M_map_size
- __num_nodes
) / 2);
603 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
606 { _M_create_nodes(__nstart
, __nfinish
); }
609 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
610 this->_M_impl
._M_map
= 0;
611 this->_M_impl
._M_map_size
= 0;
612 __throw_exception_again
;
615 this->_M_impl
._M_start
._M_set_node(__nstart
);
616 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
617 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
618 this->_M_impl
._M_finish
._M_cur
= (this->_M_impl
._M_finish
._M_first
620 % __deque_buf_size(sizeof(_Tp
)));
623 template<typename _Tp
, typename _Alloc
>
625 _Deque_base
<_Tp
, _Alloc
>::
626 _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
631 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
632 *__cur
= this->_M_allocate_node();
636 _M_destroy_nodes(__nstart
, __cur
);
637 __throw_exception_again
;
641 template<typename _Tp
, typename _Alloc
>
643 _Deque_base
<_Tp
, _Alloc
>::
644 _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
) _GLIBCXX_NOEXCEPT
646 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
647 _M_deallocate_node(*__n
);
651 * @brief A standard container using fixed-size memory allocation and
652 * constant-time manipulation of elements at either end.
656 * @tparam _Tp Type of element.
657 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
659 * Meets the requirements of a <a href="tables.html#65">container</a>, a
660 * <a href="tables.html#66">reversible container</a>, and a
661 * <a href="tables.html#67">sequence</a>, including the
662 * <a href="tables.html#68">optional sequence requirements</a>.
664 * In previous HP/SGI versions of deque, there was an extra template
665 * parameter so users could control the node size. This extension turned
666 * out to violate the C++ standard (it can be detected using template
667 * template parameters), and it was removed.
669 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
672 * - size_t _M_map_size
673 * - iterator _M_start, _M_finish
675 * map_size is at least 8. %map is an array of map_size
676 * pointers-to-@a nodes. (The name %map has nothing to do with the
677 * std::map class, and @b nodes should not be confused with
678 * std::list's usage of @a node.)
680 * A @a node has no specific type name as such, but it is referred
681 * to as @a node in this file. It is a simple array-of-Tp. If Tp
682 * is very large, there will be one Tp element per node (i.e., an
683 * @a array of one). For non-huge Tp's, node size is inversely
684 * related to Tp size: the larger the Tp, the fewer Tp's will fit
685 * in a node. The goal here is to keep the total size of a node
686 * relatively small and constant over different Tp's, to improve
687 * allocator efficiency.
689 * Not every pointer in the %map array will point to a node. If
690 * the initial number of elements in the deque is small, the
691 * /middle/ %map pointers will be valid, and the ones at the edges
692 * will be unused. This same situation will arise as the %map
693 * grows: available %map pointers, if any, will be on the ends. As
694 * new nodes are created, only a subset of the %map's pointers need
695 * to be copied @a outward.
698 * - For any nonsingular iterator i:
699 * - i.node points to a member of the %map array. (Yes, you read that
700 * correctly: i.node does not actually point to a node.) The member of
701 * the %map array is what actually points to the node.
702 * - i.first == *(i.node) (This points to the node (first Tp element).)
703 * - i.last == i.first + node_size
704 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
705 * the implication of this is that i.cur is always a dereferenceable
706 * pointer, even if i is a past-the-end iterator.
707 * - Start and Finish are always nonsingular iterators. NOTE: this
708 * means that an empty deque must have one node, a deque with <N
709 * elements (where N is the node buffer size) must have one node, a
710 * deque with N through (2N-1) elements must have two nodes, etc.
711 * - For every node other than start.node and finish.node, every
712 * element in the node is an initialized object. If start.node ==
713 * finish.node, then [start.cur, finish.cur) are initialized
714 * objects, and the elements outside that range are uninitialized
715 * storage. Otherwise, [start.cur, start.last) and [finish.first,
716 * finish.cur) are initialized objects, and [start.first, start.cur)
717 * and [finish.cur, finish.last) are uninitialized storage.
718 * - [%map, %map + map_size) is a valid, non-empty range.
719 * - [start.node, finish.node] is a valid range contained within
720 * [%map, %map + map_size).
721 * - A pointer in the range [%map, %map + map_size) points to an allocated
722 * node if and only if the pointer is in the range
723 * [start.node, finish.node].
725 * Here's the magic: nothing in deque is @b aware of the discontiguous
728 * The memory setup and layout occurs in the parent, _Base, and the iterator
729 * class is entirely responsible for @a leaping from one node to the next.
730 * All the implementation routines for deque itself work only through the
731 * start and finish iterators. This keeps the routines simple and sane,
732 * and we can use other standard algorithms as well.
734 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
735 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
737 // concept requirements
738 typedef typename
_Alloc::value_type _Alloc_value_type
;
739 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
740 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
742 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
743 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
746 typedef _Tp value_type
;
747 typedef typename
_Tp_alloc_type::pointer pointer
;
748 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
749 typedef typename
_Tp_alloc_type::reference reference
;
750 typedef typename
_Tp_alloc_type::const_reference const_reference
;
751 typedef typename
_Base::iterator iterator
;
752 typedef typename
_Base::const_iterator const_iterator
;
753 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
754 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
755 typedef size_t size_type
;
756 typedef ptrdiff_t difference_type
;
757 typedef _Alloc allocator_type
;
760 typedef pointer
* _Map_pointer
;
762 static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
763 { return __deque_buf_size(sizeof(_Tp
)); }
765 // Functions controlling memory layout, and nothing else.
766 using _Base::_M_initialize_map
;
767 using _Base::_M_create_nodes
;
768 using _Base::_M_destroy_nodes
;
769 using _Base::_M_allocate_node
;
770 using _Base::_M_deallocate_node
;
771 using _Base::_M_allocate_map
;
772 using _Base::_M_deallocate_map
;
773 using _Base::_M_get_Tp_allocator
;
776 * A total of four data members accumulated down the hierarchy.
777 * May be accessed via _M_impl.*
779 using _Base::_M_impl
;
782 // [23.2.1.1] construct/copy/destroy
783 // (assign() and get_allocator() are also listed in this section)
785 * @brief Creates a %deque with no elements.
786 * @param __a An allocator object.
789 deque(const allocator_type
& __a
= allocator_type())
792 #if __cplusplus >= 201103L
794 * @brief Creates a %deque with default constructed elements.
795 * @param __n The number of elements to initially create.
797 * This constructor fills the %deque with @a n default
798 * constructed elements.
803 { _M_default_initialize(); }
806 * @brief Creates a %deque with copies of an exemplar element.
807 * @param __n The number of elements to initially create.
808 * @param __value An element to copy.
809 * @param __a An allocator.
811 * This constructor fills the %deque with @a __n copies of @a __value.
813 deque(size_type __n
, const value_type
& __value
,
814 const allocator_type
& __a
= allocator_type())
816 { _M_fill_initialize(__value
); }
819 * @brief Creates a %deque with copies of an exemplar element.
820 * @param __n The number of elements to initially create.
821 * @param __value An element to copy.
822 * @param __a An allocator.
824 * This constructor fills the %deque with @a __n copies of @a __value.
827 deque(size_type __n
, const value_type
& __value
= value_type(),
828 const allocator_type
& __a
= allocator_type())
830 { _M_fill_initialize(__value
); }
834 * @brief %Deque copy constructor.
835 * @param __x A %deque of identical element and allocator types.
837 * The newly-created %deque uses a copy of the allocation object used
840 deque(const deque
& __x
)
841 : _Base(__x
._M_get_Tp_allocator(), __x
.size())
842 { std::__uninitialized_copy_a(__x
.begin(), __x
.end(),
843 this->_M_impl
._M_start
,
844 _M_get_Tp_allocator()); }
846 #if __cplusplus >= 201103L
848 * @brief %Deque move constructor.
849 * @param __x A %deque of identical element and allocator types.
851 * The newly-created %deque contains the exact contents of @a __x.
852 * The contents of @a __x are a valid, but unspecified %deque.
855 : _Base(std::move(__x
)) { }
858 * @brief Builds a %deque from an initializer list.
859 * @param __l An initializer_list.
860 * @param __a An allocator object.
862 * Create a %deque consisting of copies of the elements in the
863 * initializer_list @a __l.
865 * This will call the element type's copy constructor N times
866 * (where N is __l.size()) and do no memory reallocation.
868 deque(initializer_list
<value_type
> __l
,
869 const allocator_type
& __a
= allocator_type())
872 _M_range_initialize(__l
.begin(), __l
.end(),
873 random_access_iterator_tag());
878 * @brief Builds a %deque from a range.
879 * @param __first An input iterator.
880 * @param __last An input iterator.
881 * @param __a An allocator object.
883 * Create a %deque consisting of copies of the elements from [__first,
886 * If the iterators are forward, bidirectional, or random-access, then
887 * this will call the elements' copy constructor N times (where N is
888 * distance(__first,__last)) and do no memory reallocation. But if only
889 * input iterators are used, then this will do at most 2N calls to the
890 * copy constructor, and logN memory reallocations.
892 #if __cplusplus >= 201103L
893 template<typename _InputIterator
,
894 typename
= std::_RequireInputIter
<_InputIterator
>>
895 deque(_InputIterator __first
, _InputIterator __last
,
896 const allocator_type
& __a
= allocator_type())
898 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
900 template<typename _InputIterator
>
901 deque(_InputIterator __first
, _InputIterator __last
,
902 const allocator_type
& __a
= allocator_type())
905 // Check whether it's an integral type. If so, it's not an iterator.
906 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
907 _M_initialize_dispatch(__first
, __last
, _Integral());
912 * The dtor only erases the elements, and note that if the elements
913 * themselves are pointers, the pointed-to memory is not touched in any
914 * way. Managing the pointer is the user's responsibility.
916 ~deque() _GLIBCXX_NOEXCEPT
917 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
920 * @brief %Deque assignment operator.
921 * @param __x A %deque of identical element and allocator types.
923 * All the elements of @a x are copied, but unlike the copy constructor,
924 * the allocator object is not copied.
927 operator=(const deque
& __x
);
929 #if __cplusplus >= 201103L
931 * @brief %Deque move assignment operator.
932 * @param __x A %deque of identical element and allocator types.
934 * The contents of @a __x are moved into this deque (without copying).
935 * @a __x is a valid, but unspecified %deque.
938 operator=(deque
&& __x
) noexcept
948 * @brief Assigns an initializer list to a %deque.
949 * @param __l An initializer_list.
951 * This function fills a %deque with copies of the elements in the
952 * initializer_list @a __l.
954 * Note that the assignment completely changes the %deque and that the
955 * resulting %deque's size is the same as the number of elements
956 * assigned. Old data may be lost.
959 operator=(initializer_list
<value_type
> __l
)
961 this->assign(__l
.begin(), __l
.end());
967 * @brief Assigns a given value to a %deque.
968 * @param __n Number of elements to be assigned.
969 * @param __val Value to be assigned.
971 * This function fills a %deque with @a n copies of the given
972 * value. Note that the assignment completely changes the
973 * %deque and that the resulting %deque's size is the same as
974 * the number of elements assigned. Old data may be lost.
977 assign(size_type __n
, const value_type
& __val
)
978 { _M_fill_assign(__n
, __val
); }
981 * @brief Assigns a range to a %deque.
982 * @param __first An input iterator.
983 * @param __last An input iterator.
985 * This function fills a %deque with copies of the elements in the
986 * range [__first,__last).
988 * Note that the assignment completely changes the %deque and that the
989 * resulting %deque's size is the same as the number of elements
990 * assigned. Old data may be lost.
992 #if __cplusplus >= 201103L
993 template<typename _InputIterator
,
994 typename
= std::_RequireInputIter
<_InputIterator
>>
996 assign(_InputIterator __first
, _InputIterator __last
)
997 { _M_assign_dispatch(__first
, __last
, __false_type()); }
999 template<typename _InputIterator
>
1001 assign(_InputIterator __first
, _InputIterator __last
)
1003 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1004 _M_assign_dispatch(__first
, __last
, _Integral());
1008 #if __cplusplus >= 201103L
1010 * @brief Assigns an initializer list to a %deque.
1011 * @param __l An initializer_list.
1013 * This function fills a %deque with copies of the elements in the
1014 * initializer_list @a __l.
1016 * Note that the assignment completely changes the %deque and that the
1017 * resulting %deque's size is the same as the number of elements
1018 * assigned. Old data may be lost.
1021 assign(initializer_list
<value_type
> __l
)
1022 { this->assign(__l
.begin(), __l
.end()); }
1025 /// Get a copy of the memory allocation object.
1027 get_allocator() const _GLIBCXX_NOEXCEPT
1028 { return _Base::get_allocator(); }
1032 * Returns a read/write iterator that points to the first element in the
1033 * %deque. Iteration is done in ordinary element order.
1036 begin() _GLIBCXX_NOEXCEPT
1037 { return this->_M_impl
._M_start
; }
1040 * Returns a read-only (constant) iterator that points to the first
1041 * element in the %deque. Iteration is done in ordinary element order.
1044 begin() const _GLIBCXX_NOEXCEPT
1045 { return this->_M_impl
._M_start
; }
1048 * Returns a read/write iterator that points one past the last
1049 * element in the %deque. Iteration is done in ordinary
1053 end() _GLIBCXX_NOEXCEPT
1054 { return this->_M_impl
._M_finish
; }
1057 * Returns a read-only (constant) iterator that points one past
1058 * the last element in the %deque. Iteration is done in
1059 * ordinary element order.
1062 end() const _GLIBCXX_NOEXCEPT
1063 { return this->_M_impl
._M_finish
; }
1066 * Returns a read/write reverse iterator that points to the
1067 * last element in the %deque. Iteration is done in reverse
1071 rbegin() _GLIBCXX_NOEXCEPT
1072 { return reverse_iterator(this->_M_impl
._M_finish
); }
1075 * Returns a read-only (constant) reverse iterator that points
1076 * to the last element in the %deque. Iteration is done in
1077 * reverse element order.
1079 const_reverse_iterator
1080 rbegin() const _GLIBCXX_NOEXCEPT
1081 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1084 * Returns a read/write reverse iterator that points to one
1085 * before the first element in the %deque. Iteration is done
1086 * in reverse element order.
1089 rend() _GLIBCXX_NOEXCEPT
1090 { return reverse_iterator(this->_M_impl
._M_start
); }
1093 * Returns a read-only (constant) reverse iterator that points
1094 * to one before the first element in the %deque. Iteration is
1095 * done in reverse element order.
1097 const_reverse_iterator
1098 rend() const _GLIBCXX_NOEXCEPT
1099 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1101 #if __cplusplus >= 201103L
1103 * Returns a read-only (constant) iterator that points to the first
1104 * element in the %deque. Iteration is done in ordinary element order.
1107 cbegin() const noexcept
1108 { return this->_M_impl
._M_start
; }
1111 * Returns a read-only (constant) iterator that points one past
1112 * the last element in the %deque. Iteration is done in
1113 * ordinary element order.
1116 cend() const noexcept
1117 { return this->_M_impl
._M_finish
; }
1120 * Returns a read-only (constant) reverse iterator that points
1121 * to the last element in the %deque. Iteration is done in
1122 * reverse element order.
1124 const_reverse_iterator
1125 crbegin() const noexcept
1126 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
1129 * Returns a read-only (constant) reverse iterator that points
1130 * to one before the first element in the %deque. Iteration is
1131 * done in reverse element order.
1133 const_reverse_iterator
1134 crend() const noexcept
1135 { return const_reverse_iterator(this->_M_impl
._M_start
); }
1138 // [23.2.1.2] capacity
1139 /** Returns the number of elements in the %deque. */
1141 size() const _GLIBCXX_NOEXCEPT
1142 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
1144 /** Returns the size() of the largest possible %deque. */
1146 max_size() const _GLIBCXX_NOEXCEPT
1147 { return _M_get_Tp_allocator().max_size(); }
1149 #if __cplusplus >= 201103L
1151 * @brief Resizes the %deque to the specified number of elements.
1152 * @param __new_size Number of elements the %deque should contain.
1154 * This function will %resize the %deque to the specified
1155 * number of elements. If the number is smaller than the
1156 * %deque's current size the %deque is truncated, otherwise
1157 * default constructed elements are appended.
1160 resize(size_type __new_size
)
1162 const size_type __len
= size();
1163 if (__new_size
> __len
)
1164 _M_default_append(__new_size
- __len
);
1165 else if (__new_size
< __len
)
1166 _M_erase_at_end(this->_M_impl
._M_start
1167 + difference_type(__new_size
));
1171 * @brief Resizes the %deque to the specified number of elements.
1172 * @param __new_size Number of elements the %deque should contain.
1173 * @param __x Data with which new elements should be populated.
1175 * This function will %resize the %deque to the specified
1176 * number of elements. If the number is smaller than the
1177 * %deque's current size the %deque is truncated, otherwise the
1178 * %deque is extended and new elements are populated with given
1182 resize(size_type __new_size
, const value_type
& __x
)
1184 const size_type __len
= size();
1185 if (__new_size
> __len
)
1186 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1187 else if (__new_size
< __len
)
1188 _M_erase_at_end(this->_M_impl
._M_start
1189 + difference_type(__new_size
));
1193 * @brief Resizes the %deque to the specified number of elements.
1194 * @param __new_size Number of elements the %deque should contain.
1195 * @param __x Data with which new elements should be populated.
1197 * This function will %resize the %deque to the specified
1198 * number of elements. If the number is smaller than the
1199 * %deque's current size the %deque is truncated, otherwise the
1200 * %deque is extended and new elements are populated with given
1204 resize(size_type __new_size
, value_type __x
= value_type())
1206 const size_type __len
= size();
1207 if (__new_size
> __len
)
1208 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
1209 else if (__new_size
< __len
)
1210 _M_erase_at_end(this->_M_impl
._M_start
1211 + difference_type(__new_size
));
1215 #if __cplusplus >= 201103L
1216 /** A non-binding request to reduce memory use. */
1218 shrink_to_fit() noexcept
1219 { _M_shrink_to_fit(); }
1223 * Returns true if the %deque is empty. (Thus begin() would
1227 empty() const _GLIBCXX_NOEXCEPT
1228 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
1232 * @brief Subscript access to the data contained in the %deque.
1233 * @param __n The index of the element for which data should be
1235 * @return Read/write reference to data.
1237 * This operator allows for easy, array-style, data access.
1238 * Note that data access with this operator is unchecked and
1239 * out_of_range lookups are not defined. (For checked lookups
1243 operator[](size_type __n
) _GLIBCXX_NOEXCEPT
1244 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1247 * @brief Subscript access to the data contained in the %deque.
1248 * @param __n The index of the element for which data should be
1250 * @return Read-only (constant) reference to data.
1252 * This operator allows for easy, array-style, data access.
1253 * Note that data access with this operator is unchecked and
1254 * out_of_range lookups are not defined. (For checked lookups
1258 operator[](size_type __n
) const _GLIBCXX_NOEXCEPT
1259 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
1262 /// Safety check used only from at().
1264 _M_range_check(size_type __n
) const
1266 if (__n
>= this->size())
1267 __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1268 "(which is %zu)>= this->size() "
1275 * @brief Provides access to the data contained in the %deque.
1276 * @param __n The index of the element for which data should be
1278 * @return Read/write reference to data.
1279 * @throw std::out_of_range If @a __n is an invalid index.
1281 * This function provides for safer data access. The parameter
1282 * is first checked that it is in the range of the deque. The
1283 * function throws out_of_range if the check fails.
1288 _M_range_check(__n
);
1289 return (*this)[__n
];
1293 * @brief Provides access to the data contained in the %deque.
1294 * @param __n The index of the element for which data should be
1296 * @return Read-only (constant) reference to data.
1297 * @throw std::out_of_range If @a __n is an invalid index.
1299 * This function provides for safer data access. The parameter is first
1300 * checked that it is in the range of the deque. The function throws
1301 * out_of_range if the check fails.
1304 at(size_type __n
) const
1306 _M_range_check(__n
);
1307 return (*this)[__n
];
1311 * Returns a read/write reference to the data at the first
1312 * element of the %deque.
1315 front() _GLIBCXX_NOEXCEPT
1316 { return *begin(); }
1319 * Returns a read-only (constant) reference to the data at the first
1320 * element of the %deque.
1323 front() const _GLIBCXX_NOEXCEPT
1324 { return *begin(); }
1327 * Returns a read/write reference to the data at the last element of the
1331 back() _GLIBCXX_NOEXCEPT
1333 iterator __tmp
= end();
1339 * Returns a read-only (constant) reference to the data at the last
1340 * element of the %deque.
1343 back() const _GLIBCXX_NOEXCEPT
1345 const_iterator __tmp
= end();
1350 // [23.2.1.2] modifiers
1352 * @brief Add data to the front of the %deque.
1353 * @param __x Data to be added.
1355 * This is a typical stack operation. The function creates an
1356 * element at the front of the %deque and assigns the given
1357 * data to it. Due to the nature of a %deque this operation
1358 * can be done in constant time.
1361 push_front(const value_type
& __x
)
1363 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
1365 this->_M_impl
.construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
1366 --this->_M_impl
._M_start
._M_cur
;
1369 _M_push_front_aux(__x
);
1372 #if __cplusplus >= 201103L
1374 push_front(value_type
&& __x
)
1375 { emplace_front(std::move(__x
)); }
1377 template<typename
... _Args
>
1379 emplace_front(_Args
&&... __args
);
1383 * @brief Add data to the end of the %deque.
1384 * @param __x Data to be added.
1386 * This is a typical stack operation. The function creates an
1387 * element at the end of the %deque and assigns the given data
1388 * to it. Due to the nature of a %deque this operation can be
1389 * done in constant time.
1392 push_back(const value_type
& __x
)
1394 if (this->_M_impl
._M_finish
._M_cur
1395 != this->_M_impl
._M_finish
._M_last
- 1)
1397 this->_M_impl
.construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1398 ++this->_M_impl
._M_finish
._M_cur
;
1401 _M_push_back_aux(__x
);
1404 #if __cplusplus >= 201103L
1406 push_back(value_type
&& __x
)
1407 { emplace_back(std::move(__x
)); }
1409 template<typename
... _Args
>
1411 emplace_back(_Args
&&... __args
);
1415 * @brief Removes first element.
1417 * This is a typical stack operation. It shrinks the %deque by one.
1419 * Note that no data is returned, and if the first element's data is
1420 * needed, it should be retrieved before pop_front() is called.
1423 pop_front() _GLIBCXX_NOEXCEPT
1425 if (this->_M_impl
._M_start
._M_cur
1426 != this->_M_impl
._M_start
._M_last
- 1)
1428 this->_M_impl
.destroy(this->_M_impl
._M_start
._M_cur
);
1429 ++this->_M_impl
._M_start
._M_cur
;
1436 * @brief Removes last element.
1438 * This is a typical stack operation. It shrinks the %deque by one.
1440 * Note that no data is returned, and if the last element's data is
1441 * needed, it should be retrieved before pop_back() is called.
1444 pop_back() _GLIBCXX_NOEXCEPT
1446 if (this->_M_impl
._M_finish
._M_cur
1447 != this->_M_impl
._M_finish
._M_first
)
1449 --this->_M_impl
._M_finish
._M_cur
;
1450 this->_M_impl
.destroy(this->_M_impl
._M_finish
._M_cur
);
1456 #if __cplusplus >= 201103L
1458 * @brief Inserts an object in %deque before specified iterator.
1459 * @param __position A const_iterator into the %deque.
1460 * @param __args Arguments.
1461 * @return An iterator that points to the inserted data.
1463 * This function will insert an object of type T constructed
1464 * with T(std::forward<Args>(args)...) before the specified location.
1466 template<typename
... _Args
>
1468 emplace(const_iterator __position
, _Args
&&... __args
);
1471 * @brief Inserts given value into %deque before specified iterator.
1472 * @param __position A const_iterator into the %deque.
1473 * @param __x Data to be inserted.
1474 * @return An iterator that points to the inserted data.
1476 * This function will insert a copy of the given value before the
1477 * specified location.
1480 insert(const_iterator __position
, const value_type
& __x
);
1483 * @brief Inserts given value into %deque before specified iterator.
1484 * @param __position An iterator into the %deque.
1485 * @param __x Data to be inserted.
1486 * @return An iterator that points to the inserted data.
1488 * This function will insert a copy of the given value before the
1489 * specified location.
1492 insert(iterator __position
, const value_type
& __x
);
1495 #if __cplusplus >= 201103L
1497 * @brief Inserts given rvalue into %deque before specified iterator.
1498 * @param __position A const_iterator into the %deque.
1499 * @param __x Data to be inserted.
1500 * @return An iterator that points to the inserted data.
1502 * This function will insert a copy of the given rvalue before the
1503 * specified location.
1506 insert(const_iterator __position
, value_type
&& __x
)
1507 { return emplace(__position
, std::move(__x
)); }
1510 * @brief Inserts an initializer list into the %deque.
1511 * @param __p An iterator into the %deque.
1512 * @param __l An initializer_list.
1514 * This function will insert copies of the data in the
1515 * initializer_list @a __l into the %deque before the location
1516 * specified by @a __p. This is known as <em>list insert</em>.
1519 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1520 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1523 #if __cplusplus >= 201103L
1525 * @brief Inserts a number of copies of given data into the %deque.
1526 * @param __position A const_iterator into the %deque.
1527 * @param __n Number of elements to be inserted.
1528 * @param __x Data to be inserted.
1529 * @return An iterator that points to the inserted data.
1531 * This function will insert a specified number of copies of the given
1532 * data before the location specified by @a __position.
1535 insert(const_iterator __position
, size_type __n
, const value_type
& __x
)
1537 difference_type __offset
= __position
- cbegin();
1538 _M_fill_insert(__position
._M_const_cast(), __n
, __x
);
1539 return begin() + __offset
;
1543 * @brief Inserts a number of copies of given data into the %deque.
1544 * @param __position An iterator into the %deque.
1545 * @param __n Number of elements to be inserted.
1546 * @param __x Data to be inserted.
1548 * This function will insert a specified number of copies of the given
1549 * data before the location specified by @a __position.
1552 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1553 { _M_fill_insert(__position
, __n
, __x
); }
1556 #if __cplusplus >= 201103L
1558 * @brief Inserts a range into the %deque.
1559 * @param __position A const_iterator into the %deque.
1560 * @param __first An input iterator.
1561 * @param __last An input iterator.
1562 * @return An iterator that points to the inserted data.
1564 * This function will insert copies of the data in the range
1565 * [__first,__last) into the %deque before the location specified
1566 * by @a __position. This is known as <em>range insert</em>.
1568 template<typename _InputIterator
,
1569 typename
= std::_RequireInputIter
<_InputIterator
>>
1571 insert(const_iterator __position
, _InputIterator __first
,
1572 _InputIterator __last
)
1574 difference_type __offset
= __position
- cbegin();
1575 _M_insert_dispatch(__position
._M_const_cast(),
1576 __first
, __last
, __false_type());
1577 return begin() + __offset
;
1581 * @brief Inserts a range into the %deque.
1582 * @param __position An iterator into the %deque.
1583 * @param __first An input iterator.
1584 * @param __last An input iterator.
1586 * This function will insert copies of the data in the range
1587 * [__first,__last) into the %deque before the location specified
1588 * by @a __position. This is known as <em>range insert</em>.
1590 template<typename _InputIterator
>
1592 insert(iterator __position
, _InputIterator __first
,
1593 _InputIterator __last
)
1595 // Check whether it's an integral type. If so, it's not an iterator.
1596 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1597 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1602 * @brief Remove element at given position.
1603 * @param __position Iterator pointing to element to be erased.
1604 * @return An iterator pointing to the next element (or end()).
1606 * This function will erase the element at the given position and thus
1607 * shorten the %deque by one.
1609 * The user is cautioned that
1610 * this function only erases the element, and that if the element is
1611 * itself a pointer, the pointed-to memory is not touched in any way.
1612 * Managing the pointer is the user's responsibility.
1615 #if __cplusplus >= 201103L
1616 erase(const_iterator __position
)
1618 erase(iterator __position
)
1620 { return _M_erase(__position
._M_const_cast()); }
1623 * @brief Remove a range of elements.
1624 * @param __first Iterator pointing to the first element to be erased.
1625 * @param __last Iterator pointing to one past the last element to be
1627 * @return An iterator pointing to the element pointed to by @a last
1628 * prior to erasing (or end()).
1630 * This function will erase the elements in the range
1631 * [__first,__last) and shorten the %deque accordingly.
1633 * The user is cautioned that
1634 * this function only erases the elements, and that if the elements
1635 * themselves are pointers, the pointed-to memory is not touched in any
1636 * way. Managing the pointer is the user's responsibility.
1639 #if __cplusplus >= 201103L
1640 erase(const_iterator __first
, const_iterator __last
)
1642 erase(iterator __first
, iterator __last
)
1644 { return _M_erase(__first
._M_const_cast(), __last
._M_const_cast()); }
1647 * @brief Swaps data with another %deque.
1648 * @param __x A %deque of the same element and allocator types.
1650 * This exchanges the elements between two deques in constant time.
1651 * (Four pointers, so it should be quite fast.)
1652 * Note that the global std::swap() function is specialized such that
1653 * std::swap(d1,d2) will feed to this function.
1656 swap(deque
& __x
) _GLIBCXX_NOEXCEPT
1658 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1659 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1660 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1661 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1663 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1664 // 431. Swapping containers with unequal allocators.
1665 std::__alloc_swap
<_Tp_alloc_type
>::_S_do_it(_M_get_Tp_allocator(),
1666 __x
._M_get_Tp_allocator());
1670 * Erases all the elements. Note that this function only erases the
1671 * elements, and that if the elements themselves are pointers, the
1672 * pointed-to memory is not touched in any way. Managing the pointer is
1673 * the user's responsibility.
1676 clear() _GLIBCXX_NOEXCEPT
1677 { _M_erase_at_end(begin()); }
1680 // Internal constructor functions follow.
1682 // called by the range constructor to implement [23.1.1]/9
1684 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1685 // 438. Ambiguity in the "do the right thing" clause
1686 template<typename _Integer
>
1688 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1690 _M_initialize_map(static_cast<size_type
>(__n
));
1691 _M_fill_initialize(__x
);
1694 // called by the range constructor to implement [23.1.1]/9
1695 template<typename _InputIterator
>
1697 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1700 typedef typename
std::iterator_traits
<_InputIterator
>::
1701 iterator_category _IterCategory
;
1702 _M_range_initialize(__first
, __last
, _IterCategory());
1705 // called by the second initialize_dispatch above
1708 * @brief Fills the deque with whatever is in [first,last).
1709 * @param __first An input iterator.
1710 * @param __last An input iterator.
1713 * If the iterators are actually forward iterators (or better), then the
1714 * memory layout can be done all at once. Else we move forward using
1715 * push_back on each value from the iterator.
1717 template<typename _InputIterator
>
1719 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1720 std::input_iterator_tag
);
1722 // called by the second initialize_dispatch above
1723 template<typename _ForwardIterator
>
1725 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1726 std::forward_iterator_tag
);
1730 * @brief Fills the %deque with copies of value.
1731 * @param __value Initial value.
1733 * @pre _M_start and _M_finish have already been initialized,
1734 * but none of the %deque's elements have yet been constructed.
1736 * This function is called only when the user provides an explicit size
1737 * (with or without an explicit exemplar value).
1740 _M_fill_initialize(const value_type
& __value
);
1742 #if __cplusplus >= 201103L
1743 // called by deque(n).
1745 _M_default_initialize();
1748 // Internal assign functions follow. The *_aux functions do the actual
1749 // assignment work for the range versions.
1751 // called by the range assign to implement [23.1.1]/9
1753 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1754 // 438. Ambiguity in the "do the right thing" clause
1755 template<typename _Integer
>
1757 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1758 { _M_fill_assign(__n
, __val
); }
1760 // called by the range assign to implement [23.1.1]/9
1761 template<typename _InputIterator
>
1763 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1766 typedef typename
std::iterator_traits
<_InputIterator
>::
1767 iterator_category _IterCategory
;
1768 _M_assign_aux(__first
, __last
, _IterCategory());
1771 // called by the second assign_dispatch above
1772 template<typename _InputIterator
>
1774 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1775 std::input_iterator_tag
);
1777 // called by the second assign_dispatch above
1778 template<typename _ForwardIterator
>
1780 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1781 std::forward_iterator_tag
)
1783 const size_type __len
= std::distance(__first
, __last
);
1786 _ForwardIterator __mid
= __first
;
1787 std::advance(__mid
, size());
1788 std::copy(__first
, __mid
, begin());
1789 insert(end(), __mid
, __last
);
1792 _M_erase_at_end(std::copy(__first
, __last
, begin()));
1795 // Called by assign(n,t), and the range assign when it turns out
1796 // to be the same thing.
1798 _M_fill_assign(size_type __n
, const value_type
& __val
)
1802 std::fill(begin(), end(), __val
);
1803 insert(end(), __n
- size(), __val
);
1807 _M_erase_at_end(begin() + difference_type(__n
));
1808 std::fill(begin(), end(), __val
);
1813 /// Helper functions for push_* and pop_*.
1814 #if __cplusplus < 201103L
1815 void _M_push_back_aux(const value_type
&);
1817 void _M_push_front_aux(const value_type
&);
1819 template<typename
... _Args
>
1820 void _M_push_back_aux(_Args
&&... __args
);
1822 template<typename
... _Args
>
1823 void _M_push_front_aux(_Args
&&... __args
);
1826 void _M_pop_back_aux();
1828 void _M_pop_front_aux();
1831 // Internal insert functions follow. The *_aux functions do the actual
1832 // insertion work when all shortcuts fail.
1834 // called by the range insert to implement [23.1.1]/9
1836 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1837 // 438. Ambiguity in the "do the right thing" clause
1838 template<typename _Integer
>
1840 _M_insert_dispatch(iterator __pos
,
1841 _Integer __n
, _Integer __x
, __true_type
)
1842 { _M_fill_insert(__pos
, __n
, __x
); }
1844 // called by the range insert to implement [23.1.1]/9
1845 template<typename _InputIterator
>
1847 _M_insert_dispatch(iterator __pos
,
1848 _InputIterator __first
, _InputIterator __last
,
1851 typedef typename
std::iterator_traits
<_InputIterator
>::
1852 iterator_category _IterCategory
;
1853 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1856 // called by the second insert_dispatch above
1857 template<typename _InputIterator
>
1859 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1860 _InputIterator __last
, std::input_iterator_tag
);
1862 // called by the second insert_dispatch above
1863 template<typename _ForwardIterator
>
1865 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1866 _ForwardIterator __last
, std::forward_iterator_tag
);
1868 // Called by insert(p,n,x), and the range insert when it turns out to be
1869 // the same thing. Can use fill functions in optimal situations,
1870 // otherwise passes off to insert_aux(p,n,x).
1872 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1874 // called by insert(p,x)
1875 #if __cplusplus < 201103L
1877 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1879 template<typename
... _Args
>
1881 _M_insert_aux(iterator __pos
, _Args
&&... __args
);
1884 // called by insert(p,n,x) via fill_insert
1886 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1888 // called by range_insert_aux for forward iterators
1889 template<typename _ForwardIterator
>
1891 _M_insert_aux(iterator __pos
,
1892 _ForwardIterator __first
, _ForwardIterator __last
,
1896 // Internal erase functions follow.
1899 _M_destroy_data_aux(iterator __first
, iterator __last
);
1901 // Called by ~deque().
1902 // NB: Doesn't deallocate the nodes.
1903 template<typename _Alloc1
>
1905 _M_destroy_data(iterator __first
, iterator __last
, const _Alloc1
&)
1906 { _M_destroy_data_aux(__first
, __last
); }
1909 _M_destroy_data(iterator __first
, iterator __last
,
1910 const std::allocator
<_Tp
>&)
1912 if (!__has_trivial_destructor(value_type
))
1913 _M_destroy_data_aux(__first
, __last
);
1916 // Called by erase(q1, q2).
1918 _M_erase_at_begin(iterator __pos
)
1920 _M_destroy_data(begin(), __pos
, _M_get_Tp_allocator());
1921 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, __pos
._M_node
);
1922 this->_M_impl
._M_start
= __pos
;
1925 // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1926 // _M_fill_assign, operator=.
1928 _M_erase_at_end(iterator __pos
)
1930 _M_destroy_data(__pos
, end(), _M_get_Tp_allocator());
1931 _M_destroy_nodes(__pos
._M_node
+ 1,
1932 this->_M_impl
._M_finish
._M_node
+ 1);
1933 this->_M_impl
._M_finish
= __pos
;
1937 _M_erase(iterator __pos
);
1940 _M_erase(iterator __first
, iterator __last
);
1942 #if __cplusplus >= 201103L
1943 // Called by resize(sz).
1945 _M_default_append(size_type __n
);
1952 /// Memory-handling helpers for the previous internal insert functions.
1954 _M_reserve_elements_at_front(size_type __n
)
1956 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1957 - this->_M_impl
._M_start
._M_first
;
1958 if (__n
> __vacancies
)
1959 _M_new_elements_at_front(__n
- __vacancies
);
1960 return this->_M_impl
._M_start
- difference_type(__n
);
1964 _M_reserve_elements_at_back(size_type __n
)
1966 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1967 - this->_M_impl
._M_finish
._M_cur
) - 1;
1968 if (__n
> __vacancies
)
1969 _M_new_elements_at_back(__n
- __vacancies
);
1970 return this->_M_impl
._M_finish
+ difference_type(__n
);
1974 _M_new_elements_at_front(size_type __new_elements
);
1977 _M_new_elements_at_back(size_type __new_elements
);
1983 * @brief Memory-handling helpers for the major %map.
1985 * Makes sure the _M_map has space for new nodes. Does not
1986 * actually add the nodes. Can invalidate _M_map pointers.
1987 * (And consequently, %deque iterators.)
1990 _M_reserve_map_at_back(size_type __nodes_to_add
= 1)
1992 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1993 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
1994 _M_reallocate_map(__nodes_to_add
, false);
1998 _M_reserve_map_at_front(size_type __nodes_to_add
= 1)
2000 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
2001 - this->_M_impl
._M_map
))
2002 _M_reallocate_map(__nodes_to_add
, true);
2006 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
2012 * @brief Deque equality comparison.
2013 * @param __x A %deque.
2014 * @param __y A %deque of the same type as @a __x.
2015 * @return True iff the size and elements of the deques are equal.
2017 * This is an equivalence relation. It is linear in the size of the
2018 * deques. Deques are considered equivalent if their sizes are equal,
2019 * and if corresponding elements compare equal.
2021 template<typename _Tp
, typename _Alloc
>
2023 operator==(const deque
<_Tp
, _Alloc
>& __x
,
2024 const deque
<_Tp
, _Alloc
>& __y
)
2025 { return __x
.size() == __y
.size()
2026 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
2029 * @brief Deque ordering relation.
2030 * @param __x A %deque.
2031 * @param __y A %deque of the same type as @a __x.
2032 * @return True iff @a x is lexicographically less than @a __y.
2034 * This is a total ordering relation. It is linear in the size of the
2035 * deques. The elements must be comparable with @c <.
2037 * See std::lexicographical_compare() for how the determination is made.
2039 template<typename _Tp
, typename _Alloc
>
2041 operator<(const deque
<_Tp
, _Alloc
>& __x
,
2042 const deque
<_Tp
, _Alloc
>& __y
)
2043 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2044 __y
.begin(), __y
.end()); }
2046 /// Based on operator==
2047 template<typename _Tp
, typename _Alloc
>
2049 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
2050 const deque
<_Tp
, _Alloc
>& __y
)
2051 { return !(__x
== __y
); }
2053 /// Based on operator<
2054 template<typename _Tp
, typename _Alloc
>
2056 operator>(const deque
<_Tp
, _Alloc
>& __x
,
2057 const deque
<_Tp
, _Alloc
>& __y
)
2058 { return __y
< __x
; }
2060 /// Based on operator<
2061 template<typename _Tp
, typename _Alloc
>
2063 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
2064 const deque
<_Tp
, _Alloc
>& __y
)
2065 { return !(__y
< __x
); }
2067 /// Based on operator<
2068 template<typename _Tp
, typename _Alloc
>
2070 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
2071 const deque
<_Tp
, _Alloc
>& __y
)
2072 { return !(__x
< __y
); }
2074 /// See std::deque::swap().
2075 template<typename _Tp
, typename _Alloc
>
2077 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
2080 #undef _GLIBCXX_DEQUE_BUF_SIZE
2082 _GLIBCXX_END_NAMESPACE_CONTAINER
2085 #endif /* _STL_DEQUE_H */