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