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