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