1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2020 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)
69 _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; }
160 } // namespace detail
162 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
164 /// An actual node in the %list.
165 template<typename _Tp
>
166 struct _List_node
: public __detail::_List_node_base
168 #if __cplusplus >= 201103L
169 __gnu_cxx::__aligned_membuf
<_Tp
> _M_storage
;
170 _Tp
* _M_valptr() { return _M_storage
._M_ptr(); }
171 _Tp
const* _M_valptr() const { return _M_storage
._M_ptr(); }
174 _Tp
* _M_valptr() { return std::__addressof(_M_data
); }
175 _Tp
const* _M_valptr() const { return std::__addressof(_M_data
); }
180 * @brief A list::iterator.
182 * All the functions are op overloads.
184 template<typename _Tp
>
185 struct _List_iterator
187 typedef _List_iterator
<_Tp
> _Self
;
188 typedef _List_node
<_Tp
> _Node
;
190 typedef ptrdiff_t difference_type
;
191 typedef std::bidirectional_iterator_tag iterator_category
;
192 typedef _Tp value_type
;
193 typedef _Tp
* pointer
;
194 typedef _Tp
& reference
;
196 _List_iterator() _GLIBCXX_NOEXCEPT
200 _List_iterator(__detail::_List_node_base
* __x
) _GLIBCXX_NOEXCEPT
204 _M_const_cast() const _GLIBCXX_NOEXCEPT
207 // Must downcast from _List_node_base to _List_node to get to value.
209 operator*() const _GLIBCXX_NOEXCEPT
210 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
213 operator->() const _GLIBCXX_NOEXCEPT
214 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
217 operator++() _GLIBCXX_NOEXCEPT
219 _M_node
= _M_node
->_M_next
;
224 operator++(int) _GLIBCXX_NOEXCEPT
227 _M_node
= _M_node
->_M_next
;
232 operator--() _GLIBCXX_NOEXCEPT
234 _M_node
= _M_node
->_M_prev
;
239 operator--(int) _GLIBCXX_NOEXCEPT
242 _M_node
= _M_node
->_M_prev
;
247 operator==(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
248 { return __x
._M_node
== __y
._M_node
; }
250 #if __cpp_impl_three_way_comparison < 201907L
252 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
253 { return __x
._M_node
!= __y
._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 _Self
& __y
) _GLIBCXX_NOEXCEPT
334 { return __x
._M_node
== __y
._M_node
; }
336 #if __cpp_impl_three_way_comparison < 201907L
338 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
339 { return __x
._M_node
!= __y
._M_node
; }
342 // The only member points to the %list element.
343 const __detail::_List_node_base
* _M_node
;
346 _GLIBCXX_BEGIN_NAMESPACE_CXX11
347 /// See bits/stl_deque.h's _Deque_base for an explanation.
348 template<typename _Tp
, typename _Alloc
>
352 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
353 rebind
<_Tp
>::other _Tp_alloc_type
;
354 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tp_alloc_traits
;
355 typedef typename
_Tp_alloc_traits::template
356 rebind
<_List_node
<_Tp
> >::other _Node_alloc_type
;
357 typedef __gnu_cxx::__alloc_traits
<_Node_alloc_type
> _Node_alloc_traits
;
359 #if !_GLIBCXX_INLINE_VERSION
361 _S_distance(const __detail::_List_node_base
* __first
,
362 const __detail::_List_node_base
* __last
)
365 while (__first
!= __last
)
367 __first
= __first
->_M_next
;
375 : public _Node_alloc_type
377 __detail::_List_node_header _M_node
;
379 _List_impl() _GLIBCXX_NOEXCEPT_IF(
380 is_nothrow_default_constructible
<_Node_alloc_type
>::value
)
384 _List_impl(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
385 : _Node_alloc_type(__a
)
388 #if __cplusplus >= 201103L
389 _List_impl(_List_impl
&&) = default;
391 _List_impl(_Node_alloc_type
&& __a
, _List_impl
&& __x
)
392 : _Node_alloc_type(std::move(__a
)), _M_node(std::move(__x
._M_node
))
395 _List_impl(_Node_alloc_type
&& __a
) noexcept
396 : _Node_alloc_type(std::move(__a
))
403 #if _GLIBCXX_USE_CXX11_ABI
404 size_t _M_get_size() const { return _M_impl
._M_node
._M_size
; }
406 void _M_set_size(size_t __n
) { _M_impl
._M_node
._M_size
= __n
; }
408 void _M_inc_size(size_t __n
) { _M_impl
._M_node
._M_size
+= __n
; }
410 void _M_dec_size(size_t __n
) { _M_impl
._M_node
._M_size
-= __n
; }
412 # if !_GLIBCXX_INLINE_VERSION
414 _M_distance(const __detail::_List_node_base
* __first
,
415 const __detail::_List_node_base
* __last
) const
416 { return _S_distance(__first
, __last
); }
418 // return the stored size
419 size_t _M_node_count() const { return _M_get_size(); }
422 // dummy implementations used when the size is not stored
423 size_t _M_get_size() const { return 0; }
424 void _M_set_size(size_t) { }
425 void _M_inc_size(size_t) { }
426 void _M_dec_size(size_t) { }
428 # if !_GLIBCXX_INLINE_VERSION
429 size_t _M_distance(const void*, const void*) const { return 0; }
431 // count the number of nodes
432 size_t _M_node_count() const
434 return _S_distance(_M_impl
._M_node
._M_next
,
435 std::__addressof(_M_impl
._M_node
));
440 typename
_Node_alloc_traits::pointer
442 { return _Node_alloc_traits::allocate(_M_impl
, 1); }
445 _M_put_node(typename
_Node_alloc_traits::pointer __p
) _GLIBCXX_NOEXCEPT
446 { _Node_alloc_traits::deallocate(_M_impl
, __p
, 1); }
449 typedef _Alloc allocator_type
;
452 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
455 const _Node_alloc_type
&
456 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
459 #if __cplusplus >= 201103L
460 _List_base() = default;
465 _List_base(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
469 #if __cplusplus >= 201103L
470 _List_base(_List_base
&&) = default;
472 # if !_GLIBCXX_INLINE_VERSION
473 _List_base(_List_base
&& __x
, _Node_alloc_type
&& __a
)
474 : _M_impl(std::move(__a
))
476 if (__x
._M_get_Node_allocator() == _M_get_Node_allocator())
477 _M_move_nodes(std::move(__x
));
478 // else caller must move individual elements.
482 // Used when allocator is_always_equal.
483 _List_base(_Node_alloc_type
&& __a
, _List_base
&& __x
)
484 : _M_impl(std::move(__a
), std::move(__x
._M_impl
))
487 // Used when allocator !is_always_equal.
488 _List_base(_Node_alloc_type
&& __a
)
489 : _M_impl(std::move(__a
))
493 _M_move_nodes(_List_base
&& __x
)
494 { _M_impl
._M_node
._M_move_nodes(std::move(__x
._M_impl
._M_node
)); }
497 // This is what actually destroys the list.
498 ~_List_base() _GLIBCXX_NOEXCEPT
502 _M_clear() _GLIBCXX_NOEXCEPT
;
505 _M_init() _GLIBCXX_NOEXCEPT
506 { this->_M_impl
._M_node
._M_init(); }
510 * @brief A standard container with linear time access to elements,
511 * and fixed time insertion/deletion at any point in the sequence.
515 * @tparam _Tp Type of element.
516 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
518 * Meets the requirements of a <a href="tables.html#65">container</a>, a
519 * <a href="tables.html#66">reversible container</a>, and a
520 * <a href="tables.html#67">sequence</a>, including the
521 * <a href="tables.html#68">optional sequence requirements</a> with the
522 * %exception of @c at and @c operator[].
524 * This is a @e doubly @e linked %list. Traversal up and down the
525 * %list requires linear time, but adding and removing elements (or
526 * @e nodes) is done in constant time, regardless of where the
527 * change takes place. Unlike std::vector and std::deque,
528 * random-access iterators are not provided, so subscripting ( @c
529 * [] ) access is not allowed. For algorithms which only need
530 * sequential access, this lack makes no difference.
532 * Also unlike the other standard containers, std::list provides
533 * specialized algorithms %unique to linked lists, such as
534 * splicing, sorting, and in-place reversal.
536 * A couple points on memory allocation for list<Tp>:
538 * First, we never actually allocate a Tp, we allocate
539 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
540 * that after elements from %list<X,Alloc1> are spliced into
541 * %list<X,Alloc2>, destroying the memory of the second %list is a
542 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
544 * Second, a %list conceptually represented as
546 * A <---> B <---> C <---> D
548 * is actually circular; a link exists between A and D. The %list
549 * class holds (as its only data member) a private list::iterator
550 * pointing to @e D, not to @e A! To get to the head of the %list,
551 * we start at the tail and move forward by one. When this member
552 * iterator's next/previous pointers refer to itself, the %list is
555 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
556 class list
: protected _List_base
<_Tp
, _Alloc
>
558 #ifdef _GLIBCXX_CONCEPT_CHECKS
559 // concept requirements
560 typedef typename
_Alloc::value_type _Alloc_value_type
;
561 # if __cplusplus < 201103L
562 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
564 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
567 #if __cplusplus >= 201103L
568 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
569 "std::list must have a non-const, non-volatile value_type");
570 # if __cplusplus > 201703L || defined __STRICT_ANSI__
571 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
572 "std::list must have the same value_type as its allocator");
576 typedef _List_base
<_Tp
, _Alloc
> _Base
;
577 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
578 typedef typename
_Base::_Tp_alloc_traits _Tp_alloc_traits
;
579 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
580 typedef typename
_Base::_Node_alloc_traits _Node_alloc_traits
;
583 typedef _Tp value_type
;
584 typedef typename
_Tp_alloc_traits::pointer pointer
;
585 typedef typename
_Tp_alloc_traits::const_pointer const_pointer
;
586 typedef typename
_Tp_alloc_traits::reference reference
;
587 typedef typename
_Tp_alloc_traits::const_reference const_reference
;
588 typedef _List_iterator
<_Tp
> iterator
;
589 typedef _List_const_iterator
<_Tp
> const_iterator
;
590 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
591 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
592 typedef size_t size_type
;
593 typedef ptrdiff_t difference_type
;
594 typedef _Alloc allocator_type
;
597 // Note that pointers-to-_Node's can be ctor-converted to
599 typedef _List_node
<_Tp
> _Node
;
601 using _Base::_M_impl
;
602 using _Base::_M_put_node
;
603 using _Base::_M_get_node
;
604 using _Base::_M_get_Node_allocator
;
607 * @param __args An instance of user data.
609 * Allocates space for a new node and constructs a copy of
612 #if __cplusplus < 201103L
614 _M_create_node(const value_type
& __x
)
616 _Node
* __p
= this->_M_get_node();
619 _Tp_alloc_type
__alloc(_M_get_Node_allocator());
620 __alloc
.construct(__p
->_M_valptr(), __x
);
625 __throw_exception_again
;
630 template<typename
... _Args
>
632 _M_create_node(_Args
&&... __args
)
634 auto __p
= this->_M_get_node();
635 auto& __alloc
= _M_get_Node_allocator();
636 __allocated_ptr
<_Node_alloc_type
> __guard
{__alloc
, __p
};
637 _Node_alloc_traits::construct(__alloc
, __p
->_M_valptr(),
638 std::forward
<_Args
>(__args
)...);
644 #if _GLIBCXX_USE_CXX11_ABI
646 _S_distance(const_iterator __first
, const_iterator __last
)
647 { return std::distance(__first
, __last
); }
649 // return the stored size
651 _M_node_count() const
652 { return this->_M_get_size(); }
654 // dummy implementations used when the size is not stored
656 _S_distance(const_iterator
, const_iterator
)
659 // count the number of nodes
661 _M_node_count() const
662 { return std::distance(begin(), end()); }
666 // [23.2.2.1] construct/copy/destroy
667 // (assign() and get_allocator() are also listed in this section)
670 * @brief Creates a %list with no elements.
672 #if __cplusplus >= 201103L
679 * @brief Creates a %list with no elements.
680 * @param __a An allocator object.
683 list(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
684 : _Base(_Node_alloc_type(__a
)) { }
686 #if __cplusplus >= 201103L
688 * @brief Creates a %list with default constructed elements.
689 * @param __n The number of elements to initially create.
690 * @param __a An allocator object.
692 * This constructor fills the %list with @a __n default
693 * constructed elements.
696 list(size_type __n
, const allocator_type
& __a
= allocator_type())
697 : _Base(_Node_alloc_type(__a
))
698 { _M_default_initialize(__n
); }
701 * @brief Creates a %list with copies of an exemplar element.
702 * @param __n The number of elements to initially create.
703 * @param __value An element to copy.
704 * @param __a An allocator object.
706 * This constructor fills the %list with @a __n copies of @a __value.
708 list(size_type __n
, const value_type
& __value
,
709 const allocator_type
& __a
= allocator_type())
710 : _Base(_Node_alloc_type(__a
))
711 { _M_fill_initialize(__n
, __value
); }
714 * @brief Creates a %list with copies of an exemplar element.
715 * @param __n The number of elements to initially create.
716 * @param __value An element to copy.
717 * @param __a An allocator object.
719 * This constructor fills the %list with @a __n copies of @a __value.
722 list(size_type __n
, const value_type
& __value
= value_type(),
723 const allocator_type
& __a
= allocator_type())
724 : _Base(_Node_alloc_type(__a
))
725 { _M_fill_initialize(__n
, __value
); }
729 * @brief %List copy constructor.
730 * @param __x A %list of identical element and allocator types.
732 * The newly-created %list uses a copy of the allocation object used
733 * by @a __x (unless the allocator traits dictate a different object).
735 list(const list
& __x
)
736 : _Base(_Node_alloc_traits::
737 _S_select_on_copy(__x
._M_get_Node_allocator()))
738 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
740 #if __cplusplus >= 201103L
742 * @brief %List move constructor.
744 * The newly-created %list contains the exact contents of the moved
745 * instance. The contents of the moved instance are a valid, but
748 list(list
&&) = default;
751 * @brief Builds a %list from an initializer_list
752 * @param __l An initializer_list of value_type.
753 * @param __a An allocator object.
755 * Create a %list consisting of copies of the elements in the
756 * initializer_list @a __l. This is linear in __l.size().
758 list(initializer_list
<value_type
> __l
,
759 const allocator_type
& __a
= allocator_type())
760 : _Base(_Node_alloc_type(__a
))
761 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
763 list(const list
& __x
, const allocator_type
& __a
)
764 : _Base(_Node_alloc_type(__a
))
765 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
768 list(list
&& __x
, const allocator_type
& __a
, true_type
) noexcept
769 : _Base(_Node_alloc_type(__a
), std::move(__x
))
772 list(list
&& __x
, const allocator_type
& __a
, false_type
)
773 : _Base(_Node_alloc_type(__a
))
775 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
776 this->_M_move_nodes(std::move(__x
));
778 insert(begin(), std::__make_move_if_noexcept_iterator(__x
.begin()),
779 std::__make_move_if_noexcept_iterator(__x
.end()));
783 list(list
&& __x
, const allocator_type
& __a
)
784 noexcept(_Node_alloc_traits::_S_always_equal())
785 : list(std::move(__x
), __a
,
786 typename
_Node_alloc_traits::is_always_equal
{})
791 * @brief Builds a %list from a range.
792 * @param __first An input iterator.
793 * @param __last An input iterator.
794 * @param __a An allocator object.
796 * Create a %list consisting of copies of the elements from
797 * [@a __first,@a __last). This is linear in N (where N is
798 * distance(@a __first,@a __last)).
800 #if __cplusplus >= 201103L
801 template<typename _InputIterator
,
802 typename
= std::_RequireInputIter
<_InputIterator
>>
803 list(_InputIterator __first
, _InputIterator __last
,
804 const allocator_type
& __a
= allocator_type())
805 : _Base(_Node_alloc_type(__a
))
806 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
808 template<typename _InputIterator
>
809 list(_InputIterator __first
, _InputIterator __last
,
810 const allocator_type
& __a
= allocator_type())
811 : _Base(_Node_alloc_type(__a
))
813 // Check whether it's an integral type. If so, it's not an iterator.
814 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
815 _M_initialize_dispatch(__first
, __last
, _Integral());
819 #if __cplusplus >= 201103L
821 * No explicit dtor needed as the _Base dtor takes care of
822 * things. The _Base dtor only erases the elements, and note
823 * that if the elements themselves are pointers, the pointed-to
824 * memory is not touched in any way. Managing the pointer is
825 * the user's responsibility.
831 * @brief %List assignment operator.
832 * @param __x A %list of identical element and allocator types.
834 * All the elements of @a __x are copied.
836 * Whether the allocator is copied depends on the allocator traits.
839 operator=(const list
& __x
);
841 #if __cplusplus >= 201103L
843 * @brief %List move assignment operator.
844 * @param __x A %list of identical element and allocator types.
846 * The contents of @a __x are moved into this %list (without copying).
848 * Afterwards @a __x is a valid, but unspecified %list
850 * Whether the allocator is moved depends on the allocator traits.
853 operator=(list
&& __x
)
854 noexcept(_Node_alloc_traits::_S_nothrow_move())
856 constexpr bool __move_storage
=
857 _Node_alloc_traits::_S_propagate_on_move_assign()
858 || _Node_alloc_traits::_S_always_equal();
859 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
864 * @brief %List initializer list assignment operator.
865 * @param __l An initializer_list of value_type.
867 * Replace the contents of the %list with copies of the elements
868 * in the initializer_list @a __l. This is linear in l.size().
871 operator=(initializer_list
<value_type
> __l
)
873 this->assign(__l
.begin(), __l
.end());
879 * @brief Assigns a given value to a %list.
880 * @param __n Number of elements to be assigned.
881 * @param __val Value to be assigned.
883 * This function fills a %list with @a __n copies of the given
884 * value. Note that the assignment completely changes the %list
885 * and that the resulting %list's size is the same as the number
886 * of elements assigned.
889 assign(size_type __n
, const value_type
& __val
)
890 { _M_fill_assign(__n
, __val
); }
893 * @brief Assigns a range to a %list.
894 * @param __first An input iterator.
895 * @param __last An input iterator.
897 * This function fills a %list with copies of the elements in the
898 * range [@a __first,@a __last).
900 * Note that the assignment completely changes the %list and
901 * that the resulting %list's size is the same as the number of
904 #if __cplusplus >= 201103L
905 template<typename _InputIterator
,
906 typename
= std::_RequireInputIter
<_InputIterator
>>
908 assign(_InputIterator __first
, _InputIterator __last
)
909 { _M_assign_dispatch(__first
, __last
, __false_type()); }
911 template<typename _InputIterator
>
913 assign(_InputIterator __first
, _InputIterator __last
)
915 // Check whether it's an integral type. If so, it's not an iterator.
916 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
917 _M_assign_dispatch(__first
, __last
, _Integral());
921 #if __cplusplus >= 201103L
923 * @brief Assigns an initializer_list to a %list.
924 * @param __l An initializer_list of value_type.
926 * Replace the contents of the %list with copies of the elements
927 * in the initializer_list @a __l. This is linear in __l.size().
930 assign(initializer_list
<value_type
> __l
)
931 { this->_M_assign_dispatch(__l
.begin(), __l
.end(), __false_type()); }
934 /// Get a copy of the memory allocation object.
936 get_allocator() const _GLIBCXX_NOEXCEPT
937 { return allocator_type(_Base::_M_get_Node_allocator()); }
941 * Returns a read/write iterator that points to the first element in the
942 * %list. Iteration is done in ordinary element order.
945 begin() _GLIBCXX_NOEXCEPT
946 { return iterator(this->_M_impl
._M_node
._M_next
); }
949 * Returns a read-only (constant) iterator that points to the
950 * first element in the %list. Iteration is done in ordinary
954 begin() const _GLIBCXX_NOEXCEPT
955 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
958 * Returns a read/write iterator that points one past the last
959 * element in the %list. Iteration is done in ordinary element
963 end() _GLIBCXX_NOEXCEPT
964 { return iterator(&this->_M_impl
._M_node
); }
967 * Returns a read-only (constant) iterator that points one past
968 * the last element in the %list. Iteration is done in ordinary
972 end() const _GLIBCXX_NOEXCEPT
973 { return const_iterator(&this->_M_impl
._M_node
); }
976 * Returns a read/write reverse iterator that points to the last
977 * element in the %list. Iteration is done in reverse element
981 rbegin() _GLIBCXX_NOEXCEPT
982 { return reverse_iterator(end()); }
985 * Returns a read-only (constant) reverse iterator that points to
986 * the last element in the %list. Iteration is done in reverse
989 const_reverse_iterator
990 rbegin() const _GLIBCXX_NOEXCEPT
991 { return const_reverse_iterator(end()); }
994 * Returns a read/write reverse iterator that points to one
995 * before the first element in the %list. Iteration is done in
996 * reverse element order.
999 rend() _GLIBCXX_NOEXCEPT
1000 { return reverse_iterator(begin()); }
1003 * Returns a read-only (constant) reverse iterator that points to one
1004 * before the first element in the %list. Iteration is done in reverse
1007 const_reverse_iterator
1008 rend() const _GLIBCXX_NOEXCEPT
1009 { return const_reverse_iterator(begin()); }
1011 #if __cplusplus >= 201103L
1013 * Returns a read-only (constant) iterator that points to the
1014 * first element in the %list. Iteration is done in ordinary
1018 cbegin() const noexcept
1019 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
1022 * Returns a read-only (constant) iterator that points one past
1023 * the last element in the %list. Iteration is done in ordinary
1027 cend() const noexcept
1028 { return const_iterator(&this->_M_impl
._M_node
); }
1031 * Returns a read-only (constant) reverse iterator that points to
1032 * the last element in the %list. Iteration is done in reverse
1035 const_reverse_iterator
1036 crbegin() const noexcept
1037 { return const_reverse_iterator(end()); }
1040 * Returns a read-only (constant) reverse iterator that points to one
1041 * before the first element in the %list. Iteration is done in reverse
1044 const_reverse_iterator
1045 crend() const noexcept
1046 { return const_reverse_iterator(begin()); }
1049 // [23.2.2.2] capacity
1051 * Returns true if the %list is empty. (Thus begin() would equal
1054 _GLIBCXX_NODISCARD
bool
1055 empty() const _GLIBCXX_NOEXCEPT
1056 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
1058 /** Returns the number of elements in the %list. */
1060 size() const _GLIBCXX_NOEXCEPT
1061 { return _M_node_count(); }
1063 /** Returns the size() of the largest possible %list. */
1065 max_size() const _GLIBCXX_NOEXCEPT
1066 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1068 #if __cplusplus >= 201103L
1070 * @brief Resizes the %list to the specified number of elements.
1071 * @param __new_size Number of elements the %list should contain.
1073 * This function will %resize the %list to the specified number
1074 * of elements. If the number is smaller than the %list's
1075 * current size the %list is truncated, otherwise default
1076 * constructed elements are appended.
1079 resize(size_type __new_size
);
1082 * @brief Resizes the %list to the specified number of elements.
1083 * @param __new_size Number of elements the %list should contain.
1084 * @param __x Data with which new elements should be populated.
1086 * This function will %resize the %list to the specified number
1087 * of elements. If the number is smaller than the %list's
1088 * current size the %list is truncated, otherwise the %list is
1089 * extended and new elements are populated with given data.
1092 resize(size_type __new_size
, const value_type
& __x
);
1095 * @brief Resizes the %list to the specified number of elements.
1096 * @param __new_size Number of elements the %list should contain.
1097 * @param __x Data with which new elements should be populated.
1099 * This function will %resize the %list to the specified number
1100 * of elements. If the number is smaller than the %list's
1101 * current size the %list is truncated, otherwise the %list is
1102 * extended and new elements are populated with given data.
1105 resize(size_type __new_size
, value_type __x
= value_type());
1110 * Returns a read/write reference to the data at the first
1111 * element of the %list.
1114 front() _GLIBCXX_NOEXCEPT
1115 { return *begin(); }
1118 * Returns a read-only (constant) reference to the data at the first
1119 * element of the %list.
1122 front() const _GLIBCXX_NOEXCEPT
1123 { return *begin(); }
1126 * Returns a read/write reference to the data at the last element
1130 back() _GLIBCXX_NOEXCEPT
1132 iterator __tmp
= end();
1138 * Returns a read-only (constant) reference to the data at the last
1139 * element of the %list.
1142 back() const _GLIBCXX_NOEXCEPT
1144 const_iterator __tmp
= end();
1149 // [23.2.2.3] modifiers
1151 * @brief Add data to the front of the %list.
1152 * @param __x Data to be added.
1154 * This is a typical stack operation. The function creates an
1155 * element at the front of the %list and assigns the given data
1156 * to it. Due to the nature of a %list this operation can be
1157 * done in constant time, and does not invalidate iterators and
1161 push_front(const value_type
& __x
)
1162 { this->_M_insert(begin(), __x
); }
1164 #if __cplusplus >= 201103L
1166 push_front(value_type
&& __x
)
1167 { this->_M_insert(begin(), std::move(__x
)); }
1169 template<typename
... _Args
>
1170 #if __cplusplus > 201402L
1175 emplace_front(_Args
&&... __args
)
1177 this->_M_insert(begin(), std::forward
<_Args
>(__args
)...);
1178 #if __cplusplus > 201402L
1185 * @brief Removes first element.
1187 * This is a typical stack operation. It shrinks the %list by
1188 * one. Due to the nature of a %list this operation can be done
1189 * in constant time, and only invalidates iterators/references to
1190 * the element being removed.
1192 * Note that no data is returned, and if the first element's data
1193 * is needed, it should be retrieved before pop_front() is
1197 pop_front() _GLIBCXX_NOEXCEPT
1198 { this->_M_erase(begin()); }
1201 * @brief Add data to the end of the %list.
1202 * @param __x Data to be added.
1204 * This is a typical stack operation. The function creates an
1205 * element at the end of the %list and assigns the given data to
1206 * it. Due to the nature of a %list this operation can be done
1207 * in constant time, and does not invalidate iterators and
1211 push_back(const value_type
& __x
)
1212 { this->_M_insert(end(), __x
); }
1214 #if __cplusplus >= 201103L
1216 push_back(value_type
&& __x
)
1217 { this->_M_insert(end(), std::move(__x
)); }
1219 template<typename
... _Args
>
1220 #if __cplusplus > 201402L
1225 emplace_back(_Args
&&... __args
)
1227 this->_M_insert(end(), std::forward
<_Args
>(__args
)...);
1228 #if __cplusplus > 201402L
1235 * @brief Removes last element.
1237 * This is a typical stack operation. It shrinks the %list by
1238 * one. Due to the nature of a %list this operation can be done
1239 * in constant time, and only invalidates iterators/references to
1240 * the element being removed.
1242 * Note that no data is returned, and if the last element's data
1243 * is needed, it should be retrieved before pop_back() is called.
1246 pop_back() _GLIBCXX_NOEXCEPT
1247 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1249 #if __cplusplus >= 201103L
1251 * @brief Constructs object in %list before specified iterator.
1252 * @param __position A const_iterator into the %list.
1253 * @param __args Arguments.
1254 * @return An iterator that points to the inserted data.
1256 * This function will insert an object of type T constructed
1257 * with T(std::forward<Args>(args)...) before the specified
1258 * location. Due to the nature of a %list this operation can
1259 * be done in constant time, and does not invalidate iterators
1262 template<typename
... _Args
>
1264 emplace(const_iterator __position
, _Args
&&... __args
);
1267 * @brief Inserts given value into %list before specified iterator.
1268 * @param __position A const_iterator into the %list.
1269 * @param __x Data to be inserted.
1270 * @return An iterator that points to the inserted data.
1272 * This function will insert a copy of the given value before
1273 * the specified location. Due to the nature of a %list this
1274 * operation can be done in constant time, and does not
1275 * invalidate iterators and references.
1278 insert(const_iterator __position
, const value_type
& __x
);
1281 * @brief Inserts given value into %list before specified iterator.
1282 * @param __position An iterator into the %list.
1283 * @param __x Data to be inserted.
1284 * @return An iterator that points to the inserted data.
1286 * This function will insert a copy of the given value before
1287 * the specified location. Due to the nature of a %list this
1288 * operation can be done in constant time, and does not
1289 * invalidate iterators and references.
1292 insert(iterator __position
, const value_type
& __x
);
1295 #if __cplusplus >= 201103L
1297 * @brief Inserts given rvalue into %list before specified iterator.
1298 * @param __position A const_iterator into the %list.
1299 * @param __x Data to be inserted.
1300 * @return An iterator that points to the inserted data.
1302 * This function will insert a copy of the given rvalue before
1303 * the specified location. Due to the nature of a %list this
1304 * operation can be done in constant time, and does not
1305 * invalidate iterators and references.
1308 insert(const_iterator __position
, value_type
&& __x
)
1309 { return emplace(__position
, std::move(__x
)); }
1312 * @brief Inserts the contents of an initializer_list into %list
1313 * before specified const_iterator.
1314 * @param __p A const_iterator into the %list.
1315 * @param __l An initializer_list of value_type.
1316 * @return An iterator pointing to the first element inserted
1319 * This function will insert copies of the data in the
1320 * initializer_list @a l into the %list before the location
1321 * specified by @a p.
1323 * This operation is linear in the number of elements inserted and
1324 * does not invalidate iterators and references.
1327 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1328 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1331 #if __cplusplus >= 201103L
1333 * @brief Inserts a number of copies of given data into the %list.
1334 * @param __position A const_iterator into the %list.
1335 * @param __n Number of elements to be inserted.
1336 * @param __x Data to be inserted.
1337 * @return An iterator pointing to the first element inserted
1340 * This function will insert a specified number of copies of the
1341 * given data before the location specified by @a position.
1343 * This operation is linear in the number of elements inserted and
1344 * does not invalidate iterators and references.
1347 insert(const_iterator __position
, size_type __n
, const value_type
& __x
);
1350 * @brief Inserts a number of copies of given data into the %list.
1351 * @param __position An iterator into the %list.
1352 * @param __n Number of elements to be inserted.
1353 * @param __x Data to be inserted.
1355 * This function will insert a specified number of copies of the
1356 * given data before the location specified by @a position.
1358 * This operation is linear in the number of elements inserted and
1359 * does not invalidate iterators and references.
1362 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1364 list
__tmp(__n
, __x
, get_allocator());
1365 splice(__position
, __tmp
);
1369 #if __cplusplus >= 201103L
1371 * @brief Inserts a range into the %list.
1372 * @param __position A const_iterator into the %list.
1373 * @param __first An input iterator.
1374 * @param __last An input iterator.
1375 * @return An iterator pointing to the first element inserted
1378 * This function will insert copies of the data in the range [@a
1379 * first,@a last) into the %list before the location specified by
1382 * This operation is linear in the number of elements inserted and
1383 * does not invalidate iterators and references.
1385 template<typename _InputIterator
,
1386 typename
= std::_RequireInputIter
<_InputIterator
>>
1388 insert(const_iterator __position
, _InputIterator __first
,
1389 _InputIterator __last
);
1392 * @brief Inserts a range into the %list.
1393 * @param __position An iterator into the %list.
1394 * @param __first An input iterator.
1395 * @param __last An input iterator.
1397 * This function will insert copies of the data in the range [@a
1398 * first,@a last) into the %list before the location specified by
1401 * This operation is linear in the number of elements inserted and
1402 * does not invalidate iterators and references.
1404 template<typename _InputIterator
>
1406 insert(iterator __position
, _InputIterator __first
,
1407 _InputIterator __last
)
1409 list
__tmp(__first
, __last
, get_allocator());
1410 splice(__position
, __tmp
);
1415 * @brief Remove element at given position.
1416 * @param __position Iterator pointing to element to be erased.
1417 * @return An iterator pointing to the next element (or end()).
1419 * This function will erase the element at the given position and thus
1420 * shorten the %list by one.
1422 * Due to the nature of a %list this operation can be done in
1423 * constant time, and only invalidates iterators/references to
1424 * the element being removed. The user is also cautioned that
1425 * this function only erases the element, and that if the element
1426 * is itself a pointer, the pointed-to memory is not touched in
1427 * any way. Managing the pointer is the user's responsibility.
1430 #if __cplusplus >= 201103L
1431 erase(const_iterator __position
) noexcept
;
1433 erase(iterator __position
);
1437 * @brief Remove a range of elements.
1438 * @param __first Iterator pointing to the first element to be erased.
1439 * @param __last Iterator pointing to one past the last element to be
1441 * @return An iterator pointing to the element pointed to by @a last
1442 * prior to erasing (or end()).
1444 * This function will erase the elements in the range @a
1445 * [first,last) and shorten the %list accordingly.
1447 * This operation is linear time in the size of the range and only
1448 * invalidates iterators/references to the element being removed.
1449 * The user is also cautioned that this function only erases the
1450 * elements, and that if the elements themselves are pointers, the
1451 * pointed-to memory is not touched in any way. Managing the pointer
1452 * is the user's responsibility.
1455 #if __cplusplus >= 201103L
1456 erase(const_iterator __first
, const_iterator __last
) noexcept
1458 erase(iterator __first
, iterator __last
)
1461 while (__first
!= __last
)
1462 __first
= erase(__first
);
1463 return __last
._M_const_cast();
1467 * @brief Swaps data with another %list.
1468 * @param __x A %list of the same element and allocator types.
1470 * This exchanges the elements between two lists in constant
1471 * time. Note that the global std::swap() function is
1472 * specialized such that std::swap(l1,l2) will feed to this
1475 * Whether the allocators are swapped depends on the allocator traits.
1478 swap(list
& __x
) _GLIBCXX_NOEXCEPT
1480 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1481 __x
._M_impl
._M_node
);
1483 size_t __xsize
= __x
._M_get_size();
1484 __x
._M_set_size(this->_M_get_size());
1485 this->_M_set_size(__xsize
);
1487 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1488 __x
._M_get_Node_allocator());
1492 * Erases all the elements. Note that this function only erases
1493 * the elements, and that if the elements themselves are
1494 * pointers, the pointed-to memory is not touched in any way.
1495 * Managing the pointer is the user's responsibility.
1498 clear() _GLIBCXX_NOEXCEPT
1504 // [23.2.2.4] list operations
1506 * @brief Insert contents of another %list.
1507 * @param __position Iterator referencing the element to insert before.
1508 * @param __x Source list.
1510 * The elements of @a __x are inserted in constant time in front of
1511 * the element referenced by @a __position. @a __x becomes an empty
1514 * Requires this != @a __x.
1517 #if __cplusplus >= 201103L
1518 splice(const_iterator __position
, list
&& __x
) noexcept
1520 splice(iterator __position
, list
& __x
)
1525 _M_check_equal_allocators(__x
);
1527 this->_M_transfer(__position
._M_const_cast(),
1528 __x
.begin(), __x
.end());
1530 this->_M_inc_size(__x
._M_get_size());
1535 #if __cplusplus >= 201103L
1537 splice(const_iterator __position
, list
& __x
) noexcept
1538 { splice(__position
, std::move(__x
)); }
1541 #if __cplusplus >= 201103L
1543 * @brief Insert element from another %list.
1544 * @param __position Const_iterator referencing the element to
1546 * @param __x Source list.
1547 * @param __i Const_iterator referencing the element to move.
1549 * Removes the element in list @a __x referenced by @a __i and
1550 * inserts it into the current list before @a __position.
1553 splice(const_iterator __position
, list
&& __x
, const_iterator __i
) noexcept
1556 * @brief Insert element from another %list.
1557 * @param __position Iterator referencing the element to insert before.
1558 * @param __x Source list.
1559 * @param __i Iterator referencing the element to move.
1561 * Removes the element in list @a __x referenced by @a __i and
1562 * inserts it into the current list before @a __position.
1565 splice(iterator __position
, list
& __x
, iterator __i
)
1568 iterator __j
= __i
._M_const_cast();
1570 if (__position
== __i
|| __position
== __j
)
1573 if (this != std::__addressof(__x
))
1574 _M_check_equal_allocators(__x
);
1576 this->_M_transfer(__position
._M_const_cast(),
1577 __i
._M_const_cast(), __j
);
1579 this->_M_inc_size(1);
1583 #if __cplusplus >= 201103L
1585 * @brief Insert element from another %list.
1586 * @param __position Const_iterator referencing the element to
1588 * @param __x Source list.
1589 * @param __i Const_iterator referencing the element to move.
1591 * Removes the element in list @a __x referenced by @a __i and
1592 * inserts it into the current list before @a __position.
1595 splice(const_iterator __position
, list
& __x
, const_iterator __i
) noexcept
1596 { splice(__position
, std::move(__x
), __i
); }
1599 #if __cplusplus >= 201103L
1601 * @brief Insert range from another %list.
1602 * @param __position Const_iterator referencing the element to
1604 * @param __x Source list.
1605 * @param __first Const_iterator referencing the start of range in x.
1606 * @param __last Const_iterator referencing the end of range in x.
1608 * Removes elements in the range [__first,__last) and inserts them
1609 * before @a __position in constant time.
1611 * Undefined if @a __position is in [__first,__last).
1614 splice(const_iterator __position
, list
&& __x
, const_iterator __first
,
1615 const_iterator __last
) noexcept
1618 * @brief Insert range from another %list.
1619 * @param __position Iterator referencing the element to insert before.
1620 * @param __x Source list.
1621 * @param __first Iterator referencing the start of range in x.
1622 * @param __last Iterator referencing the end of range in x.
1624 * Removes elements in the range [__first,__last) and inserts them
1625 * before @a __position in constant time.
1627 * Undefined if @a __position is in [__first,__last).
1630 splice(iterator __position
, list
& __x
, iterator __first
,
1634 if (__first
!= __last
)
1636 if (this != std::__addressof(__x
))
1637 _M_check_equal_allocators(__x
);
1639 size_t __n
= _S_distance(__first
, __last
);
1640 this->_M_inc_size(__n
);
1641 __x
._M_dec_size(__n
);
1643 this->_M_transfer(__position
._M_const_cast(),
1644 __first
._M_const_cast(),
1645 __last
._M_const_cast());
1649 #if __cplusplus >= 201103L
1651 * @brief Insert range from another %list.
1652 * @param __position Const_iterator referencing the element to
1654 * @param __x Source list.
1655 * @param __first Const_iterator referencing the start of range in x.
1656 * @param __last Const_iterator referencing the end of range in x.
1658 * Removes elements in the range [__first,__last) and inserts them
1659 * before @a __position in constant time.
1661 * Undefined if @a __position is in [__first,__last).
1664 splice(const_iterator __position
, list
& __x
, const_iterator __first
,
1665 const_iterator __last
) noexcept
1666 { splice(__position
, std::move(__x
), __first
, __last
); }
1670 #if __cplusplus > 201703L
1671 # define __cpp_lib_list_remove_return_type 201806L
1672 typedef size_type __remove_return_type
;
1673 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG \
1674 __attribute__((__abi_tag__("__cxx20")))
1676 typedef void __remove_return_type
;
1677 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1682 * @brief Remove all elements equal to value.
1683 * @param __value The value to remove.
1685 * Removes every element in the list equal to @a value.
1686 * Remaining elements stay in list order. Note that this
1687 * function only erases the elements, and that if the elements
1688 * themselves are pointers, the pointed-to memory is not
1689 * touched in any way. Managing the pointer is the user's
1692 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1693 __remove_return_type
1694 remove(const _Tp
& __value
);
1697 * @brief Remove all elements satisfying a predicate.
1698 * @tparam _Predicate Unary predicate function or object.
1700 * Removes every element in the list for which the predicate
1701 * returns true. Remaining elements stay in list order. Note
1702 * that this function only erases the elements, and that if the
1703 * elements themselves are pointers, the pointed-to memory is
1704 * not touched in any way. Managing the pointer is the user's
1707 template<typename _Predicate
>
1708 __remove_return_type
1709 remove_if(_Predicate
);
1712 * @brief Remove consecutive duplicate elements.
1714 * For each consecutive set of elements with the same value,
1715 * remove all but the first one. Remaining elements stay in
1716 * list order. Note that this function only erases the
1717 * elements, and that if the elements themselves are pointers,
1718 * the pointed-to memory is not touched in any way. Managing
1719 * the pointer is the user's responsibility.
1721 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1722 __remove_return_type
1726 * @brief Remove consecutive elements satisfying a predicate.
1727 * @tparam _BinaryPredicate Binary predicate function or object.
1729 * For each consecutive set of elements [first,last) that
1730 * satisfy predicate(first,i) where i is an iterator in
1731 * [first,last), remove all but the first one. Remaining
1732 * elements stay in list order. Note that this function only
1733 * erases the elements, and that if the elements themselves are
1734 * pointers, the pointed-to memory is not touched in any way.
1735 * Managing the pointer is the user's responsibility.
1737 template<typename _BinaryPredicate
>
1738 __remove_return_type
1739 unique(_BinaryPredicate
);
1741 #undef _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1744 * @brief Merge sorted lists.
1745 * @param __x Sorted list to merge.
1747 * Assumes that both @a __x and this list are sorted according to
1748 * operator<(). Merges elements of @a __x into this list in
1749 * sorted order, leaving @a __x empty when complete. Elements in
1750 * this list precede elements in @a __x that are equal.
1752 #if __cplusplus >= 201103L
1758 { merge(std::move(__x
)); }
1765 * @brief Merge sorted lists according to comparison function.
1766 * @tparam _StrictWeakOrdering Comparison function defining
1768 * @param __x Sorted list to merge.
1769 * @param __comp Comparison functor.
1771 * Assumes that both @a __x and this list are sorted according to
1772 * StrictWeakOrdering. Merges elements of @a __x into this list
1773 * in sorted order, leaving @a __x empty when complete. Elements
1774 * in this list precede elements in @a __x that are equivalent
1775 * according to StrictWeakOrdering().
1777 #if __cplusplus >= 201103L
1778 template<typename _StrictWeakOrdering
>
1780 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1782 template<typename _StrictWeakOrdering
>
1784 merge(list
& __x
, _StrictWeakOrdering __comp
)
1785 { merge(std::move(__x
), __comp
); }
1787 template<typename _StrictWeakOrdering
>
1789 merge(list
& __x
, _StrictWeakOrdering __comp
);
1793 * @brief Reverse the elements in list.
1795 * Reverse the order of elements in the list in linear time.
1798 reverse() _GLIBCXX_NOEXCEPT
1799 { this->_M_impl
._M_node
._M_reverse(); }
1802 * @brief Sort the elements.
1804 * Sorts the elements of this list in NlogN time. Equivalent
1805 * elements remain in list order.
1811 * @brief Sort the elements according to comparison function.
1813 * Sorts the elements of this list in NlogN time. Equivalent
1814 * elements remain in list order.
1816 template<typename _StrictWeakOrdering
>
1818 sort(_StrictWeakOrdering
);
1821 // Internal constructor functions follow.
1823 // Called by the range constructor to implement [23.1.1]/9
1825 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1826 // 438. Ambiguity in the "do the right thing" clause
1827 template<typename _Integer
>
1829 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1830 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1832 // Called by the range constructor to implement [23.1.1]/9
1833 template<typename _InputIterator
>
1835 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1838 for (; __first
!= __last
; ++__first
)
1839 #if __cplusplus >= 201103L
1840 emplace_back(*__first
);
1842 push_back(*__first
);
1846 // Called by list(n,v,a), and the range constructor when it turns out
1847 // to be the same thing.
1849 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1855 #if __cplusplus >= 201103L
1856 // Called by list(n).
1858 _M_default_initialize(size_type __n
)
1864 // Called by resize(sz).
1866 _M_default_append(size_type __n
);
1869 // Internal assign functions follow.
1871 // Called by the range assign to implement [23.1.1]/9
1873 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1874 // 438. Ambiguity in the "do the right thing" clause
1875 template<typename _Integer
>
1877 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1878 { _M_fill_assign(__n
, __val
); }
1880 // Called by the range assign to implement [23.1.1]/9
1881 template<typename _InputIterator
>
1883 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1886 // Called by assign(n,t), and the range assign when it turns out
1887 // to be the same thing.
1889 _M_fill_assign(size_type __n
, const value_type
& __val
);
1892 // Moves the elements from [first,last) before position.
1894 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1895 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1897 // Inserts new element at position given and with value given.
1898 #if __cplusplus < 201103L
1900 _M_insert(iterator __position
, const value_type
& __x
)
1902 _Node
* __tmp
= _M_create_node(__x
);
1903 __tmp
->_M_hook(__position
._M_node
);
1904 this->_M_inc_size(1);
1907 template<typename
... _Args
>
1909 _M_insert(iterator __position
, _Args
&&... __args
)
1911 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1912 __tmp
->_M_hook(__position
._M_node
);
1913 this->_M_inc_size(1);
1917 // Erases element at position given.
1919 _M_erase(iterator __position
) _GLIBCXX_NOEXCEPT
1921 this->_M_dec_size(1);
1922 __position
._M_node
->_M_unhook();
1923 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1924 #if __cplusplus >= 201103L
1925 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n
->_M_valptr());
1927 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n
->_M_valptr());
1933 // To implement the splice (and merge) bits of N1599.
1935 _M_check_equal_allocators(list
& __x
) _GLIBCXX_NOEXCEPT
1937 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1938 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1942 // Used to implement resize.
1944 _M_resize_pos(size_type
& __new_size
) const;
1946 #if __cplusplus >= 201103L
1948 _M_move_assign(list
&& __x
, true_type
) noexcept
1951 this->_M_move_nodes(std::move(__x
));
1952 std::__alloc_on_move(this->_M_get_Node_allocator(),
1953 __x
._M_get_Node_allocator());
1957 _M_move_assign(list
&& __x
, false_type
)
1959 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1960 _M_move_assign(std::move(__x
), true_type
{});
1962 // The rvalue's allocator cannot be moved, or is not equal,
1963 // so we need to individually move each element.
1964 _M_assign_dispatch(std::make_move_iterator(__x
.begin()),
1965 std::make_move_iterator(__x
.end()),
1971 #if __cpp_deduction_guides >= 201606
1972 template<typename _InputIterator
, typename _ValT
1973 = typename iterator_traits
<_InputIterator
>::value_type
,
1974 typename _Allocator
= allocator
<_ValT
>,
1975 typename
= _RequireInputIter
<_InputIterator
>,
1976 typename
= _RequireAllocator
<_Allocator
>>
1977 list(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1978 -> list
<_ValT
, _Allocator
>;
1981 _GLIBCXX_END_NAMESPACE_CXX11
1984 * @brief List equality comparison.
1985 * @param __x A %list.
1986 * @param __y A %list of the same type as @a __x.
1987 * @return True iff the size and elements of the lists are equal.
1989 * This is an equivalence relation. It is linear in the size of
1990 * the lists. Lists are considered equivalent if their sizes are
1991 * equal, and if corresponding elements compare equal.
1993 template<typename _Tp
, typename _Alloc
>
1995 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1997 #if _GLIBCXX_USE_CXX11_ABI
1998 if (__x
.size() != __y
.size())
2002 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
2003 const_iterator __end1
= __x
.end();
2004 const_iterator __end2
= __y
.end();
2006 const_iterator __i1
= __x
.begin();
2007 const_iterator __i2
= __y
.begin();
2008 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
2013 return __i1
== __end1
&& __i2
== __end2
;
2016 #if __cpp_lib_three_way_comparison
2018 * @brief List ordering relation.
2019 * @param __x A `list`.
2020 * @param __y A `list` of the same type as `__x`.
2021 * @return A value indicating whether `__x` is less than, equal to,
2022 * greater than, or incomparable with `__y`.
2024 * See `std::lexicographical_compare_three_way()` for how the determination
2025 * is made. This operator is used to synthesize relational operators like
2028 template<typename _Tp
, typename _Alloc
>
2029 inline __detail::__synth3way_t
<_Tp
>
2030 operator<=>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2032 return std::lexicographical_compare_three_way(__x
.begin(), __x
.end(),
2033 __y
.begin(), __y
.end(),
2034 __detail::__synth3way
);
2038 * @brief List ordering relation.
2039 * @param __x A %list.
2040 * @param __y A %list of the same type as @a __x.
2041 * @return True iff @a __x is lexicographically less than @a __y.
2043 * This is a total ordering relation. It is linear in the size of the
2044 * lists. The elements must be comparable with @c <.
2046 * See std::lexicographical_compare() for how the determination is made.
2048 template<typename _Tp
, typename _Alloc
>
2050 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2051 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2052 __y
.begin(), __y
.end()); }
2054 /// Based on operator==
2055 template<typename _Tp
, typename _Alloc
>
2057 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2058 { return !(__x
== __y
); }
2060 /// Based on operator<
2061 template<typename _Tp
, typename _Alloc
>
2063 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2064 { return __y
< __x
; }
2066 /// Based on operator<
2067 template<typename _Tp
, typename _Alloc
>
2069 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2070 { return !(__y
< __x
); }
2072 /// Based on operator<
2073 template<typename _Tp
, typename _Alloc
>
2075 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2076 { return !(__x
< __y
); }
2077 #endif // three-way comparison
2079 /// See std::list::swap().
2080 template<typename _Tp
, typename _Alloc
>
2082 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
2083 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2086 _GLIBCXX_END_NAMESPACE_CONTAINER
2088 #if _GLIBCXX_USE_CXX11_ABI
2090 // Detect when distance is used to compute the size of the whole list.
2091 template<typename _Tp
>
2093 __distance(_GLIBCXX_STD_C::_List_iterator
<_Tp
> __first
,
2094 _GLIBCXX_STD_C::_List_iterator
<_Tp
> __last
,
2095 input_iterator_tag __tag
)
2097 typedef _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> _CIter
;
2098 return std::__distance(_CIter(__first
), _CIter(__last
), __tag
);
2101 template<typename _Tp
>
2103 __distance(_GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __first
,
2104 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __last
,
2107 typedef __detail::_List_node_header _Sentinel
;
2108 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __beyond
= __last
;
2110 const bool __whole
= __first
== __beyond
;
2111 if (__builtin_constant_p (__whole
) && __whole
)
2112 return static_cast<const _Sentinel
*>(__last
._M_node
)->_M_size
;
2115 while (__first
!= __last
)
2124 _GLIBCXX_END_NAMESPACE_VERSION
2127 #endif /* _STL_LIST_H */