1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_list.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{list}
59 #include <bits/concept_check.h>
60 #include <ext/alloc_traits.h>
61 #if __cplusplus >= 201103L
62 #include <initializer_list>
63 #include <bits/allocated_ptr.h>
64 #include <ext/aligned_buffer.h>
67 namespace std
_GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 // Supporting structures are split into common and templated
74 // types; the latter publicly inherits from the former in an
75 // effort to reduce code duplication. This results in some
76 // "needless" static_cast'ing later on, but it's all safe
79 /// Common part of a node in the %list.
80 struct _List_node_base
82 _List_node_base
* _M_next
;
83 _List_node_base
* _M_prev
;
86 swap(_List_node_base
& __x
, _List_node_base
& __y
) _GLIBCXX_USE_NOEXCEPT
;
89 _M_transfer(_List_node_base
* const __first
,
90 _List_node_base
* const __last
) _GLIBCXX_USE_NOEXCEPT
;
93 _M_reverse() _GLIBCXX_USE_NOEXCEPT
;
96 _M_hook(_List_node_base
* const __position
) _GLIBCXX_USE_NOEXCEPT
;
99 _M_unhook() _GLIBCXX_USE_NOEXCEPT
;
102 /// The %list node header.
103 struct _List_node_header
: public _List_node_base
105 #if _GLIBCXX_USE_CXX11_ABI
109 _List_node_header() _GLIBCXX_NOEXCEPT
112 #if __cplusplus >= 201103L
113 _List_node_header(_List_node_header
&& __x
) noexcept
114 : _List_node_base
{ __x
._M_next
, __x
._M_prev
}
115 # if _GLIBCXX_USE_CXX11_ABI
116 , _M_size(__x
._M_size
)
119 if (__x
._M_base()->_M_next
== __x
._M_base())
120 this->_M_next
= this->_M_prev
= this;
123 this->_M_next
->_M_prev
= this->_M_prev
->_M_next
= this->_M_base();
129 _M_move_nodes(_List_node_header
&& __x
)
131 _List_node_base
* const __xnode
= __x
._M_base();
132 if (__xnode
->_M_next
== __xnode
)
136 _List_node_base
* const __node
= this->_M_base();
137 __node
->_M_next
= __xnode
->_M_next
;
138 __node
->_M_prev
= __xnode
->_M_prev
;
139 __node
->_M_next
->_M_prev
= __node
->_M_prev
->_M_next
= __node
;
140 # if _GLIBCXX_USE_CXX11_ABI
141 _M_size
= __x
._M_size
;
149 _M_init() _GLIBCXX_NOEXCEPT
151 this->_M_next
= this->_M_prev
= this;
152 #if _GLIBCXX_USE_CXX11_ABI
158 _List_node_base
* _M_base() { return this; }
160 } // namespace detail
162 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
164 /// An actual node in the %list.
165 template<typename _Tp
>
166 struct _List_node
: public __detail::_List_node_base
168 #if __cplusplus >= 201103L
169 __gnu_cxx::__aligned_membuf
<_Tp
> _M_storage
;
170 _Tp
* _M_valptr() { return _M_storage
._M_ptr(); }
171 _Tp
const* _M_valptr() const { return _M_storage
._M_ptr(); }
174 _Tp
* _M_valptr() { return std::__addressof(_M_data
); }
175 _Tp
const* _M_valptr() const { return std::__addressof(_M_data
); }
180 * @brief A list::iterator.
182 * All the functions are op overloads.
184 template<typename _Tp
>
185 struct _List_iterator
187 typedef _List_iterator
<_Tp
> _Self
;
188 typedef _List_node
<_Tp
> _Node
;
190 typedef ptrdiff_t difference_type
;
191 typedef std::bidirectional_iterator_tag iterator_category
;
192 typedef _Tp value_type
;
193 typedef _Tp
* pointer
;
194 typedef _Tp
& reference
;
196 _List_iterator() _GLIBCXX_NOEXCEPT
200 _List_iterator(__detail::_List_node_base
* __x
) _GLIBCXX_NOEXCEPT
204 _M_const_cast() const _GLIBCXX_NOEXCEPT
207 // Must downcast from _List_node_base to _List_node to get to value.
209 operator*() const _GLIBCXX_NOEXCEPT
210 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
213 operator->() const _GLIBCXX_NOEXCEPT
214 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
217 operator++() _GLIBCXX_NOEXCEPT
219 _M_node
= _M_node
->_M_next
;
224 operator++(int) _GLIBCXX_NOEXCEPT
227 _M_node
= _M_node
->_M_next
;
232 operator--() _GLIBCXX_NOEXCEPT
234 _M_node
= _M_node
->_M_prev
;
239 operator--(int) _GLIBCXX_NOEXCEPT
242 _M_node
= _M_node
->_M_prev
;
247 operator==(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
248 { return _M_node
== __x
._M_node
; }
251 operator!=(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
252 { return _M_node
!= __x
._M_node
; }
254 // The only member points to the %list element.
255 __detail::_List_node_base
* _M_node
;
259 * @brief A list::const_iterator.
261 * All the functions are op overloads.
263 template<typename _Tp
>
264 struct _List_const_iterator
266 typedef _List_const_iterator
<_Tp
> _Self
;
267 typedef const _List_node
<_Tp
> _Node
;
268 typedef _List_iterator
<_Tp
> iterator
;
270 typedef ptrdiff_t difference_type
;
271 typedef std::bidirectional_iterator_tag iterator_category
;
272 typedef _Tp value_type
;
273 typedef const _Tp
* pointer
;
274 typedef const _Tp
& reference
;
276 _List_const_iterator() _GLIBCXX_NOEXCEPT
280 _List_const_iterator(const __detail::_List_node_base
* __x
)
284 _List_const_iterator(const iterator
& __x
) _GLIBCXX_NOEXCEPT
285 : _M_node(__x
._M_node
) { }
288 _M_const_cast() const _GLIBCXX_NOEXCEPT
289 { return iterator(const_cast<__detail::_List_node_base
*>(_M_node
)); }
291 // Must downcast from List_node_base to _List_node to get to value.
293 operator*() const _GLIBCXX_NOEXCEPT
294 { return *static_cast<_Node
*>(_M_node
)->_M_valptr(); }
297 operator->() const _GLIBCXX_NOEXCEPT
298 { return static_cast<_Node
*>(_M_node
)->_M_valptr(); }
301 operator++() _GLIBCXX_NOEXCEPT
303 _M_node
= _M_node
->_M_next
;
308 operator++(int) _GLIBCXX_NOEXCEPT
311 _M_node
= _M_node
->_M_next
;
316 operator--() _GLIBCXX_NOEXCEPT
318 _M_node
= _M_node
->_M_prev
;
323 operator--(int) _GLIBCXX_NOEXCEPT
326 _M_node
= _M_node
->_M_prev
;
331 operator==(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
332 { return _M_node
== __x
._M_node
; }
335 operator!=(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
336 { return _M_node
!= __x
._M_node
; }
338 // The only member points to the %list element.
339 const __detail::_List_node_base
* _M_node
;
342 template<typename _Val
>
344 operator==(const _List_iterator
<_Val
>& __x
,
345 const _List_const_iterator
<_Val
>& __y
) _GLIBCXX_NOEXCEPT
346 { return __x
._M_node
== __y
._M_node
; }
348 template<typename _Val
>
350 operator!=(const _List_iterator
<_Val
>& __x
,
351 const _List_const_iterator
<_Val
>& __y
) _GLIBCXX_NOEXCEPT
352 { return __x
._M_node
!= __y
._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( noexcept(_Node_alloc_type()) )
391 _List_impl(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
392 : _Node_alloc_type(__a
)
395 #if __cplusplus >= 201103L
396 _List_impl(_List_impl
&&) = default;
398 _List_impl(_Node_alloc_type
&& __a
, _List_impl
&& __x
)
399 : _Node_alloc_type(std::move(__a
)), _M_node(std::move(__x
._M_node
))
402 _List_impl(_Node_alloc_type
&& __a
) noexcept
403 : _Node_alloc_type(std::move(__a
))
410 #if _GLIBCXX_USE_CXX11_ABI
411 size_t _M_get_size() const { return _M_impl
._M_node
._M_size
; }
413 void _M_set_size(size_t __n
) { _M_impl
._M_node
._M_size
= __n
; }
415 void _M_inc_size(size_t __n
) { _M_impl
._M_node
._M_size
+= __n
; }
417 void _M_dec_size(size_t __n
) { _M_impl
._M_node
._M_size
-= __n
; }
419 # if !_GLIBCXX_INLINE_VERSION
421 _M_distance(const __detail::_List_node_base
* __first
,
422 const __detail::_List_node_base
* __last
) const
423 { return _S_distance(__first
, __last
); }
425 // return the stored size
426 size_t _M_node_count() const { return _M_get_size(); }
429 // dummy implementations used when the size is not stored
430 size_t _M_get_size() const { return 0; }
431 void _M_set_size(size_t) { }
432 void _M_inc_size(size_t) { }
433 void _M_dec_size(size_t) { }
435 # if !_GLIBCXX_INLINE_VERSION
436 size_t _M_distance(const void*, const void*) const { return 0; }
438 // count the number of nodes
439 size_t _M_node_count() const
441 return _S_distance(_M_impl
._M_node
._M_next
,
442 std::__addressof(_M_impl
._M_node
));
447 typename
_Node_alloc_traits::pointer
449 { return _Node_alloc_traits::allocate(_M_impl
, 1); }
452 _M_put_node(typename
_Node_alloc_traits::pointer __p
) _GLIBCXX_NOEXCEPT
453 { _Node_alloc_traits::deallocate(_M_impl
, __p
, 1); }
456 typedef _Alloc allocator_type
;
459 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
462 const _Node_alloc_type
&
463 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
466 #if __cplusplus >= 201103L
467 _List_base() = default;
472 _List_base(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
476 #if __cplusplus >= 201103L
477 _List_base(_List_base
&&) = default;
479 # if !_GLIBCXX_INLINE_VERSION
480 _List_base(_List_base
&& __x
, _Node_alloc_type
&& __a
)
481 : _M_impl(std::move(__a
))
483 if (__x
._M_get_Node_allocator() == _M_get_Node_allocator())
484 _M_move_nodes(std::move(__x
));
485 // else caller must move individual elements.
489 // Used when allocator is_always_equal.
490 _List_base(_Node_alloc_type
&& __a
, _List_base
&& __x
)
491 : _M_impl(std::move(__a
), std::move(__x
._M_impl
))
494 // Used when allocator !is_always_equal.
495 _List_base(_Node_alloc_type
&& __a
)
496 : _M_impl(std::move(__a
))
500 _M_move_nodes(_List_base
&& __x
)
501 { _M_impl
._M_node
._M_move_nodes(std::move(__x
._M_impl
._M_node
)); }
504 // This is what actually destroys the list.
505 ~_List_base() _GLIBCXX_NOEXCEPT
509 _M_clear() _GLIBCXX_NOEXCEPT
;
512 _M_init() _GLIBCXX_NOEXCEPT
513 { this->_M_impl
._M_node
._M_init(); }
517 * @brief A standard container with linear time access to elements,
518 * and fixed time insertion/deletion at any point in the sequence.
522 * @tparam _Tp Type of element.
523 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
525 * Meets the requirements of a <a href="tables.html#65">container</a>, a
526 * <a href="tables.html#66">reversible container</a>, and a
527 * <a href="tables.html#67">sequence</a>, including the
528 * <a href="tables.html#68">optional sequence requirements</a> with the
529 * %exception of @c at and @c operator[].
531 * This is a @e doubly @e linked %list. Traversal up and down the
532 * %list requires linear time, but adding and removing elements (or
533 * @e nodes) is done in constant time, regardless of where the
534 * change takes place. Unlike std::vector and std::deque,
535 * random-access iterators are not provided, so subscripting ( @c
536 * [] ) access is not allowed. For algorithms which only need
537 * sequential access, this lack makes no difference.
539 * Also unlike the other standard containers, std::list provides
540 * specialized algorithms %unique to linked lists, such as
541 * splicing, sorting, and in-place reversal.
543 * A couple points on memory allocation for list<Tp>:
545 * First, we never actually allocate a Tp, we allocate
546 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
547 * that after elements from %list<X,Alloc1> are spliced into
548 * %list<X,Alloc2>, destroying the memory of the second %list is a
549 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
551 * Second, a %list conceptually represented as
553 * A <---> B <---> C <---> D
555 * is actually circular; a link exists between A and D. The %list
556 * class holds (as its only data member) a private list::iterator
557 * pointing to @e D, not to @e A! To get to the head of the %list,
558 * we start at the tail and move forward by one. When this member
559 * iterator's next/previous pointers refer to itself, the %list is
562 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
563 class list
: protected _List_base
<_Tp
, _Alloc
>
565 #ifdef _GLIBCXX_CONCEPT_CHECKS
566 // concept requirements
567 typedef typename
_Alloc::value_type _Alloc_value_type
;
568 # if __cplusplus < 201103L
569 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
571 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
574 #if __cplusplus >= 201103L
575 static_assert(is_same
<typename remove_cv
<_Tp
>::type
, _Tp
>::value
,
576 "std::list must have a non-const, non-volatile value_type");
577 # ifdef __STRICT_ANSI__
578 static_assert(is_same
<typename
_Alloc::value_type
, _Tp
>::value
,
579 "std::list must have the same value_type as its allocator");
583 typedef _List_base
<_Tp
, _Alloc
> _Base
;
584 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
585 typedef typename
_Base::_Tp_alloc_traits _Tp_alloc_traits
;
586 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
587 typedef typename
_Base::_Node_alloc_traits _Node_alloc_traits
;
590 typedef _Tp value_type
;
591 typedef typename
_Tp_alloc_traits::pointer pointer
;
592 typedef typename
_Tp_alloc_traits::const_pointer const_pointer
;
593 typedef typename
_Tp_alloc_traits::reference reference
;
594 typedef typename
_Tp_alloc_traits::const_reference const_reference
;
595 typedef _List_iterator
<_Tp
> iterator
;
596 typedef _List_const_iterator
<_Tp
> const_iterator
;
597 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
598 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
599 typedef size_t size_type
;
600 typedef ptrdiff_t difference_type
;
601 typedef _Alloc allocator_type
;
604 // Note that pointers-to-_Node's can be ctor-converted to
606 typedef _List_node
<_Tp
> _Node
;
608 using _Base::_M_impl
;
609 using _Base::_M_put_node
;
610 using _Base::_M_get_node
;
611 using _Base::_M_get_Node_allocator
;
614 * @param __args An instance of user data.
616 * Allocates space for a new node and constructs a copy of
619 #if __cplusplus < 201103L
621 _M_create_node(const value_type
& __x
)
623 _Node
* __p
= this->_M_get_node();
626 _Tp_alloc_type
__alloc(_M_get_Node_allocator());
627 __alloc
.construct(__p
->_M_valptr(), __x
);
632 __throw_exception_again
;
637 template<typename
... _Args
>
639 _M_create_node(_Args
&&... __args
)
641 auto __p
= this->_M_get_node();
642 auto& __alloc
= _M_get_Node_allocator();
643 __allocated_ptr
<_Node_alloc_type
> __guard
{__alloc
, __p
};
644 _Node_alloc_traits::construct(__alloc
, __p
->_M_valptr(),
645 std::forward
<_Args
>(__args
)...);
651 #if _GLIBCXX_USE_CXX11_ABI
653 _S_distance(const_iterator __first
, const_iterator __last
)
654 { return std::distance(__first
, __last
); }
656 // return the stored size
658 _M_node_count() const
659 { return this->_M_get_size(); }
661 // dummy implementations used when the size is not stored
663 _S_distance(const_iterator
, const_iterator
)
666 // count the number of nodes
668 _M_node_count() const
669 { return std::distance(begin(), end()); }
673 // [23.2.2.1] construct/copy/destroy
674 // (assign() and get_allocator() are also listed in this section)
677 * @brief Creates a %list with no elements.
679 #if __cplusplus >= 201103L
686 * @brief Creates a %list with no elements.
687 * @param __a An allocator object.
690 list(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
691 : _Base(_Node_alloc_type(__a
)) { }
693 #if __cplusplus >= 201103L
695 * @brief Creates a %list with default constructed elements.
696 * @param __n The number of elements to initially create.
697 * @param __a An allocator object.
699 * This constructor fills the %list with @a __n default
700 * constructed elements.
703 list(size_type __n
, const allocator_type
& __a
= allocator_type())
704 : _Base(_Node_alloc_type(__a
))
705 { _M_default_initialize(__n
); }
708 * @brief Creates a %list with copies of an exemplar element.
709 * @param __n The number of elements to initially create.
710 * @param __value An element to copy.
711 * @param __a An allocator object.
713 * This constructor fills the %list with @a __n copies of @a __value.
715 list(size_type __n
, const value_type
& __value
,
716 const allocator_type
& __a
= allocator_type())
717 : _Base(_Node_alloc_type(__a
))
718 { _M_fill_initialize(__n
, __value
); }
721 * @brief Creates a %list with copies of an exemplar element.
722 * @param __n The number of elements to initially create.
723 * @param __value An element to copy.
724 * @param __a An allocator object.
726 * This constructor fills the %list with @a __n copies of @a __value.
729 list(size_type __n
, const value_type
& __value
= value_type(),
730 const allocator_type
& __a
= allocator_type())
731 : _Base(_Node_alloc_type(__a
))
732 { _M_fill_initialize(__n
, __value
); }
736 * @brief %List copy constructor.
737 * @param __x A %list of identical element and allocator types.
739 * The newly-created %list uses a copy of the allocation object used
740 * by @a __x (unless the allocator traits dictate a different object).
742 list(const list
& __x
)
743 : _Base(_Node_alloc_traits::
744 _S_select_on_copy(__x
._M_get_Node_allocator()))
745 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
747 #if __cplusplus >= 201103L
749 * @brief %List move constructor.
751 * The newly-created %list contains the exact contents of the moved
752 * instance. The contents of the moved instance are a valid, but
755 list(list
&&) = default;
758 * @brief Builds a %list from an initializer_list
759 * @param __l An initializer_list of value_type.
760 * @param __a An allocator object.
762 * Create a %list consisting of copies of the elements in the
763 * initializer_list @a __l. This is linear in __l.size().
765 list(initializer_list
<value_type
> __l
,
766 const allocator_type
& __a
= allocator_type())
767 : _Base(_Node_alloc_type(__a
))
768 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
770 list(const list
& __x
, const allocator_type
& __a
)
771 : _Base(_Node_alloc_type(__a
))
772 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
775 list(list
&& __x
, const allocator_type
& __a
, true_type
) noexcept
776 : _Base(_Node_alloc_type(__a
), std::move(__x
))
779 list(list
&& __x
, const allocator_type
& __a
, false_type
)
780 : _Base(_Node_alloc_type(__a
))
782 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
783 this->_M_move_nodes(std::move(__x
));
785 insert(begin(), std::__make_move_if_noexcept_iterator(__x
.begin()),
786 std::__make_move_if_noexcept_iterator(__x
.end()));
790 list(list
&& __x
, const allocator_type
& __a
)
791 noexcept(_Node_alloc_traits::_S_always_equal())
792 : list(std::move(__x
), __a
,
793 typename
_Node_alloc_traits::is_always_equal
{})
798 * @brief Builds a %list from a range.
799 * @param __first An input iterator.
800 * @param __last An input iterator.
801 * @param __a An allocator object.
803 * Create a %list consisting of copies of the elements from
804 * [@a __first,@a __last). This is linear in N (where N is
805 * distance(@a __first,@a __last)).
807 #if __cplusplus >= 201103L
808 template<typename _InputIterator
,
809 typename
= std::_RequireInputIter
<_InputIterator
>>
810 list(_InputIterator __first
, _InputIterator __last
,
811 const allocator_type
& __a
= allocator_type())
812 : _Base(_Node_alloc_type(__a
))
813 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
815 template<typename _InputIterator
>
816 list(_InputIterator __first
, _InputIterator __last
,
817 const allocator_type
& __a
= allocator_type())
818 : _Base(_Node_alloc_type(__a
))
820 // Check whether it's an integral type. If so, it's not an iterator.
821 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
822 _M_initialize_dispatch(__first
, __last
, _Integral());
826 #if __cplusplus >= 201103L
828 * No explicit dtor needed as the _Base dtor takes care of
829 * things. The _Base dtor only erases the elements, and note
830 * that if the elements themselves are pointers, the pointed-to
831 * memory is not touched in any way. Managing the pointer is
832 * the user's responsibility.
838 * @brief %List assignment operator.
839 * @param __x A %list of identical element and allocator types.
841 * All the elements of @a __x are copied.
843 * Whether the allocator is copied depends on the allocator traits.
846 operator=(const list
& __x
);
848 #if __cplusplus >= 201103L
850 * @brief %List move assignment operator.
851 * @param __x A %list of identical element and allocator types.
853 * The contents of @a __x are moved into this %list (without copying).
855 * Afterwards @a __x is a valid, but unspecified %list
857 * Whether the allocator is moved depends on the allocator traits.
860 operator=(list
&& __x
)
861 noexcept(_Node_alloc_traits::_S_nothrow_move())
863 constexpr bool __move_storage
=
864 _Node_alloc_traits::_S_propagate_on_move_assign()
865 || _Node_alloc_traits::_S_always_equal();
866 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
871 * @brief %List initializer list assignment operator.
872 * @param __l An initializer_list of value_type.
874 * Replace the contents of the %list with copies of the elements
875 * in the initializer_list @a __l. This is linear in l.size().
878 operator=(initializer_list
<value_type
> __l
)
880 this->assign(__l
.begin(), __l
.end());
886 * @brief Assigns a given value to a %list.
887 * @param __n Number of elements to be assigned.
888 * @param __val Value to be assigned.
890 * This function fills a %list with @a __n copies of the given
891 * value. Note that the assignment completely changes the %list
892 * and that the resulting %list's size is the same as the number
893 * of elements assigned.
896 assign(size_type __n
, const value_type
& __val
)
897 { _M_fill_assign(__n
, __val
); }
900 * @brief Assigns a range to a %list.
901 * @param __first An input iterator.
902 * @param __last An input iterator.
904 * This function fills a %list with copies of the elements in the
905 * range [@a __first,@a __last).
907 * Note that the assignment completely changes the %list and
908 * that the resulting %list's size is the same as the number of
911 #if __cplusplus >= 201103L
912 template<typename _InputIterator
,
913 typename
= std::_RequireInputIter
<_InputIterator
>>
915 assign(_InputIterator __first
, _InputIterator __last
)
916 { _M_assign_dispatch(__first
, __last
, __false_type()); }
918 template<typename _InputIterator
>
920 assign(_InputIterator __first
, _InputIterator __last
)
922 // Check whether it's an integral type. If so, it's not an iterator.
923 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
924 _M_assign_dispatch(__first
, __last
, _Integral());
928 #if __cplusplus >= 201103L
930 * @brief Assigns an initializer_list to a %list.
931 * @param __l An initializer_list of value_type.
933 * Replace the contents of the %list with copies of the elements
934 * in the initializer_list @a __l. This is linear in __l.size().
937 assign(initializer_list
<value_type
> __l
)
938 { this->_M_assign_dispatch(__l
.begin(), __l
.end(), __false_type()); }
941 /// Get a copy of the memory allocation object.
943 get_allocator() const _GLIBCXX_NOEXCEPT
944 { return allocator_type(_Base::_M_get_Node_allocator()); }
948 * Returns a read/write iterator that points to the first element in the
949 * %list. Iteration is done in ordinary element order.
952 begin() _GLIBCXX_NOEXCEPT
953 { return iterator(this->_M_impl
._M_node
._M_next
); }
956 * Returns a read-only (constant) iterator that points to the
957 * first element in the %list. Iteration is done in ordinary
961 begin() const _GLIBCXX_NOEXCEPT
962 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
965 * Returns a read/write iterator that points one past the last
966 * element in the %list. Iteration is done in ordinary element
970 end() _GLIBCXX_NOEXCEPT
971 { return iterator(&this->_M_impl
._M_node
); }
974 * Returns a read-only (constant) iterator that points one past
975 * the last element in the %list. Iteration is done in ordinary
979 end() const _GLIBCXX_NOEXCEPT
980 { return const_iterator(&this->_M_impl
._M_node
); }
983 * Returns a read/write reverse iterator that points to the last
984 * element in the %list. Iteration is done in reverse element
988 rbegin() _GLIBCXX_NOEXCEPT
989 { return reverse_iterator(end()); }
992 * Returns a read-only (constant) reverse iterator that points to
993 * the last element in the %list. Iteration is done in reverse
996 const_reverse_iterator
997 rbegin() const _GLIBCXX_NOEXCEPT
998 { return const_reverse_iterator(end()); }
1001 * Returns a read/write reverse iterator that points to one
1002 * before the first element in the %list. Iteration is done in
1003 * reverse element order.
1006 rend() _GLIBCXX_NOEXCEPT
1007 { return reverse_iterator(begin()); }
1010 * Returns a read-only (constant) reverse iterator that points to one
1011 * before the first element in the %list. Iteration is done in reverse
1014 const_reverse_iterator
1015 rend() const _GLIBCXX_NOEXCEPT
1016 { return const_reverse_iterator(begin()); }
1018 #if __cplusplus >= 201103L
1020 * Returns a read-only (constant) iterator that points to the
1021 * first element in the %list. Iteration is done in ordinary
1025 cbegin() const noexcept
1026 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
1029 * Returns a read-only (constant) iterator that points one past
1030 * the last element in the %list. Iteration is done in ordinary
1034 cend() const noexcept
1035 { return const_iterator(&this->_M_impl
._M_node
); }
1038 * Returns a read-only (constant) reverse iterator that points to
1039 * the last element in the %list. Iteration is done in reverse
1042 const_reverse_iterator
1043 crbegin() const noexcept
1044 { return const_reverse_iterator(end()); }
1047 * Returns a read-only (constant) reverse iterator that points to one
1048 * before the first element in the %list. Iteration is done in reverse
1051 const_reverse_iterator
1052 crend() const noexcept
1053 { return const_reverse_iterator(begin()); }
1056 // [23.2.2.2] capacity
1058 * Returns true if the %list is empty. (Thus begin() would equal
1062 empty() const _GLIBCXX_NOEXCEPT
1063 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
1065 /** Returns the number of elements in the %list. */
1067 size() const _GLIBCXX_NOEXCEPT
1068 { return _M_node_count(); }
1070 /** Returns the size() of the largest possible %list. */
1072 max_size() const _GLIBCXX_NOEXCEPT
1073 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1075 #if __cplusplus >= 201103L
1077 * @brief Resizes the %list to the specified number of elements.
1078 * @param __new_size Number of elements the %list should contain.
1080 * This function will %resize the %list to the specified number
1081 * of elements. If the number is smaller than the %list's
1082 * current size the %list is truncated, otherwise default
1083 * constructed elements are appended.
1086 resize(size_type __new_size
);
1089 * @brief Resizes the %list to the specified number of elements.
1090 * @param __new_size Number of elements the %list should contain.
1091 * @param __x Data with which new elements should be populated.
1093 * This function will %resize the %list to the specified number
1094 * of elements. If the number is smaller than the %list's
1095 * current size the %list is truncated, otherwise the %list is
1096 * extended and new elements are populated with given data.
1099 resize(size_type __new_size
, const value_type
& __x
);
1102 * @brief Resizes the %list to the specified number of elements.
1103 * @param __new_size Number of elements the %list should contain.
1104 * @param __x Data with which new elements should be populated.
1106 * This function will %resize the %list to the specified number
1107 * of elements. If the number is smaller than the %list's
1108 * current size the %list is truncated, otherwise the %list is
1109 * extended and new elements are populated with given data.
1112 resize(size_type __new_size
, value_type __x
= value_type());
1117 * Returns a read/write reference to the data at the first
1118 * element of the %list.
1121 front() _GLIBCXX_NOEXCEPT
1122 { return *begin(); }
1125 * Returns a read-only (constant) reference to the data at the first
1126 * element of the %list.
1129 front() const _GLIBCXX_NOEXCEPT
1130 { return *begin(); }
1133 * Returns a read/write reference to the data at the last element
1137 back() _GLIBCXX_NOEXCEPT
1139 iterator __tmp
= end();
1145 * Returns a read-only (constant) reference to the data at the last
1146 * element of the %list.
1149 back() const _GLIBCXX_NOEXCEPT
1151 const_iterator __tmp
= end();
1156 // [23.2.2.3] modifiers
1158 * @brief Add data to the front of the %list.
1159 * @param __x Data to be added.
1161 * This is a typical stack operation. The function creates an
1162 * element at the front of the %list and assigns the given data
1163 * to it. Due to the nature of a %list this operation can be
1164 * done in constant time, and does not invalidate iterators and
1168 push_front(const value_type
& __x
)
1169 { this->_M_insert(begin(), __x
); }
1171 #if __cplusplus >= 201103L
1173 push_front(value_type
&& __x
)
1174 { this->_M_insert(begin(), std::move(__x
)); }
1176 template<typename
... _Args
>
1177 #if __cplusplus > 201402L
1182 emplace_front(_Args
&&... __args
)
1184 this->_M_insert(begin(), std::forward
<_Args
>(__args
)...);
1185 #if __cplusplus > 201402L
1192 * @brief Removes first element.
1194 * This is a typical stack operation. It shrinks the %list by
1195 * one. Due to the nature of a %list this operation can be done
1196 * in constant time, and only invalidates iterators/references to
1197 * the element being removed.
1199 * Note that no data is returned, and if the first element's data
1200 * is needed, it should be retrieved before pop_front() is
1204 pop_front() _GLIBCXX_NOEXCEPT
1205 { this->_M_erase(begin()); }
1208 * @brief Add data to the end of the %list.
1209 * @param __x Data to be added.
1211 * This is a typical stack operation. The function creates an
1212 * element at the end of the %list and assigns the given data to
1213 * it. Due to the nature of a %list this operation can be done
1214 * in constant time, and does not invalidate iterators and
1218 push_back(const value_type
& __x
)
1219 { this->_M_insert(end(), __x
); }
1221 #if __cplusplus >= 201103L
1223 push_back(value_type
&& __x
)
1224 { this->_M_insert(end(), std::move(__x
)); }
1226 template<typename
... _Args
>
1227 #if __cplusplus > 201402L
1232 emplace_back(_Args
&&... __args
)
1234 this->_M_insert(end(), std::forward
<_Args
>(__args
)...);
1235 #if __cplusplus > 201402L
1242 * @brief Removes last element.
1244 * This is a typical stack operation. It shrinks the %list by
1245 * one. Due to the nature of a %list this operation can be done
1246 * in constant time, and only invalidates iterators/references to
1247 * the element being removed.
1249 * Note that no data is returned, and if the last element's data
1250 * is needed, it should be retrieved before pop_back() is called.
1253 pop_back() _GLIBCXX_NOEXCEPT
1254 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1256 #if __cplusplus >= 201103L
1258 * @brief Constructs object in %list before specified iterator.
1259 * @param __position A const_iterator into the %list.
1260 * @param __args Arguments.
1261 * @return An iterator that points to the inserted data.
1263 * This function will insert an object of type T constructed
1264 * with T(std::forward<Args>(args)...) before the specified
1265 * location. Due to the nature of a %list this operation can
1266 * be done in constant time, and does not invalidate iterators
1269 template<typename
... _Args
>
1271 emplace(const_iterator __position
, _Args
&&... __args
);
1274 * @brief Inserts given value into %list before specified iterator.
1275 * @param __position A const_iterator into the %list.
1276 * @param __x Data to be inserted.
1277 * @return An iterator that points to the inserted data.
1279 * This function will insert a copy of the given value before
1280 * the specified location. Due to the nature of a %list this
1281 * operation can be done in constant time, and does not
1282 * invalidate iterators and references.
1285 insert(const_iterator __position
, const value_type
& __x
);
1288 * @brief Inserts given value into %list before specified iterator.
1289 * @param __position An iterator into the %list.
1290 * @param __x Data to be inserted.
1291 * @return An iterator that points to the inserted data.
1293 * This function will insert a copy of the given value before
1294 * the specified location. Due to the nature of a %list this
1295 * operation can be done in constant time, and does not
1296 * invalidate iterators and references.
1299 insert(iterator __position
, const value_type
& __x
);
1302 #if __cplusplus >= 201103L
1304 * @brief Inserts given rvalue into %list before specified iterator.
1305 * @param __position A const_iterator into the %list.
1306 * @param __x Data to be inserted.
1307 * @return An iterator that points to the inserted data.
1309 * This function will insert a copy of the given rvalue before
1310 * the specified location. Due to the nature of a %list this
1311 * operation can be done in constant time, and does not
1312 * invalidate iterators and references.
1315 insert(const_iterator __position
, value_type
&& __x
)
1316 { return emplace(__position
, std::move(__x
)); }
1319 * @brief Inserts the contents of an initializer_list into %list
1320 * before specified const_iterator.
1321 * @param __p A const_iterator into the %list.
1322 * @param __l An initializer_list of value_type.
1323 * @return An iterator pointing to the first element inserted
1326 * This function will insert copies of the data in the
1327 * initializer_list @a l into the %list before the location
1328 * specified by @a p.
1330 * This operation is linear in the number of elements inserted and
1331 * does not invalidate iterators and references.
1334 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1335 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1338 #if __cplusplus >= 201103L
1340 * @brief Inserts a number of copies of given data into the %list.
1341 * @param __position A const_iterator into the %list.
1342 * @param __n Number of elements to be inserted.
1343 * @param __x Data to be inserted.
1344 * @return An iterator pointing to the first element inserted
1347 * This function will insert a specified number of copies of the
1348 * given data before the location specified by @a position.
1350 * This operation is linear in the number of elements inserted and
1351 * does not invalidate iterators and references.
1354 insert(const_iterator __position
, size_type __n
, const value_type
& __x
);
1357 * @brief Inserts a number of copies of given data into the %list.
1358 * @param __position An iterator into the %list.
1359 * @param __n Number of elements to be inserted.
1360 * @param __x Data to be inserted.
1362 * This function will insert a specified number of copies of the
1363 * given data before the location specified by @a position.
1365 * This operation is linear in the number of elements inserted and
1366 * does not invalidate iterators and references.
1369 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1371 list
__tmp(__n
, __x
, get_allocator());
1372 splice(__position
, __tmp
);
1376 #if __cplusplus >= 201103L
1378 * @brief Inserts a range into the %list.
1379 * @param __position A const_iterator into the %list.
1380 * @param __first An input iterator.
1381 * @param __last An input iterator.
1382 * @return An iterator pointing to the first element inserted
1385 * This function will insert copies of the data in the range [@a
1386 * first,@a last) into the %list before the location specified by
1389 * This operation is linear in the number of elements inserted and
1390 * does not invalidate iterators and references.
1392 template<typename _InputIterator
,
1393 typename
= std::_RequireInputIter
<_InputIterator
>>
1395 insert(const_iterator __position
, _InputIterator __first
,
1396 _InputIterator __last
);
1399 * @brief Inserts a range into the %list.
1400 * @param __position An iterator into the %list.
1401 * @param __first An input iterator.
1402 * @param __last An input iterator.
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
>
1413 insert(iterator __position
, _InputIterator __first
,
1414 _InputIterator __last
)
1416 list
__tmp(__first
, __last
, get_allocator());
1417 splice(__position
, __tmp
);
1422 * @brief Remove element at given position.
1423 * @param __position Iterator pointing to element to be erased.
1424 * @return An iterator pointing to the next element (or end()).
1426 * This function will erase the element at the given position and thus
1427 * shorten the %list by one.
1429 * Due to the nature of a %list this operation can be done in
1430 * constant time, and only invalidates iterators/references to
1431 * the element being removed. The user is also cautioned that
1432 * this function only erases the element, and that if the element
1433 * is itself a pointer, the pointed-to memory is not touched in
1434 * any way. Managing the pointer is the user's responsibility.
1437 #if __cplusplus >= 201103L
1438 erase(const_iterator __position
) noexcept
;
1440 erase(iterator __position
);
1444 * @brief Remove a range of elements.
1445 * @param __first Iterator pointing to the first element to be erased.
1446 * @param __last Iterator pointing to one past the last element to be
1448 * @return An iterator pointing to the element pointed to by @a last
1449 * prior to erasing (or end()).
1451 * This function will erase the elements in the range @a
1452 * [first,last) and shorten the %list accordingly.
1454 * This operation is linear time in the size of the range and only
1455 * invalidates iterators/references to the element being removed.
1456 * The user is also cautioned that this function only erases the
1457 * elements, and that if the elements themselves are pointers, the
1458 * pointed-to memory is not touched in any way. Managing the pointer
1459 * is the user's responsibility.
1462 #if __cplusplus >= 201103L
1463 erase(const_iterator __first
, const_iterator __last
) noexcept
1465 erase(iterator __first
, iterator __last
)
1468 while (__first
!= __last
)
1469 __first
= erase(__first
);
1470 return __last
._M_const_cast();
1474 * @brief Swaps data with another %list.
1475 * @param __x A %list of the same element and allocator types.
1477 * This exchanges the elements between two lists in constant
1478 * time. Note that the global std::swap() function is
1479 * specialized such that std::swap(l1,l2) will feed to this
1482 * Whether the allocators are swapped depends on the allocator traits.
1485 swap(list
& __x
) _GLIBCXX_NOEXCEPT
1487 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1488 __x
._M_impl
._M_node
);
1490 size_t __xsize
= __x
._M_get_size();
1491 __x
._M_set_size(this->_M_get_size());
1492 this->_M_set_size(__xsize
);
1494 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1495 __x
._M_get_Node_allocator());
1499 * Erases all the elements. Note that this function only erases
1500 * the elements, and that if the elements themselves are
1501 * pointers, the pointed-to memory is not touched in any way.
1502 * Managing the pointer is the user's responsibility.
1505 clear() _GLIBCXX_NOEXCEPT
1511 // [23.2.2.4] list operations
1513 * @brief Insert contents of another %list.
1514 * @param __position Iterator referencing the element to insert before.
1515 * @param __x Source list.
1517 * The elements of @a __x are inserted in constant time in front of
1518 * the element referenced by @a __position. @a __x becomes an empty
1521 * Requires this != @a __x.
1524 #if __cplusplus >= 201103L
1525 splice(const_iterator __position
, list
&& __x
) noexcept
1527 splice(iterator __position
, list
& __x
)
1532 _M_check_equal_allocators(__x
);
1534 this->_M_transfer(__position
._M_const_cast(),
1535 __x
.begin(), __x
.end());
1537 this->_M_inc_size(__x
._M_get_size());
1542 #if __cplusplus >= 201103L
1544 splice(const_iterator __position
, list
& __x
) noexcept
1545 { splice(__position
, std::move(__x
)); }
1548 #if __cplusplus >= 201103L
1550 * @brief Insert element from another %list.
1551 * @param __position Const_iterator referencing the element to
1553 * @param __x Source list.
1554 * @param __i Const_iterator referencing the element to move.
1556 * Removes the element in list @a __x referenced by @a __i and
1557 * inserts it into the current list before @a __position.
1560 splice(const_iterator __position
, list
&& __x
, const_iterator __i
) noexcept
1563 * @brief Insert element from another %list.
1564 * @param __position Iterator referencing the element to insert before.
1565 * @param __x Source list.
1566 * @param __i Iterator referencing the element to move.
1568 * Removes the element in list @a __x referenced by @a __i and
1569 * inserts it into the current list before @a __position.
1572 splice(iterator __position
, list
& __x
, iterator __i
)
1575 iterator __j
= __i
._M_const_cast();
1577 if (__position
== __i
|| __position
== __j
)
1580 if (this != std::__addressof(__x
))
1581 _M_check_equal_allocators(__x
);
1583 this->_M_transfer(__position
._M_const_cast(),
1584 __i
._M_const_cast(), __j
);
1586 this->_M_inc_size(1);
1590 #if __cplusplus >= 201103L
1592 * @brief Insert element from another %list.
1593 * @param __position Const_iterator referencing the element to
1595 * @param __x Source list.
1596 * @param __i Const_iterator referencing the element to move.
1598 * Removes the element in list @a __x referenced by @a __i and
1599 * inserts it into the current list before @a __position.
1602 splice(const_iterator __position
, list
& __x
, const_iterator __i
) noexcept
1603 { splice(__position
, std::move(__x
), __i
); }
1606 #if __cplusplus >= 201103L
1608 * @brief Insert range from another %list.
1609 * @param __position Const_iterator referencing the element to
1611 * @param __x Source list.
1612 * @param __first Const_iterator referencing the start of range in x.
1613 * @param __last Const_iterator referencing the end of range in x.
1615 * Removes elements in the range [__first,__last) and inserts them
1616 * before @a __position in constant time.
1618 * Undefined if @a __position is in [__first,__last).
1621 splice(const_iterator __position
, list
&& __x
, const_iterator __first
,
1622 const_iterator __last
) noexcept
1625 * @brief Insert range from another %list.
1626 * @param __position Iterator referencing the element to insert before.
1627 * @param __x Source list.
1628 * @param __first Iterator referencing the start of range in x.
1629 * @param __last Iterator referencing the end of range in x.
1631 * Removes elements in the range [__first,__last) and inserts them
1632 * before @a __position in constant time.
1634 * Undefined if @a __position is in [__first,__last).
1637 splice(iterator __position
, list
& __x
, iterator __first
,
1641 if (__first
!= __last
)
1643 if (this != std::__addressof(__x
))
1644 _M_check_equal_allocators(__x
);
1646 size_t __n
= _S_distance(__first
, __last
);
1647 this->_M_inc_size(__n
);
1648 __x
._M_dec_size(__n
);
1650 this->_M_transfer(__position
._M_const_cast(),
1651 __first
._M_const_cast(),
1652 __last
._M_const_cast());
1656 #if __cplusplus >= 201103L
1658 * @brief Insert range from another %list.
1659 * @param __position Const_iterator referencing the element to
1661 * @param __x Source list.
1662 * @param __first Const_iterator referencing the start of range in x.
1663 * @param __last Const_iterator referencing the end of range in x.
1665 * Removes elements in the range [__first,__last) and inserts them
1666 * before @a __position in constant time.
1668 * Undefined if @a __position is in [__first,__last).
1671 splice(const_iterator __position
, list
& __x
, const_iterator __first
,
1672 const_iterator __last
) noexcept
1673 { splice(__position
, std::move(__x
), __first
, __last
); }
1677 #if __cplusplus > 201703L
1678 # define __cpp_lib_list_remove_return_type 201806L
1679 typedef size_type __remove_return_type
;
1680 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG \
1681 __attribute__((__abi_tag__("__cxx20")))
1683 typedef void __remove_return_type
;
1684 # define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1689 * @brief Remove all elements equal to value.
1690 * @param __value The value to remove.
1692 * Removes every element in the list equal to @a value.
1693 * Remaining elements stay in list order. Note that this
1694 * function only erases the elements, and that if the elements
1695 * themselves are pointers, the pointed-to memory is not
1696 * touched in any way. Managing the pointer is the user's
1699 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1700 __remove_return_type
1701 remove(const _Tp
& __value
);
1704 * @brief Remove all elements satisfying a predicate.
1705 * @tparam _Predicate Unary predicate function or object.
1707 * Removes every element in the list for which the predicate
1708 * returns true. Remaining elements stay in list order. Note
1709 * that this function only erases the elements, and that if the
1710 * elements themselves are pointers, the pointed-to memory is
1711 * not touched in any way. Managing the pointer is the user's
1714 template<typename _Predicate
>
1715 __remove_return_type
1716 remove_if(_Predicate
);
1719 * @brief Remove consecutive duplicate elements.
1721 * For each consecutive set of elements with the same value,
1722 * remove all but the first one. Remaining elements stay in
1723 * list order. Note that this function only erases the
1724 * elements, and that if the elements themselves are pointers,
1725 * the pointed-to memory is not touched in any way. Managing
1726 * the pointer is the user's responsibility.
1728 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1729 __remove_return_type
1733 * @brief Remove consecutive elements satisfying a predicate.
1734 * @tparam _BinaryPredicate Binary predicate function or object.
1736 * For each consecutive set of elements [first,last) that
1737 * satisfy predicate(first,i) where i is an iterator in
1738 * [first,last), remove all but the first one. Remaining
1739 * elements stay in list order. Note that this function only
1740 * erases the elements, and that if the elements themselves are
1741 * pointers, the pointed-to memory is not touched in any way.
1742 * Managing the pointer is the user's responsibility.
1744 template<typename _BinaryPredicate
>
1745 __remove_return_type
1746 unique(_BinaryPredicate
);
1748 #undef _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1751 * @brief Merge sorted lists.
1752 * @param __x Sorted list to merge.
1754 * Assumes that both @a __x and this list are sorted according to
1755 * operator<(). Merges elements of @a __x into this list in
1756 * sorted order, leaving @a __x empty when complete. Elements in
1757 * this list precede elements in @a __x that are equal.
1759 #if __cplusplus >= 201103L
1765 { merge(std::move(__x
)); }
1772 * @brief Merge sorted lists according to comparison function.
1773 * @tparam _StrictWeakOrdering Comparison function defining
1775 * @param __x Sorted list to merge.
1776 * @param __comp Comparison functor.
1778 * Assumes that both @a __x and this list are sorted according to
1779 * StrictWeakOrdering. Merges elements of @a __x into this list
1780 * in sorted order, leaving @a __x empty when complete. Elements
1781 * in this list precede elements in @a __x that are equivalent
1782 * according to StrictWeakOrdering().
1784 #if __cplusplus >= 201103L
1785 template<typename _StrictWeakOrdering
>
1787 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1789 template<typename _StrictWeakOrdering
>
1791 merge(list
& __x
, _StrictWeakOrdering __comp
)
1792 { merge(std::move(__x
), __comp
); }
1794 template<typename _StrictWeakOrdering
>
1796 merge(list
& __x
, _StrictWeakOrdering __comp
);
1800 * @brief Reverse the elements in list.
1802 * Reverse the order of elements in the list in linear time.
1805 reverse() _GLIBCXX_NOEXCEPT
1806 { this->_M_impl
._M_node
._M_reverse(); }
1809 * @brief Sort the elements.
1811 * Sorts the elements of this list in NlogN time. Equivalent
1812 * elements remain in list order.
1818 * @brief Sort the elements according to comparison function.
1820 * Sorts the elements of this list in NlogN time. Equivalent
1821 * elements remain in list order.
1823 template<typename _StrictWeakOrdering
>
1825 sort(_StrictWeakOrdering
);
1828 // Internal constructor functions follow.
1830 // Called by the range constructor to implement [23.1.1]/9
1832 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1833 // 438. Ambiguity in the "do the right thing" clause
1834 template<typename _Integer
>
1836 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1837 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1839 // Called by the range constructor to implement [23.1.1]/9
1840 template<typename _InputIterator
>
1842 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1845 for (; __first
!= __last
; ++__first
)
1846 #if __cplusplus >= 201103L
1847 emplace_back(*__first
);
1849 push_back(*__first
);
1853 // Called by list(n,v,a), and the range constructor when it turns out
1854 // to be the same thing.
1856 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1862 #if __cplusplus >= 201103L
1863 // Called by list(n).
1865 _M_default_initialize(size_type __n
)
1871 // Called by resize(sz).
1873 _M_default_append(size_type __n
);
1876 // Internal assign functions follow.
1878 // Called by the range assign to implement [23.1.1]/9
1880 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1881 // 438. Ambiguity in the "do the right thing" clause
1882 template<typename _Integer
>
1884 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1885 { _M_fill_assign(__n
, __val
); }
1887 // Called by the range assign to implement [23.1.1]/9
1888 template<typename _InputIterator
>
1890 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1893 // Called by assign(n,t), and the range assign when it turns out
1894 // to be the same thing.
1896 _M_fill_assign(size_type __n
, const value_type
& __val
);
1899 // Moves the elements from [first,last) before position.
1901 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1902 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1904 // Inserts new element at position given and with value given.
1905 #if __cplusplus < 201103L
1907 _M_insert(iterator __position
, const value_type
& __x
)
1909 _Node
* __tmp
= _M_create_node(__x
);
1910 __tmp
->_M_hook(__position
._M_node
);
1911 this->_M_inc_size(1);
1914 template<typename
... _Args
>
1916 _M_insert(iterator __position
, _Args
&&... __args
)
1918 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1919 __tmp
->_M_hook(__position
._M_node
);
1920 this->_M_inc_size(1);
1924 // Erases element at position given.
1926 _M_erase(iterator __position
) _GLIBCXX_NOEXCEPT
1928 this->_M_dec_size(1);
1929 __position
._M_node
->_M_unhook();
1930 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1931 #if __cplusplus >= 201103L
1932 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n
->_M_valptr());
1934 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n
->_M_valptr());
1940 // To implement the splice (and merge) bits of N1599.
1942 _M_check_equal_allocators(list
& __x
) _GLIBCXX_NOEXCEPT
1944 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1945 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1949 // Used to implement resize.
1951 _M_resize_pos(size_type
& __new_size
) const;
1953 #if __cplusplus >= 201103L
1955 _M_move_assign(list
&& __x
, true_type
) noexcept
1958 this->_M_move_nodes(std::move(__x
));
1959 std::__alloc_on_move(this->_M_get_Node_allocator(),
1960 __x
._M_get_Node_allocator());
1964 _M_move_assign(list
&& __x
, false_type
)
1966 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1967 _M_move_assign(std::move(__x
), true_type
{});
1969 // The rvalue's allocator cannot be moved, or is not equal,
1970 // so we need to individually move each element.
1971 _M_assign_dispatch(std::__make_move_if_noexcept_iterator(__x
.begin()),
1972 std::__make_move_if_noexcept_iterator(__x
.end()),
1978 #if __cpp_deduction_guides >= 201606
1979 template<typename _InputIterator
, typename _ValT
1980 = typename iterator_traits
<_InputIterator
>::value_type
,
1981 typename _Allocator
= allocator
<_ValT
>,
1982 typename
= _RequireInputIter
<_InputIterator
>,
1983 typename
= _RequireAllocator
<_Allocator
>>
1984 list(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1985 -> list
<_ValT
, _Allocator
>;
1988 _GLIBCXX_END_NAMESPACE_CXX11
1991 * @brief List equality comparison.
1992 * @param __x A %list.
1993 * @param __y A %list of the same type as @a __x.
1994 * @return True iff the size and elements of the lists are equal.
1996 * This is an equivalence relation. It is linear in the size of
1997 * the lists. Lists are considered equivalent if their sizes are
1998 * equal, and if corresponding elements compare equal.
2000 template<typename _Tp
, typename _Alloc
>
2002 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2004 #if _GLIBCXX_USE_CXX11_ABI
2005 if (__x
.size() != __y
.size())
2009 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
2010 const_iterator __end1
= __x
.end();
2011 const_iterator __end2
= __y
.end();
2013 const_iterator __i1
= __x
.begin();
2014 const_iterator __i2
= __y
.begin();
2015 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
2020 return __i1
== __end1
&& __i2
== __end2
;
2024 * @brief List ordering relation.
2025 * @param __x A %list.
2026 * @param __y A %list of the same type as @a __x.
2027 * @return True iff @a __x is lexicographically less than @a __y.
2029 * This is a total ordering relation. It is linear in the size of the
2030 * lists. The elements must be comparable with @c <.
2032 * See std::lexicographical_compare() for how the determination is made.
2034 template<typename _Tp
, typename _Alloc
>
2036 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2037 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
2038 __y
.begin(), __y
.end()); }
2040 /// Based on operator==
2041 template<typename _Tp
, typename _Alloc
>
2043 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2044 { return !(__x
== __y
); }
2046 /// Based on operator<
2047 template<typename _Tp
, typename _Alloc
>
2049 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2050 { return __y
< __x
; }
2052 /// Based on operator<
2053 template<typename _Tp
, typename _Alloc
>
2055 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2056 { return !(__y
< __x
); }
2058 /// Based on operator<
2059 template<typename _Tp
, typename _Alloc
>
2061 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
2062 { return !(__x
< __y
); }
2064 /// See std::list::swap().
2065 template<typename _Tp
, typename _Alloc
>
2067 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
2068 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
2071 _GLIBCXX_END_NAMESPACE_CONTAINER
2073 #if _GLIBCXX_USE_CXX11_ABI
2075 // Detect when distance is used to compute the size of the whole list.
2076 template<typename _Tp
>
2078 __distance(_GLIBCXX_STD_C::_List_iterator
<_Tp
> __first
,
2079 _GLIBCXX_STD_C::_List_iterator
<_Tp
> __last
,
2080 input_iterator_tag __tag
)
2082 typedef _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> _CIter
;
2083 return std::__distance(_CIter(__first
), _CIter(__last
), __tag
);
2086 template<typename _Tp
>
2088 __distance(_GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __first
,
2089 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __last
,
2092 typedef __detail::_List_node_header _Sentinel
;
2093 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __beyond
= __last
;
2095 const bool __whole
= __first
== __beyond
;
2096 if (__builtin_constant_p (__whole
) && __whole
)
2097 return static_cast<const _Sentinel
*>(__last
._M_node
)->_M_size
;
2100 while (__first
!= __last
)
2109 _GLIBCXX_END_NAMESPACE_VERSION
2112 #endif /* _STL_LIST_H */