Mark as release
[official-gcc.git] / libstdc++-v3 / include / bits / stl_list.h
blobe37e5ee36f3b1fe38d4bd637c6312ab6711d5f8a
1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
57 /** @file stl_list.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
62 #ifndef _LIST_H
63 #define _LIST_H 1
65 #include <bits/concept_check.h>
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
69 // Supporting structures are split into common and templated types; the
70 // latter publicly inherits from the former in an effort to reduce code
71 // duplication. This results in some "needless" static_cast'ing later on,
72 // but it's all safe downcasting.
74 /// @if maint Common part of a node in the %list. @endif
75 struct _List_node_base
77 _List_node_base* _M_next; ///< Self-explanatory
78 _List_node_base* _M_prev; ///< Self-explanatory
80 static void
81 swap(_List_node_base& __x, _List_node_base& __y);
83 void
84 transfer(_List_node_base * const __first,
85 _List_node_base * const __last);
87 void
88 reverse();
90 void
91 hook(_List_node_base * const __position);
93 void
94 unhook();
97 /// @if maint An actual node in the %list. @endif
98 template<typename _Tp>
99 struct _List_node : public _List_node_base
101 _Tp _M_data; ///< User's data.
105 * @brief A list::iterator.
107 * @if maint
108 * All the functions are op overloads.
109 * @endif
111 template<typename _Tp>
112 struct _List_iterator
114 typedef _List_iterator<_Tp> _Self;
115 typedef _List_node<_Tp> _Node;
117 typedef ptrdiff_t difference_type;
118 typedef std::bidirectional_iterator_tag iterator_category;
119 typedef _Tp value_type;
120 typedef _Tp* pointer;
121 typedef _Tp& reference;
123 _List_iterator()
124 : _M_node() { }
126 explicit
127 _List_iterator(_List_node_base* __x)
128 : _M_node(__x) { }
130 // Must downcast from List_node_base to _List_node to get to _M_data.
131 reference
132 operator*() const
133 { return static_cast<_Node*>(_M_node)->_M_data; }
135 pointer
136 operator->() const
137 { return &static_cast<_Node*>(_M_node)->_M_data; }
139 _Self&
140 operator++()
142 _M_node = _M_node->_M_next;
143 return *this;
146 _Self
147 operator++(int)
149 _Self __tmp = *this;
150 _M_node = _M_node->_M_next;
151 return __tmp;
154 _Self&
155 operator--()
157 _M_node = _M_node->_M_prev;
158 return *this;
161 _Self
162 operator--(int)
164 _Self __tmp = *this;
165 _M_node = _M_node->_M_prev;
166 return __tmp;
169 bool
170 operator==(const _Self& __x) const
171 { return _M_node == __x._M_node; }
173 bool
174 operator!=(const _Self& __x) const
175 { return _M_node != __x._M_node; }
177 // The only member points to the %list element.
178 _List_node_base* _M_node;
182 * @brief A list::const_iterator.
184 * @if maint
185 * All the functions are op overloads.
186 * @endif
188 template<typename _Tp>
189 struct _List_const_iterator
191 typedef _List_const_iterator<_Tp> _Self;
192 typedef const _List_node<_Tp> _Node;
193 typedef _List_iterator<_Tp> iterator;
195 typedef ptrdiff_t difference_type;
196 typedef std::bidirectional_iterator_tag iterator_category;
197 typedef _Tp value_type;
198 typedef const _Tp* pointer;
199 typedef const _Tp& reference;
201 _List_const_iterator()
202 : _M_node() { }
204 explicit
205 _List_const_iterator(const _List_node_base* __x)
206 : _M_node(__x) { }
208 _List_const_iterator(const iterator& __x)
209 : _M_node(__x._M_node) { }
211 // Must downcast from List_node_base to _List_node to get to
212 // _M_data.
213 reference
214 operator*() const
215 { return static_cast<_Node*>(_M_node)->_M_data; }
217 pointer
218 operator->() const
219 { return &static_cast<_Node*>(_M_node)->_M_data; }
221 _Self&
222 operator++()
224 _M_node = _M_node->_M_next;
225 return *this;
228 _Self
229 operator++(int)
231 _Self __tmp = *this;
232 _M_node = _M_node->_M_next;
233 return __tmp;
236 _Self&
237 operator--()
239 _M_node = _M_node->_M_prev;
240 return *this;
243 _Self
244 operator--(int)
246 _Self __tmp = *this;
247 _M_node = _M_node->_M_prev;
248 return __tmp;
251 bool
252 operator==(const _Self& __x) const
253 { return _M_node == __x._M_node; }
255 bool
256 operator!=(const _Self& __x) const
257 { return _M_node != __x._M_node; }
259 // The only member points to the %list element.
260 const _List_node_base* _M_node;
263 template<typename _Val>
264 inline bool
265 operator==(const _List_iterator<_Val>& __x,
266 const _List_const_iterator<_Val>& __y)
267 { return __x._M_node == __y._M_node; }
269 template<typename _Val>
270 inline bool
271 operator!=(const _List_iterator<_Val>& __x,
272 const _List_const_iterator<_Val>& __y)
273 { return __x._M_node != __y._M_node; }
277 * @if maint
278 * See bits/stl_deque.h's _Deque_base for an explanation.
279 * @endif
281 template<typename _Tp, typename _Alloc>
282 class _List_base
284 protected:
285 // NOTA BENE
286 // The stored instance is not actually of "allocator_type"'s
287 // type. Instead we rebind the type to
288 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
289 // should probably be the same. List_node<Tp> is not the same
290 // size as Tp (it's two pointers larger), and specializations on
291 // Tp may go unused because List_node<Tp> is being bound
292 // instead.
294 // We put this to the test in the constructors and in
295 // get_allocator, where we use conversions between
296 // allocator_type and _Node_alloc_type. The conversion is
297 // required by table 32 in [20.1.5].
298 typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
299 _Node_alloc_type;
301 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
303 struct _List_impl
304 : public _Node_alloc_type
306 _List_node_base _M_node;
308 _List_impl(const _Node_alloc_type& __a)
309 : _Node_alloc_type(__a), _M_node()
313 _List_impl _M_impl;
315 _List_node<_Tp>*
316 _M_get_node()
317 { return _M_impl._Node_alloc_type::allocate(1); }
319 void
320 _M_put_node(_List_node<_Tp>* __p)
321 { _M_impl._Node_alloc_type::deallocate(__p, 1); }
323 public:
324 typedef _Alloc allocator_type;
326 _Node_alloc_type&
327 _M_get_Node_allocator()
328 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
330 const _Node_alloc_type&
331 _M_get_Node_allocator() const
332 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
334 _Tp_alloc_type
335 _M_get_Tp_allocator() const
336 { return _Tp_alloc_type(_M_get_Node_allocator()); }
338 allocator_type
339 get_allocator() const
340 { return allocator_type(_M_get_Node_allocator()); }
342 _List_base(const allocator_type& __a)
343 : _M_impl(__a)
344 { _M_init(); }
346 // This is what actually destroys the list.
347 ~_List_base()
348 { _M_clear(); }
350 void
351 _M_clear();
353 void
354 _M_init()
356 this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
357 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
362 * @brief A standard container with linear time access to elements,
363 * and fixed time insertion/deletion at any point in the sequence.
365 * @ingroup Containers
366 * @ingroup Sequences
368 * Meets the requirements of a <a href="tables.html#65">container</a>, a
369 * <a href="tables.html#66">reversible container</a>, and a
370 * <a href="tables.html#67">sequence</a>, including the
371 * <a href="tables.html#68">optional sequence requirements</a> with the
372 * %exception of @c at and @c operator[].
374 * This is a @e doubly @e linked %list. Traversal up and down the
375 * %list requires linear time, but adding and removing elements (or
376 * @e nodes) is done in constant time, regardless of where the
377 * change takes place. Unlike std::vector and std::deque,
378 * random-access iterators are not provided, so subscripting ( @c
379 * [] ) access is not allowed. For algorithms which only need
380 * sequential access, this lack makes no difference.
382 * Also unlike the other standard containers, std::list provides
383 * specialized algorithms %unique to linked lists, such as
384 * splicing, sorting, and in-place reversal.
386 * @if maint
387 * A couple points on memory allocation for list<Tp>:
389 * First, we never actually allocate a Tp, we allocate
390 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
391 * that after elements from %list<X,Alloc1> are spliced into
392 * %list<X,Alloc2>, destroying the memory of the second %list is a
393 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
395 * Second, a %list conceptually represented as
396 * @code
397 * A <---> B <---> C <---> D
398 * @endcode
399 * is actually circular; a link exists between A and D. The %list
400 * class holds (as its only data member) a private list::iterator
401 * pointing to @e D, not to @e A! To get to the head of the %list,
402 * we start at the tail and move forward by one. When this member
403 * iterator's next/previous pointers refer to itself, the %list is
404 * %empty. @endif
406 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
407 class list : protected _List_base<_Tp, _Alloc>
409 // concept requirements
410 typedef typename _Alloc::value_type _Alloc_value_type;
411 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
412 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
414 typedef _List_base<_Tp, _Alloc> _Base;
415 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
417 public:
418 typedef _Tp value_type;
419 typedef typename _Tp_alloc_type::pointer pointer;
420 typedef typename _Tp_alloc_type::const_pointer const_pointer;
421 typedef typename _Tp_alloc_type::reference reference;
422 typedef typename _Tp_alloc_type::const_reference const_reference;
423 typedef _List_iterator<_Tp> iterator;
424 typedef _List_const_iterator<_Tp> const_iterator;
425 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
426 typedef std::reverse_iterator<iterator> reverse_iterator;
427 typedef size_t size_type;
428 typedef ptrdiff_t difference_type;
429 typedef _Alloc allocator_type;
431 protected:
432 // Note that pointers-to-_Node's can be ctor-converted to
433 // iterator types.
434 typedef _List_node<_Tp> _Node;
436 using _Base::_M_impl;
437 using _Base::_M_put_node;
438 using _Base::_M_get_node;
439 using _Base::_M_get_Tp_allocator;
440 using _Base::_M_get_Node_allocator;
443 * @if maint
444 * @param x An instance of user data.
446 * Allocates space for a new node and constructs a copy of @a x in it.
447 * @endif
449 _Node*
450 _M_create_node(const value_type& __x)
452 _Node* __p = this->_M_get_node();
455 _M_get_Tp_allocator().construct(&__p->_M_data, __x);
457 catch(...)
459 _M_put_node(__p);
460 __throw_exception_again;
462 return __p;
465 public:
466 // [23.2.2.1] construct/copy/destroy
467 // (assign() and get_allocator() are also listed in this section)
469 * @brief Default constructor creates no elements.
471 explicit
472 list(const allocator_type& __a = allocator_type())
473 : _Base(__a) { }
476 * @brief Create a %list with copies of an exemplar element.
477 * @param n The number of elements to initially create.
478 * @param value An element to copy.
480 * This constructor fills the %list with @a n copies of @a value.
482 explicit
483 list(size_type __n, const value_type& __value = value_type(),
484 const allocator_type& __a = allocator_type())
485 : _Base(__a)
486 { _M_fill_initialize(__n, __value); }
489 * @brief %List copy constructor.
490 * @param x A %list of identical element and allocator types.
492 * The newly-created %list uses a copy of the allocation object used
493 * by @a x.
495 list(const list& __x)
496 : _Base(__x._M_get_Node_allocator())
497 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
500 * @brief Builds a %list from a range.
501 * @param first An input iterator.
502 * @param last An input iterator.
504 * Create a %list consisting of copies of the elements from
505 * [@a first,@a last). This is linear in N (where N is
506 * distance(@a first,@a last)).
508 template<typename _InputIterator>
509 list(_InputIterator __first, _InputIterator __last,
510 const allocator_type& __a = allocator_type())
511 : _Base(__a)
513 // Check whether it's an integral type. If so, it's not an iterator.
514 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
515 _M_initialize_dispatch(__first, __last, _Integral());
519 * No explicit dtor needed as the _Base dtor takes care of
520 * things. The _Base dtor only erases the elements, and note
521 * that if the elements themselves are pointers, the pointed-to
522 * memory is not touched in any way. Managing the pointer is
523 * the user's responsibilty.
527 * @brief %List assignment operator.
528 * @param x A %list of identical element and allocator types.
530 * All the elements of @a x are copied, but unlike the copy
531 * constructor, the allocator object is not copied.
533 list&
534 operator=(const list& __x);
537 * @brief Assigns a given value to a %list.
538 * @param n Number of elements to be assigned.
539 * @param val Value to be assigned.
541 * This function fills a %list with @a n copies of the given
542 * value. Note that the assignment completely changes the %list
543 * and that the resulting %list's size is the same as the number
544 * of elements assigned. Old data may be lost.
546 void
547 assign(size_type __n, const value_type& __val)
548 { _M_fill_assign(__n, __val); }
551 * @brief Assigns a range to a %list.
552 * @param first An input iterator.
553 * @param last An input iterator.
555 * This function fills a %list with copies of the elements in the
556 * range [@a first,@a last).
558 * Note that the assignment completely changes the %list and
559 * that the resulting %list's size is the same as the number of
560 * elements assigned. Old data may be lost.
562 template<typename _InputIterator>
563 void
564 assign(_InputIterator __first, _InputIterator __last)
566 // Check whether it's an integral type. If so, it's not an iterator.
567 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
568 _M_assign_dispatch(__first, __last, _Integral());
571 /// Get a copy of the memory allocation object.
572 allocator_type
573 get_allocator() const
574 { return _Base::get_allocator(); }
576 // iterators
578 * Returns a read/write iterator that points to the first element in the
579 * %list. Iteration is done in ordinary element order.
581 iterator
582 begin()
583 { return iterator(this->_M_impl._M_node._M_next); }
586 * Returns a read-only (constant) iterator that points to the
587 * first element in the %list. Iteration is done in ordinary
588 * element order.
590 const_iterator
591 begin() const
592 { return const_iterator(this->_M_impl._M_node._M_next); }
595 * Returns a read/write iterator that points one past the last
596 * element in the %list. Iteration is done in ordinary element
597 * order.
599 iterator
600 end()
601 { return iterator(&this->_M_impl._M_node); }
604 * Returns a read-only (constant) iterator that points one past
605 * the last element in the %list. Iteration is done in ordinary
606 * element order.
608 const_iterator
609 end() const
610 { return const_iterator(&this->_M_impl._M_node); }
613 * Returns a read/write reverse iterator that points to the last
614 * element in the %list. Iteration is done in reverse element
615 * order.
617 reverse_iterator
618 rbegin()
619 { return reverse_iterator(end()); }
622 * Returns a read-only (constant) reverse iterator that points to
623 * the last element in the %list. Iteration is done in reverse
624 * element order.
626 const_reverse_iterator
627 rbegin() const
628 { return const_reverse_iterator(end()); }
631 * Returns a read/write reverse iterator that points to one
632 * before the first element in the %list. Iteration is done in
633 * reverse element order.
635 reverse_iterator
636 rend()
637 { return reverse_iterator(begin()); }
640 * Returns a read-only (constant) reverse iterator that points to one
641 * before the first element in the %list. Iteration is done in reverse
642 * element order.
644 const_reverse_iterator
645 rend() const
646 { return const_reverse_iterator(begin()); }
648 // [23.2.2.2] capacity
650 * Returns true if the %list is empty. (Thus begin() would equal
651 * end().)
653 bool
654 empty() const
655 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
657 /** Returns the number of elements in the %list. */
658 size_type
659 size() const
660 { return std::distance(begin(), end()); }
662 /** Returns the size() of the largest possible %list. */
663 size_type
664 max_size() const
665 { return _M_get_Tp_allocator().max_size(); }
668 * @brief Resizes the %list to the specified number of elements.
669 * @param new_size Number of elements the %list should contain.
670 * @param x Data with which new elements should be populated.
672 * This function will %resize the %list to the specified number
673 * of elements. If the number is smaller than the %list's
674 * current size the %list is truncated, otherwise the %list is
675 * extended and new elements are populated with given data.
677 void
678 resize(size_type __new_size, value_type __x = value_type());
680 // element access
682 * Returns a read/write reference to the data at the first
683 * element of the %list.
685 reference
686 front()
687 { return *begin(); }
690 * Returns a read-only (constant) reference to the data at the first
691 * element of the %list.
693 const_reference
694 front() const
695 { return *begin(); }
698 * Returns a read/write reference to the data at the last element
699 * of the %list.
701 reference
702 back()
704 iterator __tmp = end();
705 --__tmp;
706 return *__tmp;
710 * Returns a read-only (constant) reference to the data at the last
711 * element of the %list.
713 const_reference
714 back() const
716 const_iterator __tmp = end();
717 --__tmp;
718 return *__tmp;
721 // [23.2.2.3] modifiers
723 * @brief Add data to the front of the %list.
724 * @param x Data to be added.
726 * This is a typical stack operation. The function creates an
727 * element at the front of the %list and assigns the given data
728 * to it. Due to the nature of a %list this operation can be
729 * done in constant time, and does not invalidate iterators and
730 * references.
732 void
733 push_front(const value_type& __x)
734 { this->_M_insert(begin(), __x); }
737 * @brief Removes first element.
739 * This is a typical stack operation. It shrinks the %list by
740 * one. Due to the nature of a %list this operation can be done
741 * in constant time, and only invalidates iterators/references to
742 * the element being removed.
744 * Note that no data is returned, and if the first element's data
745 * is needed, it should be retrieved before pop_front() is
746 * called.
748 void
749 pop_front()
750 { this->_M_erase(begin()); }
753 * @brief Add data to the end of the %list.
754 * @param x Data to be added.
756 * This is a typical stack operation. The function creates an
757 * element at the end of the %list and assigns the given data to
758 * it. Due to the nature of a %list this operation can be done
759 * in constant time, and does not invalidate iterators and
760 * references.
762 void
763 push_back(const value_type& __x)
764 { this->_M_insert(end(), __x); }
767 * @brief Removes last element.
769 * This is a typical stack operation. It shrinks the %list by
770 * one. Due to the nature of a %list this operation can be done
771 * in constant time, and only invalidates iterators/references to
772 * the element being removed.
774 * Note that no data is returned, and if the last element's data
775 * is needed, it should be retrieved before pop_back() is called.
777 void
778 pop_back()
779 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
782 * @brief Inserts given value into %list before specified iterator.
783 * @param position An iterator into the %list.
784 * @param x Data to be inserted.
785 * @return An iterator that points to the inserted data.
787 * This function will insert a copy of the given value before
788 * the specified location. Due to the nature of a %list this
789 * operation can be done in constant time, and does not
790 * invalidate iterators and references.
792 iterator
793 insert(iterator __position, const value_type& __x);
796 * @brief Inserts a number of copies of given data into the %list.
797 * @param position An iterator into the %list.
798 * @param n Number of elements to be inserted.
799 * @param x Data to be inserted.
801 * This function will insert a specified number of copies of the
802 * given data before the location specified by @a position.
804 * This operation is linear in the number of elements inserted and
805 * does not invalidate iterators and references.
807 void
808 insert(iterator __position, size_type __n, const value_type& __x)
810 list __tmp(__n, __x, _M_get_Node_allocator());
811 splice(__position, __tmp);
815 * @brief Inserts a range into the %list.
816 * @param position An iterator into the %list.
817 * @param first An input iterator.
818 * @param last An input iterator.
820 * This function will insert copies of the data in the range [@a
821 * first,@a last) into the %list before the location specified by
822 * @a position.
824 * This operation is linear in the number of elements inserted and
825 * does not invalidate iterators and references.
827 template<typename _InputIterator>
828 void
829 insert(iterator __position, _InputIterator __first,
830 _InputIterator __last)
832 list __tmp(__first, __last, _M_get_Node_allocator());
833 splice(__position, __tmp);
837 * @brief Remove element at given position.
838 * @param position Iterator pointing to element to be erased.
839 * @return An iterator pointing to the next element (or end()).
841 * This function will erase the element at the given position and thus
842 * shorten the %list by one.
844 * Due to the nature of a %list this operation can be done in
845 * constant time, and only invalidates iterators/references to
846 * the element being removed. The user is also cautioned that
847 * this function only erases the element, and that if the element
848 * is itself a pointer, the pointed-to memory is not touched in
849 * any way. Managing the pointer is the user's responsibilty.
851 iterator
852 erase(iterator __position);
855 * @brief Remove a range of elements.
856 * @param first Iterator pointing to the first element to be erased.
857 * @param last Iterator pointing to one past the last element to be
858 * erased.
859 * @return An iterator pointing to the element pointed to by @a last
860 * prior to erasing (or end()).
862 * This function will erase the elements in the range @a
863 * [first,last) and shorten the %list accordingly.
865 * This operation is linear time in the size of the range and only
866 * invalidates iterators/references to the element being removed.
867 * The user is also cautioned that this function only erases the
868 * elements, and that if the elements themselves are pointers, the
869 * pointed-to memory is not touched in any way. Managing the pointer
870 * is the user's responsibilty.
872 iterator
873 erase(iterator __first, iterator __last)
875 while (__first != __last)
876 __first = erase(__first);
877 return __last;
881 * @brief Swaps data with another %list.
882 * @param x A %list of the same element and allocator types.
884 * This exchanges the elements between two lists in constant
885 * time. Note that the global std::swap() function is
886 * specialized such that std::swap(l1,l2) will feed to this
887 * function.
889 void
890 swap(list& __x)
892 _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node);
894 // _GLIBCXX_RESOLVE_LIB_DEFECTS
895 // 431. Swapping containers with unequal allocators.
896 std::__alloc_swap<typename _Base::_Node_alloc_type>::
897 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator());
901 * Erases all the elements. Note that this function only erases
902 * the elements, and that if the elements themselves are
903 * pointers, the pointed-to memory is not touched in any way.
904 * Managing the pointer is the user's responsibilty.
906 void
907 clear()
909 _Base::_M_clear();
910 _Base::_M_init();
913 // [23.2.2.4] list operations
915 * @brief Insert contents of another %list.
916 * @param position Iterator referencing the element to insert before.
917 * @param x Source list.
919 * The elements of @a x are inserted in constant time in front of
920 * the element referenced by @a position. @a x becomes an empty
921 * list.
923 * Requires this != @a x.
925 void
926 splice(iterator __position, list& __x)
928 if (!__x.empty())
930 _M_check_equal_allocators(__x);
932 this->_M_transfer(__position, __x.begin(), __x.end());
937 * @brief Insert element from another %list.
938 * @param position Iterator referencing the element to insert before.
939 * @param x Source list.
940 * @param i Iterator referencing the element to move.
942 * Removes the element in list @a x referenced by @a i and
943 * inserts it into the current list before @a position.
945 void
946 splice(iterator __position, list& __x, iterator __i)
948 iterator __j = __i;
949 ++__j;
950 if (__position == __i || __position == __j)
951 return;
953 if (this != &__x)
954 _M_check_equal_allocators(__x);
956 this->_M_transfer(__position, __i, __j);
960 * @brief Insert range from another %list.
961 * @param position Iterator referencing the element to insert before.
962 * @param x Source list.
963 * @param first Iterator referencing the start of range in x.
964 * @param last Iterator referencing the end of range in x.
966 * Removes elements in the range [first,last) and inserts them
967 * before @a position in constant time.
969 * Undefined if @a position is in [first,last).
971 void
972 splice(iterator __position, list& __x, iterator __first, iterator __last)
974 if (__first != __last)
976 if (this != &__x)
977 _M_check_equal_allocators(__x);
979 this->_M_transfer(__position, __first, __last);
984 * @brief Remove all elements equal to value.
985 * @param value The value to remove.
987 * Removes every element in the list equal to @a value.
988 * Remaining elements stay in list order. Note that this
989 * function only erases the elements, and that if the elements
990 * themselves are pointers, the pointed-to memory is not
991 * touched in any way. Managing the pointer is the user's
992 * responsibilty.
994 void
995 remove(const _Tp& __value);
998 * @brief Remove all elements satisfying a predicate.
999 * @param Predicate Unary predicate function or object.
1001 * Removes every element in the list for which the predicate
1002 * returns true. Remaining elements stay in list order. Note
1003 * that this function only erases the elements, and that if the
1004 * elements themselves are pointers, the pointed-to memory is
1005 * not touched in any way. Managing the pointer is the user's
1006 * responsibilty.
1008 template<typename _Predicate>
1009 void
1010 remove_if(_Predicate);
1013 * @brief Remove consecutive duplicate elements.
1015 * For each consecutive set of elements with the same value,
1016 * remove all but the first one. Remaining elements stay in
1017 * list order. Note that this function only erases the
1018 * elements, and that if the elements themselves are pointers,
1019 * the pointed-to memory is not touched in any way. Managing
1020 * the pointer is the user's responsibilty.
1022 void
1023 unique();
1026 * @brief Remove consecutive elements satisfying a predicate.
1027 * @param BinaryPredicate Binary predicate function or object.
1029 * For each consecutive set of elements [first,last) that
1030 * satisfy predicate(first,i) where i is an iterator in
1031 * [first,last), remove all but the first one. Remaining
1032 * elements stay in list order. Note that this function only
1033 * erases the elements, and that if the elements themselves are
1034 * pointers, the pointed-to memory is not touched in any way.
1035 * Managing the pointer is the user's responsibilty.
1037 template<typename _BinaryPredicate>
1038 void
1039 unique(_BinaryPredicate);
1042 * @brief Merge sorted lists.
1043 * @param x Sorted list to merge.
1045 * Assumes that both @a x and this list are sorted according to
1046 * operator<(). Merges elements of @a x into this list in
1047 * sorted order, leaving @a x empty when complete. Elements in
1048 * this list precede elements in @a x that are equal.
1050 void
1051 merge(list& __x);
1054 * @brief Merge sorted lists according to comparison function.
1055 * @param x Sorted list to merge.
1056 * @param StrictWeakOrdering Comparison function definining
1057 * sort order.
1059 * Assumes that both @a x and this list are sorted according to
1060 * StrictWeakOrdering. Merges elements of @a x into this list
1061 * in sorted order, leaving @a x empty when complete. Elements
1062 * in this list precede elements in @a x that are equivalent
1063 * according to StrictWeakOrdering().
1065 template<typename _StrictWeakOrdering>
1066 void
1067 merge(list&, _StrictWeakOrdering);
1070 * @brief Reverse the elements in list.
1072 * Reverse the order of elements in the list in linear time.
1074 void
1075 reverse()
1076 { this->_M_impl._M_node.reverse(); }
1079 * @brief Sort the elements.
1081 * Sorts the elements of this list in NlogN time. Equivalent
1082 * elements remain in list order.
1084 void
1085 sort();
1088 * @brief Sort the elements according to comparison function.
1090 * Sorts the elements of this list in NlogN time. Equivalent
1091 * elements remain in list order.
1093 template<typename _StrictWeakOrdering>
1094 void
1095 sort(_StrictWeakOrdering);
1097 protected:
1098 // Internal constructor functions follow.
1100 // Called by the range constructor to implement [23.1.1]/9
1101 template<typename _Integer>
1102 void
1103 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1105 _M_fill_initialize(static_cast<size_type>(__n),
1106 static_cast<value_type>(__x));
1109 // Called by the range constructor to implement [23.1.1]/9
1110 template<typename _InputIterator>
1111 void
1112 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1113 __false_type)
1115 for (; __first != __last; ++__first)
1116 push_back(*__first);
1119 // Called by list(n,v,a), and the range constructor when it turns out
1120 // to be the same thing.
1121 void
1122 _M_fill_initialize(size_type __n, const value_type& __x)
1124 for (; __n > 0; --__n)
1125 push_back(__x);
1129 // Internal assign functions follow.
1131 // Called by the range assign to implement [23.1.1]/9
1132 template<typename _Integer>
1133 void
1134 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1136 _M_fill_assign(static_cast<size_type>(__n),
1137 static_cast<value_type>(__val));
1140 // Called by the range assign to implement [23.1.1]/9
1141 template<typename _InputIterator>
1142 void
1143 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1144 __false_type);
1146 // Called by assign(n,t), and the range assign when it turns out
1147 // to be the same thing.
1148 void
1149 _M_fill_assign(size_type __n, const value_type& __val);
1152 // Moves the elements from [first,last) before position.
1153 void
1154 _M_transfer(iterator __position, iterator __first, iterator __last)
1155 { __position._M_node->transfer(__first._M_node, __last._M_node); }
1157 // Inserts new element at position given and with value given.
1158 void
1159 _M_insert(iterator __position, const value_type& __x)
1161 _Node* __tmp = _M_create_node(__x);
1162 __tmp->hook(__position._M_node);
1165 // Erases element at position given.
1166 void
1167 _M_erase(iterator __position)
1169 __position._M_node->unhook();
1170 _Node* __n = static_cast<_Node*>(__position._M_node);
1171 _M_get_Tp_allocator().destroy(&__n->_M_data);
1172 _M_put_node(__n);
1175 // To implement the splice (and merge) bits of N1599.
1176 void
1177 _M_check_equal_allocators(list& __x)
1179 if (_M_get_Node_allocator() != __x._M_get_Node_allocator())
1180 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1185 * @brief List equality comparison.
1186 * @param x A %list.
1187 * @param y A %list of the same type as @a x.
1188 * @return True iff the size and elements of the lists are equal.
1190 * This is an equivalence relation. It is linear in the size of
1191 * the lists. Lists are considered equivalent if their sizes are
1192 * equal, and if corresponding elements compare equal.
1194 template<typename _Tp, typename _Alloc>
1195 inline bool
1196 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1198 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1199 const_iterator __end1 = __x.end();
1200 const_iterator __end2 = __y.end();
1202 const_iterator __i1 = __x.begin();
1203 const_iterator __i2 = __y.begin();
1204 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1206 ++__i1;
1207 ++__i2;
1209 return __i1 == __end1 && __i2 == __end2;
1213 * @brief List ordering relation.
1214 * @param x A %list.
1215 * @param y A %list of the same type as @a x.
1216 * @return True iff @a x is lexicographically less than @a y.
1218 * This is a total ordering relation. It is linear in the size of the
1219 * lists. The elements must be comparable with @c <.
1221 * See std::lexicographical_compare() for how the determination is made.
1223 template<typename _Tp, typename _Alloc>
1224 inline bool
1225 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1226 { return std::lexicographical_compare(__x.begin(), __x.end(),
1227 __y.begin(), __y.end()); }
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 /// Based on operator<
1236 template<typename _Tp, typename _Alloc>
1237 inline bool
1238 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1239 { return __y < __x; }
1241 /// Based on operator<
1242 template<typename _Tp, typename _Alloc>
1243 inline bool
1244 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1245 { return !(__y < __x); }
1247 /// Based on operator<
1248 template<typename _Tp, typename _Alloc>
1249 inline bool
1250 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1251 { return !(__x < __y); }
1253 /// See std::list::swap().
1254 template<typename _Tp, typename _Alloc>
1255 inline void
1256 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1257 { __x.swap(__y); }
1259 _GLIBCXX_END_NESTED_NAMESPACE
1261 #endif /* _LIST_H */