1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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>
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
79 static void swap(_List_node_base
& __x
,
80 _List_node_base
& __y
);
82 void transfer(_List_node_base
* const __first
,
83 _List_node_base
* const __last
);
86 void hook(_List_node_base
* const __position
);
90 /// @if maint An actual node in the %list. @endif
91 template<typename _Tp
>
92 struct _List_node
: public _List_node_base
94 _Tp _M_data
; ///< User's data.
99 * @brief A list::iterator.
102 * All the functions are op overloads.
105 template<typename _Tp
>
106 struct _List_iterator
108 typedef _List_iterator
<_Tp
> _Self
;
109 typedef _List_node
<_Tp
> _Node
;
111 typedef ptrdiff_t difference_type
;
112 typedef bidirectional_iterator_tag iterator_category
;
113 typedef _Tp value_type
;
114 typedef _Tp
* pointer
;
115 typedef _Tp
& reference
;
119 _List_iterator(_List_node_base
* __x
)
122 // Must downcast from List_node_base to _List_node to get to _M_data.
125 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
129 { return &static_cast<_Node
*>(_M_node
)->_M_data
; }
134 _M_node
= _M_node
->_M_next
;
142 _M_node
= _M_node
->_M_next
;
149 _M_node
= _M_node
->_M_prev
;
157 _M_node
= _M_node
->_M_prev
;
162 operator==(const _Self
& __x
) const
163 { return _M_node
== __x
._M_node
; }
166 operator!=(const _Self
& __x
) const
167 { return _M_node
!= __x
._M_node
; }
169 // The only member points to the %list element.
170 _List_node_base
* _M_node
;
174 * @brief A list::const_iterator.
177 * All the functions are op overloads.
180 template<typename _Tp
>
181 struct _List_const_iterator
183 typedef _List_const_iterator
<_Tp
> _Self
;
184 typedef const _List_node
<_Tp
> _Node
;
185 typedef _List_iterator
<_Tp
> iterator
;
187 typedef ptrdiff_t difference_type
;
188 typedef bidirectional_iterator_tag iterator_category
;
189 typedef _Tp value_type
;
190 typedef const _Tp
* pointer
;
191 typedef const _Tp
& reference
;
193 _List_const_iterator() { }
195 _List_const_iterator(const _List_node_base
* __x
)
198 _List_const_iterator(const iterator
& __x
)
199 : _M_node(__x
._M_node
) { }
201 // Must downcast from List_node_base to _List_node to get to
205 { return static_cast<_Node
*>(_M_node
)->_M_data
; }
209 { return &static_cast<_Node
*>(_M_node
)->_M_data
; }
214 _M_node
= _M_node
->_M_next
;
222 _M_node
= _M_node
->_M_next
;
229 _M_node
= _M_node
->_M_prev
;
237 _M_node
= _M_node
->_M_prev
;
242 operator==(const _Self
& __x
) const
243 { return _M_node
== __x
._M_node
; }
246 operator!=(const _Self
& __x
) const
247 { return _M_node
!= __x
._M_node
; }
249 // The only member points to the %list element.
250 const _List_node_base
* _M_node
;
253 template<typename _Val
>
255 operator==(const _List_iterator
<_Val
>& __x
,
256 const _List_const_iterator
<_Val
>& __y
)
257 { return __x
._M_node
== __y
._M_node
; }
259 template<typename _Val
>
261 operator!=(const _List_iterator
<_Val
>& __x
,
262 const _List_const_iterator
<_Val
>& __y
)
263 { return __x
._M_node
!= __y
._M_node
; }
268 * See bits/stl_deque.h's _Deque_base for an explanation.
271 template<typename _Tp
, typename _Alloc
>
273 : public _Alloc::template rebind
<_List_node
<_Tp
> >::other
277 // The stored instance is not actually of "allocator_type"'s
278 // type. Instead we rebind the type to
279 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
280 // should probably be the same. List_node<Tp> is not the same
281 // size as Tp (it's two pointers larger), and specializations on
282 // Tp may go unused because List_node<Tp> is being bound
285 // We put this to the test in the constructors and in
286 // get_allocator, where we use conversions between
287 // allocator_type and _Node_Alloc_type. The conversion is
288 // required by table 32 in [20.1.5].
289 typedef typename
_Alloc::template rebind
<_List_node
<_Tp
> >::other
292 _List_node_base _M_node
;
296 { return _Node_Alloc_type::allocate(1); }
299 _M_put_node(_List_node
<_Tp
>* __p
)
300 { _Node_Alloc_type::deallocate(__p
, 1); }
303 typedef _Alloc allocator_type
;
306 get_allocator() const
307 { return allocator_type(*static_cast<const _Node_Alloc_type
*>(this)); }
309 _List_base(const allocator_type
& __a
)
310 : _Node_Alloc_type(__a
)
313 // This is what actually destroys the list.
323 this->_M_node
._M_next
= &this->_M_node
;
324 this->_M_node
._M_prev
= &this->_M_node
;
330 * @brief A standard container with linear time access to elements,
331 * and fixed time insertion/deletion at any point in the sequence.
333 * @ingroup Containers
336 * Meets the requirements of a <a href="tables.html#65">container</a>, a
337 * <a href="tables.html#66">reversible container</a>, and a
338 * <a href="tables.html#67">sequence</a>, including the
339 * <a href="tables.html#68">optional sequence requirements</a> with the
340 * %exception of @c at and @c operator[].
342 * This is a @e doubly @e linked %list. Traversal up and down the
343 * %list requires linear time, but adding and removing elements (or
344 * @e nodes) is done in constant time, regardless of where the
345 * change takes place. Unlike std::vector and std::deque,
346 * random-access iterators are not provided, so subscripting ( @c
347 * [] ) access is not allowed. For algorithms which only need
348 * sequential access, this lack makes no difference.
350 * Also unlike the other standard containers, std::list provides
351 * specialized algorithms %unique to linked lists, such as
352 * splicing, sorting, and in-place reversal.
355 * A couple points on memory allocation for list<Tp>:
357 * First, we never actually allocate a Tp, we allocate
358 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
359 * that after elements from %list<X,Alloc1> are spliced into
360 * %list<X,Alloc2>, destroying the memory of the second %list is a
361 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
363 * Second, a %list conceptually represented as
365 * A <---> B <---> C <---> D
367 * is actually circular; a link exists between A and D. The %list
368 * class holds (as its only data member) a private list::iterator
369 * pointing to @e D, not to @e A! To get to the head of the %list,
370 * we start at the tail and move forward by one. When this member
371 * iterator's next/previous pointers refer to itself, the %list is
374 template<typename _Tp
, typename _Alloc
= allocator
<_Tp
> >
375 class list
: protected _List_base
<_Tp
, _Alloc
>
377 // concept requirements
378 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
380 typedef _List_base
<_Tp
, _Alloc
> _Base
;
383 typedef _Tp value_type
;
384 typedef value_type
* pointer
;
385 typedef const value_type
* const_pointer
;
386 typedef _List_iterator
<_Tp
> iterator
;
387 typedef _List_const_iterator
<_Tp
> const_iterator
;
388 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
389 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
390 typedef value_type
& reference
;
391 typedef const value_type
& const_reference
;
392 typedef size_t size_type
;
393 typedef ptrdiff_t difference_type
;
394 typedef typename
_Base::allocator_type allocator_type
;
397 // Note that pointers-to-_Node's can be ctor-converted to
399 typedef _List_node
<_Tp
> _Node
;
402 * One data member plus two memory-handling functions. If the
403 * _Alloc type requires separate instances, then one of those
404 * will also be included, accumulated from the topmost parent.
407 using _Base::_M_node
;
408 using _Base::_M_put_node
;
409 using _Base::_M_get_node
;
413 * @param x An instance of user data.
415 * Allocates space for a new node and constructs a copy of @a x in it.
419 _M_create_node(const value_type
& __x
)
421 _Node
* __p
= this->_M_get_node();
424 std::_Construct(&__p
->_M_data
, __x
);
429 __throw_exception_again
;
436 * Allocates space for a new node and default-constructs a new
437 * instance of @c value_type in it.
443 _Node
* __p
= this->_M_get_node();
446 std::_Construct(&__p
->_M_data
);
451 __throw_exception_again
;
457 // [23.2.2.1] construct/copy/destroy
458 // (assign() and get_allocator() are also listed in this section)
460 * @brief Default constructor creates no elements.
463 list(const allocator_type
& __a
= allocator_type())
467 * @brief Create a %list with copies of an exemplar element.
468 * @param n The number of elements to initially create.
469 * @param value An element to copy.
471 * This constructor fills the %list with @a n copies of @a value.
473 list(size_type __n
, const value_type
& __value
,
474 const allocator_type
& __a
= allocator_type())
476 { this->insert(begin(), __n
, __value
); }
479 * @brief Create a %list with default elements.
480 * @param n The number of elements to initially create.
482 * This constructor fills the %list with @a n copies of a
483 * default-constructed element.
487 : _Base(allocator_type())
488 { this->insert(begin(), __n
, value_type()); }
491 * @brief %List copy constructor.
492 * @param x A %list of identical element and allocator types.
494 * The newly-created %list uses a copy of the allocation object used
497 list(const list
& __x
)
498 : _Base(__x
.get_allocator())
499 { this->insert(begin(), __x
.begin(), __x
.end()); }
502 * @brief Builds a %list from a range.
503 * @param first An input iterator.
504 * @param last An input iterator.
506 * Create a %list consisting of copies of the elements from
507 * [@a first,@a last). This is linear in N (where N is
508 * distance(@a first,@a last)).
511 * We don't need any dispatching tricks here, because insert does all of
515 template<typename _InputIterator
>
516 list(_InputIterator __first
, _InputIterator __last
,
517 const allocator_type
& __a
= allocator_type())
519 { this->insert(begin(), __first
, __last
); }
522 * No explicit dtor needed as the _Base dtor takes care of
523 * things. The _Base dtor only erases the elements, and note
524 * that if the elements themselves are pointers, the pointed-to
525 * memory is not touched in any way. Managing the pointer is
526 * the user's responsibilty.
530 * @brief %List assignment operator.
531 * @param x A %list of identical element and allocator types.
533 * All the elements of @a x are copied, but unlike the copy
534 * constructor, the allocator object is not copied.
537 operator=(const list
& __x
);
540 * @brief Assigns a given value to a %list.
541 * @param n Number of elements to be assigned.
542 * @param val Value to be assigned.
544 * This function fills a %list with @a n copies of the given
545 * value. Note that the assignment completely changes the %list
546 * and that the resulting %list's size is the same as the number
547 * of elements assigned. Old data may be lost.
550 assign(size_type __n
, const value_type
& __val
)
551 { _M_fill_assign(__n
, __val
); }
554 * @brief Assigns a range to a %list.
555 * @param first An input iterator.
556 * @param last An input iterator.
558 * This function fills a %list with copies of the elements in the
559 * range [@a first,@a last).
561 * Note that the assignment completely changes the %list and
562 * that the resulting %list's size is the same as the number of
563 * elements assigned. Old data may be lost.
565 template<typename _InputIterator
>
567 assign(_InputIterator __first
, _InputIterator __last
)
569 // Check whether it's an integral type. If so, it's not an iterator.
570 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
571 _M_assign_dispatch(__first
, __last
, _Integral());
574 /// Get a copy of the memory allocation object.
576 get_allocator() const { return _Base::get_allocator(); }
580 * Returns a read/write iterator that points to the first element in the
581 * %list. Iteration is done in ordinary element order.
584 begin() { return this->_M_node
._M_next
; }
587 * Returns a read-only (constant) iterator that points to the
588 * first element in the %list. Iteration is done in ordinary
592 begin() const { return this->_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
600 end() { return &this->_M_node
; }
603 * Returns a read-only (constant) iterator that points one past
604 * the last element in the %list. Iteration is done in ordinary
608 end() const { return &this->_M_node
; }
611 * Returns a read/write reverse iterator that points to the last
612 * element in the %list. Iteration is done in reverse element
616 rbegin() { return reverse_iterator(end()); }
619 * Returns a read-only (constant) reverse iterator that points to
620 * the last element in the %list. Iteration is done in reverse
623 const_reverse_iterator
624 rbegin() const { 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.
632 rend() { return reverse_iterator(begin()); }
635 * Returns a read-only (constant) reverse iterator that points to one
636 * before the first element in the %list. Iteration is done in reverse
639 const_reverse_iterator
641 { return const_reverse_iterator(begin()); }
643 // [23.2.2.2] capacity
645 * Returns true if the %list is empty. (Thus begin() would equal
649 empty() const { return this->_M_node
._M_next
== &this->_M_node
; }
651 /** Returns the number of elements in the %list. */
653 size() const { return std::distance(begin(), end()); }
655 /** Returns the size() of the largest possible %list. */
657 max_size() const { return size_type(-1); }
660 * @brief Resizes the %list to the specified number of elements.
661 * @param new_size Number of elements the %list should contain.
662 * @param x Data with which new elements should be populated.
664 * This function will %resize the %list to the specified number
665 * of elements. If the number is smaller than the %list's
666 * current size the %list is truncated, otherwise the %list is
667 * extended and new elements are populated with given data.
670 resize(size_type __new_size
, const value_type
& __x
);
673 * @brief Resizes the %list to the specified number of elements.
674 * @param new_size Number of elements the %list should contain.
676 * This function will resize the %list to the specified number of
677 * elements. If the number is smaller than the %list's current
678 * size the %list is truncated, otherwise the %list is extended
679 * and new elements are default-constructed.
682 resize(size_type __new_size
) { this->resize(__new_size
, value_type()); }
686 * Returns a read/write reference to the data at the first
687 * element of the %list.
690 front() { return *begin(); }
693 * Returns a read-only (constant) reference to the data at the first
694 * element of the %list.
697 front() const { return *begin(); }
700 * Returns a read/write reference to the data at the last element
704 back() { return *(--end()); }
707 * Returns a read-only (constant) reference to the data at the last
708 * element of the %list.
711 back() const { return *(--end()); }
713 // [23.2.2.3] modifiers
715 * @brief Add data to the front of the %list.
716 * @param x Data to be added.
718 * This is a typical stack operation. The function creates an
719 * element at the front of the %list and assigns the given data
720 * to it. Due to the nature of a %list this operation can be
721 * done in constant time, and does not invalidate iterators and
725 push_front(const value_type
& __x
) { this->_M_insert(begin(), __x
); }
728 * @brief Removes first element.
730 * This is a typical stack operation. It shrinks the %list by
731 * one. Due to the nature of a %list this operation can be done
732 * in constant time, and only invalidates iterators/references to
733 * the element being removed.
735 * Note that no data is returned, and if the first element's data
736 * is needed, it should be retrieved before pop_front() is
740 pop_front() { this->_M_erase(begin()); }
743 * @brief Add data to the end of the %list.
744 * @param x Data to be added.
746 * This is a typical stack operation. The function creates an
747 * element at the end of the %list and assigns the given data to
748 * it. Due to the nature of a %list this operation can be done
749 * in constant time, and does not invalidate iterators and
753 push_back(const value_type
& __x
) { this->_M_insert(end(), __x
); }
756 * @brief Removes last element.
758 * This is a typical stack operation. It shrinks the %list by
759 * one. Due to the nature of a %list this operation can be done
760 * in constant time, and only invalidates iterators/references to
761 * the element being removed.
763 * Note that no data is returned, and if the last element's data
764 * is needed, it should be retrieved before pop_back() is called.
767 pop_back() { this->_M_erase(this->_M_node
._M_prev
); }
770 * @brief Inserts given value into %list before specified iterator.
771 * @param position An iterator into the %list.
772 * @param x Data to be inserted.
773 * @return An iterator that points to the inserted data.
775 * This function will insert a copy of the given value before
776 * the specified location. Due to the nature of a %list this
777 * operation can be done in constant time, and does not
778 * invalidate iterators and references.
781 insert(iterator __position
, const value_type
& __x
);
784 * @brief Inserts a number of copies of given data into the %list.
785 * @param position An iterator into the %list.
786 * @param n Number of elements to be inserted.
787 * @param x Data to be inserted.
789 * This function will insert a specified number of copies of the
790 * given data before the location specified by @a position.
792 * Due to the nature of a %list this operation can be done in
793 * constant time, and does not invalidate iterators and
797 insert(iterator __position
, size_type __n
, const value_type
& __x
)
798 { _M_fill_insert(__position
, __n
, __x
); }
801 * @brief Inserts a range into the %list.
802 * @param position An iterator into the %list.
803 * @param first An input iterator.
804 * @param last An input iterator.
806 * This function will insert copies of the data in the range [@a
807 * first,@a last) into the %list before the location specified by
810 * Due to the nature of a %list this operation can be done in
811 * constant time, and does not invalidate iterators and
814 template<typename _InputIterator
>
816 insert(iterator __position
, _InputIterator __first
,
817 _InputIterator __last
)
819 // Check whether it's an integral type. If so, it's not an iterator.
820 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
821 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
825 * @brief Remove element at given position.
826 * @param position Iterator pointing to element to be erased.
827 * @return An iterator pointing to the next element (or end()).
829 * This function will erase the element at the given position and thus
830 * shorten the %list by one.
832 * Due to the nature of a %list this operation can be done in
833 * constant time, and only invalidates iterators/references to
834 * the element being removed. The user is also cautioned that
835 * this function only erases the element, and that if the element
836 * is itself a pointer, the pointed-to memory is not touched in
837 * any way. Managing the pointer is the user's responsibilty.
840 erase(iterator __position
);
843 * @brief Remove a range of elements.
844 * @param first Iterator pointing to the first element to be erased.
845 * @param last Iterator pointing to one past the last element to be
847 * @return An iterator pointing to the element pointed to by @a last
848 * prior to erasing (or end()).
850 * This function will erase the elements in the range @a
851 * [first,last) and shorten the %list accordingly.
853 * Due to the nature of a %list this operation can be done in
854 * constant time, and only invalidates iterators/references to
855 * the element being removed. The user is also cautioned that
856 * this function only erases the elements, and that if the
857 * elements themselves are pointers, the pointed-to memory is not
858 * touched in any way. Managing the pointer is the user's
862 erase(iterator __first
, iterator __last
)
864 while (__first
!= __last
)
865 __first
= erase(__first
);
870 * @brief Swaps data with another %list.
871 * @param x A %list of the same element and allocator types.
873 * This exchanges the elements between two lists in constant
874 * time. Note that the global std::swap() function is
875 * specialized such that std::swap(l1,l2) will feed to this
879 swap(list
& __x
) { _List_node_base::swap(this->_M_node
,__x
._M_node
); }
882 * Erases all the elements. Note that this function only erases
883 * the elements, and that if the elements themselves are
884 * pointers, the pointed-to memory is not touched in any way.
885 * Managing the pointer is the user's responsibilty.
894 // [23.2.2.4] list operations
896 * @brief Insert contents of another %list.
897 * @param position Iterator referencing the element to insert before.
898 * @param x Source list.
900 * The elements of @a x are inserted in constant time in front of
901 * the element referenced by @a position. @a x becomes an empty
905 splice(iterator __position
, list
& __x
)
908 this->_M_transfer(__position
, __x
.begin(), __x
.end());
912 * @brief Insert element from another %list.
913 * @param position Iterator referencing the element to insert before.
914 * @param x Source list.
915 * @param i Iterator referencing the element to move.
917 * Removes the element in list @a x referenced by @a i and
918 * inserts it into the current list before @a position.
921 splice(iterator __position
, list
&, iterator __i
)
925 if (__position
== __i
|| __position
== __j
) return;
926 this->_M_transfer(__position
, __i
, __j
);
930 * @brief Insert range from another %list.
931 * @param position Iterator referencing the element to insert before.
932 * @param x Source list.
933 * @param first Iterator referencing the start of range in x.
934 * @param last Iterator referencing the end of range in x.
936 * Removes elements in the range [first,last) and inserts them
937 * before @a position in constant time.
939 * Undefined if @a position is in [first,last).
942 splice(iterator __position
, list
&, iterator __first
, iterator __last
)
944 if (__first
!= __last
)
945 this->_M_transfer(__position
, __first
, __last
);
949 * @brief Remove all elements equal to value.
950 * @param value The value to remove.
952 * Removes every element in the list equal to @a value.
953 * Remaining elements stay in list order. Note that this
954 * function only erases the elements, and that if the elements
955 * themselves are pointers, the pointed-to memory is not
956 * touched in any way. Managing the pointer is the user's
960 remove(const _Tp
& __value
);
963 * @brief Remove all elements satisfying a predicate.
964 * @param Predicate Unary predicate function or object.
966 * Removes every element in the list for which the predicate
967 * returns true. Remaining elements stay in list order. Note
968 * that this function only erases the elements, and that if the
969 * elements themselves are pointers, the pointed-to memory is
970 * not touched in any way. Managing the pointer is the user's
973 template<typename _Predicate
>
975 remove_if(_Predicate
);
978 * @brief Remove consecutive duplicate elements.
980 * For each consecutive set of elements with the same value,
981 * remove all but the first one. Remaining elements stay in
982 * list order. Note that this function only erases the
983 * elements, and that if the elements themselves are pointers,
984 * the pointed-to memory is not touched in any way. Managing
985 * the pointer is the user's responsibilty.
991 * @brief Remove consecutive elements satisfying a predicate.
992 * @param BinaryPredicate Binary predicate function or object.
994 * For each consecutive set of elements [first,last) that
995 * satisfy predicate(first,i) where i is an iterator in
996 * [first,last), remove all but the first one. Remaining
997 * elements stay in list order. Note that this function only
998 * erases the elements, and that if the elements themselves are
999 * pointers, the pointed-to memory is not touched in any way.
1000 * Managing the pointer is the user's responsibilty.
1002 template<typename _BinaryPredicate
>
1004 unique(_BinaryPredicate
);
1007 * @brief Merge sorted lists.
1008 * @param x Sorted list to merge.
1010 * Assumes that both @a x and this list are sorted according to
1011 * operator<(). Merges elements of @a x into this list in
1012 * sorted order, leaving @a x empty when complete. Elements in
1013 * this list precede elements in @a x that are equal.
1019 * @brief Merge sorted lists according to comparison function.
1020 * @param x Sorted list to merge.
1021 * @param StrictWeakOrdering Comparison function definining
1024 * Assumes that both @a x and this list are sorted according to
1025 * StrictWeakOrdering. Merges elements of @a x into this list
1026 * in sorted order, leaving @a x empty when complete. Elements
1027 * in this list precede elements in @a x that are equivalent
1028 * according to StrictWeakOrdering().
1030 template<typename _StrictWeakOrdering
>
1032 merge(list
&, _StrictWeakOrdering
);
1035 * @brief Reverse the elements in list.
1037 * Reverse the order of elements in the list in linear time.
1040 reverse() { this->_M_node
.reverse(); }
1043 * @brief Sort the elements.
1045 * Sorts the elements of this list in NlogN time. Equivalent
1046 * elements remain in list order.
1052 * @brief Sort the elements according to comparison function.
1054 * Sorts the elements of this list in NlogN time. Equivalent
1055 * elements remain in list order.
1057 template<typename _StrictWeakOrdering
>
1059 sort(_StrictWeakOrdering
);
1062 // Internal assign functions follow.
1064 // Called by the range assign to implement [23.1.1]/9
1065 template<typename _Integer
>
1067 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1069 _M_fill_assign(static_cast<size_type
>(__n
),
1070 static_cast<value_type
>(__val
));
1073 // Called by the range assign to implement [23.1.1]/9
1074 template<typename _InputIterator
>
1076 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1079 // Called by assign(n,t), and the range assign when it turns out
1080 // to be the same thing.
1082 _M_fill_assign(size_type __n
, const value_type
& __val
);
1085 // Internal insert functions follow.
1087 // Called by the range insert to implement [23.1.1]/9
1088 template<typename _Integer
>
1090 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
1093 _M_fill_insert(__pos
, static_cast<size_type
>(__n
),
1094 static_cast<value_type
>(__x
));
1097 // Called by the range insert to implement [23.1.1]/9
1098 template<typename _InputIterator
>
1100 _M_insert_dispatch(iterator __pos
,
1101 _InputIterator __first
, _InputIterator __last
,
1104 for ( ; __first
!= __last
; ++__first
)
1105 _M_insert(__pos
, *__first
);
1108 // Called by insert(p,n,x), and the range insert when it turns out
1109 // to be the same thing.
1111 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
)
1113 for ( ; __n
> 0; --__n
)
1114 _M_insert(__pos
, __x
);
1118 // Moves the elements from [first,last) before position.
1120 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1122 __position
._M_node
->transfer(__first
._M_node
,__last
._M_node
);
1125 // Inserts new element at position given and with value given.
1127 _M_insert(iterator __position
, const value_type
& __x
)
1129 _Node
* __tmp
= _M_create_node(__x
);
1131 __tmp
->hook(__position
._M_node
);
1134 // Erases element at position given.
1136 _M_erase(iterator __position
)
1138 __position
._M_node
->unhook();
1139 _Node
* __n
= static_cast<_Node
*>(__position
._M_node
);
1140 std::_Destroy(&__n
->_M_data
);
1147 * @brief List equality comparison.
1149 * @param y A %list of the same type as @a x.
1150 * @return True iff the size and elements of the lists are equal.
1152 * This is an equivalence relation. It is linear in the size of
1153 * the lists. Lists are considered equivalent if their sizes are
1154 * equal, and if corresponding elements compare equal.
1156 template<typename _Tp
, typename _Alloc
>
1158 operator==(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1160 typedef typename list
<_Tp
,_Alloc
>::const_iterator const_iterator
;
1161 const_iterator __end1
= __x
.end();
1162 const_iterator __end2
= __y
.end();
1164 const_iterator __i1
= __x
.begin();
1165 const_iterator __i2
= __y
.begin();
1166 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
)
1171 return __i1
== __end1
&& __i2
== __end2
;
1175 * @brief List ordering relation.
1177 * @param y A %list of the same type as @a x.
1178 * @return True iff @a x is lexicographically less than @a y.
1180 * This is a total ordering relation. It is linear in the size of the
1181 * lists. The elements must be comparable with @c <.
1183 * See std::lexicographical_compare() for how the determination is made.
1185 template<typename _Tp
, typename _Alloc
>
1187 operator<(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1189 return std::lexicographical_compare(__x
.begin(), __x
.end(),
1190 __y
.begin(), __y
.end());
1193 /// Based on operator==
1194 template<typename _Tp
, typename _Alloc
>
1196 operator!=(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1197 { return !(__x
== __y
); }
1199 /// Based on operator<
1200 template<typename _Tp
, typename _Alloc
>
1202 operator>(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1203 { return __y
< __x
; }
1205 /// Based on operator<
1206 template<typename _Tp
, typename _Alloc
>
1208 operator<=(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1209 { return !(__y
< __x
); }
1211 /// Based on operator<
1212 template<typename _Tp
, typename _Alloc
>
1214 operator>=(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1215 { return !(__x
< __y
); }
1217 /// See std::list::swap().
1218 template<typename _Tp
, typename _Alloc
>
1220 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1222 } // namespace __gnu_norm
1224 #endif /* _LIST_H */