1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 /// @if maint Common part of a node in the %list. @endif
75 struct _List_node_base
77 _List_node_base
* _M_next
; ///< Self-explanatory
78 _List_node_base
* _M_prev
; ///< Self-explanatory
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 /// @if maint An actual node in the %list. @endif
98 template<typename _Tp
>
99 struct _List_node
: public _List_node_base
101 _Tp _M_data
; ///< User's data.
105 * @brief A list::iterator.
108 * All the functions are op overloads.
111 template<typename _Tp
>
112 struct _List_iterator
114 typedef _List_iterator
<_Tp
> _Self
;
115 typedef _List_node
<_Tp
> _Node
;
117 typedef ptrdiff_t difference_type
;
118 typedef std::bidirectional_iterator_tag iterator_category
;
119 typedef _Tp value_type
;
120 typedef _Tp
* pointer
;
121 typedef _Tp
& reference
;
127 _List_iterator(_List_node_base
* __x
)
130 // Must downcast from List_node_base to _List_node to get to _M_data.
133 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
137 { return &static_cast<_Node
*>(_M_node
)->_M_data
; }
142 _M_node
= _M_node
->_M_next
;
150 _M_node
= _M_node
->_M_next
;
157 _M_node
= _M_node
->_M_prev
;
165 _M_node
= _M_node
->_M_prev
;
170 operator==(const _Self
& __x
) const
171 { return _M_node
== __x
._M_node
; }
174 operator!=(const _Self
& __x
) const
175 { return _M_node
!= __x
._M_node
; }
177 // The only member points to the %list element.
178 _List_node_base
* _M_node
;
182 * @brief A list::const_iterator.
185 * All the functions are op overloads.
188 template<typename _Tp
>
189 struct _List_const_iterator
191 typedef _List_const_iterator
<_Tp
> _Self
;
192 typedef const _List_node
<_Tp
> _Node
;
193 typedef _List_iterator
<_Tp
> iterator
;
195 typedef ptrdiff_t difference_type
;
196 typedef std::bidirectional_iterator_tag iterator_category
;
197 typedef _Tp value_type
;
198 typedef const _Tp
* pointer
;
199 typedef const _Tp
& reference
;
201 _List_const_iterator()
205 _List_const_iterator(const _List_node_base
* __x
)
208 _List_const_iterator(const iterator
& __x
)
209 : _M_node(__x
._M_node
) { }
211 // Must downcast from List_node_base to _List_node to get to
215 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
219 { return &static_cast<_Node
*>(_M_node
)->_M_data
; }
224 _M_node
= _M_node
->_M_next
;
232 _M_node
= _M_node
->_M_next
;
239 _M_node
= _M_node
->_M_prev
;
247 _M_node
= _M_node
->_M_prev
;
252 operator==(const _Self
& __x
) const
253 { return _M_node
== __x
._M_node
; }
256 operator!=(const _Self
& __x
) const
257 { return _M_node
!= __x
._M_node
; }
259 // The only member points to the %list element.
260 const _List_node_base
* _M_node
;
263 template<typename _Val
>
265 operator==(const _List_iterator
<_Val
>& __x
,
266 const _List_const_iterator
<_Val
>& __y
)
267 { return __x
._M_node
== __y
._M_node
; }
269 template<typename _Val
>
271 operator!=(const _List_iterator
<_Val
>& __x
,
272 const _List_const_iterator
<_Val
>& __y
)
273 { return __x
._M_node
!= __y
._M_node
; }
278 * See bits/stl_deque.h's _Deque_base for an explanation.
281 template<typename _Tp
, typename _Alloc
>
286 // The stored instance is not actually of "allocator_type"'s
287 // type. Instead we rebind the type to
288 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
289 // should probably be the same. List_node<Tp> is not the same
290 // size as Tp (it's two pointers larger), and specializations on
291 // Tp may go unused because List_node<Tp> is being bound
294 // We put this to the test in the constructors and in
295 // get_allocator, where we use conversions between
296 // allocator_type and _Node_alloc_type. The conversion is
297 // required by table 32 in [20.1.5].
298 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
301 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
304 : public _Node_alloc_type
306 _List_node_base _M_node
;
309 : _Node_alloc_type(), _M_node()
312 _List_impl(const _Node_alloc_type
& __a
)
313 : _Node_alloc_type(__a
), _M_node()
321 { return _M_impl
._Node_alloc_type::allocate(1); }
324 _M_put_node(_List_node
<_Tp
>* __p
)
325 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
328 typedef _Alloc allocator_type
;
331 _M_get_Node_allocator()
332 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
334 const _Node_alloc_type
&
335 _M_get_Node_allocator() const
336 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
339 _M_get_Tp_allocator() const
340 { return _Tp_alloc_type(_M_get_Node_allocator()); }
343 get_allocator() const
344 { return allocator_type(_M_get_Node_allocator()); }
350 _List_base(const allocator_type
& __a
)
354 // This is what actually destroys the list.
364 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
365 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
370 * @brief A standard container with linear time access to elements,
371 * and fixed time insertion/deletion at any point in the sequence.
373 * @ingroup Containers
376 * Meets the requirements of a <a href="tables.html#65">container</a>, a
377 * <a href="tables.html#66">reversible container</a>, and a
378 * <a href="tables.html#67">sequence</a>, including the
379 * <a href="tables.html#68">optional sequence requirements</a> with the
380 * %exception of @c at and @c operator[].
382 * This is a @e doubly @e linked %list. Traversal up and down the
383 * %list requires linear time, but adding and removing elements (or
384 * @e nodes) is done in constant time, regardless of where the
385 * change takes place. Unlike std::vector and std::deque,
386 * random-access iterators are not provided, so subscripting ( @c
387 * [] ) access is not allowed. For algorithms which only need
388 * sequential access, this lack makes no difference.
390 * Also unlike the other standard containers, std::list provides
391 * specialized algorithms %unique to linked lists, such as
392 * splicing, sorting, and in-place reversal.
395 * A couple points on memory allocation for list<Tp>:
397 * First, we never actually allocate a Tp, we allocate
398 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
399 * that after elements from %list<X,Alloc1> are spliced into
400 * %list<X,Alloc2>, destroying the memory of the second %list is a
401 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
403 * Second, a %list conceptually represented as
405 * A <---> B <---> C <---> D
407 * is actually circular; a link exists between A and D. The %list
408 * class holds (as its only data member) a private list::iterator
409 * pointing to @e D, not to @e A! To get to the head of the %list,
410 * we start at the tail and move forward by one. When this member
411 * iterator's next/previous pointers refer to itself, the %list is
414 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
415 class list
: protected _List_base
<_Tp
, _Alloc
>
417 // concept requirements
418 typedef typename
_Alloc::value_type _Alloc_value_type
;
419 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
420 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
422 typedef _List_base
<_Tp
, _Alloc
> _Base
;
423 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
426 typedef _Tp value_type
;
427 typedef typename
_Tp_alloc_type::pointer pointer
;
428 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
429 typedef typename
_Tp_alloc_type::reference reference
;
430 typedef typename
_Tp_alloc_type::const_reference const_reference
;
431 typedef _List_iterator
<_Tp
> iterator
;
432 typedef _List_const_iterator
<_Tp
> const_iterator
;
433 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
434 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
435 typedef size_t size_type
;
436 typedef ptrdiff_t difference_type
;
437 typedef _Alloc allocator_type
;
440 // Note that pointers-to-_Node's can be ctor-converted to
442 typedef _List_node
<_Tp
> _Node
;
444 using _Base::_M_impl
;
445 using _Base::_M_put_node
;
446 using _Base::_M_get_node
;
447 using _Base::_M_get_Tp_allocator
;
448 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.
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
;
474 // [23.2.2.1] construct/copy/destroy
475 // (assign() and get_allocator() are also listed in this section)
477 * @brief Default constructor creates no elements.
483 * @brief Creates a %list with no elements.
484 * @param a An allocator object.
487 list(const allocator_type
& __a
)
491 * @brief Creates a %list with copies of an exemplar element.
492 * @param n The number of elements to initially create.
493 * @param value An element to copy.
494 * @param a An allocator object.
496 * This constructor fills the %list with @a n copies of @a value.
499 list(size_type __n
, const value_type
& __value
= value_type(),
500 const allocator_type
& __a
= allocator_type())
502 { _M_fill_initialize(__n
, __value
); }
505 * @brief %List copy constructor.
506 * @param x A %list of identical element and allocator types.
508 * The newly-created %list uses a copy of the allocation object used
511 list(const list
& __x
)
512 : _Base(__x
._M_get_Node_allocator())
513 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
515 #ifdef __GXX_EXPERIMENTAL_CXX0X__
517 * @brief %List move constructor.
518 * @param x A %list of identical element and allocator types.
520 * The newly-created %list contains the exact contents of @a x.
521 * The contents of @a x are a valid, but unspecified %list.
524 : _Base(__x
._M_get_Node_allocator())
529 * @brief Builds a %list from a range.
530 * @param first An input iterator.
531 * @param last An input iterator.
532 * @param a An allocator object.
534 * Create a %list consisting of copies of the elements from
535 * [@a first,@a last). This is linear in N (where N is
536 * distance(@a first,@a last)).
538 template<typename _InputIterator
>
539 list(_InputIterator __first
, _InputIterator __last
,
540 const allocator_type
& __a
= allocator_type())
543 // Check whether it's an integral type. If so, it's not an iterator.
544 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
545 _M_initialize_dispatch(__first
, __last
, _Integral());
549 * No explicit dtor needed as the _Base dtor takes care of
550 * things. The _Base dtor only erases the elements, and note
551 * that if the elements themselves are pointers, the pointed-to
552 * memory is not touched in any way. Managing the pointer is
553 * the user's responsibilty.
557 * @brief %List assignment operator.
558 * @param x A %list of identical element and allocator types.
560 * All the elements of @a x are copied, but unlike the copy
561 * constructor, the allocator object is not copied.
564 operator=(const list
& __x
);
566 #ifdef __GXX_EXPERIMENTAL_CXX0X__
568 * @brief %List move assignment operator.
569 * @param x A %list of identical element and allocator types.
571 * The contents of @a x are moved into this %list (without copying).
572 * @a x is a valid, but unspecified %list
575 operator=(list
&& __x
)
583 * @brief Assigns a given value to a %list.
584 * @param n Number of elements to be assigned.
585 * @param val Value to be assigned.
587 * This function fills a %list with @a n copies of the given
588 * value. Note that the assignment completely changes the %list
589 * and that the resulting %list's size is the same as the number
590 * of elements assigned. Old data may be lost.
593 assign(size_type __n
, const value_type
& __val
)
594 { _M_fill_assign(__n
, __val
); }
597 * @brief Assigns a range to a %list.
598 * @param first An input iterator.
599 * @param last An input iterator.
601 * This function fills a %list with copies of the elements in the
602 * range [@a first,@a last).
604 * Note that the assignment completely changes the %list and
605 * that the resulting %list's size is the same as the number of
606 * elements assigned. Old data may be lost.
608 template<typename _InputIterator
>
610 assign(_InputIterator __first
, _InputIterator __last
)
612 // Check whether it's an integral type. If so, it's not an iterator.
613 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
614 _M_assign_dispatch(__first
, __last
, _Integral());
617 /// Get a copy of the memory allocation object.
619 get_allocator() const
620 { return _Base::get_allocator(); }
624 * Returns a read/write iterator that points to the first element in the
625 * %list. Iteration is done in ordinary element order.
629 { return iterator(this->_M_impl
._M_node
._M_next
); }
632 * Returns a read-only (constant) iterator that points to the
633 * first element in the %list. Iteration is done in ordinary
638 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
641 * Returns a read/write iterator that points one past the last
642 * element in the %list. Iteration is done in ordinary element
647 { return iterator(&this->_M_impl
._M_node
); }
650 * Returns a read-only (constant) iterator that points one past
651 * the last element in the %list. Iteration is done in ordinary
656 { return const_iterator(&this->_M_impl
._M_node
); }
659 * Returns a read/write reverse iterator that points to the last
660 * element in the %list. Iteration is done in reverse element
665 { return reverse_iterator(end()); }
668 * Returns a read-only (constant) reverse iterator that points to
669 * the last element in the %list. Iteration is done in reverse
672 const_reverse_iterator
674 { return const_reverse_iterator(end()); }
677 * Returns a read/write reverse iterator that points to one
678 * before the first element in the %list. Iteration is done in
679 * reverse element order.
683 { return reverse_iterator(begin()); }
686 * Returns a read-only (constant) reverse iterator that points to one
687 * before the first element in the %list. Iteration is done in reverse
690 const_reverse_iterator
692 { return const_reverse_iterator(begin()); }
694 #ifdef __GXX_EXPERIMENTAL_CXX0X__
696 * Returns a read-only (constant) iterator that points to the
697 * first element in the %list. Iteration is done in ordinary
702 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
705 * Returns a read-only (constant) iterator that points one past
706 * the last element in the %list. Iteration is done in ordinary
711 { return const_iterator(&this->_M_impl
._M_node
); }
714 * Returns a read-only (constant) reverse iterator that points to
715 * the last element in the %list. Iteration is done in reverse
718 const_reverse_iterator
720 { return const_reverse_iterator(end()); }
723 * Returns a read-only (constant) reverse iterator that points to one
724 * before the first element in the %list. Iteration is done in reverse
727 const_reverse_iterator
729 { return const_reverse_iterator(begin()); }
732 // [23.2.2.2] capacity
734 * Returns true if the %list is empty. (Thus begin() would equal
739 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
741 /** Returns the number of elements in the %list. */
744 { return std::distance(begin(), end()); }
746 /** Returns the size() of the largest possible %list. */
749 { return _M_get_Tp_allocator().max_size(); }
752 * @brief Resizes the %list to the specified number of elements.
753 * @param new_size Number of elements the %list should contain.
754 * @param x Data with which new elements should be populated.
756 * This function will %resize the %list to the specified number
757 * of elements. If the number is smaller than the %list's
758 * current size the %list is truncated, otherwise the %list is
759 * extended and new elements are populated with given data.
762 resize(size_type __new_size
, value_type __x
= value_type());
766 * Returns a read/write reference to the data at the first
767 * element of the %list.
774 * Returns a read-only (constant) reference to the data at the first
775 * element of the %list.
782 * Returns a read/write reference to the data at the last element
788 iterator __tmp
= end();
794 * Returns a read-only (constant) reference to the data at the last
795 * element of the %list.
800 const_iterator __tmp
= end();
805 // [23.2.2.3] modifiers
807 * @brief Add data to the front of the %list.
808 * @param x Data to be added.
810 * This is a typical stack operation. The function creates an
811 * element at the front of the %list and assigns the given data
812 * to it. Due to the nature of a %list this operation can be
813 * done in constant time, and does not invalidate iterators and
817 push_front(const value_type
& __x
)
818 { this->_M_insert(begin(), __x
); }
821 * @brief Removes first element.
823 * This is a typical stack operation. It shrinks the %list by
824 * one. Due to the nature of a %list this operation can be done
825 * in constant time, and only invalidates iterators/references to
826 * the element being removed.
828 * Note that no data is returned, and if the first element's data
829 * is needed, it should be retrieved before pop_front() is
834 { this->_M_erase(begin()); }
837 * @brief Add data to the end of the %list.
838 * @param x Data to be added.
840 * This is a typical stack operation. The function creates an
841 * element at the end of the %list and assigns the given data to
842 * it. Due to the nature of a %list this operation can be done
843 * in constant time, and does not invalidate iterators and
847 push_back(const value_type
& __x
)
848 { this->_M_insert(end(), __x
); }
851 * @brief Removes last element.
853 * This is a typical stack operation. It shrinks the %list by
854 * one. Due to the nature of a %list this operation can be done
855 * in constant time, and only invalidates iterators/references to
856 * the element being removed.
858 * Note that no data is returned, and if the last element's data
859 * is needed, it should be retrieved before pop_back() is called.
863 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
866 * @brief Inserts given value into %list before specified iterator.
867 * @param position An iterator into the %list.
868 * @param x Data to be inserted.
869 * @return An iterator that points to the inserted data.
871 * This function will insert a copy of the given value before
872 * the specified location. Due to the nature of a %list this
873 * operation can be done in constant time, and does not
874 * invalidate iterators and references.
877 insert(iterator __position
, const value_type
& __x
);
880 * @brief Inserts a number of copies of given data into the %list.
881 * @param position An iterator into the %list.
882 * @param n Number of elements to be inserted.
883 * @param x Data to be inserted.
885 * This function will insert a specified number of copies of the
886 * given data before the location specified by @a position.
888 * This operation is linear in the number of elements inserted and
889 * does not invalidate iterators and references.
892 insert(iterator __position
, size_type __n
, const value_type
& __x
)
894 list
__tmp(__n
, __x
, _M_get_Node_allocator());
895 splice(__position
, __tmp
);
899 * @brief Inserts a range into the %list.
900 * @param position An iterator into the %list.
901 * @param first An input iterator.
902 * @param last An input iterator.
904 * This function will insert copies of the data in the range [@a
905 * first,@a last) into the %list before the location specified by
908 * This operation is linear in the number of elements inserted and
909 * does not invalidate iterators and references.
911 template<typename _InputIterator
>
913 insert(iterator __position
, _InputIterator __first
,
914 _InputIterator __last
)
916 list
__tmp(__first
, __last
, _M_get_Node_allocator());
917 splice(__position
, __tmp
);
921 * @brief Remove element at given position.
922 * @param position Iterator pointing to element to be erased.
923 * @return An iterator pointing to the next element (or end()).
925 * This function will erase the element at the given position and thus
926 * shorten the %list by one.
928 * Due to the nature of a %list this operation can be done in
929 * constant time, and only invalidates iterators/references to
930 * the element being removed. The user is also cautioned that
931 * this function only erases the element, and that if the element
932 * is itself a pointer, the pointed-to memory is not touched in
933 * any way. Managing the pointer is the user's responsibilty.
936 erase(iterator __position
);
939 * @brief Remove a range of elements.
940 * @param first Iterator pointing to the first element to be erased.
941 * @param last Iterator pointing to one past the last element to be
943 * @return An iterator pointing to the element pointed to by @a last
944 * prior to erasing (or end()).
946 * This function will erase the elements in the range @a
947 * [first,last) and shorten the %list accordingly.
949 * This operation is linear time in the size of the range and only
950 * invalidates iterators/references to the element being removed.
951 * The user is also cautioned that this function only erases the
952 * elements, and that if the elements themselves are pointers, the
953 * pointed-to memory is not touched in any way. Managing the pointer
954 * is the user's responsibilty.
957 erase(iterator __first
, iterator __last
)
959 while (__first
!= __last
)
960 __first
= erase(__first
);
965 * @brief Swaps data with another %list.
966 * @param x A %list of the same element and allocator types.
968 * This exchanges the elements between two lists in constant
969 * time. Note that the global std::swap() function is
970 * specialized such that std::swap(l1,l2) will feed to this
974 #ifdef __GXX_EXPERIMENTAL_CXX0X__
980 _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
);
982 // _GLIBCXX_RESOLVE_LIB_DEFECTS
983 // 431. Swapping containers with unequal allocators.
984 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
985 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
989 * Erases all the elements. Note that this function only erases
990 * the elements, and that if the elements themselves are
991 * pointers, the pointed-to memory is not touched in any way.
992 * Managing the pointer is the user's responsibilty.
1001 // [23.2.2.4] list operations
1003 * @brief Insert contents of another %list.
1004 * @param position Iterator referencing the element to insert before.
1005 * @param x Source list.
1007 * The elements of @a x are inserted in constant time in front of
1008 * the element referenced by @a position. @a x becomes an empty
1011 * Requires this != @a x.
1014 splice(iterator __position
, list
& __x
)
1018 _M_check_equal_allocators(__x
);
1020 this->_M_transfer(__position
, __x
.begin(), __x
.end());
1025 * @brief Insert element from another %list.
1026 * @param position Iterator referencing the element to insert before.
1027 * @param x Source list.
1028 * @param i Iterator referencing the element to move.
1030 * Removes the element in list @a x referenced by @a i and
1031 * inserts it into the current list before @a position.
1034 splice(iterator __position
, list
& __x
, iterator __i
)
1038 if (__position
== __i
|| __position
== __j
)
1042 _M_check_equal_allocators(__x
);
1044 this->_M_transfer(__position
, __i
, __j
);
1048 * @brief Insert range from another %list.
1049 * @param position Iterator referencing the element to insert before.
1050 * @param x Source list.
1051 * @param first Iterator referencing the start of range in x.
1052 * @param last Iterator referencing the end of range in x.
1054 * Removes elements in the range [first,last) and inserts them
1055 * before @a position in constant time.
1057 * Undefined if @a position is in [first,last).
1060 splice(iterator __position
, list
& __x
, iterator __first
, iterator __last
)
1062 if (__first
!= __last
)
1065 _M_check_equal_allocators(__x
);
1067 this->_M_transfer(__position
, __first
, __last
);
1072 * @brief Remove all elements equal to value.
1073 * @param value The value to remove.
1075 * Removes every element in the list equal to @a value.
1076 * Remaining elements stay in list order. Note that this
1077 * function only erases the elements, and that if the elements
1078 * themselves are pointers, the pointed-to memory is not
1079 * touched in any way. Managing the pointer is the user's
1083 remove(const _Tp
& __value
);
1086 * @brief Remove all elements satisfying a predicate.
1087 * @param Predicate Unary predicate function or object.
1089 * Removes every element in the list for which the predicate
1090 * returns true. Remaining elements stay in list order. Note
1091 * that this function only erases the elements, and that if the
1092 * elements themselves are pointers, the pointed-to memory is
1093 * not touched in any way. Managing the pointer is the user's
1096 template<typename _Predicate
>
1098 remove_if(_Predicate
);
1101 * @brief Remove consecutive duplicate elements.
1103 * For each consecutive set of elements with the same value,
1104 * remove all but the first one. Remaining elements stay in
1105 * list order. Note that this function only erases the
1106 * elements, and that if the elements themselves are pointers,
1107 * the pointed-to memory is not touched in any way. Managing
1108 * the pointer is the user's responsibilty.
1114 * @brief Remove consecutive elements satisfying a predicate.
1115 * @param BinaryPredicate Binary predicate function or object.
1117 * For each consecutive set of elements [first,last) that
1118 * satisfy predicate(first,i) where i is an iterator in
1119 * [first,last), remove all but the first one. Remaining
1120 * elements stay in list order. Note that this function only
1121 * erases the elements, and that if the elements themselves are
1122 * pointers, the pointed-to memory is not touched in any way.
1123 * Managing the pointer is the user's responsibilty.
1125 template<typename _BinaryPredicate
>
1127 unique(_BinaryPredicate
);
1130 * @brief Merge sorted lists.
1131 * @param x Sorted list to merge.
1133 * Assumes that both @a x and this list are sorted according to
1134 * operator<(). Merges elements of @a x into this list in
1135 * sorted order, leaving @a x empty when complete. Elements in
1136 * this list precede elements in @a x that are equal.
1142 * @brief Merge sorted lists according to comparison function.
1143 * @param x Sorted list to merge.
1144 * @param StrictWeakOrdering Comparison function definining
1147 * Assumes that both @a x and this list are sorted according to
1148 * StrictWeakOrdering. Merges elements of @a x into this list
1149 * in sorted order, leaving @a x empty when complete. Elements
1150 * in this list precede elements in @a x that are equivalent
1151 * according to StrictWeakOrdering().
1153 template<typename _StrictWeakOrdering
>
1155 merge(list
&, _StrictWeakOrdering
);
1158 * @brief Reverse the elements in list.
1160 * Reverse the order of elements in the list in linear time.
1164 { this->_M_impl
._M_node
.reverse(); }
1167 * @brief Sort the elements.
1169 * Sorts the elements of this list in NlogN time. Equivalent
1170 * elements remain in list order.
1176 * @brief Sort the elements according to comparison function.
1178 * Sorts the elements of this list in NlogN time. Equivalent
1179 * elements remain in list order.
1181 template<typename _StrictWeakOrdering
>
1183 sort(_StrictWeakOrdering
);
1186 // Internal constructor functions follow.
1188 // Called by the range constructor to implement [23.1.1]/9
1190 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1191 // 438. Ambiguity in the "do the right thing" clause
1192 template<typename _Integer
>
1194 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1195 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1197 // Called by the range constructor to implement [23.1.1]/9
1198 template<typename _InputIterator
>
1200 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1203 for (; __first
!= __last
; ++__first
)
1204 push_back(*__first
);
1207 // Called by list(n,v,a), and the range constructor when it turns out
1208 // to be the same thing.
1210 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1212 for (; __n
> 0; --__n
)
1217 // Internal assign functions follow.
1219 // Called by the range assign to implement [23.1.1]/9
1221 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1222 // 438. Ambiguity in the "do the right thing" clause
1223 template<typename _Integer
>
1225 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1226 { _M_fill_assign(__n
, __val
); }
1228 // Called by the range assign to implement [23.1.1]/9
1229 template<typename _InputIterator
>
1231 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1234 // Called by assign(n,t), and the range assign when it turns out
1235 // to be the same thing.
1237 _M_fill_assign(size_type __n
, const value_type
& __val
);
1240 // Moves the elements from [first,last) before position.
1242 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1243 { __position
._M_node
->transfer(__first
._M_node
, __last
._M_node
); }
1245 // Inserts new element at position given and with value given.
1247 _M_insert(iterator __position
, const value_type
& __x
)
1249 _Node
* __tmp
= _M_create_node(__x
);
1250 __tmp
->hook(__position
._M_node
);
1253 // Erases element at position given.
1255 _M_erase(iterator __position
)
1257 __position
._M_node
->unhook();
1258 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1259 _M_get_Tp_allocator().destroy(&__n
->_M_data
);
1263 // To implement the splice (and merge) bits of N1599.
1265 _M_check_equal_allocators(list
& __x
)
1267 if (_M_get_Node_allocator() != __x
._M_get_Node_allocator())
1268 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1273 * @brief List equality comparison.
1275 * @param y A %list of the same type as @a x.
1276 * @return True iff the size and elements of the lists are equal.
1278 * This is an equivalence relation. It is linear in the size of
1279 * the lists. Lists are considered equivalent if their sizes are
1280 * equal, and if corresponding elements compare equal.
1282 template<typename _Tp
, typename _Alloc
>
1284 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1286 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1287 const_iterator __end1
= __x
.end();
1288 const_iterator __end2
= __y
.end();
1290 const_iterator __i1
= __x
.begin();
1291 const_iterator __i2
= __y
.begin();
1292 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1297 return __i1
== __end1
&& __i2
== __end2
;
1301 * @brief List ordering relation.
1303 * @param y A %list of the same type as @a x.
1304 * @return True iff @a x is lexicographically less than @a y.
1306 * This is a total ordering relation. It is linear in the size of the
1307 * lists. The elements must be comparable with @c <.
1309 * See std::lexicographical_compare() for how the determination is made.
1311 template<typename _Tp
, typename _Alloc
>
1313 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1314 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1315 __y
.begin(), __y
.end()); }
1317 /// Based on operator==
1318 template<typename _Tp
, typename _Alloc
>
1320 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1321 { return !(__x
== __y
); }
1323 /// Based on operator<
1324 template<typename _Tp
, typename _Alloc
>
1326 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1327 { return __y
< __x
; }
1329 /// Based on operator<
1330 template<typename _Tp
, typename _Alloc
>
1332 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1333 { return !(__y
< __x
); }
1335 /// Based on operator<
1336 template<typename _Tp
, typename _Alloc
>
1338 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1339 { return !(__x
< __y
); }
1341 /// See std::list::swap().
1342 template<typename _Tp
, typename _Alloc
>
1344 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1348 template<typename _Tp
, typename _Alloc
>
1350 swap(list
<_Tp
, _Alloc
>&& __x
, list
<_Tp
, _Alloc
>& __y
)
1353 template<typename _Tp
, typename _Alloc
>
1355 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>&& __y
)
1359 _GLIBCXX_END_NESTED_NAMESPACE
1361 #endif /* _STL_LIST_H */