1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2021 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.
210 operator*() const _GLIBCXX_NOEXCEPT
211 { 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
;
250 operator==(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
251 { return __x
._M_node
== __y
._M_node
; }
253 #if __cpp_impl_three_way_comparison < 201907L
256 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
257 { return __x
._M_node
!= __y
._M_node
; }
260 // The only member points to the %list element.
261 __detail::_List_node_base
* _M_node
;
265 * @brief A list::const_iterator.
267 * All the functions are op overloads.
269 template<typename _Tp
>
270 struct _List_const_iterator
272 typedef _List_const_iterator
<_Tp
> _Self
;
273 typedef const _List_node
<_Tp
> _Node
;
274 typedef _List_iterator
<_Tp
> iterator
;
276 typedef ptrdiff_t difference_type
;
277 typedef std::bidirectional_iterator_tag iterator_category
;
278 typedef _Tp value_type
;
279 typedef const _Tp
* pointer
;
280 typedef const _Tp
& reference
;
282 _List_const_iterator() _GLIBCXX_NOEXCEPT
286 _List_const_iterator(const __detail::_List_node_base
* __x
)
290 _List_const_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
291 : _M_node(__x
._M_node
) { }
294 _M_const_cast() const _GLIBCXX_NOEXCEPT
295 { return iterator(const_cast<__detail::_List_node_base
*>(_M_node
)); }
297 // Must downcast from List_node_base to _List_node to get to value.
300 operator*() const _GLIBCXX_NOEXCEPT
301 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
305 operator->() const _GLIBCXX_NOEXCEPT
306 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
309 operator++() _GLIBCXX_NOEXCEPT
311 _M_node
= _M_node
->_M_next
;
316 operator++(int) _GLIBCXX_NOEXCEPT
319 _M_node
= _M_node
->_M_next
;
324 operator--() _GLIBCXX_NOEXCEPT
326 _M_node
= _M_node
->_M_prev
;
331 operator--(int) _GLIBCXX_NOEXCEPT
334 _M_node
= _M_node
->_M_prev
;
340 operator==(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
341 { return __x
._M_node
== __y
._M_node
; }
343 #if __cpp_impl_three_way_comparison < 201907L
346 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
347 { return __x
._M_node
!= __y
._M_node
; }
350 // The only member points to the %list element.
351 const __detail::_List_node_base
* _M_node
;
354 _GLIBCXX_BEGIN_NAMESPACE_CXX11
355 /// See bits/stl_deque.h's _Deque_base for an explanation.
356 template<typename _Tp
, typename _Alloc
>
360 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
361 rebind
<_Tp
>::other _Tp_alloc_type
;
362 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tp_alloc_traits
;
363 typedef typename
_Tp_alloc_traits::template
364 rebind
<_List_node
<_Tp
> >::other _Node_alloc_type
;
365 typedef __gnu_cxx::__alloc_traits
<_Node_alloc_type
> _Node_alloc_traits
;
367 #if !_GLIBCXX_INLINE_VERSION
369 _S_distance(const __detail::_List_node_base
* __first
,
370 const __detail::_List_node_base
* __last
)
373 while (__first
!= __last
)
375 __first
= __first
->_M_next
;
383 : public _Node_alloc_type
385 __detail::_List_node_header _M_node
;
387 _List_impl() _GLIBCXX_NOEXCEPT_IF(
388 is_nothrow_default_constructible
<_Node_alloc_type
>::value
)
392 _List_impl(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
393 : _Node_alloc_type(__a
)
396 #if __cplusplus >= 201103L
397 _List_impl(_List_impl
&&) = default;
399 _List_impl(_Node_alloc_type
&& __a
, _List_impl
&& __x
)
400 : _Node_alloc_type(std::move(__a
)), _M_node(std::move(__x
._M_node
))
403 _List_impl(_Node_alloc_type
&& __a
) noexcept
404 : _Node_alloc_type(std::move(__a
))
411 #if _GLIBCXX_USE_CXX11_ABI
412 size_t _M_get_size() const { return _M_impl
._M_node
._M_size
; }
414 void _M_set_size(size_t __n
) { _M_impl
._M_node
._M_size
= __n
; }
416 void _M_inc_size(size_t __n
) { _M_impl
._M_node
._M_size
+= __n
; }
418 void _M_dec_size(size_t __n
) { _M_impl
._M_node
._M_size
-= __n
; }
420 # if !_GLIBCXX_INLINE_VERSION
422 _M_distance(const __detail::_List_node_base
* __first
,
423 const __detail::_List_node_base
* __last
) const
424 { return _S_distance(__first
, __last
); }
426 // return the stored size
427 size_t _M_node_count() const { return _M_get_size(); }
430 // dummy implementations used when the size is not stored
431 size_t _M_get_size() const { return 0; }
432 void _M_set_size(size_t) { }
433 void _M_inc_size(size_t) { }
434 void _M_dec_size(size_t) { }
436 # if !_GLIBCXX_INLINE_VERSION
437 size_t _M_distance(const void*, const void*) const { return 0; }
439 // count the number of nodes
440 size_t _M_node_count() const
442 return _S_distance(_M_impl
._M_node
._M_next
,
443 std::__addressof(_M_impl
._M_node
));
448 typename
_Node_alloc_traits::pointer
450 { return _Node_alloc_traits::allocate(_M_impl
, 1); }
453 _M_put_node(typename
_Node_alloc_traits::pointer __p
) _GLIBCXX_NOEXCEPT
454 { _Node_alloc_traits::deallocate(_M_impl
, __p
, 1); }
457 typedef _Alloc allocator_type
;
460 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
463 const _Node_alloc_type
&
464 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
467 #if __cplusplus >= 201103L
468 _List_base() = default;
473 _List_base(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
477 #if __cplusplus >= 201103L
478 _List_base(_List_base
&&) = default;
480 # if !_GLIBCXX_INLINE_VERSION
481 _List_base(_List_base
&& __x
, _Node_alloc_type
&& __a
)
482 : _M_impl(std::move(__a
))
484 if (__x
._M_get_Node_allocator() == _M_get_Node_allocator())
485 _M_move_nodes(std::move(__x
));
486 // else caller must move individual elements.
490 // Used when allocator is_always_equal.
491 _List_base(_Node_alloc_type
&& __a
, _List_base
&& __x
)
492 : _M_impl(std::move(__a
), std::move(__x
._M_impl
))
495 // Used when allocator !is_always_equal.
496 _List_base(_Node_alloc_type
&& __a
)
497 : _M_impl(std::move(__a
))
501 _M_move_nodes(_List_base
&& __x
)
502 { _M_impl
._M_node
._M_move_nodes(std::move(__x
._M_impl
._M_node
)); }
505 // This is what actually destroys the list.
506 ~_List_base() _GLIBCXX_NOEXCEPT
510 _M_clear() _GLIBCXX_NOEXCEPT
;
513 _M_init() _GLIBCXX_NOEXCEPT
514 { this->_M_impl
._M_node
._M_init(); }
518 * @brief A standard container with linear time access to elements,
519 * and fixed time insertion/deletion at any point in the sequence.
523 * @tparam _Tp Type of element.
524 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
526 * Meets the requirements of a <a href="tables.html#65">container</a>, a
527 * <a href="tables.html#66">reversible container</a>, and a
528 * <a href="tables.html#67">sequence</a>, including the
529 * <a href="tables.html#68">optional sequence requirements</a> with the
530 * %exception of @c at and @c operator[].
532 * This is a @e doubly @e linked %list. Traversal up and down the
533 * %list requires linear time, but adding and removing elements (or
534 * @e nodes) is done in constant time, regardless of where the
535 * change takes place. Unlike std::vector and std::deque,
536 * random-access iterators are not provided, so subscripting ( @c
537 * [] ) access is not allowed. For algorithms which only need
538 * sequential access, this lack makes no difference.
540 * Also unlike the other standard containers, std::list provides
541 * specialized algorithms %unique to linked lists, such as
542 * splicing, sorting, and in-place reversal.
544 * A couple points on memory allocation for list<Tp>:
546 * First, we never actually allocate a Tp, we allocate
547 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
548 * that after elements from %list<X,Alloc1> are spliced into
549 * %list<X,Alloc2>, destroying the memory of the second %list is a
550 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
552 * Second, a %list conceptually represented as
554 * A <---> B <---> C <---> D
556 * is actually circular; a link exists between A and D. The %list
557 * class holds (as its only data member) a private list::iterator
558 * pointing to @e D, not to @e A! To get to the head of the %list,
559 * we start at the tail and move forward by one. When this member
560 * iterator's next/previous pointers refer to itself, the %list is
563 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
564 class list
: protected _List_base
<_Tp
, _Alloc
>
566 #ifdef _GLIBCXX_CONCEPT_CHECKS
567 // concept requirements
568 typedef typename
_Alloc::value_type _Alloc_value_type
;
569 # if __cplusplus < 201103L
570 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
572 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
575 #if __cplusplus >= 201103L
576 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
577 "std::list must have a non-const, non-volatile value_type");
578 # if __cplusplus > 201703L || defined __STRICT_ANSI__
579 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
580 "std::list must have the same value_type as its allocator");
584 typedef _List_base
<_Tp
, _Alloc
> _Base
;
585 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
586 typedef typename
_Base::_Tp_alloc_traits _Tp_alloc_traits
;
587 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
588 typedef typename
_Base::_Node_alloc_traits _Node_alloc_traits
;
591 typedef _Tp value_type
;
592 typedef typename
_Tp_alloc_traits::pointer pointer
;
593 typedef typename
_Tp_alloc_traits::const_pointer const_pointer
;
594 typedef typename
_Tp_alloc_traits::reference reference
;
595 typedef typename
_Tp_alloc_traits::const_reference const_reference
;
596 typedef _List_iterator
<_Tp
> iterator
;
597 typedef _List_const_iterator
<_Tp
> const_iterator
;
598 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
599 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
600 typedef size_t size_type
;
601 typedef ptrdiff_t difference_type
;
602 typedef _Alloc allocator_type
;
605 // Note that pointers-to-_Node's can be ctor-converted to
607 typedef _List_node
<_Tp
> _Node
;
609 using _Base::_M_impl
;
610 using _Base::_M_put_node
;
611 using _Base::_M_get_node
;
612 using _Base::_M_get_Node_allocator
;
615 * @param __args An instance of user data.
617 * Allocates space for a new node and constructs a copy of
620 #if __cplusplus < 201103L
622 _M_create_node(const value_type
& __x
)
624 _Node
* __p
= this->_M_get_node();
627 _Tp_alloc_type
__alloc(_M_get_Node_allocator());
628 __alloc
.construct(__p
->_M_valptr(), __x
);
633 __throw_exception_again
;
638 template<typename
... _Args
>
640 _M_create_node(_Args
&&... __args
)
642 auto __p
= this->_M_get_node();
643 auto& __alloc
= _M_get_Node_allocator();
644 __allocated_ptr
<_Node_alloc_type
> __guard
{__alloc
, __p
};
645 _Node_alloc_traits::construct(__alloc
, __p
->_M_valptr(),
646 std::forward
<_Args
>(__args
)...);
652 #if _GLIBCXX_USE_CXX11_ABI
654 _S_distance(const_iterator __first
, const_iterator __last
)
655 { return std::distance(__first
, __last
); }
657 // return the stored size
659 _M_node_count() const
660 { return this->_M_get_size(); }
662 // dummy implementations used when the size is not stored
664 _S_distance(const_iterator
, const_iterator
)
667 // count the number of nodes
669 _M_node_count() const
670 { return std::distance(begin(), end()); }
674 // [23.2.2.1] construct/copy/destroy
675 // (assign() and get_allocator() are also listed in this section)
678 * @brief Creates a %list with no elements.
680 #if __cplusplus >= 201103L
687 * @brief Creates a %list with no elements.
688 * @param __a An allocator object.
691 list(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
692 : _Base(_Node_alloc_type(__a
)) { }
694 #if __cplusplus >= 201103L
696 * @brief Creates a %list with default constructed elements.
697 * @param __n The number of elements to initially create.
698 * @param __a An allocator object.
700 * This constructor fills the %list with @a __n default
701 * constructed elements.
704 list(size_type __n
, const allocator_type
& __a
= allocator_type())
705 : _Base(_Node_alloc_type(__a
))
706 { _M_default_initialize(__n
); }
709 * @brief Creates a %list with copies of an exemplar element.
710 * @param __n The number of elements to initially create.
711 * @param __value An element to copy.
712 * @param __a An allocator object.
714 * This constructor fills the %list with @a __n copies of @a __value.
716 list(size_type __n
, const value_type
& __value
,
717 const allocator_type
& __a
= allocator_type())
718 : _Base(_Node_alloc_type(__a
))
719 { _M_fill_initialize(__n
, __value
); }
722 * @brief Creates a %list with copies of an exemplar element.
723 * @param __n The number of elements to initially create.
724 * @param __value An element to copy.
725 * @param __a An allocator object.
727 * This constructor fills the %list with @a __n copies of @a __value.
730 list(size_type __n
, const value_type
& __value
= value_type(),
731 const allocator_type
& __a
= allocator_type())
732 : _Base(_Node_alloc_type(__a
))
733 { _M_fill_initialize(__n
, __value
); }
737 * @brief %List copy constructor.
738 * @param __x A %list of identical element and allocator types.
740 * The newly-created %list uses a copy of the allocation object used
741 * by @a __x (unless the allocator traits dictate a different object).
743 list(const list
& __x
)
744 : _Base(_Node_alloc_traits::
745 _S_select_on_copy(__x
._M_get_Node_allocator()))
746 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
748 #if __cplusplus >= 201103L
750 * @brief %List move constructor.
752 * The newly-created %list contains the exact contents of the moved
753 * instance. The contents of the moved instance are a valid, but
756 list(list
&&) = default;
759 * @brief Builds a %list from an initializer_list
760 * @param __l An initializer_list of value_type.
761 * @param __a An allocator object.
763 * Create a %list consisting of copies of the elements in the
764 * initializer_list @a __l. This is linear in __l.size().
766 list(initializer_list
<value_type
> __l
,
767 const allocator_type
& __a
= allocator_type())
768 : _Base(_Node_alloc_type(__a
))
769 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
771 list(const list
& __x
, const allocator_type
& __a
)
772 : _Base(_Node_alloc_type(__a
))
773 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
776 list(list
&& __x
, const allocator_type
& __a
, true_type
) noexcept
777 : _Base(_Node_alloc_type(__a
), std::move(__x
))
780 list(list
&& __x
, const allocator_type
& __a
, false_type
)
781 : _Base(_Node_alloc_type(__a
))
783 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
784 this->_M_move_nodes(std::move(__x
));
786 insert(begin(), std::__make_move_if_noexcept_iterator(__x
.begin()),
787 std::__make_move_if_noexcept_iterator(__x
.end()));
791 list(list
&& __x
, const allocator_type
& __a
)
792 noexcept(_Node_alloc_traits::_S_always_equal())
793 : list(std::move(__x
), __a
,
794 typename
_Node_alloc_traits::is_always_equal
{})
799 * @brief Builds a %list from a range.
800 * @param __first An input iterator.
801 * @param __last An input iterator.
802 * @param __a An allocator object.
804 * Create a %list consisting of copies of the elements from
805 * [@a __first,@a __last). This is linear in N (where N is
806 * distance(@a __first,@a __last)).
808 #if __cplusplus >= 201103L
809 template<typename _InputIterator
,
810 typename
= std::_RequireInputIter
<_InputIterator
>>
811 list(_InputIterator __first
, _InputIterator __last
,
812 const allocator_type
& __a
= allocator_type())
813 : _Base(_Node_alloc_type(__a
))
814 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
816 template<typename _InputIterator
>
817 list(_InputIterator __first
, _InputIterator __last
,
818 const allocator_type
& __a
= allocator_type())
819 : _Base(_Node_alloc_type(__a
))
821 // Check whether it's an integral type. If so, it's not an iterator.
822 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
823 _M_initialize_dispatch(__first
, __last
, _Integral());
827 #if __cplusplus >= 201103L
829 * No explicit dtor needed as the _Base dtor takes care of
830 * things. The _Base dtor only erases the elements, and note
831 * that if the elements themselves are pointers, the pointed-to
832 * memory is not touched in any way. Managing the pointer is
833 * the user's responsibility.
839 * @brief %List assignment operator.
840 * @param __x A %list of identical element and allocator types.
842 * All the elements of @a __x are copied.
844 * Whether the allocator is copied depends on the allocator traits.
847 operator=(const list
& __x
);
849 #if __cplusplus >= 201103L
851 * @brief %List move assignment operator.
852 * @param __x A %list of identical element and allocator types.
854 * The contents of @a __x are moved into this %list (without copying).
856 * Afterwards @a __x is a valid, but unspecified %list
858 * Whether the allocator is moved depends on the allocator traits.
861 operator=(list
&& __x
)
862 noexcept(_Node_alloc_traits::_S_nothrow_move())
864 constexpr bool __move_storage
=
865 _Node_alloc_traits::_S_propagate_on_move_assign()
866 || _Node_alloc_traits::_S_always_equal();
867 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
872 * @brief %List initializer list assignment operator.
873 * @param __l An initializer_list of value_type.
875 * Replace the contents of the %list with copies of the elements
876 * in the initializer_list @a __l. This is linear in l.size().
879 operator=(initializer_list
<value_type
> __l
)
881 this->assign(__l
.begin(), __l
.end());
887 * @brief Assigns a given value to a %list.
888 * @param __n Number of elements to be assigned.
889 * @param __val Value to be assigned.
891 * This function fills a %list with @a __n copies of the given
892 * value. Note that the assignment completely changes the %list
893 * and that the resulting %list's size is the same as the number
894 * of elements assigned.
897 assign(size_type __n
, const value_type
& __val
)
898 { _M_fill_assign(__n
, __val
); }
901 * @brief Assigns a range to a %list.
902 * @param __first An input iterator.
903 * @param __last An input iterator.
905 * This function fills a %list with copies of the elements in the
906 * range [@a __first,@a __last).
908 * Note that the assignment completely changes the %list and
909 * that the resulting %list's size is the same as the number of
912 #if __cplusplus >= 201103L
913 template<typename _InputIterator
,
914 typename
= std::_RequireInputIter
<_InputIterator
>>
916 assign(_InputIterator __first
, _InputIterator __last
)
917 { _M_assign_dispatch(__first
, __last
, __false_type()); }
919 template<typename _InputIterator
>
921 assign(_InputIterator __first
, _InputIterator __last
)
923 // Check whether it's an integral type. If so, it's not an iterator.
924 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
925 _M_assign_dispatch(__first
, __last
, _Integral());
929 #if __cplusplus >= 201103L
931 * @brief Assigns an initializer_list to a %list.
932 * @param __l An initializer_list of value_type.
934 * Replace the contents of the %list with copies of the elements
935 * in the initializer_list @a __l. This is linear in __l.size().
938 assign(initializer_list
<value_type
> __l
)
939 { this->_M_assign_dispatch(__l
.begin(), __l
.end(), __false_type()); }
942 /// Get a copy of the memory allocation object.
944 get_allocator() const _GLIBCXX_NOEXCEPT
945 { return allocator_type(_Base::_M_get_Node_allocator()); }
949 * Returns a read/write iterator that points to the first element in the
950 * %list. Iteration is done in ordinary element order.
954 begin() _GLIBCXX_NOEXCEPT
955 { return iterator(this->_M_impl
._M_node
._M_next
); }
958 * Returns a read-only (constant) iterator that points to the
959 * first element in the %list. Iteration is done in ordinary
964 begin() const _GLIBCXX_NOEXCEPT
965 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
968 * Returns a read/write iterator that points one past the last
969 * element in the %list. Iteration is done in ordinary element
974 end() _GLIBCXX_NOEXCEPT
975 { return iterator(&this->_M_impl
._M_node
); }
978 * Returns a read-only (constant) iterator that points one past
979 * the last element in the %list. Iteration is done in ordinary
984 end() const _GLIBCXX_NOEXCEPT
985 { return const_iterator(&this->_M_impl
._M_node
); }
988 * Returns a read/write reverse iterator that points to the last
989 * element in the %list. Iteration is done in reverse element
994 rbegin() _GLIBCXX_NOEXCEPT
995 { return reverse_iterator(end()); }
998 * Returns a read-only (constant) reverse iterator that points to
999 * the last element in the %list. Iteration is done in reverse
1003 const_reverse_iterator
1004 rbegin() const _GLIBCXX_NOEXCEPT
1005 { return const_reverse_iterator(end()); }
1008 * Returns a read/write reverse iterator that points to one
1009 * before the first element in the %list. Iteration is done in
1010 * reverse element order.
1014 rend() _GLIBCXX_NOEXCEPT
1015 { return reverse_iterator(begin()); }
1018 * Returns a read-only (constant) reverse iterator that points to one
1019 * before the first element in the %list. Iteration is done in reverse
1023 const_reverse_iterator
1024 rend() const _GLIBCXX_NOEXCEPT
1025 { return const_reverse_iterator(begin()); }
1027 #if __cplusplus >= 201103L
1029 * Returns a read-only (constant) iterator that points to the
1030 * first element in the %list. Iteration is done in ordinary
1035 cbegin() const noexcept
1036 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
1039 * Returns a read-only (constant) iterator that points one past
1040 * the last element in the %list. Iteration is done in ordinary
1045 cend() const noexcept
1046 { return const_iterator(&this->_M_impl
._M_node
); }
1049 * Returns a read-only (constant) reverse iterator that points to
1050 * the last element in the %list. Iteration is done in reverse
1054 const_reverse_iterator
1055 crbegin() const noexcept
1056 { return const_reverse_iterator(end()); }
1059 * Returns a read-only (constant) reverse iterator that points to one
1060 * before the first element in the %list. Iteration is done in reverse
1064 const_reverse_iterator
1065 crend() const noexcept
1066 { return const_reverse_iterator(begin()); }
1069 // [23.2.2.2] capacity
1071 * Returns true if the %list is empty. (Thus begin() would equal
1074 _GLIBCXX_NODISCARD
bool
1075 empty() const _GLIBCXX_NOEXCEPT
1076 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
1078 /** Returns the number of elements in the %list. */
1081 size() const _GLIBCXX_NOEXCEPT
1082 { return _M_node_count(); }
1084 /** Returns the size() of the largest possible %list. */
1087 max_size() const _GLIBCXX_NOEXCEPT
1088 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1090 #if __cplusplus >= 201103L
1092 * @brief Resizes the %list to the specified number of elements.
1093 * @param __new_size Number of elements the %list should contain.
1095 * This function will %resize the %list to the specified number
1096 * of elements. If the number is smaller than the %list's
1097 * current size the %list is truncated, otherwise default
1098 * constructed elements are appended.
1101 resize(size_type __new_size
);
1104 * @brief Resizes the %list to the specified number of elements.
1105 * @param __new_size Number of elements the %list should contain.
1106 * @param __x Data with which new elements should be populated.
1108 * This function will %resize the %list to the specified number
1109 * of elements. If the number is smaller than the %list's
1110 * current size the %list is truncated, otherwise the %list is
1111 * extended and new elements are populated with given data.
1114 resize(size_type __new_size
, const value_type
& __x
);
1117 * @brief Resizes the %list to the specified number of elements.
1118 * @param __new_size Number of elements the %list should contain.
1119 * @param __x Data with which new elements should be populated.
1121 * This function will %resize the %list to the specified number
1122 * of elements. If the number is smaller than the %list's
1123 * current size the %list is truncated, otherwise the %list is
1124 * extended and new elements are populated with given data.
1127 resize(size_type __new_size
, value_type __x
= value_type());
1132 * Returns a read/write reference to the data at the first
1133 * element of the %list.
1137 front() _GLIBCXX_NOEXCEPT
1138 { return *begin(); }
1141 * Returns a read-only (constant) reference to the data at the first
1142 * element of the %list.
1146 front() const _GLIBCXX_NOEXCEPT
1147 { return *begin(); }
1150 * Returns a read/write reference to the data at the last element
1155 back() _GLIBCXX_NOEXCEPT
1157 iterator __tmp
= end();
1163 * Returns a read-only (constant) reference to the data at the last
1164 * element of the %list.
1168 back() const _GLIBCXX_NOEXCEPT
1170 const_iterator __tmp
= end();
1175 // [23.2.2.3] modifiers
1177 * @brief Add data to the front of the %list.
1178 * @param __x Data to be added.
1180 * This is a typical stack operation. The function creates an
1181 * element at the front of the %list and assigns the given data
1182 * to it. Due to the nature of a %list this operation can be
1183 * done in constant time, and does not invalidate iterators and
1187 push_front(const value_type
& __x
)
1188 { this->_M_insert(begin(), __x
); }
1190 #if __cplusplus >= 201103L
1192 push_front(value_type
&& __x
)
1193 { this->_M_insert(begin(), std::move(__x
)); }
1195 template<typename
... _Args
>
1196 #if __cplusplus > 201402L
1201 emplace_front(_Args
&&... __args
)
1203 this->_M_insert(begin(), std::forward
<_Args
>(__args
)...);
1204 #if __cplusplus > 201402L
1211 * @brief Removes first element.
1213 * This is a typical stack operation. It shrinks the %list by
1214 * one. Due to the nature of a %list this operation can be done
1215 * in constant time, and only invalidates iterators/references to
1216 * the element being removed.
1218 * Note that no data is returned, and if the first element's data
1219 * is needed, it should be retrieved before pop_front() is
1223 pop_front() _GLIBCXX_NOEXCEPT
1224 { this->_M_erase(begin()); }
1227 * @brief Add data to the end of the %list.
1228 * @param __x Data to be added.
1230 * This is a typical stack operation. The function creates an
1231 * element at the end of the %list and assigns the given data to
1232 * it. Due to the nature of a %list this operation can be done
1233 * in constant time, and does not invalidate iterators and
1237 push_back(const value_type
& __x
)
1238 { this->_M_insert(end(), __x
); }
1240 #if __cplusplus >= 201103L
1242 push_back(value_type
&& __x
)
1243 { this->_M_insert(end(), std::move(__x
)); }
1245 template<typename
... _Args
>
1246 #if __cplusplus > 201402L
1251 emplace_back(_Args
&&... __args
)
1253 this->_M_insert(end(), std::forward
<_Args
>(__args
)...);
1254 #if __cplusplus > 201402L
1261 * @brief Removes last element.
1263 * This is a typical stack operation. It shrinks the %list by
1264 * one. Due to the nature of a %list this operation can be done
1265 * in constant time, and only invalidates iterators/references to
1266 * the element being removed.
1268 * Note that no data is returned, and if the last element's data
1269 * is needed, it should be retrieved before pop_back() is called.
1272 pop_back() _GLIBCXX_NOEXCEPT
1273 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1275 #if __cplusplus >= 201103L
1277 * @brief Constructs object in %list before specified iterator.
1278 * @param __position A const_iterator into the %list.
1279 * @param __args Arguments.
1280 * @return An iterator that points to the inserted data.
1282 * This function will insert an object of type T constructed
1283 * with T(std::forward<Args>(args)...) before the specified
1284 * location. Due to the nature of a %list this operation can
1285 * be done in constant time, and does not invalidate iterators
1288 template<typename
... _Args
>
1290 emplace(const_iterator __position
, _Args
&&... __args
);
1293 * @brief Inserts given value into %list before specified iterator.
1294 * @param __position A const_iterator into the %list.
1295 * @param __x Data to be inserted.
1296 * @return An iterator that points to the inserted data.
1298 * This function will insert a copy of the given value before
1299 * the specified location. Due to the nature of a %list this
1300 * operation can be done in constant time, and does not
1301 * invalidate iterators and references.
1304 insert(const_iterator __position
, const value_type
& __x
);
1307 * @brief Inserts given value into %list before specified iterator.
1308 * @param __position An iterator into the %list.
1309 * @param __x Data to be inserted.
1310 * @return An iterator that points to the inserted data.
1312 * This function will insert a copy of the given value before
1313 * the specified location. Due to the nature of a %list this
1314 * operation can be done in constant time, and does not
1315 * invalidate iterators and references.
1318 insert(iterator __position
, const value_type
& __x
);
1321 #if __cplusplus >= 201103L
1323 * @brief Inserts given rvalue into %list before specified iterator.
1324 * @param __position A const_iterator into the %list.
1325 * @param __x Data to be inserted.
1326 * @return An iterator that points to the inserted data.
1328 * This function will insert a copy of the given rvalue before
1329 * the specified location. Due to the nature of a %list this
1330 * operation can be done in constant time, and does not
1331 * invalidate iterators and references.
1334 insert(const_iterator __position
, value_type
&& __x
)
1335 { return emplace(__position
, std::move(__x
)); }
1338 * @brief Inserts the contents of an initializer_list into %list
1339 * before specified const_iterator.
1340 * @param __p A const_iterator into the %list.
1341 * @param __l An initializer_list of value_type.
1342 * @return An iterator pointing to the first element inserted
1345 * This function will insert copies of the data in the
1346 * initializer_list @a l into the %list before the location
1347 * specified by @a p.
1349 * This operation is linear in the number of elements inserted and
1350 * does not invalidate iterators and references.
1353 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1354 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1357 #if __cplusplus >= 201103L
1359 * @brief Inserts a number of copies of given data into the %list.
1360 * @param __position A const_iterator into the %list.
1361 * @param __n Number of elements to be inserted.
1362 * @param __x Data to be inserted.
1363 * @return An iterator pointing to the first element inserted
1366 * This function will insert a specified number of copies of the
1367 * given data before the location specified by @a position.
1369 * This operation is linear in the number of elements inserted and
1370 * does not invalidate iterators and references.
1373 insert(const_iterator __position
, size_type __n
, const value_type
& __x
);
1376 * @brief Inserts a number of copies of given data into the %list.
1377 * @param __position An iterator into the %list.
1378 * @param __n Number of elements to be inserted.
1379 * @param __x Data to be inserted.
1381 * This function will insert a specified number of copies of the
1382 * given data before the location specified by @a position.
1384 * This operation is linear in the number of elements inserted and
1385 * does not invalidate iterators and references.
1388 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1390 list
__tmp(__n
, __x
, get_allocator());
1391 splice(__position
, __tmp
);
1395 #if __cplusplus >= 201103L
1397 * @brief Inserts a range into the %list.
1398 * @param __position A const_iterator into the %list.
1399 * @param __first An input iterator.
1400 * @param __last An input iterator.
1401 * @return An iterator pointing to the first element inserted
1404 * This function will insert copies of the data in the range [@a
1405 * first,@a last) into the %list before the location specified by
1408 * This operation is linear in the number of elements inserted and
1409 * does not invalidate iterators and references.
1411 template<typename _InputIterator
,
1412 typename
= std::_RequireInputIter
<_InputIterator
>>
1414 insert(const_iterator __position
, _InputIterator __first
,
1415 _InputIterator __last
);
1418 * @brief Inserts a range into the %list.
1419 * @param __position An iterator into the %list.
1420 * @param __first An input iterator.
1421 * @param __last An input iterator.
1423 * This function will insert copies of the data in the range [@a
1424 * first,@a last) into the %list before the location specified by
1427 * This operation is linear in the number of elements inserted and
1428 * does not invalidate iterators and references.
1430 template<typename _InputIterator
>
1432 insert(iterator __position
, _InputIterator __first
,
1433 _InputIterator __last
)
1435 list
__tmp(__first
, __last
, get_allocator());
1436 splice(__position
, __tmp
);
1441 * @brief Remove element at given position.
1442 * @param __position Iterator pointing to element to be erased.
1443 * @return An iterator pointing to the next element (or end()).
1445 * This function will erase the element at the given position and thus
1446 * shorten the %list by one.
1448 * Due to the nature of a %list this operation can be done in
1449 * constant time, and only invalidates iterators/references to
1450 * the element being removed. The user is also cautioned that
1451 * this function only erases the element, and that if the element
1452 * is itself a pointer, the pointed-to memory is not touched in
1453 * any way. Managing the pointer is the user's responsibility.
1456 #if __cplusplus >= 201103L
1457 erase(const_iterator __position
) noexcept
;
1459 erase(iterator __position
);
1463 * @brief Remove a range of elements.
1464 * @param __first Iterator pointing to the first element to be erased.
1465 * @param __last Iterator pointing to one past the last element to be
1467 * @return An iterator pointing to the element pointed to by @a last
1468 * prior to erasing (or end()).
1470 * This function will erase the elements in the range @a
1471 * [first,last) and shorten the %list accordingly.
1473 * This operation is linear time in the size of the range and only
1474 * invalidates iterators/references to the element being removed.
1475 * The user is also cautioned that this function only erases the
1476 * elements, and that if the elements themselves are pointers, the
1477 * pointed-to memory is not touched in any way. Managing the pointer
1478 * is the user's responsibility.
1481 #if __cplusplus >= 201103L
1482 erase(const_iterator __first
, const_iterator __last
) noexcept
1484 erase(iterator __first
, iterator __last
)
1487 while (__first
!= __last
)
1488 __first
= erase(__first
);
1489 return __last
._M_const_cast();
1493 * @brief Swaps data with another %list.
1494 * @param __x A %list of the same element and allocator types.
1496 * This exchanges the elements between two lists in constant
1497 * time. Note that the global std::swap() function is
1498 * specialized such that std::swap(l1,l2) will feed to this
1501 * Whether the allocators are swapped depends on the allocator traits.
1504 swap(list
& __x
) _GLIBCXX_NOEXCEPT
1506 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1507 __x
._M_impl
._M_node
);
1509 size_t __xsize
= __x
._M_get_size();
1510 __x
._M_set_size(this->_M_get_size());
1511 this->_M_set_size(__xsize
);
1513 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1514 __x
._M_get_Node_allocator());
1518 * Erases all the elements. Note that this function only erases
1519 * the elements, and that if the elements themselves are
1520 * pointers, the pointed-to memory is not touched in any way.
1521 * Managing the pointer is the user's responsibility.
1524 clear() _GLIBCXX_NOEXCEPT
1530 // [23.2.2.4] list operations
1532 * @brief Insert contents of another %list.
1533 * @param __position Iterator referencing the element to insert before.
1534 * @param __x Source list.
1536 * The elements of @a __x are inserted in constant time in front of
1537 * the element referenced by @a __position. @a __x becomes an empty
1540 * Requires this != @a __x.
1543 #if __cplusplus >= 201103L
1544 splice(const_iterator __position
, list
&& __x
) noexcept
1546 splice(iterator __position
, list
& __x
)
1551 _M_check_equal_allocators(__x
);
1553 this->_M_transfer(__position
._M_const_cast(),
1554 __x
.begin(), __x
.end());
1556 this->_M_inc_size(__x
._M_get_size());
1561 #if __cplusplus >= 201103L
1563 splice(const_iterator __position
, list
& __x
) noexcept
1564 { splice(__position
, std::move(__x
)); }
1567 #if __cplusplus >= 201103L
1569 * @brief Insert element from another %list.
1570 * @param __position Const_iterator referencing the element to
1572 * @param __x Source list.
1573 * @param __i Const_iterator referencing the element to move.
1575 * Removes the element in list @a __x referenced by @a __i and
1576 * inserts it into the current list before @a __position.
1579 splice(const_iterator __position
, list
&& __x
, const_iterator __i
) noexcept
1582 * @brief Insert element from another %list.
1583 * @param __position Iterator referencing the element to insert before.
1584 * @param __x Source list.
1585 * @param __i Iterator referencing the element to move.
1587 * Removes the element in list @a __x referenced by @a __i and
1588 * inserts it into the current list before @a __position.
1591 splice(iterator __position
, list
& __x
, iterator __i
)
1594 iterator __j
= __i
._M_const_cast();
1596 if (__position
== __i
|| __position
== __j
)
1599 if (this != std::__addressof(__x
))
1600 _M_check_equal_allocators(__x
);
1602 this->_M_transfer(__position
._M_const_cast(),
1603 __i
._M_const_cast(), __j
);
1605 this->_M_inc_size(1);
1609 #if __cplusplus >= 201103L
1611 * @brief Insert element from another %list.
1612 * @param __position Const_iterator referencing the element to
1614 * @param __x Source list.
1615 * @param __i Const_iterator referencing the element to move.
1617 * Removes the element in list @a __x referenced by @a __i and
1618 * inserts it into the current list before @a __position.
1621 splice(const_iterator __position
, list
& __x
, const_iterator __i
) noexcept
1622 { splice(__position
, std::move(__x
), __i
); }
1625 #if __cplusplus >= 201103L
1627 * @brief Insert range from another %list.
1628 * @param __position Const_iterator referencing the element to
1630 * @param __x Source list.
1631 * @param __first Const_iterator referencing the start of range in x.
1632 * @param __last Const_iterator referencing the end of range in x.
1634 * Removes elements in the range [__first,__last) and inserts them
1635 * before @a __position in constant time.
1637 * Undefined if @a __position is in [__first,__last).
1640 splice(const_iterator __position
, list
&& __x
, const_iterator __first
,
1641 const_iterator __last
) noexcept
1644 * @brief Insert range from another %list.
1645 * @param __position Iterator referencing the element to insert before.
1646 * @param __x Source list.
1647 * @param __first Iterator referencing the start of range in x.
1648 * @param __last Iterator referencing the end of range in x.
1650 * Removes elements in the range [__first,__last) and inserts them
1651 * before @a __position in constant time.
1653 * Undefined if @a __position is in [__first,__last).
1656 splice(iterator __position
, list
& __x
, iterator __first
,
1660 if (__first
!= __last
)
1662 if (this != std::__addressof(__x
))
1663 _M_check_equal_allocators(__x
);
1665 size_t __n
= _S_distance(__first
, __last
);
1666 this->_M_inc_size(__n
);
1667 __x
._M_dec_size(__n
);
1669 this->_M_transfer(__position
._M_const_cast(),
1670 __first
._M_const_cast(),
1671 __last
._M_const_cast());
1675 #if __cplusplus >= 201103L
1677 * @brief Insert range from another %list.
1678 * @param __position Const_iterator referencing the element to
1680 * @param __x Source list.
1681 * @param __first Const_iterator referencing the start of range in x.
1682 * @param __last Const_iterator referencing the end of range in x.
1684 * Removes elements in the range [__first,__last) and inserts them
1685 * before @a __position in constant time.
1687 * Undefined if @a __position is in [__first,__last).
1690 splice(const_iterator __position
, list
& __x
, const_iterator __first
,
1691 const_iterator __last
) noexcept
1692 { splice(__position
, std::move(__x
), __first
, __last
); }
1696 #if __cplusplus > 201703L
1697 # define __cpp_lib_list_remove_return_type 201806L
1698 typedef size_type __remove_return_type
;
1699 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG \
1700 __attribute__((__abi_tag__("__cxx20")))
1702 typedef void __remove_return_type
;
1703 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1708 * @brief Remove all elements equal to value.
1709 * @param __value The value to remove.
1711 * Removes every element in the list equal to @a value.
1712 * Remaining elements stay in list order. Note that this
1713 * function only erases the elements, and that if the elements
1714 * themselves are pointers, the pointed-to memory is not
1715 * touched in any way. Managing the pointer is the user's
1718 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1719 __remove_return_type
1720 remove(const _Tp
& __value
);
1723 * @brief Remove all elements satisfying a predicate.
1724 * @tparam _Predicate Unary predicate function or object.
1726 * Removes every element in the list for which the predicate
1727 * returns true. Remaining elements stay in list order. Note
1728 * that this function only erases the elements, and that if the
1729 * elements themselves are pointers, the pointed-to memory is
1730 * not touched in any way. Managing the pointer is the user's
1733 template<typename _Predicate
>
1734 __remove_return_type
1735 remove_if(_Predicate
);
1738 * @brief Remove consecutive duplicate elements.
1740 * For each consecutive set of elements with the same value,
1741 * remove all but the first one. Remaining elements stay in
1742 * list order. Note that this function only erases the
1743 * elements, and that if the elements themselves are pointers,
1744 * the pointed-to memory is not touched in any way. Managing
1745 * the pointer is the user's responsibility.
1747 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1748 __remove_return_type
1752 * @brief Remove consecutive elements satisfying a predicate.
1753 * @tparam _BinaryPredicate Binary predicate function or object.
1755 * For each consecutive set of elements [first,last) that
1756 * satisfy predicate(first,i) where i is an iterator in
1757 * [first,last), remove all but the first one. Remaining
1758 * elements stay in list order. Note that this function only
1759 * erases the elements, and that if the elements themselves are
1760 * pointers, the pointed-to memory is not touched in any way.
1761 * Managing the pointer is the user's responsibility.
1763 template<typename _BinaryPredicate
>
1764 __remove_return_type
1765 unique(_BinaryPredicate
);
1767 #undef _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1770 * @brief Merge sorted lists.
1771 * @param __x Sorted list to merge.
1773 * Assumes that both @a __x and this list are sorted according to
1774 * operator<(). Merges elements of @a __x into this list in
1775 * sorted order, leaving @a __x empty when complete. Elements in
1776 * this list precede elements in @a __x that are equal.
1778 #if __cplusplus >= 201103L
1784 { merge(std::move(__x
)); }
1791 * @brief Merge sorted lists according to comparison function.
1792 * @tparam _StrictWeakOrdering Comparison function defining
1794 * @param __x Sorted list to merge.
1795 * @param __comp Comparison functor.
1797 * Assumes that both @a __x and this list are sorted according to
1798 * StrictWeakOrdering. Merges elements of @a __x into this list
1799 * in sorted order, leaving @a __x empty when complete. Elements
1800 * in this list precede elements in @a __x that are equivalent
1801 * according to StrictWeakOrdering().
1803 #if __cplusplus >= 201103L
1804 template<typename _StrictWeakOrdering
>
1806 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1808 template<typename _StrictWeakOrdering
>
1810 merge(list
& __x
, _StrictWeakOrdering __comp
)
1811 { merge(std::move(__x
), __comp
); }
1813 template<typename _StrictWeakOrdering
>
1815 merge(list
& __x
, _StrictWeakOrdering __comp
);
1819 * @brief Reverse the elements in list.
1821 * Reverse the order of elements in the list in linear time.
1824 reverse() _GLIBCXX_NOEXCEPT
1825 { this->_M_impl
._M_node
._M_reverse(); }
1828 * @brief Sort the elements.
1830 * Sorts the elements of this list in NlogN time. Equivalent
1831 * elements remain in list order.
1837 * @brief Sort the elements according to comparison function.
1839 * Sorts the elements of this list in NlogN time. Equivalent
1840 * elements remain in list order.
1842 template<typename _StrictWeakOrdering
>
1844 sort(_StrictWeakOrdering
);
1847 // Internal constructor functions follow.
1849 // Called by the range constructor to implement [23.1.1]/9
1851 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1852 // 438. Ambiguity in the "do the right thing" clause
1853 template<typename _Integer
>
1855 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1856 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1858 // Called by the range constructor to implement [23.1.1]/9
1859 template<typename _InputIterator
>
1861 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1864 for (; __first
!= __last
; ++__first
)
1865 #if __cplusplus >= 201103L
1866 emplace_back(*__first
);
1868 push_back(*__first
);
1872 // Called by list(n,v,a), and the range constructor when it turns out
1873 // to be the same thing.
1875 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1881 #if __cplusplus >= 201103L
1882 // Called by list(n).
1884 _M_default_initialize(size_type __n
)
1890 // Called by resize(sz).
1892 _M_default_append(size_type __n
);
1895 // Internal assign functions follow.
1897 // Called by the range assign to implement [23.1.1]/9
1899 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1900 // 438. Ambiguity in the "do the right thing" clause
1901 template<typename _Integer
>
1903 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1904 { _M_fill_assign(__n
, __val
); }
1906 // Called by the range assign to implement [23.1.1]/9
1907 template<typename _InputIterator
>
1909 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1912 // Called by assign(n,t), and the range assign when it turns out
1913 // to be the same thing.
1915 _M_fill_assign(size_type __n
, const value_type
& __val
);
1918 // Moves the elements from [first,last) before position.
1920 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1921 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1923 // Inserts new element at position given and with value given.
1924 #if __cplusplus < 201103L
1926 _M_insert(iterator __position
, const value_type
& __x
)
1928 _Node
* __tmp
= _M_create_node(__x
);
1929 __tmp
->_M_hook(__position
._M_node
);
1930 this->_M_inc_size(1);
1933 template<typename
... _Args
>
1935 _M_insert(iterator __position
, _Args
&&... __args
)
1937 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1938 __tmp
->_M_hook(__position
._M_node
);
1939 this->_M_inc_size(1);
1943 // Erases element at position given.
1945 _M_erase(iterator __position
) _GLIBCXX_NOEXCEPT
1947 this->_M_dec_size(1);
1948 __position
._M_node
->_M_unhook();
1949 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1950 #if __cplusplus >= 201103L
1951 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n
->_M_valptr());
1953 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n
->_M_valptr());
1959 // To implement the splice (and merge) bits of N1599.
1961 _M_check_equal_allocators(list
& __x
) _GLIBCXX_NOEXCEPT
1963 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1964 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1968 // Used to implement resize.
1970 _M_resize_pos(size_type
& __new_size
) const;
1972 #if __cplusplus >= 201103L
1974 _M_move_assign(list
&& __x
, true_type
) noexcept
1977 this->_M_move_nodes(std::move(__x
));
1978 std::__alloc_on_move(this->_M_get_Node_allocator(),
1979 __x
._M_get_Node_allocator());
1983 _M_move_assign(list
&& __x
, false_type
)
1985 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1986 _M_move_assign(std::move(__x
), true_type
{});
1988 // The rvalue's allocator cannot be moved, or is not equal,
1989 // so we need to individually move each element.
1990 _M_assign_dispatch(std::make_move_iterator(__x
.begin()),
1991 std::make_move_iterator(__x
.end()),
1997 #if __cpp_deduction_guides >= 201606
1998 template<typename _InputIterator
, typename _ValT
1999 = typename iterator_traits
<_InputIterator
>::value_type
,
2000 typename _Allocator
= allocator
<_ValT
>,
2001 typename
= _RequireInputIter
<_InputIterator
>,
2002 typename
= _RequireAllocator
<_Allocator
>>
2003 list(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
2004 -> list
<_ValT
, _Allocator
>;
2007 _GLIBCXX_END_NAMESPACE_CXX11
2010 * @brief List equality comparison.
2011 * @param __x A %list.
2012 * @param __y A %list of the same type as @a __x.
2013 * @return True iff the size and elements of the lists are equal.
2015 * This is an equivalence relation. It is linear in the size of
2016 * the lists. Lists are considered equivalent if their sizes are
2017 * equal, and if corresponding elements compare equal.
2019 template<typename _Tp
, typename _Alloc
>
2022 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2024 #if _GLIBCXX_USE_CXX11_ABI
2025 if (__x
.size() != __y
.size())
2029 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
2030 const_iterator __end1
= __x
.end();
2031 const_iterator __end2
= __y
.end();
2033 const_iterator __i1
= __x
.begin();
2034 const_iterator __i2
= __y
.begin();
2035 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
2040 return __i1
== __end1
&& __i2
== __end2
;
2043 #if __cpp_lib_three_way_comparison
2045 * @brief List ordering relation.
2046 * @param __x A `list`.
2047 * @param __y A `list` of the same type as `__x`.
2048 * @return A value indicating whether `__x` is less than, equal to,
2049 * greater than, or incomparable with `__y`.
2051 * See `std::lexicographical_compare_three_way()` for how the determination
2052 * is made. This operator is used to synthesize relational operators like
2055 template<typename _Tp
, typename _Alloc
>
2057 inline __detail::__synth3way_t
<_Tp
>
2058 operator<=>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2060 return std::lexicographical_compare_three_way(__x
.begin(), __x
.end(),
2061 __y
.begin(), __y
.end(),
2062 __detail::__synth3way
);
2066 * @brief List ordering relation.
2067 * @param __x A %list.
2068 * @param __y A %list of the same type as @a __x.
2069 * @return True iff @a __x is lexicographically less than @a __y.
2071 * This is a total ordering relation. It is linear in the size of the
2072 * lists. The elements must be comparable with @c <.
2074 * See std::lexicographical_compare() for how the determination is made.
2076 template<typename _Tp
, typename _Alloc
>
2079 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2080 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2081 __y
.begin(), __y
.end()); }
2083 /// Based on operator==
2084 template<typename _Tp
, typename _Alloc
>
2087 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2088 { return !(__x
== __y
); }
2090 /// Based on operator<
2091 template<typename _Tp
, typename _Alloc
>
2094 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2095 { return __y
< __x
; }
2097 /// Based on operator<
2098 template<typename _Tp
, typename _Alloc
>
2101 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2102 { return !(__y
< __x
); }
2104 /// Based on operator<
2105 template<typename _Tp
, typename _Alloc
>
2108 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2109 { return !(__x
< __y
); }
2110 #endif // three-way comparison
2112 /// See std::list::swap().
2113 template<typename _Tp
, typename _Alloc
>
2115 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
2116 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2119 _GLIBCXX_END_NAMESPACE_CONTAINER
2121 #if _GLIBCXX_USE_CXX11_ABI
2123 // Detect when distance is used to compute the size of the whole list.
2124 template<typename _Tp
>
2126 __distance(_GLIBCXX_STD_C::_List_iterator
<_Tp
> __first
,
2127 _GLIBCXX_STD_C::_List_iterator
<_Tp
> __last
,
2128 input_iterator_tag __tag
)
2130 typedef _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> _CIter
;
2131 return std::__distance(_CIter(__first
), _CIter(__last
), __tag
);
2134 template<typename _Tp
>
2136 __distance(_GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __first
,
2137 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __last
,
2140 typedef __detail::_List_node_header _Sentinel
;
2141 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __beyond
= __last
;
2143 const bool __whole
= __first
== __beyond
;
2144 if (__builtin_constant_p (__whole
) && __whole
)
2145 return static_cast<const _Sentinel
*>(__last
._M_node
)->_M_size
;
2148 while (__first
!= __last
)
2157 _GLIBCXX_END_NAMESPACE_VERSION
2160 #endif /* _STL_LIST_H */