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 responsibility.
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
837 push_front(const value_type
& __x
)
838 { this->_M_insert(begin(), __x
); }
840 #ifdef __GXX_EXPERIMENTAL_CXX0X__
842 push_front(value_type
&& __x
)
843 { this->_M_insert(begin(), std::move(__x
)); }
845 template<typename
... _Args
>
847 emplace_front(_Args
&&... __args
)
848 { this->_M_insert(begin(), std::forward
<_Args
>(__args
)...); }
852 * @brief Removes first element.
854 * This is a typical stack operation. It shrinks the %list by
855 * one. Due to the nature of a %list this operation can be done
856 * in constant time, and only invalidates iterators/references to
857 * the element being removed.
859 * Note that no data is returned, and if the first element's data
860 * is needed, it should be retrieved before pop_front() is
865 { this->_M_erase(begin()); }
868 * @brief Add data to the end of the %list.
869 * @param x Data to be added.
871 * This is a typical stack operation. The function creates an
872 * element at the end of the %list and assigns the given data to
873 * it. Due to the nature of a %list this operation can be done
874 * in constant time, and does not invalidate iterators and
878 push_back(const value_type
& __x
)
879 { this->_M_insert(end(), __x
); }
881 #ifdef __GXX_EXPERIMENTAL_CXX0X__
883 push_back(value_type
&& __x
)
884 { this->_M_insert(end(), std::move(__x
)); }
886 template<typename
... _Args
>
888 emplace_back(_Args
&&... __args
)
889 { this->_M_insert(end(), std::forward
<_Args
>(__args
)...); }
893 * @brief Removes last element.
895 * This is a typical stack operation. It shrinks the %list by
896 * one. Due to the nature of a %list this operation can be done
897 * in constant time, and only invalidates iterators/references to
898 * the element being removed.
900 * Note that no data is returned, and if the last element's data
901 * is needed, it should be retrieved before pop_back() is called.
905 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
907 #ifdef __GXX_EXPERIMENTAL_CXX0X__
909 * @brief Constructs object in %list before specified iterator.
910 * @param position A const_iterator into the %list.
911 * @param args Arguments.
912 * @return An iterator that points to the inserted data.
914 * This function will insert an object of type T constructed
915 * with T(std::forward<Args>(args)...) before the specified
916 * location. Due to the nature of a %list this operation can
917 * be done in constant time, and does not invalidate iterators
920 template<typename
... _Args
>
922 emplace(iterator __position
, _Args
&&... __args
);
926 * @brief Inserts given value into %list before specified iterator.
927 * @param position An iterator into the %list.
928 * @param x Data to be inserted.
929 * @return An iterator that points to the inserted data.
931 * This function will insert a copy of the given value before
932 * the specified location. Due to the nature of a %list this
933 * operation can be done in constant time, and does not
934 * invalidate iterators and references.
937 insert(iterator __position
, const value_type
& __x
);
939 #ifdef __GXX_EXPERIMENTAL_CXX0X__
941 * @brief Inserts given rvalue into %list before specified iterator.
942 * @param position An iterator into the %list.
943 * @param x Data to be inserted.
944 * @return An iterator that points to the inserted data.
946 * This function will insert a copy of the given rvalue before
947 * the specified location. Due to the nature of a %list this
948 * operation can be done in constant time, and does not
949 * invalidate iterators and references.
952 insert(iterator __position
, value_type
&& __x
)
953 { return emplace(__position
, std::move(__x
)); }
957 * @brief Inserts a number of copies of given data into the %list.
958 * @param position An iterator into the %list.
959 * @param n Number of elements to be inserted.
960 * @param x Data to be inserted.
962 * This function will insert a specified number of copies of the
963 * given data before the location specified by @a position.
965 * This operation is linear in the number of elements inserted and
966 * does not invalidate iterators and references.
969 insert(iterator __position
, size_type __n
, const value_type
& __x
)
971 list
__tmp(__n
, __x
, _M_get_Node_allocator());
972 splice(__position
, __tmp
);
976 * @brief Inserts a range into the %list.
977 * @param position An iterator into the %list.
978 * @param first An input iterator.
979 * @param last An input iterator.
981 * This function will insert copies of the data in the range [@a
982 * first,@a last) into the %list before the location specified by
985 * This operation is linear in the number of elements inserted and
986 * does not invalidate iterators and references.
988 template<typename _InputIterator
>
990 insert(iterator __position
, _InputIterator __first
,
991 _InputIterator __last
)
993 list
__tmp(__first
, __last
, _M_get_Node_allocator());
994 splice(__position
, __tmp
);
998 * @brief Remove element at given position.
999 * @param position Iterator pointing to element to be erased.
1000 * @return An iterator pointing to the next element (or end()).
1002 * This function will erase the element at the given position and thus
1003 * shorten the %list by one.
1005 * Due to the nature of a %list this operation can be done in
1006 * constant time, and only invalidates iterators/references to
1007 * the element being removed. The user is also cautioned that
1008 * this function only erases the element, and that if the element
1009 * is itself a pointer, the pointed-to memory is not touched in
1010 * any way. Managing the pointer is the user's responsibility.
1013 erase(iterator __position
);
1016 * @brief Remove a range of elements.
1017 * @param first Iterator pointing to the first element to be erased.
1018 * @param last Iterator pointing to one past the last element to be
1020 * @return An iterator pointing to the element pointed to by @a last
1021 * prior to erasing (or end()).
1023 * This function will erase the elements in the range @a
1024 * [first,last) and shorten the %list accordingly.
1026 * This operation is linear time in the size of the range and only
1027 * invalidates iterators/references to the element being removed.
1028 * The user is also cautioned that this function only erases the
1029 * elements, and that if the elements themselves are pointers, the
1030 * pointed-to memory is not touched in any way. Managing the pointer
1031 * is the user's responsibility.
1034 erase(iterator __first
, iterator __last
)
1036 while (__first
!= __last
)
1037 __first
= erase(__first
);
1042 * @brief Swaps data with another %list.
1043 * @param x A %list of the same element and allocator types.
1045 * This exchanges the elements between two lists in constant
1046 * time. Note that the global std::swap() function is
1047 * specialized such that std::swap(l1,l2) will feed to this
1051 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1057 _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
);
1059 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1060 // 431. Swapping containers with unequal allocators.
1061 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
1062 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
1066 * Erases all the elements. Note that this function only erases
1067 * the elements, and that if the elements themselves are
1068 * pointers, the pointed-to memory is not touched in any way.
1069 * Managing the pointer is the user's responsibility.
1078 // [23.2.2.4] list operations
1080 * @brief Insert contents of another %list.
1081 * @param position Iterator referencing the element to insert before.
1082 * @param x Source list.
1084 * The elements of @a x are inserted in constant time in front of
1085 * the element referenced by @a position. @a x becomes an empty
1088 * Requires this != @a x.
1091 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1092 splice(iterator __position
, list
&& __x
)
1094 splice(iterator __position
, list
& __x
)
1099 _M_check_equal_allocators(__x
);
1101 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1106 * @brief Insert element from another %list.
1107 * @param position Iterator referencing the element to insert before.
1108 * @param x Source list.
1109 * @param i Iterator referencing the element to move.
1111 * Removes the element in list @a x referenced by @a i and
1112 * inserts it into the current list before @a position.
1115 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1116 splice(iterator __position
, list
&& __x
, iterator __i
)
1118 splice(iterator __position
, list
& __x
, iterator __i
)
1123 if (__position
== __i
|| __position
== __j
)
1127 _M_check_equal_allocators(__x
);
1129 this->_M_transfer(__position
, __i
, __j
);
1133 * @brief Insert range from another %list.
1134 * @param position Iterator referencing the element to insert before.
1135 * @param x Source list.
1136 * @param first Iterator referencing the start of range in x.
1137 * @param last Iterator referencing the end of range in x.
1139 * Removes elements in the range [first,last) and inserts them
1140 * before @a position in constant time.
1142 * Undefined if @a position is in [first,last).
1145 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1146 splice(iterator __position
, list
&& __x
, iterator __first
,
1149 splice(iterator __position
, list
& __x
, iterator __first
,
1153 if (__first
!= __last
)
1156 _M_check_equal_allocators(__x
);
1158 this->_M_transfer(__position
, __first
, __last
);
1163 * @brief Remove all elements equal to value.
1164 * @param value The value to remove.
1166 * Removes every element in the list equal to @a value.
1167 * Remaining elements stay in list order. Note that this
1168 * function only erases the elements, and that if the elements
1169 * themselves are pointers, the pointed-to memory is not
1170 * touched in any way. Managing the pointer is the user's
1174 remove(const _Tp
& __value
);
1177 * @brief Remove all elements satisfying a predicate.
1178 * @param Predicate Unary predicate function or object.
1180 * Removes every element in the list for which the predicate
1181 * returns true. Remaining elements stay in list order. Note
1182 * that this function only erases the elements, and that if the
1183 * elements themselves are pointers, the pointed-to memory is
1184 * not touched in any way. Managing the pointer is the user's
1187 template<typename _Predicate
>
1189 remove_if(_Predicate
);
1192 * @brief Remove consecutive duplicate elements.
1194 * For each consecutive set of elements with the same value,
1195 * remove all but the first one. Remaining elements stay in
1196 * list order. Note that this function only erases the
1197 * elements, and that if the elements themselves are pointers,
1198 * the pointed-to memory is not touched in any way. Managing
1199 * the pointer is the user's responsibility.
1205 * @brief Remove consecutive elements satisfying a predicate.
1206 * @param BinaryPredicate Binary predicate function or object.
1208 * For each consecutive set of elements [first,last) that
1209 * satisfy predicate(first,i) where i is an iterator in
1210 * [first,last), remove all but the first one. Remaining
1211 * elements stay in list order. Note that this function only
1212 * erases the elements, and that if the elements themselves are
1213 * pointers, the pointed-to memory is not touched in any way.
1214 * Managing the pointer is the user's responsibility.
1216 template<typename _BinaryPredicate
>
1218 unique(_BinaryPredicate
);
1221 * @brief Merge sorted lists.
1222 * @param x Sorted list to merge.
1224 * Assumes that both @a x and this list are sorted according to
1225 * operator<(). Merges elements of @a x into this list in
1226 * sorted order, leaving @a x empty when complete. Elements in
1227 * this list precede elements in @a x that are equal.
1230 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1237 * @brief Merge sorted lists according to comparison function.
1238 * @param x Sorted list to merge.
1239 * @param StrictWeakOrdering Comparison function defining
1242 * Assumes that both @a x and this list are sorted according to
1243 * StrictWeakOrdering. Merges elements of @a x into this list
1244 * in sorted order, leaving @a x empty when complete. Elements
1245 * in this list precede elements in @a x that are equivalent
1246 * according to StrictWeakOrdering().
1248 template<typename _StrictWeakOrdering
>
1250 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1251 merge(list
&&, _StrictWeakOrdering
);
1253 merge(list
&, _StrictWeakOrdering
);
1257 * @brief Reverse the elements in list.
1259 * Reverse the order of elements in the list in linear time.
1263 { this->_M_impl
._M_node
.reverse(); }
1266 * @brief Sort the elements.
1268 * Sorts the elements of this list in NlogN time. Equivalent
1269 * elements remain in list order.
1275 * @brief Sort the elements according to comparison function.
1277 * Sorts the elements of this list in NlogN time. Equivalent
1278 * elements remain in list order.
1280 template<typename _StrictWeakOrdering
>
1282 sort(_StrictWeakOrdering
);
1285 // Internal constructor functions follow.
1287 // Called by the range constructor to implement [23.1.1]/9
1289 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1290 // 438. Ambiguity in the "do the right thing" clause
1291 template<typename _Integer
>
1293 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1294 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1296 // Called by the range constructor to implement [23.1.1]/9
1297 template<typename _InputIterator
>
1299 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1302 for (; __first
!= __last
; ++__first
)
1303 push_back(*__first
);
1306 // Called by list(n,v,a), and the range constructor when it turns out
1307 // to be the same thing.
1309 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1311 for (; __n
> 0; --__n
)
1316 // Internal assign functions follow.
1318 // Called by the range assign to implement [23.1.1]/9
1320 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1321 // 438. Ambiguity in the "do the right thing" clause
1322 template<typename _Integer
>
1324 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1325 { _M_fill_assign(__n
, __val
); }
1327 // Called by the range assign to implement [23.1.1]/9
1328 template<typename _InputIterator
>
1330 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1333 // Called by assign(n,t), and the range assign when it turns out
1334 // to be the same thing.
1336 _M_fill_assign(size_type __n
, const value_type
& __val
);
1339 // Moves the elements from [first,last) before position.
1341 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1342 { __position
._M_node
->transfer(__first
._M_node
, __last
._M_node
); }
1344 // Inserts new element at position given and with value given.
1345 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1347 _M_insert(iterator __position
, const value_type
& __x
)
1349 _Node
* __tmp
= _M_create_node(__x
);
1350 __tmp
->hook(__position
._M_node
);
1353 template<typename
... _Args
>
1355 _M_insert(iterator __position
, _Args
&&... __args
)
1357 _Node
* __tmp
= _M_create_node(std::forward
<_Args
>(__args
)...);
1358 __tmp
->hook(__position
._M_node
);
1362 // Erases element at position given.
1364 _M_erase(iterator __position
)
1366 __position
._M_node
->unhook();
1367 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1368 _M_get_Tp_allocator().destroy(&__n
->_M_data
);
1372 // To implement the splice (and merge) bits of N1599.
1374 _M_check_equal_allocators(list
& __x
)
1376 if (std::__alloc_neq
<typename
_Base::_Node_alloc_type
>::
1377 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator()))
1378 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1383 * @brief List equality comparison.
1385 * @param y A %list of the same type as @a x.
1386 * @return True iff the size and elements of the lists are equal.
1388 * This is an equivalence relation. It is linear in the size of
1389 * the lists. Lists are considered equivalent if their sizes are
1390 * equal, and if corresponding elements compare equal.
1392 template<typename _Tp
, typename _Alloc
>
1394 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1396 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1397 const_iterator __end1
= __x
.end();
1398 const_iterator __end2
= __y
.end();
1400 const_iterator __i1
= __x
.begin();
1401 const_iterator __i2
= __y
.begin();
1402 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1407 return __i1
== __end1
&& __i2
== __end2
;
1411 * @brief List ordering relation.
1413 * @param y A %list of the same type as @a x.
1414 * @return True iff @a x is lexicographically less than @a y.
1416 * This is a total ordering relation. It is linear in the size of the
1417 * lists. The elements must be comparable with @c <.
1419 * See std::lexicographical_compare() for how the determination is made.
1421 template<typename _Tp
, typename _Alloc
>
1423 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1424 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1425 __y
.begin(), __y
.end()); }
1427 /// Based on operator==
1428 template<typename _Tp
, typename _Alloc
>
1430 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1431 { return !(__x
== __y
); }
1433 /// Based on operator<
1434 template<typename _Tp
, typename _Alloc
>
1436 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1437 { return __y
< __x
; }
1439 /// Based on operator<
1440 template<typename _Tp
, typename _Alloc
>
1442 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1443 { return !(__y
< __x
); }
1445 /// Based on operator<
1446 template<typename _Tp
, typename _Alloc
>
1448 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1449 { return !(__x
< __y
); }
1451 /// See std::list::swap().
1452 template<typename _Tp
, typename _Alloc
>
1454 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1457 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1458 template<typename _Tp
, typename _Alloc
>
1460 swap(list
<_Tp
, _Alloc
>&& __x
, list
<_Tp
, _Alloc
>& __y
)
1463 template<typename _Tp
, typename _Alloc
>
1465 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>&& __y
)
1469 _GLIBCXX_END_NESTED_NAMESPACE
1471 #endif /* _STL_LIST_H */