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()
327 { return _M_impl
._Node_alloc_type::allocate(1); }
330 _M_put_node(_List_node
<_Tp
>* __p
)
331 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
334 typedef _Alloc allocator_type
;
337 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
338 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
340 const _Node_alloc_type
&
341 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
342 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
345 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
346 { return _Tp_alloc_type(_M_get_Node_allocator()); }
349 get_allocator() const _GLIBCXX_NOEXCEPT
350 { return allocator_type(_M_get_Node_allocator()); }
356 _List_base(const allocator_type
& __a
)
360 #ifdef __GXX_EXPERIMENTAL_CXX0X__
361 _List_base(_List_base
&& __x
)
362 : _M_impl(__x
._M_get_Node_allocator())
365 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
366 __x
._M_impl
._M_node
);
370 // This is what actually destroys the list.
380 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
381 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
386 * @brief A standard container with linear time access to elements,
387 * and fixed time insertion/deletion at any point in the sequence.
391 * Meets the requirements of a <a href="tables.html#65">container</a>, a
392 * <a href="tables.html#66">reversible container</a>, and a
393 * <a href="tables.html#67">sequence</a>, including the
394 * <a href="tables.html#68">optional sequence requirements</a> with the
395 * %exception of @c at and @c operator[].
397 * This is a @e doubly @e linked %list. Traversal up and down the
398 * %list requires linear time, but adding and removing elements (or
399 * @e nodes) is done in constant time, regardless of where the
400 * change takes place. Unlike std::vector and std::deque,
401 * random-access iterators are not provided, so subscripting ( @c
402 * [] ) access is not allowed. For algorithms which only need
403 * sequential access, this lack makes no difference.
405 * Also unlike the other standard containers, std::list provides
406 * specialized algorithms %unique to linked lists, such as
407 * splicing, sorting, and in-place reversal.
409 * A couple points on memory allocation for list<Tp>:
411 * First, we never actually allocate a Tp, we allocate
412 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
413 * that after elements from %list<X,Alloc1> are spliced into
414 * %list<X,Alloc2>, destroying the memory of the second %list is a
415 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
417 * Second, a %list conceptually represented as
419 * A <---> B <---> C <---> D
421 * is actually circular; a link exists between A and D. The %list
422 * class holds (as its only data member) a private list::iterator
423 * pointing to @e D, not to @e A! To get to the head of the %list,
424 * we start at the tail and move forward by one. When this member
425 * iterator's next/previous pointers refer to itself, the %list is
428 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
429 class list
: protected _List_base
<_Tp
, _Alloc
>
431 // concept requirements
432 typedef typename
_Alloc::value_type _Alloc_value_type
;
433 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
434 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
436 typedef _List_base
<_Tp
, _Alloc
> _Base
;
437 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
440 typedef _Tp value_type
;
441 typedef typename
_Tp_alloc_type::pointer pointer
;
442 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
443 typedef typename
_Tp_alloc_type::reference reference
;
444 typedef typename
_Tp_alloc_type::const_reference const_reference
;
445 typedef _List_iterator
<_Tp
> iterator
;
446 typedef _List_const_iterator
<_Tp
> const_iterator
;
447 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
448 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
449 typedef size_t size_type
;
450 typedef ptrdiff_t difference_type
;
451 typedef _Alloc allocator_type
;
454 // Note that pointers-to-_Node's can be ctor-converted to
456 typedef _List_node
<_Tp
> _Node
;
458 using _Base::_M_impl
;
459 using _Base::_M_put_node
;
460 using _Base::_M_get_node
;
461 using _Base::_M_get_Tp_allocator
;
462 using _Base::_M_get_Node_allocator
;
465 * @param x An instance of user data.
467 * Allocates space for a new node and constructs a copy of @a x in it.
469 #ifndef __GXX_EXPERIMENTAL_CXX0X__
471 _M_create_node(const value_type
& __x
)
473 _Node
* __p
= this->_M_get_node();
476 _M_get_Tp_allocator().construct
477 (std::__addressof(__p
->_M_data
), __x
);
482 __throw_exception_again
;
487 template<typename
... _Args
>
489 _M_create_node(_Args
&&... __args
)
491 _Node
* __p
= this->_M_get_node();
494 _M_get_Node_allocator().construct(__p
,
495 std::forward
<_Args
>(__args
)...);
500 __throw_exception_again
;
507 // [23.2.2.1] construct/copy/destroy
508 // (assign() and get_allocator() are also listed in this section)
510 * @brief Default constructor creates no elements.
516 * @brief Creates a %list with no elements.
517 * @param a An allocator object.
520 list(const allocator_type
& __a
)
523 #ifdef __GXX_EXPERIMENTAL_CXX0X__
525 * @brief Creates a %list with default constructed elements.
526 * @param n The number of elements to initially create.
528 * This constructor fills the %list with @a n default
529 * constructed elements.
534 { _M_default_initialize(__n
); }
537 * @brief Creates a %list with copies of an exemplar element.
538 * @param n The number of elements to initially create.
539 * @param value An element to copy.
540 * @param a An allocator object.
542 * This constructor fills the %list with @a n copies of @a value.
544 list(size_type __n
, const value_type
& __value
,
545 const allocator_type
& __a
= allocator_type())
547 { _M_fill_initialize(__n
, __value
); }
550 * @brief Creates a %list with copies of an exemplar element.
551 * @param n The number of elements to initially create.
552 * @param value An element to copy.
553 * @param a An allocator object.
555 * This constructor fills the %list with @a n copies of @a value.
558 list(size_type __n
, const value_type
& __value
= value_type(),
559 const allocator_type
& __a
= allocator_type())
561 { _M_fill_initialize(__n
, __value
); }
565 * @brief %List copy constructor.
566 * @param x A %list of identical element and allocator types.
568 * The newly-created %list uses a copy of the allocation object used
571 list(const list
& __x
)
572 : _Base(__x
._M_get_Node_allocator())
573 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
575 #ifdef __GXX_EXPERIMENTAL_CXX0X__
577 * @brief %List move constructor.
578 * @param x A %list of identical element and allocator types.
580 * The newly-created %list contains the exact contents of @a x.
581 * The contents of @a x are a valid, but unspecified %list.
584 : _Base(std::move(__x
)) { }
587 * @brief Builds a %list from an initializer_list
588 * @param l An initializer_list of value_type.
589 * @param a An allocator object.
591 * Create a %list consisting of copies of the elements in the
592 * initializer_list @a l. This is linear in l.size().
594 list(initializer_list
<value_type
> __l
,
595 const allocator_type
& __a
= allocator_type())
597 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
601 * @brief Builds a %list from a range.
602 * @param first An input iterator.
603 * @param last An input iterator.
604 * @param a An allocator object.
606 * Create a %list consisting of copies of the elements from
607 * [@a first,@a last). This is linear in N (where N is
608 * distance(@a first,@a last)).
610 template<typename _InputIterator
>
611 list(_InputIterator __first
, _InputIterator __last
,
612 const allocator_type
& __a
= allocator_type())
615 // Check whether it's an integral type. If so, it's not an iterator.
616 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
617 _M_initialize_dispatch(__first
, __last
, _Integral());
621 * No explicit dtor needed as the _Base dtor takes care of
622 * things. The _Base dtor only erases the elements, and note
623 * that if the elements themselves are pointers, the pointed-to
624 * memory is not touched in any way. Managing the pointer is
625 * the user's responsibility.
629 * @brief %List assignment operator.
630 * @param x A %list of identical element and allocator types.
632 * All the elements of @a x are copied, but unlike the copy
633 * constructor, the allocator object is not copied.
636 operator=(const list
& __x
);
638 #ifdef __GXX_EXPERIMENTAL_CXX0X__
640 * @brief %List move assignment operator.
641 * @param x A %list of identical element and allocator types.
643 * The contents of @a x are moved into this %list (without copying).
644 * @a x is a valid, but unspecified %list
647 operator=(list
&& __x
)
657 * @brief %List initializer list assignment operator.
658 * @param l An initializer_list of value_type.
660 * Replace the contents of the %list with copies of the elements
661 * in the initializer_list @a l. This is linear in l.size().
664 operator=(initializer_list
<value_type
> __l
)
666 this->assign(__l
.begin(), __l
.end());
672 * @brief Assigns a given value to a %list.
673 * @param n Number of elements to be assigned.
674 * @param val Value to be assigned.
676 * This function fills a %list with @a n copies of the given
677 * value. Note that the assignment completely changes the %list
678 * and that the resulting %list's size is the same as the number
679 * of elements assigned. Old data may be lost.
682 assign(size_type __n
, const value_type
& __val
)
683 { _M_fill_assign(__n
, __val
); }
686 * @brief Assigns a range to a %list.
687 * @param first An input iterator.
688 * @param last An input iterator.
690 * This function fills a %list with copies of the elements in the
691 * range [@a first,@a last).
693 * Note that the assignment completely changes the %list and
694 * that the resulting %list's size is the same as the number of
695 * elements assigned. Old data may be lost.
697 template<typename _InputIterator
>
699 assign(_InputIterator __first
, _InputIterator __last
)
701 // Check whether it's an integral type. If so, it's not an iterator.
702 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
703 _M_assign_dispatch(__first
, __last
, _Integral());
706 #ifdef __GXX_EXPERIMENTAL_CXX0X__
708 * @brief Assigns an initializer_list to a %list.
709 * @param l An initializer_list of value_type.
711 * Replace the contents of the %list with copies of the elements
712 * in the initializer_list @a l. This is linear in l.size().
715 assign(initializer_list
<value_type
> __l
)
716 { this->assign(__l
.begin(), __l
.end()); }
719 /// Get a copy of the memory allocation object.
721 get_allocator() const _GLIBCXX_NOEXCEPT
722 { return _Base::get_allocator(); }
726 * Returns a read/write iterator that points to the first element in the
727 * %list. Iteration is done in ordinary element order.
730 begin() _GLIBCXX_NOEXCEPT
731 { return iterator(this->_M_impl
._M_node
._M_next
); }
734 * Returns a read-only (constant) iterator that points to the
735 * first element in the %list. Iteration is done in ordinary
739 begin() const _GLIBCXX_NOEXCEPT
740 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
743 * Returns a read/write iterator that points one past the last
744 * element in the %list. Iteration is done in ordinary element
748 end() _GLIBCXX_NOEXCEPT
749 { return iterator(&this->_M_impl
._M_node
); }
752 * Returns a read-only (constant) iterator that points one past
753 * the last element in the %list. Iteration is done in ordinary
757 end() const _GLIBCXX_NOEXCEPT
758 { return const_iterator(&this->_M_impl
._M_node
); }
761 * Returns a read/write reverse iterator that points to the last
762 * element in the %list. Iteration is done in reverse element
766 rbegin() _GLIBCXX_NOEXCEPT
767 { return reverse_iterator(end()); }
770 * Returns a read-only (constant) reverse iterator that points to
771 * the last element in the %list. Iteration is done in reverse
774 const_reverse_iterator
775 rbegin() const _GLIBCXX_NOEXCEPT
776 { return const_reverse_iterator(end()); }
779 * Returns a read/write reverse iterator that points to one
780 * before the first element in the %list. Iteration is done in
781 * reverse element order.
784 rend() _GLIBCXX_NOEXCEPT
785 { return reverse_iterator(begin()); }
788 * Returns a read-only (constant) reverse iterator that points to one
789 * before the first element in the %list. Iteration is done in reverse
792 const_reverse_iterator
793 rend() const _GLIBCXX_NOEXCEPT
794 { return const_reverse_iterator(begin()); }
796 #ifdef __GXX_EXPERIMENTAL_CXX0X__
798 * Returns a read-only (constant) iterator that points to the
799 * first element in the %list. Iteration is done in ordinary
803 cbegin() const noexcept
804 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
807 * Returns a read-only (constant) iterator that points one past
808 * the last element in the %list. Iteration is done in ordinary
812 cend() const noexcept
813 { return const_iterator(&this->_M_impl
._M_node
); }
816 * Returns a read-only (constant) reverse iterator that points to
817 * the last element in the %list. Iteration is done in reverse
820 const_reverse_iterator
821 crbegin() const noexcept
822 { return const_reverse_iterator(end()); }
825 * Returns a read-only (constant) reverse iterator that points to one
826 * before the first element in the %list. Iteration is done in reverse
829 const_reverse_iterator
830 crend() const noexcept
831 { return const_reverse_iterator(begin()); }
834 // [23.2.2.2] capacity
836 * Returns true if the %list is empty. (Thus begin() would equal
840 empty() const _GLIBCXX_NOEXCEPT
841 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
843 /** Returns the number of elements in the %list. */
845 size() const _GLIBCXX_NOEXCEPT
846 { return std::distance(begin(), end()); }
848 /** Returns the size() of the largest possible %list. */
850 max_size() const _GLIBCXX_NOEXCEPT
851 { return _M_get_Node_allocator().max_size(); }
853 #ifdef __GXX_EXPERIMENTAL_CXX0X__
855 * @brief Resizes the %list to the specified number of elements.
856 * @param new_size Number of elements the %list should contain.
858 * This function will %resize the %list to the specified number
859 * of elements. If the number is smaller than the %list's
860 * current size the %list is truncated, otherwise default
861 * constructed elements are appended.
864 resize(size_type __new_size
);
867 * @brief Resizes the %list to the specified number of elements.
868 * @param new_size Number of elements the %list should contain.
869 * @param x Data with which new elements should be populated.
871 * This function will %resize the %list to the specified number
872 * of elements. If the number is smaller than the %list's
873 * current size the %list is truncated, otherwise the %list is
874 * extended and new elements are populated with given data.
877 resize(size_type __new_size
, const value_type
& __x
);
880 * @brief Resizes the %list to the specified number of elements.
881 * @param new_size Number of elements the %list should contain.
882 * @param x Data with which new elements should be populated.
884 * This function will %resize the %list to the specified number
885 * of elements. If the number is smaller than the %list's
886 * current size the %list is truncated, otherwise the %list is
887 * extended and new elements are populated with given data.
890 resize(size_type __new_size
, value_type __x
= value_type());
895 * Returns a read/write reference to the data at the first
896 * element of the %list.
903 * Returns a read-only (constant) reference to the data at the first
904 * element of the %list.
911 * Returns a read/write reference to the data at the last element
917 iterator __tmp
= end();
923 * Returns a read-only (constant) reference to the data at the last
924 * element of the %list.
929 const_iterator __tmp
= end();
934 // [23.2.2.3] modifiers
936 * @brief Add data to the front of the %list.
937 * @param x Data to be added.
939 * This is a typical stack operation. The function creates an
940 * element at the front of the %list and assigns the given data
941 * to it. Due to the nature of a %list this operation can be
942 * done in constant time, and does not invalidate iterators and
946 push_front(const value_type
& __x
)
947 { this->_M_insert(begin(), __x
); }
949 #ifdef __GXX_EXPERIMENTAL_CXX0X__
951 push_front(value_type
&& __x
)
952 { this->_M_insert(begin(), std::move(__x
)); }
954 template<typename
... _Args
>
956 emplace_front(_Args
&&... __args
)
957 { this->_M_insert(begin(), std::forward
<_Args
>(__args
)...); }
961 * @brief Removes first element.
963 * This is a typical stack operation. It shrinks the %list by
964 * one. Due to the nature of a %list this operation can be done
965 * in constant time, and only invalidates iterators/references to
966 * the element being removed.
968 * Note that no data is returned, and if the first element's data
969 * is needed, it should be retrieved before pop_front() is
974 { this->_M_erase(begin()); }
977 * @brief Add data to the end of the %list.
978 * @param x Data to be added.
980 * This is a typical stack operation. The function creates an
981 * element at the end of the %list and assigns the given data to
982 * it. Due to the nature of a %list this operation can be done
983 * in constant time, and does not invalidate iterators and
987 push_back(const value_type
& __x
)
988 { this->_M_insert(end(), __x
); }
990 #ifdef __GXX_EXPERIMENTAL_CXX0X__
992 push_back(value_type
&& __x
)
993 { this->_M_insert(end(), std::move(__x
)); }
995 template<typename
... _Args
>
997 emplace_back(_Args
&&... __args
)
998 { this->_M_insert(end(), std::forward
<_Args
>(__args
)...); }
1002 * @brief Removes last element.
1004 * This is a typical stack operation. It shrinks the %list by
1005 * one. Due to the nature of a %list this operation can be done
1006 * in constant time, and only invalidates iterators/references to
1007 * the element being removed.
1009 * Note that no data is returned, and if the last element's data
1010 * is needed, it should be retrieved before pop_back() is called.
1014 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1016 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1018 * @brief Constructs object in %list before specified iterator.
1019 * @param position A const_iterator into the %list.
1020 * @param args Arguments.
1021 * @return An iterator that points to the inserted data.
1023 * This function will insert an object of type T constructed
1024 * with T(std::forward<Args>(args)...) before the specified
1025 * location. Due to the nature of a %list this operation can
1026 * be done in constant time, and does not invalidate iterators
1029 template<typename
... _Args
>
1031 emplace(iterator __position
, _Args
&&... __args
);
1035 * @brief Inserts given value into %list before specified iterator.
1036 * @param position An iterator into the %list.
1037 * @param x Data to be inserted.
1038 * @return An iterator that points to the inserted data.
1040 * This function will insert a copy of the given value before
1041 * the specified location. Due to the nature of a %list this
1042 * operation can be done in constant time, and does not
1043 * invalidate iterators and references.
1046 insert(iterator __position
, const value_type
& __x
);
1048 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1050 * @brief Inserts given rvalue into %list before specified iterator.
1051 * @param position An iterator into the %list.
1052 * @param x Data to be inserted.
1053 * @return An iterator that points to the inserted data.
1055 * This function will insert a copy of the given rvalue before
1056 * the specified location. Due to the nature of a %list this
1057 * operation can be done in constant time, and does not
1058 * invalidate iterators and references.
1061 insert(iterator __position
, value_type
&& __x
)
1062 { return emplace(__position
, std::move(__x
)); }
1065 * @brief Inserts the contents of an initializer_list into %list
1066 * before specified iterator.
1067 * @param p An iterator into the %list.
1068 * @param l An initializer_list of value_type.
1070 * This function will insert copies of the data in the
1071 * initializer_list @a l into the %list before the location
1072 * specified by @a p.
1074 * This operation is linear in the number of elements inserted and
1075 * does not invalidate iterators and references.
1078 insert(iterator __p
, initializer_list
<value_type
> __l
)
1079 { this->insert(__p
, __l
.begin(), __l
.end()); }
1083 * @brief Inserts a number of copies of given data into the %list.
1084 * @param position An iterator into the %list.
1085 * @param n Number of elements to be inserted.
1086 * @param x Data to be inserted.
1088 * This function will insert a specified number of copies of the
1089 * given data before the location specified by @a position.
1091 * This operation is linear in the number of elements inserted and
1092 * does not invalidate iterators and references.
1095 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1097 list
__tmp(__n
, __x
, _M_get_Node_allocator());
1098 splice(__position
, __tmp
);
1102 * @brief Inserts a range into the %list.
1103 * @param position An iterator into the %list.
1104 * @param first An input iterator.
1105 * @param last An input iterator.
1107 * This function will insert copies of the data in the range [@a
1108 * first,@a last) into the %list before the location specified by
1111 * This operation is linear in the number of elements inserted and
1112 * does not invalidate iterators and references.
1114 template<typename _InputIterator
>
1116 insert(iterator __position
, _InputIterator __first
,
1117 _InputIterator __last
)
1119 list
__tmp(__first
, __last
, _M_get_Node_allocator());
1120 splice(__position
, __tmp
);
1124 * @brief Remove element at given position.
1125 * @param position Iterator pointing to element to be erased.
1126 * @return An iterator pointing to the next element (or end()).
1128 * This function will erase the element at the given position and thus
1129 * shorten the %list by one.
1131 * Due to the nature of a %list this operation can be done in
1132 * constant time, and only invalidates iterators/references to
1133 * the element being removed. The user is also cautioned that
1134 * this function only erases the element, and that if the element
1135 * is itself a pointer, the pointed-to memory is not touched in
1136 * any way. Managing the pointer is the user's responsibility.
1139 erase(iterator __position
);
1142 * @brief Remove a range of elements.
1143 * @param first Iterator pointing to the first element to be erased.
1144 * @param last Iterator pointing to one past the last element to be
1146 * @return An iterator pointing to the element pointed to by @a last
1147 * prior to erasing (or end()).
1149 * This function will erase the elements in the range @a
1150 * [first,last) and shorten the %list accordingly.
1152 * This operation is linear time in the size of the range and only
1153 * invalidates iterators/references to the element being removed.
1154 * The user is also cautioned that this function only erases the
1155 * elements, and that if the elements themselves are pointers, the
1156 * pointed-to memory is not touched in any way. Managing the pointer
1157 * is the user's responsibility.
1160 erase(iterator __first
, iterator __last
)
1162 while (__first
!= __last
)
1163 __first
= erase(__first
);
1168 * @brief Swaps data with another %list.
1169 * @param x A %list of the same element and allocator types.
1171 * This exchanges the elements between two lists in constant
1172 * time. Note that the global std::swap() function is
1173 * specialized such that std::swap(l1,l2) will feed to this
1179 __detail::_List_node_base::swap(this->_M_impl
._M_node
,
1180 __x
._M_impl
._M_node
);
1182 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1183 // 431. Swapping containers with unequal allocators.
1184 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
1185 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
1189 * Erases all the elements. Note that this function only erases
1190 * the elements, and that if the elements themselves are
1191 * pointers, the pointed-to memory is not touched in any way.
1192 * Managing the pointer is the user's responsibility.
1195 clear() _GLIBCXX_NOEXCEPT
1201 // [23.2.2.4] list operations
1203 * @brief Insert contents of another %list.
1204 * @param position Iterator referencing the element to insert before.
1205 * @param x Source list.
1207 * The elements of @a x are inserted in constant time in front of
1208 * the element referenced by @a position. @a x becomes an empty
1211 * Requires this != @a x.
1214 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1215 splice(iterator __position
, list
&& __x
)
1217 splice(iterator __position
, list
& __x
)
1222 _M_check_equal_allocators(__x
);
1224 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1228 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1230 splice(iterator __position
, list
& __x
)
1231 { splice(__position
, std::move(__x
)); }
1235 * @brief Insert element from another %list.
1236 * @param position Iterator referencing the element to insert before.
1237 * @param x Source list.
1238 * @param i Iterator referencing the element to move.
1240 * Removes the element in list @a x referenced by @a i and
1241 * inserts it into the current list before @a position.
1244 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1245 splice(iterator __position
, list
&& __x
, iterator __i
)
1247 splice(iterator __position
, list
& __x
, iterator __i
)
1252 if (__position
== __i
|| __position
== __j
)
1256 _M_check_equal_allocators(__x
);
1258 this->_M_transfer(__position
, __i
, __j
);
1261 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1263 splice(iterator __position
, list
& __x
, iterator __i
)
1264 { splice(__position
, std::move(__x
), __i
); }
1268 * @brief Insert range from another %list.
1269 * @param position Iterator referencing the element to insert before.
1270 * @param x Source list.
1271 * @param first Iterator referencing the start of range in x.
1272 * @param last Iterator referencing the end of range in x.
1274 * Removes elements in the range [first,last) and inserts them
1275 * before @a position in constant time.
1277 * Undefined if @a position is in [first,last).
1280 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1281 splice(iterator __position
, list
&& __x
, iterator __first
,
1284 splice(iterator __position
, list
& __x
, iterator __first
,
1288 if (__first
!= __last
)
1291 _M_check_equal_allocators(__x
);
1293 this->_M_transfer(__position
, __first
, __last
);
1297 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1299 splice(iterator __position
, list
& __x
, iterator __first
, iterator __last
)
1300 { splice(__position
, std::move(__x
), __first
, __last
); }
1304 * @brief Remove all elements equal to value.
1305 * @param value The value to remove.
1307 * Removes every element in the list equal to @a value.
1308 * Remaining elements stay in list order. Note that this
1309 * function only erases the elements, and that if the elements
1310 * themselves are pointers, the pointed-to memory is not
1311 * touched in any way. Managing the pointer is the user's
1315 remove(const _Tp
& __value
);
1318 * @brief Remove all elements satisfying a predicate.
1319 * @param Predicate Unary predicate function or object.
1321 * Removes every element in the list for which the predicate
1322 * returns true. Remaining elements stay in list order. Note
1323 * that this function only erases the elements, and that if the
1324 * elements themselves are pointers, the pointed-to memory is
1325 * not touched in any way. Managing the pointer is the user's
1328 template<typename _Predicate
>
1330 remove_if(_Predicate
);
1333 * @brief Remove consecutive duplicate elements.
1335 * For each consecutive set of elements with the same value,
1336 * remove all but the first one. Remaining elements stay in
1337 * list order. Note that this function only erases the
1338 * elements, and that if the elements themselves are pointers,
1339 * the pointed-to memory is not touched in any way. Managing
1340 * the pointer is the user's responsibility.
1346 * @brief Remove consecutive elements satisfying a predicate.
1347 * @param BinaryPredicate Binary predicate function or object.
1349 * For each consecutive set of elements [first,last) that
1350 * satisfy predicate(first,i) where i is an iterator in
1351 * [first,last), remove all but the first one. Remaining
1352 * elements stay in list order. Note that this function only
1353 * erases the elements, and that if the elements themselves are
1354 * pointers, the pointed-to memory is not touched in any way.
1355 * Managing the pointer is the user's responsibility.
1357 template<typename _BinaryPredicate
>
1359 unique(_BinaryPredicate
);
1362 * @brief Merge sorted lists.
1363 * @param x Sorted list to merge.
1365 * Assumes that both @a x and this list are sorted according to
1366 * operator<(). Merges elements of @a x into this list in
1367 * sorted order, leaving @a x empty when complete. Elements in
1368 * this list precede elements in @a x that are equal.
1370 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1376 { merge(std::move(__x
)); }
1383 * @brief Merge sorted lists according to comparison function.
1384 * @param x Sorted list to merge.
1385 * @param StrictWeakOrdering Comparison function defining
1388 * Assumes that both @a x and this list are sorted according to
1389 * StrictWeakOrdering. Merges elements of @a x into this list
1390 * in sorted order, leaving @a x empty when complete. Elements
1391 * in this list precede elements in @a x that are equivalent
1392 * according to StrictWeakOrdering().
1394 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1395 template<typename _StrictWeakOrdering
>
1397 merge(list
&&, _StrictWeakOrdering
);
1399 template<typename _StrictWeakOrdering
>
1401 merge(list
& __x
, _StrictWeakOrdering __comp
)
1402 { merge(std::move(__x
), __comp
); }
1404 template<typename _StrictWeakOrdering
>
1406 merge(list
&, _StrictWeakOrdering
);
1410 * @brief Reverse the elements in list.
1412 * Reverse the order of elements in the list in linear time.
1415 reverse() _GLIBCXX_NOEXCEPT
1416 { this->_M_impl
._M_node
._M_reverse(); }
1419 * @brief Sort the elements.
1421 * Sorts the elements of this list in NlogN time. Equivalent
1422 * elements remain in list order.
1428 * @brief Sort the elements according to comparison function.
1430 * Sorts the elements of this list in NlogN time. Equivalent
1431 * elements remain in list order.
1433 template<typename _StrictWeakOrdering
>
1435 sort(_StrictWeakOrdering
);
1438 // Internal constructor functions follow.
1440 // Called by the range constructor to implement [23.1.1]/9
1442 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1443 // 438. Ambiguity in the "do the right thing" clause
1444 template<typename _Integer
>
1446 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1447 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1449 // Called by the range constructor to implement [23.1.1]/9
1450 template<typename _InputIterator
>
1452 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1455 for (; __first
!= __last
; ++__first
)
1456 push_back(*__first
);
1459 // Called by list(n,v,a), and the range constructor when it turns out
1460 // to be the same thing.
1462 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1468 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1469 // Called by list(n).
1471 _M_default_initialize(size_type __n
)
1477 // Called by resize(sz).
1479 _M_default_append(size_type __n
);
1482 // Internal assign functions follow.
1484 // Called by the range assign to implement [23.1.1]/9
1486 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1487 // 438. Ambiguity in the "do the right thing" clause
1488 template<typename _Integer
>
1490 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1491 { _M_fill_assign(__n
, __val
); }
1493 // Called by the range assign to implement [23.1.1]/9
1494 template<typename _InputIterator
>
1496 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1499 // Called by assign(n,t), and the range assign when it turns out
1500 // to be the same thing.
1502 _M_fill_assign(size_type __n
, const value_type
& __val
);
1505 // Moves the elements from [first,last) before position.
1507 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1508 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1510 // Inserts new element at position given and with value given.
1511 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1513 _M_insert(iterator __position
, const value_type
& __x
)
1515 _Node
* __tmp
= _M_create_node(__x
);
1516 __tmp
->_M_hook(__position
._M_node
);
1519 template<typename
... _Args
>
1521 _M_insert(iterator __position
, _Args
&&... __args
)
1523 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1524 __tmp
->_M_hook(__position
._M_node
);
1528 // Erases element at position given.
1530 _M_erase(iterator __position
)
1532 __position
._M_node
->_M_unhook();
1533 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1535 _M_get_Node_allocator().destroy(__n
);
1537 _M_get_Tp_allocator().destroy(std::__addressof(__n
->_M_data
));
1542 // To implement the splice (and merge) bits of N1599.
1544 _M_check_equal_allocators(list
& __x
)
1546 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1547 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1548 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1553 * @brief List equality comparison.
1555 * @param y A %list of the same type as @a x.
1556 * @return True iff the size and elements of the lists are equal.
1558 * This is an equivalence relation. It is linear in the size of
1559 * the lists. Lists are considered equivalent if their sizes are
1560 * equal, and if corresponding elements compare equal.
1562 template<typename _Tp
, typename _Alloc
>
1564 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1566 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1567 const_iterator __end1
= __x
.end();
1568 const_iterator __end2
= __y
.end();
1570 const_iterator __i1
= __x
.begin();
1571 const_iterator __i2
= __y
.begin();
1572 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1577 return __i1
== __end1
&& __i2
== __end2
;
1581 * @brief List ordering relation.
1583 * @param y A %list of the same type as @a x.
1584 * @return True iff @a x is lexicographically less than @a y.
1586 * This is a total ordering relation. It is linear in the size of the
1587 * lists. The elements must be comparable with @c <.
1589 * See std::lexicographical_compare() for how the determination is made.
1591 template<typename _Tp
, typename _Alloc
>
1593 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1594 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1595 __y
.begin(), __y
.end()); }
1597 /// Based on operator==
1598 template<typename _Tp
, typename _Alloc
>
1600 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1601 { return !(__x
== __y
); }
1603 /// Based on operator<
1604 template<typename _Tp
, typename _Alloc
>
1606 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1607 { return __y
< __x
; }
1609 /// Based on operator<
1610 template<typename _Tp
, typename _Alloc
>
1612 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1613 { return !(__y
< __x
); }
1615 /// Based on operator<
1616 template<typename _Tp
, typename _Alloc
>
1618 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1619 { return !(__x
< __y
); }
1621 /// See std::list::swap().
1622 template<typename _Tp
, typename _Alloc
>
1624 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1627 _GLIBCXX_END_NAMESPACE_CONTAINER
1630 #endif /* _STL_LIST_H */