1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_LIST 11#define _LIBCPP_LIST 12 13/* 14 list synopsis 15 16namespace std 17{ 18 19template <class T, class Alloc = allocator<T> > 20class list 21{ 22public: 23 24 // types: 25 typedef T value_type; 26 typedef Alloc allocator_type; 27 typedef typename allocator_type::reference reference; 28 typedef typename allocator_type::const_reference const_reference; 29 typedef typename allocator_type::pointer pointer; 30 typedef typename allocator_type::const_pointer const_pointer; 31 typedef implementation-defined iterator; 32 typedef implementation-defined const_iterator; 33 typedef implementation-defined size_type; 34 typedef implementation-defined difference_type; 35 typedef reverse_iterator<iterator> reverse_iterator; 36 typedef reverse_iterator<const_iterator> const_reverse_iterator; 37 38 list() 39 noexcept(is_nothrow_default_constructible<allocator_type>::value); 40 explicit list(const allocator_type& a); 41 explicit list(size_type n); 42 explicit list(size_type n, const allocator_type& a); // C++14 43 list(size_type n, const value_type& value); 44 list(size_type n, const value_type& value, const allocator_type& a); 45 template <class Iter> 46 list(Iter first, Iter last); 47 template <class Iter> 48 list(Iter first, Iter last, const allocator_type& a); 49 template<container-compatible-range<T> R> 50 list(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23 51 list(const list& x); 52 list(const list&, const allocator_type& a); 53 list(list&& x) 54 noexcept(is_nothrow_move_constructible<allocator_type>::value); 55 list(list&&, const allocator_type& a); 56 list(initializer_list<value_type>); 57 list(initializer_list<value_type>, const allocator_type& a); 58 59 ~list(); 60 61 list& operator=(const list& x); 62 list& operator=(list&& x) 63 noexcept( 64 allocator_type::propagate_on_container_move_assignment::value && 65 is_nothrow_move_assignable<allocator_type>::value); 66 list& operator=(initializer_list<value_type>); 67 template <class Iter> 68 void assign(Iter first, Iter last); 69 template<container-compatible-range<T> R> 70 void assign_range(R&& rg); // C++23 71 void assign(size_type n, const value_type& t); 72 void assign(initializer_list<value_type>); 73 74 allocator_type get_allocator() const noexcept; 75 76 iterator begin() noexcept; 77 const_iterator begin() const noexcept; 78 iterator end() noexcept; 79 const_iterator end() const noexcept; 80 reverse_iterator rbegin() noexcept; 81 const_reverse_iterator rbegin() const noexcept; 82 reverse_iterator rend() noexcept; 83 const_reverse_iterator rend() const noexcept; 84 const_iterator cbegin() const noexcept; 85 const_iterator cend() const noexcept; 86 const_reverse_iterator crbegin() const noexcept; 87 const_reverse_iterator crend() const noexcept; 88 89 reference front(); 90 const_reference front() const; 91 reference back(); 92 const_reference back() const; 93 94 bool empty() const noexcept; 95 size_type size() const noexcept; 96 size_type max_size() const noexcept; 97 98 template <class... Args> 99 reference emplace_front(Args&&... args); // reference in C++17 100 void pop_front(); 101 template <class... Args> 102 reference emplace_back(Args&&... args); // reference in C++17 103 void pop_back(); 104 void push_front(const value_type& x); 105 void push_front(value_type&& x); 106 template<container-compatible-range<T> R> 107 void prepend_range(R&& rg); // C++23 108 void push_back(const value_type& x); 109 void push_back(value_type&& x); 110 template<container-compatible-range<T> R> 111 void append_range(R&& rg); // C++23 112 template <class... Args> 113 iterator emplace(const_iterator position, Args&&... args); 114 iterator insert(const_iterator position, const value_type& x); 115 iterator insert(const_iterator position, value_type&& x); 116 iterator insert(const_iterator position, size_type n, const value_type& x); 117 template <class Iter> 118 iterator insert(const_iterator position, Iter first, Iter last); 119 template<container-compatible-range<T> R> 120 iterator insert_range(const_iterator position, R&& rg); // C++23 121 iterator insert(const_iterator position, initializer_list<value_type> il); 122 123 iterator erase(const_iterator position); 124 iterator erase(const_iterator position, const_iterator last); 125 126 void resize(size_type sz); 127 void resize(size_type sz, const value_type& c); 128 129 void swap(list&) 130 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17 131 void clear() noexcept; 132 133 void splice(const_iterator position, list& x); 134 void splice(const_iterator position, list&& x); 135 void splice(const_iterator position, list& x, const_iterator i); 136 void splice(const_iterator position, list&& x, const_iterator i); 137 void splice(const_iterator position, list& x, const_iterator first, 138 const_iterator last); 139 void splice(const_iterator position, list&& x, const_iterator first, 140 const_iterator last); 141 142 size_type remove(const value_type& value); // void before C++20 143 template <class Pred> 144 size_type remove_if(Pred pred); // void before C++20 145 size_type unique(); // void before C++20 146 template <class BinaryPredicate> 147 size_type unique(BinaryPredicate binary_pred); // void before C++20 148 void merge(list& x); 149 void merge(list&& x); 150 template <class Compare> 151 void merge(list& x, Compare comp); 152 template <class Compare> 153 void merge(list&& x, Compare comp); 154 void sort(); 155 template <class Compare> 156 void sort(Compare comp); 157 void reverse() noexcept; 158}; 159 160 161template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>> 162 list(InputIterator, InputIterator, Allocator = Allocator()) 163 -> list<typename iterator_traits<InputIterator>::value_type, Allocator>; // C++17 164 165template<ranges::input_range R, class Allocator = allocator<ranges::range_value_t<R>>> 166 list(from_range_t, R&&, Allocator = Allocator()) 167 -> list<ranges::range_value_t<R>, Allocator>; // C++23 168 169template <class T, class Alloc> 170 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y); 171template <class T, class Alloc> 172 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20 173template <class T, class Alloc> 174 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20 175template <class T, class Alloc> 176 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20 177template <class T, class Alloc> 178 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20 179template <class T, class Alloc> 180 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y); // removed in C++20 181template<class T, class Allocator> 182 synth-three-way-result<T> operator<=>(const list<T, Allocator>& x, 183 const list<T, Allocator>& y); // since C++20 184 185template <class T, class Alloc> 186 void swap(list<T,Alloc>& x, list<T,Alloc>& y) 187 noexcept(noexcept(x.swap(y))); 188 189template <class T, class Allocator, class U> 190 typename list<T, Allocator>::size_type 191 erase(list<T, Allocator>& c, const U& value); // since C++20 192template <class T, class Allocator, class Predicate> 193 typename list<T, Allocator>::size_type 194 erase_if(list<T, Allocator>& c, Predicate pred); // since C++20 195 196} // std 197 198*/ 199 200#include <__algorithm/comp.h> 201#include <__algorithm/equal.h> 202#include <__algorithm/lexicographical_compare.h> 203#include <__algorithm/lexicographical_compare_three_way.h> 204#include <__algorithm/min.h> 205#include <__assert> 206#include <__availability> 207#include <__config> 208#include <__format/enable_insertable.h> 209#include <__iterator/distance.h> 210#include <__iterator/iterator_traits.h> 211#include <__iterator/move_iterator.h> 212#include <__iterator/next.h> 213#include <__iterator/prev.h> 214#include <__iterator/reverse_iterator.h> 215#include <__memory/addressof.h> 216#include <__memory/allocation_guard.h> 217#include <__memory/allocator.h> 218#include <__memory/allocator_traits.h> 219#include <__memory/compressed_pair.h> 220#include <__memory/construct_at.h> 221#include <__memory/pointer_traits.h> 222#include <__memory/swap_allocator.h> 223#include <__memory_resource/polymorphic_allocator.h> 224#include <__ranges/access.h> 225#include <__ranges/concepts.h> 226#include <__ranges/container_compatible_range.h> 227#include <__ranges/from_range.h> 228#include <__type_traits/conditional.h> 229#include <__type_traits/is_allocator.h> 230#include <__type_traits/is_nothrow_default_constructible.h> 231#include <__type_traits/is_nothrow_move_assignable.h> 232#include <__type_traits/is_nothrow_move_constructible.h> 233#include <__type_traits/is_pointer.h> 234#include <__type_traits/is_same.h> 235#include <__type_traits/type_identity.h> 236#include <__utility/forward.h> 237#include <__utility/move.h> 238#include <__utility/swap.h> 239#include <cstring> 240#include <limits> 241#include <new> // __launder 242#include <version> 243 244// standard-mandated includes 245 246// [iterator.range] 247#include <__iterator/access.h> 248#include <__iterator/data.h> 249#include <__iterator/empty.h> 250#include <__iterator/reverse_access.h> 251#include <__iterator/size.h> 252 253// [list.syn] 254#include <compare> 255#include <initializer_list> 256 257#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 258# pragma GCC system_header 259#endif 260 261_LIBCPP_PUSH_MACROS 262#include <__undef_macros> 263 264_LIBCPP_BEGIN_NAMESPACE_STD 265 266template <class _Tp, class _VoidPtr> 267struct __list_node; 268template <class _Tp, class _VoidPtr> 269struct __list_node_base; 270 271template <class _Tp, class _VoidPtr> 272struct __list_node_pointer_traits { 273 typedef __rebind_pointer_t<_VoidPtr, __list_node<_Tp, _VoidPtr> > __node_pointer; 274 typedef __rebind_pointer_t<_VoidPtr, __list_node_base<_Tp, _VoidPtr> > __base_pointer; 275 276#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB) 277 typedef __base_pointer __link_pointer; 278#else 279 typedef __conditional_t<is_pointer<_VoidPtr>::value, __base_pointer, __node_pointer> __link_pointer; 280#endif 281 282 typedef __conditional_t<is_same<__link_pointer, __node_pointer>::value, __base_pointer, __node_pointer> 283 __non_link_pointer; 284 285 static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) { return __p; } 286 287 static _LIBCPP_HIDE_FROM_ABI __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) { 288 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p)); 289 } 290}; 291 292template <class _Tp, class _VoidPtr> 293struct __list_node_base { 294 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits; 295 typedef typename _NodeTraits::__node_pointer __node_pointer; 296 typedef typename _NodeTraits::__base_pointer __base_pointer; 297 typedef typename _NodeTraits::__link_pointer __link_pointer; 298 299 __link_pointer __prev_; 300 __link_pointer __next_; 301 302 _LIBCPP_HIDE_FROM_ABI __list_node_base() 303 : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())), 304 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {} 305 306 _LIBCPP_HIDE_FROM_ABI explicit __list_node_base(__link_pointer __prev, __link_pointer __next) 307 : __prev_(__prev), __next_(__next) {} 308 309 _LIBCPP_HIDE_FROM_ABI __base_pointer __self() { return pointer_traits<__base_pointer>::pointer_to(*this); } 310 311 _LIBCPP_HIDE_FROM_ABI __node_pointer __as_node() { return static_cast<__node_pointer>(__self()); } 312}; 313 314template <class _Tp, class _VoidPtr> 315struct __list_node : public __list_node_base<_Tp, _VoidPtr> { 316 // We allow starting the lifetime of nodes without initializing the value held by the node, 317 // since that is handled by the list itself in order to be allocator-aware. 318#ifndef _LIBCPP_CXX03_LANG 319 320private: 321 union { 322 _Tp __value_; 323 }; 324 325public: 326 _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; } 327#else 328 329private: 330 _ALIGNAS_TYPE(_Tp) char __buffer_[sizeof(_Tp)]; 331 332public: 333 _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_)); } 334#endif 335 336 typedef __list_node_base<_Tp, _VoidPtr> __base; 337 typedef typename __base::__link_pointer __link_pointer; 338 339 _LIBCPP_HIDE_FROM_ABI explicit __list_node(__link_pointer __prev, __link_pointer __next) : __base(__prev, __next) {} 340 _LIBCPP_HIDE_FROM_ABI ~__list_node() {} 341 342 _LIBCPP_HIDE_FROM_ABI __link_pointer __as_link() { return static_cast<__link_pointer>(__base::__self()); } 343}; 344 345template <class _Tp, class _Alloc = allocator<_Tp> > 346class _LIBCPP_TEMPLATE_VIS list; 347template <class _Tp, class _Alloc> 348class __list_imp; 349template <class _Tp, class _VoidPtr> 350class _LIBCPP_TEMPLATE_VIS __list_const_iterator; 351 352template <class _Tp, class _VoidPtr> 353class _LIBCPP_TEMPLATE_VIS __list_iterator { 354 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits; 355 typedef typename _NodeTraits::__link_pointer __link_pointer; 356 357 __link_pointer __ptr_; 358 359 _LIBCPP_HIDE_FROM_ABI explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {} 360 361 template <class, class> 362 friend class list; 363 template <class, class> 364 friend class __list_imp; 365 template <class, class> 366 friend class __list_const_iterator; 367 368public: 369 typedef bidirectional_iterator_tag iterator_category; 370 typedef _Tp value_type; 371 typedef value_type& reference; 372 typedef __rebind_pointer_t<_VoidPtr, value_type> pointer; 373 typedef typename pointer_traits<pointer>::difference_type difference_type; 374 375 _LIBCPP_HIDE_FROM_ABI __list_iterator() _NOEXCEPT : __ptr_(nullptr) {} 376 377 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); } 378 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { 379 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value()); 380 } 381 382 _LIBCPP_HIDE_FROM_ABI __list_iterator& operator++() { 383 __ptr_ = __ptr_->__next_; 384 return *this; 385 } 386 _LIBCPP_HIDE_FROM_ABI __list_iterator operator++(int) { 387 __list_iterator __t(*this); 388 ++(*this); 389 return __t; 390 } 391 392 _LIBCPP_HIDE_FROM_ABI __list_iterator& operator--() { 393 __ptr_ = __ptr_->__prev_; 394 return *this; 395 } 396 _LIBCPP_HIDE_FROM_ABI __list_iterator operator--(int) { 397 __list_iterator __t(*this); 398 --(*this); 399 return __t; 400 } 401 402 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_iterator& __x, const __list_iterator& __y) { 403 return __x.__ptr_ == __y.__ptr_; 404 } 405 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_iterator& __x, const __list_iterator& __y) { 406 return !(__x == __y); 407 } 408}; 409 410template <class _Tp, class _VoidPtr> 411class _LIBCPP_TEMPLATE_VIS __list_const_iterator { 412 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits; 413 typedef typename _NodeTraits::__link_pointer __link_pointer; 414 415 __link_pointer __ptr_; 416 417 _LIBCPP_HIDE_FROM_ABI explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {} 418 419 template <class, class> 420 friend class list; 421 template <class, class> 422 friend class __list_imp; 423 424public: 425 typedef bidirectional_iterator_tag iterator_category; 426 typedef _Tp value_type; 427 typedef const value_type& reference; 428 typedef __rebind_pointer_t<_VoidPtr, const value_type> pointer; 429 typedef typename pointer_traits<pointer>::difference_type difference_type; 430 431 _LIBCPP_HIDE_FROM_ABI __list_const_iterator() _NOEXCEPT : __ptr_(nullptr) {} 432 _LIBCPP_HIDE_FROM_ABI __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT 433 : __ptr_(__p.__ptr_) {} 434 435 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __ptr_->__as_node()->__get_value(); } 436 _LIBCPP_HIDE_FROM_ABI pointer operator->() const { 437 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__get_value()); 438 } 439 440 _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator++() { 441 __ptr_ = __ptr_->__next_; 442 return *this; 443 } 444 _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator++(int) { 445 __list_const_iterator __t(*this); 446 ++(*this); 447 return __t; 448 } 449 450 _LIBCPP_HIDE_FROM_ABI __list_const_iterator& operator--() { 451 __ptr_ = __ptr_->__prev_; 452 return *this; 453 } 454 _LIBCPP_HIDE_FROM_ABI __list_const_iterator operator--(int) { 455 __list_const_iterator __t(*this); 456 --(*this); 457 return __t; 458 } 459 460 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y) { 461 return __x.__ptr_ == __y.__ptr_; 462 } 463 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y) { 464 return !(__x == __y); 465 } 466}; 467 468template <class _Tp, class _Alloc> 469class __list_imp { 470 __list_imp(const __list_imp&); 471 __list_imp& operator=(const __list_imp&); 472 473public: 474 typedef _Alloc allocator_type; 475 typedef allocator_traits<allocator_type> __alloc_traits; 476 typedef typename __alloc_traits::size_type size_type; 477 478protected: 479 typedef _Tp value_type; 480 typedef typename __alloc_traits::void_pointer __void_pointer; 481 typedef __list_iterator<value_type, __void_pointer> iterator; 482 typedef __list_const_iterator<value_type, __void_pointer> const_iterator; 483 typedef __list_node_base<value_type, __void_pointer> __node_base; 484 typedef __list_node<value_type, __void_pointer> __node_type; 485 typedef __rebind_alloc<__alloc_traits, __node_type> __node_allocator; 486 typedef allocator_traits<__node_allocator> __node_alloc_traits; 487 typedef typename __node_alloc_traits::pointer __node_pointer; 488 typedef typename __node_alloc_traits::pointer __node_const_pointer; 489 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits; 490 typedef typename __node_pointer_traits::__link_pointer __link_pointer; 491 typedef __link_pointer __link_const_pointer; 492 typedef typename __alloc_traits::pointer pointer; 493 typedef typename __alloc_traits::const_pointer const_pointer; 494 typedef typename __alloc_traits::difference_type difference_type; 495 496 typedef __rebind_alloc<__alloc_traits, __node_base> __node_base_allocator; 497 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer; 498 static_assert((!is_same<allocator_type, __node_allocator>::value), 499 "internal allocator type must differ from user-specified " 500 "type; otherwise overload resolution breaks"); 501 502 __node_base __end_; 503 __compressed_pair<size_type, __node_allocator> __size_alloc_; 504 505 _LIBCPP_HIDE_FROM_ABI __link_pointer __end_as_link() const _NOEXCEPT { 506 return __node_pointer_traits::__unsafe_link_pointer_cast(const_cast<__node_base&>(__end_).__self()); 507 } 508 509 _LIBCPP_HIDE_FROM_ABI size_type& __sz() _NOEXCEPT { return __size_alloc_.first(); } 510 _LIBCPP_HIDE_FROM_ABI const size_type& __sz() const _NOEXCEPT { return __size_alloc_.first(); } 511 _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __size_alloc_.second(); } 512 _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __size_alloc_.second(); } 513 514 _LIBCPP_HIDE_FROM_ABI size_type __node_alloc_max_size() const _NOEXCEPT { 515 return __node_alloc_traits::max_size(__node_alloc()); 516 } 517 _LIBCPP_HIDE_FROM_ABI static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT; 518 519 _LIBCPP_HIDE_FROM_ABI __list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value); 520 _LIBCPP_HIDE_FROM_ABI __list_imp(const allocator_type& __a); 521 _LIBCPP_HIDE_FROM_ABI __list_imp(const __node_allocator& __a); 522#ifndef _LIBCPP_CXX03_LANG 523 _LIBCPP_HIDE_FROM_ABI __list_imp(__node_allocator&& __a) _NOEXCEPT; 524#endif 525 _LIBCPP_HIDE_FROM_ABI ~__list_imp(); 526 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT; 527 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __sz() == 0; } 528 529 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__end_.__next_); } 530 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__end_.__next_); } 531 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_as_link()); } 532 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_as_link()); } 533 534 _LIBCPP_HIDE_FROM_ABI void swap(__list_imp& __c) 535#if _LIBCPP_STD_VER >= 14 536 _NOEXCEPT; 537#else 538 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value); 539#endif 540 541 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c) { 542 __copy_assign_alloc( 543 __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_copy_assignment::value>()); 544 } 545 546 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c) 547 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_move_assignment::value || 548 is_nothrow_move_assignable<__node_allocator>::value) { 549 __move_assign_alloc( 550 __c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>()); 551 } 552 553 template <class... _Args> 554 _LIBCPP_HIDE_FROM_ABI __node_pointer __create_node(__link_pointer __prev, __link_pointer __next, _Args&&... __args) { 555 __node_allocator& __alloc = __node_alloc(); 556 __allocation_guard<__node_allocator> __guard(__alloc, 1); 557 // Begin the lifetime of the node itself. Note that this doesn't begin the lifetime of the value 558 // held inside the node, since we need to use the allocator's construct() method for that. 559 // 560 // We don't use the allocator's construct() method to construct the node itself since the 561 // Cpp17FooInsertable named requirements don't require the allocator's construct() method 562 // to work on anything other than the value_type. 563 std::__construct_at(std::addressof(*__guard.__get()), __prev, __next); 564 565 // Now construct the value_type using the allocator's construct() method. 566 __node_alloc_traits::construct( 567 __alloc, std::addressof(__guard.__get()->__get_value()), std::forward<_Args>(__args)...); 568 return __guard.__release_ptr(); 569 } 570 571 template <class... _Args> 572 _LIBCPP_HIDE_FROM_ABI void __delete_node(__node_pointer __node) { 573 // For the same reason as above, we use the allocator's destroy() method for the value_type, 574 // but not for the node itself. 575 __node_allocator& __alloc = __node_alloc(); 576 __node_alloc_traits::destroy(__alloc, std::addressof(__node->__get_value())); 577 std::__destroy_at(std::addressof(*__node)); 578 __node_alloc_traits::deallocate(__alloc, __node, 1); 579 } 580 581private: 582 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp& __c, true_type) { 583 if (__node_alloc() != __c.__node_alloc()) 584 clear(); 585 __node_alloc() = __c.__node_alloc(); 586 } 587 588 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __list_imp&, false_type) {} 589 590 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp& __c, true_type) 591 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) { 592 __node_alloc() = std::move(__c.__node_alloc()); 593 } 594 595 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__list_imp&, false_type) _NOEXCEPT {} 596}; 597 598// Unlink nodes [__f, __l] 599template <class _Tp, class _Alloc> 600inline void __list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT { 601 __f->__prev_->__next_ = __l->__next_; 602 __l->__next_->__prev_ = __f->__prev_; 603} 604 605template <class _Tp, class _Alloc> 606inline __list_imp<_Tp, _Alloc>::__list_imp() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) 607 : __size_alloc_(0, __default_init_tag()) {} 608 609template <class _Tp, class _Alloc> 610inline __list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a) : __size_alloc_(0, __node_allocator(__a)) {} 611 612template <class _Tp, class _Alloc> 613inline __list_imp<_Tp, _Alloc>::__list_imp(const __node_allocator& __a) : __size_alloc_(0, __a) {} 614 615#ifndef _LIBCPP_CXX03_LANG 616template <class _Tp, class _Alloc> 617inline __list_imp<_Tp, _Alloc>::__list_imp(__node_allocator&& __a) _NOEXCEPT : __size_alloc_(0, std::move(__a)) {} 618#endif 619 620template <class _Tp, class _Alloc> 621__list_imp<_Tp, _Alloc>::~__list_imp() { 622 clear(); 623} 624 625template <class _Tp, class _Alloc> 626void __list_imp<_Tp, _Alloc>::clear() _NOEXCEPT { 627 if (!empty()) { 628 __link_pointer __f = __end_.__next_; 629 __link_pointer __l = __end_as_link(); 630 __unlink_nodes(__f, __l->__prev_); 631 __sz() = 0; 632 while (__f != __l) { 633 __node_pointer __np = __f->__as_node(); 634 __f = __f->__next_; 635 __delete_node(__np); 636 } 637 } 638} 639 640template <class _Tp, class _Alloc> 641void __list_imp<_Tp, _Alloc>::swap(__list_imp& __c) 642#if _LIBCPP_STD_VER >= 14 643 _NOEXCEPT 644#else 645 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<allocator_type>::value) 646#endif 647{ 648 _LIBCPP_ASSERT_COMPATIBLE_ALLOCATOR( 649 __alloc_traits::propagate_on_container_swap::value || this->__node_alloc() == __c.__node_alloc(), 650 "list::swap: Either propagate_on_container_swap must be true" 651 " or the allocators must compare equal"); 652 using std::swap; 653 std::__swap_allocator(__node_alloc(), __c.__node_alloc()); 654 swap(__sz(), __c.__sz()); 655 swap(__end_, __c.__end_); 656 if (__sz() == 0) 657 __end_.__next_ = __end_.__prev_ = __end_as_link(); 658 else 659 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link(); 660 if (__c.__sz() == 0) 661 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link(); 662 else 663 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link(); 664} 665 666template <class _Tp, class _Alloc /*= allocator<_Tp>*/> 667class _LIBCPP_TEMPLATE_VIS list : private __list_imp<_Tp, _Alloc> { 668 typedef __list_imp<_Tp, _Alloc> base; 669 typedef typename base::__node_type __node_type; 670 typedef typename base::__node_allocator __node_allocator; 671 typedef typename base::__node_pointer __node_pointer; 672 typedef typename base::__node_alloc_traits __node_alloc_traits; 673 typedef typename base::__node_base __node_base; 674 typedef typename base::__node_base_pointer __node_base_pointer; 675 typedef typename base::__link_pointer __link_pointer; 676 677public: 678 typedef _Tp value_type; 679 typedef _Alloc allocator_type; 680 static_assert((is_same<value_type, typename allocator_type::value_type>::value), 681 "Allocator::value_type must be same type as value_type"); 682 typedef value_type& reference; 683 typedef const value_type& const_reference; 684 typedef typename base::pointer pointer; 685 typedef typename base::const_pointer const_pointer; 686 typedef typename base::size_type size_type; 687 typedef typename base::difference_type difference_type; 688 typedef typename base::iterator iterator; 689 typedef typename base::const_iterator const_iterator; 690 typedef std::reverse_iterator<iterator> reverse_iterator; 691 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 692#if _LIBCPP_STD_VER >= 20 693 typedef size_type __remove_return_type; 694#else 695 typedef void __remove_return_type; 696#endif 697 698 static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value, 699 "[allocator.requirements] states that rebinding an allocator to the same type should result in the " 700 "original allocator"); 701 702 _LIBCPP_HIDE_FROM_ABI list() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) {} 703 _LIBCPP_HIDE_FROM_ABI explicit list(const allocator_type& __a) : base(__a) {} 704 _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n); 705#if _LIBCPP_STD_VER >= 14 706 _LIBCPP_HIDE_FROM_ABI explicit list(size_type __n, const allocator_type& __a); 707#endif 708 _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x); 709 template <__enable_if_t<__is_allocator<_Alloc>::value, int> = 0> 710 _LIBCPP_HIDE_FROM_ABI list(size_type __n, const value_type& __x, const allocator_type& __a) : base(__a) { 711 for (; __n > 0; --__n) 712 push_back(__x); 713 } 714 715 template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0> 716 _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l); 717 718 template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0> 719 _LIBCPP_HIDE_FROM_ABI list(_InpIter __f, _InpIter __l, const allocator_type& __a); 720 721#if _LIBCPP_STD_VER >= 23 722 template <_ContainerCompatibleRange<_Tp> _Range> 723 _LIBCPP_HIDE_FROM_ABI list(from_range_t, _Range&& __range, const allocator_type& __a = allocator_type()) : base(__a) { 724 prepend_range(std::forward<_Range>(__range)); 725 } 726#endif 727 728 _LIBCPP_HIDE_FROM_ABI list(const list& __c); 729 _LIBCPP_HIDE_FROM_ABI list(const list& __c, const __type_identity_t<allocator_type>& __a); 730 _LIBCPP_HIDE_FROM_ABI list& operator=(const list& __c); 731#ifndef _LIBCPP_CXX03_LANG 732 _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il); 733 _LIBCPP_HIDE_FROM_ABI list(initializer_list<value_type> __il, const allocator_type& __a); 734 735 _LIBCPP_HIDE_FROM_ABI list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value); 736 _LIBCPP_HIDE_FROM_ABI list(list&& __c, const __type_identity_t<allocator_type>& __a); 737 _LIBCPP_HIDE_FROM_ABI list& operator=(list&& __c) 738 _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&& 739 is_nothrow_move_assignable<__node_allocator>::value); 740 741 _LIBCPP_HIDE_FROM_ABI list& operator=(initializer_list<value_type> __il) { 742 assign(__il.begin(), __il.end()); 743 return *this; 744 } 745 746 _LIBCPP_HIDE_FROM_ABI void assign(initializer_list<value_type> __il) { assign(__il.begin(), __il.end()); } 747#endif // _LIBCPP_CXX03_LANG 748 749 template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0> 750 _LIBCPP_HIDE_FROM_ABI void assign(_InpIter __f, _InpIter __l); 751 752#if _LIBCPP_STD_VER >= 23 753 template <_ContainerCompatibleRange<_Tp> _Range> 754 _LIBCPP_HIDE_FROM_ABI void assign_range(_Range&& __range) { 755 __assign_with_sentinel(ranges::begin(__range), ranges::end(__range)); 756 } 757#endif 758 759 _LIBCPP_HIDE_FROM_ABI void assign(size_type __n, const value_type& __x); 760 761 _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const _NOEXCEPT; 762 763 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return base::__sz(); } 764 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return base::empty(); } 765 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { 766 return std::min<size_type>(base::__node_alloc_max_size(), numeric_limits<difference_type >::max()); 767 } 768 769 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return base::begin(); } 770 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return base::begin(); } 771 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return base::end(); } 772 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return base::end(); } 773 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return base::begin(); } 774 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return base::end(); } 775 776 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); } 777 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); } 778 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); } 779 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); } 780 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(end()); } 781 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(begin()); } 782 783 _LIBCPP_HIDE_FROM_ABI reference front() { 784 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list"); 785 return base::__end_.__next_->__as_node()->__get_value(); 786 } 787 _LIBCPP_HIDE_FROM_ABI const_reference front() const { 788 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::front called on empty list"); 789 return base::__end_.__next_->__as_node()->__get_value(); 790 } 791 _LIBCPP_HIDE_FROM_ABI reference back() { 792 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list"); 793 return base::__end_.__prev_->__as_node()->__get_value(); 794 } 795 _LIBCPP_HIDE_FROM_ABI const_reference back() const { 796 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::back called on empty list"); 797 return base::__end_.__prev_->__as_node()->__get_value(); 798 } 799 800#ifndef _LIBCPP_CXX03_LANG 801 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x); 802 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x); 803 804# if _LIBCPP_STD_VER >= 23 805 template <_ContainerCompatibleRange<_Tp> _Range> 806 _LIBCPP_HIDE_FROM_ABI void prepend_range(_Range&& __range) { 807 insert_range(begin(), std::forward<_Range>(__range)); 808 } 809 810 template <_ContainerCompatibleRange<_Tp> _Range> 811 _LIBCPP_HIDE_FROM_ABI void append_range(_Range&& __range) { 812 insert_range(end(), std::forward<_Range>(__range)); 813 } 814# endif 815 816 template <class... _Args> 817# if _LIBCPP_STD_VER >= 17 818 _LIBCPP_HIDE_FROM_ABI reference emplace_front(_Args&&... __args); 819# else 820 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args); 821# endif 822 template <class... _Args> 823# if _LIBCPP_STD_VER >= 17 824 _LIBCPP_HIDE_FROM_ABI reference emplace_back(_Args&&... __args); 825# else 826 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args); 827# endif 828 template <class... _Args> 829 _LIBCPP_HIDE_FROM_ABI iterator emplace(const_iterator __p, _Args&&... __args); 830 831 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, value_type&& __x); 832 833 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, initializer_list<value_type> __il) { 834 return insert(__p, __il.begin(), __il.end()); 835 } 836#endif // _LIBCPP_CXX03_LANG 837 838 _LIBCPP_HIDE_FROM_ABI void push_front(const value_type& __x); 839 _LIBCPP_HIDE_FROM_ABI void push_back(const value_type& __x); 840 841#ifndef _LIBCPP_CXX03_LANG 842 template <class _Arg> 843 _LIBCPP_HIDE_FROM_ABI void __emplace_back(_Arg&& __arg) { 844 emplace_back(std::forward<_Arg>(__arg)); 845 } 846#else 847 _LIBCPP_HIDE_FROM_ABI void __emplace_back(value_type const& __arg) { push_back(__arg); } 848#endif 849 850 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, const value_type& __x); 851 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, size_type __n, const value_type& __x); 852 853 template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> = 0> 854 _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __p, _InpIter __f, _InpIter __l); 855 856#if _LIBCPP_STD_VER >= 23 857 template <_ContainerCompatibleRange<_Tp> _Range> 858 _LIBCPP_HIDE_FROM_ABI iterator insert_range(const_iterator __position, _Range&& __range) { 859 return __insert_with_sentinel(__position, ranges::begin(__range), ranges::end(__range)); 860 } 861#endif 862 863 _LIBCPP_HIDE_FROM_ABI void swap(list& __c) 864#if _LIBCPP_STD_VER >= 14 865 _NOEXCEPT 866#else 867 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value || 868 __is_nothrow_swappable<__node_allocator>::value) 869#endif 870 { 871 base::swap(__c); 872 } 873 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { base::clear(); } 874 875 _LIBCPP_HIDE_FROM_ABI void pop_front(); 876 _LIBCPP_HIDE_FROM_ABI void pop_back(); 877 878 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p); 879 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l); 880 881 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n); 882 _LIBCPP_HIDE_FROM_ABI void resize(size_type __n, const value_type& __x); 883 884 _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c); 885#ifndef _LIBCPP_CXX03_LANG 886 _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c) { splice(__p, __c); } 887 _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __i) { splice(__p, __c, __i); } 888 _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l) { 889 splice(__p, __c, __f, __l); 890 } 891#endif 892 _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __i); 893 _LIBCPP_HIDE_FROM_ABI void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l); 894 895 _LIBCPP_HIDE_FROM_ABI __remove_return_type remove(const value_type& __x); 896 template <class _Pred> 897 _LIBCPP_HIDE_FROM_ABI __remove_return_type remove_if(_Pred __pred); 898 _LIBCPP_HIDE_FROM_ABI __remove_return_type unique() { return unique(__equal_to()); } 899 template <class _BinaryPred> 900 _LIBCPP_HIDE_FROM_ABI __remove_return_type unique(_BinaryPred __binary_pred); 901 _LIBCPP_HIDE_FROM_ABI void merge(list& __c); 902#ifndef _LIBCPP_CXX03_LANG 903 _LIBCPP_HIDE_FROM_ABI void merge(list&& __c) { merge(__c); } 904 905 template <class _Comp> 906 _LIBCPP_HIDE_FROM_ABI void merge(list&& __c, _Comp __comp) { 907 merge(__c, __comp); 908 } 909#endif 910 template <class _Comp> 911 _LIBCPP_HIDE_FROM_ABI void merge(list& __c, _Comp __comp); 912 913 _LIBCPP_HIDE_FROM_ABI void sort(); 914 template <class _Comp> 915 _LIBCPP_HIDE_FROM_ABI void sort(_Comp __comp); 916 917 _LIBCPP_HIDE_FROM_ABI void reverse() _NOEXCEPT; 918 919 _LIBCPP_HIDE_FROM_ABI bool __invariants() const; 920 921private: 922 template <class _Iterator, class _Sentinel> 923 _LIBCPP_HIDE_FROM_ABI void __assign_with_sentinel(_Iterator __f, _Sentinel __l); 924 925 template <class _Iterator, class _Sentinel> 926 _LIBCPP_HIDE_FROM_ABI iterator __insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l); 927 928 _LIBCPP_HIDE_FROM_ABI static void __link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l); 929 _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_front(__link_pointer __f, __link_pointer __l); 930 _LIBCPP_HIDE_FROM_ABI void __link_nodes_at_back(__link_pointer __f, __link_pointer __l); 931 _LIBCPP_HIDE_FROM_ABI iterator __iterator(size_type __n); 932 // TODO: Make this _LIBCPP_HIDE_FROM_ABI 933 template <class _Comp> 934 _LIBCPP_HIDDEN static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp); 935 936 _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, true_type) 937 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value); 938 _LIBCPP_HIDE_FROM_ABI void __move_assign(list& __c, false_type); 939}; 940 941#if _LIBCPP_STD_VER >= 17 942template <class _InputIterator, 943 class _Alloc = allocator<__iter_value_type<_InputIterator>>, 944 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>, 945 class = enable_if_t<__is_allocator<_Alloc>::value> > 946list(_InputIterator, _InputIterator) -> list<__iter_value_type<_InputIterator>, _Alloc>; 947 948template <class _InputIterator, 949 class _Alloc, 950 class = enable_if_t<__has_input_iterator_category<_InputIterator>::value>, 951 class = enable_if_t<__is_allocator<_Alloc>::value> > 952list(_InputIterator, _InputIterator, _Alloc) -> list<__iter_value_type<_InputIterator>, _Alloc>; 953#endif 954 955#if _LIBCPP_STD_VER >= 23 956template <ranges::input_range _Range, 957 class _Alloc = allocator<ranges::range_value_t<_Range>>, 958 class = enable_if_t<__is_allocator<_Alloc>::value> > 959list(from_range_t, _Range&&, _Alloc = _Alloc()) -> list<ranges::range_value_t<_Range>, _Alloc>; 960#endif 961 962// Link in nodes [__f, __l] just prior to __p 963template <class _Tp, class _Alloc> 964inline void list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l) { 965 __p->__prev_->__next_ = __f; 966 __f->__prev_ = __p->__prev_; 967 __p->__prev_ = __l; 968 __l->__next_ = __p; 969} 970 971// Link in nodes [__f, __l] at the front of the list 972template <class _Tp, class _Alloc> 973inline void list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l) { 974 __f->__prev_ = base::__end_as_link(); 975 __l->__next_ = base::__end_.__next_; 976 __l->__next_->__prev_ = __l; 977 base::__end_.__next_ = __f; 978} 979 980// Link in nodes [__f, __l] at the back of the list 981template <class _Tp, class _Alloc> 982inline void list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l) { 983 __l->__next_ = base::__end_as_link(); 984 __f->__prev_ = base::__end_.__prev_; 985 __f->__prev_->__next_ = __f; 986 base::__end_.__prev_ = __l; 987} 988 989template <class _Tp, class _Alloc> 990inline typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::__iterator(size_type __n) { 991 return __n <= base::__sz() / 2 ? std::next(begin(), __n) : std::prev(end(), base::__sz() - __n); 992} 993 994template <class _Tp, class _Alloc> 995list<_Tp, _Alloc>::list(size_type __n) { 996 for (; __n > 0; --__n) 997#ifndef _LIBCPP_CXX03_LANG 998 emplace_back(); 999#else 1000 push_back(value_type()); 1001#endif 1002} 1003 1004#if _LIBCPP_STD_VER >= 14 1005template <class _Tp, class _Alloc> 1006list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a) { 1007 for (; __n > 0; --__n) 1008 emplace_back(); 1009} 1010#endif 1011 1012template <class _Tp, class _Alloc> 1013list<_Tp, _Alloc>::list(size_type __n, const value_type& __x) { 1014 for (; __n > 0; --__n) 1015 push_back(__x); 1016} 1017 1018template <class _Tp, class _Alloc> 1019template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> > 1020list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l) { 1021 for (; __f != __l; ++__f) 1022 __emplace_back(*__f); 1023} 1024 1025template <class _Tp, class _Alloc> 1026template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> > 1027list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a) : base(__a) { 1028 for (; __f != __l; ++__f) 1029 __emplace_back(*__f); 1030} 1031 1032template <class _Tp, class _Alloc> 1033list<_Tp, _Alloc>::list(const list& __c) 1034 : base(__node_alloc_traits::select_on_container_copy_construction(__c.__node_alloc())) { 1035 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i) 1036 push_back(*__i); 1037} 1038 1039template <class _Tp, class _Alloc> 1040list<_Tp, _Alloc>::list(const list& __c, const __type_identity_t<allocator_type>& __a) : base(__a) { 1041 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i) 1042 push_back(*__i); 1043} 1044 1045#ifndef _LIBCPP_CXX03_LANG 1046 1047template <class _Tp, class _Alloc> 1048list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a) : base(__a) { 1049 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i) 1050 push_back(*__i); 1051} 1052 1053template <class _Tp, class _Alloc> 1054list<_Tp, _Alloc>::list(initializer_list<value_type> __il) { 1055 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), __e = __il.end(); __i != __e; ++__i) 1056 push_back(*__i); 1057} 1058 1059template <class _Tp, class _Alloc> 1060inline list<_Tp, _Alloc>::list(list&& __c) _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value) 1061 : base(std::move(__c.__node_alloc())) { 1062 splice(end(), __c); 1063} 1064 1065template <class _Tp, class _Alloc> 1066inline list<_Tp, _Alloc>::list(list&& __c, const __type_identity_t<allocator_type>& __a) : base(__a) { 1067 if (__a == __c.get_allocator()) 1068 splice(end(), __c); 1069 else { 1070 typedef move_iterator<iterator> _Ip; 1071 assign(_Ip(__c.begin()), _Ip(__c.end())); 1072 } 1073} 1074 1075template <class _Tp, class _Alloc> 1076inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(list&& __c) 1077 _NOEXCEPT_(__node_alloc_traits::propagate_on_container_move_assignment::value&& 1078 is_nothrow_move_assignable<__node_allocator>::value) { 1079 __move_assign(__c, integral_constant<bool, __node_alloc_traits::propagate_on_container_move_assignment::value>()); 1080 return *this; 1081} 1082 1083template <class _Tp, class _Alloc> 1084void list<_Tp, _Alloc>::__move_assign(list& __c, false_type) { 1085 if (base::__node_alloc() != __c.__node_alloc()) { 1086 typedef move_iterator<iterator> _Ip; 1087 assign(_Ip(__c.begin()), _Ip(__c.end())); 1088 } else 1089 __move_assign(__c, true_type()); 1090} 1091 1092template <class _Tp, class _Alloc> 1093void list<_Tp, _Alloc>::__move_assign(list& __c, true_type) 1094 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) { 1095 clear(); 1096 base::__move_assign_alloc(__c); 1097 splice(end(), __c); 1098} 1099 1100#endif // _LIBCPP_CXX03_LANG 1101 1102template <class _Tp, class _Alloc> 1103inline list<_Tp, _Alloc>& list<_Tp, _Alloc>::operator=(const list& __c) { 1104 if (this != std::addressof(__c)) { 1105 base::__copy_assign_alloc(__c); 1106 assign(__c.begin(), __c.end()); 1107 } 1108 return *this; 1109} 1110 1111template <class _Tp, class _Alloc> 1112template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> > 1113void list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l) { 1114 __assign_with_sentinel(__f, __l); 1115} 1116 1117template <class _Tp, class _Alloc> 1118template <class _Iterator, class _Sentinel> 1119_LIBCPP_HIDE_FROM_ABI void list<_Tp, _Alloc>::__assign_with_sentinel(_Iterator __f, _Sentinel __l) { 1120 iterator __i = begin(); 1121 iterator __e = end(); 1122 for (; __f != __l && __i != __e; ++__f, (void)++__i) 1123 *__i = *__f; 1124 if (__i == __e) 1125 __insert_with_sentinel(__e, std::move(__f), std::move(__l)); 1126 else 1127 erase(__i, __e); 1128} 1129 1130template <class _Tp, class _Alloc> 1131void list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x) { 1132 iterator __i = begin(); 1133 iterator __e = end(); 1134 for (; __n > 0 && __i != __e; --__n, (void)++__i) 1135 *__i = __x; 1136 if (__i == __e) 1137 insert(__e, __n, __x); 1138 else 1139 erase(__i, __e); 1140} 1141 1142template <class _Tp, class _Alloc> 1143inline _Alloc list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT { 1144 return allocator_type(base::__node_alloc()); 1145} 1146 1147template <class _Tp, class _Alloc> 1148typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x) { 1149 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x); 1150 __link_nodes(__p.__ptr_, __node->__as_link(), __node->__as_link()); 1151 ++base::__sz(); 1152 return iterator(__node->__as_link()); 1153} 1154 1155template <class _Tp, class _Alloc> 1156typename list<_Tp, _Alloc>::iterator 1157list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x) { 1158 iterator __r(__p.__ptr_); 1159 if (__n > 0) { 1160 size_type __ds = 0; 1161 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x); 1162 ++__ds; 1163 __r = iterator(__node->__as_link()); 1164 iterator __e = __r; 1165#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1166 try { 1167#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1168 for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { 1169 __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link(); 1170 } 1171#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1172 } catch (...) { 1173 while (true) { 1174 __link_pointer __prev = __e.__ptr_->__prev_; 1175 __node_pointer __current = __e.__ptr_->__as_node(); 1176 this->__delete_node(__current); 1177 if (__prev == 0) 1178 break; 1179 __e = iterator(__prev); 1180 } 1181 throw; 1182 } 1183#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1184 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_); 1185 base::__sz() += __ds; 1186 } 1187 return __r; 1188} 1189 1190template <class _Tp, class _Alloc> 1191template <class _InpIter, __enable_if_t<__has_input_iterator_category<_InpIter>::value, int> > 1192typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l) { 1193 return __insert_with_sentinel(__p, __f, __l); 1194} 1195 1196template <class _Tp, class _Alloc> 1197template <class _Iterator, class _Sentinel> 1198_LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Alloc>::iterator 1199list<_Tp, _Alloc>::__insert_with_sentinel(const_iterator __p, _Iterator __f, _Sentinel __l) { 1200 iterator __r(__p.__ptr_); 1201 if (__f != __l) { 1202 size_type __ds = 0; 1203 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, *__f); 1204 ++__ds; 1205 __r = iterator(__node->__as_link()); 1206 iterator __e = __r; 1207#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1208 try { 1209#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1210 for (++__f; __f != __l; ++__f, (void)++__e, ++__ds) { 1211 __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, *__f)->__as_link(); 1212 } 1213#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1214 } catch (...) { 1215 while (true) { 1216 __link_pointer __prev = __e.__ptr_->__prev_; 1217 __node_pointer __current = __e.__ptr_->__as_node(); 1218 this->__delete_node(__current); 1219 if (__prev == 0) 1220 break; 1221 __e = iterator(__prev); 1222 } 1223 throw; 1224 } 1225#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1226 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_); 1227 base::__sz() += __ds; 1228 } 1229 return __r; 1230} 1231 1232template <class _Tp, class _Alloc> 1233void list<_Tp, _Alloc>::push_front(const value_type& __x) { 1234 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x); 1235 __link_pointer __nl = __node->__as_link(); 1236 __link_nodes_at_front(__nl, __nl); 1237 ++base::__sz(); 1238} 1239 1240template <class _Tp, class _Alloc> 1241void list<_Tp, _Alloc>::push_back(const value_type& __x) { 1242 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x); 1243 __link_pointer __nl = __node->__as_link(); 1244 __link_nodes_at_back(__nl, __nl); 1245 ++base::__sz(); 1246} 1247 1248#ifndef _LIBCPP_CXX03_LANG 1249 1250template <class _Tp, class _Alloc> 1251void list<_Tp, _Alloc>::push_front(value_type&& __x) { 1252 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x)); 1253 __link_pointer __nl = __node->__as_link(); 1254 __link_nodes_at_front(__nl, __nl); 1255 ++base::__sz(); 1256} 1257 1258template <class _Tp, class _Alloc> 1259void list<_Tp, _Alloc>::push_back(value_type&& __x) { 1260 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x)); 1261 __link_pointer __nl = __node->__as_link(); 1262 __link_nodes_at_back(__nl, __nl); 1263 ++base::__sz(); 1264} 1265 1266template <class _Tp, class _Alloc> 1267template <class... _Args> 1268# if _LIBCPP_STD_VER >= 17 1269typename list<_Tp, _Alloc>::reference 1270# else 1271void 1272# endif 1273list<_Tp, _Alloc>::emplace_front(_Args&&... __args) { 1274 __node_pointer __node = 1275 this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...); 1276 __link_pointer __nl = __node->__as_link(); 1277 __link_nodes_at_front(__nl, __nl); 1278 ++base::__sz(); 1279# if _LIBCPP_STD_VER >= 17 1280 return __node->__get_value(); 1281# endif 1282} 1283 1284template <class _Tp, class _Alloc> 1285template <class... _Args> 1286# if _LIBCPP_STD_VER >= 17 1287typename list<_Tp, _Alloc>::reference 1288# else 1289void 1290# endif 1291list<_Tp, _Alloc>::emplace_back(_Args&&... __args) { 1292 __node_pointer __node = 1293 this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...); 1294 __link_pointer __nl = __node->__as_link(); 1295 __link_nodes_at_back(__nl, __nl); 1296 ++base::__sz(); 1297# if _LIBCPP_STD_VER >= 17 1298 return __node->__get_value(); 1299# endif 1300} 1301 1302template <class _Tp, class _Alloc> 1303template <class... _Args> 1304typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args) { 1305 __node_pointer __node = 1306 this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::forward<_Args>(__args)...); 1307 __link_pointer __nl = __node->__as_link(); 1308 __link_nodes(__p.__ptr_, __nl, __nl); 1309 ++base::__sz(); 1310 return iterator(__nl); 1311} 1312 1313template <class _Tp, class _Alloc> 1314typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x) { 1315 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, std::move(__x)); 1316 __link_pointer __nl = __node->__as_link(); 1317 __link_nodes(__p.__ptr_, __nl, __nl); 1318 ++base::__sz(); 1319 return iterator(__nl); 1320} 1321 1322#endif // _LIBCPP_CXX03_LANG 1323 1324template <class _Tp, class _Alloc> 1325void list<_Tp, _Alloc>::pop_front() { 1326 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_front() called with empty list"); 1327 __link_pointer __n = base::__end_.__next_; 1328 base::__unlink_nodes(__n, __n); 1329 --base::__sz(); 1330 this->__delete_node(__n->__as_node()); 1331} 1332 1333template <class _Tp, class _Alloc> 1334void list<_Tp, _Alloc>::pop_back() { 1335 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "list::pop_back() called on an empty list"); 1336 __link_pointer __n = base::__end_.__prev_; 1337 base::__unlink_nodes(__n, __n); 1338 --base::__sz(); 1339 this->__delete_node(__n->__as_node()); 1340} 1341 1342template <class _Tp, class _Alloc> 1343typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __p) { 1344 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p != end(), "list::erase(iterator) called with a non-dereferenceable iterator"); 1345 __link_pointer __n = __p.__ptr_; 1346 __link_pointer __r = __n->__next_; 1347 base::__unlink_nodes(__n, __n); 1348 --base::__sz(); 1349 this->__delete_node(__n->__as_node()); 1350 return iterator(__r); 1351} 1352 1353template <class _Tp, class _Alloc> 1354typename list<_Tp, _Alloc>::iterator list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l) { 1355 if (__f != __l) { 1356 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_); 1357 while (__f != __l) { 1358 __link_pointer __n = __f.__ptr_; 1359 ++__f; 1360 --base::__sz(); 1361 this->__delete_node(__n->__as_node()); 1362 } 1363 } 1364 return iterator(__l.__ptr_); 1365} 1366 1367template <class _Tp, class _Alloc> 1368void list<_Tp, _Alloc>::resize(size_type __n) { 1369 if (__n < base::__sz()) 1370 erase(__iterator(__n), end()); 1371 else if (__n > base::__sz()) { 1372 __n -= base::__sz(); 1373 size_type __ds = 0; 1374 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr); 1375 ++__ds; 1376 iterator __r = iterator(__node->__as_link()); 1377 iterator __e = __r; 1378#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1379 try { 1380#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1381 for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { 1382 __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr)->__as_link(); 1383 } 1384#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1385 } catch (...) { 1386 while (true) { 1387 __link_pointer __prev = __e.__ptr_->__prev_; 1388 __node_pointer __current = __e.__ptr_->__as_node(); 1389 this->__delete_node(__current); 1390 if (__prev == 0) 1391 break; 1392 __e = iterator(__prev); 1393 } 1394 throw; 1395 } 1396#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1397 __link_nodes_at_back(__r.__ptr_, __e.__ptr_); 1398 base::__sz() += __ds; 1399 } 1400} 1401 1402template <class _Tp, class _Alloc> 1403void list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x) { 1404 if (__n < base::__sz()) 1405 erase(__iterator(__n), end()); 1406 else if (__n > base::__sz()) { 1407 __n -= base::__sz(); 1408 size_type __ds = 0; 1409 __node_pointer __node = this->__create_node(/* prev = */ nullptr, /* next = */ nullptr, __x); 1410 ++__ds; 1411 __link_pointer __nl = __node->__as_link(); 1412 iterator __r = iterator(__nl); 1413 iterator __e = __r; 1414#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1415 try { 1416#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1417 for (--__n; __n != 0; --__n, (void)++__e, ++__ds) { 1418 __e.__ptr_->__next_ = this->__create_node(/* prev = */ __e.__ptr_, /* next = */ nullptr, __x)->__as_link(); 1419 } 1420#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1421 } catch (...) { 1422 while (true) { 1423 __link_pointer __prev = __e.__ptr_->__prev_; 1424 __node_pointer __current = __e.__ptr_->__as_node(); 1425 this->__delete_node(__current); 1426 if (__prev == 0) 1427 break; 1428 __e = iterator(__prev); 1429 } 1430 throw; 1431 } 1432#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1433 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_); 1434 base::__sz() += __ds; 1435 } 1436} 1437 1438template <class _Tp, class _Alloc> 1439void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) { 1440 _LIBCPP_ASSERT_VALID_INPUT_RANGE( 1441 this != std::addressof(__c), "list::splice(iterator, list) called with this == &list"); 1442 if (!__c.empty()) { 1443 __link_pointer __f = __c.__end_.__next_; 1444 __link_pointer __l = __c.__end_.__prev_; 1445 base::__unlink_nodes(__f, __l); 1446 __link_nodes(__p.__ptr_, __f, __l); 1447 base::__sz() += __c.__sz(); 1448 __c.__sz() = 0; 1449 } 1450} 1451 1452template <class _Tp, class _Alloc> 1453void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) { 1454 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_) { 1455 __link_pointer __f = __i.__ptr_; 1456 base::__unlink_nodes(__f, __f); 1457 __link_nodes(__p.__ptr_, __f, __f); 1458 --__c.__sz(); 1459 ++base::__sz(); 1460 } 1461} 1462 1463template <class _Tp, class _Alloc> 1464void list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l) { 1465 if (__f != __l) { 1466 __link_pointer __first = __f.__ptr_; 1467 --__l; 1468 __link_pointer __last = __l.__ptr_; 1469 if (this != std::addressof(__c)) { 1470 size_type __s = std::distance(__f, __l) + 1; 1471 __c.__sz() -= __s; 1472 base::__sz() += __s; 1473 } 1474 base::__unlink_nodes(__first, __last); 1475 __link_nodes(__p.__ptr_, __first, __last); 1476 } 1477} 1478 1479template <class _Tp, class _Alloc> 1480typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove(const value_type& __x) { 1481 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing 1482 for (const_iterator __i = begin(), __e = end(); __i != __e;) { 1483 if (*__i == __x) { 1484 const_iterator __j = std::next(__i); 1485 for (; __j != __e && *__j == __x; ++__j) 1486 ; 1487 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j); 1488 __i = __j; 1489 if (__i != __e) 1490 ++__i; 1491 } else 1492 ++__i; 1493 } 1494 1495 return (__remove_return_type)__deleted_nodes.size(); 1496} 1497 1498template <class _Tp, class _Alloc> 1499template <class _Pred> 1500typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::remove_if(_Pred __pred) { 1501 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing 1502 for (iterator __i = begin(), __e = end(); __i != __e;) { 1503 if (__pred(*__i)) { 1504 iterator __j = std::next(__i); 1505 for (; __j != __e && __pred(*__j); ++__j) 1506 ; 1507 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j); 1508 __i = __j; 1509 if (__i != __e) 1510 ++__i; 1511 } else 1512 ++__i; 1513 } 1514 1515 return (__remove_return_type)__deleted_nodes.size(); 1516} 1517 1518template <class _Tp, class _Alloc> 1519template <class _BinaryPred> 1520typename list<_Tp, _Alloc>::__remove_return_type list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred) { 1521 list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing 1522 for (iterator __i = begin(), __e = end(); __i != __e;) { 1523 iterator __j = std::next(__i); 1524 for (; __j != __e && __binary_pred(*__i, *__j); ++__j) 1525 ; 1526 if (++__i != __j) { 1527 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j); 1528 __i = __j; 1529 } 1530 } 1531 1532 return (__remove_return_type)__deleted_nodes.size(); 1533} 1534 1535template <class _Tp, class _Alloc> 1536inline void list<_Tp, _Alloc>::merge(list& __c) { 1537 merge(__c, __less<>()); 1538} 1539 1540template <class _Tp, class _Alloc> 1541template <class _Comp> 1542void list<_Tp, _Alloc>::merge(list& __c, _Comp __comp) { 1543 if (this != std::addressof(__c)) { 1544 iterator __f1 = begin(); 1545 iterator __e1 = end(); 1546 iterator __f2 = __c.begin(); 1547 iterator __e2 = __c.end(); 1548 while (__f1 != __e1 && __f2 != __e2) { 1549 if (__comp(*__f2, *__f1)) { 1550 size_type __ds = 1; 1551 iterator __m2 = std::next(__f2); 1552 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, (void)++__ds) 1553 ; 1554 base::__sz() += __ds; 1555 __c.__sz() -= __ds; 1556 __link_pointer __f = __f2.__ptr_; 1557 __link_pointer __l = __m2.__ptr_->__prev_; 1558 __f2 = __m2; 1559 base::__unlink_nodes(__f, __l); 1560 __m2 = std::next(__f1); 1561 __link_nodes(__f1.__ptr_, __f, __l); 1562 __f1 = __m2; 1563 } else 1564 ++__f1; 1565 } 1566 splice(__e1, __c); 1567 } 1568} 1569 1570template <class _Tp, class _Alloc> 1571inline void list<_Tp, _Alloc>::sort() { 1572 sort(__less<>()); 1573} 1574 1575template <class _Tp, class _Alloc> 1576template <class _Comp> 1577inline void list<_Tp, _Alloc>::sort(_Comp __comp) { 1578 __sort(begin(), end(), base::__sz(), __comp); 1579} 1580 1581template <class _Tp, class _Alloc> 1582template <class _Comp> 1583typename list<_Tp, _Alloc>::iterator 1584list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp) { 1585 switch (__n) { 1586 case 0: 1587 case 1: 1588 return __f1; 1589 case 2: 1590 if (__comp(*--__e2, *__f1)) { 1591 __link_pointer __f = __e2.__ptr_; 1592 base::__unlink_nodes(__f, __f); 1593 __link_nodes(__f1.__ptr_, __f, __f); 1594 return __e2; 1595 } 1596 return __f1; 1597 } 1598 size_type __n2 = __n / 2; 1599 iterator __e1 = std::next(__f1, __n2); 1600 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp); 1601 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp); 1602 if (__comp(*__f2, *__f1)) { 1603 iterator __m2 = std::next(__f2); 1604 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2) 1605 ; 1606 __link_pointer __f = __f2.__ptr_; 1607 __link_pointer __l = __m2.__ptr_->__prev_; 1608 __r = __f2; 1609 __e1 = __f2 = __m2; 1610 base::__unlink_nodes(__f, __l); 1611 __m2 = std::next(__f1); 1612 __link_nodes(__f1.__ptr_, __f, __l); 1613 __f1 = __m2; 1614 } else 1615 ++__f1; 1616 while (__f1 != __e1 && __f2 != __e2) { 1617 if (__comp(*__f2, *__f1)) { 1618 iterator __m2 = std::next(__f2); 1619 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2) 1620 ; 1621 __link_pointer __f = __f2.__ptr_; 1622 __link_pointer __l = __m2.__ptr_->__prev_; 1623 if (__e1 == __f2) 1624 __e1 = __m2; 1625 __f2 = __m2; 1626 base::__unlink_nodes(__f, __l); 1627 __m2 = std::next(__f1); 1628 __link_nodes(__f1.__ptr_, __f, __l); 1629 __f1 = __m2; 1630 } else 1631 ++__f1; 1632 } 1633 return __r; 1634} 1635 1636template <class _Tp, class _Alloc> 1637void list<_Tp, _Alloc>::reverse() _NOEXCEPT { 1638 if (base::__sz() > 1) { 1639 iterator __e = end(); 1640 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;) { 1641 std::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_); 1642 __i.__ptr_ = __i.__ptr_->__prev_; 1643 } 1644 std::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_); 1645 } 1646} 1647 1648template <class _Tp, class _Alloc> 1649bool list<_Tp, _Alloc>::__invariants() const { 1650 return size() == std::distance(begin(), end()); 1651} 1652 1653template <class _Tp, class _Alloc> 1654inline _LIBCPP_HIDE_FROM_ABI bool operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) { 1655 return __x.size() == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin()); 1656} 1657 1658#if _LIBCPP_STD_VER <= 17 1659 1660template <class _Tp, class _Alloc> 1661inline _LIBCPP_HIDE_FROM_ABI bool operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) { 1662 return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); 1663} 1664 1665template <class _Tp, class _Alloc> 1666inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) { 1667 return !(__x == __y); 1668} 1669 1670template <class _Tp, class _Alloc> 1671inline _LIBCPP_HIDE_FROM_ABI bool operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) { 1672 return __y < __x; 1673} 1674 1675template <class _Tp, class _Alloc> 1676inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) { 1677 return !(__x < __y); 1678} 1679 1680template <class _Tp, class _Alloc> 1681inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) { 1682 return !(__y < __x); 1683} 1684 1685#else // _LIBCPP_STD_VER <= 17 1686 1687template <class _Tp, class _Allocator> 1688_LIBCPP_HIDE_FROM_ABI __synth_three_way_result<_Tp> 1689operator<=>(const list<_Tp, _Allocator>& __x, const list<_Tp, _Allocator>& __y) { 1690 return std::lexicographical_compare_three_way( 1691 __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way<_Tp, _Tp>); 1692} 1693 1694#endif // _LIBCPP_STD_VER <= 17 1695 1696template <class _Tp, class _Alloc> 1697inline _LIBCPP_HIDE_FROM_ABI void swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y) 1698 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) { 1699 __x.swap(__y); 1700} 1701 1702#if _LIBCPP_STD_VER >= 20 1703template <class _Tp, class _Allocator, class _Predicate> 1704inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type 1705erase_if(list<_Tp, _Allocator>& __c, _Predicate __pred) { 1706 return __c.remove_if(__pred); 1707} 1708 1709template <class _Tp, class _Allocator, class _Up> 1710inline _LIBCPP_HIDE_FROM_ABI typename list<_Tp, _Allocator>::size_type 1711erase(list<_Tp, _Allocator>& __c, const _Up& __v) { 1712 return std::erase_if(__c, [&](auto& __elem) { return __elem == __v; }); 1713} 1714 1715template <> 1716inline constexpr bool __format::__enable_insertable<std::list<char>> = true; 1717# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 1718template <> 1719inline constexpr bool __format::__enable_insertable<std::list<wchar_t>> = true; 1720# endif 1721 1722#endif // _LIBCPP_STD_VER >= 20 1723 1724_LIBCPP_END_NAMESPACE_STD 1725 1726#if _LIBCPP_STD_VER >= 17 1727_LIBCPP_BEGIN_NAMESPACE_STD 1728namespace pmr { 1729template <class _ValueT> 1730using list _LIBCPP_AVAILABILITY_PMR = std::list<_ValueT, polymorphic_allocator<_ValueT>>; 1731} // namespace pmr 1732_LIBCPP_END_NAMESPACE_STD 1733#endif 1734 1735_LIBCPP_POP_MACROS 1736 1737#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 1738# include <algorithm> 1739# include <atomic> 1740# include <concepts> 1741# include <cstdint> 1742# include <cstdlib> 1743# include <functional> 1744# include <iosfwd> 1745# include <iterator> 1746# include <stdexcept> 1747# include <type_traits> 1748# include <typeinfo> 1749#endif 1750 1751#endif // _LIBCPP_LIST 1752