2017-07-18 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_list.h
blob5da19b32eea2ef26e134ade40fba153914ff9a4a
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 namespace __detail
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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; }
161 _GLIBCXX_END_NAMESPACE_VERSION
162 } // namespace detail
164 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
166 /// An actual node in the %list.
167 template<typename _Tp>
168 struct _List_node : public __detail::_List_node_base
170 #if __cplusplus >= 201103L
171 __gnu_cxx::__aligned_membuf<_Tp> _M_storage;
172 _Tp* _M_valptr() { return _M_storage._M_ptr(); }
173 _Tp const* _M_valptr() const { return _M_storage._M_ptr(); }
174 #else
175 _Tp _M_data;
176 _Tp* _M_valptr() { return std::__addressof(_M_data); }
177 _Tp const* _M_valptr() const { return std::__addressof(_M_data); }
178 #endif
182 * @brief A list::iterator.
184 * All the functions are op overloads.
186 template<typename _Tp>
187 struct _List_iterator
189 typedef _List_iterator<_Tp> _Self;
190 typedef _List_node<_Tp> _Node;
192 typedef ptrdiff_t difference_type;
193 typedef std::bidirectional_iterator_tag iterator_category;
194 typedef _Tp value_type;
195 typedef _Tp* pointer;
196 typedef _Tp& reference;
198 _List_iterator() _GLIBCXX_NOEXCEPT
199 : _M_node() { }
201 explicit
202 _List_iterator(__detail::_List_node_base* __x) _GLIBCXX_NOEXCEPT
203 : _M_node(__x) { }
205 _Self
206 _M_const_cast() const _GLIBCXX_NOEXCEPT
207 { return *this; }
209 // Must downcast from _List_node_base to _List_node to get to value.
210 reference
211 operator*() const _GLIBCXX_NOEXCEPT
212 { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
214 pointer
215 operator->() const _GLIBCXX_NOEXCEPT
216 { return static_cast<_Node*>(_M_node)->_M_valptr(); }
218 _Self&
219 operator++() _GLIBCXX_NOEXCEPT
221 _M_node = _M_node->_M_next;
222 return *this;
225 _Self
226 operator++(int) _GLIBCXX_NOEXCEPT
228 _Self __tmp = *this;
229 _M_node = _M_node->_M_next;
230 return __tmp;
233 _Self&
234 operator--() _GLIBCXX_NOEXCEPT
236 _M_node = _M_node->_M_prev;
237 return *this;
240 _Self
241 operator--(int) _GLIBCXX_NOEXCEPT
243 _Self __tmp = *this;
244 _M_node = _M_node->_M_prev;
245 return __tmp;
248 bool
249 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
250 { return _M_node == __x._M_node; }
252 bool
253 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
254 { return _M_node != __x._M_node; }
256 // The only member points to the %list element.
257 __detail::_List_node_base* _M_node;
261 * @brief A list::const_iterator.
263 * All the functions are op overloads.
265 template<typename _Tp>
266 struct _List_const_iterator
268 typedef _List_const_iterator<_Tp> _Self;
269 typedef const _List_node<_Tp> _Node;
270 typedef _List_iterator<_Tp> iterator;
272 typedef ptrdiff_t difference_type;
273 typedef std::bidirectional_iterator_tag iterator_category;
274 typedef _Tp value_type;
275 typedef const _Tp* pointer;
276 typedef const _Tp& reference;
278 _List_const_iterator() _GLIBCXX_NOEXCEPT
279 : _M_node() { }
281 explicit
282 _List_const_iterator(const __detail::_List_node_base* __x)
283 _GLIBCXX_NOEXCEPT
284 : _M_node(__x) { }
286 _List_const_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
287 : _M_node(__x._M_node) { }
289 iterator
290 _M_const_cast() const _GLIBCXX_NOEXCEPT
291 { return iterator(const_cast<__detail::_List_node_base*>(_M_node)); }
293 // Must downcast from List_node_base to _List_node to get to value.
294 reference
295 operator*() const _GLIBCXX_NOEXCEPT
296 { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
298 pointer
299 operator->() const _GLIBCXX_NOEXCEPT
300 { return static_cast<_Node*>(_M_node)->_M_valptr(); }
302 _Self&
303 operator++() _GLIBCXX_NOEXCEPT
305 _M_node = _M_node->_M_next;
306 return *this;
309 _Self
310 operator++(int) _GLIBCXX_NOEXCEPT
312 _Self __tmp = *this;
313 _M_node = _M_node->_M_next;
314 return __tmp;
317 _Self&
318 operator--() _GLIBCXX_NOEXCEPT
320 _M_node = _M_node->_M_prev;
321 return *this;
324 _Self
325 operator--(int) _GLIBCXX_NOEXCEPT
327 _Self __tmp = *this;
328 _M_node = _M_node->_M_prev;
329 return __tmp;
332 bool
333 operator==(const _Self& __x) const _GLIBCXX_NOEXCEPT
334 { return _M_node == __x._M_node; }
336 bool
337 operator!=(const _Self& __x) const _GLIBCXX_NOEXCEPT
338 { return _M_node != __x._M_node; }
340 // The only member points to the %list element.
341 const __detail::_List_node_base* _M_node;
344 template<typename _Val>
345 inline bool
346 operator==(const _List_iterator<_Val>& __x,
347 const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
348 { return __x._M_node == __y._M_node; }
350 template<typename _Val>
351 inline bool
352 operator!=(const _List_iterator<_Val>& __x,
353 const _List_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
354 { return __x._M_node != __y._M_node; }
356 _GLIBCXX_BEGIN_NAMESPACE_CXX11
357 /// See bits/stl_deque.h's _Deque_base for an explanation.
358 template<typename _Tp, typename _Alloc>
359 class _List_base
361 protected:
362 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
363 rebind<_Tp>::other _Tp_alloc_type;
364 typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Tp_alloc_traits;
365 typedef typename _Tp_alloc_traits::template
366 rebind<_List_node<_Tp> >::other _Node_alloc_type;
367 typedef __gnu_cxx::__alloc_traits<_Node_alloc_type> _Node_alloc_traits;
369 static size_t
370 _S_distance(const __detail::_List_node_base* __first,
371 const __detail::_List_node_base* __last)
373 size_t __n = 0;
374 while (__first != __last)
376 __first = __first->_M_next;
377 ++__n;
379 return __n;
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) noexcept
399 : _Node_alloc_type(std::move(__a))
401 #endif
404 _List_impl _M_impl;
406 #if _GLIBCXX_USE_CXX11_ABI
407 size_t _M_get_size() const { return _M_impl._M_node._M_size; }
409 void _M_set_size(size_t __n) { _M_impl._M_node._M_size = __n; }
411 void _M_inc_size(size_t __n) { _M_impl._M_node._M_size += __n; }
413 void _M_dec_size(size_t __n) { _M_impl._M_node._M_size -= __n; }
415 size_t
416 _M_distance(const __detail::_List_node_base* __first,
417 const __detail::_List_node_base* __last) const
418 { return _S_distance(__first, __last); }
420 // return the stored size
421 size_t _M_node_count() const { return _M_get_size(); }
422 #else
423 // dummy implementations used when the size is not stored
424 size_t _M_get_size() const { return 0; }
425 void _M_set_size(size_t) { }
426 void _M_inc_size(size_t) { }
427 void _M_dec_size(size_t) { }
428 size_t _M_distance(const void*, const void*) const { return 0; }
430 // count the number of nodes
431 size_t _M_node_count() const
433 return _S_distance(_M_impl._M_node._M_next,
434 std::__addressof(_M_impl._M_node));
436 #endif
438 typename _Node_alloc_traits::pointer
439 _M_get_node()
440 { return _Node_alloc_traits::allocate(_M_impl, 1); }
442 void
443 _M_put_node(typename _Node_alloc_traits::pointer __p) _GLIBCXX_NOEXCEPT
444 { _Node_alloc_traits::deallocate(_M_impl, __p, 1); }
446 public:
447 typedef _Alloc allocator_type;
449 _Node_alloc_type&
450 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
451 { return _M_impl; }
453 const _Node_alloc_type&
454 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
455 { return _M_impl; }
457 #if __cplusplus >= 201103L
458 _List_base() = default;
459 #else
460 _List_base() { }
461 #endif
463 _List_base(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
464 : _M_impl(__a)
467 #if __cplusplus >= 201103L
468 _List_base(_List_base&&) = default;
470 _List_base(_List_base&& __x, _Node_alloc_type&& __a)
471 : _M_impl(std::move(__a))
473 if (__x._M_get_Node_allocator() == _M_get_Node_allocator())
474 _M_move_nodes(std::move(__x));
475 // else caller must move individual elements.
478 void
479 _M_move_nodes(_List_base&& __x)
480 { _M_impl._M_node._M_move_nodes(std::move(__x._M_impl._M_node)); }
481 #endif
483 // This is what actually destroys the list.
484 ~_List_base() _GLIBCXX_NOEXCEPT
485 { _M_clear(); }
487 void
488 _M_clear() _GLIBCXX_NOEXCEPT;
490 void
491 _M_init() _GLIBCXX_NOEXCEPT
492 { this->_M_impl._M_node._M_init(); }
496 * @brief A standard container with linear time access to elements,
497 * and fixed time insertion/deletion at any point in the sequence.
499 * @ingroup sequences
501 * @tparam _Tp Type of element.
502 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
504 * Meets the requirements of a <a href="tables.html#65">container</a>, a
505 * <a href="tables.html#66">reversible container</a>, and a
506 * <a href="tables.html#67">sequence</a>, including the
507 * <a href="tables.html#68">optional sequence requirements</a> with the
508 * %exception of @c at and @c operator[].
510 * This is a @e doubly @e linked %list. Traversal up and down the
511 * %list requires linear time, but adding and removing elements (or
512 * @e nodes) is done in constant time, regardless of where the
513 * change takes place. Unlike std::vector and std::deque,
514 * random-access iterators are not provided, so subscripting ( @c
515 * [] ) access is not allowed. For algorithms which only need
516 * sequential access, this lack makes no difference.
518 * Also unlike the other standard containers, std::list provides
519 * specialized algorithms %unique to linked lists, such as
520 * splicing, sorting, and in-place reversal.
522 * A couple points on memory allocation for list<Tp>:
524 * First, we never actually allocate a Tp, we allocate
525 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
526 * that after elements from %list<X,Alloc1> are spliced into
527 * %list<X,Alloc2>, destroying the memory of the second %list is a
528 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
530 * Second, a %list conceptually represented as
531 * @code
532 * A <---> B <---> C <---> D
533 * @endcode
534 * is actually circular; a link exists between A and D. The %list
535 * class holds (as its only data member) a private list::iterator
536 * pointing to @e D, not to @e A! To get to the head of the %list,
537 * we start at the tail and move forward by one. When this member
538 * iterator's next/previous pointers refer to itself, the %list is
539 * %empty.
541 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
542 class list : protected _List_base<_Tp, _Alloc>
544 #ifdef _GLIBCXX_CONCEPT_CHECKS
545 // concept requirements
546 typedef typename _Alloc::value_type _Alloc_value_type;
547 # if __cplusplus < 201103L
548 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
549 # endif
550 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
551 #endif
553 typedef _List_base<_Tp, _Alloc> _Base;
554 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
555 typedef typename _Base::_Tp_alloc_traits _Tp_alloc_traits;
556 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
557 typedef typename _Base::_Node_alloc_traits _Node_alloc_traits;
559 public:
560 typedef _Tp value_type;
561 typedef typename _Tp_alloc_traits::pointer pointer;
562 typedef typename _Tp_alloc_traits::const_pointer const_pointer;
563 typedef typename _Tp_alloc_traits::reference reference;
564 typedef typename _Tp_alloc_traits::const_reference const_reference;
565 typedef _List_iterator<_Tp> iterator;
566 typedef _List_const_iterator<_Tp> const_iterator;
567 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
568 typedef std::reverse_iterator<iterator> reverse_iterator;
569 typedef size_t size_type;
570 typedef ptrdiff_t difference_type;
571 typedef _Alloc allocator_type;
573 protected:
574 // Note that pointers-to-_Node's can be ctor-converted to
575 // iterator types.
576 typedef _List_node<_Tp> _Node;
578 using _Base::_M_impl;
579 using _Base::_M_put_node;
580 using _Base::_M_get_node;
581 using _Base::_M_get_Node_allocator;
584 * @param __args An instance of user data.
586 * Allocates space for a new node and constructs a copy of
587 * @a __args in it.
589 #if __cplusplus < 201103L
590 _Node*
591 _M_create_node(const value_type& __x)
593 _Node* __p = this->_M_get_node();
594 __try
596 _Tp_alloc_type __alloc(_M_get_Node_allocator());
597 __alloc.construct(__p->_M_valptr(), __x);
599 __catch(...)
601 _M_put_node(__p);
602 __throw_exception_again;
604 return __p;
606 #else
607 template<typename... _Args>
608 _Node*
609 _M_create_node(_Args&&... __args)
611 auto __p = this->_M_get_node();
612 auto& __alloc = _M_get_Node_allocator();
613 __allocated_ptr<_Node_alloc_type> __guard{__alloc, __p};
614 _Node_alloc_traits::construct(__alloc, __p->_M_valptr(),
615 std::forward<_Args>(__args)...);
616 __guard = nullptr;
617 return __p;
619 #endif
621 public:
622 // [23.2.2.1] construct/copy/destroy
623 // (assign() and get_allocator() are also listed in this section)
626 * @brief Creates a %list with no elements.
628 #if __cplusplus >= 201103L
629 list() = default;
630 #else
631 list() { }
632 #endif
635 * @brief Creates a %list with no elements.
636 * @param __a An allocator object.
638 explicit
639 list(const allocator_type& __a) _GLIBCXX_NOEXCEPT
640 : _Base(_Node_alloc_type(__a)) { }
642 #if __cplusplus >= 201103L
644 * @brief Creates a %list with default constructed elements.
645 * @param __n The number of elements to initially create.
646 * @param __a An allocator object.
648 * This constructor fills the %list with @a __n default
649 * constructed elements.
651 explicit
652 list(size_type __n, const allocator_type& __a = allocator_type())
653 : _Base(_Node_alloc_type(__a))
654 { _M_default_initialize(__n); }
657 * @brief Creates a %list with copies of an exemplar element.
658 * @param __n The number of elements to initially create.
659 * @param __value An element to copy.
660 * @param __a An allocator object.
662 * This constructor fills the %list with @a __n copies of @a __value.
664 list(size_type __n, const value_type& __value,
665 const allocator_type& __a = allocator_type())
666 : _Base(_Node_alloc_type(__a))
667 { _M_fill_initialize(__n, __value); }
668 #else
670 * @brief Creates a %list with copies of an exemplar element.
671 * @param __n The number of elements to initially create.
672 * @param __value An element to copy.
673 * @param __a An allocator object.
675 * This constructor fills the %list with @a __n copies of @a __value.
677 explicit
678 list(size_type __n, const value_type& __value = value_type(),
679 const allocator_type& __a = allocator_type())
680 : _Base(_Node_alloc_type(__a))
681 { _M_fill_initialize(__n, __value); }
682 #endif
685 * @brief %List copy constructor.
686 * @param __x A %list of identical element and allocator types.
688 * The newly-created %list uses a copy of the allocation object used
689 * by @a __x (unless the allocator traits dictate a different object).
691 list(const list& __x)
692 : _Base(_Node_alloc_traits::
693 _S_select_on_copy(__x._M_get_Node_allocator()))
694 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
696 #if __cplusplus >= 201103L
698 * @brief %List move constructor.
700 * The newly-created %list contains the exact contents of the moved
701 * instance. The contents of the moved instance are a valid, but
702 * unspecified %list.
704 list(list&&) = default;
707 * @brief Builds a %list from an initializer_list
708 * @param __l An initializer_list of value_type.
709 * @param __a An allocator object.
711 * Create a %list consisting of copies of the elements in the
712 * initializer_list @a __l. This is linear in __l.size().
714 list(initializer_list<value_type> __l,
715 const allocator_type& __a = allocator_type())
716 : _Base(_Node_alloc_type(__a))
717 { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
719 list(const list& __x, const allocator_type& __a)
720 : _Base(_Node_alloc_type(__a))
721 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
723 list(list&& __x, const allocator_type& __a)
724 noexcept(_Node_alloc_traits::_S_always_equal())
725 : _Base(std::move(__x), _Node_alloc_type(__a))
727 // If __x is not empty it means its allocator is not equal to __a,
728 // so we need to move from each element individually.
729 insert(begin(), std::__make_move_if_noexcept_iterator(__x.begin()),
730 std::__make_move_if_noexcept_iterator(__x.end()));
732 #endif
735 * @brief Builds a %list from a range.
736 * @param __first An input iterator.
737 * @param __last An input iterator.
738 * @param __a An allocator object.
740 * Create a %list consisting of copies of the elements from
741 * [@a __first,@a __last). This is linear in N (where N is
742 * distance(@a __first,@a __last)).
744 #if __cplusplus >= 201103L
745 template<typename _InputIterator,
746 typename = std::_RequireInputIter<_InputIterator>>
747 list(_InputIterator __first, _InputIterator __last,
748 const allocator_type& __a = allocator_type())
749 : _Base(_Node_alloc_type(__a))
750 { _M_initialize_dispatch(__first, __last, __false_type()); }
751 #else
752 template<typename _InputIterator>
753 list(_InputIterator __first, _InputIterator __last,
754 const allocator_type& __a = allocator_type())
755 : _Base(_Node_alloc_type(__a))
757 // Check whether it's an integral type. If so, it's not an iterator.
758 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
759 _M_initialize_dispatch(__first, __last, _Integral());
761 #endif
763 #if __cplusplus >= 201103L
765 * No explicit dtor needed as the _Base dtor takes care of
766 * things. The _Base dtor only erases the elements, and note
767 * that if the elements themselves are pointers, the pointed-to
768 * memory is not touched in any way. Managing the pointer is
769 * the user's responsibility.
771 ~list() = default;
772 #endif
775 * @brief %List assignment operator.
776 * @param __x A %list of identical element and allocator types.
778 * All the elements of @a __x are copied.
780 * Whether the allocator is copied depends on the allocator traits.
782 list&
783 operator=(const list& __x);
785 #if __cplusplus >= 201103L
787 * @brief %List move assignment operator.
788 * @param __x A %list of identical element and allocator types.
790 * The contents of @a __x are moved into this %list (without copying).
792 * Afterwards @a __x is a valid, but unspecified %list
794 * Whether the allocator is moved depends on the allocator traits.
796 list&
797 operator=(list&& __x)
798 noexcept(_Node_alloc_traits::_S_nothrow_move())
800 constexpr bool __move_storage =
801 _Node_alloc_traits::_S_propagate_on_move_assign()
802 || _Node_alloc_traits::_S_always_equal();
803 _M_move_assign(std::move(__x), __bool_constant<__move_storage>());
804 return *this;
808 * @brief %List initializer list assignment operator.
809 * @param __l An initializer_list of value_type.
811 * Replace the contents of the %list with copies of the elements
812 * in the initializer_list @a __l. This is linear in l.size().
814 list&
815 operator=(initializer_list<value_type> __l)
817 this->assign(__l.begin(), __l.end());
818 return *this;
820 #endif
823 * @brief Assigns a given value to a %list.
824 * @param __n Number of elements to be assigned.
825 * @param __val Value to be assigned.
827 * This function fills a %list with @a __n copies of the given
828 * value. Note that the assignment completely changes the %list
829 * and that the resulting %list's size is the same as the number
830 * of elements assigned.
832 void
833 assign(size_type __n, const value_type& __val)
834 { _M_fill_assign(__n, __val); }
837 * @brief Assigns a range to a %list.
838 * @param __first An input iterator.
839 * @param __last An input iterator.
841 * This function fills a %list with copies of the elements in the
842 * range [@a __first,@a __last).
844 * Note that the assignment completely changes the %list and
845 * that the resulting %list's size is the same as the number of
846 * elements assigned.
848 #if __cplusplus >= 201103L
849 template<typename _InputIterator,
850 typename = std::_RequireInputIter<_InputIterator>>
851 void
852 assign(_InputIterator __first, _InputIterator __last)
853 { _M_assign_dispatch(__first, __last, __false_type()); }
854 #else
855 template<typename _InputIterator>
856 void
857 assign(_InputIterator __first, _InputIterator __last)
859 // Check whether it's an integral type. If so, it's not an iterator.
860 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
861 _M_assign_dispatch(__first, __last, _Integral());
863 #endif
865 #if __cplusplus >= 201103L
867 * @brief Assigns an initializer_list to a %list.
868 * @param __l An initializer_list of value_type.
870 * Replace the contents of the %list with copies of the elements
871 * in the initializer_list @a __l. This is linear in __l.size().
873 void
874 assign(initializer_list<value_type> __l)
875 { this->_M_assign_dispatch(__l.begin(), __l.end(), __false_type()); }
876 #endif
878 /// Get a copy of the memory allocation object.
879 allocator_type
880 get_allocator() const _GLIBCXX_NOEXCEPT
881 { return allocator_type(_Base::_M_get_Node_allocator()); }
883 // iterators
885 * Returns a read/write iterator that points to the first element in the
886 * %list. Iteration is done in ordinary element order.
888 iterator
889 begin() _GLIBCXX_NOEXCEPT
890 { return iterator(this->_M_impl._M_node._M_next); }
893 * Returns a read-only (constant) iterator that points to the
894 * first element in the %list. Iteration is done in ordinary
895 * element order.
897 const_iterator
898 begin() const _GLIBCXX_NOEXCEPT
899 { return const_iterator(this->_M_impl._M_node._M_next); }
902 * Returns a read/write iterator that points one past the last
903 * element in the %list. Iteration is done in ordinary element
904 * order.
906 iterator
907 end() _GLIBCXX_NOEXCEPT
908 { return iterator(&this->_M_impl._M_node); }
911 * Returns a read-only (constant) iterator that points one past
912 * the last element in the %list. Iteration is done in ordinary
913 * element order.
915 const_iterator
916 end() const _GLIBCXX_NOEXCEPT
917 { return const_iterator(&this->_M_impl._M_node); }
920 * Returns a read/write reverse iterator that points to the last
921 * element in the %list. Iteration is done in reverse element
922 * order.
924 reverse_iterator
925 rbegin() _GLIBCXX_NOEXCEPT
926 { return reverse_iterator(end()); }
929 * Returns a read-only (constant) reverse iterator that points to
930 * the last element in the %list. Iteration is done in reverse
931 * element order.
933 const_reverse_iterator
934 rbegin() const _GLIBCXX_NOEXCEPT
935 { return const_reverse_iterator(end()); }
938 * Returns a read/write reverse iterator that points to one
939 * before the first element in the %list. Iteration is done in
940 * reverse element order.
942 reverse_iterator
943 rend() _GLIBCXX_NOEXCEPT
944 { return reverse_iterator(begin()); }
947 * Returns a read-only (constant) reverse iterator that points to one
948 * before the first element in the %list. Iteration is done in reverse
949 * element order.
951 const_reverse_iterator
952 rend() const _GLIBCXX_NOEXCEPT
953 { return const_reverse_iterator(begin()); }
955 #if __cplusplus >= 201103L
957 * Returns a read-only (constant) iterator that points to the
958 * first element in the %list. Iteration is done in ordinary
959 * element order.
961 const_iterator
962 cbegin() const noexcept
963 { return const_iterator(this->_M_impl._M_node._M_next); }
966 * Returns a read-only (constant) iterator that points one past
967 * the last element in the %list. Iteration is done in ordinary
968 * element order.
970 const_iterator
971 cend() const noexcept
972 { return const_iterator(&this->_M_impl._M_node); }
975 * Returns a read-only (constant) reverse iterator that points to
976 * the last element in the %list. Iteration is done in reverse
977 * element order.
979 const_reverse_iterator
980 crbegin() const noexcept
981 { return const_reverse_iterator(end()); }
984 * Returns a read-only (constant) reverse iterator that points to one
985 * before the first element in the %list. Iteration is done in reverse
986 * element order.
988 const_reverse_iterator
989 crend() const noexcept
990 { return const_reverse_iterator(begin()); }
991 #endif
993 // [23.2.2.2] capacity
995 * Returns true if the %list is empty. (Thus begin() would equal
996 * end().)
998 bool
999 empty() const _GLIBCXX_NOEXCEPT
1000 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
1002 /** Returns the number of elements in the %list. */
1003 size_type
1004 size() const _GLIBCXX_NOEXCEPT
1005 { return this->_M_node_count(); }
1007 /** Returns the size() of the largest possible %list. */
1008 size_type
1009 max_size() const _GLIBCXX_NOEXCEPT
1010 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1012 #if __cplusplus >= 201103L
1014 * @brief Resizes the %list to the specified number of elements.
1015 * @param __new_size Number of elements the %list should contain.
1017 * This function will %resize the %list to the specified number
1018 * of elements. If the number is smaller than the %list's
1019 * current size the %list is truncated, otherwise default
1020 * constructed elements are appended.
1022 void
1023 resize(size_type __new_size);
1026 * @brief Resizes the %list to the specified number of elements.
1027 * @param __new_size Number of elements the %list should contain.
1028 * @param __x Data with which new elements should be populated.
1030 * This function will %resize the %list to the specified number
1031 * of elements. If the number is smaller than the %list's
1032 * current size the %list is truncated, otherwise the %list is
1033 * extended and new elements are populated with given data.
1035 void
1036 resize(size_type __new_size, const value_type& __x);
1037 #else
1039 * @brief Resizes the %list to the specified number of elements.
1040 * @param __new_size Number of elements the %list should contain.
1041 * @param __x Data with which new elements should be populated.
1043 * This function will %resize the %list to the specified number
1044 * of elements. If the number is smaller than the %list's
1045 * current size the %list is truncated, otherwise the %list is
1046 * extended and new elements are populated with given data.
1048 void
1049 resize(size_type __new_size, value_type __x = value_type());
1050 #endif
1052 // element access
1054 * Returns a read/write reference to the data at the first
1055 * element of the %list.
1057 reference
1058 front() _GLIBCXX_NOEXCEPT
1059 { return *begin(); }
1062 * Returns a read-only (constant) reference to the data at the first
1063 * element of the %list.
1065 const_reference
1066 front() const _GLIBCXX_NOEXCEPT
1067 { return *begin(); }
1070 * Returns a read/write reference to the data at the last element
1071 * of the %list.
1073 reference
1074 back() _GLIBCXX_NOEXCEPT
1076 iterator __tmp = end();
1077 --__tmp;
1078 return *__tmp;
1082 * Returns a read-only (constant) reference to the data at the last
1083 * element of the %list.
1085 const_reference
1086 back() const _GLIBCXX_NOEXCEPT
1088 const_iterator __tmp = end();
1089 --__tmp;
1090 return *__tmp;
1093 // [23.2.2.3] modifiers
1095 * @brief Add data to the front of the %list.
1096 * @param __x Data to be added.
1098 * This is a typical stack operation. The function creates an
1099 * element at the front of the %list and assigns the given data
1100 * to it. Due to the nature of a %list this operation can be
1101 * done in constant time, and does not invalidate iterators and
1102 * references.
1104 void
1105 push_front(const value_type& __x)
1106 { this->_M_insert(begin(), __x); }
1108 #if __cplusplus >= 201103L
1109 void
1110 push_front(value_type&& __x)
1111 { this->_M_insert(begin(), std::move(__x)); }
1113 template<typename... _Args>
1114 #if __cplusplus > 201402L
1115 reference
1116 #else
1117 void
1118 #endif
1119 emplace_front(_Args&&... __args)
1121 this->_M_insert(begin(), std::forward<_Args>(__args)...);
1122 #if __cplusplus > 201402L
1123 return front();
1124 #endif
1126 #endif
1129 * @brief Removes first element.
1131 * This is a typical stack operation. It shrinks the %list by
1132 * one. Due to the nature of a %list this operation can be done
1133 * in constant time, and only invalidates iterators/references to
1134 * the element being removed.
1136 * Note that no data is returned, and if the first element's data
1137 * is needed, it should be retrieved before pop_front() is
1138 * called.
1140 void
1141 pop_front() _GLIBCXX_NOEXCEPT
1142 { this->_M_erase(begin()); }
1145 * @brief Add data to the end of the %list.
1146 * @param __x Data to be added.
1148 * This is a typical stack operation. The function creates an
1149 * element at the end of the %list and assigns the given data to
1150 * it. Due to the nature of a %list this operation can be done
1151 * in constant time, and does not invalidate iterators and
1152 * references.
1154 void
1155 push_back(const value_type& __x)
1156 { this->_M_insert(end(), __x); }
1158 #if __cplusplus >= 201103L
1159 void
1160 push_back(value_type&& __x)
1161 { this->_M_insert(end(), std::move(__x)); }
1163 template<typename... _Args>
1164 #if __cplusplus > 201402L
1165 reference
1166 #else
1167 void
1168 #endif
1169 emplace_back(_Args&&... __args)
1171 this->_M_insert(end(), std::forward<_Args>(__args)...);
1172 #if __cplusplus > 201402L
1173 return back();
1174 #endif
1176 #endif
1179 * @brief Removes last element.
1181 * This is a typical stack operation. It shrinks the %list by
1182 * one. Due to the nature of a %list this operation can be done
1183 * in constant time, and only invalidates iterators/references to
1184 * the element being removed.
1186 * Note that no data is returned, and if the last element's data
1187 * is needed, it should be retrieved before pop_back() is called.
1189 void
1190 pop_back() _GLIBCXX_NOEXCEPT
1191 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
1193 #if __cplusplus >= 201103L
1195 * @brief Constructs object in %list before specified iterator.
1196 * @param __position A const_iterator into the %list.
1197 * @param __args Arguments.
1198 * @return An iterator that points to the inserted data.
1200 * This function will insert an object of type T constructed
1201 * with T(std::forward<Args>(args)...) before the specified
1202 * location. Due to the nature of a %list this operation can
1203 * be done in constant time, and does not invalidate iterators
1204 * and references.
1206 template<typename... _Args>
1207 iterator
1208 emplace(const_iterator __position, _Args&&... __args);
1211 * @brief Inserts given value into %list before specified iterator.
1212 * @param __position A const_iterator into the %list.
1213 * @param __x Data to be inserted.
1214 * @return An iterator that points to the inserted data.
1216 * This function will insert a copy of the given value before
1217 * the specified location. Due to the nature of a %list this
1218 * operation can be done in constant time, and does not
1219 * invalidate iterators and references.
1221 iterator
1222 insert(const_iterator __position, const value_type& __x);
1223 #else
1225 * @brief Inserts given value into %list before specified iterator.
1226 * @param __position An iterator into the %list.
1227 * @param __x Data to be inserted.
1228 * @return An iterator that points to the inserted data.
1230 * This function will insert a copy of the given value before
1231 * the specified location. Due to the nature of a %list this
1232 * operation can be done in constant time, and does not
1233 * invalidate iterators and references.
1235 iterator
1236 insert(iterator __position, const value_type& __x);
1237 #endif
1239 #if __cplusplus >= 201103L
1241 * @brief Inserts given rvalue into %list before specified iterator.
1242 * @param __position A const_iterator into the %list.
1243 * @param __x Data to be inserted.
1244 * @return An iterator that points to the inserted data.
1246 * This function will insert a copy of the given rvalue before
1247 * the specified location. Due to the nature of a %list this
1248 * operation can be done in constant time, and does not
1249 * invalidate iterators and references.
1251 iterator
1252 insert(const_iterator __position, value_type&& __x)
1253 { return emplace(__position, std::move(__x)); }
1256 * @brief Inserts the contents of an initializer_list into %list
1257 * before specified const_iterator.
1258 * @param __p A const_iterator into the %list.
1259 * @param __l An initializer_list of value_type.
1260 * @return An iterator pointing to the first element inserted
1261 * (or __position).
1263 * This function will insert copies of the data in the
1264 * initializer_list @a l into the %list before the location
1265 * specified by @a p.
1267 * This operation is linear in the number of elements inserted and
1268 * does not invalidate iterators and references.
1270 iterator
1271 insert(const_iterator __p, initializer_list<value_type> __l)
1272 { return this->insert(__p, __l.begin(), __l.end()); }
1273 #endif
1275 #if __cplusplus >= 201103L
1277 * @brief Inserts a number of copies of given data into the %list.
1278 * @param __position A const_iterator into the %list.
1279 * @param __n Number of elements to be inserted.
1280 * @param __x Data to be inserted.
1281 * @return An iterator pointing to the first element inserted
1282 * (or __position).
1284 * This function will insert a specified number of copies of the
1285 * given data before the location specified by @a position.
1287 * This operation is linear in the number of elements inserted and
1288 * does not invalidate iterators and references.
1290 iterator
1291 insert(const_iterator __position, size_type __n, const value_type& __x);
1292 #else
1294 * @brief Inserts a number of copies of given data into the %list.
1295 * @param __position An iterator into the %list.
1296 * @param __n Number of elements to be inserted.
1297 * @param __x Data to be inserted.
1299 * This function will insert a specified number of copies of the
1300 * given data before the location specified by @a position.
1302 * This operation is linear in the number of elements inserted and
1303 * does not invalidate iterators and references.
1305 void
1306 insert(iterator __position, size_type __n, const value_type& __x)
1308 list __tmp(__n, __x, get_allocator());
1309 splice(__position, __tmp);
1311 #endif
1313 #if __cplusplus >= 201103L
1315 * @brief Inserts a range into the %list.
1316 * @param __position A const_iterator into the %list.
1317 * @param __first An input iterator.
1318 * @param __last An input iterator.
1319 * @return An iterator pointing to the first element inserted
1320 * (or __position).
1322 * This function will insert copies of the data in the range [@a
1323 * first,@a last) into the %list before the location specified by
1324 * @a position.
1326 * This operation is linear in the number of elements inserted and
1327 * does not invalidate iterators and references.
1329 template<typename _InputIterator,
1330 typename = std::_RequireInputIter<_InputIterator>>
1331 iterator
1332 insert(const_iterator __position, _InputIterator __first,
1333 _InputIterator __last);
1334 #else
1336 * @brief Inserts a range into the %list.
1337 * @param __position An iterator into the %list.
1338 * @param __first An input iterator.
1339 * @param __last An input iterator.
1341 * This function will insert copies of the data in the range [@a
1342 * first,@a last) into the %list before the location specified by
1343 * @a position.
1345 * This operation is linear in the number of elements inserted and
1346 * does not invalidate iterators and references.
1348 template<typename _InputIterator>
1349 void
1350 insert(iterator __position, _InputIterator __first,
1351 _InputIterator __last)
1353 list __tmp(__first, __last, get_allocator());
1354 splice(__position, __tmp);
1356 #endif
1359 * @brief Remove element at given position.
1360 * @param __position Iterator pointing to element to be erased.
1361 * @return An iterator pointing to the next element (or end()).
1363 * This function will erase the element at the given position and thus
1364 * shorten the %list by one.
1366 * Due to the nature of a %list this operation can be done in
1367 * constant time, and only invalidates iterators/references to
1368 * the element being removed. The user is also cautioned that
1369 * this function only erases the element, and that if the element
1370 * is itself a pointer, the pointed-to memory is not touched in
1371 * any way. Managing the pointer is the user's responsibility.
1373 iterator
1374 #if __cplusplus >= 201103L
1375 erase(const_iterator __position) noexcept;
1376 #else
1377 erase(iterator __position);
1378 #endif
1381 * @brief Remove a range of elements.
1382 * @param __first Iterator pointing to the first element to be erased.
1383 * @param __last Iterator pointing to one past the last element to be
1384 * erased.
1385 * @return An iterator pointing to the element pointed to by @a last
1386 * prior to erasing (or end()).
1388 * This function will erase the elements in the range @a
1389 * [first,last) and shorten the %list accordingly.
1391 * This operation is linear time in the size of the range and only
1392 * invalidates iterators/references to the element being removed.
1393 * The user is also cautioned that this function only erases the
1394 * elements, and that if the elements themselves are pointers, the
1395 * pointed-to memory is not touched in any way. Managing the pointer
1396 * is the user's responsibility.
1398 iterator
1399 #if __cplusplus >= 201103L
1400 erase(const_iterator __first, const_iterator __last) noexcept
1401 #else
1402 erase(iterator __first, iterator __last)
1403 #endif
1405 while (__first != __last)
1406 __first = erase(__first);
1407 return __last._M_const_cast();
1411 * @brief Swaps data with another %list.
1412 * @param __x A %list of the same element and allocator types.
1414 * This exchanges the elements between two lists in constant
1415 * time. Note that the global std::swap() function is
1416 * specialized such that std::swap(l1,l2) will feed to this
1417 * function.
1419 * Whether the allocators are swapped depends on the allocator traits.
1421 void
1422 swap(list& __x) _GLIBCXX_NOEXCEPT
1424 __detail::_List_node_base::swap(this->_M_impl._M_node,
1425 __x._M_impl._M_node);
1427 size_t __xsize = __x._M_get_size();
1428 __x._M_set_size(this->_M_get_size());
1429 this->_M_set_size(__xsize);
1431 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1432 __x._M_get_Node_allocator());
1436 * Erases all the elements. Note that this function only erases
1437 * the elements, and that if the elements themselves are
1438 * pointers, the pointed-to memory is not touched in any way.
1439 * Managing the pointer is the user's responsibility.
1441 void
1442 clear() _GLIBCXX_NOEXCEPT
1444 _Base::_M_clear();
1445 _Base::_M_init();
1448 // [23.2.2.4] list operations
1450 * @brief Insert contents of another %list.
1451 * @param __position Iterator referencing the element to insert before.
1452 * @param __x Source list.
1454 * The elements of @a __x are inserted in constant time in front of
1455 * the element referenced by @a __position. @a __x becomes an empty
1456 * list.
1458 * Requires this != @a __x.
1460 void
1461 #if __cplusplus >= 201103L
1462 splice(const_iterator __position, list&& __x) noexcept
1463 #else
1464 splice(iterator __position, list& __x)
1465 #endif
1467 if (!__x.empty())
1469 _M_check_equal_allocators(__x);
1471 this->_M_transfer(__position._M_const_cast(),
1472 __x.begin(), __x.end());
1474 this->_M_inc_size(__x._M_get_size());
1475 __x._M_set_size(0);
1479 #if __cplusplus >= 201103L
1480 void
1481 splice(const_iterator __position, list& __x) noexcept
1482 { splice(__position, std::move(__x)); }
1483 #endif
1485 #if __cplusplus >= 201103L
1487 * @brief Insert element from another %list.
1488 * @param __position Const_iterator referencing the element to
1489 * insert before.
1490 * @param __x Source list.
1491 * @param __i Const_iterator referencing the element to move.
1493 * Removes the element in list @a __x referenced by @a __i and
1494 * inserts it into the current list before @a __position.
1496 void
1497 splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
1498 #else
1500 * @brief Insert element from another %list.
1501 * @param __position Iterator referencing the element to insert before.
1502 * @param __x Source list.
1503 * @param __i Iterator referencing the element to move.
1505 * Removes the element in list @a __x referenced by @a __i and
1506 * inserts it into the current list before @a __position.
1508 void
1509 splice(iterator __position, list& __x, iterator __i)
1510 #endif
1512 iterator __j = __i._M_const_cast();
1513 ++__j;
1514 if (__position == __i || __position == __j)
1515 return;
1517 if (this != std::__addressof(__x))
1518 _M_check_equal_allocators(__x);
1520 this->_M_transfer(__position._M_const_cast(),
1521 __i._M_const_cast(), __j);
1523 this->_M_inc_size(1);
1524 __x._M_dec_size(1);
1527 #if __cplusplus >= 201103L
1529 * @brief Insert element from another %list.
1530 * @param __position Const_iterator referencing the element to
1531 * insert before.
1532 * @param __x Source list.
1533 * @param __i Const_iterator referencing the element to move.
1535 * Removes the element in list @a __x referenced by @a __i and
1536 * inserts it into the current list before @a __position.
1538 void
1539 splice(const_iterator __position, list& __x, const_iterator __i) noexcept
1540 { splice(__position, std::move(__x), __i); }
1541 #endif
1543 #if __cplusplus >= 201103L
1545 * @brief Insert range from another %list.
1546 * @param __position Const_iterator referencing the element to
1547 * insert before.
1548 * @param __x Source list.
1549 * @param __first Const_iterator referencing the start of range in x.
1550 * @param __last Const_iterator referencing the end of range in x.
1552 * Removes elements in the range [__first,__last) and inserts them
1553 * before @a __position in constant time.
1555 * Undefined if @a __position is in [__first,__last).
1557 void
1558 splice(const_iterator __position, list&& __x, const_iterator __first,
1559 const_iterator __last) noexcept
1560 #else
1562 * @brief Insert range from another %list.
1563 * @param __position Iterator referencing the element to insert before.
1564 * @param __x Source list.
1565 * @param __first Iterator referencing the start of range in x.
1566 * @param __last Iterator referencing the end of range in x.
1568 * Removes elements in the range [__first,__last) and inserts them
1569 * before @a __position in constant time.
1571 * Undefined if @a __position is in [__first,__last).
1573 void
1574 splice(iterator __position, list& __x, iterator __first,
1575 iterator __last)
1576 #endif
1578 if (__first != __last)
1580 if (this != std::__addressof(__x))
1581 _M_check_equal_allocators(__x);
1583 size_t __n = this->_M_distance(__first._M_node, __last._M_node);
1584 this->_M_inc_size(__n);
1585 __x._M_dec_size(__n);
1587 this->_M_transfer(__position._M_const_cast(),
1588 __first._M_const_cast(),
1589 __last._M_const_cast());
1593 #if __cplusplus >= 201103L
1595 * @brief Insert range from another %list.
1596 * @param __position Const_iterator referencing the element to
1597 * insert before.
1598 * @param __x Source list.
1599 * @param __first Const_iterator referencing the start of range in x.
1600 * @param __last Const_iterator referencing the end of range in x.
1602 * Removes elements in the range [__first,__last) and inserts them
1603 * before @a __position in constant time.
1605 * Undefined if @a __position is in [__first,__last).
1607 void
1608 splice(const_iterator __position, list& __x, const_iterator __first,
1609 const_iterator __last) noexcept
1610 { splice(__position, std::move(__x), __first, __last); }
1611 #endif
1614 * @brief Remove all elements equal to value.
1615 * @param __value The value to remove.
1617 * Removes every element in the list equal to @a value.
1618 * Remaining elements stay in list order. Note that this
1619 * function only erases the elements, and that if the elements
1620 * themselves are pointers, the pointed-to memory is not
1621 * touched in any way. Managing the pointer is the user's
1622 * responsibility.
1624 void
1625 remove(const _Tp& __value);
1628 * @brief Remove all elements satisfying a predicate.
1629 * @tparam _Predicate Unary predicate function or object.
1631 * Removes every element in the list for which the predicate
1632 * returns true. Remaining elements stay in list order. Note
1633 * that this function only erases the elements, and that if the
1634 * elements themselves are pointers, the pointed-to memory is
1635 * not touched in any way. Managing the pointer is the user's
1636 * responsibility.
1638 template<typename _Predicate>
1639 void
1640 remove_if(_Predicate);
1643 * @brief Remove consecutive duplicate elements.
1645 * For each consecutive set of elements with the same value,
1646 * remove all but the first one. Remaining elements stay in
1647 * list order. Note that this function only erases the
1648 * elements, and that if the elements themselves are pointers,
1649 * the pointed-to memory is not touched in any way. Managing
1650 * the pointer is the user's responsibility.
1652 void
1653 unique();
1656 * @brief Remove consecutive elements satisfying a predicate.
1657 * @tparam _BinaryPredicate Binary predicate function or object.
1659 * For each consecutive set of elements [first,last) that
1660 * satisfy predicate(first,i) where i is an iterator in
1661 * [first,last), remove all but the first one. Remaining
1662 * elements stay in list order. Note that this function only
1663 * erases the elements, and that if the elements themselves are
1664 * pointers, the pointed-to memory is not touched in any way.
1665 * Managing the pointer is the user's responsibility.
1667 template<typename _BinaryPredicate>
1668 void
1669 unique(_BinaryPredicate);
1672 * @brief Merge sorted lists.
1673 * @param __x Sorted list to merge.
1675 * Assumes that both @a __x and this list are sorted according to
1676 * operator<(). Merges elements of @a __x into this list in
1677 * sorted order, leaving @a __x empty when complete. Elements in
1678 * this list precede elements in @a __x that are equal.
1680 #if __cplusplus >= 201103L
1681 void
1682 merge(list&& __x);
1684 void
1685 merge(list& __x)
1686 { merge(std::move(__x)); }
1687 #else
1688 void
1689 merge(list& __x);
1690 #endif
1693 * @brief Merge sorted lists according to comparison function.
1694 * @tparam _StrictWeakOrdering Comparison function defining
1695 * sort order.
1696 * @param __x Sorted list to merge.
1697 * @param __comp Comparison functor.
1699 * Assumes that both @a __x and this list are sorted according to
1700 * StrictWeakOrdering. Merges elements of @a __x into this list
1701 * in sorted order, leaving @a __x empty when complete. Elements
1702 * in this list precede elements in @a __x that are equivalent
1703 * according to StrictWeakOrdering().
1705 #if __cplusplus >= 201103L
1706 template<typename _StrictWeakOrdering>
1707 void
1708 merge(list&& __x, _StrictWeakOrdering __comp);
1710 template<typename _StrictWeakOrdering>
1711 void
1712 merge(list& __x, _StrictWeakOrdering __comp)
1713 { merge(std::move(__x), __comp); }
1714 #else
1715 template<typename _StrictWeakOrdering>
1716 void
1717 merge(list& __x, _StrictWeakOrdering __comp);
1718 #endif
1721 * @brief Reverse the elements in list.
1723 * Reverse the order of elements in the list in linear time.
1725 void
1726 reverse() _GLIBCXX_NOEXCEPT
1727 { this->_M_impl._M_node._M_reverse(); }
1730 * @brief Sort the elements.
1732 * Sorts the elements of this list in NlogN time. Equivalent
1733 * elements remain in list order.
1735 void
1736 sort();
1739 * @brief Sort the elements according to comparison function.
1741 * Sorts the elements of this list in NlogN time. Equivalent
1742 * elements remain in list order.
1744 template<typename _StrictWeakOrdering>
1745 void
1746 sort(_StrictWeakOrdering);
1748 protected:
1749 // Internal constructor functions follow.
1751 // Called by the range constructor to implement [23.1.1]/9
1753 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1754 // 438. Ambiguity in the "do the right thing" clause
1755 template<typename _Integer>
1756 void
1757 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1758 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1760 // Called by the range constructor to implement [23.1.1]/9
1761 template<typename _InputIterator>
1762 void
1763 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1764 __false_type)
1766 for (; __first != __last; ++__first)
1767 #if __cplusplus >= 201103L
1768 emplace_back(*__first);
1769 #else
1770 push_back(*__first);
1771 #endif
1774 // Called by list(n,v,a), and the range constructor when it turns out
1775 // to be the same thing.
1776 void
1777 _M_fill_initialize(size_type __n, const value_type& __x)
1779 for (; __n; --__n)
1780 push_back(__x);
1783 #if __cplusplus >= 201103L
1784 // Called by list(n).
1785 void
1786 _M_default_initialize(size_type __n)
1788 for (; __n; --__n)
1789 emplace_back();
1792 // Called by resize(sz).
1793 void
1794 _M_default_append(size_type __n);
1795 #endif
1797 // Internal assign functions follow.
1799 // Called by the range assign to implement [23.1.1]/9
1801 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1802 // 438. Ambiguity in the "do the right thing" clause
1803 template<typename _Integer>
1804 void
1805 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1806 { _M_fill_assign(__n, __val); }
1808 // Called by the range assign to implement [23.1.1]/9
1809 template<typename _InputIterator>
1810 void
1811 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1812 __false_type);
1814 // Called by assign(n,t), and the range assign when it turns out
1815 // to be the same thing.
1816 void
1817 _M_fill_assign(size_type __n, const value_type& __val);
1820 // Moves the elements from [first,last) before position.
1821 void
1822 _M_transfer(iterator __position, iterator __first, iterator __last)
1823 { __position._M_node->_M_transfer(__first._M_node, __last._M_node); }
1825 // Inserts new element at position given and with value given.
1826 #if __cplusplus < 201103L
1827 void
1828 _M_insert(iterator __position, const value_type& __x)
1830 _Node* __tmp = _M_create_node(__x);
1831 __tmp->_M_hook(__position._M_node);
1832 this->_M_inc_size(1);
1834 #else
1835 template<typename... _Args>
1836 void
1837 _M_insert(iterator __position, _Args&&... __args)
1839 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
1840 __tmp->_M_hook(__position._M_node);
1841 this->_M_inc_size(1);
1843 #endif
1845 // Erases element at position given.
1846 void
1847 _M_erase(iterator __position) _GLIBCXX_NOEXCEPT
1849 this->_M_dec_size(1);
1850 __position._M_node->_M_unhook();
1851 _Node* __n = static_cast<_Node*>(__position._M_node);
1852 #if __cplusplus >= 201103L
1853 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n->_M_valptr());
1854 #else
1855 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n->_M_valptr());
1856 #endif
1858 _M_put_node(__n);
1861 // To implement the splice (and merge) bits of N1599.
1862 void
1863 _M_check_equal_allocators(list& __x) _GLIBCXX_NOEXCEPT
1865 if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
1866 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
1867 __builtin_abort();
1870 // Used to implement resize.
1871 const_iterator
1872 _M_resize_pos(size_type& __new_size) const;
1874 #if __cplusplus >= 201103L
1875 void
1876 _M_move_assign(list&& __x, true_type) noexcept
1878 this->_M_clear();
1879 this->_M_move_nodes(std::move(__x));
1880 std::__alloc_on_move(this->_M_get_Node_allocator(),
1881 __x._M_get_Node_allocator());
1884 void
1885 _M_move_assign(list&& __x, false_type)
1887 if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
1888 _M_move_assign(std::move(__x), true_type{});
1889 else
1890 // The rvalue's allocator cannot be moved, or is not equal,
1891 // so we need to individually move each element.
1892 _M_assign_dispatch(std::__make_move_if_noexcept_iterator(__x.begin()),
1893 std::__make_move_if_noexcept_iterator(__x.end()),
1894 __false_type{});
1896 #endif
1899 #if __cpp_deduction_guides >= 201606
1900 template<typename _InputIterator, typename _ValT
1901 = typename iterator_traits<_InputIterator>::value_type,
1902 typename _Allocator = allocator<_ValT>,
1903 typename = _RequireInputIter<_InputIterator>,
1904 typename = _RequireAllocator<_Allocator>>
1905 list(_InputIterator, _InputIterator, _Allocator = _Allocator())
1906 -> list<_ValT, _Allocator>;
1907 #endif
1909 _GLIBCXX_END_NAMESPACE_CXX11
1912 * @brief List equality comparison.
1913 * @param __x A %list.
1914 * @param __y A %list of the same type as @a __x.
1915 * @return True iff the size and elements of the lists are equal.
1917 * This is an equivalence relation. It is linear in the size of
1918 * the lists. Lists are considered equivalent if their sizes are
1919 * equal, and if corresponding elements compare equal.
1921 template<typename _Tp, typename _Alloc>
1922 inline bool
1923 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1925 #if _GLIBCXX_USE_CXX11_ABI
1926 if (__x.size() != __y.size())
1927 return false;
1928 #endif
1930 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1931 const_iterator __end1 = __x.end();
1932 const_iterator __end2 = __y.end();
1934 const_iterator __i1 = __x.begin();
1935 const_iterator __i2 = __y.begin();
1936 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1938 ++__i1;
1939 ++__i2;
1941 return __i1 == __end1 && __i2 == __end2;
1945 * @brief List ordering relation.
1946 * @param __x A %list.
1947 * @param __y A %list of the same type as @a __x.
1948 * @return True iff @a __x is lexicographically less than @a __y.
1950 * This is a total ordering relation. It is linear in the size of the
1951 * lists. The elements must be comparable with @c <.
1953 * See std::lexicographical_compare() for how the determination is made.
1955 template<typename _Tp, typename _Alloc>
1956 inline bool
1957 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1958 { return std::lexicographical_compare(__x.begin(), __x.end(),
1959 __y.begin(), __y.end()); }
1961 /// Based on operator==
1962 template<typename _Tp, typename _Alloc>
1963 inline bool
1964 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1965 { return !(__x == __y); }
1967 /// Based on operator<
1968 template<typename _Tp, typename _Alloc>
1969 inline bool
1970 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1971 { return __y < __x; }
1973 /// Based on operator<
1974 template<typename _Tp, typename _Alloc>
1975 inline bool
1976 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1977 { return !(__y < __x); }
1979 /// Based on operator<
1980 template<typename _Tp, typename _Alloc>
1981 inline bool
1982 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1983 { return !(__x < __y); }
1985 /// See std::list::swap().
1986 template<typename _Tp, typename _Alloc>
1987 inline void
1988 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1989 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
1990 { __x.swap(__y); }
1992 _GLIBCXX_END_NAMESPACE_CONTAINER
1994 #if _GLIBCXX_USE_CXX11_ABI
1995 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1997 // Detect when distance is used to compute the size of the whole list.
1998 template<typename _Tp>
1999 inline ptrdiff_t
2000 __distance(_GLIBCXX_STD_C::_List_iterator<_Tp> __first,
2001 _GLIBCXX_STD_C::_List_iterator<_Tp> __last,
2002 input_iterator_tag __tag)
2004 typedef _GLIBCXX_STD_C::_List_const_iterator<_Tp> _CIter;
2005 return std::__distance(_CIter(__first), _CIter(__last), __tag);
2008 template<typename _Tp>
2009 inline ptrdiff_t
2010 __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp> __first,
2011 _GLIBCXX_STD_C::_List_const_iterator<_Tp> __last,
2012 input_iterator_tag)
2014 typedef __detail::_List_node_header _Sentinel;
2015 _GLIBCXX_STD_C::_List_const_iterator<_Tp> __beyond = __last;
2016 ++__beyond;
2017 const bool __whole = __first == __beyond;
2018 if (__builtin_constant_p (__whole) && __whole)
2019 return static_cast<const _Sentinel*>(__last._M_node)->_M_size;
2021 ptrdiff_t __n = 0;
2022 while (__first != __last)
2024 ++__first;
2025 ++__n;
2027 return __n;
2030 _GLIBCXX_END_NAMESPACE_VERSION
2031 #endif
2032 } // namespace std
2034 #endif /* _STL_LIST_H */