1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002 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.
61 #ifndef __GLIBCPP_INTERNAL_LIST_H
62 #define __GLIBCPP_INTERNAL_LIST_H
64 #include <bits/concept_check.h>
66 // Since this entire file is within namespace std, there's no reason to
67 // waste two spaces along the left column. Thus the leading indentation is
68 // slightly violated from here on.
72 // Supporting structures are split into common and templated types; the
73 // latter publicly inherits from the former in an effort to reduce code
74 // duplication. This results in some "needless" static_cast'ing later on,
75 // but it's all safe downcasting.
77 /// @if maint Common part of a node in the %list. @endif
78 struct _List_node_base
80 _List_node_base* _M_next; ///< Self-explanatory
81 _List_node_base* _M_prev; ///< Self-explanatory
84 /// @if maint An actual node in the %list. @endif
85 template<typename _Tp>
86 struct _List_node : public _List_node_base
88 _Tp _M_data; ///< User's data.
94 * @brief Common part of a list::iterator.
96 * A simple type to walk a doubly-linked list. All operations here should
97 * be self-explanatory after taking any decent introductory data structures
101 struct _List_iterator_base
103 typedef size_t size_type;
104 typedef ptrdiff_t difference_type;
105 typedef bidirectional_iterator_tag iterator_category;
107 /// The only member points to the %list element.
108 _List_node_base* _M_node;
110 _List_iterator_base(_List_node_base* __x)
114 _List_iterator_base()
117 /// Walk the %list forward.
120 { _M_node = _M_node->_M_next; }
122 /// Walk the %list backward.
125 { _M_node = _M_node->_M_prev; }
128 operator==(const _List_iterator_base& __x) const
129 { return _M_node == __x._M_node; }
132 operator!=(const _List_iterator_base& __x) const
133 { return _M_node != __x._M_node; }
137 * @brief A list::iterator.
139 * In addition to being used externally, a list holds one of these internally,
140 * pointing to the sequence of data.
143 * All the functions are op overloads.
146 template<typename _Tp, typename _Ref, typename _Ptr>
147 struct _List_iterator : public _List_iterator_base
149 typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
150 typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
151 typedef _List_iterator<_Tp,_Ref,_Ptr> _Self;
153 typedef _Tp value_type;
154 typedef _Ptr pointer;
155 typedef _Ref reference;
156 typedef _List_node<_Tp> _Node;
158 _List_iterator(_Node* __x)
159 : _List_iterator_base(__x)
165 _List_iterator(const iterator& __x)
166 : _List_iterator_base(__x._M_node)
171 { return static_cast<_Node*>(_M_node)->_M_data; }
172 // Must downcast from List_node_base to _List_node to get to _M_data.
176 { return &(operator*()); }
210 /// @if maint Primary default version. @endif
213 * See bits/stl_deque.h's _Deque_alloc_base for an explanation.
216 template<typename _Tp, typename _Allocator, bool _IsStatic>
217 class _List_alloc_base
220 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
224 get_allocator() const { return _M_node_allocator; }
226 _List_alloc_base(const allocator_type& __a)
227 : _M_node_allocator(__a)
233 { return _M_node_allocator.allocate(1); }
236 _M_put_node(_List_node<_Tp>* __p)
237 { _M_node_allocator.deallocate(__p, 1); }
240 // The stored instance is not actually of "allocator_type"'s type. Instead
241 // we rebind the type to Allocator<List_node<Tp>>, which according to
242 // [20.1.5]/4 should probably be the same. List_node<Tp> is not the same
243 // size as Tp (it's two pointers larger), and specializations on Tp may go
244 // unused because List_node<Tp> is being bound instead.
246 // We put this to the test in get_allocator above; if the two types are
247 // actually different, there had better be a conversion between them.
249 // None of the predefined allocators shipped with the library (as of 3.1)
250 // use this instantiation anyhow; they're all instanceless.
251 typename _Alloc_traits<_List_node<_Tp>, _Allocator>::allocator_type
254 _List_node<_Tp>* _M_node;
257 /// @if maint Specialization for instanceless allocators. @endif
258 template<typename _Tp, typename _Allocator>
259 class _List_alloc_base<_Tp, _Allocator, true>
262 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
266 get_allocator() const { return allocator_type(); }
268 _List_alloc_base(const allocator_type&)
272 // See comment in primary template class about why this is safe for the
273 // standard predefined classes.
274 typedef typename _Alloc_traits<_List_node<_Tp>, _Allocator>::_Alloc_type
279 { return _Alloc_type::allocate(1); }
282 _M_put_node(_List_node<_Tp>* __p)
283 { _Alloc_type::deallocate(__p, 1); }
285 _List_node<_Tp>* _M_node;
291 * See bits/stl_deque.h's _Deque_base for an explanation.
294 template <typename _Tp, typename _Alloc>
296 : public _List_alloc_base<_Tp, _Alloc,
297 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
300 typedef _List_alloc_base<_Tp, _Alloc,
301 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
303 typedef typename _Base::allocator_type allocator_type;
305 _List_base(const allocator_type& __a)
308 _M_node = _M_get_node();
309 _M_node->_M_next = _M_node;
310 _M_node->_M_prev = _M_node;
313 // This is what actually destroys the list.
317 _M_put_node(_M_node);
326 * @brief A standard container with linear time access to elements, and
327 * fixed time insertion/deletion at any point in the sequence.
329 * @ingroup Containers
332 * Meets the requirements of a <a href="tables.html#65">container</a>, a
333 * <a href="tables.html#66">reversible container</a>, and a
334 * <a href="tables.html#67">sequence</a>, including the
335 * <a href="tables.html#68">optional sequence requirements</a> with the
336 * %exception of @c at and @c operator[].
338 * This is a @e doubly @e linked %list. Traversal up and down the %list
339 * requires linear time, but adding and removing elements (or @e nodes) is
340 * done in constant time, regardless of where the change takes place.
341 * Unlike std::vector and std::deque, random-access iterators are not
342 * provided, so subscripting ( @c [] ) access is not allowed. For algorithms
343 * which only need sequential access, this lack makes no difference.
345 * Also unlike the other standard containers, std::list provides specialized
346 * algorithms %unique to linked lists, such as splicing, sorting, and
350 * A couple points on memory allocation for list<Tp>:
352 * First, we never actually allocate a Tp, we actally allocate List_node<Tp>'s
353 * and trust [20.1.5]/4 to DTRT. This is to ensure that after elements from
354 * %list<X,Alloc1> are spliced into %list<X,Alloc2>, destroying the memory of
355 * the second %list is a valid operation, i.e., Alloc1 giveth and Alloc2
358 * Second, a %list conceptually represented as
360 * A <---> B <---> C <---> D
362 * is actually circular; a link exists between A and D. The %list class
363 * holds (as its only data member) a private list::iterator pointing to
364 * @e D, not to @e A! To get to the head of the %list, we start at the tail
365 * and move forward by one. When this member iterator's next/previous
366 * pointers refer to itself, the %list is %empty.
369 template<typename _Tp, typename _Alloc = allocator<_Tp> >
370 class list : protected _List_base<_Tp, _Alloc>
372 // concept requirements
373 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
375 typedef _List_base<_Tp, _Alloc> _Base;
378 typedef _Tp value_type;
379 typedef value_type* pointer;
380 typedef const value_type* const_pointer;
381 typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
382 typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
383 typedef reverse_iterator<const_iterator> const_reverse_iterator;
384 typedef reverse_iterator<iterator> reverse_iterator;
385 typedef value_type& reference;
386 typedef const value_type& const_reference;
387 typedef size_t size_type;
388 typedef ptrdiff_t difference_type;
389 typedef typename _Base::allocator_type allocator_type;
392 // Note that pointers-to-_Node's can be ctor-converted to iterator types.
393 typedef _List_node<_Tp> _Node;
396 * One data member plus two memory-handling functions. If the _Alloc
397 * type requires separate instances, then one of those will also be
398 * included, accumulated from the topmost parent.
401 using _Base::_M_node;
402 using _Base::_M_put_node;
403 using _Base::_M_get_node;
407 * @param x An instance of user data.
409 * Allocates space for a new node and constructs a copy of @a x in it.
413 _M_create_node(const value_type& __x)
415 _Node* __p = _M_get_node();
417 _Construct(&__p->_M_data, __x);
422 __throw_exception_again;
429 * Allocates space for a new node and default-constructs a new instance
430 * of @c value_type in it.
436 _Node* __p = _M_get_node();
438 _Construct(&__p->_M_data);
443 __throw_exception_again;
449 // [23.2.2.1] construct/copy/destroy
450 // (assign() and get_allocator() are also listed in this section)
452 * @brief Default constructor creates no elements.
455 list(const allocator_type& __a = allocator_type())
459 * @brief Create a %list with copies of an exemplar element.
460 * @param n The number of elements to initially create.
461 * @param value An element to copy.
463 * This constructor fills the %list with @a n copies of @a value.
465 list(size_type __n, const value_type& __value,
466 const allocator_type& __a = allocator_type())
468 { this->insert(begin(), __n, __value); }
471 * @brief Create a %list with default elements.
472 * @param n The number of elements to initially create.
474 * This constructor fills the %list with @a n copies of a
475 * default-constructed element.
479 : _Base(allocator_type())
480 { this->insert(begin(), __n, value_type()); }
483 * @brief %List copy constructor.
484 * @param x A %list of identical element and allocator types.
486 * The newly-created %list uses a copy of the allocation object used
489 list(const list& __x)
490 : _Base(__x.get_allocator())
491 { this->insert(begin(), __x.begin(), __x.end()); }
494 * @brief Builds a %list from a range.
495 * @param first An input iterator.
496 * @param last An input iterator.
498 * Creats a %list consisting of copies of the elements from [first,last).
499 * This is linear in N (where N is distance(first,last)).
502 * We don't need any dispatching tricks here, because insert does all of
506 template<typename _InputIterator>
507 list(_InputIterator __first, _InputIterator __last,
508 const allocator_type& __a = allocator_type())
510 { this->insert(begin(), __first, __last); }
513 * The dtor only erases the elements, and note that if the elements
514 * themselves are pointers, the pointed-to memory is not touched in any
515 * way. Managing the pointer is the user's responsibilty.
520 * @brief %List assignment operator.
521 * @param x A %list of identical element and allocator types.
523 * All the elements of @a x are copied, but unlike the copy constructor, the
524 * allocator object is not copied.
527 operator=(const list& __x);
530 * @brief Assigns a given value to a %list.
531 * @param n Number of elements to be assigned.
532 * @param val Value to be assigned.
534 * This function fills a %list with @a n copies of the given value.
535 * Note that the assignment completely changes the %list and that the
536 * resulting %list's size is the same as the number of elements assigned.
537 * Old data may be lost.
540 assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); }
543 * @brief Assigns a range to a %list.
544 * @param first An input iterator.
545 * @param last An input iterator.
547 * This function fills a %list with copies of the elements in the
548 * range [first,last).
550 * Note that the assignment completely changes the %list and that the
551 * resulting %list's size is the same as the number of elements assigned.
552 * Old data may be lost.
554 template<typename _InputIterator>
556 assign(_InputIterator __first, _InputIterator __last)
558 // Check whether it's an integral type. If so, it's not an iterator.
559 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
560 _M_assign_dispatch(__first, __last, _Integral());
563 /// Get a copy of the memory allocation object.
565 get_allocator() const { return _Base::get_allocator(); }
569 * Returns a read/write iterator that points to the first element in the
570 * %list. Iteration is done in ordinary element order.
573 begin() { return static_cast<_Node*>(_M_node->_M_next); }
576 * Returns a read-only (constant) iterator that points to the first element
577 * in the %list. Iteration is done in ordinary element order.
580 begin() const { return static_cast<_Node*>(_M_node->_M_next); }
583 * Returns a read/write iterator that points one past the last element in
584 * the %list. Iteration is done in ordinary element order.
587 end() { return _M_node; }
590 * Returns a read-only (constant) iterator that points one past the last
591 * element in the %list. Iteration is done in ordinary element order.
594 end() const { return _M_node; }
597 * Returns a read/write reverse iterator that points to the last element in
598 * the %list. Iteration is done in reverse element order.
601 rbegin() { return reverse_iterator(end()); }
604 * Returns a read-only (constant) reverse iterator that points to the last
605 * element in the %list. Iteration is done in reverse element order.
607 const_reverse_iterator
608 rbegin() const { return const_reverse_iterator(end()); }
611 * Returns a read/write reverse iterator that points to one before the
612 * first element in the %list. Iteration is done in reverse element
616 rend() { return reverse_iterator(begin()); }
619 * Returns a read-only (constant) reverse iterator that points to one
620 * before the first element in the %list. Iteration is done in reverse
623 const_reverse_iterator
625 { return const_reverse_iterator(begin()); }
627 // [23.2.2.2] capacity
629 * Returns true if the %list is empty. (Thus begin() would equal end().)
632 empty() const { return _M_node->_M_next == _M_node; }
634 /** Returns the number of elements in the %list. */
636 size() const { return distance(begin(), end()); }
638 /** Returns the size() of the largest possible %list. */
640 max_size() const { return size_type(-1); }
643 * @brief Resizes the %list to the specified number of elements.
644 * @param new_size Number of elements the %list should contain.
645 * @param x Data with which new elements should be populated.
647 * This function will %resize the %list to the specified number of
648 * elements. If the number is smaller than the %list's current size the
649 * %list is truncated, otherwise the %list is extended and new elements
650 * are populated with given data.
653 resize(size_type __new_size, const value_type& __x);
656 * @brief Resizes the %list to the specified number of elements.
657 * @param new_size Number of elements the %list should contain.
659 * This function will resize the %list to the specified number of
660 * elements. If the number is smaller than the %list's current size the
661 * %list is truncated, otherwise the %list is extended and new elements
662 * are default-constructed.
665 resize(size_type __new_size) { this->resize(__new_size, value_type()); }
669 * Returns a read/write reference to the data at the first element of the
673 front() { return *begin(); }
676 * Returns a read-only (constant) reference to the data at the first
677 * element of the %list.
680 front() const { return *begin(); }
683 * Returns a read/write reference to the data at the last element of the
687 back() { return *(--end()); }
690 * Returns a read-only (constant) reference to the data at the last
691 * element of the %list.
694 back() const { return *(--end()); }
696 // [23.2.2.3] modifiers
698 * @brief Add data to the front of the %list.
699 * @param x Data to be added.
701 * This is a typical stack operation. The function creates an element at
702 * the front of the %list and assigns the given data to it. Due to the
703 * nature of a %list this operation can be done in constant time, and
704 * does not invalidate iterators and references.
707 push_front(const value_type& __x) { this->insert(begin(), __x); }
709 #ifdef _GLIBCPP_DEPRECATED
711 * @brief Add data to the front of the %list.
713 * This is a typical stack operation. The function creates a
714 * default-constructed element at the front of the %list. Due to the nature
715 * of a %list this operation can be done in constant time. You should
716 * consider using push_front(value_type()) instead.
718 * @note This was deprecated in 3.2 and will be removed in 3.3. You must
719 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
723 push_front() { this->insert(begin(), value_type()); }
727 * @brief Removes first element.
729 * This is a typical stack operation. It shrinks the %list by one.
730 * Due to the nature of a %list this operation can be done in constant
731 * time, and only invalidates iterators/references to the element being
734 * Note that no data is returned, and if the first element's data is
735 * needed, it should be retrieved before pop_front() is called.
738 pop_front() { this->erase(begin()); }
741 * @brief Add data to the end of the %list.
742 * @param x Data to be added.
744 * This is a typical stack operation. The function creates an element at
745 * the end of the %list and assigns the given data to it. Due to the
746 * nature of a %list this operation can be done in constant time, and
747 * does not invalidate iterators and references.
750 push_back(const value_type& __x) { this->insert(end(), __x); }
752 #ifdef _GLIBCPP_DEPRECATED
754 * @brief Add data to the end of the %list.
756 * This is a typical stack operation. The function creates a
757 * default-constructed element at the end of the %list. Due to the nature
758 * of a %list this operation can be done in constant time. You should
759 * consider using push_back(value_type()) instead.
761 * @note This was deprecated in 3.2 and will be removed in 3.3. You must
762 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
766 push_back() { this->insert(end(), value_type()); }
770 * @brief Removes last element.
772 * This is a typical stack operation. It shrinks the %list by one.
773 * Due to the nature of a %list this operation can be done in constant
774 * time, and only invalidates iterators/references to the element being
777 * Note that no data is returned, and if the last element's data is
778 * needed, it should be retrieved before pop_back() is called.
783 iterator __tmp = end();
784 this->erase(--__tmp);
788 * @brief Inserts given value into %list before specified iterator.
789 * @param position An iterator into the %list.
790 * @param x Data to be inserted.
791 * @return An iterator that points to the inserted data.
793 * This function will insert a copy of the given value before the specified
795 * Due to the nature of a %list this operation can be done in constant
796 * time, and does not invalidate iterators and references.
799 insert(iterator __position, const value_type& __x)
801 _Node* __tmp = _M_create_node(__x);
802 __tmp->_M_next = __position._M_node;
803 __tmp->_M_prev = __position._M_node->_M_prev;
804 __position._M_node->_M_prev->_M_next = __tmp;
805 __position._M_node->_M_prev = __tmp;
809 #ifdef _GLIBCPP_DEPRECATED
811 * @brief Inserts an element into the %list.
812 * @param position An iterator into the %list.
813 * @return An iterator that points to the inserted element.
815 * This function will insert a default-constructed element before the
816 * specified location. You should consider using
817 * insert(position,value_type()) instead.
818 * Due to the nature of a %list this operation can be done in constant
819 * time, and does not invalidate iterators and references.
821 * @note This was deprecated in 3.2 and will be removed in 3.3. You must
822 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
826 insert(iterator __position) { return insert(__position, value_type()); }
830 * @brief Inserts a number of copies of given data into the %list.
831 * @param position An iterator into the %list.
832 * @param n Number of elements to be inserted.
833 * @param x Data to be inserted.
835 * This function will insert a specified number of copies of the given data
836 * before the location specified by @a position.
838 * Due to the nature of a %list this operation can be done in constant
839 * time, and does not invalidate iterators and references.
842 insert(iterator __pos, size_type __n, const value_type& __x)
843 { _M_fill_insert(__pos, __n, __x); }
846 * @brief Inserts a range into the %list.
847 * @param pos An iterator into the %list.
848 * @param first An input iterator.
849 * @param last An input iterator.
851 * This function will insert copies of the data in the range [first,last)
852 * into the %list before the location specified by @a pos.
854 * Due to the nature of a %list this operation can be done in constant
855 * time, and does not invalidate iterators and references.
857 template<typename _InputIterator>
859 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
861 // Check whether it's an integral type. If so, it's not an iterator.
862 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
863 _M_insert_dispatch(__pos, __first, __last, _Integral());
867 * @brief Remove element at given position.
868 * @param position Iterator pointing to element to be erased.
869 * @return An iterator pointing to the next element (or end()).
871 * This function will erase the element at the given position and thus
872 * shorten the %list by one.
874 * Due to the nature of a %list this operation can be done in constant
875 * time, and only invalidates iterators/references to the element being
877 * The user is also cautioned that
878 * this function only erases the element, and that if the element is itself
879 * a pointer, the pointed-to memory is not touched in any way. Managing
880 * the pointer is the user's responsibilty.
883 erase(iterator __position)
885 _List_node_base* __next_node = __position._M_node->_M_next;
886 _List_node_base* __prev_node = __position._M_node->_M_prev;
887 _Node* __n = static_cast<_Node*>(__position._M_node);
888 __prev_node->_M_next = __next_node;
889 __next_node->_M_prev = __prev_node;
890 _Destroy(&__n->_M_data);
892 return iterator(static_cast<_Node*>(__next_node));
896 * @brief Remove a range of elements.
897 * @param first Iterator pointing to the first element to be erased.
898 * @param last Iterator pointing to one past the last element to be erased.
899 * @return An iterator pointing to the element pointed to by @a last
900 * prior to erasing (or end()).
902 * This function will erase the elements in the range [first,last) and
903 * shorten the %list accordingly.
905 * Due to the nature of a %list this operation can be done in constant
906 * time, and only invalidates iterators/references to the element being
908 * The user is also cautioned that
909 * this function only erases the elements, and that if the elements
910 * themselves are pointers, the pointed-to memory is not touched in any
911 * way. Managing the pointer is the user's responsibilty.
914 erase(iterator __first, iterator __last);
917 * @brief Swaps data with another %list.
918 * @param x A %list of the same element and allocator types.
920 * This exchanges the elements between two lists in constant time.
921 * (It is only swapping a single pointer, so it should be quite fast.)
922 * Note that the global std::swap() function is specialized such that
923 * std::swap(l1,l2) will feed to this function.
926 swap(list& __x) { std::swap(_M_node, __x._M_node); }
929 * Erases all the elements. Note that this function only erases the
930 * elements, and that if the elements themselves are pointers, the
931 * pointed-to memory is not touched in any way. Managing the pointer is
932 * the user's responsibilty.
935 clear() { _Base::__clear(); }
937 // [23.2.2.4] list operations
942 splice(iterator __position, list& __x)
945 this->_M_transfer(__position, __x.begin(), __x.end());
952 splice(iterator __position, list&, iterator __i)
956 if (__position == __i || __position == __j) return;
957 this->_M_transfer(__position, __i, __j);
964 splice(iterator __position, list&, iterator __first, iterator __last)
966 if (__first != __last)
967 this->_M_transfer(__position, __first, __last);
974 remove(const _Tp& __value);
979 template<typename _Predicate>
981 remove_if(_Predicate);
992 template<typename _BinaryPredicate>
994 unique(_BinaryPredicate);
1005 template<typename _StrictWeakOrdering>
1007 merge(list&, _StrictWeakOrdering);
1024 template<typename _StrictWeakOrdering>
1026 sort(_StrictWeakOrdering);
1029 // Internal assign functions follow.
1031 // called by the range assign to implement [23.1.1]/9
1032 template<typename _Integer>
1034 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1036 _M_fill_assign(static_cast<size_type>(__n),
1037 static_cast<value_type>(__val));
1040 // called by the range assign to implement [23.1.1]/9
1041 template<typename _InputIter>
1043 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type);
1045 // Called by assign(n,t), and the range assign when it turns out to be the
1048 _M_fill_assign(size_type __n, const value_type& __val);
1051 // Internal insert functions follow.
1053 // called by the range insert to implement [23.1.1]/9
1054 template<typename _Integer>
1056 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x, __true_type)
1058 _M_fill_insert(__pos, static_cast<size_type>(__n),
1059 static_cast<value_type>(__x));
1062 // called by the range insert to implement [23.1.1]/9
1063 template<typename _InputIterator>
1065 _M_insert_dispatch(iterator __pos,
1066 _InputIterator __first, _InputIterator __last,
1069 // Called by insert(p,n,x), and the range insert when it turns out to be
1072 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1075 // Moves the elements from [first,last) before position.
1077 _M_transfer(iterator __position, iterator __first, iterator __last)
1079 if (__position != __last) {
1080 // Remove [first, last) from its old position.
1081 __last._M_node->_M_prev->_M_next = __position._M_node;
1082 __first._M_node->_M_prev->_M_next = __last._M_node;
1083 __position._M_node->_M_prev->_M_next = __first._M_node;
1085 // Splice [first, last) into its new position.
1086 _List_node_base* __tmp = __position._M_node->_M_prev;
1087 __position._M_node->_M_prev = __last._M_node->_M_prev;
1088 __last._M_node->_M_prev = __first._M_node->_M_prev;
1089 __first._M_node->_M_prev = __tmp;
1096 * @brief List equality comparison.
1098 * @param y A %list of the same type as @a x.
1099 * @return True iff the size and elements of the lists are equal.
1101 * This is an equivalence relation. It is linear in the size of the
1102 * lists. Lists are considered equivalent if their sizes are equal,
1103 * and if corresponding elements compare equal.
1105 template<typename _Tp, typename _Alloc>
1107 operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1109 typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;
1110 const_iterator __end1 = __x.end();
1111 const_iterator __end2 = __y.end();
1113 const_iterator __i1 = __x.begin();
1114 const_iterator __i2 = __y.begin();
1115 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
1119 return __i1 == __end1 && __i2 == __end2;
1123 * @brief List ordering relation.
1125 * @param y A %list of the same type as @a x.
1126 * @return True iff @a x is lexographically less than @a y.
1128 * This is a total ordering relation. It is linear in the size of the
1129 * lists. The elements must be comparable with @c <.
1131 * See std::lexographical_compare() for how the determination is made.
1133 template<typename _Tp, typename _Alloc>
1135 operator<(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1137 return lexicographical_compare(__x.begin(), __x.end(),
1138 __y.begin(), __y.end());
1141 /// Based on operator==
1142 template<typename _Tp, typename _Alloc>
1144 operator!=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1145 { return !(__x == __y); }
1147 /// Based on operator<
1148 template<typename _Tp, typename _Alloc>
1150 operator>(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1151 { return __y < __x; }
1153 /// Based on operator<
1154 template<typename _Tp, typename _Alloc>
1156 operator<=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1157 { return !(__y < __x); }
1159 /// Based on operator<
1160 template<typename _Tp, typename _Alloc>
1162 operator>=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1163 { return !(__x < __y); }
1165 /// See std::list::swap().
1166 template<typename _Tp, typename _Alloc>
1168 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1172 template<typename _Tp, typename _Alloc>
1173 void _List_base<_Tp,_Alloc>::
1176 _List_node<_Tp>* __cur = static_cast<_List_node<_Tp>*>(_M_node->_M_next);
1177 while (__cur != _M_node) {
1178 _List_node<_Tp>* __tmp = __cur;
1179 __cur = static_cast<_List_node<_Tp>*>(__cur->_M_next);
1180 _Destroy(&__tmp->_M_data);
1183 _M_node->_M_next = _M_node;
1184 _M_node->_M_prev = _M_node;
1187 template<typename _Tp, typename _Alloc>
1188 template <typename _InputIter>
1189 void list<_Tp, _Alloc>::
1190 _M_insert_dispatch(iterator __position, _InputIter __first, _InputIter __last,
1193 for ( ; __first != __last; ++__first)
1194 insert(__position, *__first);
1198 template<typename _Tp, typename _Alloc>
1199 void list<_Tp, _Alloc>::
1200 _M_fill_insert(iterator __position, size_type __n, const _Tp& __x)
1202 for ( ; __n > 0; --__n)
1203 insert(__position, __x);
1206 template<typename _Tp, typename _Alloc>
1207 typename list<_Tp,_Alloc>::iterator list<_Tp, _Alloc>::
1208 erase(iterator __first, iterator __last)
1210 while (__first != __last)
1215 template<typename _Tp, typename _Alloc>
1216 void list<_Tp, _Alloc>::
1217 resize(size_type __new_size, const _Tp& __x)
1219 iterator __i = begin();
1220 size_type __len = 0;
1221 for ( ; __i != end() && __len < __new_size; ++__i, ++__len)
1223 if (__len == __new_size)
1225 else // __i == end()
1226 insert(end(), __new_size - __len, __x);
1229 template<typename _Tp, typename _Alloc>
1230 list<_Tp, _Alloc>& list<_Tp, _Alloc>::
1231 operator=(const list<_Tp, _Alloc>& __x)
1234 iterator __first1 = begin();
1235 iterator __last1 = end();
1236 const_iterator __first2 = __x.begin();
1237 const_iterator __last2 = __x.end();
1238 while (__first1 != __last1 && __first2 != __last2)
1239 *__first1++ = *__first2++;
1240 if (__first2 == __last2)
1241 erase(__first1, __last1);
1243 insert(__last1, __first2, __last2);
1248 template<typename _Tp, typename _Alloc>
1249 void list<_Tp, _Alloc>::
1250 _M_fill_assign(size_type __n, const _Tp& __val) {
1251 iterator __i = begin();
1252 for ( ; __i != end() && __n > 0; ++__i, --__n)
1255 insert(end(), __n, __val);
1260 template<typename _Tp, typename _Alloc>
1261 template <typename _InputIter>
1262 void list<_Tp, _Alloc>::
1263 _M_assign_dispatch(_InputIter __first2, _InputIter __last2, __false_type)
1265 iterator __first1 = begin();
1266 iterator __last1 = end();
1267 for ( ; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1268 *__first1 = *__first2;
1269 if (__first2 == __last2)
1270 erase(__first1, __last1);
1272 insert(__last1, __first2, __last2);
1275 template<typename _Tp, typename _Alloc>
1276 void list<_Tp, _Alloc>::
1277 remove(const _Tp& __value)
1279 iterator __first = begin();
1280 iterator __last = end();
1281 while (__first != __last) {
1282 iterator __next = __first;
1284 if (*__first == __value) erase(__first);
1289 template<typename _Tp, typename _Alloc>
1290 void list<_Tp, _Alloc>::
1293 iterator __first = begin();
1294 iterator __last = end();
1295 if (__first == __last) return;
1296 iterator __next = __first;
1297 while (++__next != __last) {
1298 if (*__first == *__next)
1306 template<typename _Tp, typename _Alloc>
1307 void list<_Tp, _Alloc>::
1308 merge(list<_Tp, _Alloc>& __x)
1310 iterator __first1 = begin();
1311 iterator __last1 = end();
1312 iterator __first2 = __x.begin();
1313 iterator __last2 = __x.end();
1314 while (__first1 != __last1 && __first2 != __last2)
1315 if (*__first2 < *__first1) {
1316 iterator __next = __first2;
1317 _M_transfer(__first1, __first2, ++__next);
1322 if (__first2 != __last2) _M_transfer(__last1, __first2, __last2);
1326 __List_base_reverse(_List_node_base* __p)
1328 _List_node_base* __tmp = __p;
1330 std::swap(__tmp->_M_next, __tmp->_M_prev);
1331 __tmp = __tmp->_M_prev; // Old next node is now prev.
1332 } while (__tmp != __p);
1335 template<typename _Tp, typename _Alloc>
1336 inline void list<_Tp, _Alloc>::
1338 { __List_base_reverse(this->_M_node); }
1340 template<typename _Tp, typename _Alloc>
1341 void list<_Tp, _Alloc>::
1344 // Do nothing if the list has length 0 or 1.
1345 if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) {
1346 list<_Tp, _Alloc> __carry;
1347 list<_Tp, _Alloc> __counter[64];
1350 __carry.splice(__carry.begin(), *this, begin());
1352 while(__i < __fill && !__counter[__i].empty()) {
1353 __counter[__i].merge(__carry);
1354 __carry.swap(__counter[__i++]);
1356 __carry.swap(__counter[__i]);
1357 if (__i == __fill) ++__fill;
1360 for (int __i = 1; __i < __fill; ++__i)
1361 __counter[__i].merge(__counter[__i-1]);
1362 swap(__counter[__fill-1]);
1366 template<typename _Tp, typename _Alloc>
1367 template <typename _Predicate>
1368 void list<_Tp, _Alloc>::
1369 remove_if(_Predicate __pred)
1371 iterator __first = begin();
1372 iterator __last = end();
1373 while (__first != __last) {
1374 iterator __next = __first;
1376 if (__pred(*__first)) erase(__first);
1381 template<typename _Tp, typename _Alloc>
1382 template <typename _BinaryPredicate>
1383 void list<_Tp, _Alloc>::
1384 unique(_BinaryPredicate __binary_pred)
1386 iterator __first = begin();
1387 iterator __last = end();
1388 if (__first == __last) return;
1389 iterator __next = __first;
1390 while (++__next != __last) {
1391 if (__binary_pred(*__first, *__next))
1399 template<typename _Tp, typename _Alloc>
1400 template <typename _StrictWeakOrdering>
1401 void list<_Tp, _Alloc>::
1402 merge(list<_Tp, _Alloc>& __x, _StrictWeakOrdering __comp)
1404 iterator __first1 = begin();
1405 iterator __last1 = end();
1406 iterator __first2 = __x.begin();
1407 iterator __last2 = __x.end();
1408 while (__first1 != __last1 && __first2 != __last2)
1409 if (__comp(*__first2, *__first1)) {
1410 iterator __next = __first2;
1411 _M_transfer(__first1, __first2, ++__next);
1416 if (__first2 != __last2) _M_transfer(__last1, __first2, __last2);
1419 template<typename _Tp, typename _Alloc>
1420 template <typename _StrictWeakOrdering>
1421 void list<_Tp, _Alloc>::
1422 sort(_StrictWeakOrdering __comp)
1424 // Do nothing if the list has length 0 or 1.
1425 if (_M_node->_M_next != _M_node && _M_node->_M_next->_M_next != _M_node) {
1426 list<_Tp, _Alloc> __carry;
1427 list<_Tp, _Alloc> __counter[64];
1430 __carry.splice(__carry.begin(), *this, begin());
1432 while(__i < __fill && !__counter[__i].empty()) {
1433 __counter[__i].merge(__carry, __comp);
1434 __carry.swap(__counter[__i++]);
1436 __carry.swap(__counter[__i]);
1437 if (__i == __fill) ++__fill;
1440 for (int __i = 1; __i < __fill; ++__i)
1441 __counter[__i].merge(__counter[__i-1], __comp);
1442 swap(__counter[__fill-1]);
1448 #endif /* __GLIBCPP_INTERNAL_LIST_H */