* fold-const.c (fold_unary): Use build1 instead of copy_node.
[official-gcc.git] / libstdc++-v3 / include / bits / stl_list.h
blobb928e00515baaf9028dfc31800720deec79cb5ec
1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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,
19 // USA.
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.
32 * Copyright (c) 1994
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.
56 /** @file stl_list.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
61 #ifndef _LIST_H
62 #define _LIST_H 1
64 #include <bits/concept_check.h>
66 namespace _GLIBCXX_STD
68 // Supporting structures are split into common and templated types; the
69 // latter publicly inherits from the former in an effort to reduce code
70 // duplication. This results in some "needless" static_cast'ing later on,
71 // but it's all safe downcasting.
73 /// @if maint Common part of a node in the %list. @endif
74 struct _List_node_base
76 _List_node_base* _M_next; ///< Self-explanatory
77 _List_node_base* _M_prev; ///< Self-explanatory
79 static void
80 swap(_List_node_base& __x, _List_node_base& __y);
82 void
83 transfer(_List_node_base * const __first,
84 _List_node_base * const __last);
86 void
87 reverse();
89 void
90 hook(_List_node_base * const __position);
92 void
93 unhook();
96 /// @if maint An actual node in the %list. @endif
97 template<typename _Tp>
98 struct _List_node : public _List_node_base
100 _Tp _M_data; ///< User's data.
104 * @brief A list::iterator.
106 * @if maint
107 * All the functions are op overloads.
108 * @endif
110 template<typename _Tp>
111 struct _List_iterator
113 typedef _List_iterator<_Tp> _Self;
114 typedef _List_node<_Tp> _Node;
116 typedef ptrdiff_t difference_type;
117 typedef bidirectional_iterator_tag iterator_category;
118 typedef _Tp value_type;
119 typedef _Tp* pointer;
120 typedef _Tp& reference;
122 _List_iterator()
123 : _M_node() { }
125 _List_iterator(_List_node_base* __x)
126 : _M_node(__x) { }
128 // Must downcast from List_node_base to _List_node to get to _M_data.
129 reference
130 operator*() const
131 { return static_cast<_Node*>(_M_node)->_M_data; }
133 pointer
134 operator->() const
135 { return &static_cast<_Node*>(_M_node)->_M_data; }
137 _Self&
138 operator++()
140 _M_node = _M_node->_M_next;
141 return *this;
144 _Self
145 operator++(int)
147 _Self __tmp = *this;
148 _M_node = _M_node->_M_next;
149 return __tmp;
152 _Self&
153 operator--()
155 _M_node = _M_node->_M_prev;
156 return *this;
159 _Self
160 operator--(int)
162 _Self __tmp = *this;
163 _M_node = _M_node->_M_prev;
164 return __tmp;
167 bool
168 operator==(const _Self& __x) const
169 { return _M_node == __x._M_node; }
171 bool
172 operator!=(const _Self& __x) const
173 { return _M_node != __x._M_node; }
175 // The only member points to the %list element.
176 _List_node_base* _M_node;
180 * @brief A list::const_iterator.
182 * @if maint
183 * All the functions are op overloads.
184 * @endif
186 template<typename _Tp>
187 struct _List_const_iterator
189 typedef _List_const_iterator<_Tp> _Self;
190 typedef const _List_node<_Tp> _Node;
191 typedef _List_iterator<_Tp> iterator;
193 typedef ptrdiff_t difference_type;
194 typedef bidirectional_iterator_tag iterator_category;
195 typedef _Tp value_type;
196 typedef const _Tp* pointer;
197 typedef const _Tp& reference;
199 _List_const_iterator()
200 : _M_node() { }
202 _List_const_iterator(const _List_node_base* __x)
203 : _M_node(__x) { }
205 _List_const_iterator(const iterator& __x)
206 : _M_node(__x._M_node) { }
208 // Must downcast from List_node_base to _List_node to get to
209 // _M_data.
210 reference
211 operator*() const
212 { return static_cast<_Node*>(_M_node)->_M_data; }
214 pointer
215 operator->() const
216 { return &static_cast<_Node*>(_M_node)->_M_data; }
218 _Self&
219 operator++()
221 _M_node = _M_node->_M_next;
222 return *this;
225 _Self
226 operator++(int)
228 _Self __tmp = *this;
229 _M_node = _M_node->_M_next;
230 return __tmp;
233 _Self&
234 operator--()
236 _M_node = _M_node->_M_prev;
237 return *this;
240 _Self
241 operator--(int)
243 _Self __tmp = *this;
244 _M_node = _M_node->_M_prev;
245 return __tmp;
248 bool
249 operator==(const _Self& __x) const
250 { return _M_node == __x._M_node; }
252 bool
253 operator!=(const _Self& __x) const
254 { return _M_node != __x._M_node; }
256 // The only member points to the %list element.
257 const _List_node_base* _M_node;
260 template<typename _Val>
261 inline bool
262 operator==(const _List_iterator<_Val>& __x,
263 const _List_const_iterator<_Val>& __y)
264 { return __x._M_node == __y._M_node; }
266 template<typename _Val>
267 inline bool
268 operator!=(const _List_iterator<_Val>& __x,
269 const _List_const_iterator<_Val>& __y)
270 { return __x._M_node != __y._M_node; }
274 * @if maint
275 * See bits/stl_deque.h's _Deque_base for an explanation.
276 * @endif
278 template<typename _Tp, typename _Alloc>
279 class _List_base
281 protected:
282 // NOTA BENE
283 // The stored instance is not actually of "allocator_type"'s
284 // type. Instead we rebind the type to
285 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
286 // should probably be the same. List_node<Tp> is not the same
287 // size as Tp (it's two pointers larger), and specializations on
288 // Tp may go unused because List_node<Tp> is being bound
289 // instead.
291 // We put this to the test in the constructors and in
292 // get_allocator, where we use conversions between
293 // allocator_type and _Node_Alloc_type. The conversion is
294 // required by table 32 in [20.1.5].
295 typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
297 _Node_Alloc_type;
299 struct _List_impl
300 : public _Node_Alloc_type
302 _List_node_base _M_node;
303 _List_impl (const _Node_Alloc_type& __a)
304 : _Node_Alloc_type(__a)
308 _List_impl _M_impl;
310 _List_node<_Tp>*
311 _M_get_node()
312 { return _M_impl._Node_Alloc_type::allocate(1); }
314 void
315 _M_put_node(_List_node<_Tp>* __p)
316 { _M_impl._Node_Alloc_type::deallocate(__p, 1); }
318 public:
319 typedef _Alloc allocator_type;
321 allocator_type
322 get_allocator() const
323 { return allocator_type(*static_cast<
324 const _Node_Alloc_type*>(&this->_M_impl)); }
326 _List_base(const allocator_type& __a)
327 : _M_impl(__a)
328 { _M_init(); }
330 // This is what actually destroys the list.
331 ~_List_base()
332 { _M_clear(); }
334 void
335 _M_clear();
337 void
338 _M_init()
340 this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
341 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
346 * @brief A standard container with linear time access to elements,
347 * and fixed time insertion/deletion at any point in the sequence.
349 * @ingroup Containers
350 * @ingroup Sequences
352 * Meets the requirements of a <a href="tables.html#65">container</a>, a
353 * <a href="tables.html#66">reversible container</a>, and a
354 * <a href="tables.html#67">sequence</a>, including the
355 * <a href="tables.html#68">optional sequence requirements</a> with the
356 * %exception of @c at and @c operator[].
358 * This is a @e doubly @e linked %list. Traversal up and down the
359 * %list requires linear time, but adding and removing elements (or
360 * @e nodes) is done in constant time, regardless of where the
361 * change takes place. Unlike std::vector and std::deque,
362 * random-access iterators are not provided, so subscripting ( @c
363 * [] ) access is not allowed. For algorithms which only need
364 * sequential access, this lack makes no difference.
366 * Also unlike the other standard containers, std::list provides
367 * specialized algorithms %unique to linked lists, such as
368 * splicing, sorting, and in-place reversal.
370 * @if maint
371 * A couple points on memory allocation for list<Tp>:
373 * First, we never actually allocate a Tp, we allocate
374 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
375 * that after elements from %list<X,Alloc1> are spliced into
376 * %list<X,Alloc2>, destroying the memory of the second %list is a
377 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
379 * Second, a %list conceptually represented as
380 * @code
381 * A <---> B <---> C <---> D
382 * @endcode
383 * is actually circular; a link exists between A and D. The %list
384 * class holds (as its only data member) a private list::iterator
385 * pointing to @e D, not to @e A! To get to the head of the %list,
386 * we start at the tail and move forward by one. When this member
387 * iterator's next/previous pointers refer to itself, the %list is
388 * %empty. @endif
390 template<typename _Tp, typename _Alloc = allocator<_Tp> >
391 class list : protected _List_base<_Tp, _Alloc>
393 // concept requirements
394 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
396 typedef _List_base<_Tp, _Alloc> _Base;
398 public:
399 typedef _Tp value_type;
400 typedef typename _Alloc::pointer pointer;
401 typedef typename _Alloc::const_pointer const_pointer;
402 typedef typename _Alloc::reference reference;
403 typedef typename _Alloc::const_reference const_reference;
404 typedef _List_iterator<_Tp> iterator;
405 typedef _List_const_iterator<_Tp> const_iterator;
406 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
407 typedef std::reverse_iterator<iterator> reverse_iterator;
408 typedef size_t size_type;
409 typedef ptrdiff_t difference_type;
410 typedef typename _Base::allocator_type allocator_type;
412 protected:
413 // Note that pointers-to-_Node's can be ctor-converted to
414 // iterator types.
415 typedef _List_node<_Tp> _Node;
417 /** @if maint
418 * One data member plus two memory-handling functions. If the
419 * _Alloc type requires separate instances, then one of those
420 * will also be included, accumulated from the topmost parent.
421 * @endif
423 using _Base::_M_impl;
424 using _Base::_M_put_node;
425 using _Base::_M_get_node;
428 * @if maint
429 * @param x An instance of user data.
431 * Allocates space for a new node and constructs a copy of @a x in it.
432 * @endif
434 _Node*
435 _M_create_node(const value_type& __x)
437 _Node* __p = this->_M_get_node();
440 this->get_allocator().construct(&__p->_M_data, __x);
442 catch(...)
444 _M_put_node(__p);
445 __throw_exception_again;
447 return __p;
450 public:
451 // [23.2.2.1] construct/copy/destroy
452 // (assign() and get_allocator() are also listed in this section)
454 * @brief Default constructor creates no elements.
456 explicit
457 list(const allocator_type& __a = allocator_type())
458 : _Base(__a) { }
461 * @brief Create a %list with copies of an exemplar element.
462 * @param n The number of elements to initially create.
463 * @param value An element to copy.
465 * This constructor fills the %list with @a n copies of @a value.
467 list(size_type __n, const value_type& __value,
468 const allocator_type& __a = allocator_type())
469 : _Base(__a)
470 { this->insert(begin(), __n, __value); }
473 * @brief Create a %list with default elements.
474 * @param n The number of elements to initially create.
476 * This constructor fills the %list with @a n copies of a
477 * default-constructed element.
479 explicit
480 list(size_type __n)
481 : _Base(allocator_type())
482 { this->insert(begin(), __n, value_type()); }
485 * @brief %List copy constructor.
486 * @param x A %list of identical element and allocator types.
488 * The newly-created %list uses a copy of the allocation object used
489 * by @a x.
491 list(const list& __x)
492 : _Base(__x.get_allocator())
493 { this->insert(begin(), __x.begin(), __x.end()); }
496 * @brief Builds a %list from a range.
497 * @param first An input iterator.
498 * @param last An input iterator.
500 * Create a %list consisting of copies of the elements from
501 * [@a first,@a last). This is linear in N (where N is
502 * distance(@a first,@a last)).
504 * @if maint
505 * We don't need any dispatching tricks here, because insert does all of
506 * that anyway.
507 * @endif
509 template<typename _InputIterator>
510 list(_InputIterator __first, _InputIterator __last,
511 const allocator_type& __a = allocator_type())
512 : _Base(__a)
513 { this->insert(begin(), __first, __last); }
516 * No explicit dtor needed as the _Base dtor takes care of
517 * things. The _Base dtor only erases the elements, and note
518 * that if the elements themselves are pointers, the pointed-to
519 * memory is not touched in any way. Managing the pointer is
520 * the user's responsibilty.
524 * @brief %List assignment operator.
525 * @param x A %list of identical element and allocator types.
527 * All the elements of @a x are copied, but unlike the copy
528 * constructor, the allocator object is not copied.
530 list&
531 operator=(const list& __x);
534 * @brief Assigns a given value to a %list.
535 * @param n Number of elements to be assigned.
536 * @param val Value to be assigned.
538 * This function fills a %list with @a n copies of the given
539 * value. Note that the assignment completely changes the %list
540 * and that the resulting %list's size is the same as the number
541 * of elements assigned. Old data may be lost.
543 void
544 assign(size_type __n, const value_type& __val)
545 { _M_fill_assign(__n, __val); }
548 * @brief Assigns a range to a %list.
549 * @param first An input iterator.
550 * @param last An input iterator.
552 * This function fills a %list with copies of the elements in the
553 * range [@a first,@a last).
555 * Note that the assignment completely changes the %list and
556 * that the resulting %list's size is the same as the number of
557 * elements assigned. Old data may be lost.
559 template<typename _InputIterator>
560 void
561 assign(_InputIterator __first, _InputIterator __last)
563 // Check whether it's an integral type. If so, it's not an iterator.
564 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
565 _M_assign_dispatch(__first, __last, _Integral());
568 /// Get a copy of the memory allocation object.
569 allocator_type
570 get_allocator() const
571 { return _Base::get_allocator(); }
573 // iterators
575 * Returns a read/write iterator that points to the first element in the
576 * %list. Iteration is done in ordinary element order.
578 iterator
579 begin()
580 { return this->_M_impl._M_node._M_next; }
583 * Returns a read-only (constant) iterator that points to the
584 * first element in the %list. Iteration is done in ordinary
585 * element order.
587 const_iterator
588 begin() const
589 { return this->_M_impl._M_node._M_next; }
592 * Returns a read/write iterator that points one past the last
593 * element in the %list. Iteration is done in ordinary element
594 * order.
596 iterator
597 end() { return &this->_M_impl._M_node; }
600 * Returns a read-only (constant) iterator that points one past
601 * the last element in the %list. Iteration is done in ordinary
602 * element order.
604 const_iterator
605 end() const
606 { return &this->_M_impl._M_node; }
609 * Returns a read/write reverse iterator that points to the last
610 * element in the %list. Iteration is done in reverse element
611 * order.
613 reverse_iterator
614 rbegin()
615 { return reverse_iterator(end()); }
618 * Returns a read-only (constant) reverse iterator that points to
619 * the last element in the %list. Iteration is done in reverse
620 * element order.
622 const_reverse_iterator
623 rbegin() const
624 { return const_reverse_iterator(end()); }
627 * Returns a read/write reverse iterator that points to one
628 * before the first element in the %list. Iteration is done in
629 * reverse element order.
631 reverse_iterator
632 rend()
633 { return reverse_iterator(begin()); }
636 * Returns a read-only (constant) reverse iterator that points to one
637 * before the first element in the %list. Iteration is done in reverse
638 * element order.
640 const_reverse_iterator
641 rend() const
642 { return const_reverse_iterator(begin()); }
644 // [23.2.2.2] capacity
646 * Returns true if the %list is empty. (Thus begin() would equal
647 * end().)
649 bool
650 empty() const
651 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
653 /** Returns the number of elements in the %list. */
654 size_type
655 size() const
656 { return std::distance(begin(), end()); }
658 /** Returns the size() of the largest possible %list. */
659 size_type
660 max_size() const
661 { return size_type(-1); }
664 * @brief Resizes the %list to the specified number of elements.
665 * @param new_size Number of elements the %list should contain.
666 * @param x Data with which new elements should be populated.
668 * This function will %resize the %list to the specified number
669 * of elements. If the number is smaller than the %list's
670 * current size the %list is truncated, otherwise the %list is
671 * extended and new elements are populated with given data.
673 void
674 resize(size_type __new_size, const value_type& __x);
677 * @brief Resizes the %list to the specified number of elements.
678 * @param new_size Number of elements the %list should contain.
680 * This function will resize the %list to the specified number of
681 * elements. If the number is smaller than the %list's current
682 * size the %list is truncated, otherwise the %list is extended
683 * and new elements are default-constructed.
685 void
686 resize(size_type __new_size)
687 { this->resize(__new_size, value_type()); }
689 // element access
691 * Returns a read/write reference to the data at the first
692 * element of the %list.
694 reference
695 front()
696 { return *begin(); }
699 * Returns a read-only (constant) reference to the data at the first
700 * element of the %list.
702 const_reference
703 front() const
704 { return *begin(); }
707 * Returns a read/write reference to the data at the last element
708 * of the %list.
710 reference
711 back()
713 iterator __tmp = end();
714 --__tmp;
715 return *__tmp;
719 * Returns a read-only (constant) reference to the data at the last
720 * element of the %list.
722 const_reference
723 back() const
725 const_iterator __tmp = end();
726 --__tmp;
727 return *__tmp;
730 // [23.2.2.3] modifiers
732 * @brief Add data to the front of the %list.
733 * @param x Data to be added.
735 * This is a typical stack operation. The function creates an
736 * element at the front of the %list and assigns the given data
737 * to it. Due to the nature of a %list this operation can be
738 * done in constant time, and does not invalidate iterators and
739 * references.
741 void
742 push_front(const value_type& __x)
743 { this->_M_insert(begin(), __x); }
746 * @brief Removes first element.
748 * This is a typical stack operation. It shrinks the %list by
749 * one. Due to the nature of a %list this operation can be done
750 * in constant time, and only invalidates iterators/references to
751 * the element being removed.
753 * Note that no data is returned, and if the first element's data
754 * is needed, it should be retrieved before pop_front() is
755 * called.
757 void
758 pop_front()
759 { this->_M_erase(begin()); }
762 * @brief Add data to the end of the %list.
763 * @param x Data to be added.
765 * This is a typical stack operation. The function creates an
766 * element at the end of the %list and assigns the given data to
767 * it. Due to the nature of a %list this operation can be done
768 * in constant time, and does not invalidate iterators and
769 * references.
771 void
772 push_back(const value_type& __x)
773 { this->_M_insert(end(), __x); }
776 * @brief Removes last element.
778 * This is a typical stack operation. It shrinks the %list by
779 * one. Due to the nature of a %list this operation can be done
780 * in constant time, and only invalidates iterators/references to
781 * the element being removed.
783 * Note that no data is returned, and if the last element's data
784 * is needed, it should be retrieved before pop_back() is called.
786 void
787 pop_back()
788 { this->_M_erase(this->_M_impl._M_node._M_prev); }
791 * @brief Inserts given value into %list before specified iterator.
792 * @param position An iterator into the %list.
793 * @param x Data to be inserted.
794 * @return An iterator that points to the inserted data.
796 * This function will insert a copy of the given value before
797 * the specified location. Due to the nature of a %list this
798 * operation can be done in constant time, and does not
799 * invalidate iterators and references.
801 iterator
802 insert(iterator __position, const value_type& __x);
805 * @brief Inserts a number of copies of given data into the %list.
806 * @param position An iterator into the %list.
807 * @param n Number of elements to be inserted.
808 * @param x Data to be inserted.
810 * This function will insert a specified number of copies of the
811 * given data before the location specified by @a position.
813 * Due to the nature of a %list this operation can be done in
814 * constant time, and does not invalidate iterators and
815 * references.
817 void
818 insert(iterator __position, size_type __n, const value_type& __x)
819 { _M_fill_insert(__position, __n, __x); }
822 * @brief Inserts a range into the %list.
823 * @param position An iterator into the %list.
824 * @param first An input iterator.
825 * @param last An input iterator.
827 * This function will insert copies of the data in the range [@a
828 * first,@a last) into the %list before the location specified by
829 * @a position.
831 * Due to the nature of a %list this operation can be done in
832 * constant time, and does not invalidate iterators and
833 * references.
835 template<typename _InputIterator>
836 void
837 insert(iterator __position, _InputIterator __first,
838 _InputIterator __last)
840 // Check whether it's an integral type. If so, it's not an iterator.
841 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
842 _M_insert_dispatch(__position, __first, __last, _Integral());
846 * @brief Remove element at given position.
847 * @param position Iterator pointing to element to be erased.
848 * @return An iterator pointing to the next element (or end()).
850 * This function will erase the element at the given position and thus
851 * shorten the %list by one.
853 * Due to the nature of a %list this operation can be done in
854 * constant time, and only invalidates iterators/references to
855 * the element being removed. The user is also cautioned that
856 * this function only erases the element, and that if the element
857 * is itself a pointer, the pointed-to memory is not touched in
858 * any way. Managing the pointer is the user's responsibilty.
860 iterator
861 erase(iterator __position);
864 * @brief Remove a range of elements.
865 * @param first Iterator pointing to the first element to be erased.
866 * @param last Iterator pointing to one past the last element to be
867 * erased.
868 * @return An iterator pointing to the element pointed to by @a last
869 * prior to erasing (or end()).
871 * This function will erase the elements in the range @a
872 * [first,last) and shorten the %list accordingly.
874 * Due to the nature of a %list this operation can be done in
875 * constant time, and only invalidates iterators/references to
876 * the element being removed. The user is also cautioned that
877 * this function only erases the elements, and that if the
878 * elements themselves are pointers, the pointed-to memory is not
879 * touched in any way. Managing the pointer is the user's
880 * responsibilty.
882 iterator
883 erase(iterator __first, iterator __last)
885 while (__first != __last)
886 __first = erase(__first);
887 return __last;
891 * @brief Swaps data with another %list.
892 * @param x A %list of the same element and allocator types.
894 * This exchanges the elements between two lists in constant
895 * time. Note that the global std::swap() function is
896 * specialized such that std::swap(l1,l2) will feed to this
897 * function.
899 void
900 swap(list& __x)
901 { _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node); }
904 * Erases all the elements. Note that this function only erases
905 * the elements, and that if the elements themselves are
906 * pointers, the pointed-to memory is not touched in any way.
907 * Managing the pointer is the user's responsibilty.
909 void
910 clear()
912 _Base::_M_clear();
913 _Base::_M_init();
916 // [23.2.2.4] list operations
918 * @brief Insert contents of another %list.
919 * @param position Iterator referencing the element to insert before.
920 * @param x Source list.
922 * The elements of @a x are inserted in constant time in front of
923 * the element referenced by @a position. @a x becomes an empty
924 * list.
926 void
927 splice(iterator __position, list& __x)
929 if (!__x.empty())
930 this->_M_transfer(__position, __x.begin(), __x.end());
934 * @brief Insert element from another %list.
935 * @param position Iterator referencing the element to insert before.
936 * @param x Source list.
937 * @param i Iterator referencing the element to move.
939 * Removes the element in list @a x referenced by @a i and
940 * inserts it into the current list before @a position.
942 void
943 splice(iterator __position, list&, iterator __i)
945 iterator __j = __i;
946 ++__j;
947 if (__position == __i || __position == __j)
948 return;
949 this->_M_transfer(__position, __i, __j);
953 * @brief Insert range from another %list.
954 * @param position Iterator referencing the element to insert before.
955 * @param x Source list.
956 * @param first Iterator referencing the start of range in x.
957 * @param last Iterator referencing the end of range in x.
959 * Removes elements in the range [first,last) and inserts them
960 * before @a position in constant time.
962 * Undefined if @a position is in [first,last).
964 void
965 splice(iterator __position, list&, iterator __first, iterator __last)
967 if (__first != __last)
968 this->_M_transfer(__position, __first, __last);
972 * @brief Remove all elements equal to value.
973 * @param value The value to remove.
975 * Removes every element in the list equal to @a value.
976 * Remaining elements stay in list order. Note that this
977 * function only erases the elements, and that if the elements
978 * themselves are pointers, the pointed-to memory is not
979 * touched in any way. Managing the pointer is the user's
980 * responsibilty.
982 void
983 remove(const _Tp& __value);
986 * @brief Remove all elements satisfying a predicate.
987 * @param Predicate Unary predicate function or object.
989 * Removes every element in the list for which the predicate
990 * returns true. Remaining elements stay in list order. Note
991 * that this function only erases the elements, and that if the
992 * elements themselves are pointers, the pointed-to memory is
993 * not touched in any way. Managing the pointer is the user's
994 * responsibilty.
996 template<typename _Predicate>
997 void
998 remove_if(_Predicate);
1001 * @brief Remove consecutive duplicate elements.
1003 * For each consecutive set of elements with the same value,
1004 * remove all but the first one. Remaining elements stay in
1005 * list order. Note that this function only erases the
1006 * elements, and that if the elements themselves are pointers,
1007 * the pointed-to memory is not touched in any way. Managing
1008 * the pointer is the user's responsibilty.
1010 void
1011 unique();
1014 * @brief Remove consecutive elements satisfying a predicate.
1015 * @param BinaryPredicate Binary predicate function or object.
1017 * For each consecutive set of elements [first,last) that
1018 * satisfy predicate(first,i) where i is an iterator in
1019 * [first,last), remove all but the first one. Remaining
1020 * elements stay in list order. Note that this function only
1021 * erases the elements, and that if the elements themselves are
1022 * pointers, the pointed-to memory is not touched in any way.
1023 * Managing the pointer is the user's responsibilty.
1025 template<typename _BinaryPredicate>
1026 void
1027 unique(_BinaryPredicate);
1030 * @brief Merge sorted lists.
1031 * @param x Sorted list to merge.
1033 * Assumes that both @a x and this list are sorted according to
1034 * operator<(). Merges elements of @a x into this list in
1035 * sorted order, leaving @a x empty when complete. Elements in
1036 * this list precede elements in @a x that are equal.
1038 void
1039 merge(list& __x);
1042 * @brief Merge sorted lists according to comparison function.
1043 * @param x Sorted list to merge.
1044 * @param StrictWeakOrdering Comparison function definining
1045 * sort order.
1047 * Assumes that both @a x and this list are sorted according to
1048 * StrictWeakOrdering. Merges elements of @a x into this list
1049 * in sorted order, leaving @a x empty when complete. Elements
1050 * in this list precede elements in @a x that are equivalent
1051 * according to StrictWeakOrdering().
1053 template<typename _StrictWeakOrdering>
1054 void
1055 merge(list&, _StrictWeakOrdering);
1058 * @brief Reverse the elements in list.
1060 * Reverse the order of elements in the list in linear time.
1062 void
1063 reverse()
1064 { this->_M_impl._M_node.reverse(); }
1067 * @brief Sort the elements.
1069 * Sorts the elements of this list in NlogN time. Equivalent
1070 * elements remain in list order.
1072 void
1073 sort();
1076 * @brief Sort the elements according to comparison function.
1078 * Sorts the elements of this list in NlogN time. Equivalent
1079 * elements remain in list order.
1081 template<typename _StrictWeakOrdering>
1082 void
1083 sort(_StrictWeakOrdering);
1085 protected:
1086 // Internal assign functions follow.
1088 // Called by the range assign to implement [23.1.1]/9
1089 template<typename _Integer>
1090 void
1091 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1093 _M_fill_assign(static_cast<size_type>(__n),
1094 static_cast<value_type>(__val));
1097 // Called by the range assign to implement [23.1.1]/9
1098 template<typename _InputIterator>
1099 void
1100 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1101 __false_type);
1103 // Called by assign(n,t), and the range assign when it turns out
1104 // to be the same thing.
1105 void
1106 _M_fill_assign(size_type __n, const value_type& __val);
1109 // Internal insert functions follow.
1111 // Called by the range insert to implement [23.1.1]/9
1112 template<typename _Integer>
1113 void
1114 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1115 __true_type)
1117 _M_fill_insert(__pos, static_cast<size_type>(__n),
1118 static_cast<value_type>(__x));
1121 // Called by the range insert to implement [23.1.1]/9
1122 template<typename _InputIterator>
1123 void
1124 _M_insert_dispatch(iterator __pos,
1125 _InputIterator __first, _InputIterator __last,
1126 __false_type)
1128 for (; __first != __last; ++__first)
1129 _M_insert(__pos, *__first);
1132 // Called by insert(p,n,x), and the range insert when it turns out
1133 // to be the same thing.
1134 void
1135 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
1137 for (; __n > 0; --__n)
1138 _M_insert(__pos, __x);
1142 // Moves the elements from [first,last) before position.
1143 void
1144 _M_transfer(iterator __position, iterator __first, iterator __last)
1145 { __position._M_node->transfer(__first._M_node, __last._M_node); }
1147 // Inserts new element at position given and with value given.
1148 void
1149 _M_insert(iterator __position, const value_type& __x)
1151 _Node* __tmp = _M_create_node(__x);
1152 __tmp->hook(__position._M_node);
1155 // Erases element at position given.
1156 void
1157 _M_erase(iterator __position)
1159 __position._M_node->unhook();
1160 _Node* __n = static_cast<_Node*>(__position._M_node);
1161 this->get_allocator().destroy(&__n->_M_data);
1162 _M_put_node(__n);
1167 * @brief List equality comparison.
1168 * @param x A %list.
1169 * @param y A %list of the same type as @a x.
1170 * @return True iff the size and elements of the lists are equal.
1172 * This is an equivalence relation. It is linear in the size of
1173 * the lists. Lists are considered equivalent if their sizes are
1174 * equal, and if corresponding elements compare equal.
1176 template<typename _Tp, typename _Alloc>
1177 inline bool
1178 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1180 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1181 const_iterator __end1 = __x.end();
1182 const_iterator __end2 = __y.end();
1184 const_iterator __i1 = __x.begin();
1185 const_iterator __i2 = __y.begin();
1186 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1188 ++__i1;
1189 ++__i2;
1191 return __i1 == __end1 && __i2 == __end2;
1195 * @brief List ordering relation.
1196 * @param x A %list.
1197 * @param y A %list of the same type as @a x.
1198 * @return True iff @a x is lexicographically less than @a y.
1200 * This is a total ordering relation. It is linear in the size of the
1201 * lists. The elements must be comparable with @c <.
1203 * See std::lexicographical_compare() for how the determination is made.
1205 template<typename _Tp, typename _Alloc>
1206 inline bool
1207 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1208 { return std::lexicographical_compare(__x.begin(), __x.end(),
1209 __y.begin(), __y.end()); }
1211 /// Based on operator==
1212 template<typename _Tp, typename _Alloc>
1213 inline bool
1214 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1215 { return !(__x == __y); }
1217 /// Based on operator<
1218 template<typename _Tp, typename _Alloc>
1219 inline bool
1220 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1221 { return __y < __x; }
1223 /// Based on operator<
1224 template<typename _Tp, typename _Alloc>
1225 inline bool
1226 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1227 { return !(__y < __x); }
1229 /// Based on operator<
1230 template<typename _Tp, typename _Alloc>
1231 inline bool
1232 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1233 { return !(__x < __y); }
1235 /// See std::list::swap().
1236 template<typename _Tp, typename _Alloc>
1237 inline void
1238 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1239 { __x.swap(__y); }
1240 } // namespace std
1242 #endif /* _LIST_H */