libstdc++
stl_list.h
Go to the documentation of this file.
1// List implementation -*- C++ -*-
2
3// Copyright (C) 2001-2024 Free Software Foundation, Inc.
4// Copyright The GNU Toolchain Authors.
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
25
26/*
27 *
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
30 *
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
38 *
39 *
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
42 *
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
50 */
51
52/** @file bits/stl_list.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{list}
55 */
56
57#ifndef _STL_LIST_H
58#define _STL_LIST_H 1
59
60#include <bits/concept_check.h>
61#include <ext/alloc_traits.h>
62#if __cplusplus >= 201103L
63#include <initializer_list>
64#include <bits/allocated_ptr.h>
65#include <ext/aligned_buffer.h>
66#endif
67#if __glibcxx_ranges_to_container // C++ >= 23
68# include <bits/ranges_base.h> // ranges::begin, ranges::distance etc.
69# include <bits/ranges_util.h> // ranges::subrange
70#endif
71
72namespace std _GLIBCXX_VISIBILITY(default)
73{
74_GLIBCXX_BEGIN_NAMESPACE_VERSION
75
76 namespace __detail
77 {
78 // Supporting structures are split into common and templated
79 // types; the latter publicly inherits from the former in an
80 // effort to reduce code duplication. This results in some
81 // "needless" static_cast'ing later on, but it's all safe
82 // downcasting.
83
84 /// Common part of a node in the %list.
86 {
87 _List_node_base* _M_next;
88 _List_node_base* _M_prev;
89
90 static void
91 swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT;
92
93 void
94 _M_transfer(_List_node_base* const __first,
95 _List_node_base* const __last) _GLIBCXX_USE_NOEXCEPT;
96
97 void
98 _M_reverse() _GLIBCXX_USE_NOEXCEPT;
99
100 void
101 _M_hook(_List_node_base* const __position) _GLIBCXX_USE_NOEXCEPT;
102
103 void
104 _M_unhook() _GLIBCXX_USE_NOEXCEPT;
105 };
106
107 /// The %list node header.
109 {
110#if _GLIBCXX_USE_CXX11_ABI
111 std::size_t _M_size;
112#endif
113
114 _List_node_header() _GLIBCXX_NOEXCEPT
115 { _M_init(); }
116
117#if __cplusplus >= 201103L
119 : _List_node_base{ __x._M_next, __x._M_prev }
120# if _GLIBCXX_USE_CXX11_ABI
121 , _M_size(__x._M_size)
122# endif
123 {
124 if (__x._M_base()->_M_next == __x._M_base())
125 this->_M_next = this->_M_prev = this;
126 else
127 {
128 this->_M_next->_M_prev = this->_M_prev->_M_next = this->_M_base();
129 __x._M_init();
130 }
131 }
132
133 void
134 _M_move_nodes(_List_node_header&& __x)
135 {
136 _List_node_base* const __xnode = __x._M_base();
137 if (__xnode->_M_next == __xnode)
138 _M_init();
139 else
140 {
141 _List_node_base* const __node = this->_M_base();
142 __node->_M_next = __xnode->_M_next;
143 __node->_M_prev = __xnode->_M_prev;
144 __node->_M_next->_M_prev = __node->_M_prev->_M_next = __node;
145# if _GLIBCXX_USE_CXX11_ABI
146 _M_size = __x._M_size;
147# endif
148 __x._M_init();
149 }
150 }
151#endif
152
153 void
154 _M_init() _GLIBCXX_NOEXCEPT
155 {
156 this->_M_next = this->_M_prev = this;
157#if _GLIBCXX_USE_CXX11_ABI
158 this->_M_size = 0;
159#endif
160 }
161
162 private:
163 _List_node_base* _M_base() { return this; }
164 };
165
166 // Used by list::sort to hold nodes being sorted.
167 struct _Scratch_list : _List_node_base
168 {
169 _Scratch_list() { _M_next = _M_prev = this; }
170
171 bool empty() const { return _M_next == this; }
172
173 void swap(_List_node_base& __l) { _List_node_base::swap(*this, __l); }
174
175 template<typename _Iter, typename _Cmp>
176 struct _Ptr_cmp
177 {
178 _Cmp _M_cmp;
179
180 bool
181 operator()(__detail::_List_node_base* __lhs,
182 __detail::_List_node_base* __rhs) /* not const */
183 { return _M_cmp(*_Iter(__lhs), *_Iter(__rhs)); }
184 };
185
186 template<typename _Iter>
187 struct _Ptr_cmp<_Iter, void>
188 {
189 bool
190 operator()(__detail::_List_node_base* __lhs,
191 __detail::_List_node_base* __rhs) const
192 { return *_Iter(__lhs) < *_Iter(__rhs); }
193 };
194
195 // Merge nodes from __x into *this. Both lists must be sorted wrt _Cmp.
196 template<typename _Cmp>
197 void
198 merge(_List_node_base& __x, _Cmp __comp)
199 {
200 _List_node_base* __first1 = _M_next;
201 _List_node_base* const __last1 = this;
202 _List_node_base* __first2 = __x._M_next;
203 _List_node_base* const __last2 = std::__addressof(__x);
204
205 while (__first1 != __last1 && __first2 != __last2)
206 {
207 if (__comp(__first2, __first1))
208 {
209 _List_node_base* __next = __first2->_M_next;
210 __first1->_M_transfer(__first2, __next);
211 __first2 = __next;
212 }
213 else
214 __first1 = __first1->_M_next;
215 }
216 if (__first2 != __last2)
217 this->_M_transfer(__first2, __last2);
218 }
219
220 // Splice the node at __i into *this.
221 void _M_take_one(_List_node_base* __i)
222 { this->_M_transfer(__i, __i->_M_next); }
223
224 // Splice all nodes from *this after __i.
225 void _M_put_all(_List_node_base* __i)
226 {
227 if (!empty())
228 __i->_M_transfer(_M_next, this);
229 }
230 };
231
232 } // namespace detail
233
234_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
235
236 /// An actual node in the %list.
237 template<typename _Tp>
239 {
240#if __cplusplus >= 201103L
241 __gnu_cxx::__aligned_membuf<_Tp> _M_storage;
242 _Tp* _M_valptr() { return _M_storage._M_ptr(); }
243 _Tp const* _M_valptr() const { return _M_storage._M_ptr(); }
244#else
245 _Tp _M_data;
246 _Tp* _M_valptr() { return std::__addressof(_M_data); }
247 _Tp const* _M_valptr() const { return std::__addressof(_M_data); }
248#endif
249 };
250
251 /**
252 * @brief A list::iterator.
253 *
254 * All the functions are op overloads.
255 */
256 template<typename _Tp>
258 {
259 typedef _List_iterator<_Tp> _Self;
260 typedef _List_node<_Tp> _Node;
261
262 typedef ptrdiff_t difference_type;
264 typedef _Tp value_type;
265 typedef _Tp* pointer;
266 typedef _Tp& reference;
267
268 _List_iterator() _GLIBCXX_NOEXCEPT
269 : _M_node() { }
270
271 explicit
272 _List_iterator(__detail::_List_node_base* __x) _GLIBCXX_NOEXCEPT
273 : _M_node(__x) { }
274
275 _Self
276 _M_const_cast() const _GLIBCXX_NOEXCEPT
277 { return *this; }
278
279 // Must downcast from _List_node_base to _List_node to get to value.
280 _GLIBCXX_NODISCARD
281 reference
282 operator*() const _GLIBCXX_NOEXCEPT
283 { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
284
285 _GLIBCXX_NODISCARD
286 pointer
287 operator->() const _GLIBCXX_NOEXCEPT
288 { return static_cast<_Node*>(_M_node)->_M_valptr(); }
289
290 _Self&
291 operator++() _GLIBCXX_NOEXCEPT
292 {
293 _M_node = _M_node->_M_next;
294 return *this;
295 }
296
297 _Self
298 operator++(int) _GLIBCXX_NOEXCEPT
299 {
300 _Self __tmp = *this;
301 _M_node = _M_node->_M_next;
302 return __tmp;
303 }
304
305 _Self&
306 operator--() _GLIBCXX_NOEXCEPT
307 {
308 _M_node = _M_node->_M_prev;
309 return *this;
310 }
311
312 _Self
313 operator--(int) _GLIBCXX_NOEXCEPT
314 {
315 _Self __tmp = *this;
316 _M_node = _M_node->_M_prev;
317 return __tmp;
318 }
319
320 _GLIBCXX_NODISCARD
321 friend bool
322 operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
323 { return __x._M_node == __y._M_node; }
324
325#if __cpp_impl_three_way_comparison < 201907L
326 _GLIBCXX_NODISCARD
327 friend bool
328 operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
329 { return __x._M_node != __y._M_node; }
330#endif
331
332 // The only member points to the %list element.
334 };
335
336 /**
337 * @brief A list::const_iterator.
338 *
339 * All the functions are op overloads.
340 */
341 template<typename _Tp>
343 {
344 typedef _List_const_iterator<_Tp> _Self;
345 typedef const _List_node<_Tp> _Node;
346 typedef _List_iterator<_Tp> iterator;
347
348 typedef ptrdiff_t difference_type;
350 typedef _Tp value_type;
351 typedef const _Tp* pointer;
352 typedef const _Tp& reference;
353
354 _List_const_iterator() _GLIBCXX_NOEXCEPT
355 : _M_node() { }
356
357 explicit
359 _GLIBCXX_NOEXCEPT
360 : _M_node(__x) { }
361
362 _List_const_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
363 : _M_node(__x._M_node) { }
364
365 iterator
366 _M_const_cast() const _GLIBCXX_NOEXCEPT
367 { return iterator(const_cast<__detail::_List_node_base*>(_M_node)); }
368
369 // Must downcast from List_node_base to _List_node to get to value.
370 _GLIBCXX_NODISCARD
371 reference
372 operator*() const _GLIBCXX_NOEXCEPT
373 { return *static_cast<_Node*>(_M_node)->_M_valptr(); }
374
375 _GLIBCXX_NODISCARD
376 pointer
377 operator->() const _GLIBCXX_NOEXCEPT
378 { return static_cast<_Node*>(_M_node)->_M_valptr(); }
379
380 _Self&
381 operator++() _GLIBCXX_NOEXCEPT
382 {
383 _M_node = _M_node->_M_next;
384 return *this;
385 }
386
387 _Self
388 operator++(int) _GLIBCXX_NOEXCEPT
389 {
390 _Self __tmp = *this;
391 _M_node = _M_node->_M_next;
392 return __tmp;
393 }
394
395 _Self&
396 operator--() _GLIBCXX_NOEXCEPT
397 {
398 _M_node = _M_node->_M_prev;
399 return *this;
400 }
401
402 _Self
403 operator--(int) _GLIBCXX_NOEXCEPT
404 {
405 _Self __tmp = *this;
406 _M_node = _M_node->_M_prev;
407 return __tmp;
408 }
409
410 _GLIBCXX_NODISCARD
411 friend bool
412 operator==(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
413 { return __x._M_node == __y._M_node; }
414
415#if __cpp_impl_three_way_comparison < 201907L
416 _GLIBCXX_NODISCARD
417 friend bool
418 operator!=(const _Self& __x, const _Self& __y) _GLIBCXX_NOEXCEPT
419 { return __x._M_node != __y._M_node; }
420#endif
421
422 // The only member points to the %list element.
423 const __detail::_List_node_base* _M_node;
424 };
425
426_GLIBCXX_BEGIN_NAMESPACE_CXX11
427 /// See bits/stl_deque.h's _Deque_base for an explanation.
428 template<typename _Tp, typename _Alloc>
430 {
431 protected:
433 rebind<_Tp>::other _Tp_alloc_type;
435 typedef typename _Tp_alloc_traits::template
436 rebind<_List_node<_Tp> >::other _Node_alloc_type;
438
439#if !_GLIBCXX_INLINE_VERSION
440 static size_t
441 _S_distance(const __detail::_List_node_base* __first,
442 const __detail::_List_node_base* __last)
443 {
444 size_t __n = 0;
445 while (__first != __last)
446 {
447 __first = __first->_M_next;
448 ++__n;
449 }
450 return __n;
451 }
452#endif
453
454 struct _List_impl
455 : public _Node_alloc_type
456 {
458
459 _List_impl() _GLIBCXX_NOEXCEPT_IF(
461 : _Node_alloc_type()
462 { }
463
464 _List_impl(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
465 : _Node_alloc_type(__a)
466 { }
467
468#if __cplusplus >= 201103L
469 _List_impl(_List_impl&&) = default;
470
471 _List_impl(_Node_alloc_type&& __a, _List_impl&& __x)
472 : _Node_alloc_type(std::move(__a)), _M_node(std::move(__x._M_node))
473 { }
474
475 _List_impl(_Node_alloc_type&& __a) noexcept
476 : _Node_alloc_type(std::move(__a))
477 { }
478#endif
479 };
480
481 _List_impl _M_impl;
482
483#if _GLIBCXX_USE_CXX11_ABI
484 size_t _M_get_size() const { return _M_impl._M_node._M_size; }
485
486 void _M_set_size(size_t __n) { _M_impl._M_node._M_size = __n; }
487
488 void _M_inc_size(size_t __n) { _M_impl._M_node._M_size += __n; }
489
490 void _M_dec_size(size_t __n) { _M_impl._M_node._M_size -= __n; }
491
492# if !_GLIBCXX_INLINE_VERSION
493 size_t
494 _M_distance(const __detail::_List_node_base* __first,
495 const __detail::_List_node_base* __last) const
496 { return _S_distance(__first, __last); }
497
498 // return the stored size
499 size_t _M_node_count() const { return _M_get_size(); }
500# endif
501#else
502 // dummy implementations used when the size is not stored
503 size_t _M_get_size() const { return 0; }
504 void _M_set_size(size_t) { }
505 void _M_inc_size(size_t) { }
506 void _M_dec_size(size_t) { }
507
508# if !_GLIBCXX_INLINE_VERSION
509 size_t _M_distance(const void*, const void*) const { return 0; }
510
511 // count the number of nodes
512 size_t _M_node_count() const
513 {
514 return _S_distance(_M_impl._M_node._M_next,
515 std::__addressof(_M_impl._M_node));
516 }
517# endif
518#endif
519
520 typename _Node_alloc_traits::pointer
521 _M_get_node()
522 { return _Node_alloc_traits::allocate(_M_impl, 1); }
523
524 void
525 _M_put_node(typename _Node_alloc_traits::pointer __p) _GLIBCXX_NOEXCEPT
526 { _Node_alloc_traits::deallocate(_M_impl, __p, 1); }
527
528 public:
529 typedef _Alloc allocator_type;
530
531 _Node_alloc_type&
532 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
533 { return _M_impl; }
534
535 const _Node_alloc_type&
536 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
537 { return _M_impl; }
538
539#if __cplusplus >= 201103L
540 _List_base() = default;
541#else
542 _List_base() { }
543#endif
544
545 _List_base(const _Node_alloc_type& __a) _GLIBCXX_NOEXCEPT
546 : _M_impl(__a)
547 { }
548
549#if __cplusplus >= 201103L
550 _List_base(_List_base&&) = default;
551
552# if !_GLIBCXX_INLINE_VERSION
553 _List_base(_List_base&& __x, _Node_alloc_type&& __a)
554 : _M_impl(std::move(__a))
555 {
556 if (__x._M_get_Node_allocator() == _M_get_Node_allocator())
557 _M_move_nodes(std::move(__x));
558 // else caller must move individual elements.
559 }
560# endif
561
562 // Used when allocator is_always_equal.
563 _List_base(_Node_alloc_type&& __a, _List_base&& __x)
564 : _M_impl(std::move(__a), std::move(__x._M_impl))
565 { }
566
567 // Used when allocator !is_always_equal.
568 _List_base(_Node_alloc_type&& __a)
569 : _M_impl(std::move(__a))
570 { }
571
572 void
573 _M_move_nodes(_List_base&& __x)
574 { _M_impl._M_node._M_move_nodes(std::move(__x._M_impl._M_node)); }
575#endif
576
577 // This is what actually destroys the list.
578 ~_List_base() _GLIBCXX_NOEXCEPT
579 { _M_clear(); }
580
581 void
582 _M_clear() _GLIBCXX_NOEXCEPT;
583
584 void
585 _M_init() _GLIBCXX_NOEXCEPT
586 { this->_M_impl._M_node._M_init(); }
587 };
588
589 /**
590 * @brief A standard container with linear time access to elements,
591 * and fixed time insertion/deletion at any point in the sequence.
592 *
593 * @ingroup sequences
594 *
595 * @tparam _Tp Type of element.
596 * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
597 *
598 * Meets the requirements of a <a href="tables.html#65">container</a>, a
599 * <a href="tables.html#66">reversible container</a>, and a
600 * <a href="tables.html#67">sequence</a>, including the
601 * <a href="tables.html#68">optional sequence requirements</a> with the
602 * %exception of @c at and @c operator[].
603 *
604 * This is a @e doubly @e linked %list. Traversal up and down the
605 * %list requires linear time, but adding and removing elements (or
606 * @e nodes) is done in constant time, regardless of where the
607 * change takes place. Unlike std::vector and std::deque,
608 * random-access iterators are not provided, so subscripting ( @c
609 * [] ) access is not allowed. For algorithms which only need
610 * sequential access, this lack makes no difference.
611 *
612 * Also unlike the other standard containers, std::list provides
613 * specialized algorithms %unique to linked lists, such as
614 * splicing, sorting, and in-place reversal.
615 *
616 * A couple points on memory allocation for list<Tp>:
617 *
618 * First, we never actually allocate a Tp, we allocate
619 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
620 * that after elements from %list<X,Alloc1> are spliced into
621 * %list<X,Alloc2>, destroying the memory of the second %list is a
622 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
623 *
624 * Second, a %list conceptually represented as
625 * @code
626 * A <---> B <---> C <---> D
627 * @endcode
628 * is actually circular; a link exists between A and D. The %list
629 * class holds (as its only data member) a private list::iterator
630 * pointing to @e D, not to @e A! To get to the head of the %list,
631 * we start at the tail and move forward by one. When this member
632 * iterator's next/previous pointers refer to itself, the %list is
633 * %empty.
634 */
635 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
636 class list : protected _List_base<_Tp, _Alloc>
637 {
638#ifdef _GLIBCXX_CONCEPT_CHECKS
639 // concept requirements
640 typedef typename _Alloc::value_type _Alloc_value_type;
641# if __cplusplus < 201103L
642 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
643# endif
644 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
645#endif
646
647#if __cplusplus >= 201103L
648 static_assert(is_same<typename remove_cv<_Tp>::type, _Tp>::value,
649 "std::list must have a non-const, non-volatile value_type");
650# if __cplusplus > 201703L || defined __STRICT_ANSI__
652 "std::list must have the same value_type as its allocator");
653# endif
654#endif
655
657 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
659 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
661
662 public:
663 typedef _Tp value_type;
664 typedef typename _Tp_alloc_traits::pointer pointer;
665 typedef typename _Tp_alloc_traits::const_pointer const_pointer;
666 typedef typename _Tp_alloc_traits::reference reference;
667 typedef typename _Tp_alloc_traits::const_reference const_reference;
668 typedef _List_iterator<_Tp> iterator;
672 typedef size_t size_type;
673 typedef ptrdiff_t difference_type;
674 typedef _Alloc allocator_type;
675
676 protected:
677 // Note that pointers-to-_Node's can be ctor-converted to
678 // iterator types.
679 typedef _List_node<_Tp> _Node;
680
681 using _Base::_M_impl;
682 using _Base::_M_put_node;
683 using _Base::_M_get_node;
684 using _Base::_M_get_Node_allocator;
685
686 /**
687 * @param __args An instance of user data.
688 *
689 * Allocates space for a new node and constructs a copy of
690 * @a __args in it.
691 */
692#if __cplusplus < 201103L
693 _Node*
694 _M_create_node(const value_type& __x)
695 {
696 _Node* __p = this->_M_get_node();
697 __try
698 {
699 _Tp_alloc_type __alloc(_M_get_Node_allocator());
700 __alloc.construct(__p->_M_valptr(), __x);
701 }
702 __catch(...)
703 {
704 _M_put_node(__p);
705 __throw_exception_again;
706 }
707 return __p;
708 }
709#else
710 template<typename... _Args>
711 _Node*
712 _M_create_node(_Args&&... __args)
713 {
714 auto __p = this->_M_get_node();
715 auto& __alloc = _M_get_Node_allocator();
716 __allocated_ptr<_Node_alloc_type> __guard{__alloc, __p};
717 _Node_alloc_traits::construct(__alloc, __p->_M_valptr(),
718 std::forward<_Args>(__args)...);
719 __guard = nullptr;
720 return __p;
721 }
722#endif
723
724#if _GLIBCXX_USE_CXX11_ABI
725 static size_t
726 _S_distance(const_iterator __first, const_iterator __last)
727 { return std::distance(__first, __last); }
728
729 // return the stored size
730 size_t
731 _M_node_count() const
732 { return this->_M_get_size(); }
733#else
734 // dummy implementations used when the size is not stored
735 static size_t
736 _S_distance(const_iterator, const_iterator)
737 { return 0; }
738
739 // count the number of nodes
740 size_t
741 _M_node_count() const
742 { return std::distance(begin(), end()); }
743#endif
744
745 public:
746 // [23.2.2.1] construct/copy/destroy
747 // (assign() and get_allocator() are also listed in this section)
748
749 /**
750 * @brief Creates a %list with no elements.
751 */
752#if __cplusplus >= 201103L
753 list() = default;
754#else
755 list() { }
756#endif
757
758 /**
759 * @brief Creates a %list with no elements.
760 * @param __a An allocator object.
761 */
762 explicit
763 list(const allocator_type& __a) _GLIBCXX_NOEXCEPT
764 : _Base(_Node_alloc_type(__a)) { }
765
766#if __cplusplus >= 201103L
767 /**
768 * @brief Creates a %list with default constructed elements.
769 * @param __n The number of elements to initially create.
770 * @param __a An allocator object.
771 *
772 * This constructor fills the %list with @a __n default
773 * constructed elements.
774 */
775 explicit
776 list(size_type __n, const allocator_type& __a = allocator_type())
777 : _Base(_Node_alloc_type(__a))
778 { _M_default_initialize(__n); }
779
780 /**
781 * @brief Creates a %list with copies of an exemplar element.
782 * @param __n The number of elements to initially create.
783 * @param __value An element to copy.
784 * @param __a An allocator object.
785 *
786 * This constructor fills the %list with @a __n copies of @a __value.
787 */
788 list(size_type __n, const value_type& __value,
789 const allocator_type& __a = allocator_type())
790 : _Base(_Node_alloc_type(__a))
791 { _M_fill_initialize(__n, __value); }
792#else
793 /**
794 * @brief Creates a %list with copies of an exemplar element.
795 * @param __n The number of elements to initially create.
796 * @param __value An element to copy.
797 * @param __a An allocator object.
798 *
799 * This constructor fills the %list with @a __n copies of @a __value.
800 */
801 explicit
802 list(size_type __n, const value_type& __value = value_type(),
803 const allocator_type& __a = allocator_type())
804 : _Base(_Node_alloc_type(__a))
805 { _M_fill_initialize(__n, __value); }
806#endif
807
808 /**
809 * @brief %List copy constructor.
810 * @param __x A %list of identical element and allocator types.
811 *
812 * The newly-created %list uses a copy of the allocation object used
813 * by @a __x (unless the allocator traits dictate a different object).
814 */
815 list(const list& __x)
817 _S_select_on_copy(__x._M_get_Node_allocator()))
818 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
819
820#if __cplusplus >= 201103L
821 /**
822 * @brief %List move constructor.
823 *
824 * The newly-created %list contains the exact contents of the moved
825 * instance. The contents of the moved instance are a valid, but
826 * unspecified %list.
827 */
828 list(list&&) = default;
829
830 /**
831 * @brief Builds a %list from an initializer_list
832 * @param __l An initializer_list of value_type.
833 * @param __a An allocator object.
834 *
835 * Create a %list consisting of copies of the elements in the
836 * initializer_list @a __l. This is linear in __l.size().
837 */
839 const allocator_type& __a = allocator_type())
840 : _Base(_Node_alloc_type(__a))
841 { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
842
843 list(const list& __x, const __type_identity_t<allocator_type>& __a)
844 : _Base(_Node_alloc_type(__a))
845 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
846
847 private:
848 list(list&& __x, const allocator_type& __a, true_type) noexcept
849 : _Base(_Node_alloc_type(__a), std::move(__x))
850 { }
851
852 list(list&& __x, const allocator_type& __a, false_type)
853 : _Base(_Node_alloc_type(__a))
854 {
855 if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
856 this->_M_move_nodes(std::move(__x));
857 else
858 insert(begin(), std::__make_move_if_noexcept_iterator(__x.begin()),
859 std::__make_move_if_noexcept_iterator(__x.end()));
860 }
861
862 public:
863 list(list&& __x, const __type_identity_t<allocator_type>& __a)
864 noexcept(_Node_alloc_traits::_S_always_equal())
865 : list(std::move(__x), __a,
866 typename _Node_alloc_traits::is_always_equal{})
867 { }
868#endif
869
870 /**
871 * @brief Builds a %list from a range.
872 * @param __first An input iterator.
873 * @param __last An input iterator.
874 * @param __a An allocator object.
875 *
876 * Create a %list consisting of copies of the elements from
877 * [@a __first,@a __last). This is linear in N (where N is
878 * distance(@a __first,@a __last)).
879 */
880#if __cplusplus >= 201103L
881 template<typename _InputIterator,
882 typename = std::_RequireInputIter<_InputIterator>>
883 list(_InputIterator __first, _InputIterator __last,
884 const allocator_type& __a = allocator_type())
885 : _Base(_Node_alloc_type(__a))
886 { _M_initialize_dispatch(__first, __last, __false_type()); }
887#else
888 template<typename _InputIterator>
889 list(_InputIterator __first, _InputIterator __last,
890 const allocator_type& __a = allocator_type())
891 : _Base(_Node_alloc_type(__a))
892 {
893 // Check whether it's an integral type. If so, it's not an iterator.
894 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
895 _M_initialize_dispatch(__first, __last, _Integral());
896 }
897#endif
898
899#if __glibcxx_ranges_to_container // C++ >= 23
900 /**
901 * @brief Construct a list from a range.
902 * @since C++23
903 */
904 template<__detail::__container_compatible_range<_Tp> _Rg>
905 list(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
906 : _Base(_Node_alloc_type(__a))
907 {
908 auto __first = ranges::begin(__rg);
909 const auto __last = ranges::end(__rg);
910 for (; __first != __last; ++__first)
911 emplace_back(*__first);
912 }
913#endif
914
915#if __cplusplus >= 201103L
916 /**
917 * No explicit dtor needed as the _Base dtor takes care of
918 * things. The _Base dtor only erases the elements, and note
919 * that if the elements themselves are pointers, the pointed-to
920 * memory is not touched in any way. Managing the pointer is
921 * the user's responsibility.
922 */
923 ~list() = default;
924#endif
925
926 /**
927 * @brief %List assignment operator.
928 * @param __x A %list of identical element and allocator types.
929 *
930 * All the elements of @a __x are copied.
931 *
932 * Whether the allocator is copied depends on the allocator traits.
933 */
934 list&
935 operator=(const list& __x);
936
937#if __cplusplus >= 201103L
938 /**
939 * @brief %List move assignment operator.
940 * @param __x A %list of identical element and allocator types.
941 *
942 * The contents of @a __x are moved into this %list (without copying).
943 *
944 * Afterwards @a __x is a valid, but unspecified %list
945 *
946 * Whether the allocator is moved depends on the allocator traits.
947 */
948 list&
950 noexcept(_Node_alloc_traits::_S_nothrow_move())
951 {
952 constexpr bool __move_storage =
953 _Node_alloc_traits::_S_propagate_on_move_assign()
954 || _Node_alloc_traits::_S_always_equal();
955 _M_move_assign(std::move(__x), __bool_constant<__move_storage>());
956 return *this;
957 }
958
959 /**
960 * @brief %List initializer list assignment operator.
961 * @param __l An initializer_list of value_type.
962 *
963 * Replace the contents of the %list with copies of the elements
964 * in the initializer_list @a __l. This is linear in l.size().
965 */
966 list&
968 {
969 this->assign(__l.begin(), __l.end());
970 return *this;
971 }
972#endif
973
974#if __glibcxx_ranges_to_container // C++ >= 23
975 /**
976 * @brief Assign a range to a list.
977 * @since C++23
978 */
979 template<__detail::__container_compatible_range<_Tp> _Rg>
980 void
981 assign_range(_Rg&& __rg)
982 {
983 static_assert(assignable_from<_Tp&, ranges::range_reference_t<_Rg>>);
984
985 iterator __first1 = begin();
986 const iterator __last1 = end();
987 auto __first2 = ranges::begin(__rg);
988 const auto __last2 = ranges::end(__rg);
989 for (; __first1 != __last1 && __first2 != __last2;
990 ++__first1, (void)++__first2)
991 *__first1 = *__first2;
992 if (__first2 == __last2)
993 erase(__first1, __last1);
994 else
995 insert_range(__last1,
996 ranges::subrange(std::move(__first2), __last2));
997 }
998#endif
999
1000 /**
1001 * @brief Assigns a given value to a %list.
1002 * @param __n Number of elements to be assigned.
1003 * @param __val Value to be assigned.
1004 *
1005 * This function fills a %list with @a __n copies of the given
1006 * value. Note that the assignment completely changes the %list
1007 * and that the resulting %list's size is the same as the number
1008 * of elements assigned.
1009 */
1010 void
1011 assign(size_type __n, const value_type& __val)
1012 { _M_fill_assign(__n, __val); }
1013
1014 /**
1015 * @brief Assigns a range to a %list.
1016 * @param __first An input iterator.
1017 * @param __last An input iterator.
1018 *
1019 * This function fills a %list with copies of the elements in the
1020 * range [@a __first,@a __last).
1021 *
1022 * Note that the assignment completely changes the %list and
1023 * that the resulting %list's size is the same as the number of
1024 * elements assigned.
1025 */
1026#if __cplusplus >= 201103L
1027 template<typename _InputIterator,
1028 typename = std::_RequireInputIter<_InputIterator>>
1029 void
1030 assign(_InputIterator __first, _InputIterator __last)
1031 { _M_assign_dispatch(__first, __last, __false_type()); }
1032#else
1033 template<typename _InputIterator>
1034 void
1035 assign(_InputIterator __first, _InputIterator __last)
1036 {
1037 // Check whether it's an integral type. If so, it's not an iterator.
1038 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1039 _M_assign_dispatch(__first, __last, _Integral());
1040 }
1041#endif
1042
1043#if __cplusplus >= 201103L
1044 /**
1045 * @brief Assigns an initializer_list to a %list.
1046 * @param __l An initializer_list of value_type.
1047 *
1048 * Replace the contents of the %list with copies of the elements
1049 * in the initializer_list @a __l. This is linear in __l.size().
1050 */
1051 void
1053 { this->_M_assign_dispatch(__l.begin(), __l.end(), __false_type()); }
1054#endif
1055
1056 /// Get a copy of the memory allocation object.
1057 allocator_type
1058 get_allocator() const _GLIBCXX_NOEXCEPT
1059 { return allocator_type(_Base::_M_get_Node_allocator()); }
1060
1061 // iterators
1062 /**
1063 * Returns a read/write iterator that points to the first element in the
1064 * %list. Iteration is done in ordinary element order.
1065 */
1066 _GLIBCXX_NODISCARD
1067 iterator
1068 begin() _GLIBCXX_NOEXCEPT
1069 { return iterator(this->_M_impl._M_node._M_next); }
1070
1071 /**
1072 * Returns a read-only (constant) iterator that points to the
1073 * first element in the %list. Iteration is done in ordinary
1074 * element order.
1075 */
1076 _GLIBCXX_NODISCARD
1077 const_iterator
1078 begin() const _GLIBCXX_NOEXCEPT
1079 { return const_iterator(this->_M_impl._M_node._M_next); }
1080
1081 /**
1082 * Returns a read/write iterator that points one past the last
1083 * element in the %list. Iteration is done in ordinary element
1084 * order.
1085 */
1086 _GLIBCXX_NODISCARD
1087 iterator
1088 end() _GLIBCXX_NOEXCEPT
1089 { return iterator(&this->_M_impl._M_node); }
1090
1091 /**
1092 * Returns a read-only (constant) iterator that points one past
1093 * the last element in the %list. Iteration is done in ordinary
1094 * element order.
1095 */
1096 _GLIBCXX_NODISCARD
1097 const_iterator
1098 end() const _GLIBCXX_NOEXCEPT
1099 { return const_iterator(&this->_M_impl._M_node); }
1100
1101 /**
1102 * Returns a read/write reverse iterator that points to the last
1103 * element in the %list. Iteration is done in reverse element
1104 * order.
1105 */
1106 _GLIBCXX_NODISCARD
1108 rbegin() _GLIBCXX_NOEXCEPT
1109 { return reverse_iterator(end()); }
1110
1111 /**
1112 * Returns a read-only (constant) reverse iterator that points to
1113 * the last element in the %list. Iteration is done in reverse
1114 * element order.
1115 */
1116 _GLIBCXX_NODISCARD
1117 const_reverse_iterator
1118 rbegin() const _GLIBCXX_NOEXCEPT
1119 { return const_reverse_iterator(end()); }
1120
1121 /**
1122 * Returns a read/write reverse iterator that points to one
1123 * before the first element in the %list. Iteration is done in
1124 * reverse element order.
1125 */
1126 _GLIBCXX_NODISCARD
1128 rend() _GLIBCXX_NOEXCEPT
1129 { return reverse_iterator(begin()); }
1130
1131 /**
1132 * Returns a read-only (constant) reverse iterator that points to one
1133 * before the first element in the %list. Iteration is done in reverse
1134 * element order.
1135 */
1136 _GLIBCXX_NODISCARD
1137 const_reverse_iterator
1138 rend() const _GLIBCXX_NOEXCEPT
1139 { return const_reverse_iterator(begin()); }
1140
1141#if __cplusplus >= 201103L
1142 /**
1143 * Returns a read-only (constant) iterator that points to the
1144 * first element in the %list. Iteration is done in ordinary
1145 * element order.
1146 */
1147 [[__nodiscard__]]
1148 const_iterator
1149 cbegin() const noexcept
1150 { return const_iterator(this->_M_impl._M_node._M_next); }
1151
1152 /**
1153 * Returns a read-only (constant) iterator that points one past
1154 * the last element in the %list. Iteration is done in ordinary
1155 * element order.
1156 */
1157 [[__nodiscard__]]
1158 const_iterator
1159 cend() const noexcept
1160 { return const_iterator(&this->_M_impl._M_node); }
1161
1162 /**
1163 * Returns a read-only (constant) reverse iterator that points to
1164 * the last element in the %list. Iteration is done in reverse
1165 * element order.
1166 */
1167 [[__nodiscard__]]
1168 const_reverse_iterator
1169 crbegin() const noexcept
1170 { return const_reverse_iterator(end()); }
1171
1172 /**
1173 * Returns a read-only (constant) reverse iterator that points to one
1174 * before the first element in the %list. Iteration is done in reverse
1175 * element order.
1176 */
1177 [[__nodiscard__]]
1178 const_reverse_iterator
1179 crend() const noexcept
1180 { return const_reverse_iterator(begin()); }
1181#endif
1182
1183 // [23.2.2.2] capacity
1184 /**
1185 * Returns true if the %list is empty. (Thus begin() would equal
1186 * end().)
1187 */
1188 _GLIBCXX_NODISCARD bool
1189 empty() const _GLIBCXX_NOEXCEPT
1190 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
1191
1192 /** Returns the number of elements in the %list. */
1193 _GLIBCXX_NODISCARD
1194 size_type
1195 size() const _GLIBCXX_NOEXCEPT
1196 { return _M_node_count(); }
1197
1198 /** Returns the size() of the largest possible %list. */
1199 _GLIBCXX_NODISCARD
1200 size_type
1201 max_size() const _GLIBCXX_NOEXCEPT
1202 { return _Node_alloc_traits::max_size(_M_get_Node_allocator()); }
1203
1204#if __cplusplus >= 201103L
1205 /**
1206 * @brief Resizes the %list to the specified number of elements.
1207 * @param __new_size Number of elements the %list should contain.
1208 *
1209 * This function will %resize the %list to the specified number
1210 * of elements. If the number is smaller than the %list's
1211 * current size the %list is truncated, otherwise default
1212 * constructed elements are appended.
1213 */
1214 void
1215 resize(size_type __new_size);
1216
1217 /**
1218 * @brief Resizes the %list to the specified number of elements.
1219 * @param __new_size Number of elements the %list should contain.
1220 * @param __x Data with which new elements should be populated.
1221 *
1222 * This function will %resize the %list to the specified number
1223 * of elements. If the number is smaller than the %list's
1224 * current size the %list is truncated, otherwise the %list is
1225 * extended and new elements are populated with given data.
1226 */
1227 void
1228 resize(size_type __new_size, const value_type& __x);
1229#else
1230 /**
1231 * @brief Resizes the %list to the specified number of elements.
1232 * @param __new_size Number of elements the %list should contain.
1233 * @param __x Data with which new elements should be populated.
1234 *
1235 * This function will %resize the %list to the specified number
1236 * of elements. If the number is smaller than the %list's
1237 * current size the %list is truncated, otherwise the %list is
1238 * extended and new elements are populated with given data.
1239 */
1240 void
1241 resize(size_type __new_size, value_type __x = value_type());
1242#endif
1243
1244 // element access
1245 /**
1246 * Returns a read/write reference to the data at the first
1247 * element of the %list.
1248 */
1249 _GLIBCXX_NODISCARD
1250 reference
1251 front() _GLIBCXX_NOEXCEPT
1252 { return *begin(); }
1253
1254 /**
1255 * Returns a read-only (constant) reference to the data at the first
1256 * element of the %list.
1257 */
1258 _GLIBCXX_NODISCARD
1259 const_reference
1260 front() const _GLIBCXX_NOEXCEPT
1261 { return *begin(); }
1262
1263 /**
1264 * Returns a read/write reference to the data at the last element
1265 * of the %list.
1266 */
1267 _GLIBCXX_NODISCARD
1268 reference
1269 back() _GLIBCXX_NOEXCEPT
1270 {
1271 iterator __tmp = end();
1272 --__tmp;
1273 return *__tmp;
1274 }
1275
1276 /**
1277 * Returns a read-only (constant) reference to the data at the last
1278 * element of the %list.
1279 */
1280 _GLIBCXX_NODISCARD
1281 const_reference
1282 back() const _GLIBCXX_NOEXCEPT
1283 {
1284 const_iterator __tmp = end();
1285 --__tmp;
1286 return *__tmp;
1287 }
1288
1289 // [23.2.2.3] modifiers
1290 /**
1291 * @brief Add data to the front of the %list.
1292 * @param __x Data to be added.
1293 *
1294 * This is a typical stack operation. The function creates an
1295 * element at the front of the %list and assigns the given data
1296 * to it. Due to the nature of a %list this operation can be
1297 * done in constant time, and does not invalidate iterators and
1298 * references.
1299 */
1300 void
1301 push_front(const value_type& __x)
1302 { this->_M_insert(begin(), __x); }
1303
1304#if __cplusplus >= 201103L
1305 void
1306 push_front(value_type&& __x)
1307 { this->_M_insert(begin(), std::move(__x)); }
1308
1309 template<typename... _Args>
1310#if __cplusplus > 201402L
1311 reference
1312#else
1313 void
1314#endif
1315 emplace_front(_Args&&... __args)
1316 {
1317 this->_M_insert(begin(), std::forward<_Args>(__args)...);
1318#if __cplusplus > 201402L
1319 return front();
1320#endif
1321 }
1322#endif
1323
1324#if __glibcxx_ranges_to_container // C++ >= 23
1325 /**
1326 * @brief Insert a range at the beginning of a list.
1327 * @param __rg An input range of elements that can be converted to
1328 * the list's value type.
1329 *
1330 * Inserts the elements of `__rg` at the beginning of the list.
1331 * No iterators to existing elements are invalidated by this function.
1332 * If the insertion fails due to an exception, no elements will be added
1333 * and so the list will be unchanged.
1334 *
1335 * @since C++23
1336 */
1337 template<__detail::__container_compatible_range<_Tp> _Rg>
1338 void
1339 prepend_range(_Rg&& __rg)
1340 {
1341 list __tmp(from_range, std::forward<_Rg>(__rg), get_allocator());
1342 if (!__tmp.empty())
1343 splice(begin(), __tmp);
1344 }
1345
1346 /**
1347 * @brief Insert a range at the end of a list.
1348 * @param __rg An input range of elements that can be converted to
1349 * the list's value type.
1350 *
1351 * Inserts the elements of `__rg` at the end of the list.
1352 * No iterators to existing elements are invalidated by this function.
1353 * If the insertion fails due to an exception, no elements will be added
1354 * and so the list will be unchanged.
1355 *
1356 * @since C++23
1357 */
1358 template<__detail::__container_compatible_range<_Tp> _Rg>
1359 void
1360 append_range(_Rg&& __rg)
1361 {
1362 list __tmp(from_range, std::forward<_Rg>(__rg), get_allocator());
1363 if (!__tmp.empty())
1364 splice(end(), __tmp);
1365 }
1366#endif
1367
1368 /**
1369 * @brief Removes first element.
1370 *
1371 * This is a typical stack operation. It shrinks the %list by
1372 * one. Due to the nature of a %list this operation can be done
1373 * in constant time, and only invalidates iterators/references to
1374 * the element being removed.
1375 *
1376 * Note that no data is returned, and if the first element's data
1377 * is needed, it should be retrieved before pop_front() is
1378 * called.
1379 */
1380 void
1381 pop_front() _GLIBCXX_NOEXCEPT
1382 { this->_M_erase(begin()); }
1383
1384 /**
1385 * @brief Add data to the end of the %list.
1386 * @param __x Data to be added.
1387 *
1388 * This is a typical stack operation. The function creates an
1389 * element at the end of the %list and assigns the given data to
1390 * it. Due to the nature of a %list this operation can be done
1391 * in constant time, and does not invalidate iterators and
1392 * references.
1393 */
1394 void
1395 push_back(const value_type& __x)
1396 { this->_M_insert(end(), __x); }
1397
1398#if __cplusplus >= 201103L
1399 void
1400 push_back(value_type&& __x)
1401 { this->_M_insert(end(), std::move(__x)); }
1402
1403 template<typename... _Args>
1404#if __cplusplus > 201402L
1405 reference
1406#else
1407 void
1408#endif
1409 emplace_back(_Args&&... __args)
1410 {
1411 this->_M_insert(end(), std::forward<_Args>(__args)...);
1412#if __cplusplus > 201402L
1413 return back();
1414#endif
1415 }
1416#endif
1417
1418 /**
1419 * @brief Removes last element.
1420 *
1421 * This is a typical stack operation. It shrinks the %list by
1422 * one. Due to the nature of a %list this operation can be done
1423 * in constant time, and only invalidates iterators/references to
1424 * the element being removed.
1425 *
1426 * Note that no data is returned, and if the last element's data
1427 * is needed, it should be retrieved before pop_back() is called.
1428 */
1429 void
1430 pop_back() _GLIBCXX_NOEXCEPT
1431 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
1432
1433#if __cplusplus >= 201103L
1434 /**
1435 * @brief Constructs object in %list before specified iterator.
1436 * @param __position A const_iterator into the %list.
1437 * @param __args Arguments.
1438 * @return An iterator that points to the inserted data.
1439 *
1440 * This function will insert an object of type T constructed
1441 * with T(std::forward<Args>(args)...) before the specified
1442 * location. Due to the nature of a %list this operation can
1443 * be done in constant time, and does not invalidate iterators
1444 * and references.
1445 */
1446 template<typename... _Args>
1447 iterator
1448 emplace(const_iterator __position, _Args&&... __args);
1449
1450 /**
1451 * @brief Inserts given value into %list before specified iterator.
1452 * @param __position A const_iterator into the %list.
1453 * @param __x Data to be inserted.
1454 * @return An iterator that points to the inserted data.
1455 *
1456 * This function will insert a copy of the given value before
1457 * the specified location. Due to the nature of a %list this
1458 * operation can be done in constant time, and does not
1459 * invalidate iterators and references.
1460 */
1461 iterator
1462 insert(const_iterator __position, const value_type& __x);
1463#else
1464 /**
1465 * @brief Inserts given value into %list before specified iterator.
1466 * @param __position An iterator into the %list.
1467 * @param __x Data to be inserted.
1468 * @return An iterator that points to the inserted data.
1469 *
1470 * This function will insert a copy of the given value before
1471 * the specified location. Due to the nature of a %list this
1472 * operation can be done in constant time, and does not
1473 * invalidate iterators and references.
1474 */
1475 iterator
1476 insert(iterator __position, const value_type& __x);
1477#endif
1478
1479#if __cplusplus >= 201103L
1480 /**
1481 * @brief Inserts given rvalue into %list before specified iterator.
1482 * @param __position A const_iterator into the %list.
1483 * @param __x Data to be inserted.
1484 * @return An iterator that points to the inserted data.
1485 *
1486 * This function will insert a copy of the given rvalue before
1487 * the specified location. Due to the nature of a %list this
1488 * operation can be done in constant time, and does not
1489 * invalidate iterators and references.
1490 */
1491 iterator
1492 insert(const_iterator __position, value_type&& __x)
1493 { return emplace(__position, std::move(__x)); }
1494
1495 /**
1496 * @brief Inserts the contents of an initializer_list into %list
1497 * before specified const_iterator.
1498 * @param __p A const_iterator into the %list.
1499 * @param __l An initializer_list of value_type.
1500 * @return An iterator pointing to the first element inserted
1501 * (or __position).
1502 *
1503 * This function will insert copies of the data in the
1504 * initializer_list @a l into the %list before the location
1505 * specified by @a p.
1506 *
1507 * This operation is linear in the number of elements inserted and
1508 * does not invalidate iterators and references.
1509 */
1510 iterator
1512 { return this->insert(__p, __l.begin(), __l.end()); }
1513#endif
1514
1515#if __cplusplus >= 201103L
1516 /**
1517 * @brief Inserts a number of copies of given data into the %list.
1518 * @param __position A const_iterator into the %list.
1519 * @param __n Number of elements to be inserted.
1520 * @param __x Data to be inserted.
1521 * @return An iterator pointing to the first element inserted
1522 * (or __position).
1523 *
1524 * This function will insert a specified number of copies of the
1525 * given data before the location specified by @a position.
1526 *
1527 * This operation is linear in the number of elements inserted and
1528 * does not invalidate iterators and references.
1529 */
1530 iterator
1531 insert(const_iterator __position, size_type __n, const value_type& __x);
1532#else
1533 /**
1534 * @brief Inserts a number of copies of given data into the %list.
1535 * @param __position An iterator into the %list.
1536 * @param __n Number of elements to be inserted.
1537 * @param __x Data to be inserted.
1538 *
1539 * This function will insert a specified number of copies of the
1540 * given data before the location specified by @a position.
1541 *
1542 * This operation is linear in the number of elements inserted and
1543 * does not invalidate iterators and references.
1544 */
1545 void
1546 insert(iterator __position, size_type __n, const value_type& __x)
1547 {
1548 list __tmp(__n, __x, get_allocator());
1549 splice(__position, __tmp);
1550 }
1551#endif
1552
1553#if __cplusplus >= 201103L
1554 /**
1555 * @brief Inserts a range into the %list.
1556 * @param __position A const_iterator into the %list.
1557 * @param __first An input iterator.
1558 * @param __last An input iterator.
1559 * @return An iterator pointing to the first element inserted
1560 * (or __position).
1561 *
1562 * This function will insert copies of the data in the range [@a
1563 * first,@a last) into the %list before the location specified by
1564 * @a position.
1565 *
1566 * This operation is linear in the number of elements inserted and
1567 * does not invalidate iterators and references.
1568 */
1569 template<typename _InputIterator,
1570 typename = std::_RequireInputIter<_InputIterator>>
1571 iterator
1572 insert(const_iterator __position, _InputIterator __first,
1573 _InputIterator __last);
1574#else
1575 /**
1576 * @brief Inserts a range into the %list.
1577 * @param __position An iterator into the %list.
1578 * @param __first An input iterator.
1579 * @param __last An input iterator.
1580 *
1581 * This function will insert copies of the data in the range [@a
1582 * first,@a last) into the %list before the location specified by
1583 * @a position.
1584 *
1585 * This operation is linear in the number of elements inserted and
1586 * does not invalidate iterators and references.
1587 */
1588 template<typename _InputIterator>
1589 void
1590 insert(iterator __position, _InputIterator __first,
1591 _InputIterator __last)
1592 {
1593 list __tmp(__first, __last, get_allocator());
1594 splice(__position, __tmp);
1595 }
1596#endif
1597
1598#if __glibcxx_ranges_to_container // C++ >= 23
1599 /**
1600 * @brief Insert a range into a list.
1601 * @param __position An iterator.
1602 * @param __rg An input range of elements that can be converted to
1603 * the list's value type.
1604 * @return An iterator pointing to the first element inserted,
1605 * or `__position` if the range is empty.
1606 *
1607 * Inserts the elements of `__rg` before `__position`.
1608 * No iterators to existing elements are invalidated by this function.
1609 * If the insertion fails due to an exception, no elements will be added
1610 * and so the list will be unchanged.
1611 *
1612 * @since C++23
1613 */
1614 template<__detail::__container_compatible_range<_Tp> _Rg>
1615 iterator
1616 insert_range(const_iterator __position, _Rg&& __rg)
1617 {
1618 list __tmp(from_range, std::forward<_Rg>(__rg), get_allocator());
1619 if (!__tmp.empty())
1620 {
1621 auto __it = __tmp.begin();
1622 splice(__position, __tmp);
1623 return __it;
1624 }
1625 return __position._M_const_cast();
1626 }
1627#endif
1628
1629 /**
1630 * @brief Remove element at given position.
1631 * @param __position Iterator pointing to element to be erased.
1632 * @return An iterator pointing to the next element (or end()).
1633 *
1634 * This function will erase the element at the given position and thus
1635 * shorten the %list by one.
1636 *
1637 * Due to the nature of a %list this operation can be done in
1638 * constant time, and only invalidates iterators/references to
1639 * the element being removed. The user is also cautioned that
1640 * this function only erases the element, and that if the element
1641 * is itself a pointer, the pointed-to memory is not touched in
1642 * any way. Managing the pointer is the user's responsibility.
1643 */
1644 iterator
1645#if __cplusplus >= 201103L
1646 erase(const_iterator __position) noexcept;
1647#else
1648 erase(iterator __position);
1649#endif
1650
1651 /**
1652 * @brief Remove a range of elements.
1653 * @param __first Iterator pointing to the first element to be erased.
1654 * @param __last Iterator pointing to one past the last element to be
1655 * erased.
1656 * @return An iterator pointing to the element pointed to by @a last
1657 * prior to erasing (or end()).
1658 *
1659 * This function will erase the elements in the range @a
1660 * [first,last) and shorten the %list accordingly.
1661 *
1662 * This operation is linear time in the size of the range and only
1663 * invalidates iterators/references to the element being removed.
1664 * The user is also cautioned that this function only erases the
1665 * elements, and that if the elements themselves are pointers, the
1666 * pointed-to memory is not touched in any way. Managing the pointer
1667 * is the user's responsibility.
1668 */
1669 iterator
1670#if __cplusplus >= 201103L
1671 erase(const_iterator __first, const_iterator __last) noexcept
1672#else
1673 erase(iterator __first, iterator __last)
1674#endif
1675 {
1676 while (__first != __last)
1677 __first = erase(__first);
1678 return __last._M_const_cast();
1679 }
1680
1681 /**
1682 * @brief Swaps data with another %list.
1683 * @param __x A %list of the same element and allocator types.
1684 *
1685 * This exchanges the elements between two lists in constant
1686 * time. Note that the global std::swap() function is
1687 * specialized such that std::swap(l1,l2) will feed to this
1688 * function.
1689 *
1690 * Whether the allocators are swapped depends on the allocator traits.
1691 */
1692 void
1693 swap(list& __x) _GLIBCXX_NOEXCEPT
1694 {
1695 __detail::_List_node_base::swap(this->_M_impl._M_node,
1696 __x._M_impl._M_node);
1697
1698 size_t __xsize = __x._M_get_size();
1699 __x._M_set_size(this->_M_get_size());
1700 this->_M_set_size(__xsize);
1701
1702 _Node_alloc_traits::_S_on_swap(this->_M_get_Node_allocator(),
1703 __x._M_get_Node_allocator());
1704 }
1705
1706 /**
1707 * Erases all the elements. Note that this function only erases
1708 * the elements, and that if the elements themselves are
1709 * pointers, the pointed-to memory is not touched in any way.
1710 * Managing the pointer is the user's responsibility.
1711 */
1712 void
1713 clear() _GLIBCXX_NOEXCEPT
1714 {
1715 _Base::_M_clear();
1716 _Base::_M_init();
1717 }
1718
1719 // [23.2.2.4] list operations
1720 /**
1721 * @brief Insert contents of another %list.
1722 * @param __position Iterator referencing the element to insert before.
1723 * @param __x Source list.
1724 *
1725 * The elements of @a __x are inserted in constant time in front of
1726 * the element referenced by @a __position. @a __x becomes an empty
1727 * list.
1728 *
1729 * Requires this != @a __x.
1730 */
1731 void
1732#if __cplusplus >= 201103L
1733 splice(const_iterator __position, list&& __x) noexcept
1734#else
1735 splice(iterator __position, list& __x)
1736#endif
1737 {
1738 if (!__x.empty())
1739 {
1740 _M_check_equal_allocators(__x);
1741
1742 this->_M_transfer(__position._M_const_cast(),
1743 __x.begin(), __x.end());
1744
1745 this->_M_inc_size(__x._M_get_size());
1746 __x._M_set_size(0);
1747 }
1748 }
1749
1750#if __cplusplus >= 201103L
1751 void
1752 splice(const_iterator __position, list& __x) noexcept
1753 { splice(__position, std::move(__x)); }
1754#endif
1755
1756#if __cplusplus >= 201103L
1757 /**
1758 * @brief Insert element from another %list.
1759 * @param __position Const_iterator referencing the element to
1760 * insert before.
1761 * @param __x Source list.
1762 * @param __i Const_iterator referencing the element to move.
1763 *
1764 * Removes the element in list @a __x referenced by @a __i and
1765 * inserts it into the current list before @a __position.
1766 */
1767 void
1768 splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
1769#else
1770 /**
1771 * @brief Insert element from another %list.
1772 * @param __position Iterator referencing the element to insert before.
1773 * @param __x Source list.
1774 * @param __i Iterator referencing the element to move.
1775 *
1776 * Removes the element in list @a __x referenced by @a __i and
1777 * inserts it into the current list before @a __position.
1778 */
1779 void
1780 splice(iterator __position, list& __x, iterator __i)
1781#endif
1782 {
1783 iterator __j = __i._M_const_cast();
1784 ++__j;
1785 if (__position == __i || __position == __j)
1786 return;
1787
1788 if (this != std::__addressof(__x))
1789 _M_check_equal_allocators(__x);
1790
1791 this->_M_transfer(__position._M_const_cast(),
1792 __i._M_const_cast(), __j);
1793
1794 this->_M_inc_size(1);
1795 __x._M_dec_size(1);
1796 }
1797
1798#if __cplusplus >= 201103L
1799 /**
1800 * @brief Insert element from another %list.
1801 * @param __position Const_iterator referencing the element to
1802 * insert before.
1803 * @param __x Source list.
1804 * @param __i Const_iterator referencing the element to move.
1805 *
1806 * Removes the element in list @a __x referenced by @a __i and
1807 * inserts it into the current list before @a __position.
1808 */
1809 void
1810 splice(const_iterator __position, list& __x, const_iterator __i) noexcept
1811 { splice(__position, std::move(__x), __i); }
1812#endif
1813
1814#if __cplusplus >= 201103L
1815 /**
1816 * @brief Insert range from another %list.
1817 * @param __position Const_iterator referencing the element to
1818 * insert before.
1819 * @param __x Source list.
1820 * @param __first Const_iterator referencing the start of range in x.
1821 * @param __last Const_iterator referencing the end of range in x.
1822 *
1823 * Removes elements in the range [__first,__last) and inserts them
1824 * before @a __position in constant time.
1825 *
1826 * Undefined if @a __position is in [__first,__last).
1827 */
1828 void
1829 splice(const_iterator __position, list&& __x, const_iterator __first,
1830 const_iterator __last) noexcept
1831#else
1832 /**
1833 * @brief Insert range from another %list.
1834 * @param __position Iterator referencing the element to insert before.
1835 * @param __x Source list.
1836 * @param __first Iterator referencing the start of range in x.
1837 * @param __last Iterator referencing the end of range in x.
1838 *
1839 * Removes elements in the range [__first,__last) and inserts them
1840 * before @a __position in constant time.
1841 *
1842 * Undefined if @a __position is in [__first,__last).
1843 */
1844 void
1845 splice(iterator __position, list& __x, iterator __first,
1846 iterator __last)
1847#endif
1848 {
1849 if (__first != __last)
1850 {
1851 if (this != std::__addressof(__x))
1852 _M_check_equal_allocators(__x);
1853
1854 size_t __n = _S_distance(__first, __last);
1855 this->_M_inc_size(__n);
1856 __x._M_dec_size(__n);
1857
1858 this->_M_transfer(__position._M_const_cast(),
1859 __first._M_const_cast(),
1860 __last._M_const_cast());
1861 }
1862 }
1863
1864#if __cplusplus >= 201103L
1865 /**
1866 * @brief Insert range from another %list.
1867 * @param __position Const_iterator referencing the element to
1868 * insert before.
1869 * @param __x Source list.
1870 * @param __first Const_iterator referencing the start of range in x.
1871 * @param __last Const_iterator referencing the end of range in x.
1872 *
1873 * Removes elements in the range [__first,__last) and inserts them
1874 * before @a __position in constant time.
1875 *
1876 * Undefined if @a __position is in [__first,__last).
1877 */
1878 void
1879 splice(const_iterator __position, list& __x, const_iterator __first,
1880 const_iterator __last) noexcept
1881 { splice(__position, std::move(__x), __first, __last); }
1882#endif
1883
1884 private:
1885#ifdef __glibcxx_list_remove_return_type // C++ >= 20 && HOSTED
1886 typedef size_type __remove_return_type;
1887# define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG \
1888 __attribute__((__abi_tag__("__cxx20")))
1889#else
1890 typedef void __remove_return_type;
1891# define _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1892#endif
1893 public:
1894
1895 /**
1896 * @brief Remove all elements equal to value.
1897 * @param __value The value to remove.
1898 *
1899 * Removes every element in the list equal to @a value.
1900 * Remaining elements stay in list order. Note that this
1901 * function only erases the elements, and that if the elements
1902 * themselves are pointers, the pointed-to memory is not
1903 * touched in any way. Managing the pointer is the user's
1904 * responsibility.
1905 */
1906 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1907 __remove_return_type
1908 remove(const _Tp& __value);
1909
1910 /**
1911 * @brief Remove all elements satisfying a predicate.
1912 * @tparam _Predicate Unary predicate function or object.
1913 *
1914 * Removes every element in the list for which the predicate
1915 * returns true. Remaining elements stay in list order. Note
1916 * that this function only erases the elements, and that if the
1917 * elements themselves are pointers, the pointed-to memory is
1918 * not touched in any way. Managing the pointer is the user's
1919 * responsibility.
1920 */
1921 template<typename _Predicate>
1922 __remove_return_type
1923 remove_if(_Predicate);
1924
1925 /**
1926 * @brief Remove consecutive duplicate elements.
1927 *
1928 * For each consecutive set of elements with the same value,
1929 * remove all but the first one. Remaining elements stay in
1930 * list order. Note that this function only erases the
1931 * elements, and that if the elements themselves are pointers,
1932 * the pointed-to memory is not touched in any way. Managing
1933 * the pointer is the user's responsibility.
1934 */
1935 _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1936 __remove_return_type
1937 unique();
1938
1939 /**
1940 * @brief Remove consecutive elements satisfying a predicate.
1941 * @tparam _BinaryPredicate Binary predicate function or object.
1942 *
1943 * For each consecutive set of elements [first,last) that
1944 * satisfy predicate(first,i) where i is an iterator in
1945 * [first,last), remove all but the first one. Remaining
1946 * elements stay in list order. Note that this function only
1947 * erases the elements, and that if the elements themselves are
1948 * pointers, the pointed-to memory is not touched in any way.
1949 * Managing the pointer is the user's responsibility.
1950 */
1951 template<typename _BinaryPredicate>
1952 __remove_return_type
1953 unique(_BinaryPredicate);
1954
1955#undef _GLIBCXX_LIST_REMOVE_RETURN_TYPE_TAG
1956
1957 /**
1958 * @brief Merge sorted lists.
1959 * @param __x Sorted list to merge.
1960 *
1961 * Assumes that both @a __x and this list are sorted according to
1962 * operator<(). Merges elements of @a __x into this list in
1963 * sorted order, leaving @a __x empty when complete. Elements in
1964 * this list precede elements in @a __x that are equal.
1965 */
1966#if __cplusplus >= 201103L
1967 void
1968 merge(list&& __x);
1969
1970 void
1971 merge(list& __x)
1972 { merge(std::move(__x)); }
1973#else
1974 void
1975 merge(list& __x);
1976#endif
1977
1978 /**
1979 * @brief Merge sorted lists according to comparison function.
1980 * @tparam _StrictWeakOrdering Comparison function defining
1981 * sort order.
1982 * @param __x Sorted list to merge.
1983 * @param __comp Comparison functor.
1984 *
1985 * Assumes that both @a __x and this list are sorted according to
1986 * StrictWeakOrdering. Merges elements of @a __x into this list
1987 * in sorted order, leaving @a __x empty when complete. Elements
1988 * in this list precede elements in @a __x that are equivalent
1989 * according to StrictWeakOrdering().
1990 */
1991#if __cplusplus >= 201103L
1992 template<typename _StrictWeakOrdering>
1993 void
1994 merge(list&& __x, _StrictWeakOrdering __comp);
1995
1996 template<typename _StrictWeakOrdering>
1997 void
1998 merge(list& __x, _StrictWeakOrdering __comp)
1999 { merge(std::move(__x), __comp); }
2000#else
2001 template<typename _StrictWeakOrdering>
2002 void
2003 merge(list& __x, _StrictWeakOrdering __comp);
2004#endif
2005
2006 /**
2007 * @brief Reverse the elements in list.
2008 *
2009 * Reverse the order of elements in the list in linear time.
2010 */
2011 void
2012 reverse() _GLIBCXX_NOEXCEPT
2013 { this->_M_impl._M_node._M_reverse(); }
2014
2015 /**
2016 * @brief Sort the elements.
2017 *
2018 * Sorts the elements of this list in NlogN time. Equivalent
2019 * elements remain in list order.
2020 */
2021 void
2022 sort();
2023
2024 /**
2025 * @brief Sort the elements according to comparison function.
2026 *
2027 * Sorts the elements of this list in NlogN time. Equivalent
2028 * elements remain in list order.
2029 */
2030 template<typename _StrictWeakOrdering>
2031 void
2032 sort(_StrictWeakOrdering);
2033
2034 protected:
2035 // Internal constructor functions follow.
2036
2037 // Called by the range constructor to implement [23.1.1]/9
2038
2039 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2040 // 438. Ambiguity in the "do the right thing" clause
2041 template<typename _Integer>
2042 void
2043 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
2044 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
2045
2046 // Called by the range constructor to implement [23.1.1]/9
2047 template<typename _InputIterator>
2048 void
2049 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
2050 __false_type)
2051 {
2052 for (; __first != __last; ++__first)
2053#if __cplusplus >= 201103L
2054 emplace_back(*__first);
2055#else
2056 push_back(*__first);
2057#endif
2058 }
2059
2060 // Called by list(n,v,a), and the range constructor when it turns out
2061 // to be the same thing.
2062 void
2063 _M_fill_initialize(size_type __n, const value_type& __x)
2064 {
2065 for (; __n; --__n)
2066 push_back(__x);
2067 }
2068
2069#if __cplusplus >= 201103L
2070 // Called by list(n).
2071 void
2072 _M_default_initialize(size_type __n)
2073 {
2074 for (; __n; --__n)
2075 emplace_back();
2076 }
2077
2078 // Called by resize(sz).
2079 void
2080 _M_default_append(size_type __n);
2081#endif
2082
2083 // Internal assign functions follow.
2084
2085 // Called by the range assign to implement [23.1.1]/9
2086
2087 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2088 // 438. Ambiguity in the "do the right thing" clause
2089 template<typename _Integer>
2090 void
2091 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
2092 { _M_fill_assign(__n, __val); }
2093
2094 // Called by the range assign to implement [23.1.1]/9
2095 template<typename _InputIterator>
2096 void
2097 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
2098 __false_type);
2099
2100 // Called by assign(n,t), and the range assign when it turns out
2101 // to be the same thing.
2102 void
2103 _M_fill_assign(size_type __n, const value_type& __val);
2104
2105
2106 // Moves the elements from [first,last) before position.
2107 void
2108 _M_transfer(iterator __position, iterator __first, iterator __last)
2109 { __position._M_node->_M_transfer(__first._M_node, __last._M_node); }
2110
2111 // Inserts new element at position given and with value given.
2112#if __cplusplus < 201103L
2113 void
2114 _M_insert(iterator __position, const value_type& __x)
2115 {
2116 _Node* __tmp = _M_create_node(__x);
2117 __tmp->_M_hook(__position._M_node);
2118 this->_M_inc_size(1);
2119 }
2120#else
2121 template<typename... _Args>
2122 void
2123 _M_insert(iterator __position, _Args&&... __args)
2124 {
2125 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
2126 __tmp->_M_hook(__position._M_node);
2127 this->_M_inc_size(1);
2128 }
2129#endif
2130
2131 // Erases element at position given.
2132 void
2133 _M_erase(iterator __position) _GLIBCXX_NOEXCEPT
2134 {
2135 this->_M_dec_size(1);
2136 __position._M_node->_M_unhook();
2137 _Node* __n = static_cast<_Node*>(__position._M_node);
2138#if __cplusplus >= 201103L
2139 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __n->_M_valptr());
2140#else
2141 _Tp_alloc_type(_M_get_Node_allocator()).destroy(__n->_M_valptr());
2142#endif
2143
2144 _M_put_node(__n);
2145 }
2146
2147 // To implement the splice (and merge) bits of N1599.
2148 void
2149 _M_check_equal_allocators(const list& __x) _GLIBCXX_NOEXCEPT
2150 {
2151 if (_M_get_Node_allocator() != __x._M_get_Node_allocator())
2152 __builtin_abort();
2153 }
2154
2155 // Used to implement resize.
2156 const_iterator
2157 _M_resize_pos(size_type& __new_size) const;
2158
2159#if __cplusplus >= 201103L
2160 void
2161 _M_move_assign(list&& __x, true_type) noexcept
2162 {
2163 this->clear();
2164 this->_M_move_nodes(std::move(__x));
2165 std::__alloc_on_move(this->_M_get_Node_allocator(),
2166 __x._M_get_Node_allocator());
2167 }
2168
2169 void
2170 _M_move_assign(list&& __x, false_type)
2171 {
2172 if (__x._M_get_Node_allocator() == this->_M_get_Node_allocator())
2173 _M_move_assign(std::move(__x), true_type{});
2174 else
2175 // The rvalue's allocator cannot be moved, or is not equal,
2176 // so we need to individually move each element.
2177 _M_assign_dispatch(std::make_move_iterator(__x.begin()),
2178 std::make_move_iterator(__x.end()),
2179 __false_type{});
2180 }
2181#endif
2182
2183#if _GLIBCXX_USE_CXX11_ABI
2184 // Update _M_size members after merging (some of) __src into __dest.
2185 struct _Finalize_merge
2186 {
2187 explicit
2188 _Finalize_merge(list& __dest, list& __src, const iterator& __src_next)
2189 : _M_dest(__dest), _M_src(__src), _M_next(__src_next)
2190 { }
2191
2192 ~_Finalize_merge()
2193 {
2194 // For the common case, _M_next == _M_sec.end() and the std::distance
2195 // call is fast. But if any *iter1 < *iter2 comparison throws then we
2196 // have to count how many elements remain in _M_src.
2197 const size_t __num_unmerged = std::distance(_M_next, _M_src.end());
2198 const size_t __orig_size = _M_src._M_get_size();
2199 _M_dest._M_inc_size(__orig_size - __num_unmerged);
2200 _M_src._M_set_size(__num_unmerged);
2201 }
2202
2203 list& _M_dest;
2204 list& _M_src;
2205 const iterator& _M_next;
2206
2207#if __cplusplus >= 201103L
2208 _Finalize_merge(const _Finalize_merge&) = delete;
2209#endif
2210 };
2211#else
2212 struct _Finalize_merge
2213 { explicit _Finalize_merge(list&, list&, const iterator&) { } };
2214#endif
2215
2216 };
2217
2218#if __cpp_deduction_guides >= 201606
2219 template<typename _InputIterator, typename _ValT
2220 = typename iterator_traits<_InputIterator>::value_type,
2221 typename _Allocator = allocator<_ValT>,
2222 typename = _RequireInputIter<_InputIterator>,
2223 typename = _RequireAllocator<_Allocator>>
2224 list(_InputIterator, _InputIterator, _Allocator = _Allocator())
2225 -> list<_ValT, _Allocator>;
2226
2227#if __glibcxx_ranges_to_container // C++ >= 23
2228 template<ranges::input_range _Rg,
2229 typename _Allocator = allocator<ranges::range_value_t<_Rg>>>
2230 list(from_range_t, _Rg&&, _Allocator = _Allocator())
2231 -> list<ranges::range_value_t<_Rg>, _Allocator>;
2232#endif
2233#endif
2234
2235_GLIBCXX_END_NAMESPACE_CXX11
2236
2237 /**
2238 * @brief List equality comparison.
2239 * @param __x A %list.
2240 * @param __y A %list of the same type as @a __x.
2241 * @return True iff the size and elements of the lists are equal.
2242 *
2243 * This is an equivalence relation. It is linear in the size of
2244 * the lists. Lists are considered equivalent if their sizes are
2245 * equal, and if corresponding elements compare equal.
2246 */
2247 template<typename _Tp, typename _Alloc>
2248 _GLIBCXX_NODISCARD
2249 inline bool
2250 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2251 {
2252#if _GLIBCXX_USE_CXX11_ABI
2253 if (__x.size() != __y.size())
2254 return false;
2255#endif
2256
2257 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
2258 const_iterator __end1 = __x.end();
2259 const_iterator __end2 = __y.end();
2260
2261 const_iterator __i1 = __x.begin();
2262 const_iterator __i2 = __y.begin();
2263 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
2264 {
2265 ++__i1;
2266 ++__i2;
2267 }
2268 return __i1 == __end1 && __i2 == __end2;
2269 }
2270
2271#if __cpp_lib_three_way_comparison
2272/**
2273 * @brief List ordering relation.
2274 * @param __x A `list`.
2275 * @param __y A `list` of the same type as `__x`.
2276 * @return A value indicating whether `__x` is less than, equal to,
2277 * greater than, or incomparable with `__y`.
2278 *
2279 * See `std::lexicographical_compare_three_way()` for how the determination
2280 * is made. This operator is used to synthesize relational operators like
2281 * `<` and `>=` etc.
2282 */
2283 template<typename _Tp, typename _Alloc>
2284 [[nodiscard]]
2285 inline __detail::__synth3way_t<_Tp>
2286 operator<=>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2287 {
2288 return std::lexicographical_compare_three_way(__x.begin(), __x.end(),
2289 __y.begin(), __y.end(),
2290 __detail::__synth3way);
2291 }
2292#else
2293 /**
2294 * @brief List ordering relation.
2295 * @param __x A %list.
2296 * @param __y A %list of the same type as @a __x.
2297 * @return True iff @a __x is lexicographically less than @a __y.
2298 *
2299 * This is a total ordering relation. It is linear in the size of the
2300 * lists. The elements must be comparable with @c <.
2301 *
2302 * See std::lexicographical_compare() for how the determination is made.
2303 */
2304 template<typename _Tp, typename _Alloc>
2305 _GLIBCXX_NODISCARD
2306 inline bool
2307 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2308 { return std::lexicographical_compare(__x.begin(), __x.end(),
2309 __y.begin(), __y.end()); }
2310
2311 /// Based on operator==
2312 template<typename _Tp, typename _Alloc>
2313 _GLIBCXX_NODISCARD
2314 inline bool
2315 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2316 { return !(__x == __y); }
2317
2318 /// Based on operator<
2319 template<typename _Tp, typename _Alloc>
2320 _GLIBCXX_NODISCARD
2321 inline bool
2322 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2323 { return __y < __x; }
2324
2325 /// Based on operator<
2326 template<typename _Tp, typename _Alloc>
2327 _GLIBCXX_NODISCARD
2328 inline bool
2329 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2330 { return !(__y < __x); }
2331
2332 /// Based on operator<
2333 template<typename _Tp, typename _Alloc>
2334 _GLIBCXX_NODISCARD
2335 inline bool
2336 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
2337 { return !(__x < __y); }
2338#endif // three-way comparison
2339
2340 /// See std::list::swap().
2341 template<typename _Tp, typename _Alloc>
2342 inline void
2344 _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y)))
2345 { __x.swap(__y); }
2346
2347_GLIBCXX_END_NAMESPACE_CONTAINER
2348
2349#if _GLIBCXX_USE_CXX11_ABI
2350
2351 // Detect when distance is used to compute the size of the whole list.
2352 template<typename _Tp>
2353 inline ptrdiff_t
2354 __distance(_GLIBCXX_STD_C::_List_iterator<_Tp> __first,
2355 _GLIBCXX_STD_C::_List_iterator<_Tp> __last,
2356 input_iterator_tag __tag)
2357 {
2358 typedef _GLIBCXX_STD_C::_List_const_iterator<_Tp> _CIter;
2359 return std::__distance(_CIter(__first), _CIter(__last), __tag);
2360 }
2361
2362 template<typename _Tp>
2363 inline ptrdiff_t
2364 __distance(_GLIBCXX_STD_C::_List_const_iterator<_Tp> __first,
2365 _GLIBCXX_STD_C::_List_const_iterator<_Tp> __last,
2366 input_iterator_tag)
2367 {
2368 typedef __detail::_List_node_header _Sentinel;
2369 _GLIBCXX_STD_C::_List_const_iterator<_Tp> __beyond = __last;
2370 ++__beyond;
2371 const bool __whole = __first == __beyond;
2372 if (__builtin_constant_p (__whole) && __whole)
2373 return static_cast<const _Sentinel*>(__last._M_node)->_M_size;
2374
2375 ptrdiff_t __n = 0;
2376 while (__first != __last)
2377 {
2378 ++__first;
2379 ++__n;
2380 }
2381 return __n;
2382 }
2383#endif
2384
2385_GLIBCXX_END_NAMESPACE_VERSION
2386} // namespace std
2387
2388#endif /* _STL_LIST_H */
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:859
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:873
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:826
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:866
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:116
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:119
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:127
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:51
constexpr auto lexicographical_compare_three_way(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2, _InputIter2 __last2, _Comp __comp) -> decltype(__comp(*__first1, *__first2))
Performs dictionary comparison on ranges.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
initializer_list
is_nothrow_default_constructible
Definition type_traits:1237
The ranges::subrange class template.
A list::iterator.
Definition stl_list.h:258
A list::const_iterator.
Definition stl_list.h:343
Bidirectional iterators support a superset of forward iterator operations.
Common iterator class.
Common part of a node in the list.
Definition stl_list.h:86
The list node header.
Definition stl_list.h:109
An actual node in the list.
Definition stl_list.h:239
See bits/stl_deque.h's _Deque_base for an explanation.
Definition stl_list.h:430
A standard container with linear time access to elements, and fixed time insertion/deletion at any po...
Definition stl_list.h:637
void resize(size_type __new_size)
Resizes the list to the specified number of elements.
Definition list.tcc:231
iterator insert(const_iterator __position, const value_type &__x)
Inserts given value into list before specified iterator.
Definition list.tcc:103
void splice(const_iterator __position, list &&__x, const_iterator __i) noexcept
Insert element from another list.
Definition stl_list.h:1768
list(list &&)=default
List move constructor.
void sort()
Sort the elements.
Definition list.tcc:482
void push_back(const value_type &__x)
Add data to the end of the list.
Definition stl_list.h:1395
iterator begin() noexcept
Definition stl_list.h:1068
iterator emplace(const_iterator __position, _Args &&... __args)
Constructs object in list before specified iterator.
Definition list.tcc:90
list & operator=(list &&__x) noexcept(_Node_alloc_traits::_S_nothrow_move())
List move assignment operator.
Definition stl_list.h:949
_Node * _M_create_node(_Args &&... __args)
Definition stl_list.h:712
iterator insert(const_iterator __position, value_type &&__x)
Inserts given rvalue into list before specified iterator.
Definition stl_list.h:1492
allocator_type get_allocator() const noexcept
Get a copy of the memory allocation object.
Definition stl_list.h:1058
list & operator=(const list &__x)
List assignment operator.
Definition list.tcc:268
void assign(initializer_list< value_type > __l)
Assigns an initializer_list to a list.
Definition stl_list.h:1052
const_iterator end() const noexcept
Definition stl_list.h:1098
const_reverse_iterator rbegin() const noexcept
Definition stl_list.h:1118
list(size_type __n, const allocator_type &__a=allocator_type())
Creates a list with default constructed elements.
Definition stl_list.h:776
reverse_iterator rend() noexcept
Definition stl_list.h:1128
void pop_back() noexcept
Removes last element.
Definition stl_list.h:1430
void push_front(const value_type &__x)
Add data to the front of the list.
Definition stl_list.h:1301
__remove_return_type unique()
Remove consecutive duplicate elements.
Definition list.tcc:368
size_type size() const noexcept
Definition stl_list.h:1195
void merge(list &&__x)
Merge sorted lists.
Definition list.tcc:404
const_reference front() const noexcept
Definition stl_list.h:1260
void splice(const_iterator __position, list &__x, const_iterator __first, const_iterator __last) noexcept
Insert range from another list.
Definition stl_list.h:1879
~list()=default
void assign(_InputIterator __first, _InputIterator __last)
Assigns a range to a list.
Definition stl_list.h:1030
const_iterator cend() const noexcept
Definition stl_list.h:1159
list(const allocator_type &__a) noexcept
Creates a list with no elements.
Definition stl_list.h:763
void reverse() noexcept
Reverse the elements in list.
Definition stl_list.h:2012
__remove_return_type remove(const _Tp &__value)
Remove all elements equal to value.
Definition list.tcc:332
list & operator=(initializer_list< value_type > __l)
List initializer list assignment operator.
Definition stl_list.h:967
reverse_iterator rbegin() noexcept
Definition stl_list.h:1108
list()=default
Creates a list with no elements.
iterator erase(const_iterator __first, const_iterator __last) noexcept
Remove a range of elements.
Definition stl_list.h:1671
reference back() noexcept
Definition stl_list.h:1269
void assign(size_type __n, const value_type &__val)
Assigns a given value to a list.
Definition stl_list.h:1011
void splice(const_iterator __position, list &&__x, const_iterator __first, const_iterator __last) noexcept
Insert range from another list.
Definition stl_list.h:1829
void splice(const_iterator __position, list &__x, const_iterator __i) noexcept
Insert element from another list.
Definition stl_list.h:1810
const_iterator cbegin() const noexcept
Definition stl_list.h:1149
const_reverse_iterator crbegin() const noexcept
Definition stl_list.h:1169
__remove_return_type remove_if(_Predicate)
Remove all elements satisfying a predicate.
Definition list.tcc:543
iterator end() noexcept
Definition stl_list.h:1088
list(initializer_list< value_type > __l, const allocator_type &__a=allocator_type())
Builds a list from an initializer_list.
Definition stl_list.h:838
size_type max_size() const noexcept
Definition stl_list.h:1201
const_reference back() const noexcept
Definition stl_list.h:1282
list(size_type __n, const value_type &__value, const allocator_type &__a=allocator_type())
Creates a list with copies of an exemplar element.
Definition stl_list.h:788
const_iterator begin() const noexcept
Definition stl_list.h:1078
reference front() noexcept
Definition stl_list.h:1251
void pop_front() noexcept
Removes first element.
Definition stl_list.h:1381
list(_InputIterator __first, _InputIterator __last, const allocator_type &__a=allocator_type())
Builds a list from a range.
Definition stl_list.h:883
void splice(const_iterator __position, list &&__x) noexcept
Insert contents of another list.
Definition stl_list.h:1733
void clear() noexcept
Definition stl_list.h:1713
list(const list &__x)
List copy constructor.
Definition stl_list.h:815
iterator erase(const_iterator __position) noexcept
Remove element at given position.
Definition list.tcc:152
const_reverse_iterator rend() const noexcept
Definition stl_list.h:1138
bool empty() const noexcept
Definition stl_list.h:1189
iterator insert(const_iterator __p, initializer_list< value_type > __l)
Inserts the contents of an initializer_list into list before specified const_iterator.
Definition stl_list.h:1511
const_reverse_iterator crend() const noexcept
Definition stl_list.h:1179
void swap(list &__x) noexcept
Swaps data with another list.
Definition stl_list.h:1693
Uniform interface to C++98 and C++11 allocators.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
static constexpr size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.