PR libstdc++/49561
[official-gcc.git] / libstdc++-v3 / include / bits / stl_list.h
blob8e6567cfe67b9052d222b2410f02671cc57afb16
1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2014 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_list.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{list}
56 #ifndef _STL_LIST_H
57 #define _STL_LIST_H 1
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
64 namespace std _GLIBCXX_VISIBILITY(default)
66 namespace __detail
68 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 // Supporting structures are split into common and templated
71 // types; the latter publicly inherits from the former in an
72 // effort to reduce code duplication. This results in some
73 // "needless" static_cast'ing later on, but it's all safe
74 // downcasting.
76 /// Common part of a node in the %list.
77 struct _List_node_base
79 _List_node_base* _M_next;
80 _List_node_base* _M_prev;
82 static void
83 swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT;
85 void
86 _M_transfer(_List_node_base* const __first,
87 _List_node_base* const __last) _GLIBCXX_USE_NOEXCEPT;
89 void
90 _M_reverse() _GLIBCXX_USE_NOEXCEPT;
92 void
93 _M_hook(_List_node_base* const __position) _GLIBCXX_USE_NOEXCEPT;
95 void
96 _M_unhook() _GLIBCXX_USE_NOEXCEPT;
99 _GLIBCXX_END_NAMESPACE_VERSION
100 } // namespace detail
102 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
104 /// An actual node in the %list.
105 template<typename _Tp>
106 struct _List_node : public __detail::_List_node_base
108 ///< User's data.
109 _Tp _M_data;
111 #if __cplusplus >= 201103L
112 template<typename... _Args>
113 _List_node(_Args&&... __args)
114 : __detail::_List_node_base(), _M_data(std::forward<_Args>(__args)...)
116 #endif
120 * @brief A list::iterator.
122 * All the functions are op overloads.
124 template<typename _Tp>
125 struct _List_iterator
127 typedef _List_iterator<_Tp> _Self;
128 typedef _List_node<_Tp> _Node;
130 typedef ptrdiff_t difference_type;
131 typedef std::bidirectional_iterator_tag iterator_category;
132 typedef _Tp value_type;
133 typedef _Tp* pointer;
134 typedef _Tp& reference;
136 _List_iterator() _GLIBCXX_NOEXCEPT
137 : _M_node() { }
139 explicit
140 _List_iterator(__detail::_List_node_base* __x) _GLIBCXX_NOEXCEPT
141 : _M_node(__x) { }
143 _Self
144 _M_const_cast() const _GLIBCXX_NOEXCEPT
145 { return *this; }
147 // Must downcast from _List_node_base to _List_node to get to _M_data.
148 reference
149 operator*() const _GLIBCXX_NOEXCEPT
150 { return static_cast<_Node*>(_M_node)->_M_data; }
152 pointer
153 operator->() const _GLIBCXX_NOEXCEPT
154 { return std::__addressof(static_cast<_Node*>(_M_node)->_M_data); }
156 _Self&
157 operator++() _GLIBCXX_NOEXCEPT
159 _M_node = _M_node->_M_next;
160 return *this;
163 _Self
164 operator++(int) _GLIBCXX_NOEXCEPT
166 _Self __tmp = *this;
167 _M_node = _M_node->_M_next;
168 return __tmp;
171 _Self&
172 operator--() _GLIBCXX_NOEXCEPT
174 _M_node = _M_node->_M_prev;
175 return *this;
178 _Self
179 operator--(int) _GLIBCXX_NOEXCEPT
181 _Self __tmp = *this;
182 _M_node = _M_node->_M_prev;
183 return __tmp;
186 bool
187 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
188 { return _M_node == __x._M_node; }
190 bool
191 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
192 { return _M_node != __x._M_node; }
194 // The only member points to the %list element.
195 __detail::_List_node_base* _M_node;
199 * @brief A list::const_iterator.
201 * All the functions are op overloads.
203 template<typename _Tp>
204 struct _List_const_iterator
206 typedef _List_const_iterator<_Tp> _Self;
207 typedef const _List_node<_Tp> _Node;
208 typedef _List_iterator<_Tp> iterator;
210 typedef ptrdiff_t difference_type;
211 typedef std::bidirectional_iterator_tag iterator_category;
212 typedef _Tp value_type;
213 typedef const _Tp* pointer;
214 typedef const _Tp& reference;
216 _List_const_iterator() _GLIBCXX_NOEXCEPT
217 : _M_node() { }
219 explicit
220 _List_const_iterator(const __detail::_List_node_base* __x)
221 _GLIBCXX_NOEXCEPT
222 : _M_node(__x) { }
224 _List_const_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
225 : _M_node(__x._M_node) { }
227 iterator
228 _M_const_cast() const _GLIBCXX_NOEXCEPT
229 { return iterator(const_cast<__detail::_List_node_base*>(_M_node)); }
231 // Must downcast from List_node_base to _List_node to get to
232 // _M_data.
233 reference
234 operator*() const _GLIBCXX_NOEXCEPT
235 { return static_cast<_Node*>(_M_node)->_M_data; }
237 pointer
238 operator->() const _GLIBCXX_NOEXCEPT
239 { return std::__addressof(static_cast<_Node*>(_M_node)->_M_data); }
241 _Self&
242 operator++() _GLIBCXX_NOEXCEPT
244 _M_node = _M_node->_M_next;
245 return *this;
248 _Self
249 operator++(int) _GLIBCXX_NOEXCEPT
251 _Self __tmp = *this;
252 _M_node = _M_node->_M_next;
253 return __tmp;
256 _Self&
257 operator--() _GLIBCXX_NOEXCEPT
259 _M_node = _M_node->_M_prev;
260 return *this;
263 _Self
264 operator--(int) _GLIBCXX_NOEXCEPT
266 _Self __tmp = *this;
267 _M_node = _M_node->_M_prev;
268 return __tmp;
271 bool
272 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
273 { return _M_node == __x._M_node; }
275 bool
276 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
277 { return _M_node != __x._M_node; }
279 // The only member points to the %list element.
280 const __detail::_List_node_base* _M_node;
283 template<typename _Val>
284 inline bool
285 operator==(const _List_iterator<_Val>& __x,
286 const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
287 { return __x._M_node == __y._M_node; }
289 template<typename _Val>
290 inline bool
291 operator!=(const _List_iterator<_Val>& __x,
292 const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
293 { return __x._M_node != __y._M_node; }
296 /// See bits/stl_deque.h's _Deque_base for an explanation.
297 template<typename _Tp, typename _Alloc>
298 class _GLIBCXX_DEFAULT_ABI_TAG _List_base
300 protected:
301 // NOTA BENE
302 // The stored instance is not actually of "allocator_type"'s
303 // type. Instead we rebind the type to
304 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
305 // should probably be the same. List_node<Tp> is not the same
306 // size as Tp (it's two pointers larger), and specializations on
307 // Tp may go unused because List_node<Tp> is being bound
308 // instead.
310 // We put this to the test in the constructors and in
311 // get_allocator, where we use conversions between
312 // allocator_type and _Node_alloc_type. The conversion is
313 // required by table 32 in [20.1.5].
314 typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
315 _Node_alloc_type;
317 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
319 static size_t
320 _S_distance(const __detail::_List_node_base* __first,
321 const __detail::_List_node_base* __last)
323 size_t __n = 0;
324 while (__first != __last)
326 __first = __first->_M_next;
327 ++__n;
329 return __n;
332 struct _List_impl
333 : public _Node_alloc_type
335 __detail::_List_node_base _M_node;
337 _List_impl()
338 : _Node_alloc_type(), _M_node()
341 _List_impl(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
342 : _Node_alloc_type(__a), _M_node()
345 #if __cplusplus >= 201103L
346 _List_impl(_Node_alloc_type&& __a) _GLIBCXX_NOEXCEPT
347 : _Node_alloc_type(std::move(__a)), _M_node()
349 #endif
352 _List_impl _M_impl;
354 #if _GLIBCXX_USE_CXX11_ABI
355 size_t _M_size;
357 size_t _M_get_size() const { return _M_size; }
359 void _M_set_size(size_t __n) { _M_size = __n; }
361 void _M_inc_size(size_t __n) { _M_size += __n; }
363 void _M_dec_size(size_t __n) { _M_size -= __n; }
365 size_t
366 _M_distance(const __detail::_List_node_base* __first,
367 const __detail::_List_node_base* __last) const
368 { return _S_distance(__first, __last); }
370 // return the stored size
371 size_t _M_node_count() const { return _M_size; }
372 #else
373 // dummy implementations used when the size is not stored
374 size_t _M_get_size() const { return 0; }
375 void _M_set_size(size_t) { }
376 void _M_inc_size(size_t) { }
377 void _M_dec_size(size_t) { }
378 size_t _M_distance(const void*, const void*) const { return 0; }
380 // count the number of nodes
381 size_t _M_node_count() const
383 return _S_distance(_M_impl._M_node._M_next,
384 std::__addressof(_M_impl._M_node));
386 #endif
388 _List_node<_Tp>*
389 _M_get_node()
390 { return _M_impl._Node_alloc_type::allocate(1); }
392 void
393 _M_put_node(_List_node<_Tp>* __p) _GLIBCXX_NOEXCEPT
394 { _M_impl._Node_alloc_type::deallocate(__p, 1); }
396 public:
397 typedef _Alloc allocator_type;
399 _Node_alloc_type&
400 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
401 { return *static_cast<_Node_alloc_type*>(&_M_impl); }
403 const _Node_alloc_type&
404 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
405 { return *static_cast<const _Node_alloc_type*>(&_M_impl); }
407 _Tp_alloc_type
408 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
409 { return _Tp_alloc_type(_M_get_Node_allocator()); }
411 allocator_type
412 get_allocator() const _GLIBCXX_NOEXCEPT
413 { return allocator_type(_M_get_Node_allocator()); }
415 _List_base()
416 : _M_impl()
417 { _M_init(); }
419 _List_base(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
420 : _M_impl(__a)
421 { _M_init(); }
423 #if __cplusplus >= 201103L
424 _List_base(_List_base&& __x) noexcept
425 : _M_impl(std::move(__x._M_get_Node_allocator()))
427 auto* const __xnode = std::__addressof(__x._M_impl._M_node);
428 if (__xnode->_M_next == __xnode)
429 _M_init();
430 else
432 auto* const __node = std::__addressof(_M_impl._M_node);
433 __node->_M_next = __xnode->_M_next;
434 __node->_M_prev = __xnode->_M_prev;
435 __node->_M_next->_M_prev = __node->_M_prev->_M_next = __node;
436 _M_set_size(__x._M_get_size());
437 __x._M_init();
440 #endif
442 // This is what actually destroys the list.
443 ~_List_base() _GLIBCXX_NOEXCEPT
444 { _M_clear(); }
446 void
447 _M_clear() _GLIBCXX_NOEXCEPT;
449 void
450 _M_init() _GLIBCXX_NOEXCEPT
452 this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
453 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
454 _M_set_size(0);
459 * @brief A standard container with linear time access to elements,
460 * and fixed time insertion/deletion at any point in the sequence.
462 * @ingroup sequences
464 * @tparam _Tp Type of element.
465 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
467 * Meets the requirements of a <a href="tables.html#65">container</a>, a
468 * <a href="tables.html#66">reversible container</a>, and a
469 * <a href="tables.html#67">sequence</a>, including the
470 * <a href="tables.html#68">optional sequence requirements</a> with the
471 * %exception of @c at and @c operator[].
473 * This is a @e doubly @e linked %list. Traversal up and down the
474 * %list requires linear time, but adding and removing elements (or
475 * @e nodes) is done in constant time, regardless of where the
476 * change takes place. Unlike std::vector and std::deque,
477 * random-access iterators are not provided, so subscripting ( @c
478 * [] ) access is not allowed. For algorithms which only need
479 * sequential access, this lack makes no difference.
481 * Also unlike the other standard containers, std::list provides
482 * specialized algorithms %unique to linked lists, such as
483 * splicing, sorting, and in-place reversal.
485 * A couple points on memory allocation for list<Tp>:
487 * First, we never actually allocate a Tp, we allocate
488 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
489 * that after elements from %list<X,Alloc1> are spliced into
490 * %list<X,Alloc2>, destroying the memory of the second %list is a
491 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
493 * Second, a %list conceptually represented as
494 * @code
495 * A <---> B <---> C <---> D
496 * @endcode
497 * is actually circular; a link exists between A and D. The %list
498 * class holds (as its only data member) a private list::iterator
499 * pointing to @e D, not to @e A! To get to the head of the %list,
500 * we start at the tail and move forward by one. When this member
501 * iterator's next/previous pointers refer to itself, the %list is
502 * %empty.
504 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
505 class _GLIBCXX_DEFAULT_ABI_TAG list : protected _List_base<_Tp, _Alloc>
507 // concept requirements
508 typedef typename _Alloc::value_type _Alloc_value_type;
509 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
510 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
512 typedef _List_base<_Tp, _Alloc> _Base;
513 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
514 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
516 public:
517 typedef _Tp value_type;
518 typedef typename _Tp_alloc_type::pointer pointer;
519 typedef typename _Tp_alloc_type::const_pointer const_pointer;
520 typedef typename _Tp_alloc_type::reference reference;
521 typedef typename _Tp_alloc_type::const_reference const_reference;
522 typedef _List_iterator<_Tp> iterator;
523 typedef _List_const_iterator<_Tp> const_iterator;
524 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
525 typedef std::reverse_iterator<iterator> reverse_iterator;
526 typedef size_t size_type;
527 typedef ptrdiff_t difference_type;
528 typedef _Alloc allocator_type;
530 protected:
531 // Note that pointers-to-_Node's can be ctor-converted to
532 // iterator types.
533 typedef _List_node<_Tp> _Node;
535 using _Base::_M_impl;
536 using _Base::_M_put_node;
537 using _Base::_M_get_node;
538 using _Base::_M_get_Tp_allocator;
539 using _Base::_M_get_Node_allocator;
542 * @param __args An instance of user data.
544 * Allocates space for a new node and constructs a copy of
545 * @a __args in it.
547 #if __cplusplus < 201103L
548 _Node*
549 _M_create_node(const value_type& __x)
551 _Node* __p = this->_M_get_node();
552 __try
554 _M_get_Tp_allocator().construct
555 (std::__addressof(__p->_M_data), __x);
557 __catch(...)
559 _M_put_node(__p);
560 __throw_exception_again;
562 return __p;
564 #else
565 template<typename... _Args>
566 _Node*
567 _M_create_node(_Args&&... __args)
569 _Node* __p = this->_M_get_node();
570 __try
572 _M_get_Node_allocator().construct(__p,
573 std::forward<_Args>(__args)...);
575 __catch(...)
577 _M_put_node(__p);
578 __throw_exception_again;
580 return __p;
582 #endif
584 public:
585 // [23.2.2.1] construct/copy/destroy
586 // (assign() and get_allocator() are also listed in this section)
589 * @brief Creates a %list with no elements.
591 list()
592 #if __cplusplus >= 201103L
593 noexcept(is_nothrow_default_constructible<_Node_alloc_type>::value)
594 #endif
595 : _Base() { }
598 * @brief Creates a %list with no elements.
599 * @param __a An allocator object.
601 explicit
602 list(const allocator_type& __a) _GLIBCXX_NOEXCEPT
603 : _Base(_Node_alloc_type(__a)) { }
605 #if __cplusplus >= 201103L
607 * @brief Creates a %list with default constructed elements.
608 * @param __n The number of elements to initially create.
610 * This constructor fills the %list with @a __n default
611 * constructed elements.
613 explicit
614 list(size_type __n)
615 : _Base()
616 { _M_default_initialize(__n); }
619 * @brief Creates a %list with copies of an exemplar element.
620 * @param __n The number of elements to initially create.
621 * @param __value An element to copy.
622 * @param __a An allocator object.
624 * This constructor fills the %list with @a __n copies of @a __value.
626 list(size_type __n, const value_type& __value,
627 const allocator_type& __a = allocator_type())
628 : _Base(_Node_alloc_type(__a))
629 { _M_fill_initialize(__n, __value); }
630 #else
632 * @brief Creates a %list with copies of an exemplar element.
633 * @param __n The number of elements to initially create.
634 * @param __value An element to copy.
635 * @param __a An allocator object.
637 * This constructor fills the %list with @a __n copies of @a __value.
639 explicit
640 list(size_type __n, const value_type& __value = value_type(),
641 const allocator_type& __a = allocator_type())
642 : _Base(_Node_alloc_type(__a))
643 { _M_fill_initialize(__n, __value); }
644 #endif
647 * @brief %List copy constructor.
648 * @param __x A %list of identical element and allocator types.
650 * The newly-created %list uses a copy of the allocation object used
651 * by @a __x.
653 list(const list& __x)
654 : _Base(__x._M_get_Node_allocator())
655 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
657 #if __cplusplus >= 201103L
659 * @brief %List move constructor.
660 * @param __x A %list of identical element and allocator types.
662 * The newly-created %list contains the exact contents of @a __x.
663 * The contents of @a __x are a valid, but unspecified %list.
665 list(list&& __x) noexcept
666 : _Base(std::move(__x)) { }
669 * @brief Builds a %list from an initializer_list
670 * @param __l An initializer_list of value_type.
671 * @param __a An allocator object.
673 * Create a %list consisting of copies of the elements in the
674 * initializer_list @a __l. This is linear in __l.size().
676 list(initializer_list<value_type> __l,
677 const allocator_type& __a = allocator_type())
678 : _Base(_Node_alloc_type(__a))
679 { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
680 #endif
683 * @brief Builds a %list from a range.
684 * @param __first An input iterator.
685 * @param __last An input iterator.
686 * @param __a An allocator object.
688 * Create a %list consisting of copies of the elements from
689 * [@a __first,@a __last). This is linear in N (where N is
690 * distance(@a __first,@a __last)).
692 #if __cplusplus >= 201103L
693 template<typename _InputIterator,
694 typename = std::_RequireInputIter<_InputIterator>>
695 list(_InputIterator __first, _InputIterator __last,
696 const allocator_type& __a = allocator_type())
697 : _Base(_Node_alloc_type(__a))
698 { _M_initialize_dispatch(__first, __last, __false_type()); }
699 #else
700 template<typename _InputIterator>
701 list(_InputIterator __first, _InputIterator __last,
702 const allocator_type& __a = allocator_type())
703 : _Base(_Node_alloc_type(__a))
705 // Check whether it's an integral type. If so, it's not an iterator.
706 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
707 _M_initialize_dispatch(__first, __last, _Integral());
709 #endif
712 * No explicit dtor needed as the _Base dtor takes care of
713 * things. The _Base dtor only erases the elements, and note
714 * that if the elements themselves are pointers, the pointed-to
715 * memory is not touched in any way. Managing the pointer is
716 * the user's responsibility.
720 * @brief %List assignment operator.
721 * @param __x A %list of identical element and allocator types.
723 * All the elements of @a __x are copied, but unlike the copy
724 * constructor, the allocator object is not copied.
726 list&
727 operator=(const list& __x);
729 #if __cplusplus >= 201103L
731 * @brief %List move assignment operator.
732 * @param __x A %list of identical element and allocator types.
734 * The contents of @a __x are moved into this %list (without copying).
735 * @a __x is a valid, but unspecified %list
737 list&
738 operator=(list&& __x)
740 // NB: DR 1204.
741 // NB: DR 675.
742 this->clear();
743 this->swap(__x);
744 return *this;
748 * @brief %List initializer list assignment operator.
749 * @param __l An initializer_list of value_type.
751 * Replace the contents of the %list with copies of the elements
752 * in the initializer_list @a __l. This is linear in l.size().
754 list&
755 operator=(initializer_list<value_type> __l)
757 this->assign(__l.begin(), __l.end());
758 return *this;
760 #endif
763 * @brief Assigns a given value to a %list.
764 * @param __n Number of elements to be assigned.
765 * @param __val Value to be assigned.
767 * This function fills a %list with @a __n copies of the given
768 * value. Note that the assignment completely changes the %list
769 * and that the resulting %list's size is the same as the number
770 * of elements assigned. Old data may be lost.
772 void
773 assign(size_type __n, const value_type& __val)
774 { _M_fill_assign(__n, __val); }
777 * @brief Assigns a range to a %list.
778 * @param __first An input iterator.
779 * @param __last An input iterator.
781 * This function fills a %list with copies of the elements in the
782 * range [@a __first,@a __last).
784 * Note that the assignment completely changes the %list and
785 * that the resulting %list's size is the same as the number of
786 * elements assigned. Old data may be lost.
788 #if __cplusplus >= 201103L
789 template<typename _InputIterator,
790 typename = std::_RequireInputIter<_InputIterator>>
791 void
792 assign(_InputIterator __first, _InputIterator __last)
793 { _M_assign_dispatch(__first, __last, __false_type()); }
794 #else
795 template<typename _InputIterator>
796 void
797 assign(_InputIterator __first, _InputIterator __last)
799 // Check whether it's an integral type. If so, it's not an iterator.
800 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
801 _M_assign_dispatch(__first, __last, _Integral());
803 #endif
805 #if __cplusplus >= 201103L
807 * @brief Assigns an initializer_list to a %list.
808 * @param __l An initializer_list of value_type.
810 * Replace the contents of the %list with copies of the elements
811 * in the initializer_list @a __l. This is linear in __l.size().
813 void
814 assign(initializer_list<value_type> __l)
815 { this->assign(__l.begin(), __l.end()); }
816 #endif
818 /// Get a copy of the memory allocation object.
819 allocator_type
820 get_allocator() const _GLIBCXX_NOEXCEPT
821 { return _Base::get_allocator(); }
823 // iterators
825 * Returns a read/write iterator that points to the first element in the
826 * %list. Iteration is done in ordinary element order.
828 iterator
829 begin() _GLIBCXX_NOEXCEPT
830 { return iterator(this->_M_impl._M_node._M_next); }
833 * Returns a read-only (constant) iterator that points to the
834 * first element in the %list. Iteration is done in ordinary
835 * element order.
837 const_iterator
838 begin() const _GLIBCXX_NOEXCEPT
839 { return const_iterator(this->_M_impl._M_node._M_next); }
842 * Returns a read/write iterator that points one past the last
843 * element in the %list. Iteration is done in ordinary element
844 * order.
846 iterator
847 end() _GLIBCXX_NOEXCEPT
848 { return iterator(&this->_M_impl._M_node); }
851 * Returns a read-only (constant) iterator that points one past
852 * the last element in the %list. Iteration is done in ordinary
853 * element order.
855 const_iterator
856 end() const _GLIBCXX_NOEXCEPT
857 { return const_iterator(&this->_M_impl._M_node); }
860 * Returns a read/write reverse iterator that points to the last
861 * element in the %list. Iteration is done in reverse element
862 * order.
864 reverse_iterator
865 rbegin() _GLIBCXX_NOEXCEPT
866 { return reverse_iterator(end()); }
869 * Returns a read-only (constant) reverse iterator that points to
870 * the last element in the %list. Iteration is done in reverse
871 * element order.
873 const_reverse_iterator
874 rbegin() const _GLIBCXX_NOEXCEPT
875 { return const_reverse_iterator(end()); }
878 * Returns a read/write reverse iterator that points to one
879 * before the first element in the %list. Iteration is done in
880 * reverse element order.
882 reverse_iterator
883 rend() _GLIBCXX_NOEXCEPT
884 { return reverse_iterator(begin()); }
887 * Returns a read-only (constant) reverse iterator that points to one
888 * before the first element in the %list. Iteration is done in reverse
889 * element order.
891 const_reverse_iterator
892 rend() const _GLIBCXX_NOEXCEPT
893 { return const_reverse_iterator(begin()); }
895 #if __cplusplus >= 201103L
897 * Returns a read-only (constant) iterator that points to the
898 * first element in the %list. Iteration is done in ordinary
899 * element order.
901 const_iterator
902 cbegin() const noexcept
903 { return const_iterator(this->_M_impl._M_node._M_next); }
906 * Returns a read-only (constant) iterator that points one past
907 * the last element in the %list. Iteration is done in ordinary
908 * element order.
910 const_iterator
911 cend() const noexcept
912 { return const_iterator(&this->_M_impl._M_node); }
915 * Returns a read-only (constant) reverse iterator that points to
916 * the last element in the %list. Iteration is done in reverse
917 * element order.
919 const_reverse_iterator
920 crbegin() const noexcept
921 { return const_reverse_iterator(end()); }
924 * Returns a read-only (constant) reverse iterator that points to one
925 * before the first element in the %list. Iteration is done in reverse
926 * element order.
928 const_reverse_iterator
929 crend() const noexcept
930 { return const_reverse_iterator(begin()); }
931 #endif
933 // [23.2.2.2] capacity
935 * Returns true if the %list is empty. (Thus begin() would equal
936 * end().)
938 bool
939 empty() const _GLIBCXX_NOEXCEPT
940 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
942 /** Returns the number of elements in the %list. */
943 size_type
944 size() const _GLIBCXX_NOEXCEPT
945 { return this->_M_node_count(); }
947 /** Returns the size() of the largest possible %list. */
948 size_type
949 max_size() const _GLIBCXX_NOEXCEPT
950 { return _M_get_Node_allocator().max_size(); }
952 #if __cplusplus >= 201103L
954 * @brief Resizes the %list to the specified number of elements.
955 * @param __new_size Number of elements the %list should contain.
957 * This function will %resize the %list to the specified number
958 * of elements. If the number is smaller than the %list's
959 * current size the %list is truncated, otherwise default
960 * constructed elements are appended.
962 void
963 resize(size_type __new_size);
966 * @brief Resizes the %list to the specified number of elements.
967 * @param __new_size Number of elements the %list should contain.
968 * @param __x Data with which new elements should be populated.
970 * This function will %resize the %list to the specified number
971 * of elements. If the number is smaller than the %list's
972 * current size the %list is truncated, otherwise the %list is
973 * extended and new elements are populated with given data.
975 void
976 resize(size_type __new_size, const value_type& __x);
977 #else
979 * @brief Resizes the %list to the specified number of elements.
980 * @param __new_size Number of elements the %list should contain.
981 * @param __x Data with which new elements should be populated.
983 * This function will %resize the %list to the specified number
984 * of elements. If the number is smaller than the %list's
985 * current size the %list is truncated, otherwise the %list is
986 * extended and new elements are populated with given data.
988 void
989 resize(size_type __new_size, value_type __x = value_type());
990 #endif
992 // element access
994 * Returns a read/write reference to the data at the first
995 * element of the %list.
997 reference
998 front() _GLIBCXX_NOEXCEPT
999 { return *begin(); }
1002 * Returns a read-only (constant) reference to the data at the first
1003 * element of the %list.
1005 const_reference
1006 front() const _GLIBCXX_NOEXCEPT
1007 { return *begin(); }
1010 * Returns a read/write reference to the data at the last element
1011 * of the %list.
1013 reference
1014 back() _GLIBCXX_NOEXCEPT
1016 iterator __tmp = end();
1017 --__tmp;
1018 return *__tmp;
1022 * Returns a read-only (constant) reference to the data at the last
1023 * element of the %list.
1025 const_reference
1026 back() const _GLIBCXX_NOEXCEPT
1028 const_iterator __tmp = end();
1029 --__tmp;
1030 return *__tmp;
1033 // [23.2.2.3] modifiers
1035 * @brief Add data to the front of the %list.
1036 * @param __x Data to be added.
1038 * This is a typical stack operation. The function creates an
1039 * element at the front of the %list and assigns the given data
1040 * to it. Due to the nature of a %list this operation can be
1041 * done in constant time, and does not invalidate iterators and
1042 * references.
1044 void
1045 push_front(const value_type& __x)
1046 { this->_M_insert(begin(), __x); }
1048 #if __cplusplus >= 201103L
1049 void
1050 push_front(value_type&& __x)
1051 { this->_M_insert(begin(), std::move(__x)); }
1053 template<typename... _Args>
1054 void
1055 emplace_front(_Args&&... __args)
1056 { this->_M_insert(begin(), std::forward<_Args>(__args)...); }
1057 #endif
1060 * @brief Removes first element.
1062 * This is a typical stack operation. It shrinks the %list by
1063 * one. Due to the nature of a %list this operation can be done
1064 * in constant time, and only invalidates iterators/references to
1065 * the element being removed.
1067 * Note that no data is returned, and if the first element's data
1068 * is needed, it should be retrieved before pop_front() is
1069 * called.
1071 void
1072 pop_front() _GLIBCXX_NOEXCEPT
1073 { this->_M_erase(begin()); }
1076 * @brief Add data to the end of the %list.
1077 * @param __x Data to be added.
1079 * This is a typical stack operation. The function creates an
1080 * element at the end of the %list and assigns the given data to
1081 * it. Due to the nature of a %list this operation can be done
1082 * in constant time, and does not invalidate iterators and
1083 * references.
1085 void
1086 push_back(const value_type& __x)
1087 { this->_M_insert(end(), __x); }
1089 #if __cplusplus >= 201103L
1090 void
1091 push_back(value_type&& __x)
1092 { this->_M_insert(end(), std::move(__x)); }
1094 template<typename... _Args>
1095 void
1096 emplace_back(_Args&&... __args)
1097 { this->_M_insert(end(), std::forward<_Args>(__args)...); }
1098 #endif
1101 * @brief Removes last element.
1103 * This is a typical stack operation. It shrinks the %list by
1104 * one. Due to the nature of a %list this operation can be done
1105 * in constant time, and only invalidates iterators/references to
1106 * the element being removed.
1108 * Note that no data is returned, and if the last element's data
1109 * is needed, it should be retrieved before pop_back() is called.
1111 void
1112 pop_back() _GLIBCXX_NOEXCEPT
1113 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
1115 #if __cplusplus >= 201103L
1117 * @brief Constructs object in %list before specified iterator.
1118 * @param __position A const_iterator into the %list.
1119 * @param __args Arguments.
1120 * @return An iterator that points to the inserted data.
1122 * This function will insert an object of type T constructed
1123 * with T(std::forward<Args>(args)...) before the specified
1124 * location. Due to the nature of a %list this operation can
1125 * be done in constant time, and does not invalidate iterators
1126 * and references.
1128 template<typename... _Args>
1129 iterator
1130 emplace(const_iterator __position, _Args&&... __args);
1133 * @brief Inserts given value into %list before specified iterator.
1134 * @param __position A const_iterator into the %list.
1135 * @param __x Data to be inserted.
1136 * @return An iterator that points to the inserted data.
1138 * This function will insert a copy of the given value before
1139 * the specified location. Due to the nature of a %list this
1140 * operation can be done in constant time, and does not
1141 * invalidate iterators and references.
1143 iterator
1144 insert(const_iterator __position, const value_type& __x);
1145 #else
1147 * @brief Inserts given value into %list before specified iterator.
1148 * @param __position An iterator into the %list.
1149 * @param __x Data to be inserted.
1150 * @return An iterator that points to the inserted data.
1152 * This function will insert a copy of the given value before
1153 * the specified location. Due to the nature of a %list this
1154 * operation can be done in constant time, and does not
1155 * invalidate iterators and references.
1157 iterator
1158 insert(iterator __position, const value_type& __x);
1159 #endif
1161 #if __cplusplus >= 201103L
1163 * @brief Inserts given rvalue into %list before specified iterator.
1164 * @param __position A const_iterator into the %list.
1165 * @param __x Data to be inserted.
1166 * @return An iterator that points to the inserted data.
1168 * This function will insert a copy of the given rvalue before
1169 * the specified location. Due to the nature of a %list this
1170 * operation can be done in constant time, and does not
1171 * invalidate iterators and references.
1173 iterator
1174 insert(const_iterator __position, value_type&& __x)
1175 { return emplace(__position, std::move(__x)); }
1178 * @brief Inserts the contents of an initializer_list into %list
1179 * before specified const_iterator.
1180 * @param __p A const_iterator into the %list.
1181 * @param __l An initializer_list of value_type.
1182 * @return An iterator pointing to the first element inserted
1183 * (or __position).
1185 * This function will insert copies of the data in the
1186 * initializer_list @a l into the %list before the location
1187 * specified by @a p.
1189 * This operation is linear in the number of elements inserted and
1190 * does not invalidate iterators and references.
1192 iterator
1193 insert(const_iterator __p, initializer_list<value_type> __l)
1194 { return this->insert(__p, __l.begin(), __l.end()); }
1195 #endif
1197 #if __cplusplus >= 201103L
1199 * @brief Inserts a number of copies of given data into the %list.
1200 * @param __position A const_iterator into the %list.
1201 * @param __n Number of elements to be inserted.
1202 * @param __x Data to be inserted.
1203 * @return An iterator pointing to the first element inserted
1204 * (or __position).
1206 * This function will insert a specified number of copies of the
1207 * given data before the location specified by @a position.
1209 * This operation is linear in the number of elements inserted and
1210 * does not invalidate iterators and references.
1212 iterator
1213 insert(const_iterator __position, size_type __n, const value_type& __x);
1214 #else
1216 * @brief Inserts a number of copies of given data into the %list.
1217 * @param __position An iterator into the %list.
1218 * @param __n Number of elements to be inserted.
1219 * @param __x Data to be inserted.
1221 * This function will insert a specified number of copies of the
1222 * given data before the location specified by @a position.
1224 * This operation is linear in the number of elements inserted and
1225 * does not invalidate iterators and references.
1227 void
1228 insert(iterator __position, size_type __n, const value_type& __x)
1230 list __tmp(__n, __x, get_allocator());
1231 splice(__position, __tmp);
1233 #endif
1235 #if __cplusplus >= 201103L
1237 * @brief Inserts a range into the %list.
1238 * @param __position A const_iterator into the %list.
1239 * @param __first An input iterator.
1240 * @param __last An input iterator.
1241 * @return An iterator pointing to the first element inserted
1242 * (or __position).
1244 * This function will insert copies of the data in the range [@a
1245 * first,@a last) into the %list before the location specified by
1246 * @a position.
1248 * This operation is linear in the number of elements inserted and
1249 * does not invalidate iterators and references.
1251 template<typename _InputIterator,
1252 typename = std::_RequireInputIter<_InputIterator>>
1253 iterator
1254 insert(const_iterator __position, _InputIterator __first,
1255 _InputIterator __last);
1256 #else
1258 * @brief Inserts a range into the %list.
1259 * @param __position An iterator into the %list.
1260 * @param __first An input iterator.
1261 * @param __last An input iterator.
1263 * This function will insert copies of the data in the range [@a
1264 * first,@a last) into the %list before the location specified by
1265 * @a position.
1267 * This operation is linear in the number of elements inserted and
1268 * does not invalidate iterators and references.
1270 template<typename _InputIterator>
1271 void
1272 insert(iterator __position, _InputIterator __first,
1273 _InputIterator __last)
1275 list __tmp(__first, __last, get_allocator());
1276 splice(__position, __tmp);
1278 #endif
1281 * @brief Remove element at given position.
1282 * @param __position Iterator pointing to element to be erased.
1283 * @return An iterator pointing to the next element (or end()).
1285 * This function will erase the element at the given position and thus
1286 * shorten the %list by one.
1288 * Due to the nature of a %list this operation can be done in
1289 * constant time, and only invalidates iterators/references to
1290 * the element being removed. The user is also cautioned that
1291 * this function only erases the element, and that if the element
1292 * is itself a pointer, the pointed-to memory is not touched in
1293 * any way. Managing the pointer is the user's responsibility.
1295 iterator
1296 #if __cplusplus >= 201103L
1297 erase(const_iterator __position) noexcept;
1298 #else
1299 erase(iterator __position);
1300 #endif
1303 * @brief Remove a range of elements.
1304 * @param __first Iterator pointing to the first element to be erased.
1305 * @param __last Iterator pointing to one past the last element to be
1306 * erased.
1307 * @return An iterator pointing to the element pointed to by @a last
1308 * prior to erasing (or end()).
1310 * This function will erase the elements in the range @a
1311 * [first,last) and shorten the %list accordingly.
1313 * This operation is linear time in the size of the range and only
1314 * invalidates iterators/references to the element being removed.
1315 * The user is also cautioned that this function only erases the
1316 * elements, and that if the elements themselves are pointers, the
1317 * pointed-to memory is not touched in any way. Managing the pointer
1318 * is the user's responsibility.
1320 iterator
1321 #if __cplusplus >= 201103L
1322 erase(const_iterator __first, const_iterator __last) noexcept
1323 #else
1324 erase(iterator __first, iterator __last)
1325 #endif
1327 while (__first != __last)
1328 __first = erase(__first);
1329 return __last._M_const_cast();
1333 * @brief Swaps data with another %list.
1334 * @param __x A %list of the same element and allocator types.
1336 * This exchanges the elements between two lists in constant
1337 * time. Note that the global std::swap() function is
1338 * specialized such that std::swap(l1,l2) will feed to this
1339 * function.
1341 void
1342 swap(list& __x)
1344 __detail::_List_node_base::swap(this->_M_impl._M_node,
1345 __x._M_impl._M_node);
1347 size_t __xsize = __x._M_get_size();
1348 __x._M_set_size(this->_M_get_size());
1349 this->_M_set_size(__xsize);
1351 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1352 // 431. Swapping containers with unequal allocators.
1353 std::__alloc_swap<typename _Base::_Node_alloc_type>::
1354 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator());
1358 * Erases all the elements. Note that this function only erases
1359 * the elements, and that if the elements themselves are
1360 * pointers, the pointed-to memory is not touched in any way.
1361 * Managing the pointer is the user's responsibility.
1363 void
1364 clear() _GLIBCXX_NOEXCEPT
1366 _Base::_M_clear();
1367 _Base::_M_init();
1370 // [23.2.2.4] list operations
1372 * @brief Insert contents of another %list.
1373 * @param __position Iterator referencing the element to insert before.
1374 * @param __x Source list.
1376 * The elements of @a __x are inserted in constant time in front of
1377 * the element referenced by @a __position. @a __x becomes an empty
1378 * list.
1380 * Requires this != @a __x.
1382 void
1383 #if __cplusplus >= 201103L
1384 splice(const_iterator __position, list&& __x) noexcept
1385 #else
1386 splice(iterator __position, list& __x)
1387 #endif
1389 if (!__x.empty())
1391 _M_check_equal_allocators(__x);
1393 this->_M_transfer(__position._M_const_cast(),
1394 __x.begin(), __x.end());
1396 this->_M_inc_size(__x._M_get_size());
1397 __x._M_set_size(0);
1401 #if __cplusplus >= 201103L
1402 void
1403 splice(const_iterator __position, list& __x) noexcept
1404 { splice(__position, std::move(__x)); }
1405 #endif
1407 #if __cplusplus >= 201103L
1409 * @brief Insert element from another %list.
1410 * @param __position Const_iterator referencing the element to
1411 * insert before.
1412 * @param __x Source list.
1413 * @param __i Const_iterator referencing the element to move.
1415 * Removes the element in list @a __x referenced by @a __i and
1416 * inserts it into the current list before @a __position.
1418 void
1419 splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
1420 #else
1422 * @brief Insert element from another %list.
1423 * @param __position Iterator referencing the element to insert before.
1424 * @param __x Source list.
1425 * @param __i Iterator referencing the element to move.
1427 * Removes the element in list @a __x referenced by @a __i and
1428 * inserts it into the current list before @a __position.
1430 void
1431 splice(iterator __position, list& __x, iterator __i)
1432 #endif
1434 iterator __j = __i._M_const_cast();
1435 ++__j;
1436 if (__position == __i || __position == __j)
1437 return;
1439 if (this != &__x)
1440 _M_check_equal_allocators(__x);
1442 this->_M_transfer(__position._M_const_cast(),
1443 __i._M_const_cast(), __j);
1445 this->_M_inc_size(1);
1446 __x._M_dec_size(1);
1449 #if __cplusplus >= 201103L
1451 * @brief Insert element from another %list.
1452 * @param __position Const_iterator referencing the element to
1453 * insert before.
1454 * @param __x Source list.
1455 * @param __i Const_iterator referencing the element to move.
1457 * Removes the element in list @a __x referenced by @a __i and
1458 * inserts it into the current list before @a __position.
1460 void
1461 splice(const_iterator __position, list& __x, const_iterator __i) noexcept
1462 { splice(__position, std::move(__x), __i); }
1463 #endif
1465 #if __cplusplus >= 201103L
1467 * @brief Insert range from another %list.
1468 * @param __position Const_iterator referencing the element to
1469 * insert before.
1470 * @param __x Source list.
1471 * @param __first Const_iterator referencing the start of range in x.
1472 * @param __last Const_iterator referencing the end of range in x.
1474 * Removes elements in the range [__first,__last) and inserts them
1475 * before @a __position in constant time.
1477 * Undefined if @a __position is in [__first,__last).
1479 void
1480 splice(const_iterator __position, list&& __x, const_iterator __first,
1481 const_iterator __last) noexcept
1482 #else
1484 * @brief Insert range from another %list.
1485 * @param __position Iterator referencing the element to insert before.
1486 * @param __x Source list.
1487 * @param __first Iterator referencing the start of range in x.
1488 * @param __last Iterator referencing the end of range in x.
1490 * Removes elements in the range [__first,__last) and inserts them
1491 * before @a __position in constant time.
1493 * Undefined if @a __position is in [__first,__last).
1495 void
1496 splice(iterator __position, list& __x, iterator __first,
1497 iterator __last)
1498 #endif
1500 if (__first != __last)
1502 if (this != &__x)
1503 _M_check_equal_allocators(__x);
1505 size_t __n = this->_M_distance(__first._M_node, __last._M_node);
1506 this->_M_inc_size(__n);
1507 __x._M_dec_size(__n);
1509 this->_M_transfer(__position._M_const_cast(),
1510 __first._M_const_cast(),
1511 __last._M_const_cast());
1515 #if __cplusplus >= 201103L
1517 * @brief Insert range from another %list.
1518 * @param __position Const_iterator referencing the element to
1519 * insert before.
1520 * @param __x Source list.
1521 * @param __first Const_iterator referencing the start of range in x.
1522 * @param __last Const_iterator referencing the end of range in x.
1524 * Removes elements in the range [__first,__last) and inserts them
1525 * before @a __position in constant time.
1527 * Undefined if @a __position is in [__first,__last).
1529 void
1530 splice(const_iterator __position, list& __x, const_iterator __first,
1531 const_iterator __last) noexcept
1532 { splice(__position, std::move(__x), __first, __last); }
1533 #endif
1536 * @brief Remove all elements equal to value.
1537 * @param __value The value to remove.
1539 * Removes every element in the list equal to @a value.
1540 * Remaining elements stay in list order. Note that this
1541 * function only erases the elements, and that if the elements
1542 * themselves are pointers, the pointed-to memory is not
1543 * touched in any way. Managing the pointer is the user's
1544 * responsibility.
1546 void
1547 remove(const _Tp& __value);
1550 * @brief Remove all elements satisfying a predicate.
1551 * @tparam _Predicate Unary predicate function or object.
1553 * Removes every element in the list for which the predicate
1554 * returns true. Remaining elements stay in list order. Note
1555 * that this function only erases the elements, and that if the
1556 * elements themselves are pointers, the pointed-to memory is
1557 * not touched in any way. Managing the pointer is the user's
1558 * responsibility.
1560 template<typename _Predicate>
1561 void
1562 remove_if(_Predicate);
1565 * @brief Remove consecutive duplicate elements.
1567 * For each consecutive set of elements with the same value,
1568 * remove all but the first one. Remaining elements stay in
1569 * list order. Note that this function only erases the
1570 * elements, and that if the elements themselves are pointers,
1571 * the pointed-to memory is not touched in any way. Managing
1572 * the pointer is the user's responsibility.
1574 void
1575 unique();
1578 * @brief Remove consecutive elements satisfying a predicate.
1579 * @tparam _BinaryPredicate Binary predicate function or object.
1581 * For each consecutive set of elements [first,last) that
1582 * satisfy predicate(first,i) where i is an iterator in
1583 * [first,last), remove all but the first one. Remaining
1584 * elements stay in list order. Note that this function only
1585 * erases the elements, and that if the elements themselves are
1586 * pointers, the pointed-to memory is not touched in any way.
1587 * Managing the pointer is the user's responsibility.
1589 template<typename _BinaryPredicate>
1590 void
1591 unique(_BinaryPredicate);
1594 * @brief Merge sorted lists.
1595 * @param __x Sorted list to merge.
1597 * Assumes that both @a __x and this list are sorted according to
1598 * operator<(). Merges elements of @a __x into this list in
1599 * sorted order, leaving @a __x empty when complete. Elements in
1600 * this list precede elements in @a __x that are equal.
1602 #if __cplusplus >= 201103L
1603 void
1604 merge(list&& __x);
1606 void
1607 merge(list& __x)
1608 { merge(std::move(__x)); }
1609 #else
1610 void
1611 merge(list& __x);
1612 #endif
1615 * @brief Merge sorted lists according to comparison function.
1616 * @tparam _StrictWeakOrdering Comparison function defining
1617 * sort order.
1618 * @param __x Sorted list to merge.
1619 * @param __comp Comparison functor.
1621 * Assumes that both @a __x and this list are sorted according to
1622 * StrictWeakOrdering. Merges elements of @a __x into this list
1623 * in sorted order, leaving @a __x empty when complete. Elements
1624 * in this list precede elements in @a __x that are equivalent
1625 * according to StrictWeakOrdering().
1627 #if __cplusplus >= 201103L
1628 template<typename _StrictWeakOrdering>
1629 void
1630 merge(list&& __x, _StrictWeakOrdering __comp);
1632 template<typename _StrictWeakOrdering>
1633 void
1634 merge(list& __x, _StrictWeakOrdering __comp)
1635 { merge(std::move(__x), __comp); }
1636 #else
1637 template<typename _StrictWeakOrdering>
1638 void
1639 merge(list& __x, _StrictWeakOrdering __comp);
1640 #endif
1643 * @brief Reverse the elements in list.
1645 * Reverse the order of elements in the list in linear time.
1647 void
1648 reverse() _GLIBCXX_NOEXCEPT
1649 { this->_M_impl._M_node._M_reverse(); }
1652 * @brief Sort the elements.
1654 * Sorts the elements of this list in NlogN time. Equivalent
1655 * elements remain in list order.
1657 void
1658 sort();
1661 * @brief Sort the elements according to comparison function.
1663 * Sorts the elements of this list in NlogN time. Equivalent
1664 * elements remain in list order.
1666 template<typename _StrictWeakOrdering>
1667 void
1668 sort(_StrictWeakOrdering);
1670 protected:
1671 // Internal constructor functions follow.
1673 // Called by the range constructor to implement [23.1.1]/9
1675 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1676 // 438. Ambiguity in the "do the right thing" clause
1677 template<typename _Integer>
1678 void
1679 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1680 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1682 // Called by the range constructor to implement [23.1.1]/9
1683 template<typename _InputIterator>
1684 void
1685 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1686 __false_type)
1688 for (; __first != __last; ++__first)
1689 #if __cplusplus >= 201103L
1690 emplace_back(*__first);
1691 #else
1692 push_back(*__first);
1693 #endif
1696 // Called by list(n,v,a), and the range constructor when it turns out
1697 // to be the same thing.
1698 void
1699 _M_fill_initialize(size_type __n, const value_type& __x)
1701 for (; __n; --__n)
1702 push_back(__x);
1705 #if __cplusplus >= 201103L
1706 // Called by list(n).
1707 void
1708 _M_default_initialize(size_type __n)
1710 for (; __n; --__n)
1711 emplace_back();
1714 // Called by resize(sz).
1715 void
1716 _M_default_append(size_type __n);
1717 #endif
1719 // Internal assign functions follow.
1721 // Called by the range assign to implement [23.1.1]/9
1723 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1724 // 438. Ambiguity in the "do the right thing" clause
1725 template<typename _Integer>
1726 void
1727 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1728 { _M_fill_assign(__n, __val); }
1730 // Called by the range assign to implement [23.1.1]/9
1731 template<typename _InputIterator>
1732 void
1733 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1734 __false_type);
1736 // Called by assign(n,t), and the range assign when it turns out
1737 // to be the same thing.
1738 void
1739 _M_fill_assign(size_type __n, const value_type& __val);
1742 // Moves the elements from [first,last) before position.
1743 void
1744 _M_transfer(iterator __position, iterator __first, iterator __last)
1745 { __position._M_node->_M_transfer(__first._M_node, __last._M_node); }
1747 // Inserts new element at position given and with value given.
1748 #if __cplusplus < 201103L
1749 void
1750 _M_insert(iterator __position, const value_type& __x)
1752 _Node* __tmp = _M_create_node(__x);
1753 __tmp->_M_hook(__position._M_node);
1754 this->_M_inc_size(1);
1756 #else
1757 template<typename... _Args>
1758 void
1759 _M_insert(iterator __position, _Args&&... __args)
1761 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
1762 __tmp->_M_hook(__position._M_node);
1763 this->_M_inc_size(1);
1765 #endif
1767 // Erases element at position given.
1768 void
1769 _M_erase(iterator __position) _GLIBCXX_NOEXCEPT
1771 this->_M_dec_size(1);
1772 __position._M_node->_M_unhook();
1773 _Node* __n = static_cast<_Node*>(__position._M_node);
1774 #if __cplusplus >= 201103L
1775 _M_get_Node_allocator().destroy(__n);
1776 #else
1777 _M_get_Tp_allocator().destroy(std::__addressof(__n->_M_data));
1778 #endif
1779 _M_put_node(__n);
1782 // To implement the splice (and merge) bits of N1599.
1783 void
1784 _M_check_equal_allocators(list& __x) _GLIBCXX_NOEXCEPT
1786 if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
1787 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
1788 __builtin_abort();
1793 * @brief List equality comparison.
1794 * @param __x A %list.
1795 * @param __y A %list of the same type as @a __x.
1796 * @return True iff the size and elements of the lists are equal.
1798 * This is an equivalence relation. It is linear in the size of
1799 * the lists. Lists are considered equivalent if their sizes are
1800 * equal, and if corresponding elements compare equal.
1802 template<typename _Tp, typename _Alloc>
1803 inline bool
1804 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1806 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1807 const_iterator __end1 = __x.end();
1808 const_iterator __end2 = __y.end();
1810 const_iterator __i1 = __x.begin();
1811 const_iterator __i2 = __y.begin();
1812 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1814 ++__i1;
1815 ++__i2;
1817 return __i1 == __end1 && __i2 == __end2;
1821 * @brief List ordering relation.
1822 * @param __x A %list.
1823 * @param __y A %list of the same type as @a __x.
1824 * @return True iff @a __x is lexicographically less than @a __y.
1826 * This is a total ordering relation. It is linear in the size of the
1827 * lists. The elements must be comparable with @c <.
1829 * See std::lexicographical_compare() for how the determination is made.
1831 template<typename _Tp, typename _Alloc>
1832 inline bool
1833 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1834 { return std::lexicographical_compare(__x.begin(), __x.end(),
1835 __y.begin(), __y.end()); }
1837 /// Based on operator==
1838 template<typename _Tp, typename _Alloc>
1839 inline bool
1840 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1841 { return !(__x == __y); }
1843 /// Based on operator<
1844 template<typename _Tp, typename _Alloc>
1845 inline bool
1846 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1847 { return __y < __x; }
1849 /// Based on operator<
1850 template<typename _Tp, typename _Alloc>
1851 inline bool
1852 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1853 { return !(__y < __x); }
1855 /// Based on operator<
1856 template<typename _Tp, typename _Alloc>
1857 inline bool
1858 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1859 { return !(__x < __y); }
1861 /// See std::list::swap().
1862 template<typename _Tp, typename _Alloc>
1863 inline void
1864 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1865 { __x.swap(__y); }
1867 _GLIBCXX_END_NAMESPACE_CONTAINER
1868 } // namespace std
1870 #endif /* _STL_LIST_H */