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>
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
<_Tp
>* _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
<_Tp
>* _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 _M_node
= _M_get_node();
305 _M_node
->_M_next
= _M_node
;
306 _M_node
->_M_prev
= _M_node
;
309 // This is what actually destroys the list.
313 _M_put_node(_M_node
);
322 * @brief A standard container with linear time access to elements, and
323 * fixed time insertion/deletion at any point in the sequence.
325 * @ingroup Containers
328 * Meets the requirements of a <a href="tables.html#65">container</a>, a
329 * <a href="tables.html#66">reversible container</a>, and a
330 * <a href="tables.html#67">sequence</a>, including the
331 * <a href="tables.html#68">optional sequence requirements</a> with the
332 * %exception of @c at and @c operator[].
334 * This is a @e doubly @e linked %list. Traversal up and down the %list
335 * requires linear time, but adding and removing elements (or @e nodes) is
336 * done in constant time, regardless of where the change takes place.
337 * Unlike std::vector and std::deque, random-access iterators are not
338 * provided, so subscripting ( @c [] ) access is not allowed. For algorithms
339 * which only need sequential access, this lack makes no difference.
341 * Also unlike the other standard containers, std::list provides specialized
342 * algorithms %unique to linked lists, such as splicing, sorting, and
346 * A couple points on memory allocation for list<Tp>:
348 * First, we never actually allocate a Tp, we allocate List_node<Tp>'s
349 * and trust [20.1.5]/4 to DTRT. This is to ensure that after elements from
350 * %list<X,Alloc1> are spliced into %list<X,Alloc2>, destroying the memory of
351 * the second %list is a valid operation, i.e., Alloc1 giveth and Alloc2
354 * Second, a %list conceptually represented as
356 * A <---> B <---> C <---> D
358 * is actually circular; a link exists between A and D. The %list class
359 * holds (as its only data member) a private list::iterator pointing to
360 * @e D, not to @e A! To get to the head of the %list, we start at the tail
361 * and move forward by one. When this member iterator's next/previous
362 * pointers refer to itself, the %list is %empty.
365 template<typename _Tp
, typename _Alloc
= allocator
<_Tp
> >
366 class list
: protected _List_base
<_Tp
, _Alloc
>
368 // concept requirements
369 __glibcpp_class_requires(_Tp
, _SGIAssignableConcept
)
371 typedef _List_base
<_Tp
, _Alloc
> _Base
;
374 typedef _Tp value_type
;
375 typedef value_type
* pointer
;
376 typedef const value_type
* const_pointer
;
377 typedef _List_iterator
<_Tp
,_Tp
&,_Tp
*> iterator
;
378 typedef _List_iterator
<_Tp
,const _Tp
&,const _Tp
*> const_iterator
;
379 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
380 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
381 typedef value_type
& reference
;
382 typedef const value_type
& const_reference
;
383 typedef size_t size_type
;
384 typedef ptrdiff_t difference_type
;
385 typedef typename
_Base::allocator_type allocator_type
;
388 // Note that pointers-to-_Node's can be ctor-converted to iterator types.
389 typedef _List_node
<_Tp
> _Node
;
392 * One data member plus two memory-handling functions. If the _Alloc
393 * type requires separate instances, then one of those will also be
394 * included, accumulated from the topmost parent.
397 using _Base::_M_node
;
398 using _Base::_M_put_node
;
399 using _Base::_M_get_node
;
403 * @param x An instance of user data.
405 * Allocates space for a new node and constructs a copy of @a x in it.
409 _M_create_node(const value_type
& __x
)
411 _Node
* __p
= _M_get_node();
413 _Construct(&__p
->_M_data
, __x
);
418 __throw_exception_again
;
425 * Allocates space for a new node and default-constructs a new instance
426 * of @c value_type in it.
432 _Node
* __p
= _M_get_node();
434 _Construct(&__p
->_M_data
);
439 __throw_exception_again
;
445 // [23.2.2.1] construct/copy/destroy
446 // (assign() and get_allocator() are also listed in this section)
448 * @brief Default constructor creates no elements.
451 list(const allocator_type
& __a
= allocator_type())
455 * @brief Create a %list with copies of an exemplar element.
456 * @param n The number of elements to initially create.
457 * @param value An element to copy.
459 * This constructor fills the %list with @a n copies of @a value.
461 list(size_type __n
, const value_type
& __value
,
462 const allocator_type
& __a
= allocator_type())
464 { this->insert(begin(), __n
, __value
); }
467 * @brief Create a %list with default elements.
468 * @param n The number of elements to initially create.
470 * This constructor fills the %list with @a n copies of a
471 * default-constructed element.
475 : _Base(allocator_type())
476 { this->insert(begin(), __n
, value_type()); }
479 * @brief %List copy constructor.
480 * @param x A %list of identical element and allocator types.
482 * The newly-created %list uses a copy of the allocation object used
485 list(const list
& __x
)
486 : _Base(__x
.get_allocator())
487 { this->insert(begin(), __x
.begin(), __x
.end()); }
490 * @brief Builds a %list from a range.
491 * @param first An input iterator.
492 * @param last An input iterator.
494 * Create a %list consisting of copies of the elements from [first,last).
495 * This is linear in N (where N is distance(first,last)).
498 * We don't need any dispatching tricks here, because insert does all of
502 template<typename _InputIterator
>
503 list(_InputIterator __first
, _InputIterator __last
,
504 const allocator_type
& __a
= allocator_type())
506 { this->insert(begin(), __first
, __last
); }
509 * The 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.
516 * @brief %List assignment operator.
517 * @param x A %list of identical element and allocator types.
519 * All the elements of @a x are copied, but unlike the copy constructor,
520 * the allocator object is not copied.
523 operator=(const list
& __x
);
526 * @brief Assigns a given value to a %list.
527 * @param n Number of elements to be assigned.
528 * @param val Value to be assigned.
530 * This function fills a %list with @a n copies of the given value.
531 * Note that the assignment completely changes the %list and that the
532 * resulting %list's size is the same as the number of elements assigned.
533 * Old data may be lost.
536 assign(size_type __n
, const value_type
& __val
) { _M_fill_assign(__n
, __val
); }
539 * @brief Assigns a range to a %list.
540 * @param first An input iterator.
541 * @param last An input iterator.
543 * This function fills a %list with copies of the elements in the
544 * range [first,last).
546 * Note that the assignment completely changes the %list and that the
547 * resulting %list's size is the same as the number of elements assigned.
548 * Old data may be lost.
550 template<typename _InputIterator
>
552 assign(_InputIterator __first
, _InputIterator __last
)
554 // Check whether it's an integral type. If so, it's not an iterator.
555 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
556 _M_assign_dispatch(__first
, __last
, _Integral());
559 /// Get a copy of the memory allocation object.
561 get_allocator() const { return _Base::get_allocator(); }
565 * Returns a read/write iterator that points to the first element in the
566 * %list. Iteration is done in ordinary element order.
569 begin() { return static_cast<_Node
*>(_M_node
->_M_next
); }
572 * Returns a read-only (constant) iterator that points to the first element
573 * in the %list. Iteration is done in ordinary element order.
576 begin() const { return static_cast<_Node
*>(_M_node
->_M_next
); }
579 * Returns a read/write iterator that points one past the last element in
580 * the %list. Iteration is done in ordinary element order.
583 end() { return _M_node
; }
586 * Returns a read-only (constant) iterator that points one past the last
587 * element in the %list. Iteration is done in ordinary element order.
590 end() const { return _M_node
; }
593 * Returns a read/write reverse iterator that points to the last element in
594 * the %list. Iteration is done in reverse element order.
597 rbegin() { return reverse_iterator(end()); }
600 * Returns a read-only (constant) reverse iterator that points to the last
601 * element in the %list. Iteration is done in reverse element order.
603 const_reverse_iterator
604 rbegin() const { return const_reverse_iterator(end()); }
607 * Returns a read/write reverse iterator that points to one before the
608 * first element in the %list. Iteration is done in reverse element
612 rend() { return reverse_iterator(begin()); }
615 * Returns a read-only (constant) reverse iterator that points to one
616 * before the first element in the %list. Iteration is done in reverse
619 const_reverse_iterator
621 { return const_reverse_iterator(begin()); }
623 // [23.2.2.2] capacity
625 * Returns true if the %list is empty. (Thus begin() would equal end().)
628 empty() const { return _M_node
->_M_next
== _M_node
; }
630 /** Returns the number of elements in the %list. */
632 size() const { return std::distance(begin(), end()); }
634 /** Returns the size() of the largest possible %list. */
636 max_size() const { return size_type(-1); }
639 * @brief Resizes the %list to the specified number of elements.
640 * @param new_size Number of elements the %list should contain.
641 * @param x Data with which new elements should be populated.
643 * This function will %resize the %list to the specified number of
644 * elements. If the number is smaller than the %list's current size the
645 * %list is truncated, otherwise the %list is extended and new elements
646 * are populated with given data.
649 resize(size_type __new_size
, const value_type
& __x
);
652 * @brief Resizes the %list to the specified number of elements.
653 * @param new_size Number of elements the %list should contain.
655 * This function will resize the %list to the specified number of
656 * elements. If the number is smaller than the %list's current size the
657 * %list is truncated, otherwise the %list is extended and new elements
658 * are default-constructed.
661 resize(size_type __new_size
) { this->resize(__new_size
, value_type()); }
665 * Returns a read/write reference to the data at the first element of the
669 front() { return *begin(); }
672 * Returns a read-only (constant) reference to the data at the first
673 * element of the %list.
676 front() const { return *begin(); }
679 * Returns a read/write reference to the data at the last element of the
683 back() { return *(--end()); }
686 * Returns a read-only (constant) reference to the data at the last
687 * element of the %list.
690 back() const { return *(--end()); }
692 // [23.2.2.3] modifiers
694 * @brief Add data to the front of the %list.
695 * @param x Data to be added.
697 * This is a typical stack operation. The function creates an element at
698 * the front of the %list and assigns the given data to it. Due to the
699 * nature of a %list this operation can be done in constant time, and
700 * does not invalidate iterators and references.
703 push_front(const value_type
& __x
) { this->insert(begin(), __x
); }
705 #ifdef _GLIBCPP_DEPRECATED
707 * @brief Add data to the front of the %list.
709 * This is a typical stack operation. The function creates a
710 * default-constructed element at the front of the %list. Due to the
711 * nature of a %list this operation can be done in constant time. You
712 * should consider using push_front(value_type()) instead.
714 * @note This was deprecated in 3.2 and will be removed in 3.4. You must
715 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
719 push_front() { this->insert(begin(), value_type()); }
723 * @brief Removes first element.
725 * This is a typical stack operation. It shrinks the %list by one.
726 * Due to the nature of a %list this operation can be done in constant
727 * time, and only invalidates iterators/references to the element being
730 * Note that no data is returned, and if the first element's data is
731 * needed, it should be retrieved before pop_front() is called.
734 pop_front() { this->erase(begin()); }
737 * @brief Add data to the end of the %list.
738 * @param x Data to be added.
740 * This is a typical stack operation. The function creates an element at
741 * the end of the %list and assigns the given data to it. Due to the
742 * nature of a %list this operation can be done in constant time, and
743 * does not invalidate iterators and references.
746 push_back(const value_type
& __x
) { this->insert(end(), __x
); }
748 #ifdef _GLIBCPP_DEPRECATED
750 * @brief Add data to the end of the %list.
752 * This is a typical stack operation. The function creates a
753 * default-constructed element at the end of the %list. Due to the nature
754 * of a %list this operation can be done in constant time. You should
755 * consider using push_back(value_type()) instead.
757 * @note This was deprecated in 3.2 and will be removed in 3.4. You must
758 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
762 push_back() { this->insert(end(), value_type()); }
766 * @brief Removes last element.
768 * This is a typical stack operation. It shrinks the %list by one.
769 * Due to the nature of a %list this operation can be done in constant
770 * time, and only invalidates iterators/references to the element being
773 * Note that no data is returned, and if the last element's data is
774 * needed, it should be retrieved before pop_back() is called.
779 iterator __tmp
= end();
780 this->erase(--__tmp
);
784 * @brief Inserts given value into %list before specified iterator.
785 * @param position An iterator into the %list.
786 * @param x Data to be inserted.
787 * @return An iterator that points to the inserted data.
789 * This function will insert a copy of the given value before the specified
791 * Due to the nature of a %list this operation can be done in constant
792 * time, and does not invalidate iterators and references.
795 insert(iterator __position
, const value_type
& __x
);
797 #ifdef _GLIBCPP_DEPRECATED
799 * @brief Inserts an element into the %list.
800 * @param position An iterator into the %list.
801 * @return An iterator that points to the inserted element.
803 * This function will insert a default-constructed element before the
804 * specified location. You should consider using
805 * insert(position,value_type()) instead.
806 * Due to the nature of a %list this operation can be done in constant
807 * time, and does not invalidate iterators and references.
809 * @note This was deprecated in 3.2 and will be removed in 3.4. You must
810 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
814 insert(iterator __position
) { return insert(__position
, value_type()); }
818 * @brief Inserts a number of copies of given data into the %list.
819 * @param position An iterator into the %list.
820 * @param n Number of elements to be inserted.
821 * @param x Data to be inserted.
823 * This function will insert a specified number of copies of the given data
824 * before the location specified by @a position.
826 * Due to the nature of a %list this operation can be done in constant
827 * time, and does not invalidate iterators and references.
830 insert(iterator __pos
, size_type __n
, const value_type
& __x
)
831 { _M_fill_insert(__pos
, __n
, __x
); }
834 * @brief Inserts a range into the %list.
835 * @param pos An iterator into the %list.
836 * @param first An input iterator.
837 * @param last An input iterator.
839 * This function will insert copies of the data in the range [first,last)
840 * into the %list before the location specified by @a pos.
842 * Due to the nature of a %list this operation can be done in constant
843 * time, and does not invalidate iterators and references.
845 template<typename _InputIterator
>
847 insert(iterator __pos
, _InputIterator __first
, _InputIterator __last
)
849 // Check whether it's an integral type. If so, it's not an iterator.
850 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
851 _M_insert_dispatch(__pos
, __first
, __last
, _Integral());
855 * @brief Remove element at given position.
856 * @param position Iterator pointing to element to be erased.
857 * @return An iterator pointing to the next element (or end()).
859 * This function will erase the element at the given position and thus
860 * shorten the %list by one.
862 * Due to the nature of a %list this operation can be done in constant
863 * time, and only invalidates iterators/references to the element being
865 * The user is also cautioned that
866 * this function only erases the element, and that if the element is itself
867 * a pointer, the pointed-to memory is not touched in any way. Managing
868 * the pointer is the user's responsibilty.
871 erase(iterator __position
);
874 * @brief Remove a range of elements.
875 * @param first Iterator pointing to the first element to be erased.
876 * @param last Iterator pointing to one past the last element to be
878 * @return An iterator pointing to the element pointed to by @a last
879 * prior to erasing (or end()).
881 * This function will erase the elements in the range [first,last) and
882 * shorten the %list accordingly.
884 * Due to the nature of a %list this operation can be done in constant
885 * time, and only invalidates iterators/references to the element being
887 * The user is also cautioned that
888 * this function only erases the elements, and that if the elements
889 * themselves are pointers, the pointed-to memory is not touched in any
890 * way. Managing the pointer is the user's responsibilty.
893 erase(iterator __first
, iterator __last
)
895 while (__first
!= __last
)
901 * @brief Swaps data with another %list.
902 * @param x A %list of the same element and allocator types.
904 * This exchanges the elements between two lists in constant time.
905 * (It is only swapping a single pointer, so it should be quite fast.)
906 * Note that the global std::swap() function is specialized such that
907 * std::swap(l1,l2) will feed to this function.
910 swap(list
& __x
) { std::swap(_M_node
, __x
._M_node
); }
913 * Erases all the elements. Note that this function only erases the
914 * elements, and that if the elements themselves are pointers, the
915 * pointed-to memory is not touched in any way. Managing the pointer is
916 * the user's responsibilty.
919 clear() { _Base::__clear(); }
921 // [23.2.2.4] list operations
926 splice(iterator __position
, list
& __x
)
929 this->_M_transfer(__position
, __x
.begin(), __x
.end());
936 splice(iterator __position
, list
&, iterator __i
)
940 if (__position
== __i
|| __position
== __j
) return;
941 this->_M_transfer(__position
, __i
, __j
);
948 splice(iterator __position
, list
&, iterator __first
, iterator __last
)
950 if (__first
!= __last
)
951 this->_M_transfer(__position
, __first
, __last
);
958 remove(const _Tp
& __value
);
963 template<typename _Predicate
>
965 remove_if(_Predicate
);
976 template<typename _BinaryPredicate
>
978 unique(_BinaryPredicate
);
989 template<typename _StrictWeakOrdering
>
991 merge(list
&, _StrictWeakOrdering
);
997 reverse() { __List_base_reverse(this->_M_node
); }
1008 template<typename _StrictWeakOrdering
>
1010 sort(_StrictWeakOrdering
);
1013 // Internal assign functions follow.
1015 // called by the range assign to implement [23.1.1]/9
1016 template<typename _Integer
>
1018 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1020 _M_fill_assign(static_cast<size_type
>(__n
),
1021 static_cast<value_type
>(__val
));
1024 // called by the range assign to implement [23.1.1]/9
1025 template<typename _InputIter
>
1027 _M_assign_dispatch(_InputIter __first
, _InputIter __last
, __false_type
);
1029 // Called by assign(n,t), and the range assign when it turns out to be the
1032 _M_fill_assign(size_type __n
, const value_type
& __val
);
1035 // Internal insert functions follow.
1037 // called by the range insert to implement [23.1.1]/9
1038 template<typename _Integer
>
1040 _M_insert_dispatch(iterator __pos
, _Integer __n
, _Integer __x
,
1043 _M_fill_insert(__pos
, static_cast<size_type
>(__n
),
1044 static_cast<value_type
>(__x
));
1047 // called by the range insert to implement [23.1.1]/9
1048 template<typename _InputIterator
>
1050 _M_insert_dispatch(iterator __pos
,
1051 _InputIterator __first
, _InputIterator __last
,
1054 for ( ; __first
!= __last
; ++__first
)
1055 insert(__pos
, *__first
);
1058 // Called by insert(p,n,x), and the range insert when it turns out to be
1061 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
)
1063 for ( ; __n
> 0; --__n
)
1068 // Moves the elements from [first,last) before position.
1070 _M_transfer(iterator __position
, iterator __first
, iterator __last
)
1072 if (__position
!= __last
) {
1073 // Remove [first, last) from its old position.
1074 __last
._M_node
->_M_prev
->_M_next
= __position
._M_node
;
1075 __first
._M_node
->_M_prev
->_M_next
= __last
._M_node
;
1076 __position
._M_node
->_M_prev
->_M_next
= __first
._M_node
;
1078 // Splice [first, last) into its new position.
1079 _List_node_base
* __tmp
= __position
._M_node
->_M_prev
;
1080 __position
._M_node
->_M_prev
= __last
._M_node
->_M_prev
;
1081 __last
._M_node
->_M_prev
= __first
._M_node
->_M_prev
;
1082 __first
._M_node
->_M_prev
= __tmp
;
1089 * @brief List equality comparison.
1091 * @param y A %list of the same type as @a x.
1092 * @return True iff the size and elements of the lists are equal.
1094 * This is an equivalence relation. It is linear in the size of the
1095 * lists. Lists are considered equivalent if their sizes are equal,
1096 * and if corresponding elements compare equal.
1098 template<typename _Tp
, typename _Alloc
>
1100 operator==(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1102 typedef typename list
<_Tp
,_Alloc
>::const_iterator const_iterator
;
1103 const_iterator __end1
= __x
.end();
1104 const_iterator __end2
= __y
.end();
1106 const_iterator __i1
= __x
.begin();
1107 const_iterator __i2
= __y
.begin();
1108 while (__i1
!= __end1
&& __i2
!= __end2
&& *__i1
== *__i2
) {
1112 return __i1
== __end1
&& __i2
== __end2
;
1116 * @brief List ordering relation.
1118 * @param y A %list of the same type as @a x.
1119 * @return True iff @a x is lexicographically less than @a y.
1121 * This is a total ordering relation. It is linear in the size of the
1122 * lists. The elements must be comparable with @c <.
1124 * See std::lexicographical_compare() for how the determination is made.
1126 template<typename _Tp
, typename _Alloc
>
1128 operator<(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1130 return lexicographical_compare(__x
.begin(), __x
.end(),
1131 __y
.begin(), __y
.end());
1134 /// Based on operator==
1135 template<typename _Tp
, typename _Alloc
>
1137 operator!=(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1138 { return !(__x
== __y
); }
1140 /// Based on operator<
1141 template<typename _Tp
, typename _Alloc
>
1143 operator>(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1144 { return __y
< __x
; }
1146 /// Based on operator<
1147 template<typename _Tp
, typename _Alloc
>
1149 operator<=(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1150 { return !(__y
< __x
); }
1152 /// Based on operator<
1153 template<typename _Tp
, typename _Alloc
>
1155 operator>=(const list
<_Tp
,_Alloc
>& __x
, const list
<_Tp
,_Alloc
>& __y
)
1156 { return !(__x
< __y
); }
1158 /// See std::list::swap().
1159 template<typename _Tp
, typename _Alloc
>
1161 swap(list
<_Tp
, _Alloc
>& __x
, list
<_Tp
, _Alloc
>& __y
)
1165 #endif /* __GLIBCPP_INTERNAL_LIST_H */