1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // 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.
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
60 #include <bits/concept_check.h>
61 #include <initializer_list>
63 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
65 // Supporting structures are split into common and templated types; the
66 // latter publicly inherits from the former in an effort to reduce code
67 // duplication. This results in some "needless" static_cast'ing later on,
68 // but it's all safe downcasting.
70 /// Common part of a node in the %list.
71 struct _List_node_base
73 _List_node_base
* _M_next
;
74 _List_node_base
* _M_prev
;
77 swap(_List_node_base
& __x
, _List_node_base
& __y
) throw ();
80 _M_transfer(_List_node_base
* const __first
,
81 _List_node_base
* const __last
) throw ();
84 _M_reverse() throw ();
87 _M_hook(_List_node_base
* const __position
) throw ();
93 /// An actual node in the %list.
94 template<typename _Tp
>
95 struct _List_node
: public _List_node_base
100 #ifdef __GXX_EXPERIMENTAL_CXX0X__
101 template<typename
... _Args
>
102 _List_node(_Args
&&... __args
)
103 : _List_node_base(), _M_data(std::forward
<_Args
>(__args
)...) { }
108 * @brief A list::iterator.
110 * All the functions are op overloads.
112 template<typename _Tp
>
113 struct _List_iterator
115 typedef _List_iterator
<_Tp
> _Self
;
116 typedef _List_node
<_Tp
> _Node
;
118 typedef ptrdiff_t difference_type
;
119 typedef std::bidirectional_iterator_tag iterator_category
;
120 typedef _Tp value_type
;
121 typedef _Tp
* pointer
;
122 typedef _Tp
& reference
;
128 _List_iterator(_List_node_base
* __x
)
131 // Must downcast from _List_node_base to _List_node to get to _M_data.
134 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
138 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
143 _M_node
= _M_node
->_M_next
;
151 _M_node
= _M_node
->_M_next
;
158 _M_node
= _M_node
->_M_prev
;
166 _M_node
= _M_node
->_M_prev
;
171 operator==(const _Self
& __x
) const
172 { return _M_node
== __x
._M_node
; }
175 operator!=(const _Self
& __x
) const
176 { return _M_node
!= __x
._M_node
; }
178 // The only member points to the %list element.
179 _List_node_base
* _M_node
;
183 * @brief A list::const_iterator.
185 * All the functions are op overloads.
187 template<typename _Tp
>
188 struct _List_const_iterator
190 typedef _List_const_iterator
<_Tp
> _Self
;
191 typedef const _List_node
<_Tp
> _Node
;
192 typedef _List_iterator
<_Tp
> iterator
;
194 typedef ptrdiff_t difference_type
;
195 typedef std::bidirectional_iterator_tag iterator_category
;
196 typedef _Tp value_type
;
197 typedef const _Tp
* pointer
;
198 typedef const _Tp
& reference
;
200 _List_const_iterator()
204 _List_const_iterator(const _List_node_base
* __x
)
207 _List_const_iterator(const iterator
& __x
)
208 : _M_node(__x
._M_node
) { }
210 // Must downcast from List_node_base to _List_node to get to
214 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
218 { return std::__addressof(static_cast<_Node
*>(_M_node
)->_M_data
); }
223 _M_node
= _M_node
->_M_next
;
231 _M_node
= _M_node
->_M_next
;
238 _M_node
= _M_node
->_M_prev
;
246 _M_node
= _M_node
->_M_prev
;
251 operator==(const _Self
& __x
) const
252 { return _M_node
== __x
._M_node
; }
255 operator!=(const _Self
& __x
) const
256 { return _M_node
!= __x
._M_node
; }
258 // The only member points to the %list element.
259 const _List_node_base
* _M_node
;
262 template<typename _Val
>
264 operator==(const _List_iterator
<_Val
>& __x
,
265 const _List_const_iterator
<_Val
>& __y
)
266 { return __x
._M_node
== __y
._M_node
; }
268 template<typename _Val
>
270 operator!=(const _List_iterator
<_Val
>& __x
,
271 const _List_const_iterator
<_Val
>& __y
)
272 { return __x
._M_node
!= __y
._M_node
; }
275 /// See bits/stl_deque.h's _Deque_base for an explanation.
276 template<typename _Tp
, typename _Alloc
>
281 // The stored instance is not actually of "allocator_type"'s
282 // type. Instead we rebind the type to
283 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
284 // should probably be the same. List_node<Tp> is not the same
285 // size as Tp (it's two pointers larger), and specializations on
286 // Tp may go unused because List_node<Tp> is being bound
289 // We put this to the test in the constructors and in
290 // get_allocator, where we use conversions between
291 // allocator_type and _Node_alloc_type. The conversion is
292 // required by table 32 in [20.1.5].
293 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
296 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
299 : public _Node_alloc_type
301 _List_node_base _M_node
;
304 : _Node_alloc_type(), _M_node()
307 _List_impl(const _Node_alloc_type
& __a
)
308 : _Node_alloc_type(__a
), _M_node()
316 { return _M_impl
._Node_alloc_type::allocate(1); }
319 _M_put_node(_List_node
<_Tp
>* __p
)
320 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
323 typedef _Alloc allocator_type
;
326 _M_get_Node_allocator()
327 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
329 const _Node_alloc_type
&
330 _M_get_Node_allocator() const
331 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
334 _M_get_Tp_allocator() const
335 { return _Tp_alloc_type(_M_get_Node_allocator()); }
338 get_allocator() const
339 { return allocator_type(_M_get_Node_allocator()); }
345 _List_base(const allocator_type
& __a
)
349 #ifdef __GXX_EXPERIMENTAL_CXX0X__
350 _List_base(_List_base
&& __x
)
351 : _M_impl(__x
._M_get_Node_allocator())
354 _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
);
358 // This is what actually destroys the list.
368 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
369 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
374 * @brief A standard container with linear time access to elements,
375 * and fixed time insertion/deletion at any point in the sequence.
379 * Meets the requirements of a <a href="tables.html#65">container</a>, a
380 * <a href="tables.html#66">reversible container</a>, and a
381 * <a href="tables.html#67">sequence</a>, including the
382 * <a href="tables.html#68">optional sequence requirements</a> with the
383 * %exception of @c at and @c operator[].
385 * This is a @e doubly @e linked %list. Traversal up and down the
386 * %list requires linear time, but adding and removing elements (or
387 * @e nodes) is done in constant time, regardless of where the
388 * change takes place. Unlike std::vector and std::deque,
389 * random-access iterators are not provided, so subscripting ( @c
390 * [] ) access is not allowed. For algorithms which only need
391 * sequential access, this lack makes no difference.
393 * Also unlike the other standard containers, std::list provides
394 * specialized algorithms %unique to linked lists, such as
395 * splicing, sorting, and in-place reversal.
397 * A couple points on memory allocation for list<Tp>:
399 * First, we never actually allocate a Tp, we allocate
400 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
401 * that after elements from %list<X,Alloc1> are spliced into
402 * %list<X,Alloc2>, destroying the memory of the second %list is a
403 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
405 * Second, a %list conceptually represented as
407 * A <---> B <---> C <---> D
409 * is actually circular; a link exists between A and D. The %list
410 * class holds (as its only data member) a private list::iterator
411 * pointing to @e D, not to @e A! To get to the head of the %list,
412 * we start at the tail and move forward by one. When this member
413 * iterator's next/previous pointers refer to itself, the %list is
416 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
417 class list
: protected _List_base
<_Tp
, _Alloc
>
419 // concept requirements
420 typedef typename
_Alloc::value_type _Alloc_value_type
;
421 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
422 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
424 typedef _List_base
<_Tp
, _Alloc
> _Base
;
425 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
428 typedef _Tp value_type
;
429 typedef typename
_Tp_alloc_type::pointer pointer
;
430 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
431 typedef typename
_Tp_alloc_type::reference reference
;
432 typedef typename
_Tp_alloc_type::const_reference const_reference
;
433 typedef _List_iterator
<_Tp
> iterator
;
434 typedef _List_const_iterator
<_Tp
> const_iterator
;
435 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
436 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
437 typedef size_t size_type
;
438 typedef ptrdiff_t difference_type
;
439 typedef _Alloc allocator_type
;
442 // Note that pointers-to-_Node's can be ctor-converted to
444 typedef _List_node
<_Tp
> _Node
;
446 using _Base::_M_impl
;
447 using _Base::_M_put_node
;
448 using _Base::_M_get_node
;
449 using _Base::_M_get_Tp_allocator
;
450 using _Base::_M_get_Node_allocator
;
453 * @param x An instance of user data.
455 * Allocates space for a new node and constructs a copy of @a x in it.
457 #ifndef __GXX_EXPERIMENTAL_CXX0X__
459 _M_create_node(const value_type
& __x
)
461 _Node
* __p
= this->_M_get_node();
464 _M_get_Tp_allocator().construct
465 (std::__addressof(__p
->_M_data
), __x
);
470 __throw_exception_again
;
475 template<typename
... _Args
>
477 _M_create_node(_Args
&&... __args
)
479 _Node
* __p
= this->_M_get_node();
482 _M_get_Node_allocator().construct(__p
,
483 std::forward
<_Args
>(__args
)...);
488 __throw_exception_again
;
495 // [23.2.2.1] construct/copy/destroy
496 // (assign() and get_allocator() are also listed in this section)
498 * @brief Default constructor creates no elements.
504 * @brief Creates a %list with no elements.
505 * @param a An allocator object.
508 list(const allocator_type
& __a
)
511 #ifdef __GXX_EXPERIMENTAL_CXX0X__
513 * @brief Creates a %list with default constructed elements.
514 * @param n The number of elements to initially create.
516 * This constructor fills the %list with @a n default
517 * constructed elements.
522 { _M_default_initialize(__n
); }
525 * @brief Creates a %list with copies of an exemplar element.
526 * @param n The number of elements to initially create.
527 * @param value An element to copy.
528 * @param a An allocator object.
530 * This constructor fills the %list with @a n copies of @a value.
532 list(size_type __n
, const value_type
& __value
,
533 const allocator_type
& __a
= allocator_type())
535 { _M_fill_initialize(__n
, __value
); }
538 * @brief Creates a %list with copies of an exemplar element.
539 * @param n The number of elements to initially create.
540 * @param value An element to copy.
541 * @param a An allocator object.
543 * This constructor fills the %list with @a n copies of @a value.
546 list(size_type __n
, const value_type
& __value
= value_type(),
547 const allocator_type
& __a
= allocator_type())
549 { _M_fill_initialize(__n
, __value
); }
553 * @brief %List copy constructor.
554 * @param x A %list of identical element and allocator types.
556 * The newly-created %list uses a copy of the allocation object used
559 list(const list
& __x
)
560 : _Base(__x
._M_get_Node_allocator())
561 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
563 #ifdef __GXX_EXPERIMENTAL_CXX0X__
565 * @brief %List move constructor.
566 * @param x A %list of identical element and allocator types.
568 * The newly-created %list contains the exact contents of @a x.
569 * The contents of @a x are a valid, but unspecified %list.
572 : _Base(std::move(__x
)) { }
575 * @brief Builds a %list from an initializer_list
576 * @param l An initializer_list of value_type.
577 * @param a An allocator object.
579 * Create a %list consisting of copies of the elements in the
580 * initializer_list @a l. This is linear in l.size().
582 list(initializer_list
<value_type
> __l
,
583 const allocator_type
& __a
= allocator_type())
585 { _M_initialize_dispatch(__l
.begin(), __l
.end(), __false_type()); }
589 * @brief Builds a %list from a range.
590 * @param first An input iterator.
591 * @param last An input iterator.
592 * @param a An allocator object.
594 * Create a %list consisting of copies of the elements from
595 * [@a first,@a last). This is linear in N (where N is
596 * distance(@a first,@a last)).
598 template<typename _InputIterator
>
599 list(_InputIterator __first
, _InputIterator __last
,
600 const allocator_type
& __a
= allocator_type())
603 // Check whether it's an integral type. If so, it's not an iterator.
604 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
605 _M_initialize_dispatch(__first
, __last
, _Integral());
609 * No explicit dtor needed as the _Base dtor takes care of
610 * things. The _Base dtor only erases the elements, and note
611 * that if the elements themselves are pointers, the pointed-to
612 * memory is not touched in any way. Managing the pointer is
613 * the user's responsibility.
617 * @brief %List assignment operator.
618 * @param x A %list of identical element and allocator types.
620 * All the elements of @a x are copied, but unlike the copy
621 * constructor, the allocator object is not copied.
624 operator=(const list
& __x
);
626 #ifdef __GXX_EXPERIMENTAL_CXX0X__
628 * @brief %List move assignment operator.
629 * @param x A %list of identical element and allocator types.
631 * The contents of @a x are moved into this %list (without copying).
632 * @a x is a valid, but unspecified %list
635 operator=(list
&& __x
)
645 * @brief %List initializer list assignment operator.
646 * @param l An initializer_list of value_type.
648 * Replace the contents of the %list with copies of the elements
649 * in the initializer_list @a l. This is linear in l.size().
652 operator=(initializer_list
<value_type
> __l
)
654 this->assign(__l
.begin(), __l
.end());
660 * @brief Assigns a given value to a %list.
661 * @param n Number of elements to be assigned.
662 * @param val Value to be assigned.
664 * This function fills a %list with @a n copies of the given
665 * value. Note that the assignment completely changes the %list
666 * and that the resulting %list's size is the same as the number
667 * of elements assigned. Old data may be lost.
670 assign(size_type __n
, const value_type
& __val
)
671 { _M_fill_assign(__n
, __val
); }
674 * @brief Assigns a range to a %list.
675 * @param first An input iterator.
676 * @param last An input iterator.
678 * This function fills a %list with copies of the elements in the
679 * range [@a first,@a last).
681 * Note that the assignment completely changes the %list and
682 * that the resulting %list's size is the same as the number of
683 * elements assigned. Old data may be lost.
685 template<typename _InputIterator
>
687 assign(_InputIterator __first
, _InputIterator __last
)
689 // Check whether it's an integral type. If so, it's not an iterator.
690 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
691 _M_assign_dispatch(__first
, __last
, _Integral());
694 #ifdef __GXX_EXPERIMENTAL_CXX0X__
696 * @brief Assigns an initializer_list to a %list.
697 * @param l An initializer_list of value_type.
699 * Replace the contents of the %list with copies of the elements
700 * in the initializer_list @a l. This is linear in l.size().
703 assign(initializer_list
<value_type
> __l
)
704 { this->assign(__l
.begin(), __l
.end()); }
707 /// Get a copy of the memory allocation object.
709 get_allocator() const
710 { return _Base::get_allocator(); }
714 * Returns a read/write iterator that points to the first element in the
715 * %list. Iteration is done in ordinary element order.
719 { return iterator(this->_M_impl
._M_node
._M_next
); }
722 * Returns a read-only (constant) iterator that points to the
723 * first element in the %list. Iteration is done in ordinary
728 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
731 * Returns a read/write iterator that points one past the last
732 * element in the %list. Iteration is done in ordinary element
737 { return iterator(&this->_M_impl
._M_node
); }
740 * Returns a read-only (constant) iterator that points one past
741 * the last element in the %list. Iteration is done in ordinary
746 { return const_iterator(&this->_M_impl
._M_node
); }
749 * Returns a read/write reverse iterator that points to the last
750 * element in the %list. Iteration is done in reverse element
755 { return reverse_iterator(end()); }
758 * Returns a read-only (constant) reverse iterator that points to
759 * the last element in the %list. Iteration is done in reverse
762 const_reverse_iterator
764 { return const_reverse_iterator(end()); }
767 * Returns a read/write reverse iterator that points to one
768 * before the first element in the %list. Iteration is done in
769 * reverse element order.
773 { return reverse_iterator(begin()); }
776 * Returns a read-only (constant) reverse iterator that points to one
777 * before the first element in the %list. Iteration is done in reverse
780 const_reverse_iterator
782 { return const_reverse_iterator(begin()); }
784 #ifdef __GXX_EXPERIMENTAL_CXX0X__
786 * Returns a read-only (constant) iterator that points to the
787 * first element in the %list. Iteration is done in ordinary
792 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
795 * Returns a read-only (constant) iterator that points one past
796 * the last element in the %list. Iteration is done in ordinary
801 { return const_iterator(&this->_M_impl
._M_node
); }
804 * Returns a read-only (constant) reverse iterator that points to
805 * the last element in the %list. Iteration is done in reverse
808 const_reverse_iterator
810 { return const_reverse_iterator(end()); }
813 * Returns a read-only (constant) reverse iterator that points to one
814 * before the first element in the %list. Iteration is done in reverse
817 const_reverse_iterator
819 { return const_reverse_iterator(begin()); }
822 // [23.2.2.2] capacity
824 * Returns true if the %list is empty. (Thus begin() would equal
829 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
831 /** Returns the number of elements in the %list. */
834 { return std::distance(begin(), end()); }
836 /** Returns the size() of the largest possible %list. */
839 { return _M_get_Node_allocator().max_size(); }
841 #ifdef __GXX_EXPERIMENTAL_CXX0X__
843 * @brief Resizes the %list to the specified number of elements.
844 * @param new_size Number of elements the %list should contain.
846 * This function will %resize the %list to the specified number
847 * of elements. If the number is smaller than the %list's
848 * current size the %list is truncated, otherwise default
849 * constructed elements are appended.
852 resize(size_type __new_size
);
855 * @brief Resizes the %list to the specified number of elements.
856 * @param new_size Number of elements the %list should contain.
857 * @param x Data with which new elements should be populated.
859 * This function will %resize the %list to the specified number
860 * of elements. If the number is smaller than the %list's
861 * current size the %list is truncated, otherwise the %list is
862 * extended and new elements are populated with given data.
865 resize(size_type __new_size
, const value_type
& __x
);
868 * @brief Resizes the %list to the specified number of elements.
869 * @param new_size Number of elements the %list should contain.
870 * @param x Data with which new elements should be populated.
872 * This function will %resize the %list to the specified number
873 * of elements. If the number is smaller than the %list's
874 * current size the %list is truncated, otherwise the %list is
875 * extended and new elements are populated with given data.
878 resize(size_type __new_size
, value_type __x
= value_type());
883 * Returns a read/write reference to the data at the first
884 * element of the %list.
891 * Returns a read-only (constant) reference to the data at the first
892 * element of the %list.
899 * Returns a read/write reference to the data at the last element
905 iterator __tmp
= end();
911 * Returns a read-only (constant) reference to the data at the last
912 * element of the %list.
917 const_iterator __tmp
= end();
922 // [23.2.2.3] modifiers
924 * @brief Add data to the front of the %list.
925 * @param x Data to be added.
927 * This is a typical stack operation. The function creates an
928 * element at the front of the %list and assigns the given data
929 * to it. Due to the nature of a %list this operation can be
930 * done in constant time, and does not invalidate iterators and
934 push_front(const value_type
& __x
)
935 { this->_M_insert(begin(), __x
); }
937 #ifdef __GXX_EXPERIMENTAL_CXX0X__
939 push_front(value_type
&& __x
)
940 { this->_M_insert(begin(), std::move(__x
)); }
942 template<typename
... _Args
>
944 emplace_front(_Args
&&... __args
)
945 { this->_M_insert(begin(), std::forward
<_Args
>(__args
)...); }
949 * @brief Removes first element.
951 * This is a typical stack operation. It shrinks the %list by
952 * one. Due to the nature of a %list this operation can be done
953 * in constant time, and only invalidates iterators/references to
954 * the element being removed.
956 * Note that no data is returned, and if the first element's data
957 * is needed, it should be retrieved before pop_front() is
962 { this->_M_erase(begin()); }
965 * @brief Add data to the end of the %list.
966 * @param x Data to be added.
968 * This is a typical stack operation. The function creates an
969 * element at the end of the %list and assigns the given data to
970 * it. Due to the nature of a %list this operation can be done
971 * in constant time, and does not invalidate iterators and
975 push_back(const value_type
& __x
)
976 { this->_M_insert(end(), __x
); }
978 #ifdef __GXX_EXPERIMENTAL_CXX0X__
980 push_back(value_type
&& __x
)
981 { this->_M_insert(end(), std::move(__x
)); }
983 template<typename
... _Args
>
985 emplace_back(_Args
&&... __args
)
986 { this->_M_insert(end(), std::forward
<_Args
>(__args
)...); }
990 * @brief Removes last element.
992 * This is a typical stack operation. It shrinks the %list by
993 * one. Due to the nature of a %list this operation can be done
994 * in constant time, and only invalidates iterators/references to
995 * the element being removed.
997 * Note that no data is returned, and if the last element's data
998 * is needed, it should be retrieved before pop_back() is called.
1002 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
1004 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1006 * @brief Constructs object in %list before specified iterator.
1007 * @param position A const_iterator into the %list.
1008 * @param args Arguments.
1009 * @return An iterator that points to the inserted data.
1011 * This function will insert an object of type T constructed
1012 * with T(std::forward<Args>(args)...) before the specified
1013 * location. Due to the nature of a %list this operation can
1014 * be done in constant time, and does not invalidate iterators
1017 template<typename
... _Args
>
1019 emplace(iterator __position
, _Args
&&... __args
);
1023 * @brief Inserts given value into %list before specified iterator.
1024 * @param position An iterator into the %list.
1025 * @param x Data to be inserted.
1026 * @return An iterator that points to the inserted data.
1028 * This function will insert a copy of the given value before
1029 * the specified location. Due to the nature of a %list this
1030 * operation can be done in constant time, and does not
1031 * invalidate iterators and references.
1034 insert(iterator __position
, const value_type
& __x
);
1036 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1038 * @brief Inserts given rvalue into %list before specified iterator.
1039 * @param position An iterator into the %list.
1040 * @param x Data to be inserted.
1041 * @return An iterator that points to the inserted data.
1043 * This function will insert a copy of the given rvalue before
1044 * the specified location. Due to the nature of a %list this
1045 * operation can be done in constant time, and does not
1046 * invalidate iterators and references.
1049 insert(iterator __position
, value_type
&& __x
)
1050 { return emplace(__position
, std::move(__x
)); }
1053 * @brief Inserts the contents of an initializer_list into %list
1054 * before specified iterator.
1055 * @param p An iterator into the %list.
1056 * @param l An initializer_list of value_type.
1058 * This function will insert copies of the data in the
1059 * initializer_list @a l into the %list before the location
1060 * specified by @a p.
1062 * This operation is linear in the number of elements inserted and
1063 * does not invalidate iterators and references.
1066 insert(iterator __p
, initializer_list
<value_type
> __l
)
1067 { this->insert(__p
, __l
.begin(), __l
.end()); }
1071 * @brief Inserts a number of copies of given data into the %list.
1072 * @param position An iterator into the %list.
1073 * @param n Number of elements to be inserted.
1074 * @param x Data to be inserted.
1076 * This function will insert a specified number of copies of the
1077 * given data before the location specified by @a position.
1079 * This operation is linear in the number of elements inserted and
1080 * does not invalidate iterators and references.
1083 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1085 list
__tmp(__n
, __x
, _M_get_Node_allocator());
1086 splice(__position
, __tmp
);
1090 * @brief Inserts a range into the %list.
1091 * @param position An iterator into the %list.
1092 * @param first An input iterator.
1093 * @param last An input iterator.
1095 * This function will insert copies of the data in the range [@a
1096 * first,@a last) into the %list before the location specified by
1099 * This operation is linear in the number of elements inserted and
1100 * does not invalidate iterators and references.
1102 template<typename _InputIterator
>
1104 insert(iterator __position
, _InputIterator __first
,
1105 _InputIterator __last
)
1107 list
__tmp(__first
, __last
, _M_get_Node_allocator());
1108 splice(__position
, __tmp
);
1112 * @brief Remove element at given position.
1113 * @param position Iterator pointing to element to be erased.
1114 * @return An iterator pointing to the next element (or end()).
1116 * This function will erase the element at the given position and thus
1117 * shorten the %list by one.
1119 * Due to the nature of a %list this operation can be done in
1120 * constant time, and only invalidates iterators/references to
1121 * the element being removed. The user is also cautioned that
1122 * this function only erases the element, and that if the element
1123 * is itself a pointer, the pointed-to memory is not touched in
1124 * any way. Managing the pointer is the user's responsibility.
1127 erase(iterator __position
);
1130 * @brief Remove a range of elements.
1131 * @param first Iterator pointing to the first element to be erased.
1132 * @param last Iterator pointing to one past the last element to be
1134 * @return An iterator pointing to the element pointed to by @a last
1135 * prior to erasing (or end()).
1137 * This function will erase the elements in the range @a
1138 * [first,last) and shorten the %list accordingly.
1140 * This operation is linear time in the size of the range and only
1141 * invalidates iterators/references to the element being removed.
1142 * The user is also cautioned that this function only erases the
1143 * elements, and that if the elements themselves are pointers, the
1144 * pointed-to memory is not touched in any way. Managing the pointer
1145 * is the user's responsibility.
1148 erase(iterator __first
, iterator __last
)
1150 while (__first
!= __last
)
1151 __first
= erase(__first
);
1156 * @brief Swaps data with another %list.
1157 * @param x A %list of the same element and allocator types.
1159 * This exchanges the elements between two lists in constant
1160 * time. Note that the global std::swap() function is
1161 * specialized such that std::swap(l1,l2) will feed to this
1167 _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
);
1169 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1170 // 431. Swapping containers with unequal allocators.
1171 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
1172 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
1176 * Erases all the elements. Note that this function only erases
1177 * the elements, and that if the elements themselves are
1178 * pointers, the pointed-to memory is not touched in any way.
1179 * Managing the pointer is the user's responsibility.
1188 // [23.2.2.4] list operations
1190 * @brief Insert contents of another %list.
1191 * @param position Iterator referencing the element to insert before.
1192 * @param x Source list.
1194 * The elements of @a x are inserted in constant time in front of
1195 * the element referenced by @a position. @a x becomes an empty
1198 * Requires this != @a x.
1201 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1202 splice(iterator __position
, list
&& __x
)
1204 splice(iterator __position
, list
& __x
)
1209 _M_check_equal_allocators(__x
);
1211 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1215 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1217 splice(iterator __position
, list
& __x
)
1218 { splice(__position
, std::move(__x
)); }
1222 * @brief Insert element from another %list.
1223 * @param position Iterator referencing the element to insert before.
1224 * @param x Source list.
1225 * @param i Iterator referencing the element to move.
1227 * Removes the element in list @a x referenced by @a i and
1228 * inserts it into the current list before @a position.
1231 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1232 splice(iterator __position
, list
&& __x
, iterator __i
)
1234 splice(iterator __position
, list
& __x
, iterator __i
)
1239 if (__position
== __i
|| __position
== __j
)
1243 _M_check_equal_allocators(__x
);
1245 this->_M_transfer(__position
, __i
, __j
);
1248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1250 splice(iterator __position
, list
& __x
, iterator __i
)
1251 { splice(__position
, std::move(__x
), __i
); }
1255 * @brief Insert range from another %list.
1256 * @param position Iterator referencing the element to insert before.
1257 * @param x Source list.
1258 * @param first Iterator referencing the start of range in x.
1259 * @param last Iterator referencing the end of range in x.
1261 * Removes elements in the range [first,last) and inserts them
1262 * before @a position in constant time.
1264 * Undefined if @a position is in [first,last).
1267 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1268 splice(iterator __position
, list
&& __x
, iterator __first
,
1271 splice(iterator __position
, list
& __x
, iterator __first
,
1275 if (__first
!= __last
)
1278 _M_check_equal_allocators(__x
);
1280 this->_M_transfer(__position
, __first
, __last
);
1284 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1286 splice(iterator __position
, list
& __x
, iterator __first
, iterator __last
)
1287 { splice(__position
, std::move(__x
), __first
, __last
); }
1291 * @brief Remove all elements equal to value.
1292 * @param value The value to remove.
1294 * Removes every element in the list equal to @a value.
1295 * Remaining elements stay in list order. Note that this
1296 * function only erases the elements, and that if the elements
1297 * themselves are pointers, the pointed-to memory is not
1298 * touched in any way. Managing the pointer is the user's
1302 remove(const _Tp
& __value
);
1305 * @brief Remove all elements satisfying a predicate.
1306 * @param Predicate Unary predicate function or object.
1308 * Removes every element in the list for which the predicate
1309 * returns true. Remaining elements stay in list order. Note
1310 * that this function only erases the elements, and that if the
1311 * elements themselves are pointers, the pointed-to memory is
1312 * not touched in any way. Managing the pointer is the user's
1315 template<typename _Predicate
>
1317 remove_if(_Predicate
);
1320 * @brief Remove consecutive duplicate elements.
1322 * For each consecutive set of elements with the same value,
1323 * remove all but the first one. Remaining elements stay in
1324 * list order. Note that this function only erases the
1325 * elements, and that if the elements themselves are pointers,
1326 * the pointed-to memory is not touched in any way. Managing
1327 * the pointer is the user's responsibility.
1333 * @brief Remove consecutive elements satisfying a predicate.
1334 * @param BinaryPredicate Binary predicate function or object.
1336 * For each consecutive set of elements [first,last) that
1337 * satisfy predicate(first,i) where i is an iterator in
1338 * [first,last), remove all but the first one. Remaining
1339 * elements stay in list order. Note that this function only
1340 * erases the elements, and that if the elements themselves are
1341 * pointers, the pointed-to memory is not touched in any way.
1342 * Managing the pointer is the user's responsibility.
1344 template<typename _BinaryPredicate
>
1346 unique(_BinaryPredicate
);
1349 * @brief Merge sorted lists.
1350 * @param x Sorted list to merge.
1352 * Assumes that both @a x and this list are sorted according to
1353 * operator<(). Merges elements of @a x into this list in
1354 * sorted order, leaving @a x empty when complete. Elements in
1355 * this list precede elements in @a x that are equal.
1357 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1363 { merge(std::move(__x
)); }
1370 * @brief Merge sorted lists according to comparison function.
1371 * @param x Sorted list to merge.
1372 * @param StrictWeakOrdering Comparison function defining
1375 * Assumes that both @a x and this list are sorted according to
1376 * StrictWeakOrdering. Merges elements of @a x into this list
1377 * in sorted order, leaving @a x empty when complete. Elements
1378 * in this list precede elements in @a x that are equivalent
1379 * according to StrictWeakOrdering().
1381 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1382 template<typename _StrictWeakOrdering
>
1384 merge(list
&&, _StrictWeakOrdering
);
1386 template<typename _StrictWeakOrdering
>
1388 merge(list
& __x
, _StrictWeakOrdering __comp
)
1389 { merge(std::move(__x
), __comp
); }
1391 template<typename _StrictWeakOrdering
>
1393 merge(list
&, _StrictWeakOrdering
);
1397 * @brief Reverse the elements in list.
1399 * Reverse the order of elements in the list in linear time.
1403 { this->_M_impl
._M_node
._M_reverse(); }
1406 * @brief Sort the elements.
1408 * Sorts the elements of this list in NlogN time. Equivalent
1409 * elements remain in list order.
1415 * @brief Sort the elements according to comparison function.
1417 * Sorts the elements of this list in NlogN time. Equivalent
1418 * elements remain in list order.
1420 template<typename _StrictWeakOrdering
>
1422 sort(_StrictWeakOrdering
);
1425 // Internal constructor functions follow.
1427 // Called by the range constructor to implement [23.1.1]/9
1429 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1430 // 438. Ambiguity in the "do the right thing" clause
1431 template<typename _Integer
>
1433 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1434 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1436 // Called by the range constructor to implement [23.1.1]/9
1437 template<typename _InputIterator
>
1439 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1442 for (; __first
!= __last
; ++__first
)
1443 push_back(*__first
);
1446 // Called by list(n,v,a), and the range constructor when it turns out
1447 // to be the same thing.
1449 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1455 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1456 // Called by list(n).
1458 _M_default_initialize(size_type __n
)
1464 // Called by resize(sz).
1466 _M_default_append(size_type __n
);
1469 // Internal assign functions follow.
1471 // Called by the range assign to implement [23.1.1]/9
1473 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1474 // 438. Ambiguity in the "do the right thing" clause
1475 template<typename _Integer
>
1477 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1478 { _M_fill_assign(__n
, __val
); }
1480 // Called by the range assign to implement [23.1.1]/9
1481 template<typename _InputIterator
>
1483 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1486 // Called by assign(n,t), and the range assign when it turns out
1487 // to be the same thing.
1489 _M_fill_assign(size_type __n
, const value_type
& __val
);
1492 // Moves the elements from [first,last) before position.
1494 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1495 { __position
._M_node
->_M_transfer(__first
._M_node
, __last
._M_node
); }
1497 // Inserts new element at position given and with value given.
1498 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1500 _M_insert(iterator __position
, const value_type
& __x
)
1502 _Node
* __tmp
= _M_create_node(__x
);
1503 __tmp
->_M_hook(__position
._M_node
);
1506 template<typename
... _Args
>
1508 _M_insert(iterator __position
, _Args
&&... __args
)
1510 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1511 __tmp
->_M_hook(__position
._M_node
);
1515 // Erases element at position given.
1517 _M_erase(iterator __position
)
1519 __position
._M_node
->_M_unhook();
1520 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1521 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1522 _M_get_Node_allocator().destroy(__n
);
1524 _M_get_Tp_allocator().destroy(std::__addressof(__n
->_M_data
));
1529 // To implement the splice (and merge) bits of N1599.
1531 _M_check_equal_allocators(list
& __x
)
1533 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1534 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1535 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1540 * @brief List equality comparison.
1542 * @param y A %list of the same type as @a x.
1543 * @return True iff the size and elements of the lists are equal.
1545 * This is an equivalence relation. It is linear in the size of
1546 * the lists. Lists are considered equivalent if their sizes are
1547 * equal, and if corresponding elements compare equal.
1549 template<typename _Tp
, typename _Alloc
>
1551 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1553 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1554 const_iterator __end1
= __x
.end();
1555 const_iterator __end2
= __y
.end();
1557 const_iterator __i1
= __x
.begin();
1558 const_iterator __i2
= __y
.begin();
1559 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1564 return __i1
== __end1
&& __i2
== __end2
;
1568 * @brief List ordering relation.
1570 * @param y A %list of the same type as @a x.
1571 * @return True iff @a x is lexicographically less than @a y.
1573 * This is a total ordering relation. It is linear in the size of the
1574 * lists. The elements must be comparable with @c <.
1576 * See std::lexicographical_compare() for how the determination is made.
1578 template<typename _Tp
, typename _Alloc
>
1580 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1581 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1582 __y
.begin(), __y
.end()); }
1584 /// Based on operator==
1585 template<typename _Tp
, typename _Alloc
>
1587 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1588 { return !(__x
== __y
); }
1590 /// Based on operator<
1591 template<typename _Tp
, typename _Alloc
>
1593 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1594 { return __y
< __x
; }
1596 /// Based on operator<
1597 template<typename _Tp
, typename _Alloc
>
1599 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1600 { return !(__y
< __x
); }
1602 /// Based on operator<
1603 template<typename _Tp
, typename _Alloc
>
1605 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1606 { return !(__x
< __y
); }
1608 /// See std::list::swap().
1609 template<typename _Tp
, typename _Alloc
>
1611 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1614 _GLIBCXX_END_NESTED_NAMESPACE
1616 #endif /* _STL_LIST_H */