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 #include <initializer_list>
63 namespace std
_GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 // Supporting structures are split into common and templated
70 // types; the latter publicly inherits from the former in an
71 // effort to reduce code duplication. This results in some
72 // "needless" static_cast'ing later on, but it's all safe
75 /// Common part of a node in the %list.
76 struct _List_node_base
78 _List_node_base
* _M_next
;
79 _List_node_base
* _M_prev
;
82 swap(_List_node_base
& __x
, _List_node_base
& __y
) _GLIBCXX_USE_NOEXCEPT
;
85 _M_transfer(_List_node_base
* const __first
,
86 _List_node_base
* const __last
) _GLIBCXX_USE_NOEXCEPT
;
89 _M_reverse() _GLIBCXX_USE_NOEXCEPT
;
92 _M_hook(_List_node_base
* const __position
) _GLIBCXX_USE_NOEXCEPT
;
95 _M_unhook() _GLIBCXX_USE_NOEXCEPT
;
98 _GLIBCXX_END_NAMESPACE_VERSION
101 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
103 /// An actual node in the %list.
104 template<typename _Tp
>
105 struct _List_node
: public __detail::_List_node_base
110 #ifdef __GXX_EXPERIMENTAL_CXX0X__
111 template<typename
... _Args
>
112 _List_node(_Args
&&... __args
)
113 : __detail::_List_node_base(), _M_data(std::forward
<_Args
>(__args
)...)
119 * @brief A list::iterator.
121 * All the functions are op overloads.
123 template<typename _Tp
>
124 struct _List_iterator
126 typedef _List_iterator
<_Tp
> _Self
;
127 typedef _List_node
<_Tp
> _Node
;
129 typedef ptrdiff_t difference_type
;
130 typedef std::bidirectional_iterator_tag iterator_category
;
131 typedef _Tp value_type
;
132 typedef _Tp
* pointer
;
133 typedef _Tp
& reference
;
139 _List_iterator(__detail::_List_node_base
* __x
)
142 // Must downcast from _List_node_base to _List_node to get to _M_data.
145 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
149 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
154 _M_node
= _M_node
->_M_next
;
162 _M_node
= _M_node
->_M_next
;
169 _M_node
= _M_node
->_M_prev
;
177 _M_node
= _M_node
->_M_prev
;
182 operator==(const _Self
& __x
) const
183 { return _M_node
== __x
._M_node
; }
186 operator!=(const _Self
& __x
) const
187 { return _M_node
!= __x
._M_node
; }
189 // The only member points to the %list element.
190 __detail::_List_node_base
* _M_node
;
194 * @brief A list::const_iterator.
196 * All the functions are op overloads.
198 template<typename _Tp
>
199 struct _List_const_iterator
201 typedef _List_const_iterator
<_Tp
> _Self
;
202 typedef const _List_node
<_Tp
> _Node
;
203 typedef _List_iterator
<_Tp
> iterator
;
205 typedef ptrdiff_t difference_type
;
206 typedef std::bidirectional_iterator_tag iterator_category
;
207 typedef _Tp value_type
;
208 typedef const _Tp
* pointer
;
209 typedef const _Tp
& reference
;
211 _List_const_iterator()
215 _List_const_iterator(const __detail::_List_node_base
* __x
)
218 _List_const_iterator(const iterator
& __x
)
219 : _M_node(__x
._M_node
) { }
221 // Must downcast from List_node_base to _List_node to get to
225 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
229 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
234 _M_node
= _M_node
->_M_next
;
242 _M_node
= _M_node
->_M_next
;
249 _M_node
= _M_node
->_M_prev
;
257 _M_node
= _M_node
->_M_prev
;
262 operator==(const _Self
& __x
) const
263 { return _M_node
== __x
._M_node
; }
266 operator!=(const _Self
& __x
) const
267 { return _M_node
!= __x
._M_node
; }
269 // The only member points to the %list element.
270 const __detail::_List_node_base
* _M_node
;
273 template<typename _Val
>
275 operator==(const _List_iterator
<_Val
>& __x
,
276 const _List_const_iterator
<_Val
>& __y
)
277 { return __x
._M_node
== __y
._M_node
; }
279 template<typename _Val
>
281 operator!=(const _List_iterator
<_Val
>& __x
,
282 const _List_const_iterator
<_Val
>& __y
)
283 { return __x
._M_node
!= __y
._M_node
; }
286 /// See bits/stl_deque.h's _Deque_base for an explanation.
287 template<typename _Tp
, typename _Alloc
>
292 // The stored instance is not actually of "allocator_type"'s
293 // type. Instead we rebind the type to
294 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
295 // should probably be the same. List_node<Tp> is not the same
296 // size as Tp (it's two pointers larger), and specializations on
297 // Tp may go unused because List_node<Tp> is being bound
300 // We put this to the test in the constructors and in
301 // get_allocator, where we use conversions between
302 // allocator_type and _Node_alloc_type. The conversion is
303 // required by table 32 in [20.1.5].
304 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
307 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
310 : public _Node_alloc_type
312 __detail::_List_node_base _M_node
;
315 : _Node_alloc_type(), _M_node()
318 _List_impl(const _Node_alloc_type
& __a
)
319 : _Node_alloc_type(__a
), _M_node()
322 #ifdef __GXX_EXPERIMENTAL_CXX0X__
323 _List_impl(_Node_alloc_type
&& __a
)
324 : _Node_alloc_type(std::move(__a
)), _M_node()
333 { return _M_impl
._Node_alloc_type::allocate(1); }
336 _M_put_node(_List_node
<_Tp
>* __p
)
337 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
340 typedef _Alloc allocator_type
;
343 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
344 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
346 const _Node_alloc_type
&
347 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
348 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
351 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
352 { return _Tp_alloc_type(_M_get_Node_allocator()); }
355 get_allocator() const _GLIBCXX_NOEXCEPT
356 { return allocator_type(_M_get_Node_allocator()); }
362 _List_base(const allocator_type
& __a
)
366 #ifdef __GXX_EXPERIMENTAL_CXX0X__
367 _List_base(_List_base
&& __x
)
368 : _M_impl(std::move(__x
._M_get_Node_allocator()))
371 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
372 __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 * Meets the requirements of a <a href="tables.html#65">container</a>, a
398 * <a href="tables.html#66">reversible container</a>, and a
399 * <a href="tables.html#67">sequence</a>, including the
400 * <a href="tables.html#68">optional sequence requirements</a> with the
401 * %exception of @c at and @c operator[].
403 * This is a @e doubly @e linked %list. Traversal up and down the
404 * %list requires linear time, but adding and removing elements (or
405 * @e nodes) is done in constant time, regardless of where the
406 * change takes place. Unlike std::vector and std::deque,
407 * random-access iterators are not provided, so subscripting ( @c
408 * [] ) access is not allowed. For algorithms which only need
409 * sequential access, this lack makes no difference.
411 * Also unlike the other standard containers, std::list provides
412 * specialized algorithms %unique to linked lists, such as
413 * splicing, sorting, and in-place reversal.
415 * A couple points on memory allocation for list<Tp>:
417 * First, we never actually allocate a Tp, we allocate
418 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
419 * that after elements from %list<X,Alloc1> are spliced into
420 * %list<X,Alloc2>, destroying the memory of the second %list is a
421 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
423 * Second, a %list conceptually represented as
425 * A <---> B <---> C <---> D
427 * is actually circular; a link exists between A and D. The %list
428 * class holds (as its only data member) a private list::iterator
429 * pointing to @e D, not to @e A! To get to the head of the %list,
430 * we start at the tail and move forward by one. When this member
431 * iterator's next/previous pointers refer to itself, the %list is
434 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
435 class list
: protected _List_base
<_Tp
, _Alloc
>
437 // concept requirements
438 typedef typename
_Alloc::value_type _Alloc_value_type
;
439 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
440 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
442 typedef _List_base
<_Tp
, _Alloc
> _Base
;
443 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
446 typedef _Tp value_type
;
447 typedef typename
_Tp_alloc_type::pointer pointer
;
448 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
449 typedef typename
_Tp_alloc_type::reference reference
;
450 typedef typename
_Tp_alloc_type::const_reference const_reference
;
451 typedef _List_iterator
<_Tp
> iterator
;
452 typedef _List_const_iterator
<_Tp
> const_iterator
;
453 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
454 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
455 typedef size_t size_type
;
456 typedef ptrdiff_t difference_type
;
457 typedef _Alloc allocator_type
;
460 // Note that pointers-to-_Node's can be ctor-converted to
462 typedef _List_node
<_Tp
> _Node
;
464 using _Base::_M_impl
;
465 using _Base::_M_put_node
;
466 using _Base::_M_get_node
;
467 using _Base::_M_get_Tp_allocator
;
468 using _Base::_M_get_Node_allocator
;
471 * @param __args An instance of user data.
473 * Allocates space for a new node and constructs a copy of
476 #ifndef __GXX_EXPERIMENTAL_CXX0X__
478 _M_create_node(const value_type
& __x
)
480 _Node
* __p
= this->_M_get_node();
483 _M_get_Tp_allocator().construct
484 (std::__addressof(__p
->_M_data
), __x
);
489 __throw_exception_again
;
494 template<typename
... _Args
>
496 _M_create_node(_Args
&&... __args
)
498 _Node
* __p
= this->_M_get_node();
501 _M_get_Node_allocator().construct(__p
,
502 std::forward
<_Args
>(__args
)...);
507 __throw_exception_again
;
514 // [23.2.2.1] construct/copy/destroy
515 // (assign() and get_allocator() are also listed in this section)
517 * @brief Default constructor creates no elements.
523 * @brief Creates a %list with no elements.
524 * @param __a An allocator object.
527 list(const allocator_type
& __a
)
530 #ifdef __GXX_EXPERIMENTAL_CXX0X__
532 * @brief Creates a %list with default constructed elements.
533 * @param __n The number of elements to initially create.
535 * This constructor fills the %list with @a __n default
536 * constructed elements.
541 { _M_default_initialize(__n
); }
544 * @brief Creates a %list with copies of an exemplar element.
545 * @param __n The number of elements to initially create.
546 * @param __value An element to copy.
547 * @param __a An allocator object.
549 * This constructor fills the %list with @a __n copies of @a __value.
551 list(size_type __n
, const value_type
& __value
,
552 const allocator_type
& __a
= allocator_type())
554 { _M_fill_initialize(__n
, __value
); }
557 * @brief Creates a %list with copies of an exemplar element.
558 * @param __n The number of elements to initially create.
559 * @param __value An element to copy.
560 * @param __a An allocator object.
562 * This constructor fills the %list with @a __n copies of @a __value.
565 list(size_type __n
, const value_type
& __value
= value_type(),
566 const allocator_type
& __a
= allocator_type())
568 { _M_fill_initialize(__n
, __value
); }
572 * @brief %List copy constructor.
573 * @param __x A %list of identical element and allocator types.
575 * The newly-created %list uses a copy of the allocation object used
578 list(const list
& __x
)
579 : _Base(__x
._M_get_Node_allocator())
580 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
582 #ifdef __GXX_EXPERIMENTAL_CXX0X__
584 * @brief %List move constructor.
585 * @param __x A %list of identical element and allocator types.
587 * The newly-created %list contains the exact contents of @a __x.
588 * The contents of @a __x are a valid, but unspecified %list.
590 list(list
&& __x
) noexcept
591 : _Base(std::move(__x
)) { }
594 * @brief Builds a %list from an initializer_list
595 * @param __l An initializer_list of value_type.
596 * @param __a An allocator object.
598 * Create a %list consisting of copies of the elements in the
599 * initializer_list @a __l. This is linear in __l.size().
601 list(initializer_list
<value_type
> __l
,
602 const allocator_type
& __a
= allocator_type())
604 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
608 * @brief Builds a %list from a range.
609 * @param __first An input iterator.
610 * @param __last An input iterator.
611 * @param __a An allocator object.
613 * Create a %list consisting of copies of the elements from
614 * [@a __first,@a __last). This is linear in N (where N is
615 * distance(@a __first,@a __last)).
617 template<typename _InputIterator
>
618 list(_InputIterator __first
, _InputIterator __last
,
619 const allocator_type
& __a
= allocator_type())
622 // Check whether it's an integral type. If so, it's not an iterator.
623 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
624 _M_initialize_dispatch(__first
, __last
, _Integral());
628 * No explicit dtor needed as the _Base dtor takes care of
629 * things. The _Base dtor only erases the elements, and note
630 * that if the elements themselves are pointers, the pointed-to
631 * memory is not touched in any way. Managing the pointer is
632 * the user's responsibility.
636 * @brief %List assignment operator.
637 * @param __x A %list of identical element and allocator types.
639 * All the elements of @a __x are copied, but unlike the copy
640 * constructor, the allocator object is not copied.
643 operator=(const list
& __x
);
645 #ifdef __GXX_EXPERIMENTAL_CXX0X__
647 * @brief %List move assignment operator.
648 * @param __x A %list of identical element and allocator types.
650 * The contents of @a __x are moved into this %list (without copying).
651 * @a __x is a valid, but unspecified %list
654 operator=(list
&& __x
)
664 * @brief %List initializer list assignment operator.
665 * @param __l An initializer_list of value_type.
667 * Replace the contents of the %list with copies of the elements
668 * in the initializer_list @a __l. This is linear in l.size().
671 operator=(initializer_list
<value_type
> __l
)
673 this->assign(__l
.begin(), __l
.end());
679 * @brief Assigns a given value to a %list.
680 * @param __n Number of elements to be assigned.
681 * @param __val Value to be assigned.
683 * This function fills a %list with @a __n copies of the given
684 * value. Note that the assignment completely changes the %list
685 * and that the resulting %list's size is the same as the number
686 * of elements assigned. Old data may be lost.
689 assign(size_type __n
, const value_type
& __val
)
690 { _M_fill_assign(__n
, __val
); }
693 * @brief Assigns a range to a %list.
694 * @param __first An input iterator.
695 * @param __last An input iterator.
697 * This function fills a %list with copies of the elements in the
698 * range [@a __first,@a __last).
700 * Note that the assignment completely changes the %list and
701 * that the resulting %list's size is the same as the number of
702 * elements assigned. Old data may be lost.
704 template<typename _InputIterator
>
706 assign(_InputIterator __first
, _InputIterator __last
)
708 // Check whether it's an integral type. If so, it's not an iterator.
709 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
710 _M_assign_dispatch(__first
, __last
, _Integral());
713 #ifdef __GXX_EXPERIMENTAL_CXX0X__
715 * @brief Assigns an initializer_list to a %list.
716 * @param __l An initializer_list of value_type.
718 * Replace the contents of the %list with copies of the elements
719 * in the initializer_list @a __l. This is linear in __l.size().
722 assign(initializer_list
<value_type
> __l
)
723 { this->assign(__l
.begin(), __l
.end()); }
726 /// Get a copy of the memory allocation object.
728 get_allocator() const _GLIBCXX_NOEXCEPT
729 { return _Base::get_allocator(); }
733 * Returns a read/write iterator that points to the first element in the
734 * %list. Iteration is done in ordinary element order.
737 begin() _GLIBCXX_NOEXCEPT
738 { return iterator(this->_M_impl
._M_node
._M_next
); }
741 * Returns a read-only (constant) iterator that points to the
742 * first element in the %list. Iteration is done in ordinary
746 begin() const _GLIBCXX_NOEXCEPT
747 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
750 * Returns a read/write iterator that points one past the last
751 * element in the %list. Iteration is done in ordinary element
755 end() _GLIBCXX_NOEXCEPT
756 { return iterator(&this->_M_impl
._M_node
); }
759 * Returns a read-only (constant) iterator that points one past
760 * the last element in the %list. Iteration is done in ordinary
764 end() const _GLIBCXX_NOEXCEPT
765 { return const_iterator(&this->_M_impl
._M_node
); }
768 * Returns a read/write reverse iterator that points to the last
769 * element in the %list. Iteration is done in reverse element
773 rbegin() _GLIBCXX_NOEXCEPT
774 { return reverse_iterator(end()); }
777 * Returns a read-only (constant) reverse iterator that points to
778 * the last element in the %list. Iteration is done in reverse
781 const_reverse_iterator
782 rbegin() const _GLIBCXX_NOEXCEPT
783 { return const_reverse_iterator(end()); }
786 * Returns a read/write reverse iterator that points to one
787 * before the first element in the %list. Iteration is done in
788 * reverse element order.
791 rend() _GLIBCXX_NOEXCEPT
792 { return reverse_iterator(begin()); }
795 * Returns a read-only (constant) reverse iterator that points to one
796 * before the first element in the %list. Iteration is done in reverse
799 const_reverse_iterator
800 rend() const _GLIBCXX_NOEXCEPT
801 { return const_reverse_iterator(begin()); }
803 #ifdef __GXX_EXPERIMENTAL_CXX0X__
805 * Returns a read-only (constant) iterator that points to the
806 * first element in the %list. Iteration is done in ordinary
810 cbegin() const noexcept
811 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
814 * Returns a read-only (constant) iterator that points one past
815 * the last element in the %list. Iteration is done in ordinary
819 cend() const noexcept
820 { return const_iterator(&this->_M_impl
._M_node
); }
823 * Returns a read-only (constant) reverse iterator that points to
824 * the last element in the %list. Iteration is done in reverse
827 const_reverse_iterator
828 crbegin() const noexcept
829 { return const_reverse_iterator(end()); }
832 * Returns a read-only (constant) reverse iterator that points to one
833 * before the first element in the %list. Iteration is done in reverse
836 const_reverse_iterator
837 crend() const noexcept
838 { return const_reverse_iterator(begin()); }
841 // [23.2.2.2] capacity
843 * Returns true if the %list is empty. (Thus begin() would equal
847 empty() const _GLIBCXX_NOEXCEPT
848 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
850 /** Returns the number of elements in the %list. */
852 size() const _GLIBCXX_NOEXCEPT
853 { return std::distance(begin(), end()); }
855 /** Returns the size() of the largest possible %list. */
857 max_size() const _GLIBCXX_NOEXCEPT
858 { return _M_get_Node_allocator().max_size(); }
860 #ifdef __GXX_EXPERIMENTAL_CXX0X__
862 * @brief Resizes the %list to the specified number of elements.
863 * @param __new_size Number of elements the %list should contain.
865 * This function will %resize the %list to the specified number
866 * of elements. If the number is smaller than the %list's
867 * current size the %list is truncated, otherwise default
868 * constructed elements are appended.
871 resize(size_type __new_size
);
874 * @brief Resizes the %list to the specified number of elements.
875 * @param __new_size Number of elements the %list should contain.
876 * @param __x Data with which new elements should be populated.
878 * This function will %resize the %list to the specified number
879 * of elements. If the number is smaller than the %list's
880 * current size the %list is truncated, otherwise the %list is
881 * extended and new elements are populated with given data.
884 resize(size_type __new_size
, const value_type
& __x
);
887 * @brief Resizes the %list to the specified number of elements.
888 * @param __new_size Number of elements the %list should contain.
889 * @param __x Data with which new elements should be populated.
891 * This function will %resize the %list to the specified number
892 * of elements. If the number is smaller than the %list's
893 * current size the %list is truncated, otherwise the %list is
894 * extended and new elements are populated with given data.
897 resize(size_type __new_size
, value_type __x
= value_type());
902 * Returns a read/write reference to the data at the first
903 * element of the %list.
910 * Returns a read-only (constant) reference to the data at the first
911 * element of the %list.
918 * Returns a read/write reference to the data at the last element
924 iterator __tmp
= end();
930 * Returns a read-only (constant) reference to the data at the last
931 * element of the %list.
936 const_iterator __tmp
= end();
941 // [23.2.2.3] modifiers
943 * @brief Add data to the front of the %list.
944 * @param __x Data to be added.
946 * This is a typical stack operation. The function creates an
947 * element at the front of the %list and assigns the given data
948 * to it. Due to the nature of a %list this operation can be
949 * done in constant time, and does not invalidate iterators and
953 push_front(const value_type
& __x
)
954 { this->_M_insert(begin(), __x
); }
956 #ifdef __GXX_EXPERIMENTAL_CXX0X__
958 push_front(value_type
&& __x
)
959 { this->_M_insert(begin(), std::move(__x
)); }
961 template<typename
... _Args
>
963 emplace_front(_Args
&&... __args
)
964 { this->_M_insert(begin(), std::forward
<_Args
>(__args
)...); }
968 * @brief Removes first element.
970 * This is a typical stack operation. It shrinks the %list by
971 * one. Due to the nature of a %list this operation can be done
972 * in constant time, and only invalidates iterators/references to
973 * the element being removed.
975 * Note that no data is returned, and if the first element's data
976 * is needed, it should be retrieved before pop_front() is
981 { this->_M_erase(begin()); }
984 * @brief Add data to the end of the %list.
985 * @param __x Data to be added.
987 * This is a typical stack operation. The function creates an
988 * element at the end of the %list and assigns the given data to
989 * it. Due to the nature of a %list this operation can be done
990 * in constant time, and does not invalidate iterators and
994 push_back(const value_type
& __x
)
995 { this->_M_insert(end(), __x
); }
997 #ifdef __GXX_EXPERIMENTAL_CXX0X__
999 push_back(value_type
&& __x
)
1000 { this->_M_insert(end(), std::move(__x
)); }
1002 template<typename
... _Args
>
1004 emplace_back(_Args
&&... __args
)
1005 { this->_M_insert(end(), std::forward
<_Args
>(__args
)...); }
1009 * @brief Removes last element.
1011 * This is a typical stack operation. It shrinks the %list by
1012 * one. Due to the nature of a %list this operation can be done
1013 * in constant time, and only invalidates iterators/references to
1014 * the element being removed.
1016 * Note that no data is returned, and if the last element's data
1017 * is needed, it should be retrieved before pop_back() is called.
1021 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1023 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1025 * @brief Constructs object in %list before specified iterator.
1026 * @param __position A const_iterator into the %list.
1027 * @param __args Arguments.
1028 * @return An iterator that points to the inserted data.
1030 * This function will insert an object of type T constructed
1031 * with T(std::forward<Args>(args)...) before the specified
1032 * location. Due to the nature of a %list this operation can
1033 * be done in constant time, and does not invalidate iterators
1036 template<typename
... _Args
>
1038 emplace(iterator __position
, _Args
&&... __args
);
1042 * @brief Inserts given value into %list before specified iterator.
1043 * @param __position An iterator into the %list.
1044 * @param __x Data to be inserted.
1045 * @return An iterator that points to the inserted data.
1047 * This function will insert a copy of the given value before
1048 * the specified location. Due to the nature of a %list this
1049 * operation can be done in constant time, and does not
1050 * invalidate iterators and references.
1053 insert(iterator __position
, const value_type
& __x
);
1055 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1057 * @brief Inserts given rvalue into %list before specified iterator.
1058 * @param __position An iterator into the %list.
1059 * @param __x Data to be inserted.
1060 * @return An iterator that points to the inserted data.
1062 * This function will insert a copy of the given rvalue before
1063 * the specified location. Due to the nature of a %list this
1064 * operation can be done in constant time, and does not
1065 * invalidate iterators and references.
1068 insert(iterator __position
, value_type
&& __x
)
1069 { return emplace(__position
, std::move(__x
)); }
1072 * @brief Inserts the contents of an initializer_list into %list
1073 * before specified iterator.
1074 * @param __p An iterator into the %list.
1075 * @param __l An initializer_list of value_type.
1077 * This function will insert copies of the data in the
1078 * initializer_list @a l into the %list before the location
1079 * specified by @a p.
1081 * This operation is linear in the number of elements inserted and
1082 * does not invalidate iterators and references.
1085 insert(iterator __p
, initializer_list
<value_type
> __l
)
1086 { this->insert(__p
, __l
.begin(), __l
.end()); }
1090 * @brief Inserts a number of copies of given data into the %list.
1091 * @param __position An iterator into the %list.
1092 * @param __n Number of elements to be inserted.
1093 * @param __x Data to be inserted.
1095 * This function will insert a specified number of copies of the
1096 * given data before the location specified by @a position.
1098 * This operation is linear in the number of elements inserted and
1099 * does not invalidate iterators and references.
1102 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1104 list
__tmp(__n
, __x
, _M_get_Node_allocator());
1105 splice(__position
, __tmp
);
1109 * @brief Inserts a range into the %list.
1110 * @param __position An iterator into the %list.
1111 * @param __first An input iterator.
1112 * @param __last An input iterator.
1114 * This function will insert copies of the data in the range [@a
1115 * first,@a last) into the %list before the location specified by
1118 * This operation is linear in the number of elements inserted and
1119 * does not invalidate iterators and references.
1121 template<typename _InputIterator
>
1123 insert(iterator __position
, _InputIterator __first
,
1124 _InputIterator __last
)
1126 list
__tmp(__first
, __last
, _M_get_Node_allocator());
1127 splice(__position
, __tmp
);
1131 * @brief Remove element at given position.
1132 * @param __position Iterator pointing to element to be erased.
1133 * @return An iterator pointing to the next element (or end()).
1135 * This function will erase the element at the given position and thus
1136 * shorten the %list by one.
1138 * Due to the nature of a %list this operation can be done in
1139 * constant time, and only invalidates iterators/references to
1140 * the element being removed. The user is also cautioned that
1141 * this function only erases the element, and that if the element
1142 * is itself a pointer, the pointed-to memory is not touched in
1143 * any way. Managing the pointer is the user's responsibility.
1146 erase(iterator __position
);
1149 * @brief Remove a range of elements.
1150 * @param __first Iterator pointing to the first element to be erased.
1151 * @param __last Iterator pointing to one past the last element to be
1153 * @return An iterator pointing to the element pointed to by @a last
1154 * prior to erasing (or end()).
1156 * This function will erase the elements in the range @a
1157 * [first,last) and shorten the %list accordingly.
1159 * This operation is linear time in the size of the range and only
1160 * invalidates iterators/references to the element being removed.
1161 * The user is also cautioned that this function only erases the
1162 * elements, and that if the elements themselves are pointers, the
1163 * pointed-to memory is not touched in any way. Managing the pointer
1164 * is the user's responsibility.
1167 erase(iterator __first
, iterator __last
)
1169 while (__first
!= __last
)
1170 __first
= erase(__first
);
1175 * @brief Swaps data with another %list.
1176 * @param __x A %list of the same element and allocator types.
1178 * This exchanges the elements between two lists in constant
1179 * time. Note that the global std::swap() function is
1180 * specialized such that std::swap(l1,l2) will feed to this
1186 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1187 __x
._M_impl
._M_node
);
1189 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1190 // 431. Swapping containers with unequal allocators.
1191 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
1192 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
1196 * Erases all the elements. Note that this function only erases
1197 * the elements, and that if the elements themselves are
1198 * pointers, the pointed-to memory is not touched in any way.
1199 * Managing the pointer is the user's responsibility.
1202 clear() _GLIBCXX_NOEXCEPT
1208 // [23.2.2.4] list operations
1210 * @brief Insert contents of another %list.
1211 * @param __position Iterator referencing the element to insert before.
1212 * @param __x Source list.
1214 * The elements of @a __x are inserted in constant time in front of
1215 * the element referenced by @a __position. @a __x becomes an empty
1218 * Requires this != @a __x.
1221 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1222 splice(iterator __position
, list
&& __x
)
1224 splice(iterator __position
, list
& __x
)
1229 _M_check_equal_allocators(__x
);
1231 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1235 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1237 splice(iterator __position
, list
& __x
)
1238 { splice(__position
, std::move(__x
)); }
1242 * @brief Insert element from another %list.
1243 * @param __position Iterator referencing the element to insert before.
1244 * @param __x Source list.
1245 * @param __i Iterator referencing the element to move.
1247 * Removes the element in list @a __x referenced by @a __i and
1248 * inserts it into the current list before @a __position.
1251 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1252 splice(iterator __position
, list
&& __x
, iterator __i
)
1254 splice(iterator __position
, list
& __x
, iterator __i
)
1259 if (__position
== __i
|| __position
== __j
)
1263 _M_check_equal_allocators(__x
);
1265 this->_M_transfer(__position
, __i
, __j
);
1268 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1270 splice(iterator __position
, list
& __x
, iterator __i
)
1271 { splice(__position
, std::move(__x
), __i
); }
1275 * @brief Insert range from another %list.
1276 * @param __position Iterator referencing the element to insert before.
1277 * @param __x Source list.
1278 * @param __first Iterator referencing the start of range in x.
1279 * @param __last Iterator referencing the end of range in x.
1281 * Removes elements in the range [__first,__last) and inserts them
1282 * before @a __position in constant time.
1284 * Undefined if @a __position is in [__first,__last).
1287 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1288 splice(iterator __position
, list
&& __x
, iterator __first
,
1291 splice(iterator __position
, list
& __x
, iterator __first
,
1295 if (__first
!= __last
)
1298 _M_check_equal_allocators(__x
);
1300 this->_M_transfer(__position
, __first
, __last
);
1304 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1306 splice(iterator __position
, list
& __x
, iterator __first
, iterator __last
)
1307 { splice(__position
, std::move(__x
), __first
, __last
); }
1311 * @brief Remove all elements equal to value.
1312 * @param __value The value to remove.
1314 * Removes every element in the list equal to @a value.
1315 * Remaining elements stay in list order. Note that this
1316 * function only erases the elements, and that if the elements
1317 * themselves are pointers, the pointed-to memory is not
1318 * touched in any way. Managing the pointer is the user's
1322 remove(const _Tp
& __value
);
1325 * @brief Remove all elements satisfying a predicate.
1326 * @tparam _Predicate Unary predicate function or object.
1328 * Removes every element in the list for which the predicate
1329 * returns true. Remaining elements stay in list order. Note
1330 * that this function only erases the elements, and that if the
1331 * elements themselves are pointers, the pointed-to memory is
1332 * not touched in any way. Managing the pointer is the user's
1335 template<typename _Predicate
>
1337 remove_if(_Predicate
);
1340 * @brief Remove consecutive duplicate elements.
1342 * For each consecutive set of elements with the same value,
1343 * remove all but the first one. Remaining elements stay in
1344 * list order. Note that this function only erases the
1345 * elements, and that if the elements themselves are pointers,
1346 * the pointed-to memory is not touched in any way. Managing
1347 * the pointer is the user's responsibility.
1353 * @brief Remove consecutive elements satisfying a predicate.
1354 * @tparam _BinaryPredicate Binary predicate function or object.
1356 * For each consecutive set of elements [first,last) that
1357 * satisfy predicate(first,i) where i is an iterator in
1358 * [first,last), remove all but the first one. Remaining
1359 * elements stay in list order. Note that this function only
1360 * erases the elements, and that if the elements themselves are
1361 * pointers, the pointed-to memory is not touched in any way.
1362 * Managing the pointer is the user's responsibility.
1364 template<typename _BinaryPredicate
>
1366 unique(_BinaryPredicate
);
1369 * @brief Merge sorted lists.
1370 * @param __x Sorted list to merge.
1372 * Assumes that both @a __x and this list are sorted according to
1373 * operator<(). Merges elements of @a __x into this list in
1374 * sorted order, leaving @a __x empty when complete. Elements in
1375 * this list precede elements in @a __x that are equal.
1377 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1383 { merge(std::move(__x
)); }
1390 * @brief Merge sorted lists according to comparison function.
1391 * @param __x Sorted list to merge.
1392 * @tparam _StrictWeakOrdering Comparison function defining
1395 * Assumes that both @a __x and this list are sorted according to
1396 * StrictWeakOrdering. Merges elements of @a __x into this list
1397 * in sorted order, leaving @a __x empty when complete. Elements
1398 * in this list precede elements in @a __x that are equivalent
1399 * according to StrictWeakOrdering().
1401 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1402 template<typename _StrictWeakOrdering
>
1404 merge(list
&& __x
, _StrictWeakOrdering __comp
);
1406 template<typename _StrictWeakOrdering
>
1408 merge(list
& __x
, _StrictWeakOrdering __comp
)
1409 { merge(std::move(__x
), __comp
); }
1411 template<typename _StrictWeakOrdering
>
1413 merge(list
& __x
, _StrictWeakOrdering __comp
);
1417 * @brief Reverse the elements in list.
1419 * Reverse the order of elements in the list in linear time.
1422 reverse() _GLIBCXX_NOEXCEPT
1423 { this->_M_impl
._M_node
._M_reverse(); }
1426 * @brief Sort the elements.
1428 * Sorts the elements of this list in NlogN time. Equivalent
1429 * elements remain in list order.
1435 * @brief Sort the elements according to comparison function.
1437 * Sorts the elements of this list in NlogN time. Equivalent
1438 * elements remain in list order.
1440 template<typename _StrictWeakOrdering
>
1442 sort(_StrictWeakOrdering
);
1445 // Internal constructor functions follow.
1447 // Called by the range constructor to implement [23.1.1]/9
1449 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1450 // 438. Ambiguity in the "do the right thing" clause
1451 template<typename _Integer
>
1453 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1454 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1456 // Called by the range constructor to implement [23.1.1]/9
1457 template<typename _InputIterator
>
1459 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1462 for (; __first
!= __last
; ++__first
)
1463 push_back(*__first
);
1466 // Called by list(n,v,a), and the range constructor when it turns out
1467 // to be the same thing.
1469 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1475 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1476 // Called by list(n).
1478 _M_default_initialize(size_type __n
)
1484 // Called by resize(sz).
1486 _M_default_append(size_type __n
);
1489 // Internal assign functions follow.
1491 // Called by the range assign to implement [23.1.1]/9
1493 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1494 // 438. Ambiguity in the "do the right thing" clause
1495 template<typename _Integer
>
1497 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1498 { _M_fill_assign(__n
, __val
); }
1500 // Called by the range assign to implement [23.1.1]/9
1501 template<typename _InputIterator
>
1503 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1506 // Called by assign(n,t), and the range assign when it turns out
1507 // to be the same thing.
1509 _M_fill_assign(size_type __n
, const value_type
& __val
);
1512 // Moves the elements from [first,last) before position.
1514 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1515 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1517 // Inserts new element at position given and with value given.
1518 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1520 _M_insert(iterator __position
, const value_type
& __x
)
1522 _Node
* __tmp
= _M_create_node(__x
);
1523 __tmp
->_M_hook(__position
._M_node
);
1526 template<typename
... _Args
>
1528 _M_insert(iterator __position
, _Args
&&... __args
)
1530 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1531 __tmp
->_M_hook(__position
._M_node
);
1535 // Erases element at position given.
1537 _M_erase(iterator __position
)
1539 __position
._M_node
->_M_unhook();
1540 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1541 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1542 _M_get_Node_allocator().destroy(__n
);
1544 _M_get_Tp_allocator().destroy(std::__addressof(__n
->_M_data
));
1549 // To implement the splice (and merge) bits of N1599.
1551 _M_check_equal_allocators(list
& __x
)
1553 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1554 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1555 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1560 * @brief List equality comparison.
1561 * @param __x A %list.
1562 * @param __y A %list of the same type as @a __x.
1563 * @return True iff the size and elements of the lists are equal.
1565 * This is an equivalence relation. It is linear in the size of
1566 * the lists. Lists are considered equivalent if their sizes are
1567 * equal, and if corresponding elements compare equal.
1569 template<typename _Tp
, typename _Alloc
>
1571 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1573 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1574 const_iterator __end1
= __x
.end();
1575 const_iterator __end2
= __y
.end();
1577 const_iterator __i1
= __x
.begin();
1578 const_iterator __i2
= __y
.begin();
1579 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1584 return __i1
== __end1
&& __i2
== __end2
;
1588 * @brief List ordering relation.
1589 * @param __x A %list.
1590 * @param __y A %list of the same type as @a __x.
1591 * @return True iff @a __x is lexicographically less than @a __y.
1593 * This is a total ordering relation. It is linear in the size of the
1594 * lists. The elements must be comparable with @c <.
1596 * See std::lexicographical_compare() for how the determination is made.
1598 template<typename _Tp
, typename _Alloc
>
1600 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1601 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1602 __y
.begin(), __y
.end()); }
1604 /// Based on operator==
1605 template<typename _Tp
, typename _Alloc
>
1607 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1608 { return !(__x
== __y
); }
1610 /// Based on operator<
1611 template<typename _Tp
, typename _Alloc
>
1613 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1614 { return __y
< __x
; }
1616 /// Based on operator<
1617 template<typename _Tp
, typename _Alloc
>
1619 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1620 { return !(__y
< __x
); }
1622 /// Based on operator<
1623 template<typename _Tp
, typename _Alloc
>
1625 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1626 { return !(__x
< __y
); }
1628 /// See std::list::swap().
1629 template<typename _Tp
, typename _Alloc
>
1631 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1634 _GLIBCXX_END_NAMESPACE_CONTAINER
1637 #endif /* _STL_LIST_H */