1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 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
80 /// @if maint An actual node in the %list. @endif
81 template<typename _Tp>
82 struct _List_node : public _List_node_base
84 _Tp _M_data; ///< User's data.
90 * @brief Common part of a list::iterator.
92 * A simple type to walk a doubly-linked list. All operations here should
93 * be self-explanatory after taking any decent introductory data structures
97 struct _List_iterator_base
99 typedef size_t size_type;
100 typedef ptrdiff_t difference_type;
101 typedef bidirectional_iterator_tag iterator_category;
103 /// The only member points to the %list element.
104 _List_node_base* _M_node;
106 _List_iterator_base(_List_node_base* __x)
110 _List_iterator_base()
113 /// Walk the %list forward.
116 { _M_node = _M_node->_M_next; }
118 /// Walk the %list backward.
121 { _M_node = _M_node->_M_prev; }
124 operator==(const _List_iterator_base& __x) const
125 { return _M_node == __x._M_node; }
128 operator!=(const _List_iterator_base& __x) const
129 { return _M_node != __x._M_node; }
133 * @brief A list::iterator.
135 * In addition to being used externally, a list holds one of these
136 * internally, pointing to the sequence of data.
139 * All the functions are op overloads.
142 template<typename _Tp, typename _Ref, typename _Ptr>
143 struct _List_iterator : public _List_iterator_base
145 typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
146 typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
147 typedef _List_iterator<_Tp,_Ref,_Ptr> _Self;
149 typedef _Tp value_type;
150 typedef _Ptr pointer;
151 typedef _Ref reference;
152 typedef _List_node<_Tp> _Node;
154 _List_iterator(_Node* __x)
155 : _List_iterator_base(__x)
161 _List_iterator(const iterator& __x)
162 : _List_iterator_base(__x._M_node)
167 { return static_cast<_Node*>(_M_node)->_M_data; }
168 // Must downcast from List_node_base to _List_node to get to _M_data.
172 { return &(operator*()); }
206 /// @if maint Primary default version. @endif
209 * See bits/stl_deque.h's _Deque_alloc_base for an explanation.
212 template<typename _Tp, typename _Allocator, bool _IsStatic>
213 class _List_alloc_base
216 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
220 get_allocator() const { return _M_node_allocator; }
222 _List_alloc_base(const allocator_type& __a)
223 : _M_node_allocator(__a)
229 { return _M_node_allocator.allocate(1); }
232 _M_put_node(_List_node<_Tp>* __p)
233 { _M_node_allocator.deallocate(__p, 1); }
236 // The stored instance is not actually of "allocator_type"'s type. Instead
237 // we rebind the type to Allocator<List_node<Tp>>, which according to
238 // [20.1.5]/4 should probably be the same. List_node<Tp> is not the same
239 // size as Tp (it's two pointers larger), and specializations on Tp may go
240 // unused because List_node<Tp> is being bound instead.
242 // We put this to the test in get_allocator above; if the two types are
243 // actually different, there had better be a conversion between them.
245 // None of the predefined allocators shipped with the library (as of 3.1)
246 // use this instantiation anyhow; they're all instanceless.
247 typename _Alloc_traits<_List_node<_Tp>, _Allocator>::allocator_type
250 _List_node_base _M_node;
253 /// @if maint Specialization for instanceless allocators. @endif
254 template<typename _Tp, typename _Allocator>
255 class _List_alloc_base<_Tp, _Allocator, true>
258 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
262 get_allocator() const { return allocator_type(); }
264 _List_alloc_base(const allocator_type&)
268 // See comment in primary template class about why this is safe for the
269 // standard predefined classes.
270 typedef typename _Alloc_traits<_List_node<_Tp>, _Allocator>::_Alloc_type
275 { return _Alloc_type::allocate(1); }
278 _M_put_node(_List_node<_Tp>* __p)
279 { _Alloc_type::deallocate(__p, 1); }
281 _List_node_base _M_node;
287 * See bits/stl_deque.h's _Deque_base for an explanation.
290 template <typename _Tp, typename _Alloc>
292 : public _List_alloc_base<_Tp, _Alloc,
293 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
296 typedef _List_alloc_base<_Tp, _Alloc,
297 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
299 typedef typename _Base::allocator_type allocator_type;
301 _List_base(const allocator_type& __a)
304 this->_M_node._M_next = &this->_M_node;
305 this->_M_node._M_prev = &this->_M_node;
308 // This is what actually destroys the list.
320 * @brief A standard container with linear time access to elements, and
321 * fixed time insertion/deletion at any point in the sequence.
323 * @ingroup Containers
326 * Meets the requirements of a <a href="tables.html#65">container</a>, a
327 * <a href="tables.html#66">reversible container</a>, and a
328 * <a href="tables.html#67">sequence</a>, including the
329 * <a href="tables.html#68">optional sequence requirements</a> with the
330 * %exception of @c at and @c operator[].
332 * This is a @e doubly @e linked %list. Traversal up and down the %list
333 * requires linear time, but adding and removing elements (or @e nodes) is
334 * done in constant time, regardless of where the change takes place.
335 * Unlike std::vector and std::deque, random-access iterators are not
336 * provided, so subscripting ( @c [] ) access is not allowed. For algorithms
337 * which only need sequential access, this lack makes no difference.
339 * Also unlike the other standard containers, std::list provides specialized
340 * algorithms %unique to linked lists, such as splicing, sorting, and
344 * A couple points on memory allocation for list<Tp>:
346 * First, we never actually allocate a Tp, we allocate List_node<Tp>'s
347 * and trust [20.1.5]/4 to DTRT. This is to ensure that after elements from
348 * %list<X,Alloc1> are spliced into %list<X,Alloc2>, destroying the memory of
349 * the second %list is a valid operation, i.e., Alloc1 giveth and Alloc2
352 * Second, a %list conceptually represented as
354 * A <---> B <---> C <---> D
356 * is actually circular; a link exists between A and D. The %list class
357 * holds (as its only data member) a private list::iterator pointing to
358 * @e D, not to @e A! To get to the head of the %list, we start at the tail
359 * and move forward by one. When this member iterator's next/previous
360 * pointers refer to itself, the %list is %empty.
363 template<typename _Tp, typename _Alloc = allocator<_Tp> >
364 class list : protected _List_base<_Tp, _Alloc>
366 // concept requirements
367 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
369 typedef _List_base<_Tp, _Alloc> _Base;
372 typedef _Tp value_type;
373 typedef value_type* pointer;
374 typedef const value_type* const_pointer;
375 typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
376 typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
377 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
378 typedef std::reverse_iterator<iterator> reverse_iterator;
379 typedef value_type& reference;
380 typedef const value_type& const_reference;
381 typedef size_t size_type;
382 typedef ptrdiff_t difference_type;
383 typedef typename _Base::allocator_type allocator_type;
386 // Note that pointers-to-_Node's can be ctor-converted to iterator types.
387 typedef _List_node<_Tp> _Node;
390 * One data member plus two memory-handling functions. If the _Alloc
391 * type requires separate instances, then one of those will also be
392 * included, accumulated from the topmost parent.
395 using _Base::_M_node;
396 using _Base::_M_put_node;
397 using _Base::_M_get_node;
401 * @param x An instance of user data.
403 * Allocates space for a new node and constructs a copy of @a x in it.
407 _M_create_node(const value_type& __x)
409 _Node* __p = this->_M_get_node();
411 std::_Construct(&__p->_M_data, __x);
416 __throw_exception_again;
423 * Allocates space for a new node and default-constructs a new instance
424 * of @c value_type in it.
430 _Node* __p = this->_M_get_node();
432 std::_Construct(&__p->_M_data);
437 __throw_exception_again;
443 // [23.2.2.1] construct/copy/destroy
444 // (assign() and get_allocator() are also listed in this section)
446 * @brief Default constructor creates no elements.
449 list(const allocator_type& __a = allocator_type())
453 * @brief Create a %list with copies of an exemplar element.
454 * @param n The number of elements to initially create.
455 * @param value An element to copy.
457 * This constructor fills the %list with @a n copies of @a value.
459 list(size_type __n, const value_type& __value,
460 const allocator_type& __a = allocator_type())
462 { this->insert(begin(), __n, __value); }
465 * @brief Create a %list with default elements.
466 * @param n The number of elements to initially create.
468 * This constructor fills the %list with @a n copies of a
469 * default-constructed element.
473 : _Base(allocator_type())
474 { this->insert(begin(), __n, value_type()); }
477 * @brief %List copy constructor.
478 * @param x A %list of identical element and allocator types.
480 * The newly-created %list uses a copy of the allocation object used
483 list(const list& __x)
484 : _Base(__x.get_allocator())
485 { this->insert(begin(), __x.begin(), __x.end()); }
488 * @brief Builds a %list from a range.
489 * @param first An input iterator.
490 * @param last An input iterator.
492 * Create a %list consisting of copies of the elements from
493 * [@a first,@a last). This is linear in N (where N is
494 * distance(@a first,@a last)).
497 * We don't need any dispatching tricks here, because insert does all of
501 template<typename _InputIterator>
502 list(_InputIterator __first, _InputIterator __last,
503 const allocator_type& __a = allocator_type())
505 { this->insert(begin(), __first, __last); }
508 * No explicit dtor needed as the _Base dtor takes care of things.
509 * The _Base dtor only erases the elements, and note that if the elements
510 * themselves are pointers, the pointed-to memory is not touched in any
511 * way. Managing the pointer is the user's responsibilty.
515 * @brief %List assignment operator.
516 * @param x A %list of identical element and allocator types.
518 * All the elements of @a x are copied, but unlike the copy constructor,
519 * the allocator object is not copied.
522 operator=(const list& __x);
525 * @brief Assigns a given value to a %list.
526 * @param n Number of elements to be assigned.
527 * @param val Value to be assigned.
529 * This function fills a %list with @a n copies of the given value.
530 * Note that the assignment completely changes the %list and that the
531 * resulting %list's size is the same as the number of elements assigned.
532 * Old data may be lost.
535 assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); }
538 * @brief Assigns a range to a %list.
539 * @param first An input iterator.
540 * @param last An input iterator.
542 * This function fills a %list with copies of the elements in the
543 * range [@a first,@a last).
545 * Note that the assignment completely changes the %list and that the
546 * resulting %list's size is the same as the number of elements assigned.
547 * Old data may be lost.
549 template<typename _InputIterator>
551 assign(_InputIterator __first, _InputIterator __last)
553 // Check whether it's an integral type. If so, it's not an iterator.
554 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
555 _M_assign_dispatch(__first, __last, _Integral());
558 /// Get a copy of the memory allocation object.
560 get_allocator() const { return _Base::get_allocator(); }
564 * Returns a read/write iterator that points to the first element in the
565 * %list. Iteration is done in ordinary element order.
568 begin() { return static_cast<_Node*>(this->_M_node._M_next); }
571 * Returns a read-only (constant) iterator that points to the first element
572 * in the %list. Iteration is done in ordinary element order.
575 begin() const { return static_cast<_Node*>(this->_M_node._M_next); }
578 * Returns a read/write iterator that points one past the last element in
579 * the %list. Iteration is done in ordinary element order.
582 end() { return static_cast<_Node*>(&this->_M_node); }
585 * Returns a read-only (constant) iterator that points one past the last
586 * element in the %list. Iteration is done in ordinary element order.
589 end() const { return const_cast<_Node *>(static_cast<const _Node*>(&this->_M_node)); }
592 * Returns a read/write reverse iterator that points to the last element in
593 * the %list. Iteration is done in reverse element order.
596 rbegin() { return reverse_iterator(end()); }
599 * Returns a read-only (constant) reverse iterator that points to the last
600 * element in the %list. Iteration is done in reverse element order.
602 const_reverse_iterator
603 rbegin() const { return const_reverse_iterator(end()); }
606 * Returns a read/write reverse iterator that points to one before the
607 * first element in the %list. Iteration is done in reverse element
611 rend() { return reverse_iterator(begin()); }
614 * Returns a read-only (constant) reverse iterator that points to one
615 * before the first element in the %list. Iteration is done in reverse
618 const_reverse_iterator
620 { return const_reverse_iterator(begin()); }
622 // [23.2.2.2] capacity
624 * Returns true if the %list is empty. (Thus begin() would equal end().)
627 empty() const { return this->_M_node._M_next == &this->_M_node; }
629 /** Returns the number of elements in the %list. */
631 size() const { return std::distance(begin(), end()); }
633 /** Returns the size() of the largest possible %list. */
635 max_size() const { return size_type(-1); }
638 * @brief Resizes the %list to the specified number of elements.
639 * @param new_size Number of elements the %list should contain.
640 * @param x Data with which new elements should be populated.
642 * This function will %resize the %list to the specified number of
643 * elements. If the number is smaller than the %list's current size the
644 * %list is truncated, otherwise the %list is extended and new elements
645 * are populated with given data.
648 resize(size_type __new_size, const value_type& __x);
651 * @brief Resizes the %list to the specified number of elements.
652 * @param new_size Number of elements the %list should contain.
654 * This function will resize the %list to the specified number of
655 * elements. If the number is smaller than the %list's current size the
656 * %list is truncated, otherwise the %list is extended and new elements
657 * are default-constructed.
660 resize(size_type __new_size) { this->resize(__new_size, value_type()); }
664 * Returns a read/write reference to the data at the first element of the
668 front() { return *begin(); }
671 * Returns a read-only (constant) reference to the data at the first
672 * element of the %list.
675 front() const { return *begin(); }
678 * Returns a read/write reference to the data at the last element of the
682 back() { return *(--end()); }
685 * Returns a read-only (constant) reference to the data at the last
686 * element of the %list.
689 back() const { return *(--end()); }
691 // [23.2.2.3] modifiers
693 * @brief Add data to the front of the %list.
694 * @param x Data to be added.
696 * This is a typical stack operation. The function creates an element at
697 * the front of the %list and assigns the given data to it. Due to the
698 * nature of a %list this operation can be done in constant time, and
699 * does not invalidate iterators and references.
702 push_front(const value_type& __x) { this->insert(begin(), __x); }
705 * @brief Removes first element.
707 * This is a typical stack operation. It shrinks the %list by one.
708 * Due to the nature of a %list this operation can be done in constant
709 * time, and only invalidates iterators/references to the element being
712 * Note that no data is returned, and if the first element's data is
713 * needed, it should be retrieved before pop_front() is called.
716 pop_front() { this->erase(begin()); }
719 * @brief Add data to the end of the %list.
720 * @param x Data to be added.
722 * This is a typical stack operation. The function creates an element at
723 * the end of the %list and assigns the given data to it. Due to the
724 * nature of a %list this operation can be done in constant time, and
725 * does not invalidate iterators and references.
728 push_back(const value_type& __x) { this->insert(end(), __x); }
731 * @brief Removes last element.
733 * This is a typical stack operation. It shrinks the %list by one.
734 * Due to the nature of a %list this operation can be done in constant
735 * time, and only invalidates iterators/references to the element being
738 * Note that no data is returned, and if the last element's data is
739 * needed, it should be retrieved before pop_back() is called.
744 iterator __tmp = end();
745 this->erase(--__tmp);
749 * @brief Inserts given value into %list before specified iterator.
750 * @param position An iterator into the %list.
751 * @param x Data to be inserted.
752 * @return An iterator that points to the inserted data.
754 * This function will insert a copy of the given value before the specified
756 * Due to the nature of a %list this operation can be done in constant
757 * time, and does not invalidate iterators and references.
760 insert(iterator __position, const value_type& __x);
763 * @brief Inserts a number of copies of given data into the %list.
764 * @param position An iterator into the %list.
765 * @param n Number of elements to be inserted.
766 * @param x Data to be inserted.
768 * This function will insert a specified number of copies of the given data
769 * before the location specified by @a position.
771 * Due to the nature of a %list this operation can be done in constant
772 * time, and does not invalidate iterators and references.
775 insert(iterator __position, size_type __n, const value_type& __x)
776 { _M_fill_insert(__position, __n, __x); }
779 * @brief Inserts a range into the %list.
780 * @param position An iterator into the %list.
781 * @param first An input iterator.
782 * @param last An input iterator.
784 * This function will insert copies of the data in the range
785 * [@a first,@a last) into the %list before the location specified by @a
788 * Due to the nature of a %list this operation can be done in constant
789 * time, and does not invalidate iterators and references.
791 template<typename _InputIterator>
793 insert(iterator __position, _InputIterator __first, _InputIterator __last)
795 // Check whether it's an integral type. If so, it's not an iterator.
796 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
797 _M_insert_dispatch(__position, __first, __last, _Integral());
801 * @brief Remove element at given position.
802 * @param position Iterator pointing to element to be erased.
803 * @return An iterator pointing to the next element (or end()).
805 * This function will erase the element at the given position and thus
806 * shorten the %list by one.
808 * Due to the nature of a %list this operation can be done in constant
809 * time, and only invalidates iterators/references to the element being
811 * The user is also cautioned that
812 * this function only erases the element, and that if the element is itself
813 * a pointer, the pointed-to memory is not touched in any way. Managing
814 * the pointer is the user's responsibilty.
817 erase(iterator __position);
820 * @brief Remove a range of elements.
821 * @param first Iterator pointing to the first element to be erased.
822 * @param last Iterator pointing to one past the last element to be
824 * @return An iterator pointing to the element pointed to by @a last
825 * prior to erasing (or end()).
827 * This function will erase the elements in the range @a [first,last) and
828 * shorten the %list accordingly.
830 * Due to the nature of a %list this operation can be done in constant
831 * time, and only invalidates iterators/references to the element being
833 * The user is also cautioned that
834 * this function only erases the elements, and that if the elements
835 * themselves are pointers, the pointed-to memory is not touched in any
836 * way. Managing the pointer is the user's responsibilty.
839 erase(iterator __first, iterator __last)
841 while (__first != __last)
847 * @brief Swaps data with another %list.
848 * @param x A %list of the same element and allocator types.
850 * This exchanges the elements between two lists in constant time.
851 * Note that the global std::swap() function is specialized such that
852 * std::swap(l1,l2) will feed to this function.
858 * Erases all the elements. Note that this function only erases the
859 * elements, and that if the elements themselves are pointers, the
860 * pointed-to memory is not touched in any way. Managing the pointer is
861 * the user's responsibilty.
864 clear() { _Base::__clear(); }
866 // [23.2.2.4] list operations
868 * @brief Insert contents of another %list.
869 * @param position Iterator referencing the element to insert before.
870 * @param x Source list.
872 * The elements of @a x are inserted in constant time in front of the
873 * element referenced by @a position. @a x becomes an empty list.
876 splice(iterator __position, list& __x)
879 this->_M_transfer(__position, __x.begin(), __x.end());
883 * @brief Insert element from another %list.
884 * @param position Iterator referencing the element to insert before.
885 * @param x Source list.
886 * @param i Iterator referencing the element to move.
888 * Removes the element in list @a x referenced by @a i and inserts it into the
889 * current list before @a position.
892 splice(iterator __position, list&, iterator __i)
896 if (__position == __i || __position == __j) return;
897 this->_M_transfer(__position, __i, __j);
901 * @brief Insert range from another %list.
902 * @param position Iterator referencing the element to insert before.
903 * @param x Source list.
904 * @param first Iterator referencing the start of range in x.
905 * @param last Iterator referencing the end of range in x.
907 * Removes elements in the range [first,last) and inserts them before
908 * @a position in constant time.
910 * Undefined if @a position is in [first,last).
913 splice(iterator __position, list&, iterator __first, iterator __last)
915 if (__first != __last)
916 this->_M_transfer(__position, __first, __last);
920 * @brief Remove all elements equal to value.
921 * @param value The value to remove.
923 * Removes every element in the list equal to @a value. Remaining
924 * elements stay in list order. Note that this function only erases the
925 * elements, and that if the elements themselves are pointers, the
926 * pointed-to memory is not touched in any way. Managing the pointer is
927 * the user's responsibilty.
930 remove(const _Tp& __value);
933 * @brief Remove all elements satisfying a predicate.
934 * @param Predicate Unary predicate function or object.
936 * Removes every element in the list for which the predicate returns
937 * true. Remaining elements stay in list order. Note that this function
938 * only erases the elements, and that if the elements themselves are
939 * pointers, the pointed-to memory is not touched in any way. Managing
940 * the pointer is the user's responsibilty.
942 template<typename _Predicate>
944 remove_if(_Predicate);
947 * @brief Remove consecutive duplicate elements.
949 * For each consecutive set of elements with the same value, remove all
950 * but the first one. Remaining elements stay in list order. Note that
951 * this function only erases the elements, and that if the elements
952 * themselves are pointers, the pointed-to memory is not touched in any
953 * way. Managing the pointer is the user's responsibilty.
959 * @brief Remove consecutive elements satisfying a predicate.
960 * @param BinaryPredicate Binary predicate function or object.
962 * For each consecutive set of elements [first,last) that satisfy
963 * predicate(first,i) where i is an iterator in [first,last), remove all
964 * but the first one. Remaining elements stay in list order. Note that
965 * this function only erases the elements, and that if the elements
966 * themselves are pointers, the pointed-to memory is not touched in any
967 * way. Managing the pointer is the user's responsibilty.
969 template<typename _BinaryPredicate>
971 unique(_BinaryPredicate);
974 * @brief Merge sorted lists.
975 * @param x Sorted list to merge.
977 * Assumes that both @a x and this list are sorted according to
978 * operator<(). Merges elements of @a x into this list in sorted order,
979 * leaving @a x empty when complete. Elements in this list precede
980 * elements in @a x that are equal.
986 * @brief Merge sorted lists according to comparison function.
987 * @param x Sorted list to merge.
988 * @param StrictWeakOrdering Comparison function definining sort order.
990 * Assumes that both @a x and this list are sorted according to
991 * StrictWeakOrdering. Merges elements of @a x into this list in sorted
992 * order, leaving @a x empty when complete. Elements in this list precede
993 * elements in @a x that are equivalent according to StrictWeakOrdering().
995 template<typename _StrictWeakOrdering>
997 merge(list&, _StrictWeakOrdering);
1000 * @brief Reverse the elements in list.
1002 * Reverse the order of elements in the list in linear time.
1005 reverse() { __List_base_reverse(&this->_M_node); }
1008 * @brief Sort the elements.
1010 * Sorts the elements of this list in NlogN time. Equivalent elements
1011 * remain in list order.
1017 * @brief Sort the elements according to comparison function.
1019 * Sorts the elements of this list in NlogN time. Equivalent elements
1020 * remain in list order.
1022 template<typename _StrictWeakOrdering>
1024 sort(_StrictWeakOrdering);
1027 // Internal assign functions follow.
1029 // called by the range assign to implement [23.1.1]/9
1030 template<typename _Integer>
1032 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1034 _M_fill_assign(static_cast<size_type>(__n),
1035 static_cast<value_type>(__val));
1038 // called by the range assign to implement [23.1.1]/9
1039 template<typename _InputIterator>
1041 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, __false_type);
1043 // Called by assign(n,t), and the range assign when it turns out to be the
1046 _M_fill_assign(size_type __n, const value_type& __val);
1049 // Internal insert functions follow.
1051 // called by the range insert to implement [23.1.1]/9
1052 template<typename _Integer>
1054 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1057 _M_fill_insert(__pos, static_cast<size_type>(__n),
1058 static_cast<value_type>(__x));
1061 // called by the range insert to implement [23.1.1]/9
1062 template<typename _InputIterator>
1064 _M_insert_dispatch(iterator __pos,
1065 _InputIterator __first, _InputIterator __last,
1068 for ( ; __first != __last; ++__first)
1069 insert(__pos, *__first);
1072 // Called by insert(p,n,x), and the range insert when it turns out to be
1075 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
1077 for ( ; __n > 0; --__n)
1082 // Moves the elements from [first,last) before position.
1084 _M_transfer(iterator __position, iterator __first, iterator __last)
1086 if (__position != __last) {
1087 // Remove [first, last) from its old position.
1088 __last._M_node->_M_prev->_M_next = __position._M_node;
1089 __first._M_node->_M_prev->_M_next = __last._M_node;
1090 __position._M_node->_M_prev->_M_next = __first._M_node;
1092 // Splice [first, last) into its new position.
1093 _List_node_base* __tmp = __position._M_node->_M_prev;
1094 __position._M_node->_M_prev = __last._M_node->_M_prev;
1095 __last._M_node->_M_prev = __first._M_node->_M_prev;
1096 __first._M_node->_M_prev = __tmp;
1103 * @brief List equality comparison.
1105 * @param y A %list of the same type as @a x.
1106 * @return True iff the size and elements of the lists are equal.
1108 * This is an equivalence relation. It is linear in the size of the
1109 * lists. Lists are considered equivalent if their sizes are equal,
1110 * and if corresponding elements compare equal.
1112 template<typename _Tp, typename _Alloc>
1114 operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1116 typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;
1117 const_iterator __end1 = __x.end();
1118 const_iterator __end2 = __y.end();
1120 const_iterator __i1 = __x.begin();
1121 const_iterator __i2 = __y.begin();
1122 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
1126 return __i1 == __end1 && __i2 == __end2;
1130 * @brief List ordering relation.
1132 * @param y A %list of the same type as @a x.
1133 * @return True iff @a x is lexicographically less than @a y.
1135 * This is a total ordering relation. It is linear in the size of the
1136 * lists. The elements must be comparable with @c <.
1138 * See std::lexicographical_compare() for how the determination is made.
1140 template<typename _Tp, typename _Alloc>
1142 operator<(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1144 return std::lexicographical_compare(__x.begin(), __x.end(),
1145 __y.begin(), __y.end());
1148 /// Based on operator==
1149 template<typename _Tp, typename _Alloc>
1151 operator!=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1152 { return !(__x == __y); }
1154 /// Based on operator<
1155 template<typename _Tp, typename _Alloc>
1157 operator>(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1158 { return __y < __x; }
1160 /// Based on operator<
1161 template<typename _Tp, typename _Alloc>
1163 operator<=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1164 { return !(__y < __x); }
1166 /// Based on operator<
1167 template<typename _Tp, typename _Alloc>
1169 operator>=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1170 { return !(__x < __y); }
1172 /// See std::list::swap().
1173 template<typename _Tp, typename _Alloc>
1175 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1179 #endif /* _LIST_H */