1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2017 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.
39 * Copyright (c) 1996,1997
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_list.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{list}
59 #include <bits/concept_check.h>
60 #include <ext/alloc_traits.h>
61 #if __cplusplus >= 201103L
62 #include <initializer_list>
63 #include <bits/allocated_ptr.h>
64 #include <ext/aligned_buffer.h>
67 namespace std
_GLIBCXX_VISIBILITY(default)
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 // Supporting structures are split into common and templated
74 // types; the latter publicly inherits from the former in an
75 // effort to reduce code duplication. This results in some
76 // "needless" static_cast'ing later on, but it's all safe
79 /// Common part of a node in the %list.
80 struct _List_node_base
82 _List_node_base
* _M_next
;
83 _List_node_base
* _M_prev
;
86 swap(_List_node_base
& __x
, _List_node_base
& __y
) _GLIBCXX_USE_NOEXCEPT
;
89 _M_transfer(_List_node_base
* const __first
,
90 _List_node_base
* const __last
) _GLIBCXX_USE_NOEXCEPT
;
93 _M_reverse() _GLIBCXX_USE_NOEXCEPT
;
96 _M_hook(_List_node_base
* const __position
) _GLIBCXX_USE_NOEXCEPT
;
99 _M_unhook() _GLIBCXX_USE_NOEXCEPT
;
102 /// The %list node header.
103 struct _List_node_header
: public _List_node_base
105 #if _GLIBCXX_USE_CXX11_ABI
109 _List_node_header() _GLIBCXX_NOEXCEPT
112 #if __cplusplus >= 201103L
113 _List_node_header(_List_node_header
&& __x
) noexcept
114 : _List_node_base
{ __x
._M_next
, __x
._M_prev
}
115 # if _GLIBCXX_USE_CXX11_ABI
116 , _M_size(__x
._M_size
)
119 if (__x
._M_base()->_M_next
== __x
._M_base())
120 this->_M_next
= this->_M_prev
= this;
123 this->_M_next
->_M_prev
= this->_M_prev
->_M_next
= this->_M_base();
129 _M_move_nodes(_List_node_header
&& __x
)
131 _List_node_base
* const __xnode
= __x
._M_base();
132 if (__xnode
->_M_next
== __xnode
)
136 _List_node_base
* const __node
= this->_M_base();
137 __node
->_M_next
= __xnode
->_M_next
;
138 __node
->_M_prev
= __xnode
->_M_prev
;
139 __node
->_M_next
->_M_prev
= __node
->_M_prev
->_M_next
= __node
;
140 # if _GLIBCXX_USE_CXX11_ABI
141 _M_size
= __x
._M_size
;
149 _M_init() _GLIBCXX_NOEXCEPT
151 this->_M_next
= this->_M_prev
= this;
152 #if _GLIBCXX_USE_CXX11_ABI
158 _List_node_base
* _M_base() { return this; }
161 _GLIBCXX_END_NAMESPACE_VERSION
162 } // namespace detail
164 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
166 /// An actual node in the %list.
167 template<typename _Tp
>
168 struct _List_node
: public __detail::_List_node_base
170 #if __cplusplus >= 201103L
171 __gnu_cxx::__aligned_membuf
<_Tp
> _M_storage
;
172 _Tp
* _M_valptr() { return _M_storage
._M_ptr(); }
173 _Tp
const* _M_valptr() const { return _M_storage
._M_ptr(); }
176 _Tp
* _M_valptr() { return std::__addressof(_M_data
); }
177 _Tp
const* _M_valptr() const { return std::__addressof(_M_data
); }
182 * @brief A list::iterator.
184 * All the functions are op overloads.
186 template<typename _Tp
>
187 struct _List_iterator
189 typedef _List_iterator
<_Tp
> _Self
;
190 typedef _List_node
<_Tp
> _Node
;
192 typedef ptrdiff_t difference_type
;
193 typedef std::bidirectional_iterator_tag iterator_category
;
194 typedef _Tp value_type
;
195 typedef _Tp
* pointer
;
196 typedef _Tp
& reference
;
198 _List_iterator() _GLIBCXX_NOEXCEPT
202 _List_iterator(__detail::_List_node_base
* __x
) _GLIBCXX_NOEXCEPT
206 _M_const_cast() const _GLIBCXX_NOEXCEPT
209 // Must downcast from _List_node_base to _List_node to get to value.
211 operator*() const _GLIBCXX_NOEXCEPT
212 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
215 operator->() const _GLIBCXX_NOEXCEPT
216 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
219 operator++() _GLIBCXX_NOEXCEPT
221 _M_node
= _M_node
->_M_next
;
226 operator++(int) _GLIBCXX_NOEXCEPT
229 _M_node
= _M_node
->_M_next
;
234 operator--() _GLIBCXX_NOEXCEPT
236 _M_node
= _M_node
->_M_prev
;
241 operator--(int) _GLIBCXX_NOEXCEPT
244 _M_node
= _M_node
->_M_prev
;
249 operator==(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
250 { return _M_node
== __x
._M_node
; }
253 operator!=(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
254 { return _M_node
!= __x
._M_node
; }
256 // The only member points to the %list element.
257 __detail::_List_node_base
* _M_node
;
261 * @brief A list::const_iterator.
263 * All the functions are op overloads.
265 template<typename _Tp
>
266 struct _List_const_iterator
268 typedef _List_const_iterator
<_Tp
> _Self
;
269 typedef const _List_node
<_Tp
> _Node
;
270 typedef _List_iterator
<_Tp
> iterator
;
272 typedef ptrdiff_t difference_type
;
273 typedef std::bidirectional_iterator_tag iterator_category
;
274 typedef _Tp value_type
;
275 typedef const _Tp
* pointer
;
276 typedef const _Tp
& reference
;
278 _List_const_iterator() _GLIBCXX_NOEXCEPT
282 _List_const_iterator(const __detail::_List_node_base
* __x
)
286 _List_const_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
287 : _M_node(__x
._M_node
) { }
290 _M_const_cast() const _GLIBCXX_NOEXCEPT
291 { return iterator(const_cast<__detail::_List_node_base
*>(_M_node
)); }
293 // Must downcast from List_node_base to _List_node to get to value.
295 operator*() const _GLIBCXX_NOEXCEPT
296 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
299 operator->() const _GLIBCXX_NOEXCEPT
300 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
303 operator++() _GLIBCXX_NOEXCEPT
305 _M_node
= _M_node
->_M_next
;
310 operator++(int) _GLIBCXX_NOEXCEPT
313 _M_node
= _M_node
->_M_next
;
318 operator--() _GLIBCXX_NOEXCEPT
320 _M_node
= _M_node
->_M_prev
;
325 operator--(int) _GLIBCXX_NOEXCEPT
328 _M_node
= _M_node
->_M_prev
;
333 operator==(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
334 { return _M_node
== __x
._M_node
; }
337 operator!=(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
338 { return _M_node
!= __x
._M_node
; }
340 // The only member points to the %list element.
341 const __detail::_List_node_base
* _M_node
;
344 template<typename _Val
>
346 operator==(const _List_iterator
<_Val
>& __x
,
347 const _List_const_iterator
<_Val
>& __y
) _GLIBCXX_NOEXCEPT
348 { return __x
._M_node
== __y
._M_node
; }
350 template<typename _Val
>
352 operator!=(const _List_iterator
<_Val
>& __x
,
353 const _List_const_iterator
<_Val
>& __y
) _GLIBCXX_NOEXCEPT
354 { return __x
._M_node
!= __y
._M_node
; }
356 _GLIBCXX_BEGIN_NAMESPACE_CXX11
357 /// See bits/stl_deque.h's _Deque_base for an explanation.
358 template<typename _Tp
, typename _Alloc
>
362 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
363 rebind
<_Tp
>::other _Tp_alloc_type
;
364 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tp_alloc_traits
;
365 typedef typename
_Tp_alloc_traits::template
366 rebind
<_List_node
<_Tp
> >::other _Node_alloc_type
;
367 typedef __gnu_cxx::__alloc_traits
<_Node_alloc_type
> _Node_alloc_traits
;
370 _S_distance(const __detail::_List_node_base
* __first
,
371 const __detail::_List_node_base
* __last
)
374 while (__first
!= __last
)
376 __first
= __first
->_M_next
;
383 : public _Node_alloc_type
385 __detail::_List_node_header _M_node
;
387 _List_impl() _GLIBCXX_NOEXCEPT_IF( noexcept(_Node_alloc_type()) )
391 _List_impl(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
392 : _Node_alloc_type(__a
)
395 #if __cplusplus >= 201103L
396 _List_impl(_List_impl
&&) = default;
398 _List_impl(_Node_alloc_type
&& __a
) noexcept
399 : _Node_alloc_type(std::move(__a
))
406 #if _GLIBCXX_USE_CXX11_ABI
407 size_t _M_get_size() const { return _M_impl
._M_node
._M_size
; }
409 void _M_set_size(size_t __n
) { _M_impl
._M_node
._M_size
= __n
; }
411 void _M_inc_size(size_t __n
) { _M_impl
._M_node
._M_size
+= __n
; }
413 void _M_dec_size(size_t __n
) { _M_impl
._M_node
._M_size
-= __n
; }
416 _M_distance(const __detail::_List_node_base
* __first
,
417 const __detail::_List_node_base
* __last
) const
418 { return _S_distance(__first
, __last
); }
420 // return the stored size
421 size_t _M_node_count() const { return _M_get_size(); }
423 // dummy implementations used when the size is not stored
424 size_t _M_get_size() const { return 0; }
425 void _M_set_size(size_t) { }
426 void _M_inc_size(size_t) { }
427 void _M_dec_size(size_t) { }
428 size_t _M_distance(const void*, const void*) const { return 0; }
430 // count the number of nodes
431 size_t _M_node_count() const
433 return _S_distance(_M_impl
._M_node
._M_next
,
434 std::__addressof(_M_impl
._M_node
));
438 typename
_Node_alloc_traits::pointer
440 { return _Node_alloc_traits::allocate(_M_impl
, 1); }
443 _M_put_node(typename
_Node_alloc_traits::pointer __p
) _GLIBCXX_NOEXCEPT
444 { _Node_alloc_traits::deallocate(_M_impl
, __p
, 1); }
447 typedef _Alloc allocator_type
;
450 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
453 const _Node_alloc_type
&
454 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
457 #if __cplusplus >= 201103L
458 _List_base() = default;
463 _List_base(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
467 #if __cplusplus >= 201103L
468 _List_base(_List_base
&&) = default;
470 _List_base(_List_base
&& __x
, _Node_alloc_type
&& __a
)
471 : _M_impl(std::move(__a
))
473 if (__x
._M_get_Node_allocator() == _M_get_Node_allocator())
474 _M_move_nodes(std::move(__x
));
475 // else caller must move individual elements.
479 _M_move_nodes(_List_base
&& __x
)
480 { _M_impl
._M_node
._M_move_nodes(std::move(__x
._M_impl
._M_node
)); }
483 // This is what actually destroys the list.
484 ~_List_base() _GLIBCXX_NOEXCEPT
488 _M_clear() _GLIBCXX_NOEXCEPT
;
491 _M_init() _GLIBCXX_NOEXCEPT
492 { this->_M_impl
._M_node
._M_init(); }
496 * @brief A standard container with linear time access to elements,
497 * and fixed time insertion/deletion at any point in the sequence.
501 * @tparam _Tp Type of element.
502 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
504 * Meets the requirements of a <a href="tables.html#65">container</a>, a
505 * <a href="tables.html#66">reversible container</a>, and a
506 * <a href="tables.html#67">sequence</a>, including the
507 * <a href="tables.html#68">optional sequence requirements</a> with the
508 * %exception of @c at and @c operator[].
510 * This is a @e doubly @e linked %list. Traversal up and down the
511 * %list requires linear time, but adding and removing elements (or
512 * @e nodes) is done in constant time, regardless of where the
513 * change takes place. Unlike std::vector and std::deque,
514 * random-access iterators are not provided, so subscripting ( @c
515 * [] ) access is not allowed. For algorithms which only need
516 * sequential access, this lack makes no difference.
518 * Also unlike the other standard containers, std::list provides
519 * specialized algorithms %unique to linked lists, such as
520 * splicing, sorting, and in-place reversal.
522 * A couple points on memory allocation for list<Tp>:
524 * First, we never actually allocate a Tp, we allocate
525 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
526 * that after elements from %list<X,Alloc1> are spliced into
527 * %list<X,Alloc2>, destroying the memory of the second %list is a
528 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
530 * Second, a %list conceptually represented as
532 * A <---> B <---> C <---> D
534 * is actually circular; a link exists between A and D. The %list
535 * class holds (as its only data member) a private list::iterator
536 * pointing to @e D, not to @e A! To get to the head of the %list,
537 * we start at the tail and move forward by one. When this member
538 * iterator's next/previous pointers refer to itself, the %list is
541 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
542 class list
: protected _List_base
<_Tp
, _Alloc
>
544 #ifdef _GLIBCXX_CONCEPT_CHECKS
545 // concept requirements
546 typedef typename
_Alloc::value_type _Alloc_value_type
;
547 # if __cplusplus < 201103L
548 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
550 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
553 typedef _List_base
<_Tp
, _Alloc
> _Base
;
554 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
555 typedef typename
_Base::_Tp_alloc_traits _Tp_alloc_traits
;
556 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
557 typedef typename
_Base::_Node_alloc_traits _Node_alloc_traits
;
560 typedef _Tp value_type
;
561 typedef typename
_Tp_alloc_traits::pointer pointer
;
562 typedef typename
_Tp_alloc_traits::const_pointer const_pointer
;
563 typedef typename
_Tp_alloc_traits::reference reference
;
564 typedef typename
_Tp_alloc_traits::const_reference const_reference
;
565 typedef _List_iterator
<_Tp
> iterator
;
566 typedef _List_const_iterator
<_Tp
> const_iterator
;
567 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
568 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
569 typedef size_t size_type
;
570 typedef ptrdiff_t difference_type
;
571 typedef _Alloc allocator_type
;
574 // Note that pointers-to-_Node's can be ctor-converted to
576 typedef _List_node
<_Tp
> _Node
;
578 using _Base::_M_impl
;
579 using _Base::_M_put_node
;
580 using _Base::_M_get_node
;
581 using _Base::_M_get_Node_allocator
;
584 * @param __args An instance of user data.
586 * Allocates space for a new node and constructs a copy of
589 #if __cplusplus < 201103L
591 _M_create_node(const value_type
& __x
)
593 _Node
* __p
= this->_M_get_node();
596 _Tp_alloc_type
__alloc(_M_get_Node_allocator());
597 __alloc
.construct(__p
->_M_valptr(), __x
);
602 __throw_exception_again
;
607 template<typename
... _Args
>
609 _M_create_node(_Args
&&... __args
)
611 auto __p
= this->_M_get_node();
612 auto& __alloc
= _M_get_Node_allocator();
613 __allocated_ptr
<_Node_alloc_type
> __guard
{__alloc
, __p
};
614 _Node_alloc_traits::construct(__alloc
, __p
->_M_valptr(),
615 std::forward
<_Args
>(__args
)...);
622 // [23.2.2.1] construct/copy/destroy
623 // (assign() and get_allocator() are also listed in this section)
626 * @brief Creates a %list with no elements.
628 #if __cplusplus >= 201103L
635 * @brief Creates a %list with no elements.
636 * @param __a An allocator object.
639 list(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
640 : _Base(_Node_alloc_type(__a
)) { }
642 #if __cplusplus >= 201103L
644 * @brief Creates a %list with default constructed elements.
645 * @param __n The number of elements to initially create.
646 * @param __a An allocator object.
648 * This constructor fills the %list with @a __n default
649 * constructed elements.
652 list(size_type __n
, const allocator_type
& __a
= allocator_type())
653 : _Base(_Node_alloc_type(__a
))
654 { _M_default_initialize(__n
); }
657 * @brief Creates a %list with copies of an exemplar element.
658 * @param __n The number of elements to initially create.
659 * @param __value An element to copy.
660 * @param __a An allocator object.
662 * This constructor fills the %list with @a __n copies of @a __value.
664 list(size_type __n
, const value_type
& __value
,
665 const allocator_type
& __a
= allocator_type())
666 : _Base(_Node_alloc_type(__a
))
667 { _M_fill_initialize(__n
, __value
); }
670 * @brief Creates a %list with copies of an exemplar element.
671 * @param __n The number of elements to initially create.
672 * @param __value An element to copy.
673 * @param __a An allocator object.
675 * This constructor fills the %list with @a __n copies of @a __value.
678 list(size_type __n
, const value_type
& __value
= value_type(),
679 const allocator_type
& __a
= allocator_type())
680 : _Base(_Node_alloc_type(__a
))
681 { _M_fill_initialize(__n
, __value
); }
685 * @brief %List copy constructor.
686 * @param __x A %list of identical element and allocator types.
688 * The newly-created %list uses a copy of the allocation object used
689 * by @a __x (unless the allocator traits dictate a different object).
691 list(const list
& __x
)
692 : _Base(_Node_alloc_traits::
693 _S_select_on_copy(__x
._M_get_Node_allocator()))
694 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
696 #if __cplusplus >= 201103L
698 * @brief %List move constructor.
700 * The newly-created %list contains the exact contents of the moved
701 * instance. The contents of the moved instance are a valid, but
704 list(list
&&) = default;
707 * @brief Builds a %list from an initializer_list
708 * @param __l An initializer_list of value_type.
709 * @param __a An allocator object.
711 * Create a %list consisting of copies of the elements in the
712 * initializer_list @a __l. This is linear in __l.size().
714 list(initializer_list
<value_type
> __l
,
715 const allocator_type
& __a
= allocator_type())
716 : _Base(_Node_alloc_type(__a
))
717 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
719 list(const list
& __x
, const allocator_type
& __a
)
720 : _Base(_Node_alloc_type(__a
))
721 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
723 list(list
&& __x
, const allocator_type
& __a
)
724 noexcept(_Node_alloc_traits::_S_always_equal())
725 : _Base(std::move(__x
), _Node_alloc_type(__a
))
727 // If __x is not empty it means its allocator is not equal to __a,
728 // so we need to move from each element individually.
729 insert(begin(), std::__make_move_if_noexcept_iterator(__x
.begin()),
730 std::__make_move_if_noexcept_iterator(__x
.end()));
735 * @brief Builds a %list from a range.
736 * @param __first An input iterator.
737 * @param __last An input iterator.
738 * @param __a An allocator object.
740 * Create a %list consisting of copies of the elements from
741 * [@a __first,@a __last). This is linear in N (where N is
742 * distance(@a __first,@a __last)).
744 #if __cplusplus >= 201103L
745 template<typename _InputIterator
,
746 typename
= std::_RequireInputIter
<_InputIterator
>>
747 list(_InputIterator __first
, _InputIterator __last
,
748 const allocator_type
& __a
= allocator_type())
749 : _Base(_Node_alloc_type(__a
))
750 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
752 template<typename _InputIterator
>
753 list(_InputIterator __first
, _InputIterator __last
,
754 const allocator_type
& __a
= allocator_type())
755 : _Base(_Node_alloc_type(__a
))
757 // Check whether it's an integral type. If so, it's not an iterator.
758 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
759 _M_initialize_dispatch(__first
, __last
, _Integral());
763 #if __cplusplus >= 201103L
765 * No explicit dtor needed as the _Base dtor takes care of
766 * things. The _Base dtor only erases the elements, and note
767 * that if the elements themselves are pointers, the pointed-to
768 * memory is not touched in any way. Managing the pointer is
769 * the user's responsibility.
775 * @brief %List assignment operator.
776 * @param __x A %list of identical element and allocator types.
778 * All the elements of @a __x are copied.
780 * Whether the allocator is copied depends on the allocator traits.
783 operator=(const list
& __x
);
785 #if __cplusplus >= 201103L
787 * @brief %List move assignment operator.
788 * @param __x A %list of identical element and allocator types.
790 * The contents of @a __x are moved into this %list (without copying).
792 * Afterwards @a __x is a valid, but unspecified %list
794 * Whether the allocator is moved depends on the allocator traits.
797 operator=(list
&& __x
)
798 noexcept(_Node_alloc_traits::_S_nothrow_move())
800 constexpr bool __move_storage
=
801 _Node_alloc_traits::_S_propagate_on_move_assign()
802 || _Node_alloc_traits::_S_always_equal();
803 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
808 * @brief %List initializer list assignment operator.
809 * @param __l An initializer_list of value_type.
811 * Replace the contents of the %list with copies of the elements
812 * in the initializer_list @a __l. This is linear in l.size().
815 operator=(initializer_list
<value_type
> __l
)
817 this->assign(__l
.begin(), __l
.end());
823 * @brief Assigns a given value to a %list.
824 * @param __n Number of elements to be assigned.
825 * @param __val Value to be assigned.
827 * This function fills a %list with @a __n copies of the given
828 * value. Note that the assignment completely changes the %list
829 * and that the resulting %list's size is the same as the number
830 * of elements assigned.
833 assign(size_type __n
, const value_type
& __val
)
834 { _M_fill_assign(__n
, __val
); }
837 * @brief Assigns a range to a %list.
838 * @param __first An input iterator.
839 * @param __last An input iterator.
841 * This function fills a %list with copies of the elements in the
842 * range [@a __first,@a __last).
844 * Note that the assignment completely changes the %list and
845 * that the resulting %list's size is the same as the number of
848 #if __cplusplus >= 201103L
849 template<typename _InputIterator
,
850 typename
= std::_RequireInputIter
<_InputIterator
>>
852 assign(_InputIterator __first
, _InputIterator __last
)
853 { _M_assign_dispatch(__first
, __last
, __false_type()); }
855 template<typename _InputIterator
>
857 assign(_InputIterator __first
, _InputIterator __last
)
859 // Check whether it's an integral type. If so, it's not an iterator.
860 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
861 _M_assign_dispatch(__first
, __last
, _Integral());
865 #if __cplusplus >= 201103L
867 * @brief Assigns an initializer_list to a %list.
868 * @param __l An initializer_list of value_type.
870 * Replace the contents of the %list with copies of the elements
871 * in the initializer_list @a __l. This is linear in __l.size().
874 assign(initializer_list
<value_type
> __l
)
875 { this->_M_assign_dispatch(__l
.begin(), __l
.end(), __false_type()); }
878 /// Get a copy of the memory allocation object.
880 get_allocator() const _GLIBCXX_NOEXCEPT
881 { return allocator_type(_Base::_M_get_Node_allocator()); }
885 * Returns a read/write iterator that points to the first element in the
886 * %list. Iteration is done in ordinary element order.
889 begin() _GLIBCXX_NOEXCEPT
890 { return iterator(this->_M_impl
._M_node
._M_next
); }
893 * Returns a read-only (constant) iterator that points to the
894 * first element in the %list. Iteration is done in ordinary
898 begin() const _GLIBCXX_NOEXCEPT
899 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
902 * Returns a read/write iterator that points one past the last
903 * element in the %list. Iteration is done in ordinary element
907 end() _GLIBCXX_NOEXCEPT
908 { return iterator(&this->_M_impl
._M_node
); }
911 * Returns a read-only (constant) iterator that points one past
912 * the last element in the %list. Iteration is done in ordinary
916 end() const _GLIBCXX_NOEXCEPT
917 { return const_iterator(&this->_M_impl
._M_node
); }
920 * Returns a read/write reverse iterator that points to the last
921 * element in the %list. Iteration is done in reverse element
925 rbegin() _GLIBCXX_NOEXCEPT
926 { return reverse_iterator(end()); }
929 * Returns a read-only (constant) reverse iterator that points to
930 * the last element in the %list. Iteration is done in reverse
933 const_reverse_iterator
934 rbegin() const _GLIBCXX_NOEXCEPT
935 { return const_reverse_iterator(end()); }
938 * Returns a read/write reverse iterator that points to one
939 * before the first element in the %list. Iteration is done in
940 * reverse element order.
943 rend() _GLIBCXX_NOEXCEPT
944 { return reverse_iterator(begin()); }
947 * Returns a read-only (constant) reverse iterator that points to one
948 * before the first element in the %list. Iteration is done in reverse
951 const_reverse_iterator
952 rend() const _GLIBCXX_NOEXCEPT
953 { return const_reverse_iterator(begin()); }
955 #if __cplusplus >= 201103L
957 * Returns a read-only (constant) iterator that points to the
958 * first element in the %list. Iteration is done in ordinary
962 cbegin() const noexcept
963 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
966 * Returns a read-only (constant) iterator that points one past
967 * the last element in the %list. Iteration is done in ordinary
971 cend() const noexcept
972 { return const_iterator(&this->_M_impl
._M_node
); }
975 * Returns a read-only (constant) reverse iterator that points to
976 * the last element in the %list. Iteration is done in reverse
979 const_reverse_iterator
980 crbegin() const noexcept
981 { return const_reverse_iterator(end()); }
984 * Returns a read-only (constant) reverse iterator that points to one
985 * before the first element in the %list. Iteration is done in reverse
988 const_reverse_iterator
989 crend() const noexcept
990 { return const_reverse_iterator(begin()); }
993 // [23.2.2.2] capacity
995 * Returns true if the %list is empty. (Thus begin() would equal
999 empty() const _GLIBCXX_NOEXCEPT
1000 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
1002 /** Returns the number of elements in the %list. */
1004 size() const _GLIBCXX_NOEXCEPT
1005 { return this->_M_node_count(); }
1007 /** Returns the size() of the largest possible %list. */
1009 max_size() const _GLIBCXX_NOEXCEPT
1010 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1012 #if __cplusplus >= 201103L
1014 * @brief Resizes the %list to the specified number of elements.
1015 * @param __new_size Number of elements the %list should contain.
1017 * This function will %resize the %list to the specified number
1018 * of elements. If the number is smaller than the %list's
1019 * current size the %list is truncated, otherwise default
1020 * constructed elements are appended.
1023 resize(size_type __new_size
);
1026 * @brief Resizes the %list to the specified number of elements.
1027 * @param __new_size Number of elements the %list should contain.
1028 * @param __x Data with which new elements should be populated.
1030 * This function will %resize the %list to the specified number
1031 * of elements. If the number is smaller than the %list's
1032 * current size the %list is truncated, otherwise the %list is
1033 * extended and new elements are populated with given data.
1036 resize(size_type __new_size
, const value_type
& __x
);
1039 * @brief Resizes the %list to the specified number of elements.
1040 * @param __new_size Number of elements the %list should contain.
1041 * @param __x Data with which new elements should be populated.
1043 * This function will %resize the %list to the specified number
1044 * of elements. If the number is smaller than the %list's
1045 * current size the %list is truncated, otherwise the %list is
1046 * extended and new elements are populated with given data.
1049 resize(size_type __new_size
, value_type __x
= value_type());
1054 * Returns a read/write reference to the data at the first
1055 * element of the %list.
1058 front() _GLIBCXX_NOEXCEPT
1059 { return *begin(); }
1062 * Returns a read-only (constant) reference to the data at the first
1063 * element of the %list.
1066 front() const _GLIBCXX_NOEXCEPT
1067 { return *begin(); }
1070 * Returns a read/write reference to the data at the last element
1074 back() _GLIBCXX_NOEXCEPT
1076 iterator __tmp
= end();
1082 * Returns a read-only (constant) reference to the data at the last
1083 * element of the %list.
1086 back() const _GLIBCXX_NOEXCEPT
1088 const_iterator __tmp
= end();
1093 // [23.2.2.3] modifiers
1095 * @brief Add data to the front of the %list.
1096 * @param __x Data to be added.
1098 * This is a typical stack operation. The function creates an
1099 * element at the front of the %list and assigns the given data
1100 * to it. Due to the nature of a %list this operation can be
1101 * done in constant time, and does not invalidate iterators and
1105 push_front(const value_type
& __x
)
1106 { this->_M_insert(begin(), __x
); }
1108 #if __cplusplus >= 201103L
1110 push_front(value_type
&& __x
)
1111 { this->_M_insert(begin(), std::move(__x
)); }
1113 template<typename
... _Args
>
1114 #if __cplusplus > 201402L
1119 emplace_front(_Args
&&... __args
)
1121 this->_M_insert(begin(), std::forward
<_Args
>(__args
)...);
1122 #if __cplusplus > 201402L
1129 * @brief Removes first element.
1131 * This is a typical stack operation. It shrinks the %list by
1132 * one. Due to the nature of a %list this operation can be done
1133 * in constant time, and only invalidates iterators/references to
1134 * the element being removed.
1136 * Note that no data is returned, and if the first element's data
1137 * is needed, it should be retrieved before pop_front() is
1141 pop_front() _GLIBCXX_NOEXCEPT
1142 { this->_M_erase(begin()); }
1145 * @brief Add data to the end of the %list.
1146 * @param __x Data to be added.
1148 * This is a typical stack operation. The function creates an
1149 * element at the end of the %list and assigns the given data to
1150 * it. Due to the nature of a %list this operation can be done
1151 * in constant time, and does not invalidate iterators and
1155 push_back(const value_type
& __x
)
1156 { this->_M_insert(end(), __x
); }
1158 #if __cplusplus >= 201103L
1160 push_back(value_type
&& __x
)
1161 { this->_M_insert(end(), std::move(__x
)); }
1163 template<typename
... _Args
>
1164 #if __cplusplus > 201402L
1169 emplace_back(_Args
&&... __args
)
1171 this->_M_insert(end(), std::forward
<_Args
>(__args
)...);
1172 #if __cplusplus > 201402L
1179 * @brief Removes last element.
1181 * This is a typical stack operation. It shrinks the %list by
1182 * one. Due to the nature of a %list this operation can be done
1183 * in constant time, and only invalidates iterators/references to
1184 * the element being removed.
1186 * Note that no data is returned, and if the last element's data
1187 * is needed, it should be retrieved before pop_back() is called.
1190 pop_back() _GLIBCXX_NOEXCEPT
1191 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1193 #if __cplusplus >= 201103L
1195 * @brief Constructs object in %list before specified iterator.
1196 * @param __position A const_iterator into the %list.
1197 * @param __args Arguments.
1198 * @return An iterator that points to the inserted data.
1200 * This function will insert an object of type T constructed
1201 * with T(std::forward<Args>(args)...) before the specified
1202 * location. Due to the nature of a %list this operation can
1203 * be done in constant time, and does not invalidate iterators
1206 template<typename
... _Args
>
1208 emplace(const_iterator __position
, _Args
&&... __args
);
1211 * @brief Inserts given value into %list before specified iterator.
1212 * @param __position A const_iterator into the %list.
1213 * @param __x Data to be inserted.
1214 * @return An iterator that points to the inserted data.
1216 * This function will insert a copy of the given value before
1217 * the specified location. Due to the nature of a %list this
1218 * operation can be done in constant time, and does not
1219 * invalidate iterators and references.
1222 insert(const_iterator __position
, const value_type
& __x
);
1225 * @brief Inserts given value into %list before specified iterator.
1226 * @param __position An iterator into the %list.
1227 * @param __x Data to be inserted.
1228 * @return An iterator that points to the inserted data.
1230 * This function will insert a copy of the given value before
1231 * the specified location. Due to the nature of a %list this
1232 * operation can be done in constant time, and does not
1233 * invalidate iterators and references.
1236 insert(iterator __position
, const value_type
& __x
);
1239 #if __cplusplus >= 201103L
1241 * @brief Inserts given rvalue into %list before specified iterator.
1242 * @param __position A const_iterator into the %list.
1243 * @param __x Data to be inserted.
1244 * @return An iterator that points to the inserted data.
1246 * This function will insert a copy of the given rvalue before
1247 * the specified location. Due to the nature of a %list this
1248 * operation can be done in constant time, and does not
1249 * invalidate iterators and references.
1252 insert(const_iterator __position
, value_type
&& __x
)
1253 { return emplace(__position
, std::move(__x
)); }
1256 * @brief Inserts the contents of an initializer_list into %list
1257 * before specified const_iterator.
1258 * @param __p A const_iterator into the %list.
1259 * @param __l An initializer_list of value_type.
1260 * @return An iterator pointing to the first element inserted
1263 * This function will insert copies of the data in the
1264 * initializer_list @a l into the %list before the location
1265 * specified by @a p.
1267 * This operation is linear in the number of elements inserted and
1268 * does not invalidate iterators and references.
1271 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1272 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1275 #if __cplusplus >= 201103L
1277 * @brief Inserts a number of copies of given data into the %list.
1278 * @param __position A const_iterator into the %list.
1279 * @param __n Number of elements to be inserted.
1280 * @param __x Data to be inserted.
1281 * @return An iterator pointing to the first element inserted
1284 * This function will insert a specified number of copies of the
1285 * given data before the location specified by @a position.
1287 * This operation is linear in the number of elements inserted and
1288 * does not invalidate iterators and references.
1291 insert(const_iterator __position
, size_type __n
, const value_type
& __x
);
1294 * @brief Inserts a number of copies of given data into the %list.
1295 * @param __position An iterator into the %list.
1296 * @param __n Number of elements to be inserted.
1297 * @param __x Data to be inserted.
1299 * This function will insert a specified number of copies of the
1300 * given data before the location specified by @a position.
1302 * This operation is linear in the number of elements inserted and
1303 * does not invalidate iterators and references.
1306 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1308 list
__tmp(__n
, __x
, get_allocator());
1309 splice(__position
, __tmp
);
1313 #if __cplusplus >= 201103L
1315 * @brief Inserts a range into the %list.
1316 * @param __position A const_iterator into the %list.
1317 * @param __first An input iterator.
1318 * @param __last An input iterator.
1319 * @return An iterator pointing to the first element inserted
1322 * This function will insert copies of the data in the range [@a
1323 * first,@a last) into the %list before the location specified by
1326 * This operation is linear in the number of elements inserted and
1327 * does not invalidate iterators and references.
1329 template<typename _InputIterator
,
1330 typename
= std::_RequireInputIter
<_InputIterator
>>
1332 insert(const_iterator __position
, _InputIterator __first
,
1333 _InputIterator __last
);
1336 * @brief Inserts a range into the %list.
1337 * @param __position An iterator into the %list.
1338 * @param __first An input iterator.
1339 * @param __last An input iterator.
1341 * This function will insert copies of the data in the range [@a
1342 * first,@a last) into the %list before the location specified by
1345 * This operation is linear in the number of elements inserted and
1346 * does not invalidate iterators and references.
1348 template<typename _InputIterator
>
1350 insert(iterator __position
, _InputIterator __first
,
1351 _InputIterator __last
)
1353 list
__tmp(__first
, __last
, get_allocator());
1354 splice(__position
, __tmp
);
1359 * @brief Remove element at given position.
1360 * @param __position Iterator pointing to element to be erased.
1361 * @return An iterator pointing to the next element (or end()).
1363 * This function will erase the element at the given position and thus
1364 * shorten the %list by one.
1366 * Due to the nature of a %list this operation can be done in
1367 * constant time, and only invalidates iterators/references to
1368 * the element being removed. The user is also cautioned that
1369 * this function only erases the element, and that if the element
1370 * is itself a pointer, the pointed-to memory is not touched in
1371 * any way. Managing the pointer is the user's responsibility.
1374 #if __cplusplus >= 201103L
1375 erase(const_iterator __position
) noexcept
;
1377 erase(iterator __position
);
1381 * @brief Remove a range of elements.
1382 * @param __first Iterator pointing to the first element to be erased.
1383 * @param __last Iterator pointing to one past the last element to be
1385 * @return An iterator pointing to the element pointed to by @a last
1386 * prior to erasing (or end()).
1388 * This function will erase the elements in the range @a
1389 * [first,last) and shorten the %list accordingly.
1391 * This operation is linear time in the size of the range and only
1392 * invalidates iterators/references to the element being removed.
1393 * The user is also cautioned that this function only erases the
1394 * elements, and that if the elements themselves are pointers, the
1395 * pointed-to memory is not touched in any way. Managing the pointer
1396 * is the user's responsibility.
1399 #if __cplusplus >= 201103L
1400 erase(const_iterator __first
, const_iterator __last
) noexcept
1402 erase(iterator __first
, iterator __last
)
1405 while (__first
!= __last
)
1406 __first
= erase(__first
);
1407 return __last
._M_const_cast();
1411 * @brief Swaps data with another %list.
1412 * @param __x A %list of the same element and allocator types.
1414 * This exchanges the elements between two lists in constant
1415 * time. Note that the global std::swap() function is
1416 * specialized such that std::swap(l1,l2) will feed to this
1419 * Whether the allocators are swapped depends on the allocator traits.
1422 swap(list
& __x
) _GLIBCXX_NOEXCEPT
1424 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1425 __x
._M_impl
._M_node
);
1427 size_t __xsize
= __x
._M_get_size();
1428 __x
._M_set_size(this->_M_get_size());
1429 this->_M_set_size(__xsize
);
1431 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1432 __x
._M_get_Node_allocator());
1436 * Erases all the elements. Note that this function only erases
1437 * the elements, and that if the elements themselves are
1438 * pointers, the pointed-to memory is not touched in any way.
1439 * Managing the pointer is the user's responsibility.
1442 clear() _GLIBCXX_NOEXCEPT
1448 // [23.2.2.4] list operations
1450 * @brief Insert contents of another %list.
1451 * @param __position Iterator referencing the element to insert before.
1452 * @param __x Source list.
1454 * The elements of @a __x are inserted in constant time in front of
1455 * the element referenced by @a __position. @a __x becomes an empty
1458 * Requires this != @a __x.
1461 #if __cplusplus >= 201103L
1462 splice(const_iterator __position
, list
&& __x
) noexcept
1464 splice(iterator __position
, list
& __x
)
1469 _M_check_equal_allocators(__x
);
1471 this->_M_transfer(__position
._M_const_cast(),
1472 __x
.begin(), __x
.end());
1474 this->_M_inc_size(__x
._M_get_size());
1479 #if __cplusplus >= 201103L
1481 splice(const_iterator __position
, list
& __x
) noexcept
1482 { splice(__position
, std::move(__x
)); }
1485 #if __cplusplus >= 201103L
1487 * @brief Insert element from another %list.
1488 * @param __position Const_iterator referencing the element to
1490 * @param __x Source list.
1491 * @param __i Const_iterator referencing the element to move.
1493 * Removes the element in list @a __x referenced by @a __i and
1494 * inserts it into the current list before @a __position.
1497 splice(const_iterator __position
, list
&& __x
, const_iterator __i
) noexcept
1500 * @brief Insert element from another %list.
1501 * @param __position Iterator referencing the element to insert before.
1502 * @param __x Source list.
1503 * @param __i Iterator referencing the element to move.
1505 * Removes the element in list @a __x referenced by @a __i and
1506 * inserts it into the current list before @a __position.
1509 splice(iterator __position
, list
& __x
, iterator __i
)
1512 iterator __j
= __i
._M_const_cast();
1514 if (__position
== __i
|| __position
== __j
)
1517 if (this != std::__addressof(__x
))
1518 _M_check_equal_allocators(__x
);
1520 this->_M_transfer(__position
._M_const_cast(),
1521 __i
._M_const_cast(), __j
);
1523 this->_M_inc_size(1);
1527 #if __cplusplus >= 201103L
1529 * @brief Insert element from another %list.
1530 * @param __position Const_iterator referencing the element to
1532 * @param __x Source list.
1533 * @param __i Const_iterator referencing the element to move.
1535 * Removes the element in list @a __x referenced by @a __i and
1536 * inserts it into the current list before @a __position.
1539 splice(const_iterator __position
, list
& __x
, const_iterator __i
) noexcept
1540 { splice(__position
, std::move(__x
), __i
); }
1543 #if __cplusplus >= 201103L
1545 * @brief Insert range from another %list.
1546 * @param __position Const_iterator referencing the element to
1548 * @param __x Source list.
1549 * @param __first Const_iterator referencing the start of range in x.
1550 * @param __last Const_iterator referencing the end of range in x.
1552 * Removes elements in the range [__first,__last) and inserts them
1553 * before @a __position in constant time.
1555 * Undefined if @a __position is in [__first,__last).
1558 splice(const_iterator __position
, list
&& __x
, const_iterator __first
,
1559 const_iterator __last
) noexcept
1562 * @brief Insert range from another %list.
1563 * @param __position Iterator referencing the element to insert before.
1564 * @param __x Source list.
1565 * @param __first Iterator referencing the start of range in x.
1566 * @param __last Iterator referencing the end of range in x.
1568 * Removes elements in the range [__first,__last) and inserts them
1569 * before @a __position in constant time.
1571 * Undefined if @a __position is in [__first,__last).
1574 splice(iterator __position
, list
& __x
, iterator __first
,
1578 if (__first
!= __last
)
1580 if (this != std::__addressof(__x
))
1581 _M_check_equal_allocators(__x
);
1583 size_t __n
= this->_M_distance(__first
._M_node
, __last
._M_node
);
1584 this->_M_inc_size(__n
);
1585 __x
._M_dec_size(__n
);
1587 this->_M_transfer(__position
._M_const_cast(),
1588 __first
._M_const_cast(),
1589 __last
._M_const_cast());
1593 #if __cplusplus >= 201103L
1595 * @brief Insert range from another %list.
1596 * @param __position Const_iterator referencing the element to
1598 * @param __x Source list.
1599 * @param __first Const_iterator referencing the start of range in x.
1600 * @param __last Const_iterator referencing the end of range in x.
1602 * Removes elements in the range [__first,__last) and inserts them
1603 * before @a __position in constant time.
1605 * Undefined if @a __position is in [__first,__last).
1608 splice(const_iterator __position
, list
& __x
, const_iterator __first
,
1609 const_iterator __last
) noexcept
1610 { splice(__position
, std::move(__x
), __first
, __last
); }
1614 * @brief Remove all elements equal to value.
1615 * @param __value The value to remove.
1617 * Removes every element in the list equal to @a value.
1618 * Remaining elements stay in list order. Note that this
1619 * function only erases the elements, and that if the elements
1620 * themselves are pointers, the pointed-to memory is not
1621 * touched in any way. Managing the pointer is the user's
1625 remove(const _Tp
& __value
);
1628 * @brief Remove all elements satisfying a predicate.
1629 * @tparam _Predicate Unary predicate function or object.
1631 * Removes every element in the list for which the predicate
1632 * returns true. Remaining elements stay in list order. Note
1633 * that this function only erases the elements, and that if the
1634 * elements themselves are pointers, the pointed-to memory is
1635 * not touched in any way. Managing the pointer is the user's
1638 template<typename _Predicate
>
1640 remove_if(_Predicate
);
1643 * @brief Remove consecutive duplicate elements.
1645 * For each consecutive set of elements with the same value,
1646 * remove all but the first one. Remaining elements stay in
1647 * list order. Note that this function only erases the
1648 * elements, and that if the elements themselves are pointers,
1649 * the pointed-to memory is not touched in any way. Managing
1650 * the pointer is the user's responsibility.
1656 * @brief Remove consecutive elements satisfying a predicate.
1657 * @tparam _BinaryPredicate Binary predicate function or object.
1659 * For each consecutive set of elements [first,last) that
1660 * satisfy predicate(first,i) where i is an iterator in
1661 * [first,last), remove all but the first one. Remaining
1662 * elements stay in list order. Note that this function only
1663 * erases the elements, and that if the elements themselves are
1664 * pointers, the pointed-to memory is not touched in any way.
1665 * Managing the pointer is the user's responsibility.
1667 template<typename _BinaryPredicate
>
1669 unique(_BinaryPredicate
);
1672 * @brief Merge sorted lists.
1673 * @param __x Sorted list to merge.
1675 * Assumes that both @a __x and this list are sorted according to
1676 * operator<(). Merges elements of @a __x into this list in
1677 * sorted order, leaving @a __x empty when complete. Elements in
1678 * this list precede elements in @a __x that are equal.
1680 #if __cplusplus >= 201103L
1686 { merge(std::move(__x
)); }
1693 * @brief Merge sorted lists according to comparison function.
1694 * @tparam _StrictWeakOrdering Comparison function defining
1696 * @param __x Sorted list to merge.
1697 * @param __comp Comparison functor.
1699 * Assumes that both @a __x and this list are sorted according to
1700 * StrictWeakOrdering. Merges elements of @a __x into this list
1701 * in sorted order, leaving @a __x empty when complete. Elements
1702 * in this list precede elements in @a __x that are equivalent
1703 * according to StrictWeakOrdering().
1705 #if __cplusplus >= 201103L
1706 template<typename _StrictWeakOrdering
>
1708 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1710 template<typename _StrictWeakOrdering
>
1712 merge(list
& __x
, _StrictWeakOrdering __comp
)
1713 { merge(std::move(__x
), __comp
); }
1715 template<typename _StrictWeakOrdering
>
1717 merge(list
& __x
, _StrictWeakOrdering __comp
);
1721 * @brief Reverse the elements in list.
1723 * Reverse the order of elements in the list in linear time.
1726 reverse() _GLIBCXX_NOEXCEPT
1727 { this->_M_impl
._M_node
._M_reverse(); }
1730 * @brief Sort the elements.
1732 * Sorts the elements of this list in NlogN time. Equivalent
1733 * elements remain in list order.
1739 * @brief Sort the elements according to comparison function.
1741 * Sorts the elements of this list in NlogN time. Equivalent
1742 * elements remain in list order.
1744 template<typename _StrictWeakOrdering
>
1746 sort(_StrictWeakOrdering
);
1749 // Internal constructor functions follow.
1751 // Called by the range constructor 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_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1758 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1760 // Called by the range constructor to implement [23.1.1]/9
1761 template<typename _InputIterator
>
1763 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1766 for (; __first
!= __last
; ++__first
)
1767 #if __cplusplus >= 201103L
1768 emplace_back(*__first
);
1770 push_back(*__first
);
1774 // Called by list(n,v,a), and the range constructor when it turns out
1775 // to be the same thing.
1777 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1783 #if __cplusplus >= 201103L
1784 // Called by list(n).
1786 _M_default_initialize(size_type __n
)
1792 // Called by resize(sz).
1794 _M_default_append(size_type __n
);
1797 // Internal assign functions follow.
1799 // Called by the range assign to implement [23.1.1]/9
1801 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1802 // 438. Ambiguity in the "do the right thing" clause
1803 template<typename _Integer
>
1805 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1806 { _M_fill_assign(__n
, __val
); }
1808 // Called by the range assign to implement [23.1.1]/9
1809 template<typename _InputIterator
>
1811 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1814 // Called by assign(n,t), and the range assign when it turns out
1815 // to be the same thing.
1817 _M_fill_assign(size_type __n
, const value_type
& __val
);
1820 // Moves the elements from [first,last) before position.
1822 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1823 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1825 // Inserts new element at position given and with value given.
1826 #if __cplusplus < 201103L
1828 _M_insert(iterator __position
, const value_type
& __x
)
1830 _Node
* __tmp
= _M_create_node(__x
);
1831 __tmp
->_M_hook(__position
._M_node
);
1832 this->_M_inc_size(1);
1835 template<typename
... _Args
>
1837 _M_insert(iterator __position
, _Args
&&... __args
)
1839 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1840 __tmp
->_M_hook(__position
._M_node
);
1841 this->_M_inc_size(1);
1845 // Erases element at position given.
1847 _M_erase(iterator __position
) _GLIBCXX_NOEXCEPT
1849 this->_M_dec_size(1);
1850 __position
._M_node
->_M_unhook();
1851 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1852 #if __cplusplus >= 201103L
1853 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n
->_M_valptr());
1855 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n
->_M_valptr());
1861 // To implement the splice (and merge) bits of N1599.
1863 _M_check_equal_allocators(list
& __x
) _GLIBCXX_NOEXCEPT
1865 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1866 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1870 // Used to implement resize.
1872 _M_resize_pos(size_type
& __new_size
) const;
1874 #if __cplusplus >= 201103L
1876 _M_move_assign(list
&& __x
, true_type
) noexcept
1879 this->_M_move_nodes(std::move(__x
));
1880 std::__alloc_on_move(this->_M_get_Node_allocator(),
1881 __x
._M_get_Node_allocator());
1885 _M_move_assign(list
&& __x
, false_type
)
1887 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1888 _M_move_assign(std::move(__x
), true_type
{});
1890 // The rvalue's allocator cannot be moved, or is not equal,
1891 // so we need to individually move each element.
1892 _M_assign_dispatch(std::__make_move_if_noexcept_iterator(__x
.begin()),
1893 std::__make_move_if_noexcept_iterator(__x
.end()),
1899 #if __cpp_deduction_guides >= 201606
1900 template<typename _InputIterator
, typename _ValT
1901 = typename iterator_traits
<_InputIterator
>::value_type
,
1902 typename _Allocator
= allocator
<_ValT
>,
1903 typename
= _RequireInputIter
<_InputIterator
>,
1904 typename
= _RequireAllocator
<_Allocator
>>
1905 list(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1906 -> list
<_ValT
, _Allocator
>;
1909 _GLIBCXX_END_NAMESPACE_CXX11
1912 * @brief List equality comparison.
1913 * @param __x A %list.
1914 * @param __y A %list of the same type as @a __x.
1915 * @return True iff the size and elements of the lists are equal.
1917 * This is an equivalence relation. It is linear in the size of
1918 * the lists. Lists are considered equivalent if their sizes are
1919 * equal, and if corresponding elements compare equal.
1921 template<typename _Tp
, typename _Alloc
>
1923 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1925 #if _GLIBCXX_USE_CXX11_ABI
1926 if (__x
.size() != __y
.size())
1930 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1931 const_iterator __end1
= __x
.end();
1932 const_iterator __end2
= __y
.end();
1934 const_iterator __i1
= __x
.begin();
1935 const_iterator __i2
= __y
.begin();
1936 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1941 return __i1
== __end1
&& __i2
== __end2
;
1945 * @brief List ordering relation.
1946 * @param __x A %list.
1947 * @param __y A %list of the same type as @a __x.
1948 * @return True iff @a __x is lexicographically less than @a __y.
1950 * This is a total ordering relation. It is linear in the size of the
1951 * lists. The elements must be comparable with @c <.
1953 * See std::lexicographical_compare() for how the determination is made.
1955 template<typename _Tp
, typename _Alloc
>
1957 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1958 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1959 __y
.begin(), __y
.end()); }
1961 /// Based on operator==
1962 template<typename _Tp
, typename _Alloc
>
1964 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1965 { return !(__x
== __y
); }
1967 /// Based on operator<
1968 template<typename _Tp
, typename _Alloc
>
1970 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1971 { return __y
< __x
; }
1973 /// Based on operator<
1974 template<typename _Tp
, typename _Alloc
>
1976 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1977 { return !(__y
< __x
); }
1979 /// Based on operator<
1980 template<typename _Tp
, typename _Alloc
>
1982 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1983 { return !(__x
< __y
); }
1985 /// See std::list::swap().
1986 template<typename _Tp
, typename _Alloc
>
1988 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1989 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1992 _GLIBCXX_END_NAMESPACE_CONTAINER
1994 #if _GLIBCXX_USE_CXX11_ABI
1995 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1997 // Detect when distance is used to compute the size of the whole list.
1998 template<typename _Tp
>
2000 __distance(_GLIBCXX_STD_C::_List_iterator
<_Tp
> __first
,
2001 _GLIBCXX_STD_C::_List_iterator
<_Tp
> __last
,
2002 input_iterator_tag __tag
)
2004 typedef _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> _CIter
;
2005 return std::__distance(_CIter(__first
), _CIter(__last
), __tag
);
2008 template<typename _Tp
>
2010 __distance(_GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __first
,
2011 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __last
,
2014 typedef __detail::_List_node_header _Sentinel
;
2015 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __beyond
= __last
;
2017 const bool __whole
= __first
== __beyond
;
2018 if (__builtin_constant_p (__whole
) && __whole
)
2019 return static_cast<const _Sentinel
*>(__last
._M_node
)->_M_size
;
2022 while (__first
!= __last
)
2030 _GLIBCXX_END_NAMESPACE_VERSION
2034 #endif /* _STL_LIST_H */