1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
65 #include <bits/concept_check.h>
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std
, _GLIBCXX_STD_D
)
69 // Supporting structures are split into common and templated types; the
70 // latter publicly inherits from the former in an effort to reduce code
71 // duplication. This results in some "needless" static_cast'ing later on,
72 // but it's all safe downcasting.
74 /// Common part of a node in the %list.
75 struct _List_node_base
77 _List_node_base
* _M_next
;
78 _List_node_base
* _M_prev
;
81 swap(_List_node_base
& __x
, _List_node_base
& __y
);
84 transfer(_List_node_base
* const __first
,
85 _List_node_base
* const __last
);
91 hook(_List_node_base
* const __position
);
97 /// An actual node in the %list.
98 template<typename _Tp
>
99 struct _List_node
: public _List_node_base
106 * @brief A list::iterator.
108 * All the functions are op overloads.
110 template<typename _Tp
>
111 struct _List_iterator
113 typedef _List_iterator
<_Tp
> _Self
;
114 typedef _List_node
<_Tp
> _Node
;
116 typedef ptrdiff_t difference_type
;
117 typedef std::bidirectional_iterator_tag iterator_category
;
118 typedef _Tp value_type
;
119 typedef _Tp
* pointer
;
120 typedef _Tp
& reference
;
126 _List_iterator(_List_node_base
* __x
)
129 // Must downcast from List_node_base to _List_node to get to _M_data.
132 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
136 { return &static_cast<_Node
*>(_M_node
)->_M_data
; }
141 _M_node
= _M_node
->_M_next
;
149 _M_node
= _M_node
->_M_next
;
156 _M_node
= _M_node
->_M_prev
;
164 _M_node
= _M_node
->_M_prev
;
169 operator==(const _Self
& __x
) const
170 { return _M_node
== __x
._M_node
; }
173 operator!=(const _Self
& __x
) const
174 { return _M_node
!= __x
._M_node
; }
176 // The only member points to the %list element.
177 _List_node_base
* _M_node
;
181 * @brief A list::const_iterator.
183 * All the functions are op overloads.
185 template<typename _Tp
>
186 struct _List_const_iterator
188 typedef _List_const_iterator
<_Tp
> _Self
;
189 typedef const _List_node
<_Tp
> _Node
;
190 typedef _List_iterator
<_Tp
> iterator
;
192 typedef ptrdiff_t difference_type
;
193 typedef std::bidirectional_iterator_tag iterator_category
;
194 typedef _Tp value_type
;
195 typedef const _Tp
* pointer
;
196 typedef const _Tp
& reference
;
198 _List_const_iterator()
202 _List_const_iterator(const _List_node_base
* __x
)
205 _List_const_iterator(const iterator
& __x
)
206 : _M_node(__x
._M_node
) { }
208 // Must downcast from List_node_base to _List_node to get to
212 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
216 { return &static_cast<_Node
*>(_M_node
)->_M_data
; }
221 _M_node
= _M_node
->_M_next
;
229 _M_node
= _M_node
->_M_next
;
236 _M_node
= _M_node
->_M_prev
;
244 _M_node
= _M_node
->_M_prev
;
249 operator==(const _Self
& __x
) const
250 { return _M_node
== __x
._M_node
; }
253 operator!=(const _Self
& __x
) const
254 { return _M_node
!= __x
._M_node
; }
256 // The only member points to the %list element.
257 const _List_node_base
* _M_node
;
260 template<typename _Val
>
262 operator==(const _List_iterator
<_Val
>& __x
,
263 const _List_const_iterator
<_Val
>& __y
)
264 { return __x
._M_node
== __y
._M_node
; }
266 template<typename _Val
>
268 operator!=(const _List_iterator
<_Val
>& __x
,
269 const _List_const_iterator
<_Val
>& __y
)
270 { return __x
._M_node
!= __y
._M_node
; }
273 /// See bits/stl_deque.h's _Deque_base for an explanation.
274 template<typename _Tp
, typename _Alloc
>
279 // The stored instance is not actually of "allocator_type"'s
280 // type. Instead we rebind the type to
281 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
282 // should probably be the same. List_node<Tp> is not the same
283 // size as Tp (it's two pointers larger), and specializations on
284 // Tp may go unused because List_node<Tp> is being bound
287 // We put this to the test in the constructors and in
288 // get_allocator, where we use conversions between
289 // allocator_type and _Node_alloc_type. The conversion is
290 // required by table 32 in [20.1.5].
291 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
294 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
297 : public _Node_alloc_type
299 _List_node_base _M_node
;
302 : _Node_alloc_type(), _M_node()
305 _List_impl(const _Node_alloc_type
& __a
)
306 : _Node_alloc_type(__a
), _M_node()
314 { return _M_impl
._Node_alloc_type::allocate(1); }
317 _M_put_node(_List_node
<_Tp
>* __p
)
318 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
321 typedef _Alloc allocator_type
;
324 _M_get_Node_allocator()
325 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
327 const _Node_alloc_type
&
328 _M_get_Node_allocator() const
329 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
332 _M_get_Tp_allocator() const
333 { return _Tp_alloc_type(_M_get_Node_allocator()); }
336 get_allocator() const
337 { return allocator_type(_M_get_Node_allocator()); }
343 _List_base(const allocator_type
& __a
)
347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
348 _List_base(_List_base
&& __x
)
349 : _M_impl(__x
._M_get_Node_allocator())
352 _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
);
356 // This is what actually destroys the list.
366 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
367 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
372 * @brief A standard container with linear time access to elements,
373 * and fixed time insertion/deletion at any point in the sequence.
375 * @ingroup Containers
378 * Meets the requirements of a <a href="tables.html#65">container</a>, a
379 * <a href="tables.html#66">reversible container</a>, and a
380 * <a href="tables.html#67">sequence</a>, including the
381 * <a href="tables.html#68">optional sequence requirements</a> with the
382 * %exception of @c at and @c operator[].
384 * This is a @e doubly @e linked %list. Traversal up and down the
385 * %list requires linear time, but adding and removing elements (or
386 * @e nodes) is done in constant time, regardless of where the
387 * change takes place. Unlike std::vector and std::deque,
388 * random-access iterators are not provided, so subscripting ( @c
389 * [] ) access is not allowed. For algorithms which only need
390 * sequential access, this lack makes no difference.
392 * Also unlike the other standard containers, std::list provides
393 * specialized algorithms %unique to linked lists, such as
394 * splicing, sorting, and in-place reversal.
396 * A couple points on memory allocation for list<Tp>:
398 * First, we never actually allocate a Tp, we allocate
399 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
400 * that after elements from %list<X,Alloc1> are spliced into
401 * %list<X,Alloc2>, destroying the memory of the second %list is a
402 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
404 * Second, a %list conceptually represented as
406 * A <---> B <---> C <---> D
408 * is actually circular; a link exists between A and D. The %list
409 * class holds (as its only data member) a private list::iterator
410 * pointing to @e D, not to @e A! To get to the head of the %list,
411 * we start at the tail and move forward by one. When this member
412 * iterator's next/previous pointers refer to itself, the %list is
415 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
416 class list
: protected _List_base
<_Tp
, _Alloc
>
418 // concept requirements
419 typedef typename
_Alloc::value_type _Alloc_value_type
;
420 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
421 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
423 typedef _List_base
<_Tp
, _Alloc
> _Base
;
424 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
427 typedef _Tp value_type
;
428 typedef typename
_Tp_alloc_type::pointer pointer
;
429 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
430 typedef typename
_Tp_alloc_type::reference reference
;
431 typedef typename
_Tp_alloc_type::const_reference const_reference
;
432 typedef _List_iterator
<_Tp
> iterator
;
433 typedef _List_const_iterator
<_Tp
> const_iterator
;
434 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
435 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
436 typedef size_t size_type
;
437 typedef ptrdiff_t difference_type
;
438 typedef _Alloc allocator_type
;
441 // Note that pointers-to-_Node's can be ctor-converted to
443 typedef _List_node
<_Tp
> _Node
;
445 using _Base::_M_impl
;
446 using _Base::_M_put_node
;
447 using _Base::_M_get_node
;
448 using _Base::_M_get_Tp_allocator
;
449 using _Base::_M_get_Node_allocator
;
452 * @param x An instance of user data.
454 * Allocates space for a new node and constructs a copy of @a x in it.
456 #ifndef __GXX_EXPERIMENTAL_CXX0X__
458 _M_create_node(const value_type
& __x
)
460 _Node
* __p
= this->_M_get_node();
463 _M_get_Tp_allocator().construct(&__p
->_M_data
, __x
);
468 __throw_exception_again
;
473 template<typename
... _Args
>
475 _M_create_node(_Args
&&... __args
)
477 _Node
* __p
= this->_M_get_node();
480 _M_get_Tp_allocator().construct(&__p
->_M_data
,
481 std::forward
<_Args
>(__args
)...);
486 __throw_exception_again
;
493 // [23.2.2.1] construct/copy/destroy
494 // (assign() and get_allocator() are also listed in this section)
496 * @brief Default constructor creates no elements.
502 * @brief Creates a %list with no elements.
503 * @param a An allocator object.
506 list(const allocator_type
& __a
)
510 * @brief Creates a %list with copies of an exemplar element.
511 * @param n The number of elements to initially create.
512 * @param value An element to copy.
513 * @param a An allocator object.
515 * This constructor fills the %list with @a n copies of @a value.
518 list(size_type __n
, const value_type
& __value
= value_type(),
519 const allocator_type
& __a
= allocator_type())
521 { _M_fill_initialize(__n
, __value
); }
524 * @brief %List copy constructor.
525 * @param x A %list of identical element and allocator types.
527 * The newly-created %list uses a copy of the allocation object used
530 list(const list
& __x
)
531 : _Base(__x
._M_get_Node_allocator())
532 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
536 * @brief %List move constructor.
537 * @param x A %list of identical element and allocator types.
539 * The newly-created %list contains the exact contents of @a x.
540 * The contents of @a x are a valid, but unspecified %list.
543 : _Base(std::forward
<_Base
>(__x
)) { }
547 * @brief Builds a %list from a range.
548 * @param first An input iterator.
549 * @param last An input iterator.
550 * @param a An allocator object.
552 * Create a %list consisting of copies of the elements from
553 * [@a first,@a last). This is linear in N (where N is
554 * distance(@a first,@a last)).
556 template<typename _InputIterator
>
557 list(_InputIterator __first
, _InputIterator __last
,
558 const allocator_type
& __a
= allocator_type())
561 // Check whether it's an integral type. If so, it's not an iterator.
562 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
563 _M_initialize_dispatch(__first
, __last
, _Integral());
567 * No explicit dtor needed as the _Base dtor takes care of
568 * things. The _Base dtor only erases the elements, and note
569 * that if the elements themselves are pointers, the pointed-to
570 * memory is not touched in any way. Managing the pointer is
571 * the user's responsibilty.
575 * @brief %List assignment operator.
576 * @param x A %list of identical element and allocator types.
578 * All the elements of @a x are copied, but unlike the copy
579 * constructor, the allocator object is not copied.
582 operator=(const list
& __x
);
584 #ifdef __GXX_EXPERIMENTAL_CXX0X__
586 * @brief %List move assignment operator.
587 * @param x A %list of identical element and allocator types.
589 * The contents of @a x are moved into this %list (without copying).
590 * @a x is a valid, but unspecified %list
593 operator=(list
&& __x
)
603 * @brief Assigns a given value to a %list.
604 * @param n Number of elements to be assigned.
605 * @param val Value to be assigned.
607 * This function fills a %list with @a n copies of the given
608 * value. Note that the assignment completely changes the %list
609 * and that the resulting %list's size is the same as the number
610 * of elements assigned. Old data may be lost.
613 assign(size_type __n
, const value_type
& __val
)
614 { _M_fill_assign(__n
, __val
); }
617 * @brief Assigns a range to a %list.
618 * @param first An input iterator.
619 * @param last An input iterator.
621 * This function fills a %list with copies of the elements in the
622 * range [@a first,@a last).
624 * Note that the assignment completely changes the %list and
625 * that the resulting %list's size is the same as the number of
626 * elements assigned. Old data may be lost.
628 template<typename _InputIterator
>
630 assign(_InputIterator __first
, _InputIterator __last
)
632 // Check whether it's an integral type. If so, it's not an iterator.
633 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
634 _M_assign_dispatch(__first
, __last
, _Integral());
637 /// Get a copy of the memory allocation object.
639 get_allocator() const
640 { return _Base::get_allocator(); }
644 * Returns a read/write iterator that points to the first element in the
645 * %list. Iteration is done in ordinary element order.
649 { return iterator(this->_M_impl
._M_node
._M_next
); }
652 * Returns a read-only (constant) iterator that points to the
653 * first element in the %list. Iteration is done in ordinary
658 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
661 * Returns a read/write iterator that points one past the last
662 * element in the %list. Iteration is done in ordinary element
667 { return iterator(&this->_M_impl
._M_node
); }
670 * Returns a read-only (constant) iterator that points one past
671 * the last element in the %list. Iteration is done in ordinary
676 { return const_iterator(&this->_M_impl
._M_node
); }
679 * Returns a read/write reverse iterator that points to the last
680 * element in the %list. Iteration is done in reverse element
685 { return reverse_iterator(end()); }
688 * Returns a read-only (constant) reverse iterator that points to
689 * the last element in the %list. Iteration is done in reverse
692 const_reverse_iterator
694 { return const_reverse_iterator(end()); }
697 * Returns a read/write reverse iterator that points to one
698 * before the first element in the %list. Iteration is done in
699 * reverse element order.
703 { return reverse_iterator(begin()); }
706 * Returns a read-only (constant) reverse iterator that points to one
707 * before the first element in the %list. Iteration is done in reverse
710 const_reverse_iterator
712 { return const_reverse_iterator(begin()); }
714 #ifdef __GXX_EXPERIMENTAL_CXX0X__
716 * Returns a read-only (constant) iterator that points to the
717 * first element in the %list. Iteration is done in ordinary
722 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
725 * Returns a read-only (constant) iterator that points one past
726 * the last element in the %list. Iteration is done in ordinary
731 { return const_iterator(&this->_M_impl
._M_node
); }
734 * Returns a read-only (constant) reverse iterator that points to
735 * the last element in the %list. Iteration is done in reverse
738 const_reverse_iterator
740 { return const_reverse_iterator(end()); }
743 * Returns a read-only (constant) reverse iterator that points to one
744 * before the first element in the %list. Iteration is done in reverse
747 const_reverse_iterator
749 { return const_reverse_iterator(begin()); }
752 // [23.2.2.2] capacity
754 * Returns true if the %list is empty. (Thus begin() would equal
759 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
761 /** Returns the number of elements in the %list. */
764 { return std::distance(begin(), end()); }
766 /** Returns the size() of the largest possible %list. */
769 { return _M_get_Tp_allocator().max_size(); }
772 * @brief Resizes the %list to the specified number of elements.
773 * @param new_size Number of elements the %list should contain.
774 * @param x Data with which new elements should be populated.
776 * This function will %resize the %list to the specified number
777 * of elements. If the number is smaller than the %list's
778 * current size the %list is truncated, otherwise the %list is
779 * extended and new elements are populated with given data.
782 resize(size_type __new_size
, value_type __x
= value_type());
786 * Returns a read/write reference to the data at the first
787 * element of the %list.
794 * Returns a read-only (constant) reference to the data at the first
795 * element of the %list.
802 * Returns a read/write reference to the data at the last element
808 iterator __tmp
= end();
814 * Returns a read-only (constant) reference to the data at the last
815 * element of the %list.
820 const_iterator __tmp
= end();
825 // [23.2.2.3] modifiers
827 * @brief Add data to the front of the %list.
828 * @param x Data to be added.
830 * This is a typical stack operation. The function creates an
831 * element at the front of the %list and assigns the given data
832 * to it. Due to the nature of a %list this operation can be
833 * done in constant time, and does not invalidate iterators and
836 #ifndef __GXX_EXPERIMENTAL_CXX0X__
838 push_front(const value_type
& __x
)
839 { this->_M_insert(begin(), __x
); }
841 template<typename
... _Args
>
843 push_front(_Args
&&... __args
)
844 { this->_M_insert(begin(), std::forward
<_Args
>(__args
)...); }
848 * @brief Removes first element.
850 * This is a typical stack operation. It shrinks the %list by
851 * one. Due to the nature of a %list this operation can be done
852 * in constant time, and only invalidates iterators/references to
853 * the element being removed.
855 * Note that no data is returned, and if the first element's data
856 * is needed, it should be retrieved before pop_front() is
861 { this->_M_erase(begin()); }
864 * @brief Add data to the end of the %list.
865 * @param x Data to be added.
867 * This is a typical stack operation. The function creates an
868 * element at the end of the %list and assigns the given data to
869 * it. Due to the nature of a %list this operation can be done
870 * in constant time, and does not invalidate iterators and
873 #ifndef __GXX_EXPERIMENTAL_CXX0X__
875 push_back(const value_type
& __x
)
876 { this->_M_insert(end(), __x
); }
878 template<typename
... _Args
>
880 push_back(_Args
&&... __args
)
881 { this->_M_insert(end(), std::forward
<_Args
>(__args
)...); }
885 * @brief Removes last element.
887 * This is a typical stack operation. It shrinks the %list by
888 * one. Due to the nature of a %list this operation can be done
889 * in constant time, and only invalidates iterators/references to
890 * the element being removed.
892 * Note that no data is returned, and if the last element's data
893 * is needed, it should be retrieved before pop_back() is called.
897 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
899 #ifdef __GXX_EXPERIMENTAL_CXX0X__
901 * @brief Constructs object in %list before specified iterator.
902 * @param position A const_iterator into the %list.
903 * @param args Arguments.
904 * @return An iterator that points to the inserted data.
906 * This function will insert an object of type T constructed
907 * with T(std::forward<Args>(args)...) before the specified
908 * location. Due to the nature of a %list this operation can
909 * be done in constant time, and does not invalidate iterators
912 template<typename
... _Args
>
914 emplace(iterator __position
, _Args
&&... __args
);
918 * @brief Inserts given value into %list before specified iterator.
919 * @param position An iterator into the %list.
920 * @param x Data to be inserted.
921 * @return An iterator that points to the inserted data.
923 * This function will insert a copy of the given value before
924 * the specified location. Due to the nature of a %list this
925 * operation can be done in constant time, and does not
926 * invalidate iterators and references.
929 insert(iterator __position
, const value_type
& __x
);
931 #ifdef __GXX_EXPERIMENTAL_CXX0X__
933 * @brief Inserts given rvalue into %list before specified iterator.
934 * @param position An iterator into the %list.
935 * @param x Data to be inserted.
936 * @return An iterator that points to the inserted data.
938 * This function will insert a copy of the given rvalue before
939 * the specified location. Due to the nature of a %list this
940 * operation can be done in constant time, and does not
941 * invalidate iterators and references.
944 insert(iterator __position
, value_type
&& __x
)
945 { return emplace(__position
, std::move(__x
)); }
949 * @brief Inserts a number of copies of given data into the %list.
950 * @param position An iterator into the %list.
951 * @param n Number of elements to be inserted.
952 * @param x Data to be inserted.
954 * This function will insert a specified number of copies of the
955 * given data before the location specified by @a position.
957 * This operation is linear in the number of elements inserted and
958 * does not invalidate iterators and references.
961 insert(iterator __position
, size_type __n
, const value_type
& __x
)
963 list
__tmp(__n
, __x
, _M_get_Node_allocator());
964 splice(__position
, __tmp
);
968 * @brief Inserts a range into the %list.
969 * @param position An iterator into the %list.
970 * @param first An input iterator.
971 * @param last An input iterator.
973 * This function will insert copies of the data in the range [@a
974 * first,@a last) into the %list before the location specified by
977 * This operation is linear in the number of elements inserted and
978 * does not invalidate iterators and references.
980 template<typename _InputIterator
>
982 insert(iterator __position
, _InputIterator __first
,
983 _InputIterator __last
)
985 list
__tmp(__first
, __last
, _M_get_Node_allocator());
986 splice(__position
, __tmp
);
990 * @brief Remove element at given position.
991 * @param position Iterator pointing to element to be erased.
992 * @return An iterator pointing to the next element (or end()).
994 * This function will erase the element at the given position and thus
995 * shorten the %list by one.
997 * Due to the nature of a %list this operation can be done in
998 * constant time, and only invalidates iterators/references to
999 * the element being removed. The user is also cautioned that
1000 * this function only erases the element, and that if the element
1001 * is itself a pointer, the pointed-to memory is not touched in
1002 * any way. Managing the pointer is the user's responsibilty.
1005 erase(iterator __position
);
1008 * @brief Remove a range of elements.
1009 * @param first Iterator pointing to the first element to be erased.
1010 * @param last Iterator pointing to one past the last element to be
1012 * @return An iterator pointing to the element pointed to by @a last
1013 * prior to erasing (or end()).
1015 * This function will erase the elements in the range @a
1016 * [first,last) and shorten the %list accordingly.
1018 * This operation is linear time in the size of the range and only
1019 * invalidates iterators/references to the element being removed.
1020 * The user is also cautioned that this function only erases the
1021 * elements, and that if the elements themselves are pointers, the
1022 * pointed-to memory is not touched in any way. Managing the pointer
1023 * is the user's responsibilty.
1026 erase(iterator __first
, iterator __last
)
1028 while (__first
!= __last
)
1029 __first
= erase(__first
);
1034 * @brief Swaps data with another %list.
1035 * @param x A %list of the same element and allocator types.
1037 * This exchanges the elements between two lists in constant
1038 * time. Note that the global std::swap() function is
1039 * specialized such that std::swap(l1,l2) will feed to this
1043 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1049 _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
);
1051 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1052 // 431. Swapping containers with unequal allocators.
1053 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
1054 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
1058 * Erases all the elements. Note that this function only erases
1059 * the elements, and that if the elements themselves are
1060 * pointers, the pointed-to memory is not touched in any way.
1061 * Managing the pointer is the user's responsibilty.
1070 // [23.2.2.4] list operations
1072 * @brief Insert contents of another %list.
1073 * @param position Iterator referencing the element to insert before.
1074 * @param x Source list.
1076 * The elements of @a x are inserted in constant time in front of
1077 * the element referenced by @a position. @a x becomes an empty
1080 * Requires this != @a x.
1083 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1084 splice(iterator __position
, list
&& __x
)
1086 splice(iterator __position
, list
& __x
)
1091 _M_check_equal_allocators(__x
);
1093 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1098 * @brief Insert element from another %list.
1099 * @param position Iterator referencing the element to insert before.
1100 * @param x Source list.
1101 * @param i Iterator referencing the element to move.
1103 * Removes the element in list @a x referenced by @a i and
1104 * inserts it into the current list before @a position.
1107 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1108 splice(iterator __position
, list
&& __x
, iterator __i
)
1110 splice(iterator __position
, list
& __x
, iterator __i
)
1115 if (__position
== __i
|| __position
== __j
)
1119 _M_check_equal_allocators(__x
);
1121 this->_M_transfer(__position
, __i
, __j
);
1125 * @brief Insert range from another %list.
1126 * @param position Iterator referencing the element to insert before.
1127 * @param x Source list.
1128 * @param first Iterator referencing the start of range in x.
1129 * @param last Iterator referencing the end of range in x.
1131 * Removes elements in the range [first,last) and inserts them
1132 * before @a position in constant time.
1134 * Undefined if @a position is in [first,last).
1137 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1138 splice(iterator __position
, list
&& __x
, iterator __first
,
1141 splice(iterator __position
, list
& __x
, iterator __first
,
1145 if (__first
!= __last
)
1148 _M_check_equal_allocators(__x
);
1150 this->_M_transfer(__position
, __first
, __last
);
1155 * @brief Remove all elements equal to value.
1156 * @param value The value to remove.
1158 * Removes every element in the list equal to @a value.
1159 * Remaining elements stay in list order. Note that this
1160 * function only erases the elements, and that if the elements
1161 * themselves are pointers, the pointed-to memory is not
1162 * touched in any way. Managing the pointer is the user's
1166 remove(const _Tp
& __value
);
1169 * @brief Remove all elements satisfying a predicate.
1170 * @param Predicate Unary predicate function or object.
1172 * Removes every element in the list for which the predicate
1173 * returns true. Remaining elements stay in list order. Note
1174 * that this function only erases the elements, and that if the
1175 * elements themselves are pointers, the pointed-to memory is
1176 * not touched in any way. Managing the pointer is the user's
1179 template<typename _Predicate
>
1181 remove_if(_Predicate
);
1184 * @brief Remove consecutive duplicate elements.
1186 * For each consecutive set of elements with the same value,
1187 * remove all but the first one. Remaining elements stay in
1188 * list order. Note that this function only erases the
1189 * elements, and that if the elements themselves are pointers,
1190 * the pointed-to memory is not touched in any way. Managing
1191 * the pointer is the user's responsibilty.
1197 * @brief Remove consecutive elements satisfying a predicate.
1198 * @param BinaryPredicate Binary predicate function or object.
1200 * For each consecutive set of elements [first,last) that
1201 * satisfy predicate(first,i) where i is an iterator in
1202 * [first,last), remove all but the first one. Remaining
1203 * elements stay in list order. Note that this function only
1204 * erases the elements, and that if the elements themselves are
1205 * pointers, the pointed-to memory is not touched in any way.
1206 * Managing the pointer is the user's responsibilty.
1208 template<typename _BinaryPredicate
>
1210 unique(_BinaryPredicate
);
1213 * @brief Merge sorted lists.
1214 * @param x Sorted list to merge.
1216 * Assumes that both @a x and this list are sorted according to
1217 * operator<(). Merges elements of @a x into this list in
1218 * sorted order, leaving @a x empty when complete. Elements in
1219 * this list precede elements in @a x that are equal.
1222 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1229 * @brief Merge sorted lists according to comparison function.
1230 * @param x Sorted list to merge.
1231 * @param StrictWeakOrdering Comparison function definining
1234 * Assumes that both @a x and this list are sorted according to
1235 * StrictWeakOrdering. Merges elements of @a x into this list
1236 * in sorted order, leaving @a x empty when complete. Elements
1237 * in this list precede elements in @a x that are equivalent
1238 * according to StrictWeakOrdering().
1240 template<typename _StrictWeakOrdering
>
1242 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1243 merge(list
&&, _StrictWeakOrdering
);
1245 merge(list
&, _StrictWeakOrdering
);
1249 * @brief Reverse the elements in list.
1251 * Reverse the order of elements in the list in linear time.
1255 { this->_M_impl
._M_node
.reverse(); }
1258 * @brief Sort the elements.
1260 * Sorts the elements of this list in NlogN time. Equivalent
1261 * elements remain in list order.
1267 * @brief Sort the elements according to comparison function.
1269 * Sorts the elements of this list in NlogN time. Equivalent
1270 * elements remain in list order.
1272 template<typename _StrictWeakOrdering
>
1274 sort(_StrictWeakOrdering
);
1277 // Internal constructor functions follow.
1279 // Called by the range constructor to implement [23.1.1]/9
1281 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1282 // 438. Ambiguity in the "do the right thing" clause
1283 template<typename _Integer
>
1285 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1286 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1288 // Called by the range constructor to implement [23.1.1]/9
1289 template<typename _InputIterator
>
1291 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1294 for (; __first
!= __last
; ++__first
)
1295 push_back(*__first
);
1298 // Called by list(n,v,a), and the range constructor when it turns out
1299 // to be the same thing.
1301 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1303 for (; __n
> 0; --__n
)
1308 // Internal assign functions follow.
1310 // Called by the range assign to implement [23.1.1]/9
1312 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1313 // 438. Ambiguity in the "do the right thing" clause
1314 template<typename _Integer
>
1316 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1317 { _M_fill_assign(__n
, __val
); }
1319 // Called by the range assign to implement [23.1.1]/9
1320 template<typename _InputIterator
>
1322 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1325 // Called by assign(n,t), and the range assign when it turns out
1326 // to be the same thing.
1328 _M_fill_assign(size_type __n
, const value_type
& __val
);
1331 // Moves the elements from [first,last) before position.
1333 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1334 { __position
._M_node
->transfer(__first
._M_node
, __last
._M_node
); }
1336 // Inserts new element at position given and with value given.
1337 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1339 _M_insert(iterator __position
, const value_type
& __x
)
1341 _Node
* __tmp
= _M_create_node(__x
);
1342 __tmp
->hook(__position
._M_node
);
1345 template<typename
... _Args
>
1347 _M_insert(iterator __position
, _Args
&&... __args
)
1349 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1350 __tmp
->hook(__position
._M_node
);
1354 // Erases element at position given.
1356 _M_erase(iterator __position
)
1358 __position
._M_node
->unhook();
1359 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1360 _M_get_Tp_allocator().destroy(&__n
->_M_data
);
1364 // To implement the splice (and merge) bits of N1599.
1366 _M_check_equal_allocators(list
& __x
)
1368 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1369 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1370 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1375 * @brief List equality comparison.
1377 * @param y A %list of the same type as @a x.
1378 * @return True iff the size and elements of the lists are equal.
1380 * This is an equivalence relation. It is linear in the size of
1381 * the lists. Lists are considered equivalent if their sizes are
1382 * equal, and if corresponding elements compare equal.
1384 template<typename _Tp
, typename _Alloc
>
1386 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1388 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1389 const_iterator __end1
= __x
.end();
1390 const_iterator __end2
= __y
.end();
1392 const_iterator __i1
= __x
.begin();
1393 const_iterator __i2
= __y
.begin();
1394 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1399 return __i1
== __end1
&& __i2
== __end2
;
1403 * @brief List ordering relation.
1405 * @param y A %list of the same type as @a x.
1406 * @return True iff @a x is lexicographically less than @a y.
1408 * This is a total ordering relation. It is linear in the size of the
1409 * lists. The elements must be comparable with @c <.
1411 * See std::lexicographical_compare() for how the determination is made.
1413 template<typename _Tp
, typename _Alloc
>
1415 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1416 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1417 __y
.begin(), __y
.end()); }
1419 /// Based on operator==
1420 template<typename _Tp
, typename _Alloc
>
1422 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1423 { return !(__x
== __y
); }
1425 /// Based on operator<
1426 template<typename _Tp
, typename _Alloc
>
1428 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1429 { return __y
< __x
; }
1431 /// Based on operator<
1432 template<typename _Tp
, typename _Alloc
>
1434 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1435 { return !(__y
< __x
); }
1437 /// Based on operator<
1438 template<typename _Tp
, typename _Alloc
>
1440 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1441 { return !(__x
< __y
); }
1443 /// See std::list::swap().
1444 template<typename _Tp
, typename _Alloc
>
1446 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1449 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1450 template<typename _Tp
, typename _Alloc
>
1452 swap(list
<_Tp
, _Alloc
>&& __x
, list
<_Tp
, _Alloc
>& __y
)
1455 template<typename _Tp
, typename _Alloc
>
1457 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>&& __y
)
1461 _GLIBCXX_END_NESTED_NAMESPACE
1463 #endif /* _STL_LIST_H */