Merge from trunk @ 138209
[official-gcc.git] / libstdc++-v3 / include / bits / stl_list.h
blobb38fa55cf794818b066e9293395d43eb0d953807
1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 _STL_LIST_H
63 #define _STL_LIST_H 1
65 #include <bits/concept_check.h>
66 #include <initializer_list>
68 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
70 // Supporting structures are split into common and templated types; the
71 // latter publicly inherits from the former in an effort to reduce code
72 // duplication. This results in some "needless" static_cast'ing later on,
73 // but it's all safe downcasting.
75 /// Common part of a node in the %list.
76 struct _List_node_base
78 _List_node_base* _M_next;
79 _List_node_base* _M_prev;
81 static void
82 swap(_List_node_base& __x, _List_node_base& __y);
84 void
85 transfer(_List_node_base * const __first,
86 _List_node_base * const __last);
88 void
89 reverse();
91 void
92 hook(_List_node_base * const __position);
94 void
95 unhook();
98 /// An actual node in the %list.
99 template<typename _Tp>
100 struct _List_node : public _List_node_base
102 ///< User's data.
103 _Tp _M_data;
107 * @brief A list::iterator.
109 * All the functions are op overloads.
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 * All the functions are op overloads.
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 std::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 explicit
203 _List_const_iterator(const _List_node_base* __x)
204 : _M_node(__x) { }
206 _List_const_iterator(const iterator& __x)
207 : _M_node(__x._M_node) { }
209 // Must downcast from List_node_base to _List_node to get to
210 // _M_data.
211 reference
212 operator*() const
213 { return static_cast<_Node*>(_M_node)->_M_data; }
215 pointer
216 operator->() const
217 { return &static_cast<_Node*>(_M_node)->_M_data; }
219 _Self&
220 operator++()
222 _M_node = _M_node->_M_next;
223 return *this;
226 _Self
227 operator++(int)
229 _Self __tmp = *this;
230 _M_node = _M_node->_M_next;
231 return __tmp;
234 _Self&
235 operator--()
237 _M_node = _M_node->_M_prev;
238 return *this;
241 _Self
242 operator--(int)
244 _Self __tmp = *this;
245 _M_node = _M_node->_M_prev;
246 return __tmp;
249 bool
250 operator==(const _Self& __x) const
251 { return _M_node == __x._M_node; }
253 bool
254 operator!=(const _Self& __x) const
255 { return _M_node != __x._M_node; }
257 // The only member points to the %list element.
258 const _List_node_base* _M_node;
261 template<typename _Val>
262 inline bool
263 operator==(const _List_iterator<_Val>& __x,
264 const _List_const_iterator<_Val>& __y)
265 { return __x._M_node == __y._M_node; }
267 template<typename _Val>
268 inline bool
269 operator!=(const _List_iterator<_Val>& __x,
270 const _List_const_iterator<_Val>& __y)
271 { return __x._M_node != __y._M_node; }
274 /// See bits/stl_deque.h's _Deque_base for an explanation.
275 template<typename _Tp, typename _Alloc>
276 class _List_base
278 protected:
279 // NOTA BENE
280 // The stored instance is not actually of "allocator_type"'s
281 // type. Instead we rebind the type to
282 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
283 // should probably be the same. List_node<Tp> is not the same
284 // size as Tp (it's two pointers larger), and specializations on
285 // Tp may go unused because List_node<Tp> is being bound
286 // instead.
288 // We put this to the test in the constructors and in
289 // get_allocator, where we use conversions between
290 // allocator_type and _Node_alloc_type. The conversion is
291 // required by table 32 in [20.1.5].
292 typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
293 _Node_alloc_type;
295 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
297 struct _List_impl
298 : public _Node_alloc_type
300 _List_node_base _M_node;
302 _List_impl()
303 : _Node_alloc_type(), _M_node()
306 _List_impl(const _Node_alloc_type& __a)
307 : _Node_alloc_type(__a), _M_node()
311 _List_impl _M_impl;
313 _List_node<_Tp>*
314 _M_get_node()
315 { return _M_impl._Node_alloc_type::allocate(1); }
317 void
318 _M_put_node(_List_node<_Tp>* __p)
319 { _M_impl._Node_alloc_type::deallocate(__p, 1); }
321 public:
322 typedef _Alloc allocator_type;
324 _Node_alloc_type&
325 _M_get_Node_allocator()
326 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
328 const _Node_alloc_type&
329 _M_get_Node_allocator() const
330 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
332 _Tp_alloc_type
333 _M_get_Tp_allocator() const
334 { return _Tp_alloc_type(_M_get_Node_allocator()); }
336 allocator_type
337 get_allocator() const
338 { return allocator_type(_M_get_Node_allocator()); }
340 _List_base()
341 : _M_impl()
342 { _M_init(); }
344 _List_base(const allocator_type& __a)
345 : _M_impl(__a)
346 { _M_init(); }
348 #ifdef __GXX_EXPERIMENTAL_CXX0X__
349 _List_base(_List_base&& __x)
350 : _M_impl(__x._M_get_Node_allocator())
352 _M_init();
353 _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node);
355 #endif
357 // This is what actually destroys the list.
358 ~_List_base()
359 { _M_clear(); }
361 void
362 _M_clear();
364 void
365 _M_init()
367 this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
368 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
373 * @brief A standard container with linear time access to elements,
374 * and fixed time insertion/deletion at any point in the sequence.
376 * @ingroup Containers
377 * @ingroup Sequences
379 * Meets the requirements of a <a href="tables.html#65">container</a>, a
380 * <a href="tables.html#66">reversible container</a>, and a
381 * <a href="tables.html#67">sequence</a>, including the
382 * <a href="tables.html#68">optional sequence requirements</a> with the
383 * %exception of @c at and @c operator[].
385 * This is a @e doubly @e linked %list. Traversal up and down the
386 * %list requires linear time, but adding and removing elements (or
387 * @e nodes) is done in constant time, regardless of where the
388 * change takes place. Unlike std::vector and std::deque,
389 * random-access iterators are not provided, so subscripting ( @c
390 * [] ) access is not allowed. For algorithms which only need
391 * sequential access, this lack makes no difference.
393 * Also unlike the other standard containers, std::list provides
394 * specialized algorithms %unique to linked lists, such as
395 * splicing, sorting, and in-place reversal.
397 * A couple points on memory allocation for list<Tp>:
399 * First, we never actually allocate a Tp, we allocate
400 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
401 * that after elements from %list<X,Alloc1> are spliced into
402 * %list<X,Alloc2>, destroying the memory of the second %list is a
403 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
405 * Second, a %list conceptually represented as
406 * @code
407 * A <---> B <---> C <---> D
408 * @endcode
409 * is actually circular; a link exists between A and D. The %list
410 * class holds (as its only data member) a private list::iterator
411 * pointing to @e D, not to @e A! To get to the head of the %list,
412 * we start at the tail and move forward by one. When this member
413 * iterator's next/previous pointers refer to itself, the %list is
414 * %empty.
416 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
417 class list : protected _List_base<_Tp, _Alloc>
419 // concept requirements
420 typedef typename _Alloc::value_type _Alloc_value_type;
421 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
422 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
424 typedef _List_base<_Tp, _Alloc> _Base;
425 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
427 public:
428 typedef _Tp value_type;
429 typedef typename _Tp_alloc_type::pointer pointer;
430 typedef typename _Tp_alloc_type::const_pointer const_pointer;
431 typedef typename _Tp_alloc_type::reference reference;
432 typedef typename _Tp_alloc_type::const_reference const_reference;
433 typedef _List_iterator<_Tp> iterator;
434 typedef _List_const_iterator<_Tp> const_iterator;
435 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
436 typedef std::reverse_iterator<iterator> reverse_iterator;
437 typedef size_t size_type;
438 typedef ptrdiff_t difference_type;
439 typedef _Alloc allocator_type;
441 protected:
442 // Note that pointers-to-_Node's can be ctor-converted to
443 // iterator types.
444 typedef _List_node<_Tp> _Node;
446 using _Base::_M_impl;
447 using _Base::_M_put_node;
448 using _Base::_M_get_node;
449 using _Base::_M_get_Tp_allocator;
450 using _Base::_M_get_Node_allocator;
453 * @param x An instance of user data.
455 * Allocates space for a new node and constructs a copy of @a x in it.
457 #ifndef __GXX_EXPERIMENTAL_CXX0X__
458 _Node*
459 _M_create_node(const value_type& __x)
461 _Node* __p = this->_M_get_node();
464 _M_get_Tp_allocator().construct(&__p->_M_data, __x);
466 catch(...)
468 _M_put_node(__p);
469 __throw_exception_again;
471 return __p;
473 #else
474 template<typename... _Args>
475 _Node*
476 _M_create_node(_Args&&... __args)
478 _Node* __p = this->_M_get_node();
481 _M_get_Tp_allocator().construct(&__p->_M_data,
482 std::forward<_Args>(__args)...);
484 catch(...)
486 _M_put_node(__p);
487 __throw_exception_again;
489 return __p;
491 #endif
493 public:
494 // [23.2.2.1] construct/copy/destroy
495 // (assign() and get_allocator() are also listed in this section)
497 * @brief Default constructor creates no elements.
499 list()
500 : _Base() { }
503 * @brief Creates a %list with no elements.
504 * @param a An allocator object.
506 explicit
507 list(const allocator_type& __a)
508 : _Base(__a) { }
511 * @brief Creates a %list with copies of an exemplar element.
512 * @param n The number of elements to initially create.
513 * @param value An element to copy.
514 * @param a An allocator object.
516 * This constructor fills the %list with @a n copies of @a value.
518 explicit
519 list(size_type __n, const value_type& __value = value_type(),
520 const allocator_type& __a = allocator_type())
521 : _Base(__a)
522 { _M_fill_initialize(__n, __value); }
525 * @brief %List copy constructor.
526 * @param x A %list of identical element and allocator types.
528 * The newly-created %list uses a copy of the allocation object used
529 * by @a x.
531 list(const list& __x)
532 : _Base(__x._M_get_Node_allocator())
533 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
535 #ifdef __GXX_EXPERIMENTAL_CXX0X__
537 * @brief %List move constructor.
538 * @param x A %list of identical element and allocator types.
540 * The newly-created %list contains the exact contents of @a x.
541 * The contents of @a x are a valid, but unspecified %list.
543 list(list&& __x)
544 : _Base(std::forward<_Base>(__x)) { }
547 * @brief Builds a %list from an initializer_list
548 * @param l An initializer_list of value_type.
549 * @param a An allocator object.
551 * Create a %list consisting of copies of the elements in the
552 * initializer_list @a l. This is linear in l.size().
554 list(initializer_list<value_type> __l,
555 const allocator_type& __a = allocator_type())
556 : _Base(__a)
557 { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
558 #endif
561 * @brief Builds a %list from a range.
562 * @param first An input iterator.
563 * @param last An input iterator.
564 * @param a An allocator object.
566 * Create a %list consisting of copies of the elements from
567 * [@a first,@a last). This is linear in N (where N is
568 * distance(@a first,@a last)).
570 template<typename _InputIterator>
571 list(_InputIterator __first, _InputIterator __last,
572 const allocator_type& __a = allocator_type())
573 : _Base(__a)
575 // Check whether it's an integral type. If so, it's not an iterator.
576 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
577 _M_initialize_dispatch(__first, __last, _Integral());
581 * No explicit dtor needed as the _Base dtor takes care of
582 * things. The _Base dtor only erases the elements, and note
583 * that if the elements themselves are pointers, the pointed-to
584 * memory is not touched in any way. Managing the pointer is
585 * the user's responsibility.
589 * @brief %List assignment operator.
590 * @param x A %list of identical element and allocator types.
592 * All the elements of @a x are copied, but unlike the copy
593 * constructor, the allocator object is not copied.
595 list&
596 operator=(const list& __x);
598 #ifdef __GXX_EXPERIMENTAL_CXX0X__
600 * @brief %List move assignment operator.
601 * @param x A %list of identical element and allocator types.
603 * The contents of @a x are moved into this %list (without copying).
604 * @a x is a valid, but unspecified %list
606 list&
607 operator=(list&& __x)
609 // NB: DR 675.
610 this->clear();
611 this->swap(__x);
612 return *this;
616 * @brief %List initializer list assignment operator.
617 * @param l An initializer_list of value_type.
619 * Replace the contents of the %list with copies of the elements
620 * in the initializer_list @a l. This is linear in l.size().
622 list&
623 operator=(initializer_list<value_type> __l)
625 this->assign(__l.begin(), __l.end());
626 return *this;
628 #endif
631 * @brief Assigns a given value to a %list.
632 * @param n Number of elements to be assigned.
633 * @param val Value to be assigned.
635 * This function fills a %list with @a n copies of the given
636 * value. Note that the assignment completely changes the %list
637 * and that the resulting %list's size is the same as the number
638 * of elements assigned. Old data may be lost.
640 void
641 assign(size_type __n, const value_type& __val)
642 { _M_fill_assign(__n, __val); }
645 * @brief Assigns a range to a %list.
646 * @param first An input iterator.
647 * @param last An input iterator.
649 * This function fills a %list with copies of the elements in the
650 * range [@a first,@a last).
652 * Note that the assignment completely changes the %list and
653 * that the resulting %list's size is the same as the number of
654 * elements assigned. Old data may be lost.
656 template<typename _InputIterator>
657 void
658 assign(_InputIterator __first, _InputIterator __last)
660 // Check whether it's an integral type. If so, it's not an iterator.
661 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
662 _M_assign_dispatch(__first, __last, _Integral());
665 #ifdef __GXX_EXPERIMENTAL_CXX0X__
667 * @brief Assigns an initializer_list to a %list.
668 * @param l An initializer_list of value_type.
670 * Replace the contents of the %list with copies of the elements
671 * in the initializer_list @a l. This is linear in l.size().
673 void
674 assign(initializer_list<value_type> __l)
675 { this->assign(__l.begin(), __l.end()); }
676 #endif
678 /// Get a copy of the memory allocation object.
679 allocator_type
680 get_allocator() const
681 { return _Base::get_allocator(); }
683 // iterators
685 * Returns a read/write iterator that points to the first element in the
686 * %list. Iteration is done in ordinary element order.
688 iterator
689 begin()
690 { return iterator(this->_M_impl._M_node._M_next); }
693 * Returns a read-only (constant) iterator that points to the
694 * first element in the %list. Iteration is done in ordinary
695 * element order.
697 const_iterator
698 begin() const
699 { return const_iterator(this->_M_impl._M_node._M_next); }
702 * Returns a read/write iterator that points one past the last
703 * element in the %list. Iteration is done in ordinary element
704 * order.
706 iterator
707 end()
708 { return iterator(&this->_M_impl._M_node); }
711 * Returns a read-only (constant) iterator that points one past
712 * the last element in the %list. Iteration is done in ordinary
713 * element order.
715 const_iterator
716 end() const
717 { return const_iterator(&this->_M_impl._M_node); }
720 * Returns a read/write reverse iterator that points to the last
721 * element in the %list. Iteration is done in reverse element
722 * order.
724 reverse_iterator
725 rbegin()
726 { return reverse_iterator(end()); }
729 * Returns a read-only (constant) reverse iterator that points to
730 * the last element in the %list. Iteration is done in reverse
731 * element order.
733 const_reverse_iterator
734 rbegin() const
735 { return const_reverse_iterator(end()); }
738 * Returns a read/write reverse iterator that points to one
739 * before the first element in the %list. Iteration is done in
740 * reverse element order.
742 reverse_iterator
743 rend()
744 { return reverse_iterator(begin()); }
747 * Returns a read-only (constant) reverse iterator that points to one
748 * before the first element in the %list. Iteration is done in reverse
749 * element order.
751 const_reverse_iterator
752 rend() const
753 { return const_reverse_iterator(begin()); }
755 #ifdef __GXX_EXPERIMENTAL_CXX0X__
757 * Returns a read-only (constant) iterator that points to the
758 * first element in the %list. Iteration is done in ordinary
759 * element order.
761 const_iterator
762 cbegin() const
763 { return const_iterator(this->_M_impl._M_node._M_next); }
766 * Returns a read-only (constant) iterator that points one past
767 * the last element in the %list. Iteration is done in ordinary
768 * element order.
770 const_iterator
771 cend() const
772 { return const_iterator(&this->_M_impl._M_node); }
775 * Returns a read-only (constant) reverse iterator that points to
776 * the last element in the %list. Iteration is done in reverse
777 * element order.
779 const_reverse_iterator
780 crbegin() const
781 { return const_reverse_iterator(end()); }
784 * Returns a read-only (constant) reverse iterator that points to one
785 * before the first element in the %list. Iteration is done in reverse
786 * element order.
788 const_reverse_iterator
789 crend() const
790 { return const_reverse_iterator(begin()); }
791 #endif
793 // [23.2.2.2] capacity
795 * Returns true if the %list is empty. (Thus begin() would equal
796 * end().)
798 bool
799 empty() const
800 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
802 /** Returns the number of elements in the %list. */
803 size_type
804 size() const
805 { return std::distance(begin(), end()); }
807 /** Returns the size() of the largest possible %list. */
808 size_type
809 max_size() const
810 { return _M_get_Tp_allocator().max_size(); }
813 * @brief Resizes the %list to the specified number of elements.
814 * @param new_size Number of elements the %list should contain.
815 * @param x Data with which new elements should be populated.
817 * This function will %resize the %list to the specified number
818 * of elements. If the number is smaller than the %list's
819 * current size the %list is truncated, otherwise the %list is
820 * extended and new elements are populated with given data.
822 void
823 resize(size_type __new_size, value_type __x = value_type());
825 // element access
827 * Returns a read/write reference to the data at the first
828 * element of the %list.
830 reference
831 front()
832 { return *begin(); }
835 * Returns a read-only (constant) reference to the data at the first
836 * element of the %list.
838 const_reference
839 front() const
840 { return *begin(); }
843 * Returns a read/write reference to the data at the last element
844 * of the %list.
846 reference
847 back()
849 iterator __tmp = end();
850 --__tmp;
851 return *__tmp;
855 * Returns a read-only (constant) reference to the data at the last
856 * element of the %list.
858 const_reference
859 back() const
861 const_iterator __tmp = end();
862 --__tmp;
863 return *__tmp;
866 // [23.2.2.3] modifiers
868 * @brief Add data to the front of the %list.
869 * @param x Data to be added.
871 * This is a typical stack operation. The function creates an
872 * element at the front of the %list and assigns the given data
873 * to it. Due to the nature of a %list this operation can be
874 * done in constant time, and does not invalidate iterators and
875 * references.
877 void
878 push_front(const value_type& __x)
879 { this->_M_insert(begin(), __x); }
881 #ifdef __GXX_EXPERIMENTAL_CXX0X__
882 void
883 push_front(value_type&& __x)
884 { this->_M_insert(begin(), std::move(__x)); }
886 template<typename... _Args>
887 void
888 emplace_front(_Args&&... __args)
889 { this->_M_insert(begin(), std::forward<_Args>(__args)...); }
890 #endif
893 * @brief Removes first element.
895 * This is a typical stack operation. It shrinks the %list by
896 * one. Due to the nature of a %list this operation can be done
897 * in constant time, and only invalidates iterators/references to
898 * the element being removed.
900 * Note that no data is returned, and if the first element's data
901 * is needed, it should be retrieved before pop_front() is
902 * called.
904 void
905 pop_front()
906 { this->_M_erase(begin()); }
909 * @brief Add data to the end of the %list.
910 * @param x Data to be added.
912 * This is a typical stack operation. The function creates an
913 * element at the end of the %list and assigns the given data to
914 * it. Due to the nature of a %list this operation can be done
915 * in constant time, and does not invalidate iterators and
916 * references.
918 void
919 push_back(const value_type& __x)
920 { this->_M_insert(end(), __x); }
922 #ifdef __GXX_EXPERIMENTAL_CXX0X__
923 void
924 push_back(value_type&& __x)
925 { this->_M_insert(end(), std::move(__x)); }
927 template<typename... _Args>
928 void
929 emplace_back(_Args&&... __args)
930 { this->_M_insert(end(), std::forward<_Args>(__args)...); }
931 #endif
934 * @brief Removes last element.
936 * This is a typical stack operation. It shrinks the %list by
937 * one. Due to the nature of a %list this operation can be done
938 * in constant time, and only invalidates iterators/references to
939 * the element being removed.
941 * Note that no data is returned, and if the last element's data
942 * is needed, it should be retrieved before pop_back() is called.
944 void
945 pop_back()
946 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
948 #ifdef __GXX_EXPERIMENTAL_CXX0X__
950 * @brief Constructs object in %list before specified iterator.
951 * @param position A const_iterator into the %list.
952 * @param args Arguments.
953 * @return An iterator that points to the inserted data.
955 * This function will insert an object of type T constructed
956 * with T(std::forward<Args>(args)...) before the specified
957 * location. Due to the nature of a %list this operation can
958 * be done in constant time, and does not invalidate iterators
959 * and references.
961 template<typename... _Args>
962 iterator
963 emplace(iterator __position, _Args&&... __args);
964 #endif
967 * @brief Inserts given value into %list before specified iterator.
968 * @param position An iterator into the %list.
969 * @param x Data to be inserted.
970 * @return An iterator that points to the inserted data.
972 * This function will insert a copy of the given value before
973 * the specified location. Due to the nature of a %list this
974 * operation can be done in constant time, and does not
975 * invalidate iterators and references.
977 iterator
978 insert(iterator __position, const value_type& __x);
980 #ifdef __GXX_EXPERIMENTAL_CXX0X__
982 * @brief Inserts given rvalue into %list before specified iterator.
983 * @param position An iterator into the %list.
984 * @param x Data to be inserted.
985 * @return An iterator that points to the inserted data.
987 * This function will insert a copy of the given rvalue before
988 * the specified location. Due to the nature of a %list this
989 * operation can be done in constant time, and does not
990 * invalidate iterators and references.
992 iterator
993 insert(iterator __position, value_type&& __x)
994 { return emplace(__position, std::move(__x)); }
997 * @brief Inserts the contents of an initializer_list into %list
998 * before specified iterator.
999 * @param p An iterator into the %list.
1000 * @param l An initializer_list of value_type.
1002 * This function will insert copies of the data in the
1003 * initializer_list @a l into the %list before the location
1004 * specified by @a p.
1006 * This operation is linear in the number of elements inserted and
1007 * does not invalidate iterators and references.
1009 void
1010 insert(iterator __p, initializer_list<value_type> __l)
1011 { this->insert(__p, __l.begin(), __l.end()); }
1012 #endif
1015 * @brief Inserts a number of copies of given data into the %list.
1016 * @param position An iterator into the %list.
1017 * @param n Number of elements to be inserted.
1018 * @param x Data to be inserted.
1020 * This function will insert a specified number of copies of the
1021 * given data before the location specified by @a position.
1023 * This operation is linear in the number of elements inserted and
1024 * does not invalidate iterators and references.
1026 void
1027 insert(iterator __position, size_type __n, const value_type& __x)
1029 list __tmp(__n, __x, _M_get_Node_allocator());
1030 splice(__position, __tmp);
1034 * @brief Inserts a range into the %list.
1035 * @param position An iterator into the %list.
1036 * @param first An input iterator.
1037 * @param last An input iterator.
1039 * This function will insert copies of the data in the range [@a
1040 * first,@a last) into the %list before the location specified by
1041 * @a position.
1043 * This operation is linear in the number of elements inserted and
1044 * does not invalidate iterators and references.
1046 template<typename _InputIterator>
1047 void
1048 insert(iterator __position, _InputIterator __first,
1049 _InputIterator __last)
1051 list __tmp(__first, __last, _M_get_Node_allocator());
1052 splice(__position, __tmp);
1056 * @brief Remove element at given position.
1057 * @param position Iterator pointing to element to be erased.
1058 * @return An iterator pointing to the next element (or end()).
1060 * This function will erase the element at the given position and thus
1061 * shorten the %list by one.
1063 * Due to the nature of a %list this operation can be done in
1064 * constant time, and only invalidates iterators/references to
1065 * the element being removed. The user is also cautioned that
1066 * this function only erases the element, and that if the element
1067 * is itself a pointer, the pointed-to memory is not touched in
1068 * any way. Managing the pointer is the user's responsibility.
1070 iterator
1071 erase(iterator __position);
1074 * @brief Remove a range of elements.
1075 * @param first Iterator pointing to the first element to be erased.
1076 * @param last Iterator pointing to one past the last element to be
1077 * erased.
1078 * @return An iterator pointing to the element pointed to by @a last
1079 * prior to erasing (or end()).
1081 * This function will erase the elements in the range @a
1082 * [first,last) and shorten the %list accordingly.
1084 * This operation is linear time in the size of the range and only
1085 * invalidates iterators/references to the element being removed.
1086 * The user is also cautioned that this function only erases the
1087 * elements, and that if the elements themselves are pointers, the
1088 * pointed-to memory is not touched in any way. Managing the pointer
1089 * is the user's responsibility.
1091 iterator
1092 erase(iterator __first, iterator __last)
1094 while (__first != __last)
1095 __first = erase(__first);
1096 return __last;
1100 * @brief Swaps data with another %list.
1101 * @param x A %list of the same element and allocator types.
1103 * This exchanges the elements between two lists in constant
1104 * time. Note that the global std::swap() function is
1105 * specialized such that std::swap(l1,l2) will feed to this
1106 * function.
1108 void
1109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1110 swap(list&& __x)
1111 #else
1112 swap(list& __x)
1113 #endif
1115 _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node);
1117 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1118 // 431. Swapping containers with unequal allocators.
1119 std::__alloc_swap<typename _Base::_Node_alloc_type>::
1120 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator());
1124 * Erases all the elements. Note that this function only erases
1125 * the elements, and that if the elements themselves are
1126 * pointers, the pointed-to memory is not touched in any way.
1127 * Managing the pointer is the user's responsibility.
1129 void
1130 clear()
1132 _Base::_M_clear();
1133 _Base::_M_init();
1136 // [23.2.2.4] list operations
1138 * @brief Insert contents of another %list.
1139 * @param position Iterator referencing the element to insert before.
1140 * @param x Source list.
1142 * The elements of @a x are inserted in constant time in front of
1143 * the element referenced by @a position. @a x becomes an empty
1144 * list.
1146 * Requires this != @a x.
1148 void
1149 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1150 splice(iterator __position, list&& __x)
1151 #else
1152 splice(iterator __position, list& __x)
1153 #endif
1155 if (!__x.empty())
1157 _M_check_equal_allocators(__x);
1159 this->_M_transfer(__position, __x.begin(), __x.end());
1164 * @brief Insert element from another %list.
1165 * @param position Iterator referencing the element to insert before.
1166 * @param x Source list.
1167 * @param i Iterator referencing the element to move.
1169 * Removes the element in list @a x referenced by @a i and
1170 * inserts it into the current list before @a position.
1172 void
1173 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1174 splice(iterator __position, list&& __x, iterator __i)
1175 #else
1176 splice(iterator __position, list& __x, iterator __i)
1177 #endif
1179 iterator __j = __i;
1180 ++__j;
1181 if (__position == __i || __position == __j)
1182 return;
1184 if (this != &__x)
1185 _M_check_equal_allocators(__x);
1187 this->_M_transfer(__position, __i, __j);
1191 * @brief Insert range from another %list.
1192 * @param position Iterator referencing the element to insert before.
1193 * @param x Source list.
1194 * @param first Iterator referencing the start of range in x.
1195 * @param last Iterator referencing the end of range in x.
1197 * Removes elements in the range [first,last) and inserts them
1198 * before @a position in constant time.
1200 * Undefined if @a position is in [first,last).
1202 void
1203 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1204 splice(iterator __position, list&& __x, iterator __first,
1205 iterator __last)
1206 #else
1207 splice(iterator __position, list& __x, iterator __first,
1208 iterator __last)
1209 #endif
1211 if (__first != __last)
1213 if (this != &__x)
1214 _M_check_equal_allocators(__x);
1216 this->_M_transfer(__position, __first, __last);
1221 * @brief Remove all elements equal to value.
1222 * @param value The value to remove.
1224 * Removes every element in the list equal to @a value.
1225 * Remaining elements stay in list order. Note that this
1226 * function only erases the elements, and that if the elements
1227 * themselves are pointers, the pointed-to memory is not
1228 * touched in any way. Managing the pointer is the user's
1229 * responsibility.
1231 void
1232 remove(const _Tp& __value);
1235 * @brief Remove all elements satisfying a predicate.
1236 * @param Predicate Unary predicate function or object.
1238 * Removes every element in the list for which the predicate
1239 * returns true. Remaining elements stay in list order. Note
1240 * that this function only erases the elements, and that if the
1241 * elements themselves are pointers, the pointed-to memory is
1242 * not touched in any way. Managing the pointer is the user's
1243 * responsibility.
1245 template<typename _Predicate>
1246 void
1247 remove_if(_Predicate);
1250 * @brief Remove consecutive duplicate elements.
1252 * For each consecutive set of elements with the same value,
1253 * remove all but the first one. Remaining elements stay in
1254 * list order. Note that this function only erases the
1255 * elements, and that if the elements themselves are pointers,
1256 * the pointed-to memory is not touched in any way. Managing
1257 * the pointer is the user's responsibility.
1259 void
1260 unique();
1263 * @brief Remove consecutive elements satisfying a predicate.
1264 * @param BinaryPredicate Binary predicate function or object.
1266 * For each consecutive set of elements [first,last) that
1267 * satisfy predicate(first,i) where i is an iterator in
1268 * [first,last), remove all but the first one. Remaining
1269 * elements stay in list order. Note that this function only
1270 * erases the elements, and that if the elements themselves are
1271 * pointers, the pointed-to memory is not touched in any way.
1272 * Managing the pointer is the user's responsibility.
1274 template<typename _BinaryPredicate>
1275 void
1276 unique(_BinaryPredicate);
1279 * @brief Merge sorted lists.
1280 * @param x Sorted list to merge.
1282 * Assumes that both @a x and this list are sorted according to
1283 * operator<(). Merges elements of @a x into this list in
1284 * sorted order, leaving @a x empty when complete. Elements in
1285 * this list precede elements in @a x that are equal.
1287 void
1288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1289 merge(list&& __x);
1290 #else
1291 merge(list& __x);
1292 #endif
1295 * @brief Merge sorted lists according to comparison function.
1296 * @param x Sorted list to merge.
1297 * @param StrictWeakOrdering Comparison function defining
1298 * sort order.
1300 * Assumes that both @a x and this list are sorted according to
1301 * StrictWeakOrdering. Merges elements of @a x into this list
1302 * in sorted order, leaving @a x empty when complete. Elements
1303 * in this list precede elements in @a x that are equivalent
1304 * according to StrictWeakOrdering().
1306 template<typename _StrictWeakOrdering>
1307 void
1308 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1309 merge(list&&, _StrictWeakOrdering);
1310 #else
1311 merge(list&, _StrictWeakOrdering);
1312 #endif
1315 * @brief Reverse the elements in list.
1317 * Reverse the order of elements in the list in linear time.
1319 void
1320 reverse()
1321 { this->_M_impl._M_node.reverse(); }
1324 * @brief Sort the elements.
1326 * Sorts the elements of this list in NlogN time. Equivalent
1327 * elements remain in list order.
1329 void
1330 sort();
1333 * @brief Sort the elements according to comparison function.
1335 * Sorts the elements of this list in NlogN time. Equivalent
1336 * elements remain in list order.
1338 template<typename _StrictWeakOrdering>
1339 void
1340 sort(_StrictWeakOrdering);
1342 protected:
1343 // Internal constructor functions follow.
1345 // Called by the range constructor to implement [23.1.1]/9
1347 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1348 // 438. Ambiguity in the "do the right thing" clause
1349 template<typename _Integer>
1350 void
1351 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1352 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1354 // Called by the range constructor to implement [23.1.1]/9
1355 template<typename _InputIterator>
1356 void
1357 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1358 __false_type)
1360 for (; __first != __last; ++__first)
1361 push_back(*__first);
1364 // Called by list(n,v,a), and the range constructor when it turns out
1365 // to be the same thing.
1366 void
1367 _M_fill_initialize(size_type __n, const value_type& __x)
1369 for (; __n > 0; --__n)
1370 push_back(__x);
1374 // Internal assign functions follow.
1376 // Called by the range assign to implement [23.1.1]/9
1378 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1379 // 438. Ambiguity in the "do the right thing" clause
1380 template<typename _Integer>
1381 void
1382 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1383 { _M_fill_assign(__n, __val); }
1385 // Called by the range assign to implement [23.1.1]/9
1386 template<typename _InputIterator>
1387 void
1388 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1389 __false_type);
1391 // Called by assign(n,t), and the range assign when it turns out
1392 // to be the same thing.
1393 void
1394 _M_fill_assign(size_type __n, const value_type& __val);
1397 // Moves the elements from [first,last) before position.
1398 void
1399 _M_transfer(iterator __position, iterator __first, iterator __last)
1400 { __position._M_node->transfer(__first._M_node, __last._M_node); }
1402 // Inserts new element at position given and with value given.
1403 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1404 void
1405 _M_insert(iterator __position, const value_type& __x)
1407 _Node* __tmp = _M_create_node(__x);
1408 __tmp->hook(__position._M_node);
1410 #else
1411 template<typename... _Args>
1412 void
1413 _M_insert(iterator __position, _Args&&... __args)
1415 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
1416 __tmp->hook(__position._M_node);
1418 #endif
1420 // Erases element at position given.
1421 void
1422 _M_erase(iterator __position)
1424 __position._M_node->unhook();
1425 _Node* __n = static_cast<_Node*>(__position._M_node);
1426 _M_get_Tp_allocator().destroy(&__n->_M_data);
1427 _M_put_node(__n);
1430 // To implement the splice (and merge) bits of N1599.
1431 void
1432 _M_check_equal_allocators(list& __x)
1434 if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
1435 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
1436 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1441 * @brief List equality comparison.
1442 * @param x A %list.
1443 * @param y A %list of the same type as @a x.
1444 * @return True iff the size and elements of the lists are equal.
1446 * This is an equivalence relation. It is linear in the size of
1447 * the lists. Lists are considered equivalent if their sizes are
1448 * equal, and if corresponding elements compare equal.
1450 template<typename _Tp, typename _Alloc>
1451 inline bool
1452 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1454 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1455 const_iterator __end1 = __x.end();
1456 const_iterator __end2 = __y.end();
1458 const_iterator __i1 = __x.begin();
1459 const_iterator __i2 = __y.begin();
1460 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1462 ++__i1;
1463 ++__i2;
1465 return __i1 == __end1 && __i2 == __end2;
1469 * @brief List ordering relation.
1470 * @param x A %list.
1471 * @param y A %list of the same type as @a x.
1472 * @return True iff @a x is lexicographically less than @a y.
1474 * This is a total ordering relation. It is linear in the size of the
1475 * lists. The elements must be comparable with @c <.
1477 * See std::lexicographical_compare() for how the determination is made.
1479 template<typename _Tp, typename _Alloc>
1480 inline bool
1481 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1482 { return std::lexicographical_compare(__x.begin(), __x.end(),
1483 __y.begin(), __y.end()); }
1485 /// Based on operator==
1486 template<typename _Tp, typename _Alloc>
1487 inline bool
1488 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1489 { return !(__x == __y); }
1491 /// Based on operator<
1492 template<typename _Tp, typename _Alloc>
1493 inline bool
1494 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1495 { return __y < __x; }
1497 /// Based on operator<
1498 template<typename _Tp, typename _Alloc>
1499 inline bool
1500 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1501 { return !(__y < __x); }
1503 /// Based on operator<
1504 template<typename _Tp, typename _Alloc>
1505 inline bool
1506 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1507 { return !(__x < __y); }
1509 /// See std::list::swap().
1510 template<typename _Tp, typename _Alloc>
1511 inline void
1512 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1513 { __x.swap(__y); }
1515 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1516 template<typename _Tp, typename _Alloc>
1517 inline void
1518 swap(list<_Tp, _Alloc>&& __x, list<_Tp, _Alloc>& __y)
1519 { __x.swap(__y); }
1521 template<typename _Tp, typename _Alloc>
1522 inline void
1523 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>&& __y)
1524 { __x.swap(__y); }
1525 #endif
1527 _GLIBCXX_END_NESTED_NAMESPACE
1529 #endif /* _STL_LIST_H */