1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_list.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{list}
59 #include <bits/concept_check.h>
60 #include <ext/alloc_traits.h>
61 #if __cplusplus >= 201103L
62 #include <initializer_list>
63 #include <bits/allocated_ptr.h>
64 #include <ext/aligned_buffer.h>
67 namespace std
_GLIBCXX_VISIBILITY(default)
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
;
368 _S_distance(const __detail::_List_node_base
* __first
,
369 const __detail::_List_node_base
* __last
)
372 while (__first
!= __last
)
374 __first
= __first
->_M_next
;
381 : public _Node_alloc_type
383 __detail::_List_node_header _M_node
;
385 _List_impl() _GLIBCXX_NOEXCEPT_IF( noexcept(_Node_alloc_type()) )
389 _List_impl(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
390 : _Node_alloc_type(__a
)
393 #if __cplusplus >= 201103L
394 _List_impl(_List_impl
&&) = default;
396 _List_impl(_Node_alloc_type
&& __a
) noexcept
397 : _Node_alloc_type(std::move(__a
))
404 #if _GLIBCXX_USE_CXX11_ABI
405 size_t _M_get_size() const { return _M_impl
._M_node
._M_size
; }
407 void _M_set_size(size_t __n
) { _M_impl
._M_node
._M_size
= __n
; }
409 void _M_inc_size(size_t __n
) { _M_impl
._M_node
._M_size
+= __n
; }
411 void _M_dec_size(size_t __n
) { _M_impl
._M_node
._M_size
-= __n
; }
414 _M_distance(const __detail::_List_node_base
* __first
,
415 const __detail::_List_node_base
* __last
) const
416 { return _S_distance(__first
, __last
); }
418 // return the stored size
419 size_t _M_node_count() const { return _M_get_size(); }
421 // dummy implementations used when the size is not stored
422 size_t _M_get_size() const { return 0; }
423 void _M_set_size(size_t) { }
424 void _M_inc_size(size_t) { }
425 void _M_dec_size(size_t) { }
426 size_t _M_distance(const void*, const void*) const { return 0; }
428 // count the number of nodes
429 size_t _M_node_count() const
431 return _S_distance(_M_impl
._M_node
._M_next
,
432 std::__addressof(_M_impl
._M_node
));
436 typename
_Node_alloc_traits::pointer
438 { return _Node_alloc_traits::allocate(_M_impl
, 1); }
441 _M_put_node(typename
_Node_alloc_traits::pointer __p
) _GLIBCXX_NOEXCEPT
442 { _Node_alloc_traits::deallocate(_M_impl
, __p
, 1); }
445 typedef _Alloc allocator_type
;
448 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
451 const _Node_alloc_type
&
452 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
455 #if __cplusplus >= 201103L
456 _List_base() = default;
461 _List_base(const _Node_alloc_type
& __a
) _GLIBCXX_NOEXCEPT
465 #if __cplusplus >= 201103L
466 _List_base(_List_base
&&) = default;
468 _List_base(_List_base
&& __x
, _Node_alloc_type
&& __a
)
469 : _M_impl(std::move(__a
))
471 if (__x
._M_get_Node_allocator() == _M_get_Node_allocator())
472 _M_move_nodes(std::move(__x
));
473 // else caller must move individual elements.
477 _M_move_nodes(_List_base
&& __x
)
478 { _M_impl
._M_node
._M_move_nodes(std::move(__x
._M_impl
._M_node
)); }
481 // This is what actually destroys the list.
482 ~_List_base() _GLIBCXX_NOEXCEPT
486 _M_clear() _GLIBCXX_NOEXCEPT
;
489 _M_init() _GLIBCXX_NOEXCEPT
490 { this->_M_impl
._M_node
._M_init(); }
494 * @brief A standard container with linear time access to elements,
495 * and fixed time insertion/deletion at any point in the sequence.
499 * @tparam _Tp Type of element.
500 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
502 * Meets the requirements of a <a href="tables.html#65">container</a>, a
503 * <a href="tables.html#66">reversible container</a>, and a
504 * <a href="tables.html#67">sequence</a>, including the
505 * <a href="tables.html#68">optional sequence requirements</a> with the
506 * %exception of @c at and @c operator[].
508 * This is a @e doubly @e linked %list. Traversal up and down the
509 * %list requires linear time, but adding and removing elements (or
510 * @e nodes) is done in constant time, regardless of where the
511 * change takes place. Unlike std::vector and std::deque,
512 * random-access iterators are not provided, so subscripting ( @c
513 * [] ) access is not allowed. For algorithms which only need
514 * sequential access, this lack makes no difference.
516 * Also unlike the other standard containers, std::list provides
517 * specialized algorithms %unique to linked lists, such as
518 * splicing, sorting, and in-place reversal.
520 * A couple points on memory allocation for list<Tp>:
522 * First, we never actually allocate a Tp, we allocate
523 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
524 * that after elements from %list<X,Alloc1> are spliced into
525 * %list<X,Alloc2>, destroying the memory of the second %list is a
526 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
528 * Second, a %list conceptually represented as
530 * A <---> B <---> C <---> D
532 * is actually circular; a link exists between A and D. The %list
533 * class holds (as its only data member) a private list::iterator
534 * pointing to @e D, not to @e A! To get to the head of the %list,
535 * we start at the tail and move forward by one. When this member
536 * iterator's next/previous pointers refer to itself, the %list is
539 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
540 class list
: protected _List_base
<_Tp
, _Alloc
>
542 #ifdef _GLIBCXX_CONCEPT_CHECKS
543 // concept requirements
544 typedef typename
_Alloc::value_type _Alloc_value_type
;
545 # if __cplusplus < 201103L
546 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
548 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
551 typedef _List_base
<_Tp
, _Alloc
> _Base
;
552 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
553 typedef typename
_Base::_Tp_alloc_traits _Tp_alloc_traits
;
554 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
555 typedef typename
_Base::_Node_alloc_traits _Node_alloc_traits
;
558 typedef _Tp value_type
;
559 typedef typename
_Tp_alloc_traits::pointer pointer
;
560 typedef typename
_Tp_alloc_traits::const_pointer const_pointer
;
561 typedef typename
_Tp_alloc_traits::reference reference
;
562 typedef typename
_Tp_alloc_traits::const_reference const_reference
;
563 typedef _List_iterator
<_Tp
> iterator
;
564 typedef _List_const_iterator
<_Tp
> const_iterator
;
565 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
566 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
567 typedef size_t size_type
;
568 typedef ptrdiff_t difference_type
;
569 typedef _Alloc allocator_type
;
572 // Note that pointers-to-_Node's can be ctor-converted to
574 typedef _List_node
<_Tp
> _Node
;
576 using _Base::_M_impl
;
577 using _Base::_M_put_node
;
578 using _Base::_M_get_node
;
579 using _Base::_M_get_Node_allocator
;
582 * @param __args An instance of user data.
584 * Allocates space for a new node and constructs a copy of
587 #if __cplusplus < 201103L
589 _M_create_node(const value_type
& __x
)
591 _Node
* __p
= this->_M_get_node();
594 _Tp_alloc_type
__alloc(_M_get_Node_allocator());
595 __alloc
.construct(__p
->_M_valptr(), __x
);
600 __throw_exception_again
;
605 template<typename
... _Args
>
607 _M_create_node(_Args
&&... __args
)
609 auto __p
= this->_M_get_node();
610 auto& __alloc
= _M_get_Node_allocator();
611 __allocated_ptr
<_Node_alloc_type
> __guard
{__alloc
, __p
};
612 _Node_alloc_traits::construct(__alloc
, __p
->_M_valptr(),
613 std::forward
<_Args
>(__args
)...);
620 // [23.2.2.1] construct/copy/destroy
621 // (assign() and get_allocator() are also listed in this section)
624 * @brief Creates a %list with no elements.
626 #if __cplusplus >= 201103L
633 * @brief Creates a %list with no elements.
634 * @param __a An allocator object.
637 list(const allocator_type
& __a
) _GLIBCXX_NOEXCEPT
638 : _Base(_Node_alloc_type(__a
)) { }
640 #if __cplusplus >= 201103L
642 * @brief Creates a %list with default constructed elements.
643 * @param __n The number of elements to initially create.
644 * @param __a An allocator object.
646 * This constructor fills the %list with @a __n default
647 * constructed elements.
650 list(size_type __n
, const allocator_type
& __a
= allocator_type())
651 : _Base(_Node_alloc_type(__a
))
652 { _M_default_initialize(__n
); }
655 * @brief Creates a %list with copies of an exemplar element.
656 * @param __n The number of elements to initially create.
657 * @param __value An element to copy.
658 * @param __a An allocator object.
660 * This constructor fills the %list with @a __n copies of @a __value.
662 list(size_type __n
, const value_type
& __value
,
663 const allocator_type
& __a
= allocator_type())
664 : _Base(_Node_alloc_type(__a
))
665 { _M_fill_initialize(__n
, __value
); }
668 * @brief Creates a %list with copies of an exemplar element.
669 * @param __n The number of elements to initially create.
670 * @param __value An element to copy.
671 * @param __a An allocator object.
673 * This constructor fills the %list with @a __n copies of @a __value.
676 list(size_type __n
, const value_type
& __value
= value_type(),
677 const allocator_type
& __a
= allocator_type())
678 : _Base(_Node_alloc_type(__a
))
679 { _M_fill_initialize(__n
, __value
); }
683 * @brief %List copy constructor.
684 * @param __x A %list of identical element and allocator types.
686 * The newly-created %list uses a copy of the allocation object used
687 * by @a __x (unless the allocator traits dictate a different object).
689 list(const list
& __x
)
690 : _Base(_Node_alloc_traits::
691 _S_select_on_copy(__x
._M_get_Node_allocator()))
692 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
694 #if __cplusplus >= 201103L
696 * @brief %List move constructor.
698 * The newly-created %list contains the exact contents of the moved
699 * instance. The contents of the moved instance are a valid, but
702 list(list
&&) = default;
705 * @brief Builds a %list from an initializer_list
706 * @param __l An initializer_list of value_type.
707 * @param __a An allocator object.
709 * Create a %list consisting of copies of the elements in the
710 * initializer_list @a __l. This is linear in __l.size().
712 list(initializer_list
<value_type
> __l
,
713 const allocator_type
& __a
= allocator_type())
714 : _Base(_Node_alloc_type(__a
))
715 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
717 list(const list
& __x
, const allocator_type
& __a
)
718 : _Base(_Node_alloc_type(__a
))
719 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
721 list(list
&& __x
, const allocator_type
& __a
)
722 noexcept(_Node_alloc_traits::_S_always_equal())
723 : _Base(std::move(__x
), _Node_alloc_type(__a
))
725 // If __x is not empty it means its allocator is not equal to __a,
726 // so we need to move from each element individually.
727 insert(begin(), std::__make_move_if_noexcept_iterator(__x
.begin()),
728 std::__make_move_if_noexcept_iterator(__x
.end()));
733 * @brief Builds a %list from a range.
734 * @param __first An input iterator.
735 * @param __last An input iterator.
736 * @param __a An allocator object.
738 * Create a %list consisting of copies of the elements from
739 * [@a __first,@a __last). This is linear in N (where N is
740 * distance(@a __first,@a __last)).
742 #if __cplusplus >= 201103L
743 template<typename _InputIterator
,
744 typename
= std::_RequireInputIter
<_InputIterator
>>
745 list(_InputIterator __first
, _InputIterator __last
,
746 const allocator_type
& __a
= allocator_type())
747 : _Base(_Node_alloc_type(__a
))
748 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
750 template<typename _InputIterator
>
751 list(_InputIterator __first
, _InputIterator __last
,
752 const allocator_type
& __a
= allocator_type())
753 : _Base(_Node_alloc_type(__a
))
755 // Check whether it's an integral type. If so, it's not an iterator.
756 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
757 _M_initialize_dispatch(__first
, __last
, _Integral());
761 #if __cplusplus >= 201103L
763 * No explicit dtor needed as the _Base dtor takes care of
764 * things. The _Base dtor only erases the elements, and note
765 * that if the elements themselves are pointers, the pointed-to
766 * memory is not touched in any way. Managing the pointer is
767 * the user's responsibility.
773 * @brief %List assignment operator.
774 * @param __x A %list of identical element and allocator types.
776 * All the elements of @a __x are copied.
778 * Whether the allocator is copied depends on the allocator traits.
781 operator=(const list
& __x
);
783 #if __cplusplus >= 201103L
785 * @brief %List move assignment operator.
786 * @param __x A %list of identical element and allocator types.
788 * The contents of @a __x are moved into this %list (without copying).
790 * Afterwards @a __x is a valid, but unspecified %list
792 * Whether the allocator is moved depends on the allocator traits.
795 operator=(list
&& __x
)
796 noexcept(_Node_alloc_traits::_S_nothrow_move())
798 constexpr bool __move_storage
=
799 _Node_alloc_traits::_S_propagate_on_move_assign()
800 || _Node_alloc_traits::_S_always_equal();
801 _M_move_assign(std::move(__x
), __bool_constant
<__move_storage
>());
806 * @brief %List initializer list assignment operator.
807 * @param __l An initializer_list of value_type.
809 * Replace the contents of the %list with copies of the elements
810 * in the initializer_list @a __l. This is linear in l.size().
813 operator=(initializer_list
<value_type
> __l
)
815 this->assign(__l
.begin(), __l
.end());
821 * @brief Assigns a given value to a %list.
822 * @param __n Number of elements to be assigned.
823 * @param __val Value to be assigned.
825 * This function fills a %list with @a __n copies of the given
826 * value. Note that the assignment completely changes the %list
827 * and that the resulting %list's size is the same as the number
828 * of elements assigned.
831 assign(size_type __n
, const value_type
& __val
)
832 { _M_fill_assign(__n
, __val
); }
835 * @brief Assigns a range to a %list.
836 * @param __first An input iterator.
837 * @param __last An input iterator.
839 * This function fills a %list with copies of the elements in the
840 * range [@a __first,@a __last).
842 * Note that the assignment completely changes the %list and
843 * that the resulting %list's size is the same as the number of
846 #if __cplusplus >= 201103L
847 template<typename _InputIterator
,
848 typename
= std::_RequireInputIter
<_InputIterator
>>
850 assign(_InputIterator __first
, _InputIterator __last
)
851 { _M_assign_dispatch(__first
, __last
, __false_type()); }
853 template<typename _InputIterator
>
855 assign(_InputIterator __first
, _InputIterator __last
)
857 // Check whether it's an integral type. If so, it's not an iterator.
858 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
859 _M_assign_dispatch(__first
, __last
, _Integral());
863 #if __cplusplus >= 201103L
865 * @brief Assigns an initializer_list to a %list.
866 * @param __l An initializer_list of value_type.
868 * Replace the contents of the %list with copies of the elements
869 * in the initializer_list @a __l. This is linear in __l.size().
872 assign(initializer_list
<value_type
> __l
)
873 { this->_M_assign_dispatch(__l
.begin(), __l
.end(), __false_type()); }
876 /// Get a copy of the memory allocation object.
878 get_allocator() const _GLIBCXX_NOEXCEPT
879 { return allocator_type(_Base::_M_get_Node_allocator()); }
883 * Returns a read/write iterator that points to the first element in the
884 * %list. Iteration is done in ordinary element order.
887 begin() _GLIBCXX_NOEXCEPT
888 { return iterator(this->_M_impl
._M_node
._M_next
); }
891 * Returns a read-only (constant) iterator that points to the
892 * first element in the %list. Iteration is done in ordinary
896 begin() const _GLIBCXX_NOEXCEPT
897 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
900 * Returns a read/write iterator that points one past the last
901 * element in the %list. Iteration is done in ordinary element
905 end() _GLIBCXX_NOEXCEPT
906 { return iterator(&this->_M_impl
._M_node
); }
909 * Returns a read-only (constant) iterator that points one past
910 * the last element in the %list. Iteration is done in ordinary
914 end() const _GLIBCXX_NOEXCEPT
915 { return const_iterator(&this->_M_impl
._M_node
); }
918 * Returns a read/write reverse iterator that points to the last
919 * element in the %list. Iteration is done in reverse element
923 rbegin() _GLIBCXX_NOEXCEPT
924 { return reverse_iterator(end()); }
927 * Returns a read-only (constant) reverse iterator that points to
928 * the last element in the %list. Iteration is done in reverse
931 const_reverse_iterator
932 rbegin() const _GLIBCXX_NOEXCEPT
933 { return const_reverse_iterator(end()); }
936 * Returns a read/write reverse iterator that points to one
937 * before the first element in the %list. Iteration is done in
938 * reverse element order.
941 rend() _GLIBCXX_NOEXCEPT
942 { return reverse_iterator(begin()); }
945 * Returns a read-only (constant) reverse iterator that points to one
946 * before the first element in the %list. Iteration is done in reverse
949 const_reverse_iterator
950 rend() const _GLIBCXX_NOEXCEPT
951 { return const_reverse_iterator(begin()); }
953 #if __cplusplus >= 201103L
955 * Returns a read-only (constant) iterator that points to the
956 * first element in the %list. Iteration is done in ordinary
960 cbegin() const noexcept
961 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
964 * Returns a read-only (constant) iterator that points one past
965 * the last element in the %list. Iteration is done in ordinary
969 cend() const noexcept
970 { return const_iterator(&this->_M_impl
._M_node
); }
973 * Returns a read-only (constant) reverse iterator that points to
974 * the last element in the %list. Iteration is done in reverse
977 const_reverse_iterator
978 crbegin() const noexcept
979 { return const_reverse_iterator(end()); }
982 * Returns a read-only (constant) reverse iterator that points to one
983 * before the first element in the %list. Iteration is done in reverse
986 const_reverse_iterator
987 crend() const noexcept
988 { return const_reverse_iterator(begin()); }
991 // [23.2.2.2] capacity
993 * Returns true if the %list is empty. (Thus begin() would equal
997 empty() const _GLIBCXX_NOEXCEPT
998 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
1000 /** Returns the number of elements in the %list. */
1002 size() const _GLIBCXX_NOEXCEPT
1003 { return this->_M_node_count(); }
1005 /** Returns the size() of the largest possible %list. */
1007 max_size() const _GLIBCXX_NOEXCEPT
1008 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1010 #if __cplusplus >= 201103L
1012 * @brief Resizes the %list to the specified number of elements.
1013 * @param __new_size Number of elements the %list should contain.
1015 * This function will %resize the %list to the specified number
1016 * of elements. If the number is smaller than the %list's
1017 * current size the %list is truncated, otherwise default
1018 * constructed elements are appended.
1021 resize(size_type __new_size
);
1024 * @brief Resizes the %list to the specified number of elements.
1025 * @param __new_size Number of elements the %list should contain.
1026 * @param __x Data with which new elements should be populated.
1028 * This function will %resize the %list to the specified number
1029 * of elements. If the number is smaller than the %list's
1030 * current size the %list is truncated, otherwise the %list is
1031 * extended and new elements are populated with given data.
1034 resize(size_type __new_size
, const value_type
& __x
);
1037 * @brief Resizes the %list to the specified number of elements.
1038 * @param __new_size Number of elements the %list should contain.
1039 * @param __x Data with which new elements should be populated.
1041 * This function will %resize the %list to the specified number
1042 * of elements. If the number is smaller than the %list's
1043 * current size the %list is truncated, otherwise the %list is
1044 * extended and new elements are populated with given data.
1047 resize(size_type __new_size
, value_type __x
= value_type());
1052 * Returns a read/write reference to the data at the first
1053 * element of the %list.
1056 front() _GLIBCXX_NOEXCEPT
1057 { return *begin(); }
1060 * Returns a read-only (constant) reference to the data at the first
1061 * element of the %list.
1064 front() const _GLIBCXX_NOEXCEPT
1065 { return *begin(); }
1068 * Returns a read/write reference to the data at the last element
1072 back() _GLIBCXX_NOEXCEPT
1074 iterator __tmp
= end();
1080 * Returns a read-only (constant) reference to the data at the last
1081 * element of the %list.
1084 back() const _GLIBCXX_NOEXCEPT
1086 const_iterator __tmp
= end();
1091 // [23.2.2.3] modifiers
1093 * @brief Add data to the front of the %list.
1094 * @param __x Data to be added.
1096 * This is a typical stack operation. The function creates an
1097 * element at the front of the %list and assigns the given data
1098 * to it. Due to the nature of a %list this operation can be
1099 * done in constant time, and does not invalidate iterators and
1103 push_front(const value_type
& __x
)
1104 { this->_M_insert(begin(), __x
); }
1106 #if __cplusplus >= 201103L
1108 push_front(value_type
&& __x
)
1109 { this->_M_insert(begin(), std::move(__x
)); }
1111 template<typename
... _Args
>
1112 #if __cplusplus > 201402L
1117 emplace_front(_Args
&&... __args
)
1119 this->_M_insert(begin(), std::forward
<_Args
>(__args
)...);
1120 #if __cplusplus > 201402L
1127 * @brief Removes first element.
1129 * This is a typical stack operation. It shrinks the %list by
1130 * one. Due to the nature of a %list this operation can be done
1131 * in constant time, and only invalidates iterators/references to
1132 * the element being removed.
1134 * Note that no data is returned, and if the first element's data
1135 * is needed, it should be retrieved before pop_front() is
1139 pop_front() _GLIBCXX_NOEXCEPT
1140 { this->_M_erase(begin()); }
1143 * @brief Add data to the end of the %list.
1144 * @param __x Data to be added.
1146 * This is a typical stack operation. The function creates an
1147 * element at the end of the %list and assigns the given data to
1148 * it. Due to the nature of a %list this operation can be done
1149 * in constant time, and does not invalidate iterators and
1153 push_back(const value_type
& __x
)
1154 { this->_M_insert(end(), __x
); }
1156 #if __cplusplus >= 201103L
1158 push_back(value_type
&& __x
)
1159 { this->_M_insert(end(), std::move(__x
)); }
1161 template<typename
... _Args
>
1162 #if __cplusplus > 201402L
1167 emplace_back(_Args
&&... __args
)
1169 this->_M_insert(end(), std::forward
<_Args
>(__args
)...);
1170 #if __cplusplus > 201402L
1177 * @brief Removes last element.
1179 * This is a typical stack operation. It shrinks the %list by
1180 * one. Due to the nature of a %list this operation can be done
1181 * in constant time, and only invalidates iterators/references to
1182 * the element being removed.
1184 * Note that no data is returned, and if the last element's data
1185 * is needed, it should be retrieved before pop_back() is called.
1188 pop_back() _GLIBCXX_NOEXCEPT
1189 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1191 #if __cplusplus >= 201103L
1193 * @brief Constructs object in %list before specified iterator.
1194 * @param __position A const_iterator into the %list.
1195 * @param __args Arguments.
1196 * @return An iterator that points to the inserted data.
1198 * This function will insert an object of type T constructed
1199 * with T(std::forward<Args>(args)...) before the specified
1200 * location. Due to the nature of a %list this operation can
1201 * be done in constant time, and does not invalidate iterators
1204 template<typename
... _Args
>
1206 emplace(const_iterator __position
, _Args
&&... __args
);
1209 * @brief Inserts given value into %list before specified iterator.
1210 * @param __position A const_iterator into the %list.
1211 * @param __x Data to be inserted.
1212 * @return An iterator that points to the inserted data.
1214 * This function will insert a copy of the given value before
1215 * the specified location. Due to the nature of a %list this
1216 * operation can be done in constant time, and does not
1217 * invalidate iterators and references.
1220 insert(const_iterator __position
, const value_type
& __x
);
1223 * @brief Inserts given value into %list before specified iterator.
1224 * @param __position An iterator into the %list.
1225 * @param __x Data to be inserted.
1226 * @return An iterator that points to the inserted data.
1228 * This function will insert a copy of the given value before
1229 * the specified location. Due to the nature of a %list this
1230 * operation can be done in constant time, and does not
1231 * invalidate iterators and references.
1234 insert(iterator __position
, const value_type
& __x
);
1237 #if __cplusplus >= 201103L
1239 * @brief Inserts given rvalue into %list before specified iterator.
1240 * @param __position A const_iterator into the %list.
1241 * @param __x Data to be inserted.
1242 * @return An iterator that points to the inserted data.
1244 * This function will insert a copy of the given rvalue before
1245 * the specified location. Due to the nature of a %list this
1246 * operation can be done in constant time, and does not
1247 * invalidate iterators and references.
1250 insert(const_iterator __position
, value_type
&& __x
)
1251 { return emplace(__position
, std::move(__x
)); }
1254 * @brief Inserts the contents of an initializer_list into %list
1255 * before specified const_iterator.
1256 * @param __p A const_iterator into the %list.
1257 * @param __l An initializer_list of value_type.
1258 * @return An iterator pointing to the first element inserted
1261 * This function will insert copies of the data in the
1262 * initializer_list @a l into the %list before the location
1263 * specified by @a p.
1265 * This operation is linear in the number of elements inserted and
1266 * does not invalidate iterators and references.
1269 insert(const_iterator __p
, initializer_list
<value_type
> __l
)
1270 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1273 #if __cplusplus >= 201103L
1275 * @brief Inserts a number of copies of given data into the %list.
1276 * @param __position A const_iterator into the %list.
1277 * @param __n Number of elements to be inserted.
1278 * @param __x Data to be inserted.
1279 * @return An iterator pointing to the first element inserted
1282 * This function will insert a specified number of copies of the
1283 * given data before the location specified by @a position.
1285 * This operation is linear in the number of elements inserted and
1286 * does not invalidate iterators and references.
1289 insert(const_iterator __position
, size_type __n
, const value_type
& __x
);
1292 * @brief Inserts a number of copies of given data into the %list.
1293 * @param __position An iterator into the %list.
1294 * @param __n Number of elements to be inserted.
1295 * @param __x Data to be inserted.
1297 * This function will insert a specified number of copies of the
1298 * given data before the location specified by @a position.
1300 * This operation is linear in the number of elements inserted and
1301 * does not invalidate iterators and references.
1304 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1306 list
__tmp(__n
, __x
, get_allocator());
1307 splice(__position
, __tmp
);
1311 #if __cplusplus >= 201103L
1313 * @brief Inserts a range into the %list.
1314 * @param __position A const_iterator into the %list.
1315 * @param __first An input iterator.
1316 * @param __last An input iterator.
1317 * @return An iterator pointing to the first element inserted
1320 * This function will insert copies of the data in the range [@a
1321 * first,@a last) into the %list before the location specified by
1324 * This operation is linear in the number of elements inserted and
1325 * does not invalidate iterators and references.
1327 template<typename _InputIterator
,
1328 typename
= std::_RequireInputIter
<_InputIterator
>>
1330 insert(const_iterator __position
, _InputIterator __first
,
1331 _InputIterator __last
);
1334 * @brief Inserts a range into the %list.
1335 * @param __position An iterator into the %list.
1336 * @param __first An input iterator.
1337 * @param __last An input iterator.
1339 * This function will insert copies of the data in the range [@a
1340 * first,@a last) into the %list before the location specified by
1343 * This operation is linear in the number of elements inserted and
1344 * does not invalidate iterators and references.
1346 template<typename _InputIterator
>
1348 insert(iterator __position
, _InputIterator __first
,
1349 _InputIterator __last
)
1351 list
__tmp(__first
, __last
, get_allocator());
1352 splice(__position
, __tmp
);
1357 * @brief Remove element at given position.
1358 * @param __position Iterator pointing to element to be erased.
1359 * @return An iterator pointing to the next element (or end()).
1361 * This function will erase the element at the given position and thus
1362 * shorten the %list by one.
1364 * Due to the nature of a %list this operation can be done in
1365 * constant time, and only invalidates iterators/references to
1366 * the element being removed. The user is also cautioned that
1367 * this function only erases the element, and that if the element
1368 * is itself a pointer, the pointed-to memory is not touched in
1369 * any way. Managing the pointer is the user's responsibility.
1372 #if __cplusplus >= 201103L
1373 erase(const_iterator __position
) noexcept
;
1375 erase(iterator __position
);
1379 * @brief Remove a range of elements.
1380 * @param __first Iterator pointing to the first element to be erased.
1381 * @param __last Iterator pointing to one past the last element to be
1383 * @return An iterator pointing to the element pointed to by @a last
1384 * prior to erasing (or end()).
1386 * This function will erase the elements in the range @a
1387 * [first,last) and shorten the %list accordingly.
1389 * This operation is linear time in the size of the range and only
1390 * invalidates iterators/references to the element being removed.
1391 * The user is also cautioned that this function only erases the
1392 * elements, and that if the elements themselves are pointers, the
1393 * pointed-to memory is not touched in any way. Managing the pointer
1394 * is the user's responsibility.
1397 #if __cplusplus >= 201103L
1398 erase(const_iterator __first
, const_iterator __last
) noexcept
1400 erase(iterator __first
, iterator __last
)
1403 while (__first
!= __last
)
1404 __first
= erase(__first
);
1405 return __last
._M_const_cast();
1409 * @brief Swaps data with another %list.
1410 * @param __x A %list of the same element and allocator types.
1412 * This exchanges the elements between two lists in constant
1413 * time. Note that the global std::swap() function is
1414 * specialized such that std::swap(l1,l2) will feed to this
1417 * Whether the allocators are swapped depends on the allocator traits.
1420 swap(list
& __x
) _GLIBCXX_NOEXCEPT
1422 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1423 __x
._M_impl
._M_node
);
1425 size_t __xsize
= __x
._M_get_size();
1426 __x
._M_set_size(this->_M_get_size());
1427 this->_M_set_size(__xsize
);
1429 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1430 __x
._M_get_Node_allocator());
1434 * Erases all the elements. Note that this function only erases
1435 * the elements, and that if the elements themselves are
1436 * pointers, the pointed-to memory is not touched in any way.
1437 * Managing the pointer is the user's responsibility.
1440 clear() _GLIBCXX_NOEXCEPT
1446 // [23.2.2.4] list operations
1448 * @brief Insert contents of another %list.
1449 * @param __position Iterator referencing the element to insert before.
1450 * @param __x Source list.
1452 * The elements of @a __x are inserted in constant time in front of
1453 * the element referenced by @a __position. @a __x becomes an empty
1456 * Requires this != @a __x.
1459 #if __cplusplus >= 201103L
1460 splice(const_iterator __position
, list
&& __x
) noexcept
1462 splice(iterator __position
, list
& __x
)
1467 _M_check_equal_allocators(__x
);
1469 this->_M_transfer(__position
._M_const_cast(),
1470 __x
.begin(), __x
.end());
1472 this->_M_inc_size(__x
._M_get_size());
1477 #if __cplusplus >= 201103L
1479 splice(const_iterator __position
, list
& __x
) noexcept
1480 { splice(__position
, std::move(__x
)); }
1483 #if __cplusplus >= 201103L
1485 * @brief Insert element from another %list.
1486 * @param __position Const_iterator referencing the element to
1488 * @param __x Source list.
1489 * @param __i Const_iterator referencing the element to move.
1491 * Removes the element in list @a __x referenced by @a __i and
1492 * inserts it into the current list before @a __position.
1495 splice(const_iterator __position
, list
&& __x
, const_iterator __i
) noexcept
1498 * @brief Insert element from another %list.
1499 * @param __position Iterator referencing the element to insert before.
1500 * @param __x Source list.
1501 * @param __i Iterator referencing the element to move.
1503 * Removes the element in list @a __x referenced by @a __i and
1504 * inserts it into the current list before @a __position.
1507 splice(iterator __position
, list
& __x
, iterator __i
)
1510 iterator __j
= __i
._M_const_cast();
1512 if (__position
== __i
|| __position
== __j
)
1515 if (this != std::__addressof(__x
))
1516 _M_check_equal_allocators(__x
);
1518 this->_M_transfer(__position
._M_const_cast(),
1519 __i
._M_const_cast(), __j
);
1521 this->_M_inc_size(1);
1525 #if __cplusplus >= 201103L
1527 * @brief Insert element from another %list.
1528 * @param __position Const_iterator referencing the element to
1530 * @param __x Source list.
1531 * @param __i Const_iterator referencing the element to move.
1533 * Removes the element in list @a __x referenced by @a __i and
1534 * inserts it into the current list before @a __position.
1537 splice(const_iterator __position
, list
& __x
, const_iterator __i
) noexcept
1538 { splice(__position
, std::move(__x
), __i
); }
1541 #if __cplusplus >= 201103L
1543 * @brief Insert range from another %list.
1544 * @param __position Const_iterator referencing the element to
1546 * @param __x Source list.
1547 * @param __first Const_iterator referencing the start of range in x.
1548 * @param __last Const_iterator referencing the end of range in x.
1550 * Removes elements in the range [__first,__last) and inserts them
1551 * before @a __position in constant time.
1553 * Undefined if @a __position is in [__first,__last).
1556 splice(const_iterator __position
, list
&& __x
, const_iterator __first
,
1557 const_iterator __last
) noexcept
1560 * @brief Insert range from another %list.
1561 * @param __position Iterator referencing the element to insert before.
1562 * @param __x Source list.
1563 * @param __first Iterator referencing the start of range in x.
1564 * @param __last Iterator referencing the end of range in x.
1566 * Removes elements in the range [__first,__last) and inserts them
1567 * before @a __position in constant time.
1569 * Undefined if @a __position is in [__first,__last).
1572 splice(iterator __position
, list
& __x
, iterator __first
,
1576 if (__first
!= __last
)
1578 if (this != std::__addressof(__x
))
1579 _M_check_equal_allocators(__x
);
1581 size_t __n
= this->_M_distance(__first
._M_node
, __last
._M_node
);
1582 this->_M_inc_size(__n
);
1583 __x
._M_dec_size(__n
);
1585 this->_M_transfer(__position
._M_const_cast(),
1586 __first
._M_const_cast(),
1587 __last
._M_const_cast());
1591 #if __cplusplus >= 201103L
1593 * @brief Insert range from another %list.
1594 * @param __position Const_iterator referencing the element to
1596 * @param __x Source list.
1597 * @param __first Const_iterator referencing the start of range in x.
1598 * @param __last Const_iterator referencing the end of range in x.
1600 * Removes elements in the range [__first,__last) and inserts them
1601 * before @a __position in constant time.
1603 * Undefined if @a __position is in [__first,__last).
1606 splice(const_iterator __position
, list
& __x
, const_iterator __first
,
1607 const_iterator __last
) noexcept
1608 { splice(__position
, std::move(__x
), __first
, __last
); }
1612 * @brief Remove all elements equal to value.
1613 * @param __value The value to remove.
1615 * Removes every element in the list equal to @a value.
1616 * Remaining elements stay in list order. Note that this
1617 * function only erases the elements, and that if the elements
1618 * themselves are pointers, the pointed-to memory is not
1619 * touched in any way. Managing the pointer is the user's
1623 remove(const _Tp
& __value
);
1626 * @brief Remove all elements satisfying a predicate.
1627 * @tparam _Predicate Unary predicate function or object.
1629 * Removes every element in the list for which the predicate
1630 * returns true. Remaining elements stay in list order. Note
1631 * that this function only erases the elements, and that if the
1632 * elements themselves are pointers, the pointed-to memory is
1633 * not touched in any way. Managing the pointer is the user's
1636 template<typename _Predicate
>
1638 remove_if(_Predicate
);
1641 * @brief Remove consecutive duplicate elements.
1643 * For each consecutive set of elements with the same value,
1644 * remove all but the first one. Remaining elements stay in
1645 * list order. Note that this function only erases the
1646 * elements, and that if the elements themselves are pointers,
1647 * the pointed-to memory is not touched in any way. Managing
1648 * the pointer is the user's responsibility.
1654 * @brief Remove consecutive elements satisfying a predicate.
1655 * @tparam _BinaryPredicate Binary predicate function or object.
1657 * For each consecutive set of elements [first,last) that
1658 * satisfy predicate(first,i) where i is an iterator in
1659 * [first,last), remove all but the first one. Remaining
1660 * elements stay in list order. Note that this function only
1661 * erases the elements, and that if the elements themselves are
1662 * pointers, the pointed-to memory is not touched in any way.
1663 * Managing the pointer is the user's responsibility.
1665 template<typename _BinaryPredicate
>
1667 unique(_BinaryPredicate
);
1670 * @brief Merge sorted lists.
1671 * @param __x Sorted list to merge.
1673 * Assumes that both @a __x and this list are sorted according to
1674 * operator<(). Merges elements of @a __x into this list in
1675 * sorted order, leaving @a __x empty when complete. Elements in
1676 * this list precede elements in @a __x that are equal.
1678 #if __cplusplus >= 201103L
1684 { merge(std::move(__x
)); }
1691 * @brief Merge sorted lists according to comparison function.
1692 * @tparam _StrictWeakOrdering Comparison function defining
1694 * @param __x Sorted list to merge.
1695 * @param __comp Comparison functor.
1697 * Assumes that both @a __x and this list are sorted according to
1698 * StrictWeakOrdering. Merges elements of @a __x into this list
1699 * in sorted order, leaving @a __x empty when complete. Elements
1700 * in this list precede elements in @a __x that are equivalent
1701 * according to StrictWeakOrdering().
1703 #if __cplusplus >= 201103L
1704 template<typename _StrictWeakOrdering
>
1706 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1708 template<typename _StrictWeakOrdering
>
1710 merge(list
& __x
, _StrictWeakOrdering __comp
)
1711 { merge(std::move(__x
), __comp
); }
1713 template<typename _StrictWeakOrdering
>
1715 merge(list
& __x
, _StrictWeakOrdering __comp
);
1719 * @brief Reverse the elements in list.
1721 * Reverse the order of elements in the list in linear time.
1724 reverse() _GLIBCXX_NOEXCEPT
1725 { this->_M_impl
._M_node
._M_reverse(); }
1728 * @brief Sort the elements.
1730 * Sorts the elements of this list in NlogN time. Equivalent
1731 * elements remain in list order.
1737 * @brief Sort the elements according to comparison function.
1739 * Sorts the elements of this list in NlogN time. Equivalent
1740 * elements remain in list order.
1742 template<typename _StrictWeakOrdering
>
1744 sort(_StrictWeakOrdering
);
1747 // Internal constructor functions follow.
1749 // Called by the range constructor to implement [23.1.1]/9
1751 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1752 // 438. Ambiguity in the "do the right thing" clause
1753 template<typename _Integer
>
1755 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1756 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1758 // Called by the range constructor to implement [23.1.1]/9
1759 template<typename _InputIterator
>
1761 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1764 for (; __first
!= __last
; ++__first
)
1765 #if __cplusplus >= 201103L
1766 emplace_back(*__first
);
1768 push_back(*__first
);
1772 // Called by list(n,v,a), and the range constructor when it turns out
1773 // to be the same thing.
1775 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1781 #if __cplusplus >= 201103L
1782 // Called by list(n).
1784 _M_default_initialize(size_type __n
)
1790 // Called by resize(sz).
1792 _M_default_append(size_type __n
);
1795 // Internal assign functions follow.
1797 // Called by the range assign to implement [23.1.1]/9
1799 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1800 // 438. Ambiguity in the "do the right thing" clause
1801 template<typename _Integer
>
1803 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1804 { _M_fill_assign(__n
, __val
); }
1806 // Called by the range assign to implement [23.1.1]/9
1807 template<typename _InputIterator
>
1809 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1812 // Called by assign(n,t), and the range assign when it turns out
1813 // to be the same thing.
1815 _M_fill_assign(size_type __n
, const value_type
& __val
);
1818 // Moves the elements from [first,last) before position.
1820 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1821 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1823 // Inserts new element at position given and with value given.
1824 #if __cplusplus < 201103L
1826 _M_insert(iterator __position
, const value_type
& __x
)
1828 _Node
* __tmp
= _M_create_node(__x
);
1829 __tmp
->_M_hook(__position
._M_node
);
1830 this->_M_inc_size(1);
1833 template<typename
... _Args
>
1835 _M_insert(iterator __position
, _Args
&&... __args
)
1837 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1838 __tmp
->_M_hook(__position
._M_node
);
1839 this->_M_inc_size(1);
1843 // Erases element at position given.
1845 _M_erase(iterator __position
) _GLIBCXX_NOEXCEPT
1847 this->_M_dec_size(1);
1848 __position
._M_node
->_M_unhook();
1849 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1850 #if __cplusplus >= 201103L
1851 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n
->_M_valptr());
1853 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n
->_M_valptr());
1859 // To implement the splice (and merge) bits of N1599.
1861 _M_check_equal_allocators(list
& __x
) _GLIBCXX_NOEXCEPT
1863 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1864 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1868 // Used to implement resize.
1870 _M_resize_pos(size_type
& __new_size
) const;
1872 #if __cplusplus >= 201103L
1874 _M_move_assign(list
&& __x
, true_type
) noexcept
1877 this->_M_move_nodes(std::move(__x
));
1878 std::__alloc_on_move(this->_M_get_Node_allocator(),
1879 __x
._M_get_Node_allocator());
1883 _M_move_assign(list
&& __x
, false_type
)
1885 if (__x
._M_get_Node_allocator() == this->_M_get_Node_allocator())
1886 _M_move_assign(std::move(__x
), true_type
{});
1888 // The rvalue's allocator cannot be moved, or is not equal,
1889 // so we need to individually move each element.
1890 _M_assign_dispatch(std::__make_move_if_noexcept_iterator(__x
.begin()),
1891 std::__make_move_if_noexcept_iterator(__x
.end()),
1897 #if __cpp_deduction_guides >= 201606
1898 template<typename _InputIterator
, typename _ValT
1899 = typename iterator_traits
<_InputIterator
>::value_type
,
1900 typename _Allocator
= allocator
<_ValT
>,
1901 typename
= _RequireInputIter
<_InputIterator
>,
1902 typename
= _RequireAllocator
<_Allocator
>>
1903 list(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
1904 -> list
<_ValT
, _Allocator
>;
1907 _GLIBCXX_END_NAMESPACE_CXX11
1910 * @brief List equality comparison.
1911 * @param __x A %list.
1912 * @param __y A %list of the same type as @a __x.
1913 * @return True iff the size and elements of the lists are equal.
1915 * This is an equivalence relation. It is linear in the size of
1916 * the lists. Lists are considered equivalent if their sizes are
1917 * equal, and if corresponding elements compare equal.
1919 template<typename _Tp
, typename _Alloc
>
1921 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1923 #if _GLIBCXX_USE_CXX11_ABI
1924 if (__x
.size() != __y
.size())
1928 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1929 const_iterator __end1
= __x
.end();
1930 const_iterator __end2
= __y
.end();
1932 const_iterator __i1
= __x
.begin();
1933 const_iterator __i2
= __y
.begin();
1934 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1939 return __i1
== __end1
&& __i2
== __end2
;
1943 * @brief List ordering relation.
1944 * @param __x A %list.
1945 * @param __y A %list of the same type as @a __x.
1946 * @return True iff @a __x is lexicographically less than @a __y.
1948 * This is a total ordering relation. It is linear in the size of the
1949 * lists. The elements must be comparable with @c <.
1951 * See std::lexicographical_compare() for how the determination is made.
1953 template<typename _Tp
, typename _Alloc
>
1955 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1956 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1957 __y
.begin(), __y
.end()); }
1959 /// Based on operator==
1960 template<typename _Tp
, typename _Alloc
>
1962 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1963 { return !(__x
== __y
); }
1965 /// Based on operator<
1966 template<typename _Tp
, typename _Alloc
>
1968 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1969 { return __y
< __x
; }
1971 /// Based on operator<
1972 template<typename _Tp
, typename _Alloc
>
1974 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1975 { return !(__y
< __x
); }
1977 /// Based on operator<
1978 template<typename _Tp
, typename _Alloc
>
1980 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1981 { return !(__x
< __y
); }
1983 /// See std::list::swap().
1984 template<typename _Tp
, typename _Alloc
>
1986 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1987 _GLIBCXX_NOEXCEPT_IF(noexcept(__x
.swap(__y
)))
1990 _GLIBCXX_END_NAMESPACE_CONTAINER
1992 #if _GLIBCXX_USE_CXX11_ABI
1994 // Detect when distance is used to compute the size of the whole list.
1995 template<typename _Tp
>
1997 __distance(_GLIBCXX_STD_C::_List_iterator
<_Tp
> __first
,
1998 _GLIBCXX_STD_C::_List_iterator
<_Tp
> __last
,
1999 input_iterator_tag __tag
)
2001 typedef _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> _CIter
;
2002 return std::__distance(_CIter(__first
), _CIter(__last
), __tag
);
2005 template<typename _Tp
>
2007 __distance(_GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __first
,
2008 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __last
,
2011 typedef __detail::_List_node_header _Sentinel
;
2012 _GLIBCXX_STD_C::_List_const_iterator
<_Tp
> __beyond
= __last
;
2014 const bool __whole
= __first
== __beyond
;
2015 if (__builtin_constant_p (__whole
) && __whole
)
2016 return static_cast<const _Sentinel
*>(__last
._M_node
)->_M_size
;
2019 while (__first
!= __last
)
2028 _GLIBCXX_END_NAMESPACE_VERSION
2031 #endif /* _STL_LIST_H */