2017-08-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_list.h
blobbe5bb5e5b68dc45430a114a6670ce875f0c70f4c
1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001-2017 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 #include <ext/alloc_traits.h>
61 #if __cplusplus >= 201103L
62 #include <initializer_list>
63 #include <bits/allocated_ptr.h>
64 #include <ext/aligned_buffer.h>
65 #endif
67 namespace std _GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 namespace __detail
73 // Supporting structures are split into common and templated
74 // types; the latter publicly inherits from the former in an
75 // effort to reduce code duplication. This results in some
76 // "needless" static_cast'ing later on, but it's all safe
77 // downcasting.
79 /// Common part of a node in the %list.
80 struct _List_node_base
82 _List_node_base* _M_next;
83 _List_node_base* _M_prev;
85 static void
86 swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT;
88 void
89 _M_transfer(_List_node_base* const __first,
90 _List_node_base* const __last) _GLIBCXX_USE_NOEXCEPT;
92 void
93 _M_reverse() _GLIBCXX_USE_NOEXCEPT;
95 void
96 _M_hook(_List_node_base* const __position) _GLIBCXX_USE_NOEXCEPT;
98 void
99 _M_unhook() _GLIBCXX_USE_NOEXCEPT;
102 /// The %list node header.
103 struct _List_node_header : public _List_node_base
105 #if _GLIBCXX_USE_CXX11_ABI
106 std::size_t _M_size;
107 #endif
109 _List_node_header() _GLIBCXX_NOEXCEPT
110 { _M_init(); }
112 #if __cplusplus >= 201103L
113 _List_node_header(_List_node_header&& __x) noexcept
114 : _List_node_base{ __x._M_next, __x._M_prev }
115 # if _GLIBCXX_USE_CXX11_ABI
116 , _M_size(__x._M_size)
117 # endif
119 if (__x._M_base()->_M_next == __x._M_base())
120 this->_M_next = this->_M_prev = this;
121 else
123 this->_M_next->_M_prev = this->_M_prev->_M_next = this->_M_base();
124 __x._M_init();
128 void
129 _M_move_nodes(_List_node_header&& __x)
131 _List_node_base* const __xnode = __x._M_base();
132 if (__xnode->_M_next == __xnode)
133 _M_init();
134 else
136 _List_node_base* const __node = this->_M_base();
137 __node->_M_next = __xnode->_M_next;
138 __node->_M_prev = __xnode->_M_prev;
139 __node->_M_next->_M_prev = __node->_M_prev->_M_next = __node;
140 # if _GLIBCXX_USE_CXX11_ABI
141 _M_size = __x._M_size;
142 # endif
143 __x._M_init();
146 #endif
148 void
149 _M_init() _GLIBCXX_NOEXCEPT
151 this->_M_next = this->_M_prev = this;
152 #if _GLIBCXX_USE_CXX11_ABI
153 this->_M_size = 0;
154 #endif
157 private:
158 _List_node_base* _M_base() { return this; }
160 } // namespace detail
162 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
164 /// An actual node in the %list.
165 template<typename _Tp>
166 struct _List_node : public __detail::_List_node_base
168 #if __cplusplus >= 201103L
169 __gnu_cxx::__aligned_membuf<_Tp> _M_storage;
170 _Tp* _M_valptr() { return _M_storage._M_ptr(); }
171 _Tp const* _M_valptr() const { return _M_storage._M_ptr(); }
172 #else
173 _Tp _M_data;
174 _Tp* _M_valptr() { return std::__addressof(_M_data); }
175 _Tp const* _M_valptr() const { return std::__addressof(_M_data); }
176 #endif
180 * @brief A list::iterator.
182 * All the functions are op overloads.
184 template<typename _Tp>
185 struct _List_iterator
187 typedef _List_iterator<_Tp> _Self;
188 typedef _List_node<_Tp> _Node;
190 typedef ptrdiff_t difference_type;
191 typedef std::bidirectional_iterator_tag iterator_category;
192 typedef _Tp value_type;
193 typedef _Tp* pointer;
194 typedef _Tp& reference;
196 _List_iterator() _GLIBCXX_NOEXCEPT
197 : _M_node() { }
199 explicit
200 _List_iterator(__detail::_List_node_base* __x) _GLIBCXX_NOEXCEPT
201 : _M_node(__x) { }
203 _Self
204 _M_const_cast() const _GLIBCXX_NOEXCEPT
205 { return *this; }
207 // Must downcast from _List_node_base to _List_node to get to value.
208 reference
209 operator*() const _GLIBCXX_NOEXCEPT
210 { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
212 pointer
213 operator->() const _GLIBCXX_NOEXCEPT
214 { return static_cast<_Node*>(_M_node)->_M_valptr(); }
216 _Self&
217 operator++() _GLIBCXX_NOEXCEPT
219 _M_node = _M_node->_M_next;
220 return *this;
223 _Self
224 operator++(int) _GLIBCXX_NOEXCEPT
226 _Self __tmp = *this;
227 _M_node = _M_node->_M_next;
228 return __tmp;
231 _Self&
232 operator--() _GLIBCXX_NOEXCEPT
234 _M_node = _M_node->_M_prev;
235 return *this;
238 _Self
239 operator--(int) _GLIBCXX_NOEXCEPT
241 _Self __tmp = *this;
242 _M_node = _M_node->_M_prev;
243 return __tmp;
246 bool
247 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
248 { return _M_node == __x._M_node; }
250 bool
251 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
252 { return _M_node != __x._M_node; }
254 // The only member points to the %list element.
255 __detail::_List_node_base* _M_node;
259 * @brief A list::const_iterator.
261 * All the functions are op overloads.
263 template<typename _Tp>
264 struct _List_const_iterator
266 typedef _List_const_iterator<_Tp> _Self;
267 typedef const _List_node<_Tp> _Node;
268 typedef _List_iterator<_Tp> iterator;
270 typedef ptrdiff_t difference_type;
271 typedef std::bidirectional_iterator_tag iterator_category;
272 typedef _Tp value_type;
273 typedef const _Tp* pointer;
274 typedef const _Tp& reference;
276 _List_const_iterator() _GLIBCXX_NOEXCEPT
277 : _M_node() { }
279 explicit
280 _List_const_iterator(const __detail::_List_node_base* __x)
281 _GLIBCXX_NOEXCEPT
282 : _M_node(__x) { }
284 _List_const_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
285 : _M_node(__x._M_node) { }
287 iterator
288 _M_const_cast() const _GLIBCXX_NOEXCEPT
289 { return iterator(const_cast<__detail::_List_node_base*>(_M_node)); }
291 // Must downcast from List_node_base to _List_node to get to value.
292 reference
293 operator*() const _GLIBCXX_NOEXCEPT
294 { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
296 pointer
297 operator->() const _GLIBCXX_NOEXCEPT
298 { return static_cast<_Node*>(_M_node)->_M_valptr(); }
300 _Self&
301 operator++() _GLIBCXX_NOEXCEPT
303 _M_node = _M_node->_M_next;
304 return *this;
307 _Self
308 operator++(int) _GLIBCXX_NOEXCEPT
310 _Self __tmp = *this;
311 _M_node = _M_node->_M_next;
312 return __tmp;
315 _Self&
316 operator--() _GLIBCXX_NOEXCEPT
318 _M_node = _M_node->_M_prev;
319 return *this;
322 _Self
323 operator--(int) _GLIBCXX_NOEXCEPT
325 _Self __tmp = *this;
326 _M_node = _M_node->_M_prev;
327 return __tmp;
330 bool
331 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
332 { return _M_node == __x._M_node; }
334 bool
335 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
336 { return _M_node != __x._M_node; }
338 // The only member points to the %list element.
339 const __detail::_List_node_base* _M_node;
342 template<typename _Val>
343 inline bool
344 operator==(const _List_iterator<_Val>& __x,
345 const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
346 { return __x._M_node == __y._M_node; }
348 template<typename _Val>
349 inline bool
350 operator!=(const _List_iterator<_Val>& __x,
351 const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
352 { return __x._M_node != __y._M_node; }
354 _GLIBCXX_BEGIN_NAMESPACE_CXX11
355 /// See bits/stl_deque.h's _Deque_base for an explanation.
356 template<typename _Tp, typename _Alloc>
357 class _List_base
359 protected:
360 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
361 rebind<_Tp>::other _Tp_alloc_type;
362 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tp_alloc_traits;
363 typedef typename _Tp_alloc_traits::template
364 rebind<_List_node<_Tp> >::other _Node_alloc_type;
365 typedef __gnu_cxx::__alloc_traits<_Node_alloc_type> _Node_alloc_traits;
367 #if !_GLIBCXX_INLINE_VERSION
368 static size_t
369 _S_distance(const __detail::_List_node_base* __first,
370 const __detail::_List_node_base* __last)
372 size_t __n = 0;
373 while (__first != __last)
375 __first = __first->_M_next;
376 ++__n;
378 return __n;
380 #endif
382 struct _List_impl
383 : public _Node_alloc_type
385 __detail::_List_node_header _M_node;
387 _List_impl() _GLIBCXX_NOEXCEPT_IF( noexcept(_Node_alloc_type()) )
388 : _Node_alloc_type()
391 _List_impl(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
392 : _Node_alloc_type(__a)
395 #if __cplusplus >= 201103L
396 _List_impl(_List_impl&&) = default;
398 _List_impl(_Node_alloc_type&& __a, _List_impl&& __x)
399 : _Node_alloc_type(std::move(__a)), _M_node(std::move(__x._M_node))
402 _List_impl(_Node_alloc_type&& __a) noexcept
403 : _Node_alloc_type(std::move(__a))
405 #endif
408 _List_impl _M_impl;
410 #if _GLIBCXX_USE_CXX11_ABI
411 size_t _M_get_size() const { return _M_impl._M_node._M_size; }
413 void _M_set_size(size_t __n) { _M_impl._M_node._M_size = __n; }
415 void _M_inc_size(size_t __n) { _M_impl._M_node._M_size += __n; }
417 void _M_dec_size(size_t __n) { _M_impl._M_node._M_size -= __n; }
419 # if !_GLIBCXX_INLINE_VERSION
420 size_t
421 _M_distance(const __detail::_List_node_base* __first,
422 const __detail::_List_node_base* __last) const
423 { return _S_distance(__first, __last); }
425 // return the stored size
426 size_t _M_node_count() const { return _M_get_size(); }
427 # endif
428 #else
429 // dummy implementations used when the size is not stored
430 size_t _M_get_size() const { return 0; }
431 void _M_set_size(size_t) { }
432 void _M_inc_size(size_t) { }
433 void _M_dec_size(size_t) { }
435 # if !_GLIBCXX_INLINE_VERSION
436 size_t _M_distance(const void*, const void*) const { return 0; }
438 // count the number of nodes
439 size_t _M_node_count() const
441 return _S_distance(_M_impl._M_node._M_next,
442 std::__addressof(_M_impl._M_node));
444 # endif
445 #endif
447 typename _Node_alloc_traits::pointer
448 _M_get_node()
449 { return _Node_alloc_traits::allocate(_M_impl, 1); }
451 void
452 _M_put_node(typename _Node_alloc_traits::pointer __p) _GLIBCXX_NOEXCEPT
453 { _Node_alloc_traits::deallocate(_M_impl, __p, 1); }
455 public:
456 typedef _Alloc allocator_type;
458 _Node_alloc_type&
459 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
460 { return _M_impl; }
462 const _Node_alloc_type&
463 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
464 { return _M_impl; }
466 #if __cplusplus >= 201103L
467 _List_base() = default;
468 #else
469 _List_base() { }
470 #endif
472 _List_base(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
473 : _M_impl(__a)
476 #if __cplusplus >= 201103L
477 _List_base(_List_base&&) = default;
479 # if !_GLIBCXX_INLINE_VERSION
480 _List_base(_List_base&& __x, _Node_alloc_type&& __a)
481 : _M_impl(std::move(__a))
483 if (__x._M_get_Node_allocator() == _M_get_Node_allocator())
484 _M_move_nodes(std::move(__x));
485 // else caller must move individual elements.
487 # endif
489 // Used when allocator is_always_equal.
490 _List_base(_Node_alloc_type&& __a, _List_base&& __x)
491 : _M_impl(std::move(__a), std::move(__x._M_impl))
494 // Used when allocator !is_always_equal.
495 _List_base(_Node_alloc_type&& __a)
496 : _M_impl(std::move(__a))
499 void
500 _M_move_nodes(_List_base&& __x)
501 { _M_impl._M_node._M_move_nodes(std::move(__x._M_impl._M_node)); }
502 #endif
504 // This is what actually destroys the list.
505 ~_List_base() _GLIBCXX_NOEXCEPT
506 { _M_clear(); }
508 void
509 _M_clear() _GLIBCXX_NOEXCEPT;
511 void
512 _M_init() _GLIBCXX_NOEXCEPT
513 { this->_M_impl._M_node._M_init(); }
517 * @brief A standard container with linear time access to elements,
518 * and fixed time insertion/deletion at any point in the sequence.
520 * @ingroup sequences
522 * @tparam _Tp Type of element.
523 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
525 * Meets the requirements of a <a href="tables.html#65">container</a>, a
526 * <a href="tables.html#66">reversible container</a>, and a
527 * <a href="tables.html#67">sequence</a>, including the
528 * <a href="tables.html#68">optional sequence requirements</a> with the
529 * %exception of @c at and @c operator[].
531 * This is a @e doubly @e linked %list. Traversal up and down the
532 * %list requires linear time, but adding and removing elements (or
533 * @e nodes) is done in constant time, regardless of where the
534 * change takes place. Unlike std::vector and std::deque,
535 * random-access iterators are not provided, so subscripting ( @c
536 * [] ) access is not allowed. For algorithms which only need
537 * sequential access, this lack makes no difference.
539 * Also unlike the other standard containers, std::list provides
540 * specialized algorithms %unique to linked lists, such as
541 * splicing, sorting, and in-place reversal.
543 * A couple points on memory allocation for list<Tp>:
545 * First, we never actually allocate a Tp, we allocate
546 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
547 * that after elements from %list<X,Alloc1> are spliced into
548 * %list<X,Alloc2>, destroying the memory of the second %list is a
549 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
551 * Second, a %list conceptually represented as
552 * @code
553 * A <---> B <---> C <---> D
554 * @endcode
555 * is actually circular; a link exists between A and D. The %list
556 * class holds (as its only data member) a private list::iterator
557 * pointing to @e D, not to @e A! To get to the head of the %list,
558 * we start at the tail and move forward by one. When this member
559 * iterator's next/previous pointers refer to itself, the %list is
560 * %empty.
562 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
563 class list : protected _List_base<_Tp, _Alloc>
565 #ifdef _GLIBCXX_CONCEPT_CHECKS
566 // concept requirements
567 typedef typename _Alloc::value_type _Alloc_value_type;
568 # if __cplusplus < 201103L
569 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
570 # endif
571 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
572 #endif
574 typedef _List_base<_Tp, _Alloc> _Base;
575 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
576 typedef typename _Base::_Tp_alloc_traits _Tp_alloc_traits;
577 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
578 typedef typename _Base::_Node_alloc_traits _Node_alloc_traits;
580 public:
581 typedef _Tp value_type;
582 typedef typename _Tp_alloc_traits::pointer pointer;
583 typedef typename _Tp_alloc_traits::const_pointer const_pointer;
584 typedef typename _Tp_alloc_traits::reference reference;
585 typedef typename _Tp_alloc_traits::const_reference const_reference;
586 typedef _List_iterator<_Tp> iterator;
587 typedef _List_const_iterator<_Tp> const_iterator;
588 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
589 typedef std::reverse_iterator<iterator> reverse_iterator;
590 typedef size_t size_type;
591 typedef ptrdiff_t difference_type;
592 typedef _Alloc allocator_type;
594 protected:
595 // Note that pointers-to-_Node's can be ctor-converted to
596 // iterator types.
597 typedef _List_node<_Tp> _Node;
599 using _Base::_M_impl;
600 using _Base::_M_put_node;
601 using _Base::_M_get_node;
602 using _Base::_M_get_Node_allocator;
605 * @param __args An instance of user data.
607 * Allocates space for a new node and constructs a copy of
608 * @a __args in it.
610 #if __cplusplus < 201103L
611 _Node*
612 _M_create_node(const value_type& __x)
614 _Node* __p = this->_M_get_node();
615 __try
617 _Tp_alloc_type __alloc(_M_get_Node_allocator());
618 __alloc.construct(__p->_M_valptr(), __x);
620 __catch(...)
622 _M_put_node(__p);
623 __throw_exception_again;
625 return __p;
627 #else
628 template<typename... _Args>
629 _Node*
630 _M_create_node(_Args&&... __args)
632 auto __p = this->_M_get_node();
633 auto& __alloc = _M_get_Node_allocator();
634 __allocated_ptr<_Node_alloc_type> __guard{__alloc, __p};
635 _Node_alloc_traits::construct(__alloc, __p->_M_valptr(),
636 std::forward<_Args>(__args)...);
637 __guard = nullptr;
638 return __p;
640 #endif
642 #if _GLIBCXX_USE_CXX11_ABI
643 static size_t
644 _S_distance(const_iterator __first, const_iterator __last)
645 { return std::distance(__first, __last); }
647 // return the stored size
648 size_t
649 _M_node_count() const
650 { return this->_M_get_size(); }
651 #else
652 // dummy implementations used when the size is not stored
653 static size_t
654 _S_distance(const_iterator, const_iterator)
655 { return 0; }
657 // count the number of nodes
658 size_t
659 _M_node_count() const
660 { return std::distance(begin(), end()); }
661 #endif
663 public:
664 // [23.2.2.1] construct/copy/destroy
665 // (assign() and get_allocator() are also listed in this section)
668 * @brief Creates a %list with no elements.
670 #if __cplusplus >= 201103L
671 list() = default;
672 #else
673 list() { }
674 #endif
677 * @brief Creates a %list with no elements.
678 * @param __a An allocator object.
680 explicit
681 list(const allocator_type& __a) _GLIBCXX_NOEXCEPT
682 : _Base(_Node_alloc_type(__a)) { }
684 #if __cplusplus >= 201103L
686 * @brief Creates a %list with default constructed elements.
687 * @param __n The number of elements to initially create.
688 * @param __a An allocator object.
690 * This constructor fills the %list with @a __n default
691 * constructed elements.
693 explicit
694 list(size_type __n, const allocator_type& __a = allocator_type())
695 : _Base(_Node_alloc_type(__a))
696 { _M_default_initialize(__n); }
699 * @brief Creates a %list with copies of an exemplar element.
700 * @param __n The number of elements to initially create.
701 * @param __value An element to copy.
702 * @param __a An allocator object.
704 * This constructor fills the %list with @a __n copies of @a __value.
706 list(size_type __n, const value_type& __value,
707 const allocator_type& __a = allocator_type())
708 : _Base(_Node_alloc_type(__a))
709 { _M_fill_initialize(__n, __value); }
710 #else
712 * @brief Creates a %list with copies of an exemplar element.
713 * @param __n The number of elements to initially create.
714 * @param __value An element to copy.
715 * @param __a An allocator object.
717 * This constructor fills the %list with @a __n copies of @a __value.
719 explicit
720 list(size_type __n, const value_type& __value = value_type(),
721 const allocator_type& __a = allocator_type())
722 : _Base(_Node_alloc_type(__a))
723 { _M_fill_initialize(__n, __value); }
724 #endif
727 * @brief %List copy constructor.
728 * @param __x A %list of identical element and allocator types.
730 * The newly-created %list uses a copy of the allocation object used
731 * by @a __x (unless the allocator traits dictate a different object).
733 list(const list& __x)
734 : _Base(_Node_alloc_traits::
735 _S_select_on_copy(__x._M_get_Node_allocator()))
736 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
738 #if __cplusplus >= 201103L
740 * @brief %List move constructor.
742 * The newly-created %list contains the exact contents of the moved
743 * instance. The contents of the moved instance are a valid, but
744 * unspecified %list.
746 list(list&&) = default;
749 * @brief Builds a %list from an initializer_list
750 * @param __l An initializer_list of value_type.
751 * @param __a An allocator object.
753 * Create a %list consisting of copies of the elements in the
754 * initializer_list @a __l. This is linear in __l.size().
756 list(initializer_list<value_type> __l,
757 const allocator_type& __a = allocator_type())
758 : _Base(_Node_alloc_type(__a))
759 { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
761 list(const list& __x, const allocator_type& __a)
762 : _Base(_Node_alloc_type(__a))
763 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
765 private:
766 list(list&& __x, const allocator_type& __a, true_type) noexcept
767 : _Base(_Node_alloc_type(__a), std::move(__x))
770 list(list&& __x, const allocator_type& __a, false_type)
771 : _Base(_Node_alloc_type(__a))
773 if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
774 this->_M_move_nodes(std::move(__x));
775 else
776 insert(begin(), std::__make_move_if_noexcept_iterator(__x.begin()),
777 std::__make_move_if_noexcept_iterator(__x.end()));
780 public:
781 list(list&& __x, const allocator_type& __a)
782 noexcept(_Node_alloc_traits::_S_always_equal())
783 : list(std::move(__x), __a,
784 typename _Node_alloc_traits::is_always_equal{})
786 #endif
789 * @brief Builds a %list from a range.
790 * @param __first An input iterator.
791 * @param __last An input iterator.
792 * @param __a An allocator object.
794 * Create a %list consisting of copies of the elements from
795 * [@a __first,@a __last). This is linear in N (where N is
796 * distance(@a __first,@a __last)).
798 #if __cplusplus >= 201103L
799 template<typename _InputIterator,
800 typename = std::_RequireInputIter<_InputIterator>>
801 list(_InputIterator __first, _InputIterator __last,
802 const allocator_type& __a = allocator_type())
803 : _Base(_Node_alloc_type(__a))
804 { _M_initialize_dispatch(__first, __last, __false_type()); }
805 #else
806 template<typename _InputIterator>
807 list(_InputIterator __first, _InputIterator __last,
808 const allocator_type& __a = allocator_type())
809 : _Base(_Node_alloc_type(__a))
811 // Check whether it's an integral type. If so, it's not an iterator.
812 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
813 _M_initialize_dispatch(__first, __last, _Integral());
815 #endif
817 #if __cplusplus >= 201103L
819 * No explicit dtor needed as the _Base dtor takes care of
820 * things. The _Base dtor only erases the elements, and note
821 * that if the elements themselves are pointers, the pointed-to
822 * memory is not touched in any way. Managing the pointer is
823 * the user's responsibility.
825 ~list() = default;
826 #endif
829 * @brief %List assignment operator.
830 * @param __x A %list of identical element and allocator types.
832 * All the elements of @a __x are copied.
834 * Whether the allocator is copied depends on the allocator traits.
836 list&
837 operator=(const list& __x);
839 #if __cplusplus >= 201103L
841 * @brief %List move assignment operator.
842 * @param __x A %list of identical element and allocator types.
844 * The contents of @a __x are moved into this %list (without copying).
846 * Afterwards @a __x is a valid, but unspecified %list
848 * Whether the allocator is moved depends on the allocator traits.
850 list&
851 operator=(list&& __x)
852 noexcept(_Node_alloc_traits::_S_nothrow_move())
854 constexpr bool __move_storage =
855 _Node_alloc_traits::_S_propagate_on_move_assign()
856 || _Node_alloc_traits::_S_always_equal();
857 _M_move_assign(std::move(__x), __bool_constant<__move_storage>());
858 return *this;
862 * @brief %List initializer list assignment operator.
863 * @param __l An initializer_list of value_type.
865 * Replace the contents of the %list with copies of the elements
866 * in the initializer_list @a __l. This is linear in l.size().
868 list&
869 operator=(initializer_list<value_type> __l)
871 this->assign(__l.begin(), __l.end());
872 return *this;
874 #endif
877 * @brief Assigns a given value to a %list.
878 * @param __n Number of elements to be assigned.
879 * @param __val Value to be assigned.
881 * This function fills a %list with @a __n copies of the given
882 * value. Note that the assignment completely changes the %list
883 * and that the resulting %list's size is the same as the number
884 * of elements assigned.
886 void
887 assign(size_type __n, const value_type& __val)
888 { _M_fill_assign(__n, __val); }
891 * @brief Assigns a range to a %list.
892 * @param __first An input iterator.
893 * @param __last An input iterator.
895 * This function fills a %list with copies of the elements in the
896 * range [@a __first,@a __last).
898 * Note that the assignment completely changes the %list and
899 * that the resulting %list's size is the same as the number of
900 * elements assigned.
902 #if __cplusplus >= 201103L
903 template<typename _InputIterator,
904 typename = std::_RequireInputIter<_InputIterator>>
905 void
906 assign(_InputIterator __first, _InputIterator __last)
907 { _M_assign_dispatch(__first, __last, __false_type()); }
908 #else
909 template<typename _InputIterator>
910 void
911 assign(_InputIterator __first, _InputIterator __last)
913 // Check whether it's an integral type. If so, it's not an iterator.
914 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
915 _M_assign_dispatch(__first, __last, _Integral());
917 #endif
919 #if __cplusplus >= 201103L
921 * @brief Assigns an initializer_list to a %list.
922 * @param __l An initializer_list of value_type.
924 * Replace the contents of the %list with copies of the elements
925 * in the initializer_list @a __l. This is linear in __l.size().
927 void
928 assign(initializer_list<value_type> __l)
929 { this->_M_assign_dispatch(__l.begin(), __l.end(), __false_type()); }
930 #endif
932 /// Get a copy of the memory allocation object.
933 allocator_type
934 get_allocator() const _GLIBCXX_NOEXCEPT
935 { return allocator_type(_Base::_M_get_Node_allocator()); }
937 // iterators
939 * Returns a read/write iterator that points to the first element in the
940 * %list. Iteration is done in ordinary element order.
942 iterator
943 begin() _GLIBCXX_NOEXCEPT
944 { return iterator(this->_M_impl._M_node._M_next); }
947 * Returns a read-only (constant) iterator that points to the
948 * first element in the %list. Iteration is done in ordinary
949 * element order.
951 const_iterator
952 begin() const _GLIBCXX_NOEXCEPT
953 { return const_iterator(this->_M_impl._M_node._M_next); }
956 * Returns a read/write iterator that points one past the last
957 * element in the %list. Iteration is done in ordinary element
958 * order.
960 iterator
961 end() _GLIBCXX_NOEXCEPT
962 { return iterator(&this->_M_impl._M_node); }
965 * Returns a read-only (constant) iterator that points one past
966 * the last element in the %list. Iteration is done in ordinary
967 * element order.
969 const_iterator
970 end() const _GLIBCXX_NOEXCEPT
971 { return const_iterator(&this->_M_impl._M_node); }
974 * Returns a read/write reverse iterator that points to the last
975 * element in the %list. Iteration is done in reverse element
976 * order.
978 reverse_iterator
979 rbegin() _GLIBCXX_NOEXCEPT
980 { return reverse_iterator(end()); }
983 * Returns a read-only (constant) reverse iterator that points to
984 * the last element in the %list. Iteration is done in reverse
985 * element order.
987 const_reverse_iterator
988 rbegin() const _GLIBCXX_NOEXCEPT
989 { return const_reverse_iterator(end()); }
992 * Returns a read/write reverse iterator that points to one
993 * before the first element in the %list. Iteration is done in
994 * reverse element order.
996 reverse_iterator
997 rend() _GLIBCXX_NOEXCEPT
998 { return reverse_iterator(begin()); }
1001 * Returns a read-only (constant) reverse iterator that points to one
1002 * before the first element in the %list. Iteration is done in reverse
1003 * element order.
1005 const_reverse_iterator
1006 rend() const _GLIBCXX_NOEXCEPT
1007 { return const_reverse_iterator(begin()); }
1009 #if __cplusplus >= 201103L
1011 * Returns a read-only (constant) iterator that points to the
1012 * first element in the %list. Iteration is done in ordinary
1013 * element order.
1015 const_iterator
1016 cbegin() const noexcept
1017 { return const_iterator(this->_M_impl._M_node._M_next); }
1020 * Returns a read-only (constant) iterator that points one past
1021 * the last element in the %list. Iteration is done in ordinary
1022 * element order.
1024 const_iterator
1025 cend() const noexcept
1026 { return const_iterator(&this->_M_impl._M_node); }
1029 * Returns a read-only (constant) reverse iterator that points to
1030 * the last element in the %list. Iteration is done in reverse
1031 * element order.
1033 const_reverse_iterator
1034 crbegin() const noexcept
1035 { return const_reverse_iterator(end()); }
1038 * Returns a read-only (constant) reverse iterator that points to one
1039 * before the first element in the %list. Iteration is done in reverse
1040 * element order.
1042 const_reverse_iterator
1043 crend() const noexcept
1044 { return const_reverse_iterator(begin()); }
1045 #endif
1047 // [23.2.2.2] capacity
1049 * Returns true if the %list is empty. (Thus begin() would equal
1050 * end().)
1052 bool
1053 empty() const _GLIBCXX_NOEXCEPT
1054 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
1056 /** Returns the number of elements in the %list. */
1057 size_type
1058 size() const _GLIBCXX_NOEXCEPT
1059 { return _M_node_count(); }
1061 /** Returns the size() of the largest possible %list. */
1062 size_type
1063 max_size() const _GLIBCXX_NOEXCEPT
1064 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1066 #if __cplusplus >= 201103L
1068 * @brief Resizes the %list to the specified number of elements.
1069 * @param __new_size Number of elements the %list should contain.
1071 * This function will %resize the %list to the specified number
1072 * of elements. If the number is smaller than the %list's
1073 * current size the %list is truncated, otherwise default
1074 * constructed elements are appended.
1076 void
1077 resize(size_type __new_size);
1080 * @brief Resizes the %list to the specified number of elements.
1081 * @param __new_size Number of elements the %list should contain.
1082 * @param __x Data with which new elements should be populated.
1084 * This function will %resize the %list to the specified number
1085 * of elements. If the number is smaller than the %list's
1086 * current size the %list is truncated, otherwise the %list is
1087 * extended and new elements are populated with given data.
1089 void
1090 resize(size_type __new_size, const value_type& __x);
1091 #else
1093 * @brief Resizes the %list to the specified number of elements.
1094 * @param __new_size Number of elements the %list should contain.
1095 * @param __x Data with which new elements should be populated.
1097 * This function will %resize the %list to the specified number
1098 * of elements. If the number is smaller than the %list's
1099 * current size the %list is truncated, otherwise the %list is
1100 * extended and new elements are populated with given data.
1102 void
1103 resize(size_type __new_size, value_type __x = value_type());
1104 #endif
1106 // element access
1108 * Returns a read/write reference to the data at the first
1109 * element of the %list.
1111 reference
1112 front() _GLIBCXX_NOEXCEPT
1113 { return *begin(); }
1116 * Returns a read-only (constant) reference to the data at the first
1117 * element of the %list.
1119 const_reference
1120 front() const _GLIBCXX_NOEXCEPT
1121 { return *begin(); }
1124 * Returns a read/write reference to the data at the last element
1125 * of the %list.
1127 reference
1128 back() _GLIBCXX_NOEXCEPT
1130 iterator __tmp = end();
1131 --__tmp;
1132 return *__tmp;
1136 * Returns a read-only (constant) reference to the data at the last
1137 * element of the %list.
1139 const_reference
1140 back() const _GLIBCXX_NOEXCEPT
1142 const_iterator __tmp = end();
1143 --__tmp;
1144 return *__tmp;
1147 // [23.2.2.3] modifiers
1149 * @brief Add data to the front of the %list.
1150 * @param __x Data to be added.
1152 * This is a typical stack operation. The function creates an
1153 * element at the front of the %list and assigns the given data
1154 * to it. Due to the nature of a %list this operation can be
1155 * done in constant time, and does not invalidate iterators and
1156 * references.
1158 void
1159 push_front(const value_type& __x)
1160 { this->_M_insert(begin(), __x); }
1162 #if __cplusplus >= 201103L
1163 void
1164 push_front(value_type&& __x)
1165 { this->_M_insert(begin(), std::move(__x)); }
1167 template<typename... _Args>
1168 #if __cplusplus > 201402L
1169 reference
1170 #else
1171 void
1172 #endif
1173 emplace_front(_Args&&... __args)
1175 this->_M_insert(begin(), std::forward<_Args>(__args)...);
1176 #if __cplusplus > 201402L
1177 return front();
1178 #endif
1180 #endif
1183 * @brief Removes first element.
1185 * This is a typical stack operation. It shrinks the %list by
1186 * one. Due to the nature of a %list this operation can be done
1187 * in constant time, and only invalidates iterators/references to
1188 * the element being removed.
1190 * Note that no data is returned, and if the first element's data
1191 * is needed, it should be retrieved before pop_front() is
1192 * called.
1194 void
1195 pop_front() _GLIBCXX_NOEXCEPT
1196 { this->_M_erase(begin()); }
1199 * @brief Add data to the end of the %list.
1200 * @param __x Data to be added.
1202 * This is a typical stack operation. The function creates an
1203 * element at the end of the %list and assigns the given data to
1204 * it. Due to the nature of a %list this operation can be done
1205 * in constant time, and does not invalidate iterators and
1206 * references.
1208 void
1209 push_back(const value_type& __x)
1210 { this->_M_insert(end(), __x); }
1212 #if __cplusplus >= 201103L
1213 void
1214 push_back(value_type&& __x)
1215 { this->_M_insert(end(), std::move(__x)); }
1217 template<typename... _Args>
1218 #if __cplusplus > 201402L
1219 reference
1220 #else
1221 void
1222 #endif
1223 emplace_back(_Args&&... __args)
1225 this->_M_insert(end(), std::forward<_Args>(__args)...);
1226 #if __cplusplus > 201402L
1227 return back();
1228 #endif
1230 #endif
1233 * @brief Removes last element.
1235 * This is a typical stack operation. It shrinks the %list by
1236 * one. Due to the nature of a %list this operation can be done
1237 * in constant time, and only invalidates iterators/references to
1238 * the element being removed.
1240 * Note that no data is returned, and if the last element's data
1241 * is needed, it should be retrieved before pop_back() is called.
1243 void
1244 pop_back() _GLIBCXX_NOEXCEPT
1245 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
1247 #if __cplusplus >= 201103L
1249 * @brief Constructs object in %list before specified iterator.
1250 * @param __position A const_iterator into the %list.
1251 * @param __args Arguments.
1252 * @return An iterator that points to the inserted data.
1254 * This function will insert an object of type T constructed
1255 * with T(std::forward<Args>(args)...) before the specified
1256 * location. Due to the nature of a %list this operation can
1257 * be done in constant time, and does not invalidate iterators
1258 * and references.
1260 template<typename... _Args>
1261 iterator
1262 emplace(const_iterator __position, _Args&&... __args);
1265 * @brief Inserts given value into %list before specified iterator.
1266 * @param __position A const_iterator into the %list.
1267 * @param __x Data to be inserted.
1268 * @return An iterator that points to the inserted data.
1270 * This function will insert a copy of the given value before
1271 * the specified location. Due to the nature of a %list this
1272 * operation can be done in constant time, and does not
1273 * invalidate iterators and references.
1275 iterator
1276 insert(const_iterator __position, const value_type& __x);
1277 #else
1279 * @brief Inserts given value into %list before specified iterator.
1280 * @param __position An iterator into the %list.
1281 * @param __x Data to be inserted.
1282 * @return An iterator that points to the inserted data.
1284 * This function will insert a copy of the given value before
1285 * the specified location. Due to the nature of a %list this
1286 * operation can be done in constant time, and does not
1287 * invalidate iterators and references.
1289 iterator
1290 insert(iterator __position, const value_type& __x);
1291 #endif
1293 #if __cplusplus >= 201103L
1295 * @brief Inserts given rvalue into %list before specified iterator.
1296 * @param __position A const_iterator into the %list.
1297 * @param __x Data to be inserted.
1298 * @return An iterator that points to the inserted data.
1300 * This function will insert a copy of the given rvalue before
1301 * the specified location. Due to the nature of a %list this
1302 * operation can be done in constant time, and does not
1303 * invalidate iterators and references.
1305 iterator
1306 insert(const_iterator __position, value_type&& __x)
1307 { return emplace(__position, std::move(__x)); }
1310 * @brief Inserts the contents of an initializer_list into %list
1311 * before specified const_iterator.
1312 * @param __p A const_iterator into the %list.
1313 * @param __l An initializer_list of value_type.
1314 * @return An iterator pointing to the first element inserted
1315 * (or __position).
1317 * This function will insert copies of the data in the
1318 * initializer_list @a l into the %list before the location
1319 * specified by @a p.
1321 * This operation is linear in the number of elements inserted and
1322 * does not invalidate iterators and references.
1324 iterator
1325 insert(const_iterator __p, initializer_list<value_type> __l)
1326 { return this->insert(__p, __l.begin(), __l.end()); }
1327 #endif
1329 #if __cplusplus >= 201103L
1331 * @brief Inserts a number of copies of given data into the %list.
1332 * @param __position A const_iterator into the %list.
1333 * @param __n Number of elements to be inserted.
1334 * @param __x Data to be inserted.
1335 * @return An iterator pointing to the first element inserted
1336 * (or __position).
1338 * This function will insert a specified number of copies of the
1339 * given data before the location specified by @a position.
1341 * This operation is linear in the number of elements inserted and
1342 * does not invalidate iterators and references.
1344 iterator
1345 insert(const_iterator __position, size_type __n, const value_type& __x);
1346 #else
1348 * @brief Inserts a number of copies of given data into the %list.
1349 * @param __position An iterator into the %list.
1350 * @param __n Number of elements to be inserted.
1351 * @param __x Data to be inserted.
1353 * This function will insert a specified number of copies of the
1354 * given data before the location specified by @a position.
1356 * This operation is linear in the number of elements inserted and
1357 * does not invalidate iterators and references.
1359 void
1360 insert(iterator __position, size_type __n, const value_type& __x)
1362 list __tmp(__n, __x, get_allocator());
1363 splice(__position, __tmp);
1365 #endif
1367 #if __cplusplus >= 201103L
1369 * @brief Inserts a range into the %list.
1370 * @param __position A const_iterator into the %list.
1371 * @param __first An input iterator.
1372 * @param __last An input iterator.
1373 * @return An iterator pointing to the first element inserted
1374 * (or __position).
1376 * This function will insert copies of the data in the range [@a
1377 * first,@a last) into the %list before the location specified by
1378 * @a position.
1380 * This operation is linear in the number of elements inserted and
1381 * does not invalidate iterators and references.
1383 template<typename _InputIterator,
1384 typename = std::_RequireInputIter<_InputIterator>>
1385 iterator
1386 insert(const_iterator __position, _InputIterator __first,
1387 _InputIterator __last);
1388 #else
1390 * @brief Inserts a range into the %list.
1391 * @param __position An iterator into the %list.
1392 * @param __first An input iterator.
1393 * @param __last An input iterator.
1395 * This function will insert copies of the data in the range [@a
1396 * first,@a last) into the %list before the location specified by
1397 * @a position.
1399 * This operation is linear in the number of elements inserted and
1400 * does not invalidate iterators and references.
1402 template<typename _InputIterator>
1403 void
1404 insert(iterator __position, _InputIterator __first,
1405 _InputIterator __last)
1407 list __tmp(__first, __last, get_allocator());
1408 splice(__position, __tmp);
1410 #endif
1413 * @brief Remove element at given position.
1414 * @param __position Iterator pointing to element to be erased.
1415 * @return An iterator pointing to the next element (or end()).
1417 * This function will erase the element at the given position and thus
1418 * shorten the %list by one.
1420 * Due to the nature of a %list this operation can be done in
1421 * constant time, and only invalidates iterators/references to
1422 * the element being removed. The user is also cautioned that
1423 * this function only erases the element, and that if the element
1424 * is itself a pointer, the pointed-to memory is not touched in
1425 * any way. Managing the pointer is the user's responsibility.
1427 iterator
1428 #if __cplusplus >= 201103L
1429 erase(const_iterator __position) noexcept;
1430 #else
1431 erase(iterator __position);
1432 #endif
1435 * @brief Remove a range of elements.
1436 * @param __first Iterator pointing to the first element to be erased.
1437 * @param __last Iterator pointing to one past the last element to be
1438 * erased.
1439 * @return An iterator pointing to the element pointed to by @a last
1440 * prior to erasing (or end()).
1442 * This function will erase the elements in the range @a
1443 * [first,last) and shorten the %list accordingly.
1445 * This operation is linear time in the size of the range and only
1446 * invalidates iterators/references to the element being removed.
1447 * The user is also cautioned that this function only erases the
1448 * elements, and that if the elements themselves are pointers, the
1449 * pointed-to memory is not touched in any way. Managing the pointer
1450 * is the user's responsibility.
1452 iterator
1453 #if __cplusplus >= 201103L
1454 erase(const_iterator __first, const_iterator __last) noexcept
1455 #else
1456 erase(iterator __first, iterator __last)
1457 #endif
1459 while (__first != __last)
1460 __first = erase(__first);
1461 return __last._M_const_cast();
1465 * @brief Swaps data with another %list.
1466 * @param __x A %list of the same element and allocator types.
1468 * This exchanges the elements between two lists in constant
1469 * time. Note that the global std::swap() function is
1470 * specialized such that std::swap(l1,l2) will feed to this
1471 * function.
1473 * Whether the allocators are swapped depends on the allocator traits.
1475 void
1476 swap(list& __x) _GLIBCXX_NOEXCEPT
1478 __detail::_List_node_base::swap(this->_M_impl._M_node,
1479 __x._M_impl._M_node);
1481 size_t __xsize = __x._M_get_size();
1482 __x._M_set_size(this->_M_get_size());
1483 this->_M_set_size(__xsize);
1485 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1486 __x._M_get_Node_allocator());
1490 * Erases all the elements. Note that this function only erases
1491 * the elements, and that if the elements themselves are
1492 * pointers, the pointed-to memory is not touched in any way.
1493 * Managing the pointer is the user's responsibility.
1495 void
1496 clear() _GLIBCXX_NOEXCEPT
1498 _Base::_M_clear();
1499 _Base::_M_init();
1502 // [23.2.2.4] list operations
1504 * @brief Insert contents of another %list.
1505 * @param __position Iterator referencing the element to insert before.
1506 * @param __x Source list.
1508 * The elements of @a __x are inserted in constant time in front of
1509 * the element referenced by @a __position. @a __x becomes an empty
1510 * list.
1512 * Requires this != @a __x.
1514 void
1515 #if __cplusplus >= 201103L
1516 splice(const_iterator __position, list&& __x) noexcept
1517 #else
1518 splice(iterator __position, list& __x)
1519 #endif
1521 if (!__x.empty())
1523 _M_check_equal_allocators(__x);
1525 this->_M_transfer(__position._M_const_cast(),
1526 __x.begin(), __x.end());
1528 this->_M_inc_size(__x._M_get_size());
1529 __x._M_set_size(0);
1533 #if __cplusplus >= 201103L
1534 void
1535 splice(const_iterator __position, list& __x) noexcept
1536 { splice(__position, std::move(__x)); }
1537 #endif
1539 #if __cplusplus >= 201103L
1541 * @brief Insert element from another %list.
1542 * @param __position Const_iterator referencing the element to
1543 * insert before.
1544 * @param __x Source list.
1545 * @param __i Const_iterator referencing the element to move.
1547 * Removes the element in list @a __x referenced by @a __i and
1548 * inserts it into the current list before @a __position.
1550 void
1551 splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
1552 #else
1554 * @brief Insert element from another %list.
1555 * @param __position Iterator referencing the element to insert before.
1556 * @param __x Source list.
1557 * @param __i Iterator referencing the element to move.
1559 * Removes the element in list @a __x referenced by @a __i and
1560 * inserts it into the current list before @a __position.
1562 void
1563 splice(iterator __position, list& __x, iterator __i)
1564 #endif
1566 iterator __j = __i._M_const_cast();
1567 ++__j;
1568 if (__position == __i || __position == __j)
1569 return;
1571 if (this != std::__addressof(__x))
1572 _M_check_equal_allocators(__x);
1574 this->_M_transfer(__position._M_const_cast(),
1575 __i._M_const_cast(), __j);
1577 this->_M_inc_size(1);
1578 __x._M_dec_size(1);
1581 #if __cplusplus >= 201103L
1583 * @brief Insert element from another %list.
1584 * @param __position Const_iterator referencing the element to
1585 * insert before.
1586 * @param __x Source list.
1587 * @param __i Const_iterator referencing the element to move.
1589 * Removes the element in list @a __x referenced by @a __i and
1590 * inserts it into the current list before @a __position.
1592 void
1593 splice(const_iterator __position, list& __x, const_iterator __i) noexcept
1594 { splice(__position, std::move(__x), __i); }
1595 #endif
1597 #if __cplusplus >= 201103L
1599 * @brief Insert range from another %list.
1600 * @param __position Const_iterator referencing the element to
1601 * insert before.
1602 * @param __x Source list.
1603 * @param __first Const_iterator referencing the start of range in x.
1604 * @param __last Const_iterator referencing the end of range in x.
1606 * Removes elements in the range [__first,__last) and inserts them
1607 * before @a __position in constant time.
1609 * Undefined if @a __position is in [__first,__last).
1611 void
1612 splice(const_iterator __position, list&& __x, const_iterator __first,
1613 const_iterator __last) noexcept
1614 #else
1616 * @brief Insert range from another %list.
1617 * @param __position Iterator referencing the element to insert before.
1618 * @param __x Source list.
1619 * @param __first Iterator referencing the start of range in x.
1620 * @param __last Iterator referencing the end of range in x.
1622 * Removes elements in the range [__first,__last) and inserts them
1623 * before @a __position in constant time.
1625 * Undefined if @a __position is in [__first,__last).
1627 void
1628 splice(iterator __position, list& __x, iterator __first,
1629 iterator __last)
1630 #endif
1632 if (__first != __last)
1634 if (this != std::__addressof(__x))
1635 _M_check_equal_allocators(__x);
1637 size_t __n = _S_distance(__first, __last);
1638 this->_M_inc_size(__n);
1639 __x._M_dec_size(__n);
1641 this->_M_transfer(__position._M_const_cast(),
1642 __first._M_const_cast(),
1643 __last._M_const_cast());
1647 #if __cplusplus >= 201103L
1649 * @brief Insert range from another %list.
1650 * @param __position Const_iterator referencing the element to
1651 * insert before.
1652 * @param __x Source list.
1653 * @param __first Const_iterator referencing the start of range in x.
1654 * @param __last Const_iterator referencing the end of range in x.
1656 * Removes elements in the range [__first,__last) and inserts them
1657 * before @a __position in constant time.
1659 * Undefined if @a __position is in [__first,__last).
1661 void
1662 splice(const_iterator __position, list& __x, const_iterator __first,
1663 const_iterator __last) noexcept
1664 { splice(__position, std::move(__x), __first, __last); }
1665 #endif
1668 * @brief Remove all elements equal to value.
1669 * @param __value The value to remove.
1671 * Removes every element in the list equal to @a value.
1672 * Remaining elements stay in list order. Note that this
1673 * function only erases the elements, and that if the elements
1674 * themselves are pointers, the pointed-to memory is not
1675 * touched in any way. Managing the pointer is the user's
1676 * responsibility.
1678 void
1679 remove(const _Tp& __value);
1682 * @brief Remove all elements satisfying a predicate.
1683 * @tparam _Predicate Unary predicate function or object.
1685 * Removes every element in the list for which the predicate
1686 * returns true. Remaining elements stay in list order. Note
1687 * that this function only erases the elements, and that if the
1688 * elements themselves are pointers, the pointed-to memory is
1689 * not touched in any way. Managing the pointer is the user's
1690 * responsibility.
1692 template<typename _Predicate>
1693 void
1694 remove_if(_Predicate);
1697 * @brief Remove consecutive duplicate elements.
1699 * For each consecutive set of elements with the same value,
1700 * remove all but the first one. Remaining elements stay in
1701 * list order. Note that this function only erases the
1702 * elements, and that if the elements themselves are pointers,
1703 * the pointed-to memory is not touched in any way. Managing
1704 * the pointer is the user's responsibility.
1706 void
1707 unique();
1710 * @brief Remove consecutive elements satisfying a predicate.
1711 * @tparam _BinaryPredicate Binary predicate function or object.
1713 * For each consecutive set of elements [first,last) that
1714 * satisfy predicate(first,i) where i is an iterator in
1715 * [first,last), remove all but the first one. Remaining
1716 * elements stay in list order. Note that this function only
1717 * erases the elements, and that if the elements themselves are
1718 * pointers, the pointed-to memory is not touched in any way.
1719 * Managing the pointer is the user's responsibility.
1721 template<typename _BinaryPredicate>
1722 void
1723 unique(_BinaryPredicate);
1726 * @brief Merge sorted lists.
1727 * @param __x Sorted list to merge.
1729 * Assumes that both @a __x and this list are sorted according to
1730 * operator<(). Merges elements of @a __x into this list in
1731 * sorted order, leaving @a __x empty when complete. Elements in
1732 * this list precede elements in @a __x that are equal.
1734 #if __cplusplus >= 201103L
1735 void
1736 merge(list&& __x);
1738 void
1739 merge(list& __x)
1740 { merge(std::move(__x)); }
1741 #else
1742 void
1743 merge(list& __x);
1744 #endif
1747 * @brief Merge sorted lists according to comparison function.
1748 * @tparam _StrictWeakOrdering Comparison function defining
1749 * sort order.
1750 * @param __x Sorted list to merge.
1751 * @param __comp Comparison functor.
1753 * Assumes that both @a __x and this list are sorted according to
1754 * StrictWeakOrdering. Merges elements of @a __x into this list
1755 * in sorted order, leaving @a __x empty when complete. Elements
1756 * in this list precede elements in @a __x that are equivalent
1757 * according to StrictWeakOrdering().
1759 #if __cplusplus >= 201103L
1760 template<typename _StrictWeakOrdering>
1761 void
1762 merge(list&& __x, _StrictWeakOrdering __comp);
1764 template<typename _StrictWeakOrdering>
1765 void
1766 merge(list& __x, _StrictWeakOrdering __comp)
1767 { merge(std::move(__x), __comp); }
1768 #else
1769 template<typename _StrictWeakOrdering>
1770 void
1771 merge(list& __x, _StrictWeakOrdering __comp);
1772 #endif
1775 * @brief Reverse the elements in list.
1777 * Reverse the order of elements in the list in linear time.
1779 void
1780 reverse() _GLIBCXX_NOEXCEPT
1781 { this->_M_impl._M_node._M_reverse(); }
1784 * @brief Sort the elements.
1786 * Sorts the elements of this list in NlogN time. Equivalent
1787 * elements remain in list order.
1789 void
1790 sort();
1793 * @brief Sort the elements according to comparison function.
1795 * Sorts the elements of this list in NlogN time. Equivalent
1796 * elements remain in list order.
1798 template<typename _StrictWeakOrdering>
1799 void
1800 sort(_StrictWeakOrdering);
1802 protected:
1803 // Internal constructor functions follow.
1805 // Called by the range constructor to implement [23.1.1]/9
1807 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1808 // 438. Ambiguity in the "do the right thing" clause
1809 template<typename _Integer>
1810 void
1811 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1812 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1814 // Called by the range constructor to implement [23.1.1]/9
1815 template<typename _InputIterator>
1816 void
1817 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1818 __false_type)
1820 for (; __first != __last; ++__first)
1821 #if __cplusplus >= 201103L
1822 emplace_back(*__first);
1823 #else
1824 push_back(*__first);
1825 #endif
1828 // Called by list(n,v,a), and the range constructor when it turns out
1829 // to be the same thing.
1830 void
1831 _M_fill_initialize(size_type __n, const value_type& __x)
1833 for (; __n; --__n)
1834 push_back(__x);
1837 #if __cplusplus >= 201103L
1838 // Called by list(n).
1839 void
1840 _M_default_initialize(size_type __n)
1842 for (; __n; --__n)
1843 emplace_back();
1846 // Called by resize(sz).
1847 void
1848 _M_default_append(size_type __n);
1849 #endif
1851 // Internal assign functions follow.
1853 // Called by the range assign to implement [23.1.1]/9
1855 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1856 // 438. Ambiguity in the "do the right thing" clause
1857 template<typename _Integer>
1858 void
1859 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1860 { _M_fill_assign(__n, __val); }
1862 // Called by the range assign to implement [23.1.1]/9
1863 template<typename _InputIterator>
1864 void
1865 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1866 __false_type);
1868 // Called by assign(n,t), and the range assign when it turns out
1869 // to be the same thing.
1870 void
1871 _M_fill_assign(size_type __n, const value_type& __val);
1874 // Moves the elements from [first,last) before position.
1875 void
1876 _M_transfer(iterator __position, iterator __first, iterator __last)
1877 { __position._M_node->_M_transfer(__first._M_node, __last._M_node); }
1879 // Inserts new element at position given and with value given.
1880 #if __cplusplus < 201103L
1881 void
1882 _M_insert(iterator __position, const value_type& __x)
1884 _Node* __tmp = _M_create_node(__x);
1885 __tmp->_M_hook(__position._M_node);
1886 this->_M_inc_size(1);
1888 #else
1889 template<typename... _Args>
1890 void
1891 _M_insert(iterator __position, _Args&&... __args)
1893 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
1894 __tmp->_M_hook(__position._M_node);
1895 this->_M_inc_size(1);
1897 #endif
1899 // Erases element at position given.
1900 void
1901 _M_erase(iterator __position) _GLIBCXX_NOEXCEPT
1903 this->_M_dec_size(1);
1904 __position._M_node->_M_unhook();
1905 _Node* __n = static_cast<_Node*>(__position._M_node);
1906 #if __cplusplus >= 201103L
1907 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n->_M_valptr());
1908 #else
1909 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n->_M_valptr());
1910 #endif
1912 _M_put_node(__n);
1915 // To implement the splice (and merge) bits of N1599.
1916 void
1917 _M_check_equal_allocators(list& __x) _GLIBCXX_NOEXCEPT
1919 if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
1920 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
1921 __builtin_abort();
1924 // Used to implement resize.
1925 const_iterator
1926 _M_resize_pos(size_type& __new_size) const;
1928 #if __cplusplus >= 201103L
1929 void
1930 _M_move_assign(list&& __x, true_type) noexcept
1932 this->_M_clear();
1933 this->_M_move_nodes(std::move(__x));
1934 std::__alloc_on_move(this->_M_get_Node_allocator(),
1935 __x._M_get_Node_allocator());
1938 void
1939 _M_move_assign(list&& __x, false_type)
1941 if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
1942 _M_move_assign(std::move(__x), true_type{});
1943 else
1944 // The rvalue's allocator cannot be moved, or is not equal,
1945 // so we need to individually move each element.
1946 _M_assign_dispatch(std::__make_move_if_noexcept_iterator(__x.begin()),
1947 std::__make_move_if_noexcept_iterator(__x.end()),
1948 __false_type{});
1950 #endif
1953 #if __cpp_deduction_guides >= 201606
1954 template<typename _InputIterator, typename _ValT
1955 = typename iterator_traits<_InputIterator>::value_type,
1956 typename _Allocator = allocator<_ValT>,
1957 typename = _RequireInputIter<_InputIterator>,
1958 typename = _RequireAllocator<_Allocator>>
1959 list(_InputIterator, _InputIterator, _Allocator = _Allocator())
1960 -> list<_ValT, _Allocator>;
1961 #endif
1963 _GLIBCXX_END_NAMESPACE_CXX11
1966 * @brief List equality comparison.
1967 * @param __x A %list.
1968 * @param __y A %list of the same type as @a __x.
1969 * @return True iff the size and elements of the lists are equal.
1971 * This is an equivalence relation. It is linear in the size of
1972 * the lists. Lists are considered equivalent if their sizes are
1973 * equal, and if corresponding elements compare equal.
1975 template<typename _Tp, typename _Alloc>
1976 inline bool
1977 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1979 #if _GLIBCXX_USE_CXX11_ABI
1980 if (__x.size() != __y.size())
1981 return false;
1982 #endif
1984 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1985 const_iterator __end1 = __x.end();
1986 const_iterator __end2 = __y.end();
1988 const_iterator __i1 = __x.begin();
1989 const_iterator __i2 = __y.begin();
1990 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1992 ++__i1;
1993 ++__i2;
1995 return __i1 == __end1 && __i2 == __end2;
1999 * @brief List ordering relation.
2000 * @param __x A %list.
2001 * @param __y A %list of the same type as @a __x.
2002 * @return True iff @a __x is lexicographically less than @a __y.
2004 * This is a total ordering relation. It is linear in the size of the
2005 * lists. The elements must be comparable with @c <.
2007 * See std::lexicographical_compare() for how the determination is made.
2009 template<typename _Tp, typename _Alloc>
2010 inline bool
2011 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2012 { return std::lexicographical_compare(__x.begin(), __x.end(),
2013 __y.begin(), __y.end()); }
2015 /// Based on operator==
2016 template<typename _Tp, typename _Alloc>
2017 inline bool
2018 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2019 { return !(__x == __y); }
2021 /// Based on operator<
2022 template<typename _Tp, typename _Alloc>
2023 inline bool
2024 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2025 { return __y < __x; }
2027 /// Based on operator<
2028 template<typename _Tp, typename _Alloc>
2029 inline bool
2030 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2031 { return !(__y < __x); }
2033 /// Based on operator<
2034 template<typename _Tp, typename _Alloc>
2035 inline bool
2036 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2037 { return !(__x < __y); }
2039 /// See std::list::swap().
2040 template<typename _Tp, typename _Alloc>
2041 inline void
2042 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
2043 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2044 { __x.swap(__y); }
2046 _GLIBCXX_END_NAMESPACE_CONTAINER
2048 #if _GLIBCXX_USE_CXX11_ABI
2050 // Detect when distance is used to compute the size of the whole list.
2051 template<typename _Tp>
2052 inline ptrdiff_t
2053 __distance(_GLIBCXX_STD_C::_List_iterator<_Tp> __first,
2054 _GLIBCXX_STD_C::_List_iterator<_Tp> __last,
2055 input_iterator_tag __tag)
2057 typedef _GLIBCXX_STD_C::_List_const_iterator<_Tp> _CIter;
2058 return std::__distance(_CIter(__first), _CIter(__last), __tag);
2061 template<typename _Tp>
2062 inline ptrdiff_t
2063 __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp> __first,
2064 _GLIBCXX_STD_C::_List_const_iterator<_Tp> __last,
2065 input_iterator_tag)
2067 typedef __detail::_List_node_header _Sentinel;
2068 _GLIBCXX_STD_C::_List_const_iterator<_Tp> __beyond = __last;
2069 ++__beyond;
2070 const bool __whole = __first == __beyond;
2071 if (__builtin_constant_p (__whole) && __whole)
2072 return static_cast<const _Sentinel*>(__last._M_node)->_M_size;
2074 ptrdiff_t __n = 0;
2075 while (__first != __last)
2077 ++__first;
2078 ++__n;
2080 return __n;
2082 #endif
2084 _GLIBCXX_END_NAMESPACE_VERSION
2085 } // namespace std
2087 #endif /* _STL_LIST_H */