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