1// -*- C++ -*- 2//===---------------------------- list ------------------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_LIST 12#define _LIBCPP_LIST 13 14/* 15 list synopsis 16 17namespace std 18{ 19 20template <class T, class Alloc = allocator<T> > 21class list 22{ 23public: 24 25 // types: 26 typedef T value_type; 27 typedef Alloc allocator_type; 28 typedef typename allocator_type::reference reference; 29 typedef typename allocator_type::const_reference const_reference; 30 typedef typename allocator_type::pointer pointer; 31 typedef typename allocator_type::const_pointer const_pointer; 32 typedef implementation-defined iterator; 33 typedef implementation-defined const_iterator; 34 typedef implementation-defined size_type; 35 typedef implementation-defined difference_type; 36 typedef reverse_iterator<iterator> reverse_iterator; 37 typedef reverse_iterator<const_iterator> const_reverse_iterator; 38 39 list() 40 noexcept(is_nothrow_default_constructible<allocator_type>::value); 41 explicit list(const allocator_type& a); 42 explicit list(size_type n); 43 explicit list(size_type n, const allocator_type& a); // C++14 44 list(size_type n, const value_type& value); 45 list(size_type n, const value_type& value, const allocator_type& a); 46 template <class Iter> 47 list(Iter first, Iter last); 48 template <class Iter> 49 list(Iter first, Iter last, const allocator_type& a); 50 list(const list& x); 51 list(const list&, const allocator_type& a); 52 list(list&& x) 53 noexcept(is_nothrow_move_constructible<allocator_type>::value); 54 list(list&&, const allocator_type& a); 55 list(initializer_list<value_type>); 56 list(initializer_list<value_type>, const allocator_type& a); 57 58 ~list(); 59 60 list& operator=(const list& x); 61 list& operator=(list&& x) 62 noexcept( 63 allocator_type::propagate_on_container_move_assignment::value && 64 is_nothrow_move_assignable<allocator_type>::value); 65 list& operator=(initializer_list<value_type>); 66 template <class Iter> 67 void assign(Iter first, Iter last); 68 void assign(size_type n, const value_type& t); 69 void assign(initializer_list<value_type>); 70 71 allocator_type get_allocator() const noexcept; 72 73 iterator begin() noexcept; 74 const_iterator begin() const noexcept; 75 iterator end() noexcept; 76 const_iterator end() const noexcept; 77 reverse_iterator rbegin() noexcept; 78 const_reverse_iterator rbegin() const noexcept; 79 reverse_iterator rend() noexcept; 80 const_reverse_iterator rend() const noexcept; 81 const_iterator cbegin() const noexcept; 82 const_iterator cend() const noexcept; 83 const_reverse_iterator crbegin() const noexcept; 84 const_reverse_iterator crend() const noexcept; 85 86 reference front(); 87 const_reference front() const; 88 reference back(); 89 const_reference back() const; 90 91 bool empty() const noexcept; 92 size_type size() const noexcept; 93 size_type max_size() const noexcept; 94 95 template <class... Args> 96 reference emplace_front(Args&&... args); 97 void pop_front(); 98 template <class... Args> 99 reference emplace_back(Args&&... args); 100 void pop_back(); 101 void push_front(const value_type& x); 102 void push_front(value_type&& x); 103 void push_back(const value_type& x); 104 void push_back(value_type&& x); 105 template <class... Args> 106 iterator emplace(const_iterator position, Args&&... args); 107 iterator insert(const_iterator position, const value_type& x); 108 iterator insert(const_iterator position, value_type&& x); 109 iterator insert(const_iterator position, size_type n, const value_type& x); 110 template <class Iter> 111 iterator insert(const_iterator position, Iter first, Iter last); 112 iterator insert(const_iterator position, initializer_list<value_type> il); 113 114 iterator erase(const_iterator position); 115 iterator erase(const_iterator position, const_iterator last); 116 117 void resize(size_type sz); 118 void resize(size_type sz, const value_type& c); 119 120 void swap(list&) 121 noexcept(allocator_traits<allocator_type>::is_always_equal::value); // C++17 122 void clear() noexcept; 123 124 void splice(const_iterator position, list& x); 125 void splice(const_iterator position, list&& x); 126 void splice(const_iterator position, list& x, const_iterator i); 127 void splice(const_iterator position, list&& x, const_iterator i); 128 void splice(const_iterator position, list& x, const_iterator first, 129 const_iterator last); 130 void splice(const_iterator position, list&& x, const_iterator first, 131 const_iterator last); 132 133 void remove(const value_type& value); 134 template <class Pred> void remove_if(Pred pred); 135 void unique(); 136 template <class BinaryPredicate> 137 void unique(BinaryPredicate binary_pred); 138 void merge(list& x); 139 void merge(list&& x); 140 template <class Compare> 141 void merge(list& x, Compare comp); 142 template <class Compare> 143 void merge(list&& x, Compare comp); 144 void sort(); 145 template <class Compare> 146 void sort(Compare comp); 147 void reverse() noexcept; 148}; 149 150template <class T, class Alloc> 151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y); 152template <class T, class Alloc> 153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y); 154template <class T, class Alloc> 155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y); 156template <class T, class Alloc> 157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y); 158template <class T, class Alloc> 159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y); 160template <class T, class Alloc> 161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y); 162 163template <class T, class Alloc> 164 void swap(list<T,Alloc>& x, list<T,Alloc>& y) 165 noexcept(noexcept(x.swap(y))); 166 167} // std 168 169*/ 170 171#include <__config> 172 173#include <memory> 174#include <limits> 175#include <initializer_list> 176#include <iterator> 177#include <algorithm> 178#include <type_traits> 179 180#include <__undef_min_max> 181 182#include <__debug> 183 184#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 185#pragma GCC system_header 186#endif 187 188_LIBCPP_BEGIN_NAMESPACE_STD 189 190template <class _Tp, class _VoidPtr> struct __list_node; 191template <class _Tp, class _VoidPtr> struct __list_node_base; 192 193template <class _Tp, class _VoidPtr> 194struct __list_node_pointer_traits { 195 typedef typename __rebind_pointer<_VoidPtr, __list_node<_Tp, _VoidPtr> >::type 196 __node_pointer; 197 typedef typename __rebind_pointer<_VoidPtr, __list_node_base<_Tp, _VoidPtr> >::type 198 __base_pointer; 199 200#if defined(_LIBCPP_ABI_LIST_REMOVE_NODE_POINTER_UB) 201 typedef __base_pointer __link_pointer; 202#else 203 typedef typename conditional< 204 is_pointer<_VoidPtr>::value, 205 __base_pointer, 206 __node_pointer 207 >::type __link_pointer; 208#endif 209 210 typedef typename conditional< 211 is_same<__link_pointer, __node_pointer>::value, 212 __base_pointer, 213 __node_pointer 214 >::type __non_link_pointer; 215 216 static _LIBCPP_INLINE_VISIBILITY 217 __link_pointer __unsafe_link_pointer_cast(__link_pointer __p) { 218 return __p; 219 } 220 221 static _LIBCPP_INLINE_VISIBILITY 222 __link_pointer __unsafe_link_pointer_cast(__non_link_pointer __p) { 223 return static_cast<__link_pointer>(static_cast<_VoidPtr>(__p)); 224 } 225 226}; 227 228template <class _Tp, class _VoidPtr> 229struct __list_node_base 230{ 231 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits; 232 typedef typename _NodeTraits::__node_pointer __node_pointer; 233 typedef typename _NodeTraits::__base_pointer __base_pointer; 234 typedef typename _NodeTraits::__link_pointer __link_pointer; 235 236 __link_pointer __prev_; 237 __link_pointer __next_; 238 239 _LIBCPP_INLINE_VISIBILITY 240 __list_node_base() : __prev_(_NodeTraits::__unsafe_link_pointer_cast(__self())), 241 __next_(_NodeTraits::__unsafe_link_pointer_cast(__self())) {} 242 243 _LIBCPP_INLINE_VISIBILITY 244 __base_pointer __self() { 245 return pointer_traits<__base_pointer>::pointer_to(*this); 246 } 247 248 _LIBCPP_INLINE_VISIBILITY 249 __node_pointer __as_node() { 250 return static_cast<__node_pointer>(__self()); 251 } 252}; 253 254template <class _Tp, class _VoidPtr> 255struct __list_node 256 : public __list_node_base<_Tp, _VoidPtr> 257{ 258 _Tp __value_; 259 260 typedef __list_node_base<_Tp, _VoidPtr> __base; 261 typedef typename __base::__link_pointer __link_pointer; 262 263 _LIBCPP_INLINE_VISIBILITY 264 __link_pointer __as_link() { 265 return static_cast<__link_pointer>(__base::__self()); 266 } 267}; 268 269template <class _Tp, class _Alloc = allocator<_Tp> > class _LIBCPP_TYPE_VIS_ONLY list; 270template <class _Tp, class _Alloc> class __list_imp; 271template <class _Tp, class _VoidPtr> class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator; 272 273template <class _Tp, class _VoidPtr> 274class _LIBCPP_TYPE_VIS_ONLY __list_iterator 275{ 276 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits; 277 typedef typename _NodeTraits::__link_pointer __link_pointer; 278 279 __link_pointer __ptr_; 280 281#if _LIBCPP_DEBUG_LEVEL >= 2 282 _LIBCPP_INLINE_VISIBILITY 283 explicit __list_iterator(__link_pointer __p, const void* __c) _NOEXCEPT 284 : __ptr_(__p) 285 { 286 __get_db()->__insert_ic(this, __c); 287 } 288#else 289 _LIBCPP_INLINE_VISIBILITY 290 explicit __list_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {} 291#endif 292 293 294 295 template<class, class> friend class list; 296 template<class, class> friend class __list_imp; 297 template<class, class> friend class __list_const_iterator; 298public: 299 typedef bidirectional_iterator_tag iterator_category; 300 typedef _Tp value_type; 301 typedef value_type& reference; 302 typedef typename __rebind_pointer<_VoidPtr, value_type>::type pointer; 303 typedef typename pointer_traits<pointer>::difference_type difference_type; 304 305 _LIBCPP_INLINE_VISIBILITY 306 __list_iterator() _NOEXCEPT : __ptr_(nullptr) 307 { 308#if _LIBCPP_DEBUG_LEVEL >= 2 309 __get_db()->__insert_i(this); 310#endif 311 } 312 313#if _LIBCPP_DEBUG_LEVEL >= 2 314 315 _LIBCPP_INLINE_VISIBILITY 316 __list_iterator(const __list_iterator& __p) 317 : __ptr_(__p.__ptr_) 318 { 319 __get_db()->__iterator_copy(this, &__p); 320 } 321 322 _LIBCPP_INLINE_VISIBILITY 323 ~__list_iterator() 324 { 325 __get_db()->__erase_i(this); 326 } 327 328 _LIBCPP_INLINE_VISIBILITY 329 __list_iterator& operator=(const __list_iterator& __p) 330 { 331 if (this != &__p) 332 { 333 __get_db()->__iterator_copy(this, &__p); 334 __ptr_ = __p.__ptr_; 335 } 336 return *this; 337 } 338 339#endif // _LIBCPP_DEBUG_LEVEL >= 2 340 341 _LIBCPP_INLINE_VISIBILITY 342 reference operator*() const 343 { 344#if _LIBCPP_DEBUG_LEVEL >= 2 345 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), 346 "Attempted to dereference a non-dereferenceable list::iterator"); 347#endif 348 return __ptr_->__as_node()->__value_; 349 } 350 _LIBCPP_INLINE_VISIBILITY 351 pointer operator->() const 352 { 353#if _LIBCPP_DEBUG_LEVEL >= 2 354 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), 355 "Attempted to dereference a non-dereferenceable list::iterator"); 356#endif 357 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_); 358 } 359 360 _LIBCPP_INLINE_VISIBILITY 361 __list_iterator& operator++() 362 { 363#if _LIBCPP_DEBUG_LEVEL >= 2 364 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), 365 "Attempted to increment non-incrementable list::iterator"); 366#endif 367 __ptr_ = __ptr_->__next_; 368 return *this; 369 } 370 _LIBCPP_INLINE_VISIBILITY 371 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;} 372 373 _LIBCPP_INLINE_VISIBILITY 374 __list_iterator& operator--() 375 { 376#if _LIBCPP_DEBUG_LEVEL >= 2 377 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this), 378 "Attempted to decrement non-decrementable list::iterator"); 379#endif 380 __ptr_ = __ptr_->__prev_; 381 return *this; 382 } 383 _LIBCPP_INLINE_VISIBILITY 384 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;} 385 386 friend _LIBCPP_INLINE_VISIBILITY 387 bool operator==(const __list_iterator& __x, const __list_iterator& __y) 388 { 389 return __x.__ptr_ == __y.__ptr_; 390 } 391 friend _LIBCPP_INLINE_VISIBILITY 392 bool operator!=(const __list_iterator& __x, const __list_iterator& __y) 393 {return !(__x == __y);} 394}; 395 396template <class _Tp, class _VoidPtr> 397class _LIBCPP_TYPE_VIS_ONLY __list_const_iterator 398{ 399 typedef __list_node_pointer_traits<_Tp, _VoidPtr> _NodeTraits; 400 typedef typename _NodeTraits::__link_pointer __link_pointer; 401 402 __link_pointer __ptr_; 403 404#if _LIBCPP_DEBUG_LEVEL >= 2 405 _LIBCPP_INLINE_VISIBILITY 406 explicit __list_const_iterator(__link_pointer __p, const void* __c) _NOEXCEPT 407 : __ptr_(__p) 408 { 409 __get_db()->__insert_ic(this, __c); 410 } 411#else 412 _LIBCPP_INLINE_VISIBILITY 413 explicit __list_const_iterator(__link_pointer __p) _NOEXCEPT : __ptr_(__p) {} 414#endif 415 416 template<class, class> friend class list; 417 template<class, class> friend class __list_imp; 418public: 419 typedef bidirectional_iterator_tag iterator_category; 420 typedef _Tp value_type; 421 typedef const value_type& reference; 422 typedef typename __rebind_pointer<_VoidPtr, const value_type>::type pointer; 423 typedef typename pointer_traits<pointer>::difference_type difference_type; 424 425 _LIBCPP_INLINE_VISIBILITY 426 __list_const_iterator() _NOEXCEPT : __ptr_(nullptr) 427 { 428#if _LIBCPP_DEBUG_LEVEL >= 2 429 __get_db()->__insert_i(this); 430#endif 431 } 432 _LIBCPP_INLINE_VISIBILITY 433 __list_const_iterator(const __list_iterator<_Tp, _VoidPtr>& __p) _NOEXCEPT 434 : __ptr_(__p.__ptr_) 435 { 436#if _LIBCPP_DEBUG_LEVEL >= 2 437 __get_db()->__iterator_copy(this, &__p); 438#endif 439 } 440 441#if _LIBCPP_DEBUG_LEVEL >= 2 442 443 _LIBCPP_INLINE_VISIBILITY 444 __list_const_iterator(const __list_const_iterator& __p) 445 : __ptr_(__p.__ptr_) 446 { 447 __get_db()->__iterator_copy(this, &__p); 448 } 449 450 _LIBCPP_INLINE_VISIBILITY 451 ~__list_const_iterator() 452 { 453 __get_db()->__erase_i(this); 454 } 455 456 _LIBCPP_INLINE_VISIBILITY 457 __list_const_iterator& operator=(const __list_const_iterator& __p) 458 { 459 if (this != &__p) 460 { 461 __get_db()->__iterator_copy(this, &__p); 462 __ptr_ = __p.__ptr_; 463 } 464 return *this; 465 } 466 467#endif // _LIBCPP_DEBUG_LEVEL >= 2 468 _LIBCPP_INLINE_VISIBILITY 469 reference operator*() const 470 { 471#if _LIBCPP_DEBUG_LEVEL >= 2 472 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), 473 "Attempted to dereference a non-dereferenceable list::const_iterator"); 474#endif 475 return __ptr_->__as_node()->__value_; 476 } 477 _LIBCPP_INLINE_VISIBILITY 478 pointer operator->() const 479 { 480#if _LIBCPP_DEBUG_LEVEL >= 2 481 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), 482 "Attempted to dereference a non-dereferenceable list::iterator"); 483#endif 484 return pointer_traits<pointer>::pointer_to(__ptr_->__as_node()->__value_); 485 } 486 487 _LIBCPP_INLINE_VISIBILITY 488 __list_const_iterator& operator++() 489 { 490#if _LIBCPP_DEBUG_LEVEL >= 2 491 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(this), 492 "Attempted to increment non-incrementable list::const_iterator"); 493#endif 494 __ptr_ = __ptr_->__next_; 495 return *this; 496 } 497 _LIBCPP_INLINE_VISIBILITY 498 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;} 499 500 _LIBCPP_INLINE_VISIBILITY 501 __list_const_iterator& operator--() 502 { 503#if _LIBCPP_DEBUG_LEVEL >= 2 504 _LIBCPP_ASSERT(__get_const_db()->__decrementable(this), 505 "Attempted to decrement non-decrementable list::const_iterator"); 506#endif 507 __ptr_ = __ptr_->__prev_; 508 return *this; 509 } 510 _LIBCPP_INLINE_VISIBILITY 511 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;} 512 513 friend _LIBCPP_INLINE_VISIBILITY 514 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y) 515 { 516 return __x.__ptr_ == __y.__ptr_; 517 } 518 friend _LIBCPP_INLINE_VISIBILITY 519 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y) 520 {return !(__x == __y);} 521}; 522 523template <class _Tp, class _Alloc> 524class __list_imp 525{ 526 __list_imp(const __list_imp&); 527 __list_imp& operator=(const __list_imp&); 528protected: 529 typedef _Tp value_type; 530 typedef _Alloc allocator_type; 531 typedef allocator_traits<allocator_type> __alloc_traits; 532 typedef typename __alloc_traits::size_type size_type; 533 typedef typename __alloc_traits::void_pointer __void_pointer; 534 typedef __list_iterator<value_type, __void_pointer> iterator; 535 typedef __list_const_iterator<value_type, __void_pointer> const_iterator; 536 typedef __list_node_base<value_type, __void_pointer> __node_base; 537 typedef __list_node<value_type, __void_pointer> __node; 538 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator; 539 typedef allocator_traits<__node_allocator> __node_alloc_traits; 540 typedef typename __node_alloc_traits::pointer __node_pointer; 541 typedef typename __node_alloc_traits::pointer __node_const_pointer; 542 typedef __list_node_pointer_traits<value_type, __void_pointer> __node_pointer_traits; 543 typedef typename __node_pointer_traits::__link_pointer __link_pointer; 544 typedef __link_pointer __link_const_pointer; 545 typedef typename __alloc_traits::pointer pointer; 546 typedef typename __alloc_traits::const_pointer const_pointer; 547 typedef typename __alloc_traits::difference_type difference_type; 548 549 typedef typename __rebind_alloc_helper<__alloc_traits, __node_base>::type __node_base_allocator; 550 typedef typename allocator_traits<__node_base_allocator>::pointer __node_base_pointer; 551 552 __node_base __end_; 553 __compressed_pair<size_type, __node_allocator> __size_alloc_; 554 555 _LIBCPP_INLINE_VISIBILITY 556 __link_pointer __end_as_link() const _NOEXCEPT { 557 return __node_pointer_traits::__unsafe_link_pointer_cast( 558 const_cast<__node_base&>(__end_).__self()); 559 } 560 561 _LIBCPP_INLINE_VISIBILITY 562 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();} 563 _LIBCPP_INLINE_VISIBILITY 564 const size_type& __sz() const _NOEXCEPT 565 {return __size_alloc_.first();} 566 _LIBCPP_INLINE_VISIBILITY 567 __node_allocator& __node_alloc() _NOEXCEPT 568 {return __size_alloc_.second();} 569 _LIBCPP_INLINE_VISIBILITY 570 const __node_allocator& __node_alloc() const _NOEXCEPT 571 {return __size_alloc_.second();} 572 573 _LIBCPP_INLINE_VISIBILITY 574 size_type __node_alloc_max_size() const _NOEXCEPT { 575 return __node_alloc_traits::max_size(__node_alloc()); 576 } 577 _LIBCPP_INLINE_VISIBILITY 578 static void __unlink_nodes(__link_pointer __f, __link_pointer __l) _NOEXCEPT; 579 580 _LIBCPP_INLINE_VISIBILITY 581 __list_imp() 582 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value); 583 _LIBCPP_INLINE_VISIBILITY 584 __list_imp(const allocator_type& __a); 585 ~__list_imp(); 586 void clear() _NOEXCEPT; 587 _LIBCPP_INLINE_VISIBILITY 588 bool empty() const _NOEXCEPT {return __sz() == 0;} 589 590 _LIBCPP_INLINE_VISIBILITY 591 iterator begin() _NOEXCEPT 592 { 593#if _LIBCPP_DEBUG_LEVEL >= 2 594 return iterator(__end_.__next_, this); 595#else 596 return iterator(__end_.__next_); 597#endif 598 } 599 _LIBCPP_INLINE_VISIBILITY 600 const_iterator begin() const _NOEXCEPT 601 { 602#if _LIBCPP_DEBUG_LEVEL >= 2 603 return const_iterator(__end_.__next_, this); 604#else 605 return const_iterator(__end_.__next_); 606#endif 607 } 608 _LIBCPP_INLINE_VISIBILITY 609 iterator end() _NOEXCEPT 610 { 611#if _LIBCPP_DEBUG_LEVEL >= 2 612 return iterator(__end_as_link(), this); 613#else 614 return iterator(__end_as_link()); 615#endif 616 } 617 _LIBCPP_INLINE_VISIBILITY 618 const_iterator end() const _NOEXCEPT 619 { 620#if _LIBCPP_DEBUG_LEVEL >= 2 621 return const_iterator(__end_as_link(), this); 622#else 623 return const_iterator(__end_as_link()); 624#endif 625 } 626 627 void swap(__list_imp& __c) 628#if _LIBCPP_STD_VER >= 14 629 _NOEXCEPT; 630#else 631 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 632 __is_nothrow_swappable<allocator_type>::value); 633#endif 634 635 _LIBCPP_INLINE_VISIBILITY 636 void __copy_assign_alloc(const __list_imp& __c) 637 {__copy_assign_alloc(__c, integral_constant<bool, 638 __node_alloc_traits::propagate_on_container_copy_assignment::value>());} 639 640 _LIBCPP_INLINE_VISIBILITY 641 void __move_assign_alloc(__list_imp& __c) 642 _NOEXCEPT_( 643 !__node_alloc_traits::propagate_on_container_move_assignment::value || 644 is_nothrow_move_assignable<__node_allocator>::value) 645 {__move_assign_alloc(__c, integral_constant<bool, 646 __node_alloc_traits::propagate_on_container_move_assignment::value>());} 647 648private: 649 _LIBCPP_INLINE_VISIBILITY 650 void __copy_assign_alloc(const __list_imp& __c, true_type) 651 { 652 if (__node_alloc() != __c.__node_alloc()) 653 clear(); 654 __node_alloc() = __c.__node_alloc(); 655 } 656 657 _LIBCPP_INLINE_VISIBILITY 658 void __copy_assign_alloc(const __list_imp& __c, false_type) 659 {} 660 661 _LIBCPP_INLINE_VISIBILITY 662 void __move_assign_alloc(__list_imp& __c, true_type) 663 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) 664 { 665 __node_alloc() = _VSTD::move(__c.__node_alloc()); 666 } 667 668 _LIBCPP_INLINE_VISIBILITY 669 void __move_assign_alloc(__list_imp& __c, false_type) 670 _NOEXCEPT 671 {} 672}; 673 674// Unlink nodes [__f, __l] 675template <class _Tp, class _Alloc> 676inline 677void 678__list_imp<_Tp, _Alloc>::__unlink_nodes(__link_pointer __f, __link_pointer __l) 679 _NOEXCEPT 680{ 681 __f->__prev_->__next_ = __l->__next_; 682 __l->__next_->__prev_ = __f->__prev_; 683} 684 685template <class _Tp, class _Alloc> 686inline 687__list_imp<_Tp, _Alloc>::__list_imp() 688 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) 689 : __size_alloc_(0) 690{ 691} 692 693template <class _Tp, class _Alloc> 694inline 695__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a) 696 : __size_alloc_(0, __node_allocator(__a)) 697{ 698} 699 700template <class _Tp, class _Alloc> 701__list_imp<_Tp, _Alloc>::~__list_imp() 702{ 703 clear(); 704#if _LIBCPP_DEBUG_LEVEL >= 2 705 __get_db()->__erase_c(this); 706#endif 707} 708 709template <class _Tp, class _Alloc> 710void 711__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT 712{ 713 if (!empty()) 714 { 715 __node_allocator& __na = __node_alloc(); 716 __link_pointer __f = __end_.__next_; 717 __link_pointer __l = __end_as_link(); 718 __unlink_nodes(__f, __l->__prev_); 719 __sz() = 0; 720 while (__f != __l) 721 { 722 __node_pointer __np = __f->__as_node(); 723 __f = __f->__next_; 724 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_)); 725 __node_alloc_traits::deallocate(__na, __np, 1); 726 } 727#if _LIBCPP_DEBUG_LEVEL >= 2 728 __c_node* __c = __get_db()->__find_c_and_lock(this); 729 for (__i_node** __p = __c->end_; __p != __c->beg_; ) 730 { 731 --__p; 732 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_); 733 if (__i->__ptr_ != __l) 734 { 735 (*__p)->__c_ = nullptr; 736 if (--__c->end_ != __p) 737 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); 738 } 739 } 740 __get_db()->unlock(); 741#endif 742 } 743} 744 745template <class _Tp, class _Alloc> 746void 747__list_imp<_Tp, _Alloc>::swap(__list_imp& __c) 748#if _LIBCPP_STD_VER >= 14 749 _NOEXCEPT 750#else 751 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 752 __is_nothrow_swappable<allocator_type>::value) 753#endif 754{ 755 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value || 756 this->__node_alloc() == __c.__node_alloc(), 757 "list::swap: Either propagate_on_container_swap must be true" 758 " or the allocators must compare equal"); 759 using _VSTD::swap; 760 __swap_allocator(__node_alloc(), __c.__node_alloc()); 761 swap(__sz(), __c.__sz()); 762 swap(__end_, __c.__end_); 763 if (__sz() == 0) 764 __end_.__next_ = __end_.__prev_ = __end_as_link(); 765 else 766 __end_.__prev_->__next_ = __end_.__next_->__prev_ = __end_as_link(); 767 if (__c.__sz() == 0) 768 __c.__end_.__next_ = __c.__end_.__prev_ = __c.__end_as_link(); 769 else 770 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_ = __c.__end_as_link(); 771 772#if _LIBCPP_DEBUG_LEVEL >= 2 773 __libcpp_db* __db = __get_db(); 774 __c_node* __cn1 = __db->__find_c_and_lock(this); 775 __c_node* __cn2 = __db->__find_c(&__c); 776 std::swap(__cn1->beg_, __cn2->beg_); 777 std::swap(__cn1->end_, __cn2->end_); 778 std::swap(__cn1->cap_, __cn2->cap_); 779 for (__i_node** __p = __cn1->end_; __p != __cn1->beg_;) 780 { 781 --__p; 782 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_); 783 if (__i->__ptr_ == __c.__end_as_link()) 784 { 785 __cn2->__add(*__p); 786 if (--__cn1->end_ != __p) 787 memmove(__p, __p+1, (__cn1->end_ - __p)*sizeof(__i_node*)); 788 } 789 else 790 (*__p)->__c_ = __cn1; 791 } 792 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;) 793 { 794 --__p; 795 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_); 796 if (__i->__ptr_ == __end_as_link()) 797 { 798 __cn1->__add(*__p); 799 if (--__cn2->end_ != __p) 800 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*)); 801 } 802 else 803 (*__p)->__c_ = __cn2; 804 } 805 __db->unlock(); 806#endif 807} 808 809template <class _Tp, class _Alloc /*= allocator<_Tp>*/> 810class _LIBCPP_TYPE_VIS_ONLY list 811 : private __list_imp<_Tp, _Alloc> 812{ 813 typedef __list_imp<_Tp, _Alloc> base; 814 typedef typename base::__node __node; 815 typedef typename base::__node_allocator __node_allocator; 816 typedef typename base::__node_pointer __node_pointer; 817 typedef typename base::__node_alloc_traits __node_alloc_traits; 818 typedef typename base::__node_base __node_base; 819 typedef typename base::__node_base_pointer __node_base_pointer; 820 typedef typename base::__link_pointer __link_pointer; 821 822public: 823 typedef _Tp value_type; 824 typedef _Alloc allocator_type; 825 static_assert((is_same<value_type, typename allocator_type::value_type>::value), 826 "Invalid allocator::value_type"); 827 typedef value_type& reference; 828 typedef const value_type& const_reference; 829 typedef typename base::pointer pointer; 830 typedef typename base::const_pointer const_pointer; 831 typedef typename base::size_type size_type; 832 typedef typename base::difference_type difference_type; 833 typedef typename base::iterator iterator; 834 typedef typename base::const_iterator const_iterator; 835 typedef _VSTD::reverse_iterator<iterator> reverse_iterator; 836 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; 837 838 _LIBCPP_INLINE_VISIBILITY 839 list() 840 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) 841 { 842#if _LIBCPP_DEBUG_LEVEL >= 2 843 __get_db()->__insert_c(this); 844#endif 845 } 846 _LIBCPP_INLINE_VISIBILITY 847 explicit list(const allocator_type& __a) : base(__a) 848 { 849#if _LIBCPP_DEBUG_LEVEL >= 2 850 __get_db()->__insert_c(this); 851#endif 852 } 853 explicit list(size_type __n); 854#if _LIBCPP_STD_VER > 11 855 explicit list(size_type __n, const allocator_type& __a); 856#endif 857 list(size_type __n, const value_type& __x); 858 list(size_type __n, const value_type& __x, const allocator_type& __a); 859 template <class _InpIter> 860 list(_InpIter __f, _InpIter __l, 861 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0); 862 template <class _InpIter> 863 list(_InpIter __f, _InpIter __l, const allocator_type& __a, 864 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0); 865 866 list(const list& __c); 867 list(const list& __c, const allocator_type& __a); 868 _LIBCPP_INLINE_VISIBILITY 869 list& operator=(const list& __c); 870#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 871 list(initializer_list<value_type> __il); 872 list(initializer_list<value_type> __il, const allocator_type& __a); 873#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 874#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 875 _LIBCPP_INLINE_VISIBILITY 876 list(list&& __c) 877 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value); 878 _LIBCPP_INLINE_VISIBILITY 879 list(list&& __c, const allocator_type& __a); 880 _LIBCPP_INLINE_VISIBILITY 881 list& operator=(list&& __c) 882 _NOEXCEPT_( 883 __node_alloc_traits::propagate_on_container_move_assignment::value && 884 is_nothrow_move_assignable<__node_allocator>::value); 885#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 886#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 887 _LIBCPP_INLINE_VISIBILITY 888 list& operator=(initializer_list<value_type> __il) 889 {assign(__il.begin(), __il.end()); return *this;} 890#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 891 892 template <class _InpIter> 893 void assign(_InpIter __f, _InpIter __l, 894 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0); 895 void assign(size_type __n, const value_type& __x); 896#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 897 _LIBCPP_INLINE_VISIBILITY 898 void assign(initializer_list<value_type> __il) 899 {assign(__il.begin(), __il.end());} 900#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 901 902 _LIBCPP_INLINE_VISIBILITY 903 allocator_type get_allocator() const _NOEXCEPT; 904 905 _LIBCPP_INLINE_VISIBILITY 906 size_type size() const _NOEXCEPT {return base::__sz();} 907 _LIBCPP_INLINE_VISIBILITY 908 bool empty() const _NOEXCEPT {return base::empty();} 909 _LIBCPP_INLINE_VISIBILITY 910 size_type max_size() const _NOEXCEPT 911 { 912 return std::min<size_type>( 913 base::__node_alloc_max_size(), 914 numeric_limits<difference_type >::max()); 915 } 916 917 _LIBCPP_INLINE_VISIBILITY 918 iterator begin() _NOEXCEPT {return base::begin();} 919 _LIBCPP_INLINE_VISIBILITY 920 const_iterator begin() const _NOEXCEPT {return base::begin();} 921 _LIBCPP_INLINE_VISIBILITY 922 iterator end() _NOEXCEPT {return base::end();} 923 _LIBCPP_INLINE_VISIBILITY 924 const_iterator end() const _NOEXCEPT {return base::end();} 925 _LIBCPP_INLINE_VISIBILITY 926 const_iterator cbegin() const _NOEXCEPT {return base::begin();} 927 _LIBCPP_INLINE_VISIBILITY 928 const_iterator cend() const _NOEXCEPT {return base::end();} 929 930 _LIBCPP_INLINE_VISIBILITY 931 reverse_iterator rbegin() _NOEXCEPT 932 {return reverse_iterator(end());} 933 _LIBCPP_INLINE_VISIBILITY 934 const_reverse_iterator rbegin() const _NOEXCEPT 935 {return const_reverse_iterator(end());} 936 _LIBCPP_INLINE_VISIBILITY 937 reverse_iterator rend() _NOEXCEPT 938 {return reverse_iterator(begin());} 939 _LIBCPP_INLINE_VISIBILITY 940 const_reverse_iterator rend() const _NOEXCEPT 941 {return const_reverse_iterator(begin());} 942 _LIBCPP_INLINE_VISIBILITY 943 const_reverse_iterator crbegin() const _NOEXCEPT 944 {return const_reverse_iterator(end());} 945 _LIBCPP_INLINE_VISIBILITY 946 const_reverse_iterator crend() const _NOEXCEPT 947 {return const_reverse_iterator(begin());} 948 949 _LIBCPP_INLINE_VISIBILITY 950 reference front() 951 { 952 _LIBCPP_ASSERT(!empty(), "list::front called on empty list"); 953 return base::__end_.__next_->__as_node()->__value_; 954 } 955 _LIBCPP_INLINE_VISIBILITY 956 const_reference front() const 957 { 958 _LIBCPP_ASSERT(!empty(), "list::front called on empty list"); 959 return base::__end_.__next_->__as_node()->__value_; 960 } 961 _LIBCPP_INLINE_VISIBILITY 962 reference back() 963 { 964 _LIBCPP_ASSERT(!empty(), "list::back called on empty list"); 965 return base::__end_.__prev_->__as_node()->__value_; 966 } 967 _LIBCPP_INLINE_VISIBILITY 968 const_reference back() const 969 { 970 _LIBCPP_ASSERT(!empty(), "list::back called on empty list"); 971 return base::__end_.__prev_->__as_node()->__value_; 972 } 973 974#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 975 void push_front(value_type&& __x); 976 void push_back(value_type&& __x); 977#ifndef _LIBCPP_HAS_NO_VARIADICS 978 template <class... _Args> 979 reference emplace_front(_Args&&... __args); 980 template <class... _Args> 981 reference emplace_back(_Args&&... __args); 982 template <class... _Args> 983 iterator emplace(const_iterator __p, _Args&&... __args); 984#endif // _LIBCPP_HAS_NO_VARIADICS 985 iterator insert(const_iterator __p, value_type&& __x); 986#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 987 988 void push_front(const value_type& __x); 989 void push_back(const value_type& __x); 990 991 iterator insert(const_iterator __p, const value_type& __x); 992 iterator insert(const_iterator __p, size_type __n, const value_type& __x); 993 template <class _InpIter> 994 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l, 995 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0); 996#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 997 _LIBCPP_INLINE_VISIBILITY 998 iterator insert(const_iterator __p, initializer_list<value_type> __il) 999 {return insert(__p, __il.begin(), __il.end());} 1000#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 1001 1002 _LIBCPP_INLINE_VISIBILITY 1003 void swap(list& __c) 1004#if _LIBCPP_STD_VER >= 14 1005 _NOEXCEPT 1006#else 1007 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value || 1008 __is_nothrow_swappable<__node_allocator>::value) 1009#endif 1010 {base::swap(__c);} 1011 _LIBCPP_INLINE_VISIBILITY 1012 void clear() _NOEXCEPT {base::clear();} 1013 1014 void pop_front(); 1015 void pop_back(); 1016 1017 iterator erase(const_iterator __p); 1018 iterator erase(const_iterator __f, const_iterator __l); 1019 1020 void resize(size_type __n); 1021 void resize(size_type __n, const value_type& __x); 1022 1023 void splice(const_iterator __p, list& __c); 1024#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1025 _LIBCPP_INLINE_VISIBILITY 1026 void splice(const_iterator __p, list&& __c) {splice(__p, __c);} 1027#endif 1028 void splice(const_iterator __p, list& __c, const_iterator __i); 1029#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1030 _LIBCPP_INLINE_VISIBILITY 1031 void splice(const_iterator __p, list&& __c, const_iterator __i) 1032 {splice(__p, __c, __i);} 1033#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1034 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l); 1035#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1036 _LIBCPP_INLINE_VISIBILITY 1037 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l) 1038 {splice(__p, __c, __f, __l);} 1039#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1040 1041 void remove(const value_type& __x); 1042 template <class _Pred> void remove_if(_Pred __pred); 1043 _LIBCPP_INLINE_VISIBILITY 1044 void unique(); 1045 template <class _BinaryPred> 1046 void unique(_BinaryPred __binary_pred); 1047 _LIBCPP_INLINE_VISIBILITY 1048 void merge(list& __c); 1049#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1050 _LIBCPP_INLINE_VISIBILITY 1051 void merge(list&& __c) {merge(__c);} 1052#endif 1053 template <class _Comp> 1054 void merge(list& __c, _Comp __comp); 1055#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1056 template <class _Comp> 1057 _LIBCPP_INLINE_VISIBILITY 1058 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);} 1059#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1060 _LIBCPP_INLINE_VISIBILITY 1061 void sort(); 1062 template <class _Comp> 1063 _LIBCPP_INLINE_VISIBILITY 1064 void sort(_Comp __comp); 1065 1066 void reverse() _NOEXCEPT; 1067 1068 bool __invariants() const; 1069 1070#if _LIBCPP_DEBUG_LEVEL >= 2 1071 1072 bool __dereferenceable(const const_iterator* __i) const; 1073 bool __decrementable(const const_iterator* __i) const; 1074 bool __addable(const const_iterator* __i, ptrdiff_t __n) const; 1075 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const; 1076 1077#endif // _LIBCPP_DEBUG_LEVEL >= 2 1078 1079private: 1080 _LIBCPP_INLINE_VISIBILITY 1081 static void __link_nodes (__link_pointer __p, __link_pointer __f, __link_pointer __l); 1082 _LIBCPP_INLINE_VISIBILITY 1083 void __link_nodes_at_front(__link_pointer __f, __link_pointer __l); 1084 _LIBCPP_INLINE_VISIBILITY 1085 void __link_nodes_at_back (__link_pointer __f, __link_pointer __l); 1086 iterator __iterator(size_type __n); 1087 template <class _Comp> 1088 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp); 1089 1090 void __move_assign(list& __c, true_type) 1091 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value); 1092 void __move_assign(list& __c, false_type); 1093}; 1094 1095// Link in nodes [__f, __l] just prior to __p 1096template <class _Tp, class _Alloc> 1097inline 1098void 1099list<_Tp, _Alloc>::__link_nodes(__link_pointer __p, __link_pointer __f, __link_pointer __l) 1100{ 1101 __p->__prev_->__next_ = __f; 1102 __f->__prev_ = __p->__prev_; 1103 __p->__prev_ = __l; 1104 __l->__next_ = __p; 1105} 1106 1107// Link in nodes [__f, __l] at the front of the list 1108template <class _Tp, class _Alloc> 1109inline 1110void 1111list<_Tp, _Alloc>::__link_nodes_at_front(__link_pointer __f, __link_pointer __l) 1112{ 1113 __f->__prev_ = base::__end_as_link(); 1114 __l->__next_ = base::__end_.__next_; 1115 __l->__next_->__prev_ = __l; 1116 base::__end_.__next_ = __f; 1117} 1118 1119// Link in nodes [__f, __l] at the front of the list 1120template <class _Tp, class _Alloc> 1121inline 1122void 1123list<_Tp, _Alloc>::__link_nodes_at_back(__link_pointer __f, __link_pointer __l) 1124{ 1125 __l->__next_ = base::__end_as_link(); 1126 __f->__prev_ = base::__end_.__prev_; 1127 __f->__prev_->__next_ = __f; 1128 base::__end_.__prev_ = __l; 1129} 1130 1131 1132template <class _Tp, class _Alloc> 1133inline 1134typename list<_Tp, _Alloc>::iterator 1135list<_Tp, _Alloc>::__iterator(size_type __n) 1136{ 1137 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n) 1138 : _VSTD::prev(end(), base::__sz() - __n); 1139} 1140 1141template <class _Tp, class _Alloc> 1142list<_Tp, _Alloc>::list(size_type __n) 1143{ 1144#if _LIBCPP_DEBUG_LEVEL >= 2 1145 __get_db()->__insert_c(this); 1146#endif 1147 for (; __n > 0; --__n) 1148#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1149 emplace_back(); 1150#else 1151 push_back(value_type()); 1152#endif 1153} 1154 1155#if _LIBCPP_STD_VER > 11 1156template <class _Tp, class _Alloc> 1157list<_Tp, _Alloc>::list(size_type __n, const allocator_type& __a) : base(__a) 1158{ 1159#if _LIBCPP_DEBUG_LEVEL >= 2 1160 __get_db()->__insert_c(this); 1161#endif 1162 for (; __n > 0; --__n) 1163#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1164 emplace_back(); 1165#else 1166 push_back(value_type()); 1167#endif 1168} 1169#endif 1170 1171template <class _Tp, class _Alloc> 1172list<_Tp, _Alloc>::list(size_type __n, const value_type& __x) 1173{ 1174#if _LIBCPP_DEBUG_LEVEL >= 2 1175 __get_db()->__insert_c(this); 1176#endif 1177 for (; __n > 0; --__n) 1178 push_back(__x); 1179} 1180 1181template <class _Tp, class _Alloc> 1182list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a) 1183 : base(__a) 1184{ 1185#if _LIBCPP_DEBUG_LEVEL >= 2 1186 __get_db()->__insert_c(this); 1187#endif 1188 for (; __n > 0; --__n) 1189 push_back(__x); 1190} 1191 1192template <class _Tp, class _Alloc> 1193template <class _InpIter> 1194list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, 1195 typename enable_if<__is_input_iterator<_InpIter>::value>::type*) 1196{ 1197#if _LIBCPP_DEBUG_LEVEL >= 2 1198 __get_db()->__insert_c(this); 1199#endif 1200 for (; __f != __l; ++__f) 1201 push_back(*__f); 1202} 1203 1204template <class _Tp, class _Alloc> 1205template <class _InpIter> 1206list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a, 1207 typename enable_if<__is_input_iterator<_InpIter>::value>::type*) 1208 : base(__a) 1209{ 1210#if _LIBCPP_DEBUG_LEVEL >= 2 1211 __get_db()->__insert_c(this); 1212#endif 1213 for (; __f != __l; ++__f) 1214 push_back(*__f); 1215} 1216 1217template <class _Tp, class _Alloc> 1218list<_Tp, _Alloc>::list(const list& __c) 1219 : base(allocator_type( 1220 __node_alloc_traits::select_on_container_copy_construction( 1221 __c.__node_alloc()))) 1222{ 1223#if _LIBCPP_DEBUG_LEVEL >= 2 1224 __get_db()->__insert_c(this); 1225#endif 1226 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i) 1227 push_back(*__i); 1228} 1229 1230template <class _Tp, class _Alloc> 1231list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a) 1232 : base(__a) 1233{ 1234#if _LIBCPP_DEBUG_LEVEL >= 2 1235 __get_db()->__insert_c(this); 1236#endif 1237 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i) 1238 push_back(*__i); 1239} 1240 1241#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 1242 1243template <class _Tp, class _Alloc> 1244list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a) 1245 : base(__a) 1246{ 1247#if _LIBCPP_DEBUG_LEVEL >= 2 1248 __get_db()->__insert_c(this); 1249#endif 1250 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), 1251 __e = __il.end(); __i != __e; ++__i) 1252 push_back(*__i); 1253} 1254 1255template <class _Tp, class _Alloc> 1256list<_Tp, _Alloc>::list(initializer_list<value_type> __il) 1257{ 1258#if _LIBCPP_DEBUG_LEVEL >= 2 1259 __get_db()->__insert_c(this); 1260#endif 1261 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(), 1262 __e = __il.end(); __i != __e; ++__i) 1263 push_back(*__i); 1264} 1265 1266#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 1267 1268template <class _Tp, class _Alloc> 1269inline 1270list<_Tp, _Alloc>& 1271list<_Tp, _Alloc>::operator=(const list& __c) 1272{ 1273 if (this != &__c) 1274 { 1275 base::__copy_assign_alloc(__c); 1276 assign(__c.begin(), __c.end()); 1277 } 1278 return *this; 1279} 1280 1281#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1282 1283template <class _Tp, class _Alloc> 1284inline 1285list<_Tp, _Alloc>::list(list&& __c) 1286 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value) 1287 : base(allocator_type(_VSTD::move(__c.__node_alloc()))) 1288{ 1289#if _LIBCPP_DEBUG_LEVEL >= 2 1290 __get_db()->__insert_c(this); 1291#endif 1292 splice(end(), __c); 1293} 1294 1295template <class _Tp, class _Alloc> 1296inline 1297list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a) 1298 : base(__a) 1299{ 1300#if _LIBCPP_DEBUG_LEVEL >= 2 1301 __get_db()->__insert_c(this); 1302#endif 1303 if (__a == __c.get_allocator()) 1304 splice(end(), __c); 1305 else 1306 { 1307 typedef move_iterator<iterator> _Ip; 1308 assign(_Ip(__c.begin()), _Ip(__c.end())); 1309 } 1310} 1311 1312template <class _Tp, class _Alloc> 1313inline 1314list<_Tp, _Alloc>& 1315list<_Tp, _Alloc>::operator=(list&& __c) 1316 _NOEXCEPT_( 1317 __node_alloc_traits::propagate_on_container_move_assignment::value && 1318 is_nothrow_move_assignable<__node_allocator>::value) 1319{ 1320 __move_assign(__c, integral_constant<bool, 1321 __node_alloc_traits::propagate_on_container_move_assignment::value>()); 1322 return *this; 1323} 1324 1325template <class _Tp, class _Alloc> 1326void 1327list<_Tp, _Alloc>::__move_assign(list& __c, false_type) 1328{ 1329 if (base::__node_alloc() != __c.__node_alloc()) 1330 { 1331 typedef move_iterator<iterator> _Ip; 1332 assign(_Ip(__c.begin()), _Ip(__c.end())); 1333 } 1334 else 1335 __move_assign(__c, true_type()); 1336} 1337 1338template <class _Tp, class _Alloc> 1339void 1340list<_Tp, _Alloc>::__move_assign(list& __c, true_type) 1341 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) 1342{ 1343 clear(); 1344 base::__move_assign_alloc(__c); 1345 splice(end(), __c); 1346} 1347 1348#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1349 1350template <class _Tp, class _Alloc> 1351template <class _InpIter> 1352void 1353list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l, 1354 typename enable_if<__is_input_iterator<_InpIter>::value>::type*) 1355{ 1356 iterator __i = begin(); 1357 iterator __e = end(); 1358 for (; __f != __l && __i != __e; ++__f, ++__i) 1359 *__i = *__f; 1360 if (__i == __e) 1361 insert(__e, __f, __l); 1362 else 1363 erase(__i, __e); 1364} 1365 1366template <class _Tp, class _Alloc> 1367void 1368list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x) 1369{ 1370 iterator __i = begin(); 1371 iterator __e = end(); 1372 for (; __n > 0 && __i != __e; --__n, ++__i) 1373 *__i = __x; 1374 if (__i == __e) 1375 insert(__e, __n, __x); 1376 else 1377 erase(__i, __e); 1378} 1379 1380template <class _Tp, class _Alloc> 1381inline 1382_Alloc 1383list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT 1384{ 1385 return allocator_type(base::__node_alloc()); 1386} 1387 1388template <class _Tp, class _Alloc> 1389typename list<_Tp, _Alloc>::iterator 1390list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x) 1391{ 1392#if _LIBCPP_DEBUG_LEVEL >= 2 1393 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1394 "list::insert(iterator, x) called with an iterator not" 1395 " referring to this list"); 1396#endif 1397 __node_allocator& __na = base::__node_alloc(); 1398 typedef __allocator_destructor<__node_allocator> _Dp; 1399 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1400 __hold->__prev_ = 0; 1401 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); 1402 __link_nodes(__p.__ptr_, __hold->__as_link(), __hold->__as_link()); 1403 ++base::__sz(); 1404#if _LIBCPP_DEBUG_LEVEL >= 2 1405 return iterator(__hold.release()->__as_link(), this); 1406#else 1407 return iterator(__hold.release()->__as_link()); 1408#endif 1409} 1410 1411template <class _Tp, class _Alloc> 1412typename list<_Tp, _Alloc>::iterator 1413list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x) 1414{ 1415#if _LIBCPP_DEBUG_LEVEL >= 2 1416 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1417 "list::insert(iterator, n, x) called with an iterator not" 1418 " referring to this list"); 1419 iterator __r(__p.__ptr_, this); 1420#else 1421 iterator __r(__p.__ptr_); 1422#endif 1423 if (__n > 0) 1424 { 1425 size_type __ds = 0; 1426 __node_allocator& __na = base::__node_alloc(); 1427 typedef __allocator_destructor<__node_allocator> _Dp; 1428 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1429 __hold->__prev_ = 0; 1430 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); 1431 ++__ds; 1432#if _LIBCPP_DEBUG_LEVEL >= 2 1433 __r = iterator(__hold->__as_link(), this); 1434#else 1435 __r = iterator(__hold->__as_link()); 1436#endif 1437 __hold.release(); 1438 iterator __e = __r; 1439#ifndef _LIBCPP_NO_EXCEPTIONS 1440 try 1441 { 1442#endif // _LIBCPP_NO_EXCEPTIONS 1443 for (--__n; __n != 0; --__n, ++__e, ++__ds) 1444 { 1445 __hold.reset(__node_alloc_traits::allocate(__na, 1)); 1446 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); 1447 __e.__ptr_->__next_ = __hold->__as_link(); 1448 __hold->__prev_ = __e.__ptr_; 1449 __hold.release(); 1450 } 1451#ifndef _LIBCPP_NO_EXCEPTIONS 1452 } 1453 catch (...) 1454 { 1455 while (true) 1456 { 1457 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e)); 1458 __link_pointer __prev = __e.__ptr_->__prev_; 1459 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1); 1460 if (__prev == 0) 1461 break; 1462#if _LIBCPP_DEBUG_LEVEL >= 2 1463 __e = iterator(__prev, this); 1464#else 1465 __e = iterator(__prev); 1466#endif 1467 } 1468 throw; 1469 } 1470#endif // _LIBCPP_NO_EXCEPTIONS 1471 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_); 1472 base::__sz() += __ds; 1473 } 1474 return __r; 1475} 1476 1477template <class _Tp, class _Alloc> 1478template <class _InpIter> 1479typename list<_Tp, _Alloc>::iterator 1480list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l, 1481 typename enable_if<__is_input_iterator<_InpIter>::value>::type*) 1482{ 1483#if _LIBCPP_DEBUG_LEVEL >= 2 1484 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1485 "list::insert(iterator, range) called with an iterator not" 1486 " referring to this list"); 1487 iterator __r(__p.__ptr_, this); 1488#else 1489 iterator __r(__p.__ptr_); 1490#endif 1491 if (__f != __l) 1492 { 1493 size_type __ds = 0; 1494 __node_allocator& __na = base::__node_alloc(); 1495 typedef __allocator_destructor<__node_allocator> _Dp; 1496 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1497 __hold->__prev_ = 0; 1498 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f); 1499 ++__ds; 1500#if _LIBCPP_DEBUG_LEVEL >= 2 1501 __r = iterator(__hold.get()->__as_link(), this); 1502#else 1503 __r = iterator(__hold.get()->__as_link()); 1504#endif 1505 __hold.release(); 1506 iterator __e = __r; 1507#ifndef _LIBCPP_NO_EXCEPTIONS 1508 try 1509 { 1510#endif // _LIBCPP_NO_EXCEPTIONS 1511 for (++__f; __f != __l; ++__f, (void) ++__e, (void) ++__ds) 1512 { 1513 __hold.reset(__node_alloc_traits::allocate(__na, 1)); 1514 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f); 1515 __e.__ptr_->__next_ = __hold.get()->__as_link(); 1516 __hold->__prev_ = __e.__ptr_; 1517 __hold.release(); 1518 } 1519#ifndef _LIBCPP_NO_EXCEPTIONS 1520 } 1521 catch (...) 1522 { 1523 while (true) 1524 { 1525 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e)); 1526 __link_pointer __prev = __e.__ptr_->__prev_; 1527 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1); 1528 if (__prev == 0) 1529 break; 1530#if _LIBCPP_DEBUG_LEVEL >= 2 1531 __e = iterator(__prev, this); 1532#else 1533 __e = iterator(__prev); 1534#endif 1535 } 1536 throw; 1537 } 1538#endif // _LIBCPP_NO_EXCEPTIONS 1539 __link_nodes(__p.__ptr_, __r.__ptr_, __e.__ptr_); 1540 base::__sz() += __ds; 1541 } 1542 return __r; 1543} 1544 1545template <class _Tp, class _Alloc> 1546void 1547list<_Tp, _Alloc>::push_front(const value_type& __x) 1548{ 1549 __node_allocator& __na = base::__node_alloc(); 1550 typedef __allocator_destructor<__node_allocator> _Dp; 1551 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1552 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); 1553 __link_pointer __nl = __hold->__as_link(); 1554 __link_nodes_at_front(__nl, __nl); 1555 ++base::__sz(); 1556 __hold.release(); 1557} 1558 1559template <class _Tp, class _Alloc> 1560void 1561list<_Tp, _Alloc>::push_back(const value_type& __x) 1562{ 1563 __node_allocator& __na = base::__node_alloc(); 1564 typedef __allocator_destructor<__node_allocator> _Dp; 1565 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1566 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); 1567 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link()); 1568 ++base::__sz(); 1569 __hold.release(); 1570} 1571 1572#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1573 1574template <class _Tp, class _Alloc> 1575void 1576list<_Tp, _Alloc>::push_front(value_type&& __x) 1577{ 1578 __node_allocator& __na = base::__node_alloc(); 1579 typedef __allocator_destructor<__node_allocator> _Dp; 1580 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1581 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x)); 1582 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link()); 1583 ++base::__sz(); 1584 __hold.release(); 1585} 1586 1587template <class _Tp, class _Alloc> 1588void 1589list<_Tp, _Alloc>::push_back(value_type&& __x) 1590{ 1591 __node_allocator& __na = base::__node_alloc(); 1592 typedef __allocator_destructor<__node_allocator> _Dp; 1593 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1594 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x)); 1595 __link_nodes_at_back(__hold.get()->__as_link(), __hold.get()->__as_link()); 1596 ++base::__sz(); 1597 __hold.release(); 1598} 1599 1600#ifndef _LIBCPP_HAS_NO_VARIADICS 1601 1602template <class _Tp, class _Alloc> 1603template <class... _Args> 1604typename list<_Tp, _Alloc>::reference 1605list<_Tp, _Alloc>::emplace_front(_Args&&... __args) 1606{ 1607 __node_allocator& __na = base::__node_alloc(); 1608 typedef __allocator_destructor<__node_allocator> _Dp; 1609 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1610 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...); 1611 __link_nodes_at_front(__hold.get()->__as_link(), __hold.get()->__as_link()); 1612 ++base::__sz(); 1613 return __hold.release()->__value_; 1614} 1615 1616template <class _Tp, class _Alloc> 1617template <class... _Args> 1618typename list<_Tp, _Alloc>::reference 1619list<_Tp, _Alloc>::emplace_back(_Args&&... __args) 1620{ 1621 __node_allocator& __na = base::__node_alloc(); 1622 typedef __allocator_destructor<__node_allocator> _Dp; 1623 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1624 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...); 1625 __link_pointer __nl = __hold->__as_link(); 1626 __link_nodes_at_back(__nl, __nl); 1627 ++base::__sz(); 1628 return __hold.release()->__value_; 1629} 1630 1631template <class _Tp, class _Alloc> 1632template <class... _Args> 1633typename list<_Tp, _Alloc>::iterator 1634list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args) 1635{ 1636#if _LIBCPP_DEBUG_LEVEL >= 2 1637 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1638 "list::emplace(iterator, args...) called with an iterator not" 1639 " referring to this list"); 1640#endif 1641 __node_allocator& __na = base::__node_alloc(); 1642 typedef __allocator_destructor<__node_allocator> _Dp; 1643 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1644 __hold->__prev_ = 0; 1645 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...); 1646 __link_pointer __nl = __hold.get()->__as_link(); 1647 __link_nodes(__p.__ptr_, __nl, __nl); 1648 ++base::__sz(); 1649 __hold.release(); 1650#if _LIBCPP_DEBUG_LEVEL >= 2 1651 return iterator(__nl, this); 1652#else 1653 return iterator(__nl); 1654#endif 1655} 1656 1657#endif // _LIBCPP_HAS_NO_VARIADICS 1658 1659template <class _Tp, class _Alloc> 1660typename list<_Tp, _Alloc>::iterator 1661list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x) 1662{ 1663#if _LIBCPP_DEBUG_LEVEL >= 2 1664 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1665 "list::insert(iterator, x) called with an iterator not" 1666 " referring to this list"); 1667#endif 1668 __node_allocator& __na = base::__node_alloc(); 1669 typedef __allocator_destructor<__node_allocator> _Dp; 1670 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1671 __hold->__prev_ = 0; 1672 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x)); 1673 __link_pointer __nl = __hold->__as_link(); 1674 __link_nodes(__p.__ptr_, __nl, __nl); 1675 ++base::__sz(); 1676 __hold.release(); 1677#if _LIBCPP_DEBUG_LEVEL >= 2 1678 return iterator(__nl, this); 1679#else 1680 return iterator(__nl); 1681#endif 1682} 1683 1684#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1685 1686template <class _Tp, class _Alloc> 1687void 1688list<_Tp, _Alloc>::pop_front() 1689{ 1690 _LIBCPP_ASSERT(!empty(), "list::pop_front() called with empty list"); 1691 __node_allocator& __na = base::__node_alloc(); 1692 __link_pointer __n = base::__end_.__next_; 1693 base::__unlink_nodes(__n, __n); 1694 --base::__sz(); 1695#if _LIBCPP_DEBUG_LEVEL >= 2 1696 __c_node* __c = __get_db()->__find_c_and_lock(this); 1697 for (__i_node** __p = __c->end_; __p != __c->beg_; ) 1698 { 1699 --__p; 1700 iterator* __i = static_cast<iterator*>((*__p)->__i_); 1701 if (__i->__ptr_ == __n) 1702 { 1703 (*__p)->__c_ = nullptr; 1704 if (--__c->end_ != __p) 1705 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); 1706 } 1707 } 1708 __get_db()->unlock(); 1709#endif 1710 __node_pointer __np = __n->__as_node(); 1711 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_)); 1712 __node_alloc_traits::deallocate(__na, __np, 1); 1713} 1714 1715template <class _Tp, class _Alloc> 1716void 1717list<_Tp, _Alloc>::pop_back() 1718{ 1719 _LIBCPP_ASSERT(!empty(), "list::pop_back() called with empty list"); 1720 __node_allocator& __na = base::__node_alloc(); 1721 __link_pointer __n = base::__end_.__prev_; 1722 base::__unlink_nodes(__n, __n); 1723 --base::__sz(); 1724#if _LIBCPP_DEBUG_LEVEL >= 2 1725 __c_node* __c = __get_db()->__find_c_and_lock(this); 1726 for (__i_node** __p = __c->end_; __p != __c->beg_; ) 1727 { 1728 --__p; 1729 iterator* __i = static_cast<iterator*>((*__p)->__i_); 1730 if (__i->__ptr_ == __n) 1731 { 1732 (*__p)->__c_ = nullptr; 1733 if (--__c->end_ != __p) 1734 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); 1735 } 1736 } 1737 __get_db()->unlock(); 1738#endif 1739 __node_pointer __np = __n->__as_node(); 1740 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_)); 1741 __node_alloc_traits::deallocate(__na, __np, 1); 1742} 1743 1744template <class _Tp, class _Alloc> 1745typename list<_Tp, _Alloc>::iterator 1746list<_Tp, _Alloc>::erase(const_iterator __p) 1747{ 1748#if _LIBCPP_DEBUG_LEVEL >= 2 1749 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1750 "list::erase(iterator) called with an iterator not" 1751 " referring to this list"); 1752#endif 1753 _LIBCPP_ASSERT(__p != end(), 1754 "list::erase(iterator) called with a non-dereferenceable iterator"); 1755 __node_allocator& __na = base::__node_alloc(); 1756 __link_pointer __n = __p.__ptr_; 1757 __link_pointer __r = __n->__next_; 1758 base::__unlink_nodes(__n, __n); 1759 --base::__sz(); 1760#if _LIBCPP_DEBUG_LEVEL >= 2 1761 __c_node* __c = __get_db()->__find_c_and_lock(this); 1762 for (__i_node** __ip = __c->end_; __ip != __c->beg_; ) 1763 { 1764 --__ip; 1765 iterator* __i = static_cast<iterator*>((*__ip)->__i_); 1766 if (__i->__ptr_ == __n) 1767 { 1768 (*__ip)->__c_ = nullptr; 1769 if (--__c->end_ != __ip) 1770 memmove(__ip, __ip+1, (__c->end_ - __ip)*sizeof(__i_node*)); 1771 } 1772 } 1773 __get_db()->unlock(); 1774#endif 1775 __node_pointer __np = __n->__as_node(); 1776 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_)); 1777 __node_alloc_traits::deallocate(__na, __np, 1); 1778#if _LIBCPP_DEBUG_LEVEL >= 2 1779 return iterator(__r, this); 1780#else 1781 return iterator(__r); 1782#endif 1783} 1784 1785template <class _Tp, class _Alloc> 1786typename list<_Tp, _Alloc>::iterator 1787list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l) 1788{ 1789#if _LIBCPP_DEBUG_LEVEL >= 2 1790 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == this, 1791 "list::erase(iterator, iterator) called with an iterator not" 1792 " referring to this list"); 1793#endif 1794 if (__f != __l) 1795 { 1796 __node_allocator& __na = base::__node_alloc(); 1797 base::__unlink_nodes(__f.__ptr_, __l.__ptr_->__prev_); 1798 while (__f != __l) 1799 { 1800 __link_pointer __n = __f.__ptr_; 1801 ++__f; 1802 --base::__sz(); 1803#if _LIBCPP_DEBUG_LEVEL >= 2 1804 __c_node* __c = __get_db()->__find_c_and_lock(this); 1805 for (__i_node** __p = __c->end_; __p != __c->beg_; ) 1806 { 1807 --__p; 1808 iterator* __i = static_cast<iterator*>((*__p)->__i_); 1809 if (__i->__ptr_ == __n) 1810 { 1811 (*__p)->__c_ = nullptr; 1812 if (--__c->end_ != __p) 1813 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); 1814 } 1815 } 1816 __get_db()->unlock(); 1817#endif 1818 __node_pointer __np = __n->__as_node(); 1819 __node_alloc_traits::destroy(__na, _VSTD::addressof(__np->__value_)); 1820 __node_alloc_traits::deallocate(__na, __np, 1); 1821 } 1822 } 1823#if _LIBCPP_DEBUG_LEVEL >= 2 1824 return iterator(__l.__ptr_, this); 1825#else 1826 return iterator(__l.__ptr_); 1827#endif 1828} 1829 1830template <class _Tp, class _Alloc> 1831void 1832list<_Tp, _Alloc>::resize(size_type __n) 1833{ 1834 if (__n < base::__sz()) 1835 erase(__iterator(__n), end()); 1836 else if (__n > base::__sz()) 1837 { 1838 __n -= base::__sz(); 1839 size_type __ds = 0; 1840 __node_allocator& __na = base::__node_alloc(); 1841 typedef __allocator_destructor<__node_allocator> _Dp; 1842 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1843 __hold->__prev_ = 0; 1844 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_)); 1845 ++__ds; 1846#if _LIBCPP_DEBUG_LEVEL >= 2 1847 iterator __r = iterator(__hold.release()->__as_link(), this); 1848#else 1849 iterator __r = iterator(__hold.release()->__as_link()); 1850#endif 1851 iterator __e = __r; 1852#ifndef _LIBCPP_NO_EXCEPTIONS 1853 try 1854 { 1855#endif // _LIBCPP_NO_EXCEPTIONS 1856 for (--__n; __n != 0; --__n, ++__e, ++__ds) 1857 { 1858 __hold.reset(__node_alloc_traits::allocate(__na, 1)); 1859 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_)); 1860 __e.__ptr_->__next_ = __hold.get()->__as_link(); 1861 __hold->__prev_ = __e.__ptr_; 1862 __hold.release(); 1863 } 1864#ifndef _LIBCPP_NO_EXCEPTIONS 1865 } 1866 catch (...) 1867 { 1868 while (true) 1869 { 1870 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e)); 1871 __link_pointer __prev = __e.__ptr_->__prev_; 1872 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1); 1873 if (__prev == 0) 1874 break; 1875#if _LIBCPP_DEBUG_LEVEL >= 2 1876 __e = iterator(__prev, this); 1877#else 1878 __e = iterator(__prev); 1879#endif 1880 } 1881 throw; 1882 } 1883#endif // _LIBCPP_NO_EXCEPTIONS 1884 __link_nodes_at_back(__r.__ptr_, __e.__ptr_); 1885 base::__sz() += __ds; 1886 } 1887} 1888 1889template <class _Tp, class _Alloc> 1890void 1891list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x) 1892{ 1893 if (__n < base::__sz()) 1894 erase(__iterator(__n), end()); 1895 else if (__n > base::__sz()) 1896 { 1897 __n -= base::__sz(); 1898 size_type __ds = 0; 1899 __node_allocator& __na = base::__node_alloc(); 1900 typedef __allocator_destructor<__node_allocator> _Dp; 1901 unique_ptr<__node, _Dp> __hold(__node_alloc_traits::allocate(__na, 1), _Dp(__na, 1)); 1902 __hold->__prev_ = 0; 1903 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); 1904 ++__ds; 1905 __link_pointer __nl = __hold.release()->__as_link(); 1906#if _LIBCPP_DEBUG_LEVEL >= 2 1907 iterator __r = iterator(__nl, this); 1908#else 1909 iterator __r = iterator(__nl); 1910#endif 1911 iterator __e = __r; 1912#ifndef _LIBCPP_NO_EXCEPTIONS 1913 try 1914 { 1915#endif // _LIBCPP_NO_EXCEPTIONS 1916 for (--__n; __n != 0; --__n, ++__e, ++__ds) 1917 { 1918 __hold.reset(__node_alloc_traits::allocate(__na, 1)); 1919 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x); 1920 __e.__ptr_->__next_ = __hold.get()->__as_link(); 1921 __hold->__prev_ = __e.__ptr_; 1922 __hold.release(); 1923 } 1924#ifndef _LIBCPP_NO_EXCEPTIONS 1925 } 1926 catch (...) 1927 { 1928 while (true) 1929 { 1930 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e)); 1931 __link_pointer __prev = __e.__ptr_->__prev_; 1932 __node_alloc_traits::deallocate(__na, __e.__ptr_->__as_node(), 1); 1933 if (__prev == 0) 1934 break; 1935#if _LIBCPP_DEBUG_LEVEL >= 2 1936 __e = iterator(__prev, this); 1937#else 1938 __e = iterator(__prev); 1939#endif 1940 } 1941 throw; 1942 } 1943#endif // _LIBCPP_NO_EXCEPTIONS 1944 __link_nodes(base::__end_as_link(), __r.__ptr_, __e.__ptr_); 1945 base::__sz() += __ds; 1946 } 1947} 1948 1949template <class _Tp, class _Alloc> 1950void 1951list<_Tp, _Alloc>::splice(const_iterator __p, list& __c) 1952{ 1953 _LIBCPP_ASSERT(this != &__c, 1954 "list::splice(iterator, list) called with this == &list"); 1955#if _LIBCPP_DEBUG_LEVEL >= 2 1956 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1957 "list::splice(iterator, list) called with an iterator not" 1958 " referring to this list"); 1959#endif 1960 if (!__c.empty()) 1961 { 1962 __link_pointer __f = __c.__end_.__next_; 1963 __link_pointer __l = __c.__end_.__prev_; 1964 base::__unlink_nodes(__f, __l); 1965 __link_nodes(__p.__ptr_, __f, __l); 1966 base::__sz() += __c.__sz(); 1967 __c.__sz() = 0; 1968#if _LIBCPP_DEBUG_LEVEL >= 2 1969 __libcpp_db* __db = __get_db(); 1970 __c_node* __cn1 = __db->__find_c_and_lock(this); 1971 __c_node* __cn2 = __db->__find_c(&__c); 1972 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;) 1973 { 1974 --__ip; 1975 iterator* __i = static_cast<iterator*>((*__ip)->__i_); 1976 if (__i->__ptr_ != __c.__end_as_link()) 1977 { 1978 __cn1->__add(*__ip); 1979 (*__ip)->__c_ = __cn1; 1980 if (--__cn2->end_ != __ip) 1981 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*)); 1982 } 1983 } 1984 __db->unlock(); 1985#endif 1986 } 1987} 1988 1989template <class _Tp, class _Alloc> 1990void 1991list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i) 1992{ 1993#if _LIBCPP_DEBUG_LEVEL >= 2 1994 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 1995 "list::splice(iterator, list, iterator) called with first iterator not" 1996 " referring to this list"); 1997 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__i) == &__c, 1998 "list::splice(iterator, list, iterator) called with second iterator not" 1999 " referring to list argument"); 2000 _LIBCPP_ASSERT(__get_const_db()->__dereferenceable(&__i), 2001 "list::splice(iterator, list, iterator) called with second iterator not" 2002 " derefereceable"); 2003#endif 2004 if (__p.__ptr_ != __i.__ptr_ && __p.__ptr_ != __i.__ptr_->__next_) 2005 { 2006 __link_pointer __f = __i.__ptr_; 2007 base::__unlink_nodes(__f, __f); 2008 __link_nodes(__p.__ptr_, __f, __f); 2009 --__c.__sz(); 2010 ++base::__sz(); 2011#if _LIBCPP_DEBUG_LEVEL >= 2 2012 __libcpp_db* __db = __get_db(); 2013 __c_node* __cn1 = __db->__find_c_and_lock(this); 2014 __c_node* __cn2 = __db->__find_c(&__c); 2015 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;) 2016 { 2017 --__ip; 2018 iterator* __j = static_cast<iterator*>((*__ip)->__i_); 2019 if (__j->__ptr_ == __f) 2020 { 2021 __cn1->__add(*__ip); 2022 (*__ip)->__c_ = __cn1; 2023 if (--__cn2->end_ != __ip) 2024 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*)); 2025 } 2026 } 2027 __db->unlock(); 2028#endif 2029 } 2030} 2031 2032template <class _Tp, class _Alloc> 2033void 2034list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l) 2035{ 2036#if _LIBCPP_DEBUG_LEVEL >= 2 2037 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this, 2038 "list::splice(iterator, list, iterator, iterator) called with first iterator not" 2039 " referring to this list"); 2040 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__f) == &__c, 2041 "list::splice(iterator, list, iterator, iterator) called with second iterator not" 2042 " referring to list argument"); 2043 if (this == &__c) 2044 { 2045 for (const_iterator __i = __f; __i != __l; ++__i) 2046 _LIBCPP_ASSERT(__i != __p, 2047 "list::splice(iterator, list, iterator, iterator)" 2048 " called with the first iterator within the range" 2049 " of the second and third iterators"); 2050 } 2051#endif 2052 if (__f != __l) 2053 { 2054 if (this != &__c) 2055 { 2056 size_type __s = _VSTD::distance(__f, __l); 2057 __c.__sz() -= __s; 2058 base::__sz() += __s; 2059 } 2060 __link_pointer __first = __f.__ptr_; 2061 --__l; 2062 __link_pointer __last = __l.__ptr_; 2063 base::__unlink_nodes(__first, __last); 2064 __link_nodes(__p.__ptr_, __first, __last); 2065#if _LIBCPP_DEBUG_LEVEL >= 2 2066 __libcpp_db* __db = __get_db(); 2067 __c_node* __cn1 = __db->__find_c_and_lock(this); 2068 __c_node* __cn2 = __db->__find_c(&__c); 2069 for (__i_node** __ip = __cn2->end_; __ip != __cn2->beg_;) 2070 { 2071 --__ip; 2072 iterator* __j = static_cast<iterator*>((*__ip)->__i_); 2073 for (__link_pointer __k = __f.__ptr_; 2074 __k != __l.__ptr_; __k = __k->__next_) 2075 { 2076 if (__j->__ptr_ == __k) 2077 { 2078 __cn1->__add(*__ip); 2079 (*__ip)->__c_ = __cn1; 2080 if (--__cn2->end_ != __ip) 2081 memmove(__ip, __ip+1, (__cn2->end_ - __ip)*sizeof(__i_node*)); 2082 } 2083 } 2084 } 2085 __db->unlock(); 2086#endif 2087 } 2088} 2089 2090template <class _Tp, class _Alloc> 2091void 2092list<_Tp, _Alloc>::remove(const value_type& __x) 2093{ 2094 list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing 2095 for (const_iterator __i = begin(), __e = end(); __i != __e;) 2096 { 2097 if (*__i == __x) 2098 { 2099 const_iterator __j = _VSTD::next(__i); 2100 for (; __j != __e && *__j == __x; ++__j) 2101 ; 2102 __deleted_nodes.splice(__deleted_nodes.end(), *this, __i, __j); 2103 __i = __j; 2104 if (__i != __e) 2105 ++__i; 2106 } 2107 else 2108 ++__i; 2109 } 2110} 2111 2112template <class _Tp, class _Alloc> 2113template <class _Pred> 2114void 2115list<_Tp, _Alloc>::remove_if(_Pred __pred) 2116{ 2117 for (iterator __i = begin(), __e = end(); __i != __e;) 2118 { 2119 if (__pred(*__i)) 2120 { 2121 iterator __j = _VSTD::next(__i); 2122 for (; __j != __e && __pred(*__j); ++__j) 2123 ; 2124 __i = erase(__i, __j); 2125 if (__i != __e) 2126 ++__i; 2127 } 2128 else 2129 ++__i; 2130 } 2131} 2132 2133template <class _Tp, class _Alloc> 2134inline 2135void 2136list<_Tp, _Alloc>::unique() 2137{ 2138 unique(__equal_to<value_type>()); 2139} 2140 2141template <class _Tp, class _Alloc> 2142template <class _BinaryPred> 2143void 2144list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred) 2145{ 2146 for (iterator __i = begin(), __e = end(); __i != __e;) 2147 { 2148 iterator __j = _VSTD::next(__i); 2149 for (; __j != __e && __binary_pred(*__i, *__j); ++__j) 2150 ; 2151 if (++__i != __j) 2152 __i = erase(__i, __j); 2153 } 2154} 2155 2156template <class _Tp, class _Alloc> 2157inline 2158void 2159list<_Tp, _Alloc>::merge(list& __c) 2160{ 2161 merge(__c, __less<value_type>()); 2162} 2163 2164template <class _Tp, class _Alloc> 2165template <class _Comp> 2166void 2167list<_Tp, _Alloc>::merge(list& __c, _Comp __comp) 2168{ 2169 if (this != &__c) 2170 { 2171 iterator __f1 = begin(); 2172 iterator __e1 = end(); 2173 iterator __f2 = __c.begin(); 2174 iterator __e2 = __c.end(); 2175 while (__f1 != __e1 && __f2 != __e2) 2176 { 2177 if (__comp(*__f2, *__f1)) 2178 { 2179 size_type __ds = 1; 2180 iterator __m2 = _VSTD::next(__f2); 2181 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds) 2182 ; 2183 base::__sz() += __ds; 2184 __c.__sz() -= __ds; 2185 __link_pointer __f = __f2.__ptr_; 2186 __link_pointer __l = __m2.__ptr_->__prev_; 2187 __f2 = __m2; 2188 base::__unlink_nodes(__f, __l); 2189 __m2 = _VSTD::next(__f1); 2190 __link_nodes(__f1.__ptr_, __f, __l); 2191 __f1 = __m2; 2192 } 2193 else 2194 ++__f1; 2195 } 2196 splice(__e1, __c); 2197#if _LIBCPP_DEBUG_LEVEL >= 2 2198 __libcpp_db* __db = __get_db(); 2199 __c_node* __cn1 = __db->__find_c_and_lock(this); 2200 __c_node* __cn2 = __db->__find_c(&__c); 2201 for (__i_node** __p = __cn2->end_; __p != __cn2->beg_;) 2202 { 2203 --__p; 2204 iterator* __i = static_cast<iterator*>((*__p)->__i_); 2205 if (__i->__ptr_ != __c.__end_as_link()) 2206 { 2207 __cn1->__add(*__p); 2208 (*__p)->__c_ = __cn1; 2209 if (--__cn2->end_ != __p) 2210 memmove(__p, __p+1, (__cn2->end_ - __p)*sizeof(__i_node*)); 2211 } 2212 } 2213 __db->unlock(); 2214#endif 2215 } 2216} 2217 2218template <class _Tp, class _Alloc> 2219inline 2220void 2221list<_Tp, _Alloc>::sort() 2222{ 2223 sort(__less<value_type>()); 2224} 2225 2226template <class _Tp, class _Alloc> 2227template <class _Comp> 2228inline 2229void 2230list<_Tp, _Alloc>::sort(_Comp __comp) 2231{ 2232 __sort(begin(), end(), base::__sz(), __comp); 2233} 2234 2235template <class _Tp, class _Alloc> 2236template <class _Comp> 2237typename list<_Tp, _Alloc>::iterator 2238list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp) 2239{ 2240 switch (__n) 2241 { 2242 case 0: 2243 case 1: 2244 return __f1; 2245 case 2: 2246 if (__comp(*--__e2, *__f1)) 2247 { 2248 __link_pointer __f = __e2.__ptr_; 2249 base::__unlink_nodes(__f, __f); 2250 __link_nodes(__f1.__ptr_, __f, __f); 2251 return __e2; 2252 } 2253 return __f1; 2254 } 2255 size_type __n2 = __n / 2; 2256 iterator __e1 = _VSTD::next(__f1, __n2); 2257 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp); 2258 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp); 2259 if (__comp(*__f2, *__f1)) 2260 { 2261 iterator __m2 = _VSTD::next(__f2); 2262 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2) 2263 ; 2264 __link_pointer __f = __f2.__ptr_; 2265 __link_pointer __l = __m2.__ptr_->__prev_; 2266 __r = __f2; 2267 __e1 = __f2 = __m2; 2268 base::__unlink_nodes(__f, __l); 2269 __m2 = _VSTD::next(__f1); 2270 __link_nodes(__f1.__ptr_, __f, __l); 2271 __f1 = __m2; 2272 } 2273 else 2274 ++__f1; 2275 while (__f1 != __e1 && __f2 != __e2) 2276 { 2277 if (__comp(*__f2, *__f1)) 2278 { 2279 iterator __m2 = _VSTD::next(__f2); 2280 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2) 2281 ; 2282 __link_pointer __f = __f2.__ptr_; 2283 __link_pointer __l = __m2.__ptr_->__prev_; 2284 if (__e1 == __f2) 2285 __e1 = __m2; 2286 __f2 = __m2; 2287 base::__unlink_nodes(__f, __l); 2288 __m2 = _VSTD::next(__f1); 2289 __link_nodes(__f1.__ptr_, __f, __l); 2290 __f1 = __m2; 2291 } 2292 else 2293 ++__f1; 2294 } 2295 return __r; 2296} 2297 2298template <class _Tp, class _Alloc> 2299void 2300list<_Tp, _Alloc>::reverse() _NOEXCEPT 2301{ 2302 if (base::__sz() > 1) 2303 { 2304 iterator __e = end(); 2305 for (iterator __i = begin(); __i.__ptr_ != __e.__ptr_;) 2306 { 2307 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_); 2308 __i.__ptr_ = __i.__ptr_->__prev_; 2309 } 2310 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_); 2311 } 2312} 2313 2314template <class _Tp, class _Alloc> 2315bool 2316list<_Tp, _Alloc>::__invariants() const 2317{ 2318 return size() == _VSTD::distance(begin(), end()); 2319} 2320 2321#if _LIBCPP_DEBUG_LEVEL >= 2 2322 2323template <class _Tp, class _Alloc> 2324bool 2325list<_Tp, _Alloc>::__dereferenceable(const const_iterator* __i) const 2326{ 2327 return __i->__ptr_ != this->__end_as_link(); 2328} 2329 2330template <class _Tp, class _Alloc> 2331bool 2332list<_Tp, _Alloc>::__decrementable(const const_iterator* __i) const 2333{ 2334 return !empty() && __i->__ptr_ != base::__end_.__next_; 2335} 2336 2337template <class _Tp, class _Alloc> 2338bool 2339list<_Tp, _Alloc>::__addable(const const_iterator* __i, ptrdiff_t __n) const 2340{ 2341 return false; 2342} 2343 2344template <class _Tp, class _Alloc> 2345bool 2346list<_Tp, _Alloc>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const 2347{ 2348 return false; 2349} 2350 2351#endif // _LIBCPP_DEBUG_LEVEL >= 2 2352 2353template <class _Tp, class _Alloc> 2354inline _LIBCPP_INLINE_VISIBILITY 2355bool 2356operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 2357{ 2358 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin()); 2359} 2360 2361template <class _Tp, class _Alloc> 2362inline _LIBCPP_INLINE_VISIBILITY 2363bool 2364operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 2365{ 2366 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); 2367} 2368 2369template <class _Tp, class _Alloc> 2370inline _LIBCPP_INLINE_VISIBILITY 2371bool 2372operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 2373{ 2374 return !(__x == __y); 2375} 2376 2377template <class _Tp, class _Alloc> 2378inline _LIBCPP_INLINE_VISIBILITY 2379bool 2380operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 2381{ 2382 return __y < __x; 2383} 2384 2385template <class _Tp, class _Alloc> 2386inline _LIBCPP_INLINE_VISIBILITY 2387bool 2388operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 2389{ 2390 return !(__x < __y); 2391} 2392 2393template <class _Tp, class _Alloc> 2394inline _LIBCPP_INLINE_VISIBILITY 2395bool 2396operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y) 2397{ 2398 return !(__y < __x); 2399} 2400 2401template <class _Tp, class _Alloc> 2402inline _LIBCPP_INLINE_VISIBILITY 2403void 2404swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y) 2405 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 2406{ 2407 __x.swap(__y); 2408} 2409 2410_LIBCPP_END_NAMESPACE_STD 2411 2412#endif // _LIBCPP_LIST 2413