1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/concept_check.h>
66 namespace _GLIBCXX_STD
68 // Supporting structures are split into common and templated types; the
69 // latter publicly inherits from the former in an effort to reduce code
70 // duplication. This results in some "needless" static_cast'ing later on,
71 // but it's all safe downcasting.
73 /// @if maint Common part of a node in the %list. @endif
74 struct _List_node_base
76 _List_node_base
* _M_next
; ///< Self-explanatory
77 _List_node_base
* _M_prev
; ///< Self-explanatory
80 swap(_List_node_base
& __x
, _List_node_base
& __y
);
83 transfer(_List_node_base
* const __first
,
84 _List_node_base
* const __last
);
90 hook(_List_node_base
* const __position
);
96 /// @if maint An actual node in the %list. @endif
97 template<typename _Tp
>
98 struct _List_node
: public _List_node_base
100 _Tp _M_data
; ///< User's data.
104 * @brief A list::iterator.
107 * 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.
184 * All the functions are op overloads.
187 template<typename _Tp
>
188 struct _List_const_iterator
190 typedef _List_const_iterator
<_Tp
> _Self
;
191 typedef const _List_node
<_Tp
> _Node
;
192 typedef _List_iterator
<_Tp
> iterator
;
194 typedef ptrdiff_t difference_type
;
195 typedef std::bidirectional_iterator_tag iterator_category
;
196 typedef _Tp value_type
;
197 typedef const _Tp
* pointer
;
198 typedef const _Tp
& reference
;
200 _List_const_iterator()
204 _List_const_iterator(const _List_node_base
* __x
)
207 _List_const_iterator(const iterator
& __x
)
208 : _M_node(__x
._M_node
) { }
210 // Must downcast from List_node_base to _List_node to get to
214 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
218 { return &static_cast<_Node
*>(_M_node
)->_M_data
; }
223 _M_node
= _M_node
->_M_next
;
231 _M_node
= _M_node
->_M_next
;
238 _M_node
= _M_node
->_M_prev
;
246 _M_node
= _M_node
->_M_prev
;
251 operator==(const _Self
& __x
) const
252 { return _M_node
== __x
._M_node
; }
255 operator!=(const _Self
& __x
) const
256 { return _M_node
!= __x
._M_node
; }
258 // The only member points to the %list element.
259 const _List_node_base
* _M_node
;
262 template<typename _Val
>
264 operator==(const _List_iterator
<_Val
>& __x
,
265 const _List_const_iterator
<_Val
>& __y
)
266 { return __x
._M_node
== __y
._M_node
; }
268 template<typename _Val
>
270 operator!=(const _List_iterator
<_Val
>& __x
,
271 const _List_const_iterator
<_Val
>& __y
)
272 { return __x
._M_node
!= __y
._M_node
; }
277 * See bits/stl_deque.h's _Deque_base for an explanation.
280 template<typename _Tp
, typename _Alloc
>
285 // The stored instance is not actually of "allocator_type"'s
286 // type. Instead we rebind the type to
287 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
288 // should probably be the same. List_node<Tp> is not the same
289 // size as Tp (it's two pointers larger), and specializations on
290 // Tp may go unused because List_node<Tp> is being bound
293 // We put this to the test in the constructors and in
294 // get_allocator, where we use conversions between
295 // allocator_type and _Node_alloc_type. The conversion is
296 // required by table 32 in [20.1.5].
297 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
300 typedef typename
_Alloc::template rebind
<_Tp
>::other _Tp_alloc_type
;
303 : public _Node_alloc_type
305 _List_node_base _M_node
;
306 _List_impl(const _Node_alloc_type
& __a
)
307 : _Node_alloc_type(__a
)
315 { return _M_impl
._Node_alloc_type::allocate(1); }
318 _M_put_node(_List_node
<_Tp
>* __p
)
319 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
322 typedef _Alloc allocator_type
;
325 _M_get_Tp_allocator() const
326 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
329 get_allocator() const
330 { return _M_get_Tp_allocator(); }
332 _List_base(const allocator_type
& __a
)
336 // This is what actually destroys the list.
346 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
347 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
352 * @brief A standard container with linear time access to elements,
353 * and fixed time insertion/deletion at any point in the sequence.
355 * @ingroup Containers
358 * Meets the requirements of a <a href="tables.html#65">container</a>, a
359 * <a href="tables.html#66">reversible container</a>, and a
360 * <a href="tables.html#67">sequence</a>, including the
361 * <a href="tables.html#68">optional sequence requirements</a> with the
362 * %exception of @c at and @c operator[].
364 * This is a @e doubly @e linked %list. Traversal up and down the
365 * %list requires linear time, but adding and removing elements (or
366 * @e nodes) is done in constant time, regardless of where the
367 * change takes place. Unlike std::vector and std::deque,
368 * random-access iterators are not provided, so subscripting ( @c
369 * [] ) access is not allowed. For algorithms which only need
370 * sequential access, this lack makes no difference.
372 * Also unlike the other standard containers, std::list provides
373 * specialized algorithms %unique to linked lists, such as
374 * splicing, sorting, and in-place reversal.
377 * A couple points on memory allocation for list<Tp>:
379 * First, we never actually allocate a Tp, we allocate
380 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
381 * that after elements from %list<X,Alloc1> are spliced into
382 * %list<X,Alloc2>, destroying the memory of the second %list is a
383 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
385 * Second, a %list conceptually represented as
387 * A <---> B <---> C <---> D
389 * is actually circular; a link exists between A and D. The %list
390 * class holds (as its only data member) a private list::iterator
391 * pointing to @e D, not to @e A! To get to the head of the %list,
392 * we start at the tail and move forward by one. When this member
393 * iterator's next/previous pointers refer to itself, the %list is
396 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
397 class list
: protected _List_base
<_Tp
, _Alloc
>
399 // concept requirements
400 typedef typename
_Alloc::value_type _Alloc_value_type
;
401 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
402 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
404 typedef _List_base
<_Tp
, _Alloc
> _Base
;
405 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
408 typedef _Tp value_type
;
409 typedef typename
_Tp_alloc_type::pointer pointer
;
410 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
411 typedef typename
_Tp_alloc_type::reference reference
;
412 typedef typename
_Tp_alloc_type::const_reference const_reference
;
413 typedef _List_iterator
<_Tp
> iterator
;
414 typedef _List_const_iterator
<_Tp
> const_iterator
;
415 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
416 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
417 typedef size_t size_type
;
418 typedef ptrdiff_t difference_type
;
419 typedef _Alloc allocator_type
;
422 // Note that pointers-to-_Node's can be ctor-converted to
424 typedef _List_node
<_Tp
> _Node
;
427 * One data member plus two memory-handling functions. If the
428 * _Alloc type requires separate instances, then one of those
429 * will also be included, accumulated from the topmost parent.
432 using _Base::_M_impl
;
433 using _Base::_M_put_node
;
434 using _Base::_M_get_node
;
435 using _Base::_M_get_Tp_allocator
;
439 * @param x An instance of user data.
441 * Allocates space for a new node and constructs a copy of @a x in it.
445 _M_create_node(const value_type
& __x
)
447 _Node
* __p
= this->_M_get_node();
450 _M_get_Tp_allocator().construct(&__p
->_M_data
, __x
);
455 __throw_exception_again
;
461 // [23.2.2.1] construct/copy/destroy
462 // (assign() and get_allocator() are also listed in this section)
464 * @brief Default constructor creates no elements.
467 list(const allocator_type
& __a
= allocator_type())
471 * @brief Create a %list with copies of an exemplar element.
472 * @param n The number of elements to initially create.
473 * @param value An element to copy.
475 * This constructor fills the %list with @a n copies of @a value.
478 list(size_type __n
, const value_type
& __value
= value_type(),
479 const allocator_type
& __a
= allocator_type())
481 { this->insert(begin(), __n
, __value
); }
484 * @brief %List copy constructor.
485 * @param x A %list of identical element and allocator types.
487 * The newly-created %list uses a copy of the allocation object used
490 list(const list
& __x
)
491 : _Base(__x
.get_allocator())
492 { this->insert(begin(), __x
.begin(), __x
.end()); }
495 * @brief Builds a %list from a range.
496 * @param first An input iterator.
497 * @param last An input iterator.
499 * Create a %list consisting of copies of the elements from
500 * [@a first,@a last). This is linear in N (where N is
501 * distance(@a first,@a last)).
504 * We don't need any dispatching tricks here, because insert does all of
508 template<typename _InputIterator
>
509 list(_InputIterator __first
, _InputIterator __last
,
510 const allocator_type
& __a
= allocator_type())
512 { this->insert(begin(), __first
, __last
); }
515 * No explicit dtor needed as the _Base dtor takes care of
516 * things. The _Base dtor only erases the elements, and note
517 * that if the elements themselves are pointers, the pointed-to
518 * memory is not touched in any way. Managing the pointer is
519 * the user's responsibilty.
523 * @brief %List assignment operator.
524 * @param x A %list of identical element and allocator types.
526 * All the elements of @a x are copied, but unlike the copy
527 * constructor, the allocator object is not copied.
530 operator=(const list
& __x
);
533 * @brief Assigns a given value to a %list.
534 * @param n Number of elements to be assigned.
535 * @param val Value to be assigned.
537 * This function fills a %list with @a n copies of the given
538 * value. Note that the assignment completely changes the %list
539 * and that the resulting %list's size is the same as the number
540 * of elements assigned. Old data may be lost.
543 assign(size_type __n
, const value_type
& __val
)
544 { _M_fill_assign(__n
, __val
); }
547 * @brief Assigns a range to a %list.
548 * @param first An input iterator.
549 * @param last An input iterator.
551 * This function fills a %list with copies of the elements in the
552 * range [@a first,@a last).
554 * Note that the assignment completely changes the %list and
555 * that the resulting %list's size is the same as the number of
556 * elements assigned. Old data may be lost.
558 template<typename _InputIterator
>
560 assign(_InputIterator __first
, _InputIterator __last
)
562 // Check whether it's an integral type. If so, it's not an iterator.
563 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
564 _M_assign_dispatch(__first
, __last
, _Integral());
567 /// Get a copy of the memory allocation object.
569 get_allocator() const
570 { return _Base::get_allocator(); }
574 * Returns a read/write iterator that points to the first element in the
575 * %list. Iteration is done in ordinary element order.
579 { return iterator(this->_M_impl
._M_node
._M_next
); }
582 * Returns a read-only (constant) iterator that points to the
583 * first element in the %list. Iteration is done in ordinary
588 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
591 * Returns a read/write iterator that points one past the last
592 * element in the %list. Iteration is done in ordinary element
597 { return iterator(&this->_M_impl
._M_node
); }
600 * Returns a read-only (constant) iterator that points one past
601 * the last element in the %list. Iteration is done in ordinary
606 { return const_iterator(&this->_M_impl
._M_node
); }
609 * Returns a read/write reverse iterator that points to the last
610 * element in the %list. Iteration is done in reverse element
615 { return reverse_iterator(end()); }
618 * Returns a read-only (constant) reverse iterator that points to
619 * the last element in the %list. Iteration is done in reverse
622 const_reverse_iterator
624 { return const_reverse_iterator(end()); }
627 * Returns a read/write reverse iterator that points to one
628 * before the first element in the %list. Iteration is done in
629 * reverse element order.
633 { return reverse_iterator(begin()); }
636 * Returns a read-only (constant) reverse iterator that points to one
637 * before the first element in the %list. Iteration is done in reverse
640 const_reverse_iterator
642 { return const_reverse_iterator(begin()); }
644 // [23.2.2.2] capacity
646 * Returns true if the %list is empty. (Thus begin() would equal
651 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
653 /** Returns the number of elements in the %list. */
656 { return std::distance(begin(), end()); }
658 /** Returns the size() of the largest possible %list. */
661 { return size_type(-1); }
664 * @brief Resizes the %list to the specified number of elements.
665 * @param new_size Number of elements the %list should contain.
666 * @param x Data with which new elements should be populated.
668 * This function will %resize the %list to the specified number
669 * of elements. If the number is smaller than the %list's
670 * current size the %list is truncated, otherwise the %list is
671 * extended and new elements are populated with given data.
674 resize(size_type __new_size
, value_type __x
= value_type());
678 * Returns a read/write reference to the data at the first
679 * element of the %list.
686 * Returns a read-only (constant) reference to the data at the first
687 * element of the %list.
694 * Returns a read/write reference to the data at the last element
700 iterator __tmp
= end();
706 * Returns a read-only (constant) reference to the data at the last
707 * element of the %list.
712 const_iterator __tmp
= end();
717 // [23.2.2.3] modifiers
719 * @brief Add data to the front of the %list.
720 * @param x Data to be added.
722 * This is a typical stack operation. The function creates an
723 * element at the front of the %list and assigns the given data
724 * to it. Due to the nature of a %list this operation can be
725 * done in constant time, and does not invalidate iterators and
729 push_front(const value_type
& __x
)
730 { this->_M_insert(begin(), __x
); }
733 * @brief Removes first element.
735 * This is a typical stack operation. It shrinks the %list by
736 * one. Due to the nature of a %list this operation can be done
737 * in constant time, and only invalidates iterators/references to
738 * the element being removed.
740 * Note that no data is returned, and if the first element's data
741 * is needed, it should be retrieved before pop_front() is
746 { this->_M_erase(begin()); }
749 * @brief Add data to the end of the %list.
750 * @param x Data to be added.
752 * This is a typical stack operation. The function creates an
753 * element at the end of the %list and assigns the given data to
754 * it. Due to the nature of a %list this operation can be done
755 * in constant time, and does not invalidate iterators and
759 push_back(const value_type
& __x
)
760 { this->_M_insert(end(), __x
); }
763 * @brief Removes last element.
765 * This is a typical stack operation. It shrinks the %list by
766 * one. Due to the nature of a %list this operation can be done
767 * in constant time, and only invalidates iterators/references to
768 * the element being removed.
770 * Note that no data is returned, and if the last element's data
771 * is needed, it should be retrieved before pop_back() is called.
775 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
778 * @brief Inserts given value into %list before specified iterator.
779 * @param position An iterator into the %list.
780 * @param x Data to be inserted.
781 * @return An iterator that points to the inserted data.
783 * This function will insert a copy of the given value before
784 * the specified location. Due to the nature of a %list this
785 * operation can be done in constant time, and does not
786 * invalidate iterators and references.
789 insert(iterator __position
, const value_type
& __x
);
792 * @brief Inserts a number of copies of given data into the %list.
793 * @param position An iterator into the %list.
794 * @param n Number of elements to be inserted.
795 * @param x Data to be inserted.
797 * This function will insert a specified number of copies of the
798 * given data before the location specified by @a position.
800 * Due to the nature of a %list this operation can be done in
801 * constant time, and does not invalidate iterators and
805 insert(iterator __position
, size_type __n
, const value_type
& __x
)
806 { _M_fill_insert(__position
, __n
, __x
); }
809 * @brief Inserts a range into the %list.
810 * @param position An iterator into the %list.
811 * @param first An input iterator.
812 * @param last An input iterator.
814 * This function will insert copies of the data in the range [@a
815 * first,@a last) into the %list before the location specified by
818 * Due to the nature of a %list this operation can be done in
819 * constant time, and does not invalidate iterators and
822 template<typename _InputIterator
>
824 insert(iterator __position
, _InputIterator __first
,
825 _InputIterator __last
)
827 // Check whether it's an integral type. If so, it's not an iterator.
828 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
829 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
833 * @brief Remove element at given position.
834 * @param position Iterator pointing to element to be erased.
835 * @return An iterator pointing to the next element (or end()).
837 * This function will erase the element at the given position and thus
838 * shorten the %list by one.
840 * Due to the nature of a %list this operation can be done in
841 * constant time, and only invalidates iterators/references to
842 * the element being removed. The user is also cautioned that
843 * this function only erases the element, and that if the element
844 * is itself a pointer, the pointed-to memory is not touched in
845 * any way. Managing the pointer is the user's responsibilty.
848 erase(iterator __position
);
851 * @brief Remove a range of elements.
852 * @param first Iterator pointing to the first element to be erased.
853 * @param last Iterator pointing to one past the last element to be
855 * @return An iterator pointing to the element pointed to by @a last
856 * prior to erasing (or end()).
858 * This function will erase the elements in the range @a
859 * [first,last) and shorten the %list accordingly.
861 * Due to the nature of a %list this operation can be done in
862 * constant time, and only invalidates iterators/references to
863 * the element being removed. The user is also cautioned that
864 * this function only erases the elements, and that if the
865 * elements themselves are pointers, the pointed-to memory is not
866 * touched in any way. Managing the pointer is the user's
870 erase(iterator __first
, iterator __last
)
872 while (__first
!= __last
)
873 __first
= erase(__first
);
878 * @brief Swaps data with another %list.
879 * @param x A %list of the same element and allocator types.
881 * This exchanges the elements between two lists in constant
882 * time. Note that the global std::swap() function is
883 * specialized such that std::swap(l1,l2) will feed to this
888 { _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
); }
891 * Erases all the elements. Note that this function only erases
892 * the elements, and that if the elements themselves are
893 * pointers, the pointed-to memory is not touched in any way.
894 * Managing the pointer is the user's responsibilty.
903 // [23.2.2.4] list operations
905 * @brief Insert contents of another %list.
906 * @param position Iterator referencing the element to insert before.
907 * @param x Source list.
909 * The elements of @a x are inserted in constant time in front of
910 * the element referenced by @a position. @a x becomes an empty
914 splice(iterator __position
, list
& __x
)
917 this->_M_transfer(__position
, __x
.begin(), __x
.end());
921 * @brief Insert element from another %list.
922 * @param position Iterator referencing the element to insert before.
923 * @param x Source list.
924 * @param i Iterator referencing the element to move.
926 * Removes the element in list @a x referenced by @a i and
927 * inserts it into the current list before @a position.
930 splice(iterator __position
, list
&, iterator __i
)
934 if (__position
== __i
|| __position
== __j
)
936 this->_M_transfer(__position
, __i
, __j
);
940 * @brief Insert range from another %list.
941 * @param position Iterator referencing the element to insert before.
942 * @param x Source list.
943 * @param first Iterator referencing the start of range in x.
944 * @param last Iterator referencing the end of range in x.
946 * Removes elements in the range [first,last) and inserts them
947 * before @a position in constant time.
949 * Undefined if @a position is in [first,last).
952 splice(iterator __position
, list
&, iterator __first
, iterator __last
)
954 if (__first
!= __last
)
955 this->_M_transfer(__position
, __first
, __last
);
959 * @brief Remove all elements equal to value.
960 * @param value The value to remove.
962 * Removes every element in the list equal to @a value.
963 * Remaining elements stay in list order. Note that this
964 * function only erases the elements, and that if the elements
965 * themselves are pointers, the pointed-to memory is not
966 * touched in any way. Managing the pointer is the user's
970 remove(const _Tp
& __value
);
973 * @brief Remove all elements satisfying a predicate.
974 * @param Predicate Unary predicate function or object.
976 * Removes every element in the list for which the predicate
977 * returns true. Remaining elements stay in list order. Note
978 * that this function only erases the elements, and that if the
979 * elements themselves are pointers, the pointed-to memory is
980 * not touched in any way. Managing the pointer is the user's
983 template<typename _Predicate
>
985 remove_if(_Predicate
);
988 * @brief Remove consecutive duplicate elements.
990 * For each consecutive set of elements with the same value,
991 * remove all but the first one. Remaining elements stay in
992 * list order. Note that this function only erases the
993 * elements, and that if the elements themselves are pointers,
994 * the pointed-to memory is not touched in any way. Managing
995 * the pointer is the user's responsibilty.
1001 * @brief Remove consecutive elements satisfying a predicate.
1002 * @param BinaryPredicate Binary predicate function or object.
1004 * For each consecutive set of elements [first,last) that
1005 * satisfy predicate(first,i) where i is an iterator in
1006 * [first,last), remove all but the first one. Remaining
1007 * elements stay in list order. Note that this function only
1008 * erases the elements, and that if the elements themselves are
1009 * pointers, the pointed-to memory is not touched in any way.
1010 * Managing the pointer is the user's responsibilty.
1012 template<typename _BinaryPredicate
>
1014 unique(_BinaryPredicate
);
1017 * @brief Merge sorted lists.
1018 * @param x Sorted list to merge.
1020 * Assumes that both @a x and this list are sorted according to
1021 * operator<(). Merges elements of @a x into this list in
1022 * sorted order, leaving @a x empty when complete. Elements in
1023 * this list precede elements in @a x that are equal.
1029 * @brief Merge sorted lists according to comparison function.
1030 * @param x Sorted list to merge.
1031 * @param StrictWeakOrdering Comparison function definining
1034 * Assumes that both @a x and this list are sorted according to
1035 * StrictWeakOrdering. Merges elements of @a x into this list
1036 * in sorted order, leaving @a x empty when complete. Elements
1037 * in this list precede elements in @a x that are equivalent
1038 * according to StrictWeakOrdering().
1040 template<typename _StrictWeakOrdering
>
1042 merge(list
&, _StrictWeakOrdering
);
1045 * @brief Reverse the elements in list.
1047 * Reverse the order of elements in the list in linear time.
1051 { this->_M_impl
._M_node
.reverse(); }
1054 * @brief Sort the elements.
1056 * Sorts the elements of this list in NlogN time. Equivalent
1057 * elements remain in list order.
1063 * @brief Sort the elements according to comparison function.
1065 * Sorts the elements of this list in NlogN time. Equivalent
1066 * elements remain in list order.
1068 template<typename _StrictWeakOrdering
>
1070 sort(_StrictWeakOrdering
);
1073 // Internal assign functions follow.
1075 // Called by the range assign to implement [23.1.1]/9
1076 template<typename _Integer
>
1078 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1080 _M_fill_assign(static_cast<size_type
>(__n
),
1081 static_cast<value_type
>(__val
));
1084 // Called by the range assign to implement [23.1.1]/9
1085 template<typename _InputIterator
>
1087 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1090 // Called by assign(n,t), and the range assign when it turns out
1091 // to be the same thing.
1093 _M_fill_assign(size_type __n
, const value_type
& __val
);
1096 // Internal insert functions follow.
1098 // Called by the range insert to implement [23.1.1]/9
1099 template<typename _Integer
>
1101 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
1104 _M_fill_insert(__pos
, static_cast<size_type
>(__n
),
1105 static_cast<value_type
>(__x
));
1108 // Called by the range insert to implement [23.1.1]/9
1109 template<typename _InputIterator
>
1111 _M_insert_dispatch(iterator __pos
,
1112 _InputIterator __first
, _InputIterator __last
,
1115 for (; __first
!= __last
; ++__first
)
1116 _M_insert(__pos
, *__first
);
1119 // Called by insert(p,n,x), and the range insert when it turns out
1120 // to be the same thing.
1122 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
)
1124 for (; __n
> 0; --__n
)
1125 _M_insert(__pos
, __x
);
1129 // Moves the elements from [first,last) before position.
1131 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1132 { __position
._M_node
->transfer(__first
._M_node
, __last
._M_node
); }
1134 // Inserts new element at position given and with value given.
1136 _M_insert(iterator __position
, const value_type
& __x
)
1138 _Node
* __tmp
= _M_create_node(__x
);
1139 __tmp
->hook(__position
._M_node
);
1142 // Erases element at position given.
1144 _M_erase(iterator __position
)
1146 __position
._M_node
->unhook();
1147 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1148 _M_get_Tp_allocator().destroy(&__n
->_M_data
);
1154 * @brief List equality comparison.
1156 * @param y A %list of the same type as @a x.
1157 * @return True iff the size and elements of the lists are equal.
1159 * This is an equivalence relation. It is linear in the size of
1160 * the lists. Lists are considered equivalent if their sizes are
1161 * equal, and if corresponding elements compare equal.
1163 template<typename _Tp
, typename _Alloc
>
1165 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1167 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1168 const_iterator __end1
= __x
.end();
1169 const_iterator __end2
= __y
.end();
1171 const_iterator __i1
= __x
.begin();
1172 const_iterator __i2
= __y
.begin();
1173 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1178 return __i1
== __end1
&& __i2
== __end2
;
1182 * @brief List ordering relation.
1184 * @param y A %list of the same type as @a x.
1185 * @return True iff @a x is lexicographically less than @a y.
1187 * This is a total ordering relation. It is linear in the size of the
1188 * lists. The elements must be comparable with @c <.
1190 * See std::lexicographical_compare() for how the determination is made.
1192 template<typename _Tp
, typename _Alloc
>
1194 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1195 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1196 __y
.begin(), __y
.end()); }
1198 /// Based on operator==
1199 template<typename _Tp
, typename _Alloc
>
1201 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1202 { return !(__x
== __y
); }
1204 /// Based on operator<
1205 template<typename _Tp
, typename _Alloc
>
1207 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1208 { return __y
< __x
; }
1210 /// Based on operator<
1211 template<typename _Tp
, typename _Alloc
>
1213 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1214 { return !(__y
< __x
); }
1216 /// Based on operator<
1217 template<typename _Tp
, typename _Alloc
>
1219 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1220 { return !(__x
< __y
); }
1222 /// See std::list::swap().
1223 template<typename _Tp
, typename _Alloc
>
1225 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1229 #endif /* _LIST_H */