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