1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2021 Free Software Foundation, Inc.
4 // Copyright The GNU Toolchain Authors.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file bits/stl_list.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{list}
60 #include <bits/concept_check.h>
61 #include <ext/alloc_traits.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
64 #include <bits/allocated_ptr.h>
65 #include <ext/aligned_buffer.h>
68 namespace std
_GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74 // Supporting structures are split into common and templated
75 // types; the latter publicly inherits from the former in an
76 // effort to reduce code duplication. This results in some
77 // "needless" static_cast'ing later on, but it's all safe
80 /// Common part of a node in the %list.
81 struct _List_node_base
83 _List_node_base
* _M_next
;
84 _List_node_base
* _M_prev
;
87 swap(_List_node_base
& __x
, _List_node_base
& __y
) _GLIBCXX_USE_NOEXCEPT
;
90 _M_transfer(_List_node_base
* const __first
,
91 _List_node_base
* const __last
) _GLIBCXX_USE_NOEXCEPT
;
94 _M_reverse() _GLIBCXX_USE_NOEXCEPT
;
97 _M_hook(_List_node_base
* const __position
) _GLIBCXX_USE_NOEXCEPT
;
100 _M_unhook() _GLIBCXX_USE_NOEXCEPT
;
103 /// The %list node header.
104 struct _List_node_header
: public _List_node_base
106 #if _GLIBCXX_USE_CXX11_ABI
110 _List_node_header() _GLIBCXX_NOEXCEPT
113 #if __cplusplus >= 201103L
114 _List_node_header(_List_node_header
&& __x
) noexcept
115 : _List_node_base
{ __x
._M_next
, __x
._M_prev
}
116 # if _GLIBCXX_USE_CXX11_ABI
117 , _M_size(__x
._M_size
)
120 if (__x
._M_base()->_M_next
== __x
._M_base())
121 this->_M_next
= this->_M_prev
= this;
124 this->_M_next
->_M_prev
= this->_M_prev
->_M_next
= this->_M_base();
130 _M_move_nodes(_List_node_header
&& __x
)
132 _List_node_base
* const __xnode
= __x
._M_base();
133 if (__xnode
->_M_next
== __xnode
)
137 _List_node_base
* const __node
= this->_M_base();
138 __node
->_M_next
= __xnode
->_M_next
;
139 __node
->_M_prev
= __xnode
->_M_prev
;
140 __node
->_M_next
->_M_prev
= __node
->_M_prev
->_M_next
= __node
;
141 # if _GLIBCXX_USE_CXX11_ABI
142 _M_size
= __x
._M_size
;
150 _M_init() _GLIBCXX_NOEXCEPT
152 this->_M_next
= this->_M_prev
= this;
153 #if _GLIBCXX_USE_CXX11_ABI
159 _List_node_base
* _M_base() { return this; }
161 } // namespace detail
163 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
165 /// An actual node in the %list.
166 template<typename _Tp
>
167 struct _List_node
: public __detail::_List_node_base
169 #if __cplusplus >= 201103L
170 __gnu_cxx::__aligned_membuf
<_Tp
> _M_storage
;
171 _Tp
* _M_valptr() { return _M_storage
._M_ptr(); }
172 _Tp
const* _M_valptr() const { return _M_storage
._M_ptr(); }
175 _Tp
* _M_valptr() { return std::__addressof(_M_data
); }
176 _Tp
const* _M_valptr() const { return std::__addressof(_M_data
); }
181 * @brief A list::iterator.
183 * All the functions are op overloads.
185 template<typename _Tp
>
186 struct _List_iterator
188 typedef _List_iterator
<_Tp
> _Self
;
189 typedef _List_node
<_Tp
> _Node
;
191 typedef ptrdiff_t difference_type
;
192 typedef std::bidirectional_iterator_tag iterator_category
;
193 typedef _Tp value_type
;
194 typedef _Tp
* pointer
;
195 typedef _Tp
& reference
;
197 _List_iterator() _GLIBCXX_NOEXCEPT
201 _List_iterator(__detail::_List_node_base
* __x
) _GLIBCXX_NOEXCEPT
205 _M_const_cast() const _GLIBCXX_NOEXCEPT
208 // Must downcast from _List_node_base to _List_node to get to value.
211 operator*() const _GLIBCXX_NOEXCEPT
212 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
216 operator->() const _GLIBCXX_NOEXCEPT
217 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
220 operator++() _GLIBCXX_NOEXCEPT
222 _M_node
= _M_node
->_M_next
;
227 operator++(int) _GLIBCXX_NOEXCEPT
230 _M_node
= _M_node
->_M_next
;
235 operator--() _GLIBCXX_NOEXCEPT
237 _M_node
= _M_node
->_M_prev
;
242 operator--(int) _GLIBCXX_NOEXCEPT
245 _M_node
= _M_node
->_M_prev
;
251 operator==(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
252 { return __x
._M_node
== __y
._M_node
; }
254 #if __cpp_impl_three_way_comparison < 201907L
257 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
258 { return __x
._M_node
!= __y
._M_node
; }
261 // The only member points to the %list element.
262 __detail::_List_node_base
* _M_node
;
266 * @brief A list::const_iterator.
268 * All the functions are op overloads.
270 template<typename _Tp
>
271 struct _List_const_iterator
273 typedef _List_const_iterator
<_Tp
> _Self
;
274 typedef const _List_node
<_Tp
> _Node
;
275 typedef _List_iterator
<_Tp
> iterator
;
277 typedef ptrdiff_t difference_type
;
278 typedef std::bidirectional_iterator_tag iterator_category
;
279 typedef _Tp value_type
;
280 typedef const _Tp
* pointer
;
281 typedef const _Tp
& reference
;
283 _List_const_iterator() _GLIBCXX_NOEXCEPT
287 _List_const_iterator(const __detail::_List_node_base
* __x
)
291 _List_const_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
292 : _M_node(__x
._M_node
) { }
295 _M_const_cast() const _GLIBCXX_NOEXCEPT
296 { return iterator(const_cast<__detail::_List_node_base
*>(_M_node
)); }
298 // Must downcast from List_node_base to _List_node to get to value.
301 operator*() const _GLIBCXX_NOEXCEPT
302 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
306 operator->() const _GLIBCXX_NOEXCEPT
307 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
310 operator++() _GLIBCXX_NOEXCEPT
312 _M_node
= _M_node
->_M_next
;
317 operator++(int) _GLIBCXX_NOEXCEPT
320 _M_node
= _M_node
->_M_next
;
325 operator--() _GLIBCXX_NOEXCEPT
327 _M_node
= _M_node
->_M_prev
;
332 operator--(int) _GLIBCXX_NOEXCEPT
335 _M_node
= _M_node
->_M_prev
;
341 operator==(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
342 { return __x
._M_node
== __y
._M_node
; }
344 #if __cpp_impl_three_way_comparison < 201907L
347 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
348 { return __x
._M_node
!= __y
._M_node
; }
351 // The only member points to the %list element.
352 const __detail::_List_node_base
* _M_node
;
355 _GLIBCXX_BEGIN_NAMESPACE_CXX11
356 /// See bits/stl_deque.h's _Deque_base for an explanation.
357 template<typename _Tp
, typename _Alloc
>
361 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
362 rebind
<_Tp
>::other _Tp_alloc_type
;
363 typedef __gnu_cxx::__alloc_traits
<_Tp_alloc_type
> _Tp_alloc_traits
;
364 typedef typename
_Tp_alloc_traits::template
365 rebind
<_List_node
<_Tp
> >::other _Node_alloc_type
;
366 typedef __gnu_cxx::__alloc_traits
<_Node_alloc_type
> _Node_alloc_traits
;
368 #if !_GLIBCXX_INLINE_VERSION
370 _S_distance(const __detail::_List_node_base
* __first
,
371 const __detail::_List_node_base
* __last
)
374 while (__first
!= __last
)
376 __first
= __first
->_M_next
;
384 : public _Node_alloc_type
386 __detail::_List_node_header _M_node
;
388 _List_impl() _GLIBCXX_NOEXCEPT_IF(
389 is_nothrow_default_constructible
<_Node_alloc_type
>::value
)
393 _List_impl(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
394 : _Node_alloc_type(__a
)
397 #if __cplusplus >= 201103L
398 _List_impl(_List_impl
&&) = default;
400 _List_impl(_Node_alloc_type
&& __a
, _List_impl
&& __x
)
401 : _Node_alloc_type(std::move(__a
)), _M_node(std::move(__x
._M_node
))
404 _List_impl(_Node_alloc_type
&& __a
) noexcept
405 : _Node_alloc_type(std::move(__a
))
412 #if _GLIBCXX_USE_CXX11_ABI
413 size_t _M_get_size() const { return _M_impl
._M_node
._M_size
; }
415 void _M_set_size(size_t __n
) { _M_impl
._M_node
._M_size
= __n
; }
417 void _M_inc_size(size_t __n
) { _M_impl
._M_node
._M_size
+= __n
; }
419 void _M_dec_size(size_t __n
) { _M_impl
._M_node
._M_size
-= __n
; }
421 # if !_GLIBCXX_INLINE_VERSION
423 _M_distance(const __detail::_List_node_base
* __first
,
424 const __detail::_List_node_base
* __last
) const
425 { return _S_distance(__first
, __last
); }
427 // return the stored size
428 size_t _M_node_count() const { return _M_get_size(); }
431 // dummy implementations used when the size is not stored
432 size_t _M_get_size() const { return 0; }
433 void _M_set_size(size_t) { }
434 void _M_inc_size(size_t) { }
435 void _M_dec_size(size_t) { }
437 # if !_GLIBCXX_INLINE_VERSION
438 size_t _M_distance(const void*, const void*) const { return 0; }
440 // count the number of nodes
441 size_t _M_node_count() const
443 return _S_distance(_M_impl
._M_node
._M_next
,
444 std::__addressof(_M_impl
._M_node
));
449 typename
_Node_alloc_traits::pointer
451 { return _Node_alloc_traits::allocate(_M_impl
, 1); }
454 _M_put_node(typename
_Node_alloc_traits::pointer __p
) _GLIBCXX_NOEXCEPT
455 { _Node_alloc_traits::deallocate(_M_impl
, __p
, 1); }
458 typedef _Alloc allocator_type
;
461 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
464 const _Node_alloc_type
&
465 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
468 #if __cplusplus >= 201103L
469 _List_base() = default;
474 _List_base(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
478 #if __cplusplus >= 201103L
479 _List_base(_List_base
&&) = default;
481 # if !_GLIBCXX_INLINE_VERSION
482 _List_base(_List_base
&& __x
, _Node_alloc_type
&& __a
)
483 : _M_impl(std::move(__a
))
485 if (__x
._M_get_Node_allocator() == _M_get_Node_allocator())
486 _M_move_nodes(std::move(__x
));
487 // else caller must move individual elements.
491 // Used when allocator is_always_equal.
492 _List_base(_Node_alloc_type
&& __a
, _List_base
&& __x
)
493 : _M_impl(std::move(__a
), std::move(__x
._M_impl
))
496 // Used when allocator !is_always_equal.
497 _List_base(_Node_alloc_type
&& __a
)
498 : _M_impl(std::move(__a
))
502 _M_move_nodes(_List_base
&& __x
)
503 { _M_impl
._M_node
._M_move_nodes(std::move(__x
._M_impl
._M_node
)); }
506 // This is what actually destroys the list.
507 ~_List_base() _GLIBCXX_NOEXCEPT
511 _M_clear() _GLIBCXX_NOEXCEPT
;
514 _M_init() _GLIBCXX_NOEXCEPT
515 { this->_M_impl
._M_node
._M_init(); }
519 * @brief A standard container with linear time access to elements,
520 * and fixed time insertion/deletion at any point in the sequence.
524 * @tparam _Tp Type of element.
525 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
527 * Meets the requirements of a <a href="tables.html#65">container</a>, a
528 * <a href="tables.html#66">reversible container</a>, and a
529 * <a href="tables.html#67">sequence</a>, including the
530 * <a href="tables.html#68">optional sequence requirements</a> with the
531 * %exception of @c at and @c operator[].
533 * This is a @e doubly @e linked %list. Traversal up and down the
534 * %list requires linear time, but adding and removing elements (or
535 * @e nodes) is done in constant time, regardless of where the
536 * change takes place. Unlike std::vector and std::deque,
537 * random-access iterators are not provided, so subscripting ( @c
538 * [] ) access is not allowed. For algorithms which only need
539 * sequential access, this lack makes no difference.
541 * Also unlike the other standard containers, std::list provides
542 * specialized algorithms %unique to linked lists, such as
543 * splicing, sorting, and in-place reversal.
545 * A couple points on memory allocation for list<Tp>:
547 * First, we never actually allocate a Tp, we allocate
548 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
549 * that after elements from %list<X,Alloc1> are spliced into
550 * %list<X,Alloc2>, destroying the memory of the second %list is a
551 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
553 * Second, a %list conceptually represented as
555 * A <---> B <---> C <---> D
557 * is actually circular; a link exists between A and D. The %list
558 * class holds (as its only data member) a private list::iterator
559 * pointing to @e D, not to @e A! To get to the head of the %list,
560 * we start at the tail and move forward by one. When this member
561 * iterator's next/previous pointers refer to itself, the %list is
564 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
565 class list
: protected _List_base
<_Tp
, _Alloc
>
567 #ifdef _GLIBCXX_CONCEPT_CHECKS
568 // concept requirements
569 typedef typename
_Alloc::value_type _Alloc_value_type
;
570 # if __cplusplus < 201103L
571 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
573 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
576 #if __cplusplus >= 201103L
577 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
578 "std::list must have a non-const, non-volatile value_type");
579 # if __cplusplus > 201703L || defined __STRICT_ANSI__
580 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
581 "std::list must have the same value_type as its allocator");
585 typedef _List_base
<_Tp
, _Alloc
> _Base
;
586 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
587 typedef typename
_Base::_Tp_alloc_traits _Tp_alloc_traits
;
588 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
589 typedef typename
_Base::_Node_alloc_traits _Node_alloc_traits
;
592 typedef _Tp value_type
;
593 typedef typename
_Tp_alloc_traits::pointer pointer
;
594 typedef typename
_Tp_alloc_traits::const_pointer const_pointer
;
595 typedef typename
_Tp_alloc_traits::reference reference
;
596 typedef typename
_Tp_alloc_traits::const_reference const_reference
;
597 typedef _List_iterator
<_Tp
> iterator
;
598 typedef _List_const_iterator
<_Tp
> const_iterator
;
599 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
600 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
601 typedef size_t size_type
;
602 typedef ptrdiff_t difference_type
;
603 typedef _Alloc allocator_type
;
606 // Note that pointers-to-_Node's can be ctor-converted to
608 typedef _List_node
<_Tp
> _Node
;
610 using _Base::_M_impl
;
611 using _Base::_M_put_node
;
612 using _Base::_M_get_node
;
613 using _Base::_M_get_Node_allocator
;
616 * @param __args An instance of user data.
618 * Allocates space for a new node and constructs a copy of
621 #if __cplusplus < 201103L
623 _M_create_node(const value_type
& __x
)
625 _Node
* __p
= this->_M_get_node();
628 _Tp_alloc_type
__alloc(_M_get_Node_allocator());
629 __alloc
.construct(__p
->_M_valptr(), __x
);
634 __throw_exception_again
;
639 template<typename
... _Args
>
641 _M_create_node(_Args
&&... __args
)
643 auto __p
= this->_M_get_node();
644 auto& __alloc
= _M_get_Node_allocator();
645 __allocated_ptr
<_Node_alloc_type
> __guard
{__alloc
, __p
};
646 _Node_alloc_traits::construct(__alloc
, __p
->_M_valptr(),
647 std::forward
<_Args
>(__args
)...);
653 #if _GLIBCXX_USE_CXX11_ABI
655 _S_distance(const_iterator __first
, const_iterator __last
)
656 { return std::distance(__first
, __last
); }
658 // return the stored size
660 _M_node_count() const
661 { return this->_M_get_size(); }
663 // dummy implementations used when the size is not stored
665 _S_distance(const_iterator
, const_iterator
)
668 // count the number of nodes
670 _M_node_count() const
671 { return std::distance(begin(), end()); }
675 // [23.2.2.1] construct/copy/destroy
676 // (assign() and get_allocator() are also listed in this section)
679 * @brief Creates a %list with no elements.
681 #if __cplusplus >= 201103L
688 * @brief Creates a %list with no elements.
689 * @param __a An allocator object.
692 list(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
693 : _Base(_Node_alloc_type(__a
)) { }
695 #if __cplusplus >= 201103L
697 * @brief Creates a %list with default constructed elements.
698 * @param __n The number of elements to initially create.
699 * @param __a An allocator object.
701 * This constructor fills the %list with @a __n default
702 * constructed elements.
705 list(size_type __n
, const allocator_type
& __a
= allocator_type())
706 : _Base(_Node_alloc_type(__a
))
707 { _M_default_initialize(__n
); }
710 * @brief Creates a %list with copies of an exemplar element.
711 * @param __n The number of elements to initially create.
712 * @param __value An element to copy.
713 * @param __a An allocator object.
715 * This constructor fills the %list with @a __n copies of @a __value.
717 list(size_type __n
, const value_type
& __value
,
718 const allocator_type
& __a
= allocator_type())
719 : _Base(_Node_alloc_type(__a
))
720 { _M_fill_initialize(__n
, __value
); }
723 * @brief Creates a %list with copies of an exemplar element.
724 * @param __n The number of elements to initially create.
725 * @param __value An element to copy.
726 * @param __a An allocator object.
728 * This constructor fills the %list with @a __n copies of @a __value.
731 list(size_type __n
, const value_type
& __value
= value_type(),
732 const allocator_type
& __a
= allocator_type())
733 : _Base(_Node_alloc_type(__a
))
734 { _M_fill_initialize(__n
, __value
); }
738 * @brief %List copy constructor.
739 * @param __x A %list of identical element and allocator types.
741 * The newly-created %list uses a copy of the allocation object used
742 * by @a __x (unless the allocator traits dictate a different object).
744 list(const list
& __x
)
745 : _Base(_Node_alloc_traits::
746 _S_select_on_copy(__x
._M_get_Node_allocator()))
747 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
749 #if __cplusplus >= 201103L
751 * @brief %List move constructor.
753 * The newly-created %list contains the exact contents of the moved
754 * instance. The contents of the moved instance are a valid, but
757 list(list
&&) = default;
760 * @brief Builds a %list from an initializer_list
761 * @param __l An initializer_list of value_type.
762 * @param __a An allocator object.
764 * Create a %list consisting of copies of the elements in the
765 * initializer_list @a __l. This is linear in __l.size().
767 list(initializer_list
<value_type
> __l
,
768 const allocator_type
& __a
= allocator_type())
769 : _Base(_Node_alloc_type(__a
))
770 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
772 list(const list
& __x
, const allocator_type
& __a
)
773 : _Base(_Node_alloc_type(__a
))
774 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
777 list(list
&& __x
, const allocator_type
& __a
, true_type
) noexcept
778 : _Base(_Node_alloc_type(__a
), std::move(__x
))
781 list(list
&& __x
, const allocator_type
& __a
, false_type
)
782 : _Base(_Node_alloc_type(__a
))
784 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
785 this->_M_move_nodes(std::move(__x
));
787 insert(begin(), std::__make_move_if_noexcept_iterator(__x
.begin()),
788 std::__make_move_if_noexcept_iterator(__x
.end()));
792 list(list
&& __x
, const allocator_type
& __a
)
793 noexcept(_Node_alloc_traits::_S_always_equal())
794 : list(std::move(__x
), __a
,
795 typename
_Node_alloc_traits::is_always_equal
{})
800 * @brief Builds a %list from a range.
801 * @param __first An input iterator.
802 * @param __last An input iterator.
803 * @param __a An allocator object.
805 * Create a %list consisting of copies of the elements from
806 * [@a __first,@a __last). This is linear in N (where N is
807 * distance(@a __first,@a __last)).
809 #if __cplusplus >= 201103L
810 template<typename _InputIterator
,
811 typename
= std::_RequireInputIter
<_InputIterator
>>
812 list(_InputIterator __first
, _InputIterator __last
,
813 const allocator_type
& __a
= allocator_type())
814 : _Base(_Node_alloc_type(__a
))
815 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
817 template<typename _InputIterator
>
818 list(_InputIterator __first
, _InputIterator __last
,
819 const allocator_type
& __a
= allocator_type())
820 : _Base(_Node_alloc_type(__a
))
822 // Check whether it's an integral type. If so, it's not an iterator.
823 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
824 _M_initialize_dispatch(__first
, __last
, _Integral());
828 #if __cplusplus >= 201103L
830 * No explicit dtor needed as the _Base dtor takes care of
831 * things. The _Base dtor only erases the elements, and note
832 * that if the elements themselves are pointers, the pointed-to
833 * memory is not touched in any way. Managing the pointer is
834 * the user's responsibility.
840 * @brief %List assignment operator.
841 * @param __x A %list of identical element and allocator types.
843 * All the elements of @a __x are copied.
845 * Whether the allocator is copied depends on the allocator traits.
848 operator=(const list
& __x
);
850 #if __cplusplus >= 201103L
852 * @brief %List move assignment operator.
853 * @param __x A %list of identical element and allocator types.
855 * The contents of @a __x are moved into this %list (without copying).
857 * Afterwards @a __x is a valid, but unspecified %list
859 * Whether the allocator is moved depends on the allocator traits.
862 operator=(list
&& __x
)
863 noexcept(_Node_alloc_traits::_S_nothrow_move())
865 constexpr bool __move_storage
=
866 _Node_alloc_traits::_S_propagate_on_move_assign()
867 || _Node_alloc_traits::_S_always_equal();
868 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
873 * @brief %List initializer list assignment operator.
874 * @param __l An initializer_list of value_type.
876 * Replace the contents of the %list with copies of the elements
877 * in the initializer_list @a __l. This is linear in l.size().
880 operator=(initializer_list
<value_type
> __l
)
882 this->assign(__l
.begin(), __l
.end());
888 * @brief Assigns a given value to a %list.
889 * @param __n Number of elements to be assigned.
890 * @param __val Value to be assigned.
892 * This function fills a %list with @a __n copies of the given
893 * value. Note that the assignment completely changes the %list
894 * and that the resulting %list's size is the same as the number
895 * of elements assigned.
898 assign(size_type __n
, const value_type
& __val
)
899 { _M_fill_assign(__n
, __val
); }
902 * @brief Assigns a range to a %list.
903 * @param __first An input iterator.
904 * @param __last An input iterator.
906 * This function fills a %list with copies of the elements in the
907 * range [@a __first,@a __last).
909 * Note that the assignment completely changes the %list and
910 * that the resulting %list's size is the same as the number of
913 #if __cplusplus >= 201103L
914 template<typename _InputIterator
,
915 typename
= std::_RequireInputIter
<_InputIterator
>>
917 assign(_InputIterator __first
, _InputIterator __last
)
918 { _M_assign_dispatch(__first
, __last
, __false_type()); }
920 template<typename _InputIterator
>
922 assign(_InputIterator __first
, _InputIterator __last
)
924 // Check whether it's an integral type. If so, it's not an iterator.
925 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
926 _M_assign_dispatch(__first
, __last
, _Integral());
930 #if __cplusplus >= 201103L
932 * @brief Assigns an initializer_list to a %list.
933 * @param __l An initializer_list of value_type.
935 * Replace the contents of the %list with copies of the elements
936 * in the initializer_list @a __l. This is linear in __l.size().
939 assign(initializer_list
<value_type
> __l
)
940 { this->_M_assign_dispatch(__l
.begin(), __l
.end(), __false_type()); }
943 /// Get a copy of the memory allocation object.
945 get_allocator() const _GLIBCXX_NOEXCEPT
946 { return allocator_type(_Base::_M_get_Node_allocator()); }
950 * Returns a read/write iterator that points to the first element in the
951 * %list. Iteration is done in ordinary element order.
955 begin() _GLIBCXX_NOEXCEPT
956 { return iterator(this->_M_impl
._M_node
._M_next
); }
959 * Returns a read-only (constant) iterator that points to the
960 * first element in the %list. Iteration is done in ordinary
965 begin() const _GLIBCXX_NOEXCEPT
966 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
969 * Returns a read/write iterator that points one past the last
970 * element in the %list. Iteration is done in ordinary element
975 end() _GLIBCXX_NOEXCEPT
976 { return iterator(&this->_M_impl
._M_node
); }
979 * Returns a read-only (constant) iterator that points one past
980 * the last element in the %list. Iteration is done in ordinary
985 end() const _GLIBCXX_NOEXCEPT
986 { return const_iterator(&this->_M_impl
._M_node
); }
989 * Returns a read/write reverse iterator that points to the last
990 * element in the %list. Iteration is done in reverse element
995 rbegin() _GLIBCXX_NOEXCEPT
996 { return reverse_iterator(end()); }
999 * Returns a read-only (constant) reverse iterator that points to
1000 * the last element in the %list. Iteration is done in reverse
1004 const_reverse_iterator
1005 rbegin() const _GLIBCXX_NOEXCEPT
1006 { return const_reverse_iterator(end()); }
1009 * Returns a read/write reverse iterator that points to one
1010 * before the first element in the %list. Iteration is done in
1011 * reverse element order.
1015 rend() _GLIBCXX_NOEXCEPT
1016 { return reverse_iterator(begin()); }
1019 * Returns a read-only (constant) reverse iterator that points to one
1020 * before the first element in the %list. Iteration is done in reverse
1024 const_reverse_iterator
1025 rend() const _GLIBCXX_NOEXCEPT
1026 { return const_reverse_iterator(begin()); }
1028 #if __cplusplus >= 201103L
1030 * Returns a read-only (constant) iterator that points to the
1031 * first element in the %list. Iteration is done in ordinary
1036 cbegin() const noexcept
1037 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
1040 * Returns a read-only (constant) iterator that points one past
1041 * the last element in the %list. Iteration is done in ordinary
1046 cend() const noexcept
1047 { return const_iterator(&this->_M_impl
._M_node
); }
1050 * Returns a read-only (constant) reverse iterator that points to
1051 * the last element in the %list. Iteration is done in reverse
1055 const_reverse_iterator
1056 crbegin() const noexcept
1057 { return const_reverse_iterator(end()); }
1060 * Returns a read-only (constant) reverse iterator that points to one
1061 * before the first element in the %list. Iteration is done in reverse
1065 const_reverse_iterator
1066 crend() const noexcept
1067 { return const_reverse_iterator(begin()); }
1070 // [23.2.2.2] capacity
1072 * Returns true if the %list is empty. (Thus begin() would equal
1075 _GLIBCXX_NODISCARD
bool
1076 empty() const _GLIBCXX_NOEXCEPT
1077 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
1079 /** Returns the number of elements in the %list. */
1082 size() const _GLIBCXX_NOEXCEPT
1083 { return _M_node_count(); }
1085 /** Returns the size() of the largest possible %list. */
1088 max_size() const _GLIBCXX_NOEXCEPT
1089 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1091 #if __cplusplus >= 201103L
1093 * @brief Resizes the %list to the specified number of elements.
1094 * @param __new_size Number of elements the %list should contain.
1096 * This function will %resize the %list to the specified number
1097 * of elements. If the number is smaller than the %list's
1098 * current size the %list is truncated, otherwise default
1099 * constructed elements are appended.
1102 resize(size_type __new_size
);
1105 * @brief Resizes the %list to the specified number of elements.
1106 * @param __new_size Number of elements the %list should contain.
1107 * @param __x Data with which new elements should be populated.
1109 * This function will %resize the %list to the specified number
1110 * of elements. If the number is smaller than the %list's
1111 * current size the %list is truncated, otherwise the %list is
1112 * extended and new elements are populated with given data.
1115 resize(size_type __new_size
, const value_type
& __x
);
1118 * @brief Resizes the %list to the specified number of elements.
1119 * @param __new_size Number of elements the %list should contain.
1120 * @param __x Data with which new elements should be populated.
1122 * This function will %resize the %list to the specified number
1123 * of elements. If the number is smaller than the %list's
1124 * current size the %list is truncated, otherwise the %list is
1125 * extended and new elements are populated with given data.
1128 resize(size_type __new_size
, value_type __x
= value_type());
1133 * Returns a read/write reference to the data at the first
1134 * element of the %list.
1138 front() _GLIBCXX_NOEXCEPT
1139 { return *begin(); }
1142 * Returns a read-only (constant) reference to the data at the first
1143 * element of the %list.
1147 front() const _GLIBCXX_NOEXCEPT
1148 { return *begin(); }
1151 * Returns a read/write reference to the data at the last element
1156 back() _GLIBCXX_NOEXCEPT
1158 iterator __tmp
= end();
1164 * Returns a read-only (constant) reference to the data at the last
1165 * element of the %list.
1169 back() const _GLIBCXX_NOEXCEPT
1171 const_iterator __tmp
= end();
1176 // [23.2.2.3] modifiers
1178 * @brief Add data to the front of the %list.
1179 * @param __x Data to be added.
1181 * This is a typical stack operation. The function creates an
1182 * element at the front of the %list and assigns the given data
1183 * to it. Due to the nature of a %list this operation can be
1184 * done in constant time, and does not invalidate iterators and
1188 push_front(const value_type
& __x
)
1189 { this->_M_insert(begin(), __x
); }
1191 #if __cplusplus >= 201103L
1193 push_front(value_type
&& __x
)
1194 { this->_M_insert(begin(), std::move(__x
)); }
1196 template<typename
... _Args
>
1197 #if __cplusplus > 201402L
1202 emplace_front(_Args
&&... __args
)
1204 this->_M_insert(begin(), std::forward
<_Args
>(__args
)...);
1205 #if __cplusplus > 201402L
1212 * @brief Removes first element.
1214 * This is a typical stack operation. It shrinks the %list by
1215 * one. Due to the nature of a %list this operation can be done
1216 * in constant time, and only invalidates iterators/references to
1217 * the element being removed.
1219 * Note that no data is returned, and if the first element's data
1220 * is needed, it should be retrieved before pop_front() is
1224 pop_front() _GLIBCXX_NOEXCEPT
1225 { this->_M_erase(begin()); }
1228 * @brief Add data to the end of the %list.
1229 * @param __x Data to be added.
1231 * This is a typical stack operation. The function creates an
1232 * element at the end of the %list and assigns the given data to
1233 * it. Due to the nature of a %list this operation can be done
1234 * in constant time, and does not invalidate iterators and
1238 push_back(const value_type
& __x
)
1239 { this->_M_insert(end(), __x
); }
1241 #if __cplusplus >= 201103L
1243 push_back(value_type
&& __x
)
1244 { this->_M_insert(end(), std::move(__x
)); }
1246 template<typename
... _Args
>
1247 #if __cplusplus > 201402L
1252 emplace_back(_Args
&&... __args
)
1254 this->_M_insert(end(), std::forward
<_Args
>(__args
)...);
1255 #if __cplusplus > 201402L
1262 * @brief Removes last element.
1264 * This is a typical stack operation. It shrinks the %list by
1265 * one. Due to the nature of a %list this operation can be done
1266 * in constant time, and only invalidates iterators/references to
1267 * the element being removed.
1269 * Note that no data is returned, and if the last element's data
1270 * is needed, it should be retrieved before pop_back() is called.
1273 pop_back() _GLIBCXX_NOEXCEPT
1274 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1276 #if __cplusplus >= 201103L
1278 * @brief Constructs object in %list before specified iterator.
1279 * @param __position A const_iterator into the %list.
1280 * @param __args Arguments.
1281 * @return An iterator that points to the inserted data.
1283 * This function will insert an object of type T constructed
1284 * with T(std::forward<Args>(args)...) before the specified
1285 * location. Due to the nature of a %list this operation can
1286 * be done in constant time, and does not invalidate iterators
1289 template<typename
... _Args
>
1291 emplace(const_iterator __position
, _Args
&&... __args
);
1294 * @brief Inserts given value into %list before specified iterator.
1295 * @param __position A const_iterator into the %list.
1296 * @param __x Data to be inserted.
1297 * @return An iterator that points to the inserted data.
1299 * This function will insert a copy of the given value before
1300 * the specified location. Due to the nature of a %list this
1301 * operation can be done in constant time, and does not
1302 * invalidate iterators and references.
1305 insert(const_iterator __position
, const value_type
& __x
);
1308 * @brief Inserts given value into %list before specified iterator.
1309 * @param __position An iterator into the %list.
1310 * @param __x Data to be inserted.
1311 * @return An iterator that points to the inserted data.
1313 * This function will insert a copy of the given value before
1314 * the specified location. Due to the nature of a %list this
1315 * operation can be done in constant time, and does not
1316 * invalidate iterators and references.
1319 insert(iterator __position
, const value_type
& __x
);
1322 #if __cplusplus >= 201103L
1324 * @brief Inserts given rvalue into %list before specified iterator.
1325 * @param __position A const_iterator into the %list.
1326 * @param __x Data to be inserted.
1327 * @return An iterator that points to the inserted data.
1329 * This function will insert a copy of the given rvalue before
1330 * the specified location. Due to the nature of a %list this
1331 * operation can be done in constant time, and does not
1332 * invalidate iterators and references.
1335 insert(const_iterator __position
, value_type
&& __x
)
1336 { return emplace(__position
, std::move(__x
)); }
1339 * @brief Inserts the contents of an initializer_list into %list
1340 * before specified const_iterator.
1341 * @param __p A const_iterator into the %list.
1342 * @param __l An initializer_list of value_type.
1343 * @return An iterator pointing to the first element inserted
1346 * This function will insert copies of the data in the
1347 * initializer_list @a l into the %list before the location
1348 * specified by @a p.
1350 * This operation is linear in the number of elements inserted and
1351 * does not invalidate iterators and references.
1354 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1355 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1358 #if __cplusplus >= 201103L
1360 * @brief Inserts a number of copies of given data into the %list.
1361 * @param __position A const_iterator into the %list.
1362 * @param __n Number of elements to be inserted.
1363 * @param __x Data to be inserted.
1364 * @return An iterator pointing to the first element inserted
1367 * This function will insert a specified number of copies of the
1368 * given data before the location specified by @a position.
1370 * This operation is linear in the number of elements inserted and
1371 * does not invalidate iterators and references.
1374 insert(const_iterator __position
, size_type __n
, const value_type
& __x
);
1377 * @brief Inserts a number of copies of given data into the %list.
1378 * @param __position An iterator into the %list.
1379 * @param __n Number of elements to be inserted.
1380 * @param __x Data to be inserted.
1382 * This function will insert a specified number of copies of the
1383 * given data before the location specified by @a position.
1385 * This operation is linear in the number of elements inserted and
1386 * does not invalidate iterators and references.
1389 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1391 list
__tmp(__n
, __x
, get_allocator());
1392 splice(__position
, __tmp
);
1396 #if __cplusplus >= 201103L
1398 * @brief Inserts a range into the %list.
1399 * @param __position A const_iterator into the %list.
1400 * @param __first An input iterator.
1401 * @param __last An input iterator.
1402 * @return An iterator pointing to the first element inserted
1405 * This function will insert copies of the data in the range [@a
1406 * first,@a last) into the %list before the location specified by
1409 * This operation is linear in the number of elements inserted and
1410 * does not invalidate iterators and references.
1412 template<typename _InputIterator
,
1413 typename
= std::_RequireInputIter
<_InputIterator
>>
1415 insert(const_iterator __position
, _InputIterator __first
,
1416 _InputIterator __last
);
1419 * @brief Inserts a range into the %list.
1420 * @param __position An iterator into the %list.
1421 * @param __first An input iterator.
1422 * @param __last An input iterator.
1424 * This function will insert copies of the data in the range [@a
1425 * first,@a last) into the %list before the location specified by
1428 * This operation is linear in the number of elements inserted and
1429 * does not invalidate iterators and references.
1431 template<typename _InputIterator
>
1433 insert(iterator __position
, _InputIterator __first
,
1434 _InputIterator __last
)
1436 list
__tmp(__first
, __last
, get_allocator());
1437 splice(__position
, __tmp
);
1442 * @brief Remove element at given position.
1443 * @param __position Iterator pointing to element to be erased.
1444 * @return An iterator pointing to the next element (or end()).
1446 * This function will erase the element at the given position and thus
1447 * shorten the %list by one.
1449 * Due to the nature of a %list this operation can be done in
1450 * constant time, and only invalidates iterators/references to
1451 * the element being removed. The user is also cautioned that
1452 * this function only erases the element, and that if the element
1453 * is itself a pointer, the pointed-to memory is not touched in
1454 * any way. Managing the pointer is the user's responsibility.
1457 #if __cplusplus >= 201103L
1458 erase(const_iterator __position
) noexcept
;
1460 erase(iterator __position
);
1464 * @brief Remove a range of elements.
1465 * @param __first Iterator pointing to the first element to be erased.
1466 * @param __last Iterator pointing to one past the last element to be
1468 * @return An iterator pointing to the element pointed to by @a last
1469 * prior to erasing (or end()).
1471 * This function will erase the elements in the range @a
1472 * [first,last) and shorten the %list accordingly.
1474 * This operation is linear time in the size of the range and only
1475 * invalidates iterators/references to the element being removed.
1476 * The user is also cautioned that this function only erases the
1477 * elements, and that if the elements themselves are pointers, the
1478 * pointed-to memory is not touched in any way. Managing the pointer
1479 * is the user's responsibility.
1482 #if __cplusplus >= 201103L
1483 erase(const_iterator __first
, const_iterator __last
) noexcept
1485 erase(iterator __first
, iterator __last
)
1488 while (__first
!= __last
)
1489 __first
= erase(__first
);
1490 return __last
._M_const_cast();
1494 * @brief Swaps data with another %list.
1495 * @param __x A %list of the same element and allocator types.
1497 * This exchanges the elements between two lists in constant
1498 * time. Note that the global std::swap() function is
1499 * specialized such that std::swap(l1,l2) will feed to this
1502 * Whether the allocators are swapped depends on the allocator traits.
1505 swap(list
& __x
) _GLIBCXX_NOEXCEPT
1507 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1508 __x
._M_impl
._M_node
);
1510 size_t __xsize
= __x
._M_get_size();
1511 __x
._M_set_size(this->_M_get_size());
1512 this->_M_set_size(__xsize
);
1514 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1515 __x
._M_get_Node_allocator());
1519 * Erases all the elements. Note that this function only erases
1520 * the elements, and that if the elements themselves are
1521 * pointers, the pointed-to memory is not touched in any way.
1522 * Managing the pointer is the user's responsibility.
1525 clear() _GLIBCXX_NOEXCEPT
1531 // [23.2.2.4] list operations
1533 * @brief Insert contents of another %list.
1534 * @param __position Iterator referencing the element to insert before.
1535 * @param __x Source list.
1537 * The elements of @a __x are inserted in constant time in front of
1538 * the element referenced by @a __position. @a __x becomes an empty
1541 * Requires this != @a __x.
1544 #if __cplusplus >= 201103L
1545 splice(const_iterator __position
, list
&& __x
) noexcept
1547 splice(iterator __position
, list
& __x
)
1552 _M_check_equal_allocators(__x
);
1554 this->_M_transfer(__position
._M_const_cast(),
1555 __x
.begin(), __x
.end());
1557 this->_M_inc_size(__x
._M_get_size());
1562 #if __cplusplus >= 201103L
1564 splice(const_iterator __position
, list
& __x
) noexcept
1565 { splice(__position
, std::move(__x
)); }
1568 #if __cplusplus >= 201103L
1570 * @brief Insert element from another %list.
1571 * @param __position Const_iterator referencing the element to
1573 * @param __x Source list.
1574 * @param __i Const_iterator referencing the element to move.
1576 * Removes the element in list @a __x referenced by @a __i and
1577 * inserts it into the current list before @a __position.
1580 splice(const_iterator __position
, list
&& __x
, const_iterator __i
) noexcept
1583 * @brief Insert element from another %list.
1584 * @param __position Iterator referencing the element to insert before.
1585 * @param __x Source list.
1586 * @param __i Iterator referencing the element to move.
1588 * Removes the element in list @a __x referenced by @a __i and
1589 * inserts it into the current list before @a __position.
1592 splice(iterator __position
, list
& __x
, iterator __i
)
1595 iterator __j
= __i
._M_const_cast();
1597 if (__position
== __i
|| __position
== __j
)
1600 if (this != std::__addressof(__x
))
1601 _M_check_equal_allocators(__x
);
1603 this->_M_transfer(__position
._M_const_cast(),
1604 __i
._M_const_cast(), __j
);
1606 this->_M_inc_size(1);
1610 #if __cplusplus >= 201103L
1612 * @brief Insert element from another %list.
1613 * @param __position Const_iterator referencing the element to
1615 * @param __x Source list.
1616 * @param __i Const_iterator referencing the element to move.
1618 * Removes the element in list @a __x referenced by @a __i and
1619 * inserts it into the current list before @a __position.
1622 splice(const_iterator __position
, list
& __x
, const_iterator __i
) noexcept
1623 { splice(__position
, std::move(__x
), __i
); }
1626 #if __cplusplus >= 201103L
1628 * @brief Insert range from another %list.
1629 * @param __position Const_iterator referencing the element to
1631 * @param __x Source list.
1632 * @param __first Const_iterator referencing the start of range in x.
1633 * @param __last Const_iterator referencing the end of range in x.
1635 * Removes elements in the range [__first,__last) and inserts them
1636 * before @a __position in constant time.
1638 * Undefined if @a __position is in [__first,__last).
1641 splice(const_iterator __position
, list
&& __x
, const_iterator __first
,
1642 const_iterator __last
) noexcept
1645 * @brief Insert range from another %list.
1646 * @param __position Iterator referencing the element to insert before.
1647 * @param __x Source list.
1648 * @param __first Iterator referencing the start of range in x.
1649 * @param __last Iterator referencing the end of range in x.
1651 * Removes elements in the range [__first,__last) and inserts them
1652 * before @a __position in constant time.
1654 * Undefined if @a __position is in [__first,__last).
1657 splice(iterator __position
, list
& __x
, iterator __first
,
1661 if (__first
!= __last
)
1663 if (this != std::__addressof(__x
))
1664 _M_check_equal_allocators(__x
);
1666 size_t __n
= _S_distance(__first
, __last
);
1667 this->_M_inc_size(__n
);
1668 __x
._M_dec_size(__n
);
1670 this->_M_transfer(__position
._M_const_cast(),
1671 __first
._M_const_cast(),
1672 __last
._M_const_cast());
1676 #if __cplusplus >= 201103L
1678 * @brief Insert range from another %list.
1679 * @param __position Const_iterator referencing the element to
1681 * @param __x Source list.
1682 * @param __first Const_iterator referencing the start of range in x.
1683 * @param __last Const_iterator referencing the end of range in x.
1685 * Removes elements in the range [__first,__last) and inserts them
1686 * before @a __position in constant time.
1688 * Undefined if @a __position is in [__first,__last).
1691 splice(const_iterator __position
, list
& __x
, const_iterator __first
,
1692 const_iterator __last
) noexcept
1693 { splice(__position
, std::move(__x
), __first
, __last
); }
1697 #if __cplusplus > 201703L
1698 # define __cpp_lib_list_remove_return_type 201806L
1699 typedef size_type __remove_return_type
;
1700 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG \
1701 __attribute__((__abi_tag__("__cxx20")))
1703 typedef void __remove_return_type
;
1704 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1709 * @brief Remove all elements equal to value.
1710 * @param __value The value to remove.
1712 * Removes every element in the list equal to @a value.
1713 * Remaining elements stay in list order. Note that this
1714 * function only erases the elements, and that if the elements
1715 * themselves are pointers, the pointed-to memory is not
1716 * touched in any way. Managing the pointer is the user's
1719 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1720 __remove_return_type
1721 remove(const _Tp
& __value
);
1724 * @brief Remove all elements satisfying a predicate.
1725 * @tparam _Predicate Unary predicate function or object.
1727 * Removes every element in the list for which the predicate
1728 * returns true. Remaining elements stay in list order. Note
1729 * that this function only erases the elements, and that if the
1730 * elements themselves are pointers, the pointed-to memory is
1731 * not touched in any way. Managing the pointer is the user's
1734 template<typename _Predicate
>
1735 __remove_return_type
1736 remove_if(_Predicate
);
1739 * @brief Remove consecutive duplicate elements.
1741 * For each consecutive set of elements with the same value,
1742 * remove all but the first one. Remaining elements stay in
1743 * list order. Note that this function only erases the
1744 * elements, and that if the elements themselves are pointers,
1745 * the pointed-to memory is not touched in any way. Managing
1746 * the pointer is the user's responsibility.
1748 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1749 __remove_return_type
1753 * @brief Remove consecutive elements satisfying a predicate.
1754 * @tparam _BinaryPredicate Binary predicate function or object.
1756 * For each consecutive set of elements [first,last) that
1757 * satisfy predicate(first,i) where i is an iterator in
1758 * [first,last), remove all but the first one. Remaining
1759 * elements stay in list order. Note that this function only
1760 * erases the elements, and that if the elements themselves are
1761 * pointers, the pointed-to memory is not touched in any way.
1762 * Managing the pointer is the user's responsibility.
1764 template<typename _BinaryPredicate
>
1765 __remove_return_type
1766 unique(_BinaryPredicate
);
1768 #undef _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1771 * @brief Merge sorted lists.
1772 * @param __x Sorted list to merge.
1774 * Assumes that both @a __x and this list are sorted according to
1775 * operator<(). Merges elements of @a __x into this list in
1776 * sorted order, leaving @a __x empty when complete. Elements in
1777 * this list precede elements in @a __x that are equal.
1779 #if __cplusplus >= 201103L
1785 { merge(std::move(__x
)); }
1792 * @brief Merge sorted lists according to comparison function.
1793 * @tparam _StrictWeakOrdering Comparison function defining
1795 * @param __x Sorted list to merge.
1796 * @param __comp Comparison functor.
1798 * Assumes that both @a __x and this list are sorted according to
1799 * StrictWeakOrdering. Merges elements of @a __x into this list
1800 * in sorted order, leaving @a __x empty when complete. Elements
1801 * in this list precede elements in @a __x that are equivalent
1802 * according to StrictWeakOrdering().
1804 #if __cplusplus >= 201103L
1805 template<typename _StrictWeakOrdering
>
1807 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1809 template<typename _StrictWeakOrdering
>
1811 merge(list
& __x
, _StrictWeakOrdering __comp
)
1812 { merge(std::move(__x
), __comp
); }
1814 template<typename _StrictWeakOrdering
>
1816 merge(list
& __x
, _StrictWeakOrdering __comp
);
1820 * @brief Reverse the elements in list.
1822 * Reverse the order of elements in the list in linear time.
1825 reverse() _GLIBCXX_NOEXCEPT
1826 { this->_M_impl
._M_node
._M_reverse(); }
1829 * @brief Sort the elements.
1831 * Sorts the elements of this list in NlogN time. Equivalent
1832 * elements remain in list order.
1838 * @brief Sort the elements according to comparison function.
1840 * Sorts the elements of this list in NlogN time. Equivalent
1841 * elements remain in list order.
1843 template<typename _StrictWeakOrdering
>
1845 sort(_StrictWeakOrdering
);
1848 // Internal constructor functions follow.
1850 // Called by the range constructor to implement [23.1.1]/9
1852 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1853 // 438. Ambiguity in the "do the right thing" clause
1854 template<typename _Integer
>
1856 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1857 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1859 // Called by the range constructor to implement [23.1.1]/9
1860 template<typename _InputIterator
>
1862 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1865 for (; __first
!= __last
; ++__first
)
1866 #if __cplusplus >= 201103L
1867 emplace_back(*__first
);
1869 push_back(*__first
);
1873 // Called by list(n,v,a), and the range constructor when it turns out
1874 // to be the same thing.
1876 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1882 #if __cplusplus >= 201103L
1883 // Called by list(n).
1885 _M_default_initialize(size_type __n
)
1891 // Called by resize(sz).
1893 _M_default_append(size_type __n
);
1896 // Internal assign functions follow.
1898 // Called by the range assign to implement [23.1.1]/9
1900 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1901 // 438. Ambiguity in the "do the right thing" clause
1902 template<typename _Integer
>
1904 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1905 { _M_fill_assign(__n
, __val
); }
1907 // Called by the range assign to implement [23.1.1]/9
1908 template<typename _InputIterator
>
1910 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1913 // Called by assign(n,t), and the range assign when it turns out
1914 // to be the same thing.
1916 _M_fill_assign(size_type __n
, const value_type
& __val
);
1919 // Moves the elements from [first,last) before position.
1921 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1922 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1924 // Inserts new element at position given and with value given.
1925 #if __cplusplus < 201103L
1927 _M_insert(iterator __position
, const value_type
& __x
)
1929 _Node
* __tmp
= _M_create_node(__x
);
1930 __tmp
->_M_hook(__position
._M_node
);
1931 this->_M_inc_size(1);
1934 template<typename
... _Args
>
1936 _M_insert(iterator __position
, _Args
&&... __args
)
1938 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1939 __tmp
->_M_hook(__position
._M_node
);
1940 this->_M_inc_size(1);
1944 // Erases element at position given.
1946 _M_erase(iterator __position
) _GLIBCXX_NOEXCEPT
1948 this->_M_dec_size(1);
1949 __position
._M_node
->_M_unhook();
1950 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1951 #if __cplusplus >= 201103L
1952 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n
->_M_valptr());
1954 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n
->_M_valptr());
1960 // To implement the splice (and merge) bits of N1599.
1962 _M_check_equal_allocators(list
& __x
) _GLIBCXX_NOEXCEPT
1964 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1965 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1969 // Used to implement resize.
1971 _M_resize_pos(size_type
& __new_size
) const;
1973 #if __cplusplus >= 201103L
1975 _M_move_assign(list
&& __x
, true_type
) noexcept
1978 this->_M_move_nodes(std::move(__x
));
1979 std::__alloc_on_move(this->_M_get_Node_allocator(),
1980 __x
._M_get_Node_allocator());
1984 _M_move_assign(list
&& __x
, false_type
)
1986 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1987 _M_move_assign(std::move(__x
), true_type
{});
1989 // The rvalue's allocator cannot be moved, or is not equal,
1990 // so we need to individually move each element.
1991 _M_assign_dispatch(std::make_move_iterator(__x
.begin()),
1992 std::make_move_iterator(__x
.end()),
1997 #if _GLIBCXX_USE_CXX11_ABI
1998 // Update _M_size members after merging (some of) __src into __dest.
1999 struct _Finalize_merge
2002 _Finalize_merge(list
& __dest
, list
& __src
, const iterator
& __src_next
)
2003 : _M_dest(__dest
), _M_src(__src
), _M_next(__src_next
)
2008 // For the common case, _M_next == _M_sec.end() and the std::distance
2009 // call is fast. But if any *iter1 < *iter2 comparison throws then we
2010 // have to count how many elements remain in _M_src.
2011 const size_t __num_unmerged
= std::distance(_M_next
, _M_src
.end());
2012 const size_t __orig_size
= _M_src
._M_get_size();
2013 _M_dest
._M_inc_size(__orig_size
- __num_unmerged
);
2014 _M_src
._M_set_size(__num_unmerged
);
2019 const iterator
& _M_next
;
2021 #if __cplusplus >= 201103L
2022 _Finalize_merge(const _Finalize_merge
&) = delete;
2026 struct _Finalize_merge
2027 { explicit _Finalize_merge(list
&, list
&, const iterator
&) { } };
2032 #if __cpp_deduction_guides >= 201606
2033 template<typename _InputIterator
, typename _ValT
2034 = typename iterator_traits
<_InputIterator
>::value_type
,
2035 typename _Allocator
= allocator
<_ValT
>,
2036 typename
= _RequireInputIter
<_InputIterator
>,
2037 typename
= _RequireAllocator
<_Allocator
>>
2038 list(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
2039 -> list
<_ValT
, _Allocator
>;
2042 _GLIBCXX_END_NAMESPACE_CXX11
2045 * @brief List equality comparison.
2046 * @param __x A %list.
2047 * @param __y A %list of the same type as @a __x.
2048 * @return True iff the size and elements of the lists are equal.
2050 * This is an equivalence relation. It is linear in the size of
2051 * the lists. Lists are considered equivalent if their sizes are
2052 * equal, and if corresponding elements compare equal.
2054 template<typename _Tp
, typename _Alloc
>
2057 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2059 #if _GLIBCXX_USE_CXX11_ABI
2060 if (__x
.size() != __y
.size())
2064 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
2065 const_iterator __end1
= __x
.end();
2066 const_iterator __end2
= __y
.end();
2068 const_iterator __i1
= __x
.begin();
2069 const_iterator __i2
= __y
.begin();
2070 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
2075 return __i1
== __end1
&& __i2
== __end2
;
2078 #if __cpp_lib_three_way_comparison
2080 * @brief List ordering relation.
2081 * @param __x A `list`.
2082 * @param __y A `list` of the same type as `__x`.
2083 * @return A value indicating whether `__x` is less than, equal to,
2084 * greater than, or incomparable with `__y`.
2086 * See `std::lexicographical_compare_three_way()` for how the determination
2087 * is made. This operator is used to synthesize relational operators like
2090 template<typename _Tp
, typename _Alloc
>
2092 inline __detail::__synth3way_t
<_Tp
>
2093 operator<=>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2095 return std::lexicographical_compare_three_way(__x
.begin(), __x
.end(),
2096 __y
.begin(), __y
.end(),
2097 __detail::__synth3way
);
2101 * @brief List ordering relation.
2102 * @param __x A %list.
2103 * @param __y A %list of the same type as @a __x.
2104 * @return True iff @a __x is lexicographically less than @a __y.
2106 * This is a total ordering relation. It is linear in the size of the
2107 * lists. The elements must be comparable with @c <.
2109 * See std::lexicographical_compare() for how the determination is made.
2111 template<typename _Tp
, typename _Alloc
>
2114 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2115 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2116 __y
.begin(), __y
.end()); }
2118 /// Based on operator==
2119 template<typename _Tp
, typename _Alloc
>
2122 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2123 { return !(__x
== __y
); }
2125 /// Based on operator<
2126 template<typename _Tp
, typename _Alloc
>
2129 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2130 { return __y
< __x
; }
2132 /// Based on operator<
2133 template<typename _Tp
, typename _Alloc
>
2136 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2137 { return !(__y
< __x
); }
2139 /// Based on operator<
2140 template<typename _Tp
, typename _Alloc
>
2143 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2144 { return !(__x
< __y
); }
2145 #endif // three-way comparison
2147 /// See std::list::swap().
2148 template<typename _Tp
, typename _Alloc
>
2150 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
2151 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2154 _GLIBCXX_END_NAMESPACE_CONTAINER
2156 #if _GLIBCXX_USE_CXX11_ABI
2158 // Detect when distance is used to compute the size of the whole list.
2159 template<typename _Tp
>
2161 __distance(_GLIBCXX_STD_C::_List_iterator
<_Tp
> __first
,
2162 _GLIBCXX_STD_C::_List_iterator
<_Tp
> __last
,
2163 input_iterator_tag __tag
)
2165 typedef _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> _CIter
;
2166 return std::__distance(_CIter(__first
), _CIter(__last
), __tag
);
2169 template<typename _Tp
>
2171 __distance(_GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __first
,
2172 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __last
,
2175 typedef __detail::_List_node_header _Sentinel
;
2176 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __beyond
= __last
;
2178 const bool __whole
= __first
== __beyond
;
2179 if (__builtin_constant_p (__whole
) && __whole
)
2180 return static_cast<const _Sentinel
*>(__last
._M_node
)->_M_size
;
2183 while (__first
!= __last
)
2192 _GLIBCXX_END_NAMESPACE_VERSION
2195 #endif /* _STL_LIST_H */