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