1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2013 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 #if __cplusplus >= 201103L
61 #include <initializer_list>
64 namespace std
_GLIBCXX_VISIBILITY(default)
68 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 // Supporting structures are split into common and templated
71 // types; the latter publicly inherits from the former in an
72 // effort to reduce code duplication. This results in some
73 // "needless" static_cast'ing later on, but it's all safe
76 /// Common part of a node in the %list.
77 struct _List_node_base
79 _List_node_base
* _M_next
;
80 _List_node_base
* _M_prev
;
83 swap(_List_node_base
& __x
, _List_node_base
& __y
) _GLIBCXX_USE_NOEXCEPT
;
86 _M_transfer(_List_node_base
* const __first
,
87 _List_node_base
* const __last
) _GLIBCXX_USE_NOEXCEPT
;
90 _M_reverse() _GLIBCXX_USE_NOEXCEPT
;
93 _M_hook(_List_node_base
* const __position
) _GLIBCXX_USE_NOEXCEPT
;
96 _M_unhook() _GLIBCXX_USE_NOEXCEPT
;
99 _GLIBCXX_END_NAMESPACE_VERSION
100 } // namespace detail
102 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
104 /// An actual node in the %list.
105 template<typename _Tp
>
106 struct _List_node
: public __detail::_List_node_base
111 #if __cplusplus >= 201103L
112 template<typename
... _Args
>
113 _List_node(_Args
&&... __args
)
114 : __detail::_List_node_base(), _M_data(std::forward
<_Args
>(__args
)...)
120 * @brief A list::iterator.
122 * All the functions are op overloads.
124 template<typename _Tp
>
125 struct _List_iterator
127 typedef _List_iterator
<_Tp
> _Self
;
128 typedef _List_node
<_Tp
> _Node
;
130 typedef ptrdiff_t difference_type
;
131 typedef std::bidirectional_iterator_tag iterator_category
;
132 typedef _Tp value_type
;
133 typedef _Tp
* pointer
;
134 typedef _Tp
& reference
;
140 _List_iterator(__detail::_List_node_base
* __x
)
143 // Must downcast from _List_node_base to _List_node to get to _M_data.
146 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
150 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
155 _M_node
= _M_node
->_M_next
;
163 _M_node
= _M_node
->_M_next
;
170 _M_node
= _M_node
->_M_prev
;
178 _M_node
= _M_node
->_M_prev
;
183 operator==(const _Self
& __x
) const
184 { return _M_node
== __x
._M_node
; }
187 operator!=(const _Self
& __x
) const
188 { return _M_node
!= __x
._M_node
; }
190 // The only member points to the %list element.
191 __detail::_List_node_base
* _M_node
;
195 * @brief A list::const_iterator.
197 * All the functions are op overloads.
199 template<typename _Tp
>
200 struct _List_const_iterator
202 typedef _List_const_iterator
<_Tp
> _Self
;
203 typedef const _List_node
<_Tp
> _Node
;
204 typedef _List_iterator
<_Tp
> iterator
;
206 typedef ptrdiff_t difference_type
;
207 typedef std::bidirectional_iterator_tag iterator_category
;
208 typedef _Tp value_type
;
209 typedef const _Tp
* pointer
;
210 typedef const _Tp
& reference
;
212 _List_const_iterator()
216 _List_const_iterator(const __detail::_List_node_base
* __x
)
219 _List_const_iterator(const iterator
& __x
)
220 : _M_node(__x
._M_node
) { }
222 // Must downcast from List_node_base to _List_node to get to
226 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
230 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
235 _M_node
= _M_node
->_M_next
;
243 _M_node
= _M_node
->_M_next
;
250 _M_node
= _M_node
->_M_prev
;
258 _M_node
= _M_node
->_M_prev
;
263 operator==(const _Self
& __x
) const
264 { return _M_node
== __x
._M_node
; }
267 operator!=(const _Self
& __x
) const
268 { return _M_node
!= __x
._M_node
; }
270 // The only member points to the %list element.
271 const __detail::_List_node_base
* _M_node
;
274 template<typename _Val
>
276 operator==(const _List_iterator
<_Val
>& __x
,
277 const _List_const_iterator
<_Val
>& __y
)
278 { return __x
._M_node
== __y
._M_node
; }
280 template<typename _Val
>
282 operator!=(const _List_iterator
<_Val
>& __x
,
283 const _List_const_iterator
<_Val
>& __y
)
284 { return __x
._M_node
!= __y
._M_node
; }
287 /// See bits/stl_deque.h's _Deque_base for an explanation.
288 template<typename _Tp
, typename _Alloc
>
293 // The stored instance is not actually of "allocator_type"'s
294 // type. Instead we rebind the type to
295 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
296 // should probably be the same. List_node<Tp> is not the same
297 // size as Tp (it's two pointers larger), and specializations on
298 // Tp may go unused because List_node<Tp> is being bound
301 // We put this to the test in the constructors and in
302 // get_allocator, where we use conversions between
303 // allocator_type and _Node_alloc_type. The conversion is
304 // required by table 32 in [20.1.5].
305 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
308 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
311 : public _Node_alloc_type
313 __detail::_List_node_base _M_node
;
316 : _Node_alloc_type(), _M_node()
319 _List_impl(const _Node_alloc_type
& __a
)
320 : _Node_alloc_type(__a
), _M_node()
323 #if __cplusplus >= 201103L
324 _List_impl(_Node_alloc_type
&& __a
)
325 : _Node_alloc_type(std::move(__a
)), _M_node()
334 { return _M_impl
._Node_alloc_type::allocate(1); }
337 _M_put_node(_List_node
<_Tp
>* __p
)
338 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
341 typedef _Alloc allocator_type
;
344 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
345 { return *static_cast<_Node_alloc_type
*>(&_M_impl
); }
347 const _Node_alloc_type
&
348 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
349 { return *static_cast<const _Node_alloc_type
*>(&_M_impl
); }
352 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
353 { return _Tp_alloc_type(_M_get_Node_allocator()); }
356 get_allocator() const _GLIBCXX_NOEXCEPT
357 { return allocator_type(_M_get_Node_allocator()); }
363 _List_base(const _Node_alloc_type
& __a
)
367 #if __cplusplus >= 201103L
368 _List_base(_List_base
&& __x
)
369 : _M_impl(std::move(__x
._M_get_Node_allocator()))
372 __detail::_List_node_base::swap(_M_impl
._M_node
, __x
._M_impl
._M_node
);
376 // This is what actually destroys the list.
377 ~_List_base() _GLIBCXX_NOEXCEPT
386 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
387 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
392 * @brief A standard container with linear time access to elements,
393 * and fixed time insertion/deletion at any point in the sequence.
397 * @tparam _Tp Type of element.
398 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
400 * Meets the requirements of a <a href="tables.html#65">container</a>, a
401 * <a href="tables.html#66">reversible container</a>, and a
402 * <a href="tables.html#67">sequence</a>, including the
403 * <a href="tables.html#68">optional sequence requirements</a> with the
404 * %exception of @c at and @c operator[].
406 * This is a @e doubly @e linked %list. Traversal up and down the
407 * %list requires linear time, but adding and removing elements (or
408 * @e nodes) is done in constant time, regardless of where the
409 * change takes place. Unlike std::vector and std::deque,
410 * random-access iterators are not provided, so subscripting ( @c
411 * [] ) access is not allowed. For algorithms which only need
412 * sequential access, this lack makes no difference.
414 * Also unlike the other standard containers, std::list provides
415 * specialized algorithms %unique to linked lists, such as
416 * splicing, sorting, and in-place reversal.
418 * A couple points on memory allocation for list<Tp>:
420 * First, we never actually allocate a Tp, we allocate
421 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
422 * that after elements from %list<X,Alloc1> are spliced into
423 * %list<X,Alloc2>, destroying the memory of the second %list is a
424 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
426 * Second, a %list conceptually represented as
428 * A <---> B <---> C <---> D
430 * is actually circular; a link exists between A and D. The %list
431 * class holds (as its only data member) a private list::iterator
432 * pointing to @e D, not to @e A! To get to the head of the %list,
433 * we start at the tail and move forward by one. When this member
434 * iterator's next/previous pointers refer to itself, the %list is
437 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
438 class list
: protected _List_base
<_Tp
, _Alloc
>
440 // concept requirements
441 typedef typename
_Alloc::value_type _Alloc_value_type
;
442 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
443 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
445 typedef _List_base
<_Tp
, _Alloc
> _Base
;
446 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
447 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
450 typedef _Tp value_type
;
451 typedef typename
_Tp_alloc_type::pointer pointer
;
452 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
453 typedef typename
_Tp_alloc_type::reference reference
;
454 typedef typename
_Tp_alloc_type::const_reference const_reference
;
455 typedef _List_iterator
<_Tp
> iterator
;
456 typedef _List_const_iterator
<_Tp
> const_iterator
;
457 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
458 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
459 typedef size_t size_type
;
460 typedef ptrdiff_t difference_type
;
461 typedef _Alloc allocator_type
;
464 // Note that pointers-to-_Node's can be ctor-converted to
466 typedef _List_node
<_Tp
> _Node
;
468 using _Base::_M_impl
;
469 using _Base::_M_put_node
;
470 using _Base::_M_get_node
;
471 using _Base::_M_get_Tp_allocator
;
472 using _Base::_M_get_Node_allocator
;
475 * @param __args An instance of user data.
477 * Allocates space for a new node and constructs a copy of
480 #if __cplusplus < 201103L
482 _M_create_node(const value_type
& __x
)
484 _Node
* __p
= this->_M_get_node();
487 _M_get_Tp_allocator().construct
488 (std::__addressof(__p
->_M_data
), __x
);
493 __throw_exception_again
;
498 template<typename
... _Args
>
500 _M_create_node(_Args
&&... __args
)
502 _Node
* __p
= this->_M_get_node();
505 _M_get_Node_allocator().construct(__p
,
506 std::forward
<_Args
>(__args
)...);
511 __throw_exception_again
;
518 // [23.2.2.1] construct/copy/destroy
519 // (assign() and get_allocator() are also listed in this section)
521 * @brief Default constructor creates no elements.
527 * @brief Creates a %list with no elements.
528 * @param __a An allocator object.
531 list(const allocator_type
& __a
)
532 : _Base(_Node_alloc_type(__a
)) { }
534 #if __cplusplus >= 201103L
536 * @brief Creates a %list with default constructed elements.
537 * @param __n The number of elements to initially create.
539 * This constructor fills the %list with @a __n default
540 * constructed elements.
545 { _M_default_initialize(__n
); }
548 * @brief Creates a %list with copies of an exemplar element.
549 * @param __n The number of elements to initially create.
550 * @param __value An element to copy.
551 * @param __a An allocator object.
553 * This constructor fills the %list with @a __n copies of @a __value.
555 list(size_type __n
, const value_type
& __value
,
556 const allocator_type
& __a
= allocator_type())
557 : _Base(_Node_alloc_type(__a
))
558 { _M_fill_initialize(__n
, __value
); }
561 * @brief Creates a %list with copies of an exemplar element.
562 * @param __n The number of elements to initially create.
563 * @param __value An element to copy.
564 * @param __a An allocator object.
566 * This constructor fills the %list with @a __n copies of @a __value.
569 list(size_type __n
, const value_type
& __value
= value_type(),
570 const allocator_type
& __a
= allocator_type())
571 : _Base(_Node_alloc_type(__a
))
572 { _M_fill_initialize(__n
, __value
); }
576 * @brief %List copy constructor.
577 * @param __x A %list of identical element and allocator types.
579 * The newly-created %list uses a copy of the allocation object used
582 list(const list
& __x
)
583 : _Base(__x
._M_get_Node_allocator())
584 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
586 #if __cplusplus >= 201103L
588 * @brief %List move constructor.
589 * @param __x A %list of identical element and allocator types.
591 * The newly-created %list contains the exact contents of @a __x.
592 * The contents of @a __x are a valid, but unspecified %list.
594 list(list
&& __x
) noexcept
595 : _Base(std::move(__x
)) { }
598 * @brief Builds a %list from an initializer_list
599 * @param __l An initializer_list of value_type.
600 * @param __a An allocator object.
602 * Create a %list consisting of copies of the elements in the
603 * initializer_list @a __l. This is linear in __l.size().
605 list(initializer_list
<value_type
> __l
,
606 const allocator_type
& __a
= allocator_type())
607 : _Base(_Node_alloc_type(__a
))
608 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
612 * @brief Builds a %list from a range.
613 * @param __first An input iterator.
614 * @param __last An input iterator.
615 * @param __a An allocator object.
617 * Create a %list consisting of copies of the elements from
618 * [@a __first,@a __last). This is linear in N (where N is
619 * distance(@a __first,@a __last)).
621 #if __cplusplus >= 201103L
622 template<typename _InputIterator
,
623 typename
= std::_RequireInputIter
<_InputIterator
>>
624 list(_InputIterator __first
, _InputIterator __last
,
625 const allocator_type
& __a
= allocator_type())
626 : _Base(_Node_alloc_type(__a
))
627 { _M_initialize_dispatch(__first
, __last
, __false_type()); }
629 template<typename _InputIterator
>
630 list(_InputIterator __first
, _InputIterator __last
,
631 const allocator_type
& __a
= allocator_type())
632 : _Base(_Node_alloc_type(__a
))
634 // Check whether it's an integral type. If so, it's not an iterator.
635 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
636 _M_initialize_dispatch(__first
, __last
, _Integral());
641 * No explicit dtor needed as the _Base dtor takes care of
642 * things. The _Base dtor only erases the elements, and note
643 * that if the elements themselves are pointers, the pointed-to
644 * memory is not touched in any way. Managing the pointer is
645 * the user's responsibility.
649 * @brief %List assignment operator.
650 * @param __x A %list of identical element and allocator types.
652 * All the elements of @a __x are copied, but unlike the copy
653 * constructor, the allocator object is not copied.
656 operator=(const list
& __x
);
658 #if __cplusplus >= 201103L
660 * @brief %List move assignment operator.
661 * @param __x A %list of identical element and allocator types.
663 * The contents of @a __x are moved into this %list (without copying).
664 * @a __x is a valid, but unspecified %list
667 operator=(list
&& __x
)
677 * @brief %List initializer list assignment operator.
678 * @param __l An initializer_list of value_type.
680 * Replace the contents of the %list with copies of the elements
681 * in the initializer_list @a __l. This is linear in l.size().
684 operator=(initializer_list
<value_type
> __l
)
686 this->assign(__l
.begin(), __l
.end());
692 * @brief Assigns a given value to a %list.
693 * @param __n Number of elements to be assigned.
694 * @param __val Value to be assigned.
696 * This function fills a %list with @a __n copies of the given
697 * value. Note that the assignment completely changes the %list
698 * and that the resulting %list's size is the same as the number
699 * of elements assigned. Old data may be lost.
702 assign(size_type __n
, const value_type
& __val
)
703 { _M_fill_assign(__n
, __val
); }
706 * @brief Assigns a range to a %list.
707 * @param __first An input iterator.
708 * @param __last An input iterator.
710 * This function fills a %list with copies of the elements in the
711 * range [@a __first,@a __last).
713 * Note that the assignment completely changes the %list and
714 * that the resulting %list's size is the same as the number of
715 * elements assigned. Old data may be lost.
717 #if __cplusplus >= 201103L
718 template<typename _InputIterator
,
719 typename
= std::_RequireInputIter
<_InputIterator
>>
721 assign(_InputIterator __first
, _InputIterator __last
)
722 { _M_assign_dispatch(__first
, __last
, __false_type()); }
724 template<typename _InputIterator
>
726 assign(_InputIterator __first
, _InputIterator __last
)
728 // Check whether it's an integral type. If so, it's not an iterator.
729 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
730 _M_assign_dispatch(__first
, __last
, _Integral());
734 #if __cplusplus >= 201103L
736 * @brief Assigns an initializer_list to a %list.
737 * @param __l An initializer_list of value_type.
739 * Replace the contents of the %list with copies of the elements
740 * in the initializer_list @a __l. This is linear in __l.size().
743 assign(initializer_list
<value_type
> __l
)
744 { this->assign(__l
.begin(), __l
.end()); }
747 /// Get a copy of the memory allocation object.
749 get_allocator() const _GLIBCXX_NOEXCEPT
750 { return _Base::get_allocator(); }
754 * Returns a read/write iterator that points to the first element in the
755 * %list. Iteration is done in ordinary element order.
758 begin() _GLIBCXX_NOEXCEPT
759 { return iterator(this->_M_impl
._M_node
._M_next
); }
762 * Returns a read-only (constant) iterator that points to the
763 * first element in the %list. Iteration is done in ordinary
767 begin() const _GLIBCXX_NOEXCEPT
768 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
771 * Returns a read/write iterator that points one past the last
772 * element in the %list. Iteration is done in ordinary element
776 end() _GLIBCXX_NOEXCEPT
777 { return iterator(&this->_M_impl
._M_node
); }
780 * Returns a read-only (constant) iterator that points one past
781 * the last element in the %list. Iteration is done in ordinary
785 end() const _GLIBCXX_NOEXCEPT
786 { return const_iterator(&this->_M_impl
._M_node
); }
789 * Returns a read/write reverse iterator that points to the last
790 * element in the %list. Iteration is done in reverse element
794 rbegin() _GLIBCXX_NOEXCEPT
795 { return reverse_iterator(end()); }
798 * Returns a read-only (constant) reverse iterator that points to
799 * the last element in the %list. Iteration is done in reverse
802 const_reverse_iterator
803 rbegin() const _GLIBCXX_NOEXCEPT
804 { return const_reverse_iterator(end()); }
807 * Returns a read/write reverse iterator that points to one
808 * before the first element in the %list. Iteration is done in
809 * reverse element order.
812 rend() _GLIBCXX_NOEXCEPT
813 { return reverse_iterator(begin()); }
816 * Returns a read-only (constant) reverse iterator that points to one
817 * before the first element in the %list. Iteration is done in reverse
820 const_reverse_iterator
821 rend() const _GLIBCXX_NOEXCEPT
822 { return const_reverse_iterator(begin()); }
824 #if __cplusplus >= 201103L
826 * Returns a read-only (constant) iterator that points to the
827 * first element in the %list. Iteration is done in ordinary
831 cbegin() const noexcept
832 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
835 * Returns a read-only (constant) iterator that points one past
836 * the last element in the %list. Iteration is done in ordinary
840 cend() const noexcept
841 { return const_iterator(&this->_M_impl
._M_node
); }
844 * Returns a read-only (constant) reverse iterator that points to
845 * the last element in the %list. Iteration is done in reverse
848 const_reverse_iterator
849 crbegin() const noexcept
850 { return const_reverse_iterator(end()); }
853 * Returns a read-only (constant) reverse iterator that points to one
854 * before the first element in the %list. Iteration is done in reverse
857 const_reverse_iterator
858 crend() const noexcept
859 { return const_reverse_iterator(begin()); }
862 // [23.2.2.2] capacity
864 * Returns true if the %list is empty. (Thus begin() would equal
868 empty() const _GLIBCXX_NOEXCEPT
869 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
871 /** Returns the number of elements in the %list. */
873 size() const _GLIBCXX_NOEXCEPT
874 { return std::distance(begin(), end()); }
876 /** Returns the size() of the largest possible %list. */
878 max_size() const _GLIBCXX_NOEXCEPT
879 { return _M_get_Node_allocator().max_size(); }
881 #if __cplusplus >= 201103L
883 * @brief Resizes the %list to the specified number of elements.
884 * @param __new_size Number of elements the %list should contain.
886 * This function will %resize the %list to the specified number
887 * of elements. If the number is smaller than the %list's
888 * current size the %list is truncated, otherwise default
889 * constructed elements are appended.
892 resize(size_type __new_size
);
895 * @brief Resizes the %list to the specified number of elements.
896 * @param __new_size Number of elements the %list should contain.
897 * @param __x Data with which new elements should be populated.
899 * This function will %resize the %list to the specified number
900 * of elements. If the number is smaller than the %list's
901 * current size the %list is truncated, otherwise the %list is
902 * extended and new elements are populated with given data.
905 resize(size_type __new_size
, const value_type
& __x
);
908 * @brief Resizes the %list to the specified number of elements.
909 * @param __new_size Number of elements the %list should contain.
910 * @param __x Data with which new elements should be populated.
912 * This function will %resize the %list to the specified number
913 * of elements. If the number is smaller than the %list's
914 * current size the %list is truncated, otherwise the %list is
915 * extended and new elements are populated with given data.
918 resize(size_type __new_size
, value_type __x
= value_type());
923 * Returns a read/write reference to the data at the first
924 * element of the %list.
931 * Returns a read-only (constant) reference to the data at the first
932 * element of the %list.
939 * Returns a read/write reference to the data at the last element
945 iterator __tmp
= end();
951 * Returns a read-only (constant) reference to the data at the last
952 * element of the %list.
957 const_iterator __tmp
= end();
962 // [23.2.2.3] modifiers
964 * @brief Add data to the front of the %list.
965 * @param __x Data to be added.
967 * This is a typical stack operation. The function creates an
968 * element at the front of the %list and assigns the given data
969 * to it. Due to the nature of a %list this operation can be
970 * done in constant time, and does not invalidate iterators and
974 push_front(const value_type
& __x
)
975 { this->_M_insert(begin(), __x
); }
977 #if __cplusplus >= 201103L
979 push_front(value_type
&& __x
)
980 { this->_M_insert(begin(), std::move(__x
)); }
982 template<typename
... _Args
>
984 emplace_front(_Args
&&... __args
)
985 { this->_M_insert(begin(), std::forward
<_Args
>(__args
)...); }
989 * @brief Removes first element.
991 * This is a typical stack operation. It shrinks the %list by
992 * one. Due to the nature of a %list this operation can be done
993 * in constant time, and only invalidates iterators/references to
994 * the element being removed.
996 * Note that no data is returned, and if the first element's data
997 * is needed, it should be retrieved before pop_front() is
1002 { this->_M_erase(begin()); }
1005 * @brief Add data to the end of the %list.
1006 * @param __x Data to be added.
1008 * This is a typical stack operation. The function creates an
1009 * element at the end of the %list and assigns the given data to
1010 * it. Due to the nature of a %list this operation can be done
1011 * in constant time, and does not invalidate iterators and
1015 push_back(const value_type
& __x
)
1016 { this->_M_insert(end(), __x
); }
1018 #if __cplusplus >= 201103L
1020 push_back(value_type
&& __x
)
1021 { this->_M_insert(end(), std::move(__x
)); }
1023 template<typename
... _Args
>
1025 emplace_back(_Args
&&... __args
)
1026 { this->_M_insert(end(), std::forward
<_Args
>(__args
)...); }
1030 * @brief Removes last element.
1032 * This is a typical stack operation. It shrinks the %list by
1033 * one. Due to the nature of a %list this operation can be done
1034 * in constant time, and only invalidates iterators/references to
1035 * the element being removed.
1037 * Note that no data is returned, and if the last element's data
1038 * is needed, it should be retrieved before pop_back() is called.
1042 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1044 #if __cplusplus >= 201103L
1046 * @brief Constructs object in %list before specified iterator.
1047 * @param __position A const_iterator into the %list.
1048 * @param __args Arguments.
1049 * @return An iterator that points to the inserted data.
1051 * This function will insert an object of type T constructed
1052 * with T(std::forward<Args>(args)...) before the specified
1053 * location. Due to the nature of a %list this operation can
1054 * be done in constant time, and does not invalidate iterators
1057 template<typename
... _Args
>
1059 emplace(iterator __position
, _Args
&&... __args
);
1063 * @brief Inserts given value into %list before specified iterator.
1064 * @param __position An iterator into the %list.
1065 * @param __x Data to be inserted.
1066 * @return An iterator that points to the inserted data.
1068 * This function will insert a copy of the given value before
1069 * the specified location. Due to the nature of a %list this
1070 * operation can be done in constant time, and does not
1071 * invalidate iterators and references.
1074 insert(iterator __position
, const value_type
& __x
);
1076 #if __cplusplus >= 201103L
1078 * @brief Inserts given rvalue into %list before specified iterator.
1079 * @param __position An iterator into the %list.
1080 * @param __x Data to be inserted.
1081 * @return An iterator that points to the inserted data.
1083 * This function will insert a copy of the given rvalue before
1084 * the specified location. Due to the nature of a %list this
1085 * operation can be done in constant time, and does not
1086 * invalidate iterators and references.
1089 insert(iterator __position
, value_type
&& __x
)
1090 { return emplace(__position
, std::move(__x
)); }
1093 * @brief Inserts the contents of an initializer_list into %list
1094 * before specified iterator.
1095 * @param __p An iterator into the %list.
1096 * @param __l An initializer_list of value_type.
1098 * This function will insert copies of the data in the
1099 * initializer_list @a l into the %list before the location
1100 * specified by @a p.
1102 * This operation is linear in the number of elements inserted and
1103 * does not invalidate iterators and references.
1106 insert(iterator __p
, initializer_list
<value_type
> __l
)
1107 { this->insert(__p
, __l
.begin(), __l
.end()); }
1111 * @brief Inserts a number of copies of given data into the %list.
1112 * @param __position An iterator into the %list.
1113 * @param __n Number of elements to be inserted.
1114 * @param __x Data to be inserted.
1116 * This function will insert a specified number of copies of the
1117 * given data before the location specified by @a position.
1119 * This operation is linear in the number of elements inserted and
1120 * does not invalidate iterators and references.
1123 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1125 list
__tmp(__n
, __x
, get_allocator());
1126 splice(__position
, __tmp
);
1130 * @brief Inserts a range into the %list.
1131 * @param __position An iterator into the %list.
1132 * @param __first An input iterator.
1133 * @param __last An input iterator.
1135 * This function will insert copies of the data in the range [@a
1136 * first,@a last) into the %list before the location specified by
1139 * This operation is linear in the number of elements inserted and
1140 * does not invalidate iterators and references.
1142 #if __cplusplus >= 201103L
1143 template<typename _InputIterator
,
1144 typename
= std::_RequireInputIter
<_InputIterator
>>
1146 template<typename _InputIterator
>
1149 insert(iterator __position
, _InputIterator __first
,
1150 _InputIterator __last
)
1152 list
__tmp(__first
, __last
, get_allocator());
1153 splice(__position
, __tmp
);
1157 * @brief Remove element at given position.
1158 * @param __position Iterator pointing to element to be erased.
1159 * @return An iterator pointing to the next element (or end()).
1161 * This function will erase the element at the given position and thus
1162 * shorten the %list by one.
1164 * Due to the nature of a %list this operation can be done in
1165 * constant time, and only invalidates iterators/references to
1166 * the element being removed. The user is also cautioned that
1167 * this function only erases the element, and that if the element
1168 * is itself a pointer, the pointed-to memory is not touched in
1169 * any way. Managing the pointer is the user's responsibility.
1172 erase(iterator __position
);
1175 * @brief Remove a range of elements.
1176 * @param __first Iterator pointing to the first element to be erased.
1177 * @param __last Iterator pointing to one past the last element to be
1179 * @return An iterator pointing to the element pointed to by @a last
1180 * prior to erasing (or end()).
1182 * This function will erase the elements in the range @a
1183 * [first,last) and shorten the %list accordingly.
1185 * This operation is linear time in the size of the range and only
1186 * invalidates iterators/references to the element being removed.
1187 * The user is also cautioned that this function only erases the
1188 * elements, and that if the elements themselves are pointers, the
1189 * pointed-to memory is not touched in any way. Managing the pointer
1190 * is the user's responsibility.
1193 erase(iterator __first
, iterator __last
)
1195 while (__first
!= __last
)
1196 __first
= erase(__first
);
1201 * @brief Swaps data with another %list.
1202 * @param __x A %list of the same element and allocator types.
1204 * This exchanges the elements between two lists in constant
1205 * time. Note that the global std::swap() function is
1206 * specialized such that std::swap(l1,l2) will feed to this
1212 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1213 __x
._M_impl
._M_node
);
1215 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1216 // 431. Swapping containers with unequal allocators.
1217 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
1218 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
1222 * Erases all the elements. Note that this function only erases
1223 * the elements, and that if the elements themselves are
1224 * pointers, the pointed-to memory is not touched in any way.
1225 * Managing the pointer is the user's responsibility.
1228 clear() _GLIBCXX_NOEXCEPT
1234 // [23.2.2.4] list operations
1236 * @brief Insert contents of another %list.
1237 * @param __position Iterator referencing the element to insert before.
1238 * @param __x Source list.
1240 * The elements of @a __x are inserted in constant time in front of
1241 * the element referenced by @a __position. @a __x becomes an empty
1244 * Requires this != @a __x.
1247 #if __cplusplus >= 201103L
1248 splice(iterator __position
, list
&& __x
)
1250 splice(iterator __position
, list
& __x
)
1255 _M_check_equal_allocators(__x
);
1257 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1261 #if __cplusplus >= 201103L
1263 splice(iterator __position
, list
& __x
)
1264 { splice(__position
, std::move(__x
)); }
1268 * @brief Insert element from another %list.
1269 * @param __position Iterator referencing the element to insert before.
1270 * @param __x Source list.
1271 * @param __i Iterator referencing the element to move.
1273 * Removes the element in list @a __x referenced by @a __i and
1274 * inserts it into the current list before @a __position.
1277 #if __cplusplus >= 201103L
1278 splice(iterator __position
, list
&& __x
, iterator __i
)
1280 splice(iterator __position
, list
& __x
, iterator __i
)
1285 if (__position
== __i
|| __position
== __j
)
1289 _M_check_equal_allocators(__x
);
1291 this->_M_transfer(__position
, __i
, __j
);
1294 #if __cplusplus >= 201103L
1296 splice(iterator __position
, list
& __x
, iterator __i
)
1297 { splice(__position
, std::move(__x
), __i
); }
1301 * @brief Insert range from another %list.
1302 * @param __position Iterator referencing the element to insert before.
1303 * @param __x Source list.
1304 * @param __first Iterator referencing the start of range in x.
1305 * @param __last Iterator referencing the end of range in x.
1307 * Removes elements in the range [__first,__last) and inserts them
1308 * before @a __position in constant time.
1310 * Undefined if @a __position is in [__first,__last).
1313 #if __cplusplus >= 201103L
1314 splice(iterator __position
, list
&& __x
, iterator __first
,
1317 splice(iterator __position
, list
& __x
, iterator __first
,
1321 if (__first
!= __last
)
1324 _M_check_equal_allocators(__x
);
1326 this->_M_transfer(__position
, __first
, __last
);
1330 #if __cplusplus >= 201103L
1332 splice(iterator __position
, list
& __x
, iterator __first
, iterator __last
)
1333 { splice(__position
, std::move(__x
), __first
, __last
); }
1337 * @brief Remove all elements equal to value.
1338 * @param __value The value to remove.
1340 * Removes every element in the list equal to @a value.
1341 * Remaining elements stay in list order. Note that this
1342 * function only erases the elements, and that if the elements
1343 * themselves are pointers, the pointed-to memory is not
1344 * touched in any way. Managing the pointer is the user's
1348 remove(const _Tp
& __value
);
1351 * @brief Remove all elements satisfying a predicate.
1352 * @tparam _Predicate Unary predicate function or object.
1354 * Removes every element in the list for which the predicate
1355 * returns true. Remaining elements stay in list order. Note
1356 * that this function only erases the elements, and that if the
1357 * elements themselves are pointers, the pointed-to memory is
1358 * not touched in any way. Managing the pointer is the user's
1361 template<typename _Predicate
>
1363 remove_if(_Predicate
);
1366 * @brief Remove consecutive duplicate elements.
1368 * For each consecutive set of elements with the same value,
1369 * remove all but the first one. Remaining elements stay in
1370 * list order. Note that this function only erases the
1371 * elements, and that if the elements themselves are pointers,
1372 * the pointed-to memory is not touched in any way. Managing
1373 * the pointer is the user's responsibility.
1379 * @brief Remove consecutive elements satisfying a predicate.
1380 * @tparam _BinaryPredicate Binary predicate function or object.
1382 * For each consecutive set of elements [first,last) that
1383 * satisfy predicate(first,i) where i is an iterator in
1384 * [first,last), remove all but the first one. Remaining
1385 * elements stay in list order. Note that this function only
1386 * erases the elements, and that if the elements themselves are
1387 * pointers, the pointed-to memory is not touched in any way.
1388 * Managing the pointer is the user's responsibility.
1390 template<typename _BinaryPredicate
>
1392 unique(_BinaryPredicate
);
1395 * @brief Merge sorted lists.
1396 * @param __x Sorted list to merge.
1398 * Assumes that both @a __x and this list are sorted according to
1399 * operator<(). Merges elements of @a __x into this list in
1400 * sorted order, leaving @a __x empty when complete. Elements in
1401 * this list precede elements in @a __x that are equal.
1403 #if __cplusplus >= 201103L
1409 { merge(std::move(__x
)); }
1416 * @brief Merge sorted lists according to comparison function.
1417 * @tparam _StrictWeakOrdering Comparison function defining
1419 * @param __x Sorted list to merge.
1420 * @param __comp Comparison functor.
1422 * Assumes that both @a __x and this list are sorted according to
1423 * StrictWeakOrdering. Merges elements of @a __x into this list
1424 * in sorted order, leaving @a __x empty when complete. Elements
1425 * in this list precede elements in @a __x that are equivalent
1426 * according to StrictWeakOrdering().
1428 #if __cplusplus >= 201103L
1429 template<typename _StrictWeakOrdering
>
1431 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1433 template<typename _StrictWeakOrdering
>
1435 merge(list
& __x
, _StrictWeakOrdering __comp
)
1436 { merge(std::move(__x
), __comp
); }
1438 template<typename _StrictWeakOrdering
>
1440 merge(list
& __x
, _StrictWeakOrdering __comp
);
1444 * @brief Reverse the elements in list.
1446 * Reverse the order of elements in the list in linear time.
1449 reverse() _GLIBCXX_NOEXCEPT
1450 { this->_M_impl
._M_node
._M_reverse(); }
1453 * @brief Sort the elements.
1455 * Sorts the elements of this list in NlogN time. Equivalent
1456 * elements remain in list order.
1462 * @brief Sort the elements according to comparison function.
1464 * Sorts the elements of this list in NlogN time. Equivalent
1465 * elements remain in list order.
1467 template<typename _StrictWeakOrdering
>
1469 sort(_StrictWeakOrdering
);
1472 // Internal constructor functions follow.
1474 // Called by the range constructor to implement [23.1.1]/9
1476 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1477 // 438. Ambiguity in the "do the right thing" clause
1478 template<typename _Integer
>
1480 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1481 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1483 // Called by the range constructor to implement [23.1.1]/9
1484 template<typename _InputIterator
>
1486 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1489 for (; __first
!= __last
; ++__first
)
1490 #if __cplusplus >= 201103L
1491 emplace_back(*__first
);
1493 push_back(*__first
);
1497 // Called by list(n,v,a), and the range constructor when it turns out
1498 // to be the same thing.
1500 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1506 #if __cplusplus >= 201103L
1507 // Called by list(n).
1509 _M_default_initialize(size_type __n
)
1515 // Called by resize(sz).
1517 _M_default_append(size_type __n
);
1520 // Internal assign functions follow.
1522 // Called by the range assign to implement [23.1.1]/9
1524 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1525 // 438. Ambiguity in the "do the right thing" clause
1526 template<typename _Integer
>
1528 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1529 { _M_fill_assign(__n
, __val
); }
1531 // Called by the range assign to implement [23.1.1]/9
1532 template<typename _InputIterator
>
1534 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1537 // Called by assign(n,t), and the range assign when it turns out
1538 // to be the same thing.
1540 _M_fill_assign(size_type __n
, const value_type
& __val
);
1543 // Moves the elements from [first,last) before position.
1545 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1546 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1548 // Inserts new element at position given and with value given.
1549 #if __cplusplus < 201103L
1551 _M_insert(iterator __position
, const value_type
& __x
)
1553 _Node
* __tmp
= _M_create_node(__x
);
1554 __tmp
->_M_hook(__position
._M_node
);
1557 template<typename
... _Args
>
1559 _M_insert(iterator __position
, _Args
&&... __args
)
1561 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1562 __tmp
->_M_hook(__position
._M_node
);
1566 // Erases element at position given.
1568 _M_erase(iterator __position
)
1570 __position
._M_node
->_M_unhook();
1571 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1572 #if __cplusplus >= 201103L
1573 _M_get_Node_allocator().destroy(__n
);
1575 _M_get_Tp_allocator().destroy(std::__addressof(__n
->_M_data
));
1580 // To implement the splice (and merge) bits of N1599.
1582 _M_check_equal_allocators(list
& __x
)
1584 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1585 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1586 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1591 * @brief List equality comparison.
1592 * @param __x A %list.
1593 * @param __y A %list of the same type as @a __x.
1594 * @return True iff the size and elements of the lists are equal.
1596 * This is an equivalence relation. It is linear in the size of
1597 * the lists. Lists are considered equivalent if their sizes are
1598 * equal, and if corresponding elements compare equal.
1600 template<typename _Tp
, typename _Alloc
>
1602 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1604 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1605 const_iterator __end1
= __x
.end();
1606 const_iterator __end2
= __y
.end();
1608 const_iterator __i1
= __x
.begin();
1609 const_iterator __i2
= __y
.begin();
1610 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1615 return __i1
== __end1
&& __i2
== __end2
;
1619 * @brief List ordering relation.
1620 * @param __x A %list.
1621 * @param __y A %list of the same type as @a __x.
1622 * @return True iff @a __x is lexicographically less than @a __y.
1624 * This is a total ordering relation. It is linear in the size of the
1625 * lists. The elements must be comparable with @c <.
1627 * See std::lexicographical_compare() for how the determination is made.
1629 template<typename _Tp
, typename _Alloc
>
1631 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1632 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1633 __y
.begin(), __y
.end()); }
1635 /// Based on operator==
1636 template<typename _Tp
, typename _Alloc
>
1638 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1639 { return !(__x
== __y
); }
1641 /// Based on operator<
1642 template<typename _Tp
, typename _Alloc
>
1644 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1645 { return __y
< __x
; }
1647 /// Based on operator<
1648 template<typename _Tp
, typename _Alloc
>
1650 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1651 { return !(__y
< __x
); }
1653 /// Based on operator<
1654 template<typename _Tp
, typename _Alloc
>
1656 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1657 { return !(__x
< __y
); }
1659 /// See std::list::swap().
1660 template<typename _Tp
, typename _Alloc
>
1662 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1665 _GLIBCXX_END_NAMESPACE_CONTAINER
1668 #endif /* _STL_LIST_H */