1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file bits/stl_list.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{list}
60 #include <bits/concept_check.h>
61 #ifdef __GXX_EXPERIMENTAL_CXX0X__
62 #include <initializer_list>
65 namespace std
_GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 // Supporting structures are split into common and templated
72 // types; the latter publicly inherits from the former in an
73 // effort to reduce code duplication. This results in some
74 // "needless" static_cast'ing later on, but it's all safe
77 /// Common part of a node in the %list.
78 struct _List_node_base
80 _List_node_base
* _M_next
;
81 _List_node_base
* _M_prev
;
84 swap(_List_node_base
& __x
, _List_node_base
& __y
) _GLIBCXX_USE_NOEXCEPT
;
87 _M_transfer(_List_node_base
* const __first
,
88 _List_node_base
* const __last
) _GLIBCXX_USE_NOEXCEPT
;
91 _M_reverse() _GLIBCXX_USE_NOEXCEPT
;
94 _M_hook(_List_node_base
* const __position
) _GLIBCXX_USE_NOEXCEPT
;
97 _M_unhook() _GLIBCXX_USE_NOEXCEPT
;
100 _GLIBCXX_END_NAMESPACE_VERSION
101 } // namespace detail
103 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
105 /// An actual node in the %list.
106 template<typename _Tp
>
107 struct _List_node
: public __detail::_List_node_base
112 #ifdef __GXX_EXPERIMENTAL_CXX0X__
113 template<typename
... _Args
>
114 _List_node(_Args
&&... __args
)
115 : __detail::_List_node_base(), _M_data(std::forward
<_Args
>(__args
)...)
121 * @brief A list::iterator.
123 * All the functions are op overloads.
125 template<typename _Tp
>
126 struct _List_iterator
128 typedef _List_iterator
<_Tp
> _Self
;
129 typedef _List_node
<_Tp
> _Node
;
131 typedef ptrdiff_t difference_type
;
132 typedef std::bidirectional_iterator_tag iterator_category
;
133 typedef _Tp value_type
;
134 typedef _Tp
* pointer
;
135 typedef _Tp
& reference
;
141 _List_iterator(__detail::_List_node_base
* __x
)
144 // Must downcast from _List_node_base to _List_node to get to _M_data.
147 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
151 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
156 _M_node
= _M_node
->_M_next
;
164 _M_node
= _M_node
->_M_next
;
171 _M_node
= _M_node
->_M_prev
;
179 _M_node
= _M_node
->_M_prev
;
184 operator==(const _Self
& __x
) const
185 { return _M_node
== __x
._M_node
; }
188 operator!=(const _Self
& __x
) const
189 { return _M_node
!= __x
._M_node
; }
191 // The only member points to the %list element.
192 __detail::_List_node_base
* _M_node
;
196 * @brief A list::const_iterator.
198 * All the functions are op overloads.
200 template<typename _Tp
>
201 struct _List_const_iterator
203 typedef _List_const_iterator
<_Tp
> _Self
;
204 typedef const _List_node
<_Tp
> _Node
;
205 typedef _List_iterator
<_Tp
> iterator
;
207 typedef ptrdiff_t difference_type
;
208 typedef std::bidirectional_iterator_tag iterator_category
;
209 typedef _Tp value_type
;
210 typedef const _Tp
* pointer
;
211 typedef const _Tp
& reference
;
213 _List_const_iterator()
217 _List_const_iterator(const __detail::_List_node_base
* __x
)
220 _List_const_iterator(const iterator
& __x
)
221 : _M_node(__x
._M_node
) { }
223 // Must downcast from List_node_base to _List_node to get to
227 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
231 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
236 _M_node
= _M_node
->_M_next
;
244 _M_node
= _M_node
->_M_next
;
251 _M_node
= _M_node
->_M_prev
;
259 _M_node
= _M_node
->_M_prev
;
264 operator==(const _Self
& __x
) const
265 { return _M_node
== __x
._M_node
; }
268 operator!=(const _Self
& __x
) const
269 { return _M_node
!= __x
._M_node
; }
271 // The only member points to the %list element.
272 const __detail::_List_node_base
* _M_node
;
275 template<typename _Val
>
277 operator==(const _List_iterator
<_Val
>& __x
,
278 const _List_const_iterator
<_Val
>& __y
)
279 { return __x
._M_node
== __y
._M_node
; }
281 template<typename _Val
>
283 operator!=(const _List_iterator
<_Val
>& __x
,
284 const _List_const_iterator
<_Val
>& __y
)
285 { return __x
._M_node
!= __y
._M_node
; }
288 /// See bits/stl_deque.h's _Deque_base for an explanation.
289 template<typename _Tp
, typename _Alloc
>
294 // The stored instance is not actually of "allocator_type"'s
295 // type. Instead we rebind the type to
296 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
297 // should probably be the same. List_node<Tp> is not the same
298 // size as Tp (it's two pointers larger), and specializations on
299 // Tp may go unused because List_node<Tp> is being bound
302 // We put this to the test in the constructors and in
303 // get_allocator, where we use conversions between
304 // allocator_type and _Node_alloc_type. The conversion is
305 // required by table 32 in [20.1.5].
306 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
309 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
312 : public _Node_alloc_type
314 __detail::_List_node_base _M_node
;
316 #ifdef __GXX_EXPERIMENTAL_CXX0X__
321 : _Node_alloc_type(), _M_node()
322 #ifdef __GXX_EXPERIMENTAL_CXX0X__
327 _List_impl(const _Node_alloc_type
& __a
)
328 : _Node_alloc_type(__a
), _M_node()
329 #ifdef __GXX_EXPERIMENTAL_CXX0X__
334 #ifdef __GXX_EXPERIMENTAL_CXX0X__
335 _List_impl(_Node_alloc_type
&& __a
)
336 : _Node_alloc_type(std::move(__a
)), _M_node(), _M_size(0)
346 _List_node
<_Tp
>* __tmp
= _M_impl
._Node_alloc_type::allocate(1);
347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
354 _M_put_node(_List_node
<_Tp
>* __p
)
356 _M_impl
._Node_alloc_type::deallocate(__p
, 1);
357 #ifdef __GXX_EXPERIMENTAL_CXX0X__
363 typedef _Alloc allocator_type
;
366 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
367 { return *static_cast<_Node_alloc_type
*>(&_M_impl
); }
369 const _Node_alloc_type
&
370 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
371 { return *static_cast<const _Node_alloc_type
*>(&_M_impl
); }
374 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
375 { return _Tp_alloc_type(_M_get_Node_allocator()); }
378 get_allocator() const _GLIBCXX_NOEXCEPT
379 { return allocator_type(_M_get_Node_allocator()); }
385 _List_base(const _Node_alloc_type
& __a
)
389 #ifdef __GXX_EXPERIMENTAL_CXX0X__
390 _List_base(_List_base
&& __x
)
391 : _M_impl(std::move(__x
._M_get_Node_allocator()))
394 __detail::_List_node_base::swap(_M_impl
._M_node
, __x
._M_impl
._M_node
);
395 std::swap(_M_impl
._M_size
, __x
._M_impl
._M_size
);
399 // This is what actually destroys the list.
400 ~_List_base() _GLIBCXX_NOEXCEPT
409 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
410 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
415 * @brief A standard container with linear time access to elements,
416 * and fixed time insertion/deletion at any point in the sequence.
420 * Meets the requirements of a <a href="tables.html#65">container</a>, a
421 * <a href="tables.html#66">reversible container</a>, and a
422 * <a href="tables.html#67">sequence</a>, including the
423 * <a href="tables.html#68">optional sequence requirements</a> with the
424 * %exception of @c at and @c operator[].
426 * This is a @e doubly @e linked %list. Traversal up and down the
427 * %list requires linear time, but adding and removing elements (or
428 * @e nodes) is done in constant time, regardless of where the
429 * change takes place. Unlike std::vector and std::deque,
430 * random-access iterators are not provided, so subscripting ( @c
431 * [] ) access is not allowed. For algorithms which only need
432 * sequential access, this lack makes no difference.
434 * Also unlike the other standard containers, std::list provides
435 * specialized algorithms %unique to linked lists, such as
436 * splicing, sorting, and in-place reversal.
438 * A couple points on memory allocation for list<Tp>:
440 * First, we never actually allocate a Tp, we allocate
441 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
442 * that after elements from %list<X,Alloc1> are spliced into
443 * %list<X,Alloc2>, destroying the memory of the second %list is a
444 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
446 * Second, a %list conceptually represented as
448 * A <---> B <---> C <---> D
450 * is actually circular; a link exists between A and D. The %list
451 * class holds (as its only data member) a private list::iterator
452 * pointing to @e D, not to @e A! To get to the head of the %list,
453 * we start at the tail and move forward by one. When this member
454 * iterator's next/previous pointers refer to itself, the %list is
457 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
458 class list
: protected _List_base
<_Tp
, _Alloc
>
460 // concept requirements
461 typedef typename
_Alloc::value_type _Alloc_value_type
;
462 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
463 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
465 typedef _List_base
<_Tp
, _Alloc
> _Base
;
466 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
467 typedef typename
_Base::_Node_alloc_type _Node_alloc_type
;
470 typedef _Tp value_type
;
471 typedef typename
_Tp_alloc_type::pointer pointer
;
472 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
473 typedef typename
_Tp_alloc_type::reference reference
;
474 typedef typename
_Tp_alloc_type::const_reference const_reference
;
475 typedef _List_iterator
<_Tp
> iterator
;
476 typedef _List_const_iterator
<_Tp
> const_iterator
;
477 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
478 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
479 typedef size_t size_type
;
480 typedef ptrdiff_t difference_type
;
481 typedef _Alloc allocator_type
;
484 // Note that pointers-to-_Node's can be ctor-converted to
486 typedef _List_node
<_Tp
> _Node
;
488 using _Base::_M_impl
;
489 using _Base::_M_put_node
;
490 using _Base::_M_get_node
;
491 using _Base::_M_get_Tp_allocator
;
492 using _Base::_M_get_Node_allocator
;
495 * @param __args An instance of user data.
497 * Allocates space for a new node and constructs a copy of
500 #ifndef __GXX_EXPERIMENTAL_CXX0X__
502 _M_create_node(const value_type
& __x
)
504 _Node
* __p
= this->_M_get_node();
507 _M_get_Tp_allocator().construct
508 (std::__addressof(__p
->_M_data
), __x
);
513 __throw_exception_again
;
518 template<typename
... _Args
>
520 _M_create_node(_Args
&&... __args
)
522 _Node
* __p
= this->_M_get_node();
525 _M_get_Node_allocator().construct(__p
,
526 std::forward
<_Args
>(__args
)...);
531 __throw_exception_again
;
538 // [23.2.2.1] construct/copy/destroy
539 // (assign() and get_allocator() are also listed in this section)
541 * @brief Default constructor creates no elements.
547 * @brief Creates a %list with no elements.
548 * @param __a An allocator object.
551 list(const allocator_type
& __a
)
552 : _Base(_Node_alloc_type(__a
)) { }
554 #ifdef __GXX_EXPERIMENTAL_CXX0X__
556 * @brief Creates a %list with default constructed elements.
557 * @param __n The number of elements to initially create.
559 * This constructor fills the %list with @a __n default
560 * constructed elements.
565 { _M_default_initialize(__n
); }
568 * @brief Creates a %list with copies of an exemplar element.
569 * @param __n The number of elements to initially create.
570 * @param __value An element to copy.
571 * @param __a An allocator object.
573 * This constructor fills the %list with @a __n copies of @a __value.
575 list(size_type __n
, const value_type
& __value
,
576 const allocator_type
& __a
= allocator_type())
577 : _Base(_Node_alloc_type(__a
))
578 { _M_fill_initialize(__n
, __value
); }
581 * @brief Creates a %list with copies of an exemplar element.
582 * @param __n The number of elements to initially create.
583 * @param __value An element to copy.
584 * @param __a An allocator object.
586 * This constructor fills the %list with @a __n copies of @a __value.
589 list(size_type __n
, const value_type
& __value
= value_type(),
590 const allocator_type
& __a
= allocator_type())
591 : _Base(_Node_alloc_type(__a
))
592 { _M_fill_initialize(__n
, __value
); }
596 * @brief %List copy constructor.
597 * @param __x A %list of identical element and allocator types.
599 * The newly-created %list uses a copy of the allocation object used
602 list(const list
& __x
)
603 : _Base(__x
._M_get_Node_allocator())
604 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
606 #ifdef __GXX_EXPERIMENTAL_CXX0X__
608 * @brief %List move constructor.
609 * @param __x A %list of identical element and allocator types.
611 * The newly-created %list contains the exact contents of @a __x.
612 * The contents of @a __x are a valid, but unspecified %list.
614 list(list
&& __x
) noexcept
615 : _Base(std::move(__x
)) { }
618 * @brief Builds a %list from an initializer_list
619 * @param __l An initializer_list of value_type.
620 * @param __a An allocator object.
622 * Create a %list consisting of copies of the elements in the
623 * initializer_list @a __l. This is linear in __l.size().
625 list(initializer_list
<value_type
> __l
,
626 const allocator_type
& __a
= allocator_type())
627 : _Base(_Node_alloc_type(__a
))
628 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
632 * @brief Builds a %list from a range.
633 * @param __first An input iterator.
634 * @param __last An input iterator.
635 * @param __a An allocator object.
637 * Create a %list consisting of copies of the elements from
638 * [@a __first,@a __last). This is linear in N (where N is
639 * distance(@a __first,@a __last)).
641 template<typename _InputIterator
>
642 list(_InputIterator __first
, _InputIterator __last
,
643 const allocator_type
& __a
= allocator_type())
644 : _Base(_Node_alloc_type(__a
))
646 // Check whether it's an integral type. If so, it's not an iterator.
647 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
648 _M_initialize_dispatch(__first
, __last
, _Integral());
652 * No explicit dtor needed as the _Base dtor takes care of
653 * things. The _Base dtor only erases the elements, and note
654 * that if the elements themselves are pointers, the pointed-to
655 * memory is not touched in any way. Managing the pointer is
656 * the user's responsibility.
660 * @brief %List assignment operator.
661 * @param __x A %list of identical element and allocator types.
663 * All the elements of @a __x are copied, but unlike the copy
664 * constructor, the allocator object is not copied.
667 operator=(const list
& __x
);
669 #ifdef __GXX_EXPERIMENTAL_CXX0X__
671 * @brief %List move assignment operator.
672 * @param __x A %list of identical element and allocator types.
674 * The contents of @a __x are moved into this %list (without copying).
675 * @a __x is a valid, but unspecified %list
678 operator=(list
&& __x
)
688 * @brief %List initializer list assignment operator.
689 * @param __l An initializer_list of value_type.
691 * Replace the contents of the %list with copies of the elements
692 * in the initializer_list @a __l. This is linear in l.size().
695 operator=(initializer_list
<value_type
> __l
)
697 this->assign(__l
.begin(), __l
.end());
703 * @brief Assigns a given value to a %list.
704 * @param __n Number of elements to be assigned.
705 * @param __val Value to be assigned.
707 * This function fills a %list with @a __n copies of the given
708 * value. Note that the assignment completely changes the %list
709 * and that the resulting %list's size is the same as the number
710 * of elements assigned. Old data may be lost.
713 assign(size_type __n
, const value_type
& __val
)
714 { _M_fill_assign(__n
, __val
); }
717 * @brief Assigns a range to a %list.
718 * @param __first An input iterator.
719 * @param __last An input iterator.
721 * This function fills a %list with copies of the elements in the
722 * range [@a __first,@a __last).
724 * Note that the assignment completely changes the %list and
725 * that the resulting %list's size is the same as the number of
726 * elements assigned. Old data may be lost.
728 template<typename _InputIterator
>
730 assign(_InputIterator __first
, _InputIterator __last
)
732 // Check whether it's an integral type. If so, it's not an iterator.
733 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
734 _M_assign_dispatch(__first
, __last
, _Integral());
737 #ifdef __GXX_EXPERIMENTAL_CXX0X__
739 * @brief Assigns an initializer_list to a %list.
740 * @param __l An initializer_list of value_type.
742 * Replace the contents of the %list with copies of the elements
743 * in the initializer_list @a __l. This is linear in __l.size().
746 assign(initializer_list
<value_type
> __l
)
747 { this->assign(__l
.begin(), __l
.end()); }
750 /// Get a copy of the memory allocation object.
752 get_allocator() const _GLIBCXX_NOEXCEPT
753 { return _Base::get_allocator(); }
757 * Returns a read/write iterator that points to the first element in the
758 * %list. Iteration is done in ordinary element order.
761 begin() _GLIBCXX_NOEXCEPT
762 { return iterator(this->_M_impl
._M_node
._M_next
); }
765 * Returns a read-only (constant) iterator that points to the
766 * first element in the %list. Iteration is done in ordinary
770 begin() const _GLIBCXX_NOEXCEPT
771 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
774 * Returns a read/write iterator that points one past the last
775 * element in the %list. Iteration is done in ordinary element
779 end() _GLIBCXX_NOEXCEPT
780 { return iterator(&this->_M_impl
._M_node
); }
783 * Returns a read-only (constant) iterator that points one past
784 * the last element in the %list. Iteration is done in ordinary
788 end() const _GLIBCXX_NOEXCEPT
789 { return const_iterator(&this->_M_impl
._M_node
); }
792 * Returns a read/write reverse iterator that points to the last
793 * element in the %list. Iteration is done in reverse element
797 rbegin() _GLIBCXX_NOEXCEPT
798 { return reverse_iterator(end()); }
801 * Returns a read-only (constant) reverse iterator that points to
802 * the last element in the %list. Iteration is done in reverse
805 const_reverse_iterator
806 rbegin() const _GLIBCXX_NOEXCEPT
807 { return const_reverse_iterator(end()); }
810 * Returns a read/write reverse iterator that points to one
811 * before the first element in the %list. Iteration is done in
812 * reverse element order.
815 rend() _GLIBCXX_NOEXCEPT
816 { return reverse_iterator(begin()); }
819 * Returns a read-only (constant) reverse iterator that points to one
820 * before the first element in the %list. Iteration is done in reverse
823 const_reverse_iterator
824 rend() const _GLIBCXX_NOEXCEPT
825 { return const_reverse_iterator(begin()); }
827 #ifdef __GXX_EXPERIMENTAL_CXX0X__
829 * Returns a read-only (constant) iterator that points to the
830 * first element in the %list. Iteration is done in ordinary
834 cbegin() const noexcept
835 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
838 * Returns a read-only (constant) iterator that points one past
839 * the last element in the %list. Iteration is done in ordinary
843 cend() const noexcept
844 { return const_iterator(&this->_M_impl
._M_node
); }
847 * Returns a read-only (constant) reverse iterator that points to
848 * the last element in the %list. Iteration is done in reverse
851 const_reverse_iterator
852 crbegin() const noexcept
853 { return const_reverse_iterator(end()); }
856 * Returns a read-only (constant) reverse iterator that points to one
857 * before the first element in the %list. Iteration is done in reverse
860 const_reverse_iterator
861 crend() const noexcept
862 { return const_reverse_iterator(begin()); }
865 // [23.2.2.2] capacity
867 * Returns true if the %list is empty. (Thus begin() would equal
871 empty() const _GLIBCXX_NOEXCEPT
872 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
874 /** Returns the number of elements in the %list. */
876 size() const _GLIBCXX_NOEXCEPT
878 #ifdef __GXX_EXPERIMENTAL_CXX0X__
879 return this->_M_impl
._M_size
;
881 return std::distance(begin(), end());
885 /** Returns the size() of the largest possible %list. */
887 max_size() const _GLIBCXX_NOEXCEPT
888 { return _M_get_Node_allocator().max_size(); }
890 #ifdef __GXX_EXPERIMENTAL_CXX0X__
892 * @brief Resizes the %list to the specified number of elements.
893 * @param __new_size Number of elements the %list should contain.
895 * This function will %resize the %list to the specified number
896 * of elements. If the number is smaller than the %list's
897 * current size the %list is truncated, otherwise default
898 * constructed elements are appended.
901 resize(size_type __new_size
);
904 * @brief Resizes the %list to the specified number of elements.
905 * @param __new_size Number of elements the %list should contain.
906 * @param __x Data with which new elements should be populated.
908 * This function will %resize the %list to the specified number
909 * of elements. If the number is smaller than the %list's
910 * current size the %list is truncated, otherwise the %list is
911 * extended and new elements are populated with given data.
914 resize(size_type __new_size
, const value_type
& __x
);
917 * @brief Resizes the %list to the specified number of elements.
918 * @param __new_size Number of elements the %list should contain.
919 * @param __x Data with which new elements should be populated.
921 * This function will %resize the %list to the specified number
922 * of elements. If the number is smaller than the %list's
923 * current size the %list is truncated, otherwise the %list is
924 * extended and new elements are populated with given data.
927 resize(size_type __new_size
, value_type __x
= value_type());
932 * Returns a read/write reference to the data at the first
933 * element of the %list.
940 * Returns a read-only (constant) reference to the data at the first
941 * element of the %list.
948 * Returns a read/write reference to the data at the last element
954 iterator __tmp
= end();
960 * Returns a read-only (constant) reference to the data at the last
961 * element of the %list.
966 const_iterator __tmp
= end();
971 // [23.2.2.3] modifiers
973 * @brief Add data to the front of the %list.
974 * @param __x Data to be added.
976 * This is a typical stack operation. The function creates an
977 * element at the front of the %list and assigns the given data
978 * to it. Due to the nature of a %list this operation can be
979 * done in constant time, and does not invalidate iterators and
983 push_front(const value_type
& __x
)
984 { this->_M_insert(begin(), __x
); }
986 #ifdef __GXX_EXPERIMENTAL_CXX0X__
988 push_front(value_type
&& __x
)
989 { this->_M_insert(begin(), std::move(__x
)); }
991 template<typename
... _Args
>
993 emplace_front(_Args
&&... __args
)
994 { this->_M_insert(begin(), std::forward
<_Args
>(__args
)...); }
998 * @brief Removes first element.
1000 * This is a typical stack operation. It shrinks the %list by
1001 * one. Due to the nature of a %list this operation can be done
1002 * in constant time, and only invalidates iterators/references to
1003 * the element being removed.
1005 * Note that no data is returned, and if the first element's data
1006 * is needed, it should be retrieved before pop_front() is
1011 { this->_M_erase(begin()); }
1014 * @brief Add data to the end of the %list.
1015 * @param __x Data to be added.
1017 * This is a typical stack operation. The function creates an
1018 * element at the end of the %list and assigns the given data to
1019 * it. Due to the nature of a %list this operation can be done
1020 * in constant time, and does not invalidate iterators and
1024 push_back(const value_type
& __x
)
1025 { this->_M_insert(end(), __x
); }
1027 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1029 push_back(value_type
&& __x
)
1030 { this->_M_insert(end(), std::move(__x
)); }
1032 template<typename
... _Args
>
1034 emplace_back(_Args
&&... __args
)
1035 { this->_M_insert(end(), std::forward
<_Args
>(__args
)...); }
1039 * @brief Removes last element.
1041 * This is a typical stack operation. It shrinks the %list by
1042 * one. Due to the nature of a %list this operation can be done
1043 * in constant time, and only invalidates iterators/references to
1044 * the element being removed.
1046 * Note that no data is returned, and if the last element's data
1047 * is needed, it should be retrieved before pop_back() is called.
1051 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1053 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1055 * @brief Constructs object in %list before specified iterator.
1056 * @param __position A const_iterator into the %list.
1057 * @param __args Arguments.
1058 * @return An iterator that points to the inserted data.
1060 * This function will insert an object of type T constructed
1061 * with T(std::forward<Args>(args)...) before the specified
1062 * location. Due to the nature of a %list this operation can
1063 * be done in constant time, and does not invalidate iterators
1066 template<typename
... _Args
>
1068 emplace(iterator __position
, _Args
&&... __args
);
1072 * @brief Inserts given value into %list before specified iterator.
1073 * @param __position An iterator into the %list.
1074 * @param __x Data to be inserted.
1075 * @return An iterator that points to the inserted data.
1077 * This function will insert a copy of the given value before
1078 * the specified location. Due to the nature of a %list this
1079 * operation can be done in constant time, and does not
1080 * invalidate iterators and references.
1083 insert(iterator __position
, const value_type
& __x
);
1085 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1087 * @brief Inserts given rvalue into %list before specified iterator.
1088 * @param __position An iterator into the %list.
1089 * @param __x Data to be inserted.
1090 * @return An iterator that points to the inserted data.
1092 * This function will insert a copy of the given rvalue before
1093 * the specified location. Due to the nature of a %list this
1094 * operation can be done in constant time, and does not
1095 * invalidate iterators and references.
1098 insert(iterator __position
, value_type
&& __x
)
1099 { return emplace(__position
, std::move(__x
)); }
1102 * @brief Inserts the contents of an initializer_list into %list
1103 * before specified iterator.
1104 * @param __p An iterator into the %list.
1105 * @param __l An initializer_list of value_type.
1107 * This function will insert copies of the data in the
1108 * initializer_list @a l into the %list before the location
1109 * specified by @a p.
1111 * This operation is linear in the number of elements inserted and
1112 * does not invalidate iterators and references.
1115 insert(iterator __p
, initializer_list
<value_type
> __l
)
1116 { this->insert(__p
, __l
.begin(), __l
.end()); }
1120 * @brief Inserts a number of copies of given data into the %list.
1121 * @param __position An iterator into the %list.
1122 * @param __n Number of elements to be inserted.
1123 * @param __x Data to be inserted.
1125 * This function will insert a specified number of copies of the
1126 * given data before the location specified by @a position.
1128 * This operation is linear in the number of elements inserted and
1129 * does not invalidate iterators and references.
1132 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1134 list
__tmp(__n
, __x
, get_allocator());
1135 splice(__position
, __tmp
);
1139 * @brief Inserts a range into the %list.
1140 * @param __position An iterator into the %list.
1141 * @param __first An input iterator.
1142 * @param __last An input iterator.
1144 * This function will insert copies of the data in the range [@a
1145 * first,@a last) into the %list before the location specified by
1148 * This operation is linear in the number of elements inserted and
1149 * does not invalidate iterators and references.
1151 template<typename _InputIterator
>
1153 insert(iterator __position
, _InputIterator __first
,
1154 _InputIterator __last
)
1156 list
__tmp(__first
, __last
, get_allocator());
1157 splice(__position
, __tmp
);
1161 * @brief Remove element at given position.
1162 * @param __position Iterator pointing to element to be erased.
1163 * @return An iterator pointing to the next element (or end()).
1165 * This function will erase the element at the given position and thus
1166 * shorten the %list by one.
1168 * Due to the nature of a %list this operation can be done in
1169 * constant time, and only invalidates iterators/references to
1170 * the element being removed. The user is also cautioned that
1171 * this function only erases the element, and that if the element
1172 * is itself a pointer, the pointed-to memory is not touched in
1173 * any way. Managing the pointer is the user's responsibility.
1176 erase(iterator __position
);
1179 * @brief Remove a range of elements.
1180 * @param __first Iterator pointing to the first element to be erased.
1181 * @param __last Iterator pointing to one past the last element to be
1183 * @return An iterator pointing to the element pointed to by @a last
1184 * prior to erasing (or end()).
1186 * This function will erase the elements in the range @a
1187 * [first,last) and shorten the %list accordingly.
1189 * This operation is linear time in the size of the range and only
1190 * invalidates iterators/references to the element being removed.
1191 * The user is also cautioned that this function only erases the
1192 * elements, and that if the elements themselves are pointers, the
1193 * pointed-to memory is not touched in any way. Managing the pointer
1194 * is the user's responsibility.
1197 erase(iterator __first
, iterator __last
)
1199 while (__first
!= __last
)
1200 __first
= erase(__first
);
1205 * @brief Swaps data with another %list.
1206 * @param __x A %list of the same element and allocator types.
1208 * This exchanges the elements between two lists in constant
1209 * time. Note that the global std::swap() function is
1210 * specialized such that std::swap(l1,l2) will feed to this
1216 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1217 __x
._M_impl
._M_node
);
1218 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1219 std::swap(this->_M_impl
._M_size
, __x
._M_impl
._M_size
);
1222 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1223 // 431. Swapping containers with unequal allocators.
1224 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
1225 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
1229 * Erases all the elements. Note that this function only erases
1230 * the elements, and that if the elements themselves are
1231 * pointers, the pointed-to memory is not touched in any way.
1232 * Managing the pointer is the user's responsibility.
1235 clear() _GLIBCXX_NOEXCEPT
1241 // [23.2.2.4] list operations
1243 * @brief Insert contents of another %list.
1244 * @param __position Iterator referencing the element to insert before.
1245 * @param __x Source list.
1247 * The elements of @a __x are inserted in constant time in front of
1248 * the element referenced by @a __position. @a __x becomes an empty
1251 * Requires this != @a __x.
1254 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1255 splice(iterator __position
, list
&& __x
)
1257 splice(iterator __position
, list
& __x
)
1262 _M_check_equal_allocators(__x
);
1264 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1266 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1267 this->_M_impl
._M_size
+= __x
.size();
1268 __x
._M_impl
._M_size
= 0;
1273 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1275 splice(iterator __position
, list
& __x
)
1276 { splice(__position
, std::move(__x
)); }
1280 * @brief Insert element from another %list.
1281 * @param __position Iterator referencing the element to insert before.
1282 * @param __x Source list.
1283 * @param __i Iterator referencing the element to move.
1285 * Removes the element in list @a __x referenced by @a __i and
1286 * inserts it into the current list before @a __position.
1289 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1290 splice(iterator __position
, list
&& __x
, iterator __i
)
1292 splice(iterator __position
, list
& __x
, iterator __i
)
1297 if (__position
== __i
|| __position
== __j
)
1302 _M_check_equal_allocators(__x
);
1304 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1305 ++this->_M_impl
._M_size
;
1306 --__x
._M_impl
._M_size
;
1310 this->_M_transfer(__position
, __i
, __j
);
1313 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1315 splice(iterator __position
, list
& __x
, iterator __i
)
1316 { splice(__position
, std::move(__x
), __i
); }
1320 * @brief Insert range from another %list.
1321 * @param __position Iterator referencing the element to insert before.
1322 * @param __x Source list.
1323 * @param __first Iterator referencing the start of range in x.
1324 * @param __last Iterator referencing the end of range in x.
1326 * Removes elements in the range [__first,__last) and inserts them
1327 * before @a __position in constant time.
1329 * Undefined if @a __position is in [__first,__last).
1332 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1333 splice(iterator __position
, list
&& __x
, iterator __first
,
1336 splice(iterator __position
, list
& __x
, iterator __first
,
1340 if (__first
!= __last
)
1344 _M_check_equal_allocators(__x
);
1346 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1347 const size_type __size
= std::distance(__first
, __last
);
1348 this->_M_impl
._M_size
+= __size
;
1349 __x
._M_impl
._M_size
-= __size
;
1353 this->_M_transfer(__position
, __first
, __last
);
1357 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1359 splice(iterator __position
, list
& __x
, iterator __first
, iterator __last
)
1360 { splice(__position
, std::move(__x
), __first
, __last
); }
1364 * @brief Remove all elements equal to value.
1365 * @param __value The value to remove.
1367 * Removes every element in the list equal to @a value.
1368 * Remaining elements stay in list order. Note that this
1369 * function only erases the elements, and that if the elements
1370 * themselves are pointers, the pointed-to memory is not
1371 * touched in any way. Managing the pointer is the user's
1375 remove(const _Tp
& __value
);
1378 * @brief Remove all elements satisfying a predicate.
1379 * @tparam _Predicate Unary predicate function or object.
1381 * Removes every element in the list for which the predicate
1382 * returns true. Remaining elements stay in list order. Note
1383 * that this function only erases the elements, and that if the
1384 * elements themselves are pointers, the pointed-to memory is
1385 * not touched in any way. Managing the pointer is the user's
1388 template<typename _Predicate
>
1390 remove_if(_Predicate
);
1393 * @brief Remove consecutive duplicate elements.
1395 * For each consecutive set of elements with the same value,
1396 * remove all but the first one. Remaining elements stay in
1397 * list order. Note that this function only erases the
1398 * elements, and that if the elements themselves are pointers,
1399 * the pointed-to memory is not touched in any way. Managing
1400 * the pointer is the user's responsibility.
1406 * @brief Remove consecutive elements satisfying a predicate.
1407 * @tparam _BinaryPredicate Binary predicate function or object.
1409 * For each consecutive set of elements [first,last) that
1410 * satisfy predicate(first,i) where i is an iterator in
1411 * [first,last), remove all but the first one. Remaining
1412 * elements stay in list order. Note that this function only
1413 * erases the elements, and that if the elements themselves are
1414 * pointers, the pointed-to memory is not touched in any way.
1415 * Managing the pointer is the user's responsibility.
1417 template<typename _BinaryPredicate
>
1419 unique(_BinaryPredicate
);
1422 * @brief Merge sorted lists.
1423 * @param __x Sorted list to merge.
1425 * Assumes that both @a __x and this list are sorted according to
1426 * operator<(). Merges elements of @a __x into this list in
1427 * sorted order, leaving @a __x empty when complete. Elements in
1428 * this list precede elements in @a __x that are equal.
1430 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1436 { merge(std::move(__x
)); }
1443 * @brief Merge sorted lists according to comparison function.
1444 * @param __x Sorted list to merge.
1445 * @tparam _StrictWeakOrdering Comparison function defining
1448 * Assumes that both @a __x and this list are sorted according to
1449 * StrictWeakOrdering. Merges elements of @a __x into this list
1450 * in sorted order, leaving @a __x empty when complete. Elements
1451 * in this list precede elements in @a __x that are equivalent
1452 * according to StrictWeakOrdering().
1454 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1455 template<typename _StrictWeakOrdering
>
1457 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1459 template<typename _StrictWeakOrdering
>
1461 merge(list
& __x
, _StrictWeakOrdering __comp
)
1462 { merge(std::move(__x
), __comp
); }
1464 template<typename _StrictWeakOrdering
>
1466 merge(list
& __x
, _StrictWeakOrdering __comp
);
1470 * @brief Reverse the elements in list.
1472 * Reverse the order of elements in the list in linear time.
1475 reverse() _GLIBCXX_NOEXCEPT
1476 { this->_M_impl
._M_node
._M_reverse(); }
1479 * @brief Sort the elements.
1481 * Sorts the elements of this list in NlogN time. Equivalent
1482 * elements remain in list order.
1488 * @brief Sort the elements according to comparison function.
1490 * Sorts the elements of this list in NlogN time. Equivalent
1491 * elements remain in list order.
1493 template<typename _StrictWeakOrdering
>
1495 sort(_StrictWeakOrdering
);
1498 // Internal constructor functions follow.
1500 // Called by the range constructor to implement [23.1.1]/9
1502 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1503 // 438. Ambiguity in the "do the right thing" clause
1504 template<typename _Integer
>
1506 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1507 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1509 // Called by the range constructor to implement [23.1.1]/9
1510 template<typename _InputIterator
>
1512 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1515 for (; __first
!= __last
; ++__first
)
1516 push_back(*__first
);
1519 // Called by list(n,v,a), and the range constructor when it turns out
1520 // to be the same thing.
1522 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1528 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1529 // Called by list(n).
1531 _M_default_initialize(size_type __n
)
1537 // Called by resize(sz).
1539 _M_default_append(size_type __n
);
1542 // Internal assign functions follow.
1544 // Called by the range assign to implement [23.1.1]/9
1546 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1547 // 438. Ambiguity in the "do the right thing" clause
1548 template<typename _Integer
>
1550 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1551 { _M_fill_assign(__n
, __val
); }
1553 // Called by the range assign to implement [23.1.1]/9
1554 template<typename _InputIterator
>
1556 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1559 // Called by assign(n,t), and the range assign when it turns out
1560 // to be the same thing.
1562 _M_fill_assign(size_type __n
, const value_type
& __val
);
1565 // Moves the elements from [first,last) before position.
1567 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1568 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1570 // Inserts new element at position given and with value given.
1571 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1573 _M_insert(iterator __position
, const value_type
& __x
)
1575 _Node
* __tmp
= _M_create_node(__x
);
1576 __tmp
->_M_hook(__position
._M_node
);
1579 template<typename
... _Args
>
1581 _M_insert(iterator __position
, _Args
&&... __args
)
1583 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1584 __tmp
->_M_hook(__position
._M_node
);
1588 // Erases element at position given.
1590 _M_erase(iterator __position
)
1592 __position
._M_node
->_M_unhook();
1593 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1594 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1595 _M_get_Node_allocator().destroy(__n
);
1597 _M_get_Tp_allocator().destroy(std::__addressof(__n
->_M_data
));
1602 // To implement the splice (and merge) bits of N1599.
1604 _M_check_equal_allocators(list
& __x
)
1606 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1607 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1608 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1613 * @brief List equality comparison.
1614 * @param __x A %list.
1615 * @param __y A %list of the same type as @a __x.
1616 * @return True iff the size and elements of the lists are equal.
1618 * This is an equivalence relation. It is linear in the size of
1619 * the lists. Lists are considered equivalent if their sizes are
1620 * equal, and if corresponding elements compare equal.
1622 template<typename _Tp
, typename _Alloc
>
1624 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1626 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1627 return (__x
.size() == __y
.size()
1628 && std::equal(__x
.begin(), __x
.end(), __y
.begin()));
1630 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1631 const_iterator __end1
= __x
.end();
1632 const_iterator __end2
= __y
.end();
1634 const_iterator __i1
= __x
.begin();
1635 const_iterator __i2
= __y
.begin();
1636 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1641 return __i1
== __end1
&& __i2
== __end2
;
1646 * @brief List ordering relation.
1647 * @param __x A %list.
1648 * @param __y A %list of the same type as @a __x.
1649 * @return True iff @a __x is lexicographically less than @a __y.
1651 * This is a total ordering relation. It is linear in the size of the
1652 * lists. The elements must be comparable with @c <.
1654 * See std::lexicographical_compare() for how the determination is made.
1656 template<typename _Tp
, typename _Alloc
>
1658 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1659 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1660 __y
.begin(), __y
.end()); }
1662 /// Based on operator==
1663 template<typename _Tp
, typename _Alloc
>
1665 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1666 { return !(__x
== __y
); }
1668 /// Based on operator<
1669 template<typename _Tp
, typename _Alloc
>
1671 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1672 { return __y
< __x
; }
1674 /// Based on operator<
1675 template<typename _Tp
, typename _Alloc
>
1677 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1678 { return !(__y
< __x
); }
1680 /// Based on operator<
1681 template<typename _Tp
, typename _Alloc
>
1683 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1684 { return !(__x
< __y
); }
1686 /// See std::list::swap().
1687 template<typename _Tp
, typename _Alloc
>
1689 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1692 _GLIBCXX_END_NAMESPACE_CONTAINER
1695 #endif /* _STL_LIST_H */