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
)
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
;
308 _List_impl(const _Node_alloc_type
& __a
)
309 : _Node_alloc_type(__a
), _M_node()
317 { return _M_impl
._Node_alloc_type::allocate(1); }
320 _M_put_node(_List_node
<_Tp
>* __p
)
321 { _M_impl
._Node_alloc_type::deallocate(__p
, 1); }
324 typedef _Alloc allocator_type
;
327 _M_get_Node_allocator()
328 { return *static_cast<_Node_alloc_type
*>(&this->_M_impl
); }
330 const _Node_alloc_type
&
331 _M_get_Node_allocator() const
332 { return *static_cast<const _Node_alloc_type
*>(&this->_M_impl
); }
335 _M_get_Tp_allocator() const
336 { return _Tp_alloc_type(_M_get_Node_allocator()); }
339 get_allocator() const
340 { return allocator_type(_M_get_Node_allocator()); }
342 _List_base(const allocator_type
& __a
)
346 // This is what actually destroys the list.
356 this->_M_impl
._M_node
._M_next
= &this->_M_impl
._M_node
;
357 this->_M_impl
._M_node
._M_prev
= &this->_M_impl
._M_node
;
362 * @brief A standard container with linear time access to elements,
363 * and fixed time insertion/deletion at any point in the sequence.
365 * @ingroup Containers
368 * Meets the requirements of a <a href="tables.html#65">container</a>, a
369 * <a href="tables.html#66">reversible container</a>, and a
370 * <a href="tables.html#67">sequence</a>, including the
371 * <a href="tables.html#68">optional sequence requirements</a> with the
372 * %exception of @c at and @c operator[].
374 * This is a @e doubly @e linked %list. Traversal up and down the
375 * %list requires linear time, but adding and removing elements (or
376 * @e nodes) is done in constant time, regardless of where the
377 * change takes place. Unlike std::vector and std::deque,
378 * random-access iterators are not provided, so subscripting ( @c
379 * [] ) access is not allowed. For algorithms which only need
380 * sequential access, this lack makes no difference.
382 * Also unlike the other standard containers, std::list provides
383 * specialized algorithms %unique to linked lists, such as
384 * splicing, sorting, and in-place reversal.
387 * A couple points on memory allocation for list<Tp>:
389 * First, we never actually allocate a Tp, we allocate
390 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
391 * that after elements from %list<X,Alloc1> are spliced into
392 * %list<X,Alloc2>, destroying the memory of the second %list is a
393 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
395 * Second, a %list conceptually represented as
397 * A <---> B <---> C <---> D
399 * is actually circular; a link exists between A and D. The %list
400 * class holds (as its only data member) a private list::iterator
401 * pointing to @e D, not to @e A! To get to the head of the %list,
402 * we start at the tail and move forward by one. When this member
403 * iterator's next/previous pointers refer to itself, the %list is
406 template<typename _Tp
, typename _Alloc
= std::allocator
<_Tp
> >
407 class list
: protected _List_base
<_Tp
, _Alloc
>
409 // concept requirements
410 typedef typename
_Alloc::value_type _Alloc_value_type
;
411 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
412 __glibcxx_class_requires2(_Tp
, _Alloc_value_type
, _SameTypeConcept
)
414 typedef _List_base
<_Tp
, _Alloc
> _Base
;
415 typedef typename
_Base::_Tp_alloc_type _Tp_alloc_type
;
418 typedef _Tp value_type
;
419 typedef typename
_Tp_alloc_type::pointer pointer
;
420 typedef typename
_Tp_alloc_type::const_pointer const_pointer
;
421 typedef typename
_Tp_alloc_type::reference reference
;
422 typedef typename
_Tp_alloc_type::const_reference const_reference
;
423 typedef _List_iterator
<_Tp
> iterator
;
424 typedef _List_const_iterator
<_Tp
> const_iterator
;
425 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
426 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
427 typedef size_t size_type
;
428 typedef ptrdiff_t difference_type
;
429 typedef _Alloc allocator_type
;
432 // Note that pointers-to-_Node's can be ctor-converted to
434 typedef _List_node
<_Tp
> _Node
;
436 using _Base::_M_impl
;
437 using _Base::_M_put_node
;
438 using _Base::_M_get_node
;
439 using _Base::_M_get_Tp_allocator
;
440 using _Base::_M_get_Node_allocator
;
444 * @param x An instance of user data.
446 * Allocates space for a new node and constructs a copy of @a x in it.
450 _M_create_node(const value_type
& __x
)
452 _Node
* __p
= this->_M_get_node();
455 _M_get_Tp_allocator().construct(&__p
->_M_data
, __x
);
460 __throw_exception_again
;
466 // [23.2.2.1] construct/copy/destroy
467 // (assign() and get_allocator() are also listed in this section)
469 * @brief Default constructor creates no elements.
472 list(const allocator_type
& __a
= allocator_type())
476 * @brief Create a %list with copies of an exemplar element.
477 * @param n The number of elements to initially create.
478 * @param value An element to copy.
480 * This constructor fills the %list with @a n copies of @a value.
483 list(size_type __n
, const value_type
& __value
= value_type(),
484 const allocator_type
& __a
= allocator_type())
486 { _M_fill_initialize(__n
, __value
); }
489 * @brief %List copy constructor.
490 * @param x A %list of identical element and allocator types.
492 * The newly-created %list uses a copy of the allocation object used
495 list(const list
& __x
)
496 : _Base(__x
._M_get_Node_allocator())
497 { _M_initialize_dispatch(__x
.begin(), __x
.end(), __false_type()); }
500 * @brief Builds a %list from a range.
501 * @param first An input iterator.
502 * @param last An input iterator.
504 * Create a %list consisting of copies of the elements from
505 * [@a first,@a last). This is linear in N (where N is
506 * distance(@a first,@a last)).
508 template<typename _InputIterator
>
509 list(_InputIterator __first
, _InputIterator __last
,
510 const allocator_type
& __a
= allocator_type())
513 // Check whether it's an integral type. If so, it's not an iterator.
514 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
515 _M_initialize_dispatch(__first
, __last
, _Integral());
519 * No explicit dtor needed as the _Base dtor takes care of
520 * things. The _Base dtor only erases the elements, and note
521 * that if the elements themselves are pointers, the pointed-to
522 * memory is not touched in any way. Managing the pointer is
523 * the user's responsibilty.
527 * @brief %List assignment operator.
528 * @param x A %list of identical element and allocator types.
530 * All the elements of @a x are copied, but unlike the copy
531 * constructor, the allocator object is not copied.
534 operator=(const list
& __x
);
537 * @brief Assigns a given value to a %list.
538 * @param n Number of elements to be assigned.
539 * @param val Value to be assigned.
541 * This function fills a %list with @a n copies of the given
542 * value. Note that the assignment completely changes the %list
543 * and that the resulting %list's size is the same as the number
544 * of elements assigned. Old data may be lost.
547 assign(size_type __n
, const value_type
& __val
)
548 { _M_fill_assign(__n
, __val
); }
551 * @brief Assigns a range to a %list.
552 * @param first An input iterator.
553 * @param last An input iterator.
555 * This function fills a %list with copies of the elements in the
556 * range [@a first,@a last).
558 * Note that the assignment completely changes the %list and
559 * that the resulting %list's size is the same as the number of
560 * elements assigned. Old data may be lost.
562 template<typename _InputIterator
>
564 assign(_InputIterator __first
, _InputIterator __last
)
566 // Check whether it's an integral type. If so, it's not an iterator.
567 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
568 _M_assign_dispatch(__first
, __last
, _Integral());
571 /// Get a copy of the memory allocation object.
573 get_allocator() const
574 { return _Base::get_allocator(); }
578 * Returns a read/write iterator that points to the first element in the
579 * %list. Iteration is done in ordinary element order.
583 { return iterator(this->_M_impl
._M_node
._M_next
); }
586 * Returns a read-only (constant) iterator that points to the
587 * first element in the %list. Iteration is done in ordinary
592 { return const_iterator(this->_M_impl
._M_node
._M_next
); }
595 * Returns a read/write iterator that points one past the last
596 * element in the %list. Iteration is done in ordinary element
601 { return iterator(&this->_M_impl
._M_node
); }
604 * Returns a read-only (constant) iterator that points one past
605 * the last element in the %list. Iteration is done in ordinary
610 { return const_iterator(&this->_M_impl
._M_node
); }
613 * Returns a read/write reverse iterator that points to the last
614 * element in the %list. Iteration is done in reverse element
619 { return reverse_iterator(end()); }
622 * Returns a read-only (constant) reverse iterator that points to
623 * the last element in the %list. Iteration is done in reverse
626 const_reverse_iterator
628 { return const_reverse_iterator(end()); }
631 * Returns a read/write reverse iterator that points to one
632 * before the first element in the %list. Iteration is done in
633 * reverse element order.
637 { return reverse_iterator(begin()); }
640 * Returns a read-only (constant) reverse iterator that points to one
641 * before the first element in the %list. Iteration is done in reverse
644 const_reverse_iterator
646 { return const_reverse_iterator(begin()); }
648 // [23.2.2.2] capacity
650 * Returns true if the %list is empty. (Thus begin() would equal
655 { return this->_M_impl
._M_node
._M_next
== &this->_M_impl
._M_node
; }
657 /** Returns the number of elements in the %list. */
660 { return std::distance(begin(), end()); }
662 /** Returns the size() of the largest possible %list. */
665 { return _M_get_Tp_allocator().max_size(); }
668 * @brief Resizes the %list to the specified number of elements.
669 * @param new_size Number of elements the %list should contain.
670 * @param x Data with which new elements should be populated.
672 * This function will %resize the %list to the specified number
673 * of elements. If the number is smaller than the %list's
674 * current size the %list is truncated, otherwise the %list is
675 * extended and new elements are populated with given data.
678 resize(size_type __new_size
, value_type __x
= value_type());
682 * Returns a read/write reference to the data at the first
683 * element of the %list.
690 * Returns a read-only (constant) reference to the data at the first
691 * element of the %list.
698 * Returns a read/write reference to the data at the last element
704 iterator __tmp
= end();
710 * Returns a read-only (constant) reference to the data at the last
711 * element of the %list.
716 const_iterator __tmp
= end();
721 // [23.2.2.3] modifiers
723 * @brief Add data to the front of the %list.
724 * @param x Data to be added.
726 * This is a typical stack operation. The function creates an
727 * element at the front of the %list and assigns the given data
728 * to it. Due to the nature of a %list this operation can be
729 * done in constant time, and does not invalidate iterators and
733 push_front(const value_type
& __x
)
734 { this->_M_insert(begin(), __x
); }
737 * @brief Removes first element.
739 * This is a typical stack operation. It shrinks the %list by
740 * one. Due to the nature of a %list this operation can be done
741 * in constant time, and only invalidates iterators/references to
742 * the element being removed.
744 * Note that no data is returned, and if the first element's data
745 * is needed, it should be retrieved before pop_front() is
750 { this->_M_erase(begin()); }
753 * @brief Add data to the end of the %list.
754 * @param x Data to be added.
756 * This is a typical stack operation. The function creates an
757 * element at the end of the %list and assigns the given data to
758 * it. Due to the nature of a %list this operation can be done
759 * in constant time, and does not invalidate iterators and
763 push_back(const value_type
& __x
)
764 { this->_M_insert(end(), __x
); }
767 * @brief Removes last element.
769 * This is a typical stack operation. It shrinks the %list by
770 * one. Due to the nature of a %list this operation can be done
771 * in constant time, and only invalidates iterators/references to
772 * the element being removed.
774 * Note that no data is returned, and if the last element's data
775 * is needed, it should be retrieved before pop_back() is called.
779 { this->_M_erase(iterator(this->_M_impl
._M_node
._M_prev
)); }
782 * @brief Inserts given value into %list before specified iterator.
783 * @param position An iterator into the %list.
784 * @param x Data to be inserted.
785 * @return An iterator that points to the inserted data.
787 * This function will insert a copy of the given value before
788 * the specified location. Due to the nature of a %list this
789 * operation can be done in constant time, and does not
790 * invalidate iterators and references.
793 insert(iterator __position
, const value_type
& __x
);
796 * @brief Inserts a number of copies of given data into the %list.
797 * @param position An iterator into the %list.
798 * @param n Number of elements to be inserted.
799 * @param x Data to be inserted.
801 * This function will insert a specified number of copies of the
802 * given data before the location specified by @a position.
804 * This operation is linear in the number of elements inserted and
805 * does not invalidate iterators and references.
808 insert(iterator __position
, size_type __n
, const value_type
& __x
)
810 list
__tmp(__n
, __x
, _M_get_Node_allocator());
811 splice(__position
, __tmp
);
815 * @brief Inserts a range into the %list.
816 * @param position An iterator into the %list.
817 * @param first An input iterator.
818 * @param last An input iterator.
820 * This function will insert copies of the data in the range [@a
821 * first,@a last) into the %list before the location specified by
824 * This operation is linear in the number of elements inserted and
825 * does not invalidate iterators and references.
827 template<typename _InputIterator
>
829 insert(iterator __position
, _InputIterator __first
,
830 _InputIterator __last
)
832 list
__tmp(__first
, __last
, _M_get_Node_allocator());
833 splice(__position
, __tmp
);
837 * @brief Remove element at given position.
838 * @param position Iterator pointing to element to be erased.
839 * @return An iterator pointing to the next element (or end()).
841 * This function will erase the element at the given position and thus
842 * shorten the %list by one.
844 * Due to the nature of a %list this operation can be done in
845 * constant time, and only invalidates iterators/references to
846 * the element being removed. The user is also cautioned that
847 * this function only erases the element, and that if the element
848 * is itself a pointer, the pointed-to memory is not touched in
849 * any way. Managing the pointer is the user's responsibilty.
852 erase(iterator __position
);
855 * @brief Remove a range of elements.
856 * @param first Iterator pointing to the first element to be erased.
857 * @param last Iterator pointing to one past the last element to be
859 * @return An iterator pointing to the element pointed to by @a last
860 * prior to erasing (or end()).
862 * This function will erase the elements in the range @a
863 * [first,last) and shorten the %list accordingly.
865 * This operation is linear time in the size of the range and only
866 * invalidates iterators/references to the element being removed.
867 * The user is also cautioned that this function only erases the
868 * elements, and that if the elements themselves are pointers, the
869 * pointed-to memory is not touched in any way. Managing the pointer
870 * is the user's responsibilty.
873 erase(iterator __first
, iterator __last
)
875 while (__first
!= __last
)
876 __first
= erase(__first
);
881 * @brief Swaps data with another %list.
882 * @param x A %list of the same element and allocator types.
884 * This exchanges the elements between two lists in constant
885 * time. Note that the global std::swap() function is
886 * specialized such that std::swap(l1,l2) will feed to this
892 _List_node_base::swap(this->_M_impl
._M_node
, __x
._M_impl
._M_node
);
894 // _GLIBCXX_RESOLVE_LIB_DEFECTS
895 // 431. Swapping containers with unequal allocators.
896 std::__alloc_swap
<typename
_Base::_Node_alloc_type
>::
897 _S_do_it(_M_get_Node_allocator(), __x
._M_get_Node_allocator());
901 * Erases all the elements. Note that this function only erases
902 * the elements, and that if the elements themselves are
903 * pointers, the pointed-to memory is not touched in any way.
904 * Managing the pointer is the user's responsibilty.
913 // [23.2.2.4] list operations
915 * @brief Insert contents of another %list.
916 * @param position Iterator referencing the element to insert before.
917 * @param x Source list.
919 * The elements of @a x are inserted in constant time in front of
920 * the element referenced by @a position. @a x becomes an empty
923 * Requires this != @a x.
926 splice(iterator __position
, list
& __x
)
930 _M_check_equal_allocators(__x
);
932 this->_M_transfer(__position
, __x
.begin(), __x
.end());
937 * @brief Insert element from another %list.
938 * @param position Iterator referencing the element to insert before.
939 * @param x Source list.
940 * @param i Iterator referencing the element to move.
942 * Removes the element in list @a x referenced by @a i and
943 * inserts it into the current list before @a position.
946 splice(iterator __position
, list
& __x
, iterator __i
)
950 if (__position
== __i
|| __position
== __j
)
954 _M_check_equal_allocators(__x
);
956 this->_M_transfer(__position
, __i
, __j
);
960 * @brief Insert range from another %list.
961 * @param position Iterator referencing the element to insert before.
962 * @param x Source list.
963 * @param first Iterator referencing the start of range in x.
964 * @param last Iterator referencing the end of range in x.
966 * Removes elements in the range [first,last) and inserts them
967 * before @a position in constant time.
969 * Undefined if @a position is in [first,last).
972 splice(iterator __position
, list
& __x
, iterator __first
, iterator __last
)
974 if (__first
!= __last
)
977 _M_check_equal_allocators(__x
);
979 this->_M_transfer(__position
, __first
, __last
);
984 * @brief Remove all elements equal to value.
985 * @param value The value to remove.
987 * Removes every element in the list equal to @a value.
988 * Remaining elements stay in list order. Note that this
989 * function only erases the elements, and that if the elements
990 * themselves are pointers, the pointed-to memory is not
991 * touched in any way. Managing the pointer is the user's
995 remove(const _Tp
& __value
);
998 * @brief Remove all elements satisfying a predicate.
999 * @param Predicate Unary predicate function or object.
1001 * Removes every element in the list for which the predicate
1002 * returns true. Remaining elements stay in list order. Note
1003 * that this function only erases the elements, and that if the
1004 * elements themselves are pointers, the pointed-to memory is
1005 * not touched in any way. Managing the pointer is the user's
1008 template<typename _Predicate
>
1010 remove_if(_Predicate
);
1013 * @brief Remove consecutive duplicate elements.
1015 * For each consecutive set of elements with the same value,
1016 * remove all but the first one. Remaining elements stay in
1017 * list order. Note that this function only erases the
1018 * elements, and that if the elements themselves are pointers,
1019 * the pointed-to memory is not touched in any way. Managing
1020 * the pointer is the user's responsibilty.
1026 * @brief Remove consecutive elements satisfying a predicate.
1027 * @param BinaryPredicate Binary predicate function or object.
1029 * For each consecutive set of elements [first,last) that
1030 * satisfy predicate(first,i) where i is an iterator in
1031 * [first,last), remove all but the first one. Remaining
1032 * elements stay in list order. Note that this function only
1033 * erases the elements, and that if the elements themselves are
1034 * pointers, the pointed-to memory is not touched in any way.
1035 * Managing the pointer is the user's responsibilty.
1037 template<typename _BinaryPredicate
>
1039 unique(_BinaryPredicate
);
1042 * @brief Merge sorted lists.
1043 * @param x Sorted list to merge.
1045 * Assumes that both @a x and this list are sorted according to
1046 * operator<(). Merges elements of @a x into this list in
1047 * sorted order, leaving @a x empty when complete. Elements in
1048 * this list precede elements in @a x that are equal.
1054 * @brief Merge sorted lists according to comparison function.
1055 * @param x Sorted list to merge.
1056 * @param StrictWeakOrdering Comparison function definining
1059 * Assumes that both @a x and this list are sorted according to
1060 * StrictWeakOrdering. Merges elements of @a x into this list
1061 * in sorted order, leaving @a x empty when complete. Elements
1062 * in this list precede elements in @a x that are equivalent
1063 * according to StrictWeakOrdering().
1065 template<typename _StrictWeakOrdering
>
1067 merge(list
&, _StrictWeakOrdering
);
1070 * @brief Reverse the elements in list.
1072 * Reverse the order of elements in the list in linear time.
1076 { this->_M_impl
._M_node
.reverse(); }
1079 * @brief Sort the elements.
1081 * Sorts the elements of this list in NlogN time. Equivalent
1082 * elements remain in list order.
1088 * @brief Sort the elements according to comparison function.
1090 * Sorts the elements of this list in NlogN time. Equivalent
1091 * elements remain in list order.
1093 template<typename _StrictWeakOrdering
>
1095 sort(_StrictWeakOrdering
);
1098 // Internal constructor functions follow.
1100 // Called by the range constructor to implement [23.1.1]/9
1102 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1103 // 438. Ambiguity in the "do the right thing" clause
1104 template<typename _Integer
>
1106 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1107 { _M_fill_initialize(static_cast<size_type
>(__n
), __x
); }
1109 // Called by the range constructor to implement [23.1.1]/9
1110 template<typename _InputIterator
>
1112 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1115 for (; __first
!= __last
; ++__first
)
1116 push_back(*__first
);
1119 // Called by list(n,v,a), and the range constructor when it turns out
1120 // to be the same thing.
1122 _M_fill_initialize(size_type __n
, const value_type
& __x
)
1124 for (; __n
> 0; --__n
)
1129 // Internal assign functions follow.
1131 // Called by the range assign to implement [23.1.1]/9
1133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1134 // 438. Ambiguity in the "do the right thing" clause
1135 template<typename _Integer
>
1137 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1138 { _M_fill_assign(__n
, __val
); }
1140 // Called by the range assign to implement [23.1.1]/9
1141 template<typename _InputIterator
>
1143 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1146 // Called by assign(n,t), and the range assign when it turns out
1147 // to be the same thing.
1149 _M_fill_assign(size_type __n
, const value_type
& __val
);
1152 // Moves the elements from [first,last) before position.
1154 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1155 { __position
._M_node
->transfer(__first
._M_node
, __last
._M_node
); }
1157 // Inserts new element at position given and with value given.
1159 _M_insert(iterator __position
, const value_type
& __x
)
1161 _Node
* __tmp
= _M_create_node(__x
);
1162 __tmp
->hook(__position
._M_node
);
1165 // Erases element at position given.
1167 _M_erase(iterator __position
)
1169 __position
._M_node
->unhook();
1170 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1171 _M_get_Tp_allocator().destroy(&__n
->_M_data
);
1175 // To implement the splice (and merge) bits of N1599.
1177 _M_check_equal_allocators(list
& __x
)
1179 if (_M_get_Node_allocator() != __x
._M_get_Node_allocator())
1180 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1185 * @brief List equality comparison.
1187 * @param y A %list of the same type as @a x.
1188 * @return True iff the size and elements of the lists are equal.
1190 * This is an equivalence relation. It is linear in the size of
1191 * the lists. Lists are considered equivalent if their sizes are
1192 * equal, and if corresponding elements compare equal.
1194 template<typename _Tp
, typename _Alloc
>
1196 operator==(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1198 typedef typename list
<_Tp
, _Alloc
>::const_iterator const_iterator
;
1199 const_iterator __end1
= __x
.end();
1200 const_iterator __end2
= __y
.end();
1202 const_iterator __i1
= __x
.begin();
1203 const_iterator __i2
= __y
.begin();
1204 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1209 return __i1
== __end1
&& __i2
== __end2
;
1213 * @brief List ordering relation.
1215 * @param y A %list of the same type as @a x.
1216 * @return True iff @a x is lexicographically less than @a y.
1218 * This is a total ordering relation. It is linear in the size of the
1219 * lists. The elements must be comparable with @c <.
1221 * See std::lexicographical_compare() for how the determination is made.
1223 template<typename _Tp
, typename _Alloc
>
1225 operator<(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1226 { return std::lexicographical_compare(__x
.begin(), __x
.end(),
1227 __y
.begin(), __y
.end()); }
1229 /// Based on operator==
1230 template<typename _Tp
, typename _Alloc
>
1232 operator!=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1233 { return !(__x
== __y
); }
1235 /// Based on operator<
1236 template<typename _Tp
, typename _Alloc
>
1238 operator>(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1239 { return __y
< __x
; }
1241 /// Based on operator<
1242 template<typename _Tp
, typename _Alloc
>
1244 operator<=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1245 { return !(__y
< __x
); }
1247 /// Based on operator<
1248 template<typename _Tp
, typename _Alloc
>
1250 operator>=(const list
<_Tp
, _Alloc
>& __x
, const list
<_Tp
, _Alloc
>& __y
)
1251 { return !(__x
< __y
); }
1253 /// See std::list::swap().
1254 template<typename _Tp
, typename _Alloc
>
1256 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1259 _GLIBCXX_END_NESTED_NAMESPACE
1261 #endif /* _LIST_H */