1// -*- C++ -*- 2//===------------------------------ vector --------------------------------===// 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_VECTOR 12#define _LIBCPP_VECTOR 13 14/* 15 vector synopsis 16 17namespace std 18{ 19 20template <class T, class Allocator = allocator<T> > 21class vector 22{ 23public: 24 typedef T value_type; 25 typedef Allocator allocator_type; 26 typedef typename allocator_type::reference reference; 27 typedef typename allocator_type::const_reference const_reference; 28 typedef implementation-defined iterator; 29 typedef implementation-defined const_iterator; 30 typedef typename allocator_type::size_type size_type; 31 typedef typename allocator_type::difference_type difference_type; 32 typedef typename allocator_type::pointer pointer; 33 typedef typename allocator_type::const_pointer const_pointer; 34 typedef std::reverse_iterator<iterator> reverse_iterator; 35 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 36 37 vector() 38 noexcept(is_nothrow_default_constructible<allocator_type>::value); 39 explicit vector(const allocator_type&); 40 explicit vector(size_type n); 41 explicit vector(size_type n, const allocator_type&); // C++14 42 vector(size_type n, const value_type& value, const allocator_type& = allocator_type()); 43 template <class InputIterator> 44 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type()); 45 vector(const vector& x); 46 vector(vector&& x) 47 noexcept(is_nothrow_move_constructible<allocator_type>::value); 48 vector(initializer_list<value_type> il); 49 vector(initializer_list<value_type> il, const allocator_type& a); 50 ~vector(); 51 vector& operator=(const vector& x); 52 vector& operator=(vector&& x) 53 noexcept( 54 allocator_type::propagate_on_container_move_assignment::value || 55 allocator_type::is_always_equal::value); // C++17 56 vector& operator=(initializer_list<value_type> il); 57 template <class InputIterator> 58 void assign(InputIterator first, InputIterator last); 59 void assign(size_type n, const value_type& u); 60 void assign(initializer_list<value_type> il); 61 62 allocator_type get_allocator() const noexcept; 63 64 iterator begin() noexcept; 65 const_iterator begin() const noexcept; 66 iterator end() noexcept; 67 const_iterator end() const noexcept; 68 69 reverse_iterator rbegin() noexcept; 70 const_reverse_iterator rbegin() const noexcept; 71 reverse_iterator rend() noexcept; 72 const_reverse_iterator rend() const noexcept; 73 74 const_iterator cbegin() const noexcept; 75 const_iterator cend() const noexcept; 76 const_reverse_iterator crbegin() const noexcept; 77 const_reverse_iterator crend() const noexcept; 78 79 size_type size() const noexcept; 80 size_type max_size() const noexcept; 81 size_type capacity() const noexcept; 82 bool empty() const noexcept; 83 void reserve(size_type n); 84 void shrink_to_fit() noexcept; 85 86 reference operator[](size_type n); 87 const_reference operator[](size_type n) const; 88 reference at(size_type n); 89 const_reference at(size_type n) const; 90 91 reference front(); 92 const_reference front() const; 93 reference back(); 94 const_reference back() const; 95 96 value_type* data() noexcept; 97 const value_type* data() const noexcept; 98 99 void push_back(const value_type& x); 100 void push_back(value_type&& x); 101 template <class... Args> 102 reference emplace_back(Args&&... args); 103 void pop_back(); 104 105 template <class... Args> iterator emplace(const_iterator position, Args&&... args); 106 iterator insert(const_iterator position, const value_type& x); 107 iterator insert(const_iterator position, value_type&& x); 108 iterator insert(const_iterator position, size_type n, const value_type& x); 109 template <class InputIterator> 110 iterator insert(const_iterator position, InputIterator first, InputIterator last); 111 iterator insert(const_iterator position, initializer_list<value_type> il); 112 113 iterator erase(const_iterator position); 114 iterator erase(const_iterator first, const_iterator last); 115 116 void clear() noexcept; 117 118 void resize(size_type sz); 119 void resize(size_type sz, const value_type& c); 120 121 void swap(vector&) 122 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value || 123 allocator_traits<allocator_type>::is_always_equal::value); // C++17 124 125 bool __invariants() const; 126}; 127 128template <class Allocator = allocator<T> > 129class vector<bool, Allocator> 130{ 131public: 132 typedef bool value_type; 133 typedef Allocator allocator_type; 134 typedef implementation-defined iterator; 135 typedef implementation-defined const_iterator; 136 typedef typename allocator_type::size_type size_type; 137 typedef typename allocator_type::difference_type difference_type; 138 typedef iterator pointer; 139 typedef const_iterator const_pointer; 140 typedef std::reverse_iterator<iterator> reverse_iterator; 141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 142 143 class reference 144 { 145 public: 146 reference(const reference&) noexcept; 147 operator bool() const noexcept; 148 reference& operator=(const bool x) noexcept; 149 reference& operator=(const reference& x) noexcept; 150 iterator operator&() const noexcept; 151 void flip() noexcept; 152 }; 153 154 class const_reference 155 { 156 public: 157 const_reference(const reference&) noexcept; 158 operator bool() const noexcept; 159 const_iterator operator&() const noexcept; 160 }; 161 162 vector() 163 noexcept(is_nothrow_default_constructible<allocator_type>::value); 164 explicit vector(const allocator_type&); 165 explicit vector(size_type n, const allocator_type& a = allocator_type()); // C++14 166 vector(size_type n, const value_type& value, const allocator_type& = allocator_type()); 167 template <class InputIterator> 168 vector(InputIterator first, InputIterator last, const allocator_type& = allocator_type()); 169 vector(const vector& x); 170 vector(vector&& x) 171 noexcept(is_nothrow_move_constructible<allocator_type>::value); 172 vector(initializer_list<value_type> il); 173 vector(initializer_list<value_type> il, const allocator_type& a); 174 ~vector(); 175 vector& operator=(const vector& x); 176 vector& operator=(vector&& x) 177 noexcept( 178 allocator_type::propagate_on_container_move_assignment::value || 179 allocator_type::is_always_equal::value); // C++17 180 vector& operator=(initializer_list<value_type> il); 181 template <class InputIterator> 182 void assign(InputIterator first, InputIterator last); 183 void assign(size_type n, const value_type& u); 184 void assign(initializer_list<value_type> il); 185 186 allocator_type get_allocator() const noexcept; 187 188 iterator begin() noexcept; 189 const_iterator begin() const noexcept; 190 iterator end() noexcept; 191 const_iterator end() const noexcept; 192 193 reverse_iterator rbegin() noexcept; 194 const_reverse_iterator rbegin() const noexcept; 195 reverse_iterator rend() noexcept; 196 const_reverse_iterator rend() const noexcept; 197 198 const_iterator cbegin() const noexcept; 199 const_iterator cend() const noexcept; 200 const_reverse_iterator crbegin() const noexcept; 201 const_reverse_iterator crend() const noexcept; 202 203 size_type size() const noexcept; 204 size_type max_size() const noexcept; 205 size_type capacity() const noexcept; 206 bool empty() const noexcept; 207 void reserve(size_type n); 208 void shrink_to_fit() noexcept; 209 210 reference operator[](size_type n); 211 const_reference operator[](size_type n) const; 212 reference at(size_type n); 213 const_reference at(size_type n) const; 214 215 reference front(); 216 const_reference front() const; 217 reference back(); 218 const_reference back() const; 219 220 void push_back(const value_type& x); 221 template <class... Args> reference emplace_back(Args&&... args); // C++14 222 void pop_back(); 223 224 template <class... Args> iterator emplace(const_iterator position, Args&&... args); // C++14 225 iterator insert(const_iterator position, const value_type& x); 226 iterator insert(const_iterator position, size_type n, const value_type& x); 227 template <class InputIterator> 228 iterator insert(const_iterator position, InputIterator first, InputIterator last); 229 iterator insert(const_iterator position, initializer_list<value_type> il); 230 231 iterator erase(const_iterator position); 232 iterator erase(const_iterator first, const_iterator last); 233 234 void clear() noexcept; 235 236 void resize(size_type sz); 237 void resize(size_type sz, value_type x); 238 239 void swap(vector&) 240 noexcept(allocator_traits<allocator_type>::propagate_on_container_swap::value || 241 allocator_traits<allocator_type>::is_always_equal::value); // C++17 242 void flip() noexcept; 243 244 bool __invariants() const; 245}; 246 247template <class Allocator> struct hash<std::vector<bool, Allocator>>; 248 249template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y); 250template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y); 251template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); 252template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y); 253template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); 254template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y); 255 256template <class T, class Allocator> 257void swap(vector<T,Allocator>& x, vector<T,Allocator>& y) 258 noexcept(noexcept(x.swap(y))); 259 260} // std 261 262*/ 263 264#include <__config> 265#include <iosfwd> // for forward declaration of vector 266#include <__bit_reference> 267#include <type_traits> 268#include <climits> 269#include <limits> 270#include <initializer_list> 271#include <memory> 272#include <stdexcept> 273#include <algorithm> 274#include <cstring> 275#include <__split_buffer> 276#include <__functional_base> 277 278#include <__undef_min_max> 279 280#include <__debug> 281 282#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 283#pragma GCC system_header 284#endif 285 286_LIBCPP_BEGIN_NAMESPACE_STD 287 288template <bool> 289class __vector_base_common 290{ 291protected: 292 _LIBCPP_ALWAYS_INLINE __vector_base_common() {} 293 _LIBCPP_NORETURN void __throw_length_error() const; 294 _LIBCPP_NORETURN void __throw_out_of_range() const; 295}; 296 297template <bool __b> 298void 299__vector_base_common<__b>::__throw_length_error() const 300{ 301 _VSTD::__throw_length_error("vector"); 302} 303 304template <bool __b> 305void 306__vector_base_common<__b>::__throw_out_of_range() const 307{ 308 _VSTD::__throw_out_of_range("vector"); 309} 310 311#ifdef _LIBCPP_MSVC 312#pragma warning( push ) 313#pragma warning( disable: 4231 ) 314#endif // _LIBCPP_MSVC 315_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS __vector_base_common<true>) 316#ifdef _LIBCPP_MSVC 317#pragma warning( pop ) 318#endif // _LIBCPP_MSVC 319 320template <class _Tp, class _Allocator> 321class __vector_base 322 : protected __vector_base_common<true> 323{ 324protected: 325 typedef _Tp value_type; 326 typedef _Allocator allocator_type; 327 typedef allocator_traits<allocator_type> __alloc_traits; 328 typedef value_type& reference; 329 typedef const value_type& const_reference; 330 typedef typename __alloc_traits::size_type size_type; 331 typedef typename __alloc_traits::difference_type difference_type; 332 typedef typename __alloc_traits::pointer pointer; 333 typedef typename __alloc_traits::const_pointer const_pointer; 334 typedef pointer iterator; 335 typedef const_pointer const_iterator; 336 337 pointer __begin_; 338 pointer __end_; 339 __compressed_pair<pointer, allocator_type> __end_cap_; 340 341 _LIBCPP_INLINE_VISIBILITY 342 allocator_type& __alloc() _NOEXCEPT 343 {return __end_cap_.second();} 344 _LIBCPP_INLINE_VISIBILITY 345 const allocator_type& __alloc() const _NOEXCEPT 346 {return __end_cap_.second();} 347 _LIBCPP_INLINE_VISIBILITY 348 pointer& __end_cap() _NOEXCEPT 349 {return __end_cap_.first();} 350 _LIBCPP_INLINE_VISIBILITY 351 const pointer& __end_cap() const _NOEXCEPT 352 {return __end_cap_.first();} 353 354 _LIBCPP_INLINE_VISIBILITY 355 __vector_base() 356 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); 357 _LIBCPP_INLINE_VISIBILITY __vector_base(const allocator_type& __a); 358 ~__vector_base(); 359 360 _LIBCPP_INLINE_VISIBILITY 361 void clear() _NOEXCEPT {__destruct_at_end(__begin_);} 362 _LIBCPP_INLINE_VISIBILITY 363 size_type capacity() const _NOEXCEPT 364 {return static_cast<size_type>(__end_cap() - __begin_);} 365 366 _LIBCPP_INLINE_VISIBILITY 367 void __destruct_at_end(pointer __new_last) _NOEXCEPT; 368 369 _LIBCPP_INLINE_VISIBILITY 370 void __copy_assign_alloc(const __vector_base& __c) 371 {__copy_assign_alloc(__c, integral_constant<bool, 372 __alloc_traits::propagate_on_container_copy_assignment::value>());} 373 374 _LIBCPP_INLINE_VISIBILITY 375 void __move_assign_alloc(__vector_base& __c) 376 _NOEXCEPT_( 377 !__alloc_traits::propagate_on_container_move_assignment::value || 378 is_nothrow_move_assignable<allocator_type>::value) 379 {__move_assign_alloc(__c, integral_constant<bool, 380 __alloc_traits::propagate_on_container_move_assignment::value>());} 381private: 382 _LIBCPP_INLINE_VISIBILITY 383 void __copy_assign_alloc(const __vector_base& __c, true_type) 384 { 385 if (__alloc() != __c.__alloc()) 386 { 387 clear(); 388 __alloc_traits::deallocate(__alloc(), __begin_, capacity()); 389 __begin_ = __end_ = __end_cap() = nullptr; 390 } 391 __alloc() = __c.__alloc(); 392 } 393 394 _LIBCPP_INLINE_VISIBILITY 395 void __copy_assign_alloc(const __vector_base&, false_type) 396 {} 397 398 _LIBCPP_INLINE_VISIBILITY 399 void __move_assign_alloc(__vector_base& __c, true_type) 400 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) 401 { 402 __alloc() = _VSTD::move(__c.__alloc()); 403 } 404 405 _LIBCPP_INLINE_VISIBILITY 406 void __move_assign_alloc(__vector_base&, false_type) 407 _NOEXCEPT 408 {} 409}; 410 411template <class _Tp, class _Allocator> 412inline _LIBCPP_INLINE_VISIBILITY 413void 414__vector_base<_Tp, _Allocator>::__destruct_at_end(pointer __new_last) _NOEXCEPT 415{ 416 while (__new_last != __end_) 417 __alloc_traits::destroy(__alloc(), _VSTD::__to_raw_pointer(--__end_)); 418} 419 420template <class _Tp, class _Allocator> 421inline _LIBCPP_INLINE_VISIBILITY 422__vector_base<_Tp, _Allocator>::__vector_base() 423 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) 424 : __begin_(nullptr), 425 __end_(nullptr), 426 __end_cap_(nullptr) 427{ 428} 429 430template <class _Tp, class _Allocator> 431inline _LIBCPP_INLINE_VISIBILITY 432__vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a) 433 : __begin_(nullptr), 434 __end_(nullptr), 435 __end_cap_(nullptr, __a) 436{ 437} 438 439template <class _Tp, class _Allocator> 440__vector_base<_Tp, _Allocator>::~__vector_base() 441{ 442 if (__begin_ != nullptr) 443 { 444 clear(); 445 __alloc_traits::deallocate(__alloc(), __begin_, capacity()); 446 } 447} 448 449template <class _Tp, class _Allocator /* = allocator<_Tp> */> 450class _LIBCPP_TYPE_VIS_ONLY vector 451 : private __vector_base<_Tp, _Allocator> 452{ 453private: 454 typedef __vector_base<_Tp, _Allocator> __base; 455 typedef allocator<_Tp> __default_allocator_type; 456public: 457 typedef vector __self; 458 typedef _Tp value_type; 459 typedef _Allocator allocator_type; 460 typedef typename __base::__alloc_traits __alloc_traits; 461 typedef typename __base::reference reference; 462 typedef typename __base::const_reference const_reference; 463 typedef typename __base::size_type size_type; 464 typedef typename __base::difference_type difference_type; 465 typedef typename __base::pointer pointer; 466 typedef typename __base::const_pointer const_pointer; 467 typedef __wrap_iter<pointer> iterator; 468 typedef __wrap_iter<const_pointer> const_iterator; 469 typedef _VSTD::reverse_iterator<iterator> reverse_iterator; 470 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; 471 472 static_assert((is_same<typename allocator_type::value_type, value_type>::value), 473 "Allocator::value_type must be same type as value_type"); 474 475 _LIBCPP_INLINE_VISIBILITY 476 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) 477 { 478#if _LIBCPP_DEBUG_LEVEL >= 2 479 __get_db()->__insert_c(this); 480#endif 481 } 482 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a) 483#if _LIBCPP_STD_VER <= 14 484 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value) 485#else 486 _NOEXCEPT 487#endif 488 : __base(__a) 489 { 490#if _LIBCPP_DEBUG_LEVEL >= 2 491 __get_db()->__insert_c(this); 492#endif 493 } 494 explicit vector(size_type __n); 495#if _LIBCPP_STD_VER > 11 496 explicit vector(size_type __n, const allocator_type& __a); 497#endif 498 vector(size_type __n, const_reference __x); 499 vector(size_type __n, const_reference __x, const allocator_type& __a); 500 template <class _InputIterator> 501 vector(_InputIterator __first, 502 typename enable_if<__is_input_iterator <_InputIterator>::value && 503 !__is_forward_iterator<_InputIterator>::value && 504 is_constructible< 505 value_type, 506 typename iterator_traits<_InputIterator>::reference>::value, 507 _InputIterator>::type __last); 508 template <class _InputIterator> 509 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a, 510 typename enable_if<__is_input_iterator <_InputIterator>::value && 511 !__is_forward_iterator<_InputIterator>::value && 512 is_constructible< 513 value_type, 514 typename iterator_traits<_InputIterator>::reference>::value>::type* = 0); 515 template <class _ForwardIterator> 516 vector(_ForwardIterator __first, 517 typename enable_if<__is_forward_iterator<_ForwardIterator>::value && 518 is_constructible< 519 value_type, 520 typename iterator_traits<_ForwardIterator>::reference>::value, 521 _ForwardIterator>::type __last); 522 template <class _ForwardIterator> 523 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a, 524 typename enable_if<__is_forward_iterator<_ForwardIterator>::value && 525 is_constructible< 526 value_type, 527 typename iterator_traits<_ForwardIterator>::reference>::value>::type* = 0); 528#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 529 _LIBCPP_INLINE_VISIBILITY 530 vector(initializer_list<value_type> __il); 531 _LIBCPP_INLINE_VISIBILITY 532 vector(initializer_list<value_type> __il, const allocator_type& __a); 533#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 534#if _LIBCPP_DEBUG_LEVEL >= 2 535 _LIBCPP_INLINE_VISIBILITY 536 ~vector() 537 { 538 __get_db()->__erase_c(this); 539 } 540#endif 541 542 vector(const vector& __x); 543 vector(const vector& __x, const allocator_type& __a); 544 _LIBCPP_INLINE_VISIBILITY 545 vector& operator=(const vector& __x); 546#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 547 _LIBCPP_INLINE_VISIBILITY 548 vector(vector&& __x) 549#if _LIBCPP_STD_VER > 14 550 _NOEXCEPT; 551#else 552 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); 553#endif 554 _LIBCPP_INLINE_VISIBILITY 555 vector(vector&& __x, const allocator_type& __a); 556 _LIBCPP_INLINE_VISIBILITY 557 vector& operator=(vector&& __x) 558 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)); 559#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 560#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 561 _LIBCPP_INLINE_VISIBILITY 562 vector& operator=(initializer_list<value_type> __il) 563 {assign(__il.begin(), __il.end()); return *this;} 564#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 565 566 template <class _InputIterator> 567 typename enable_if 568 < 569 __is_input_iterator <_InputIterator>::value && 570 !__is_forward_iterator<_InputIterator>::value && 571 is_constructible< 572 value_type, 573 typename iterator_traits<_InputIterator>::reference>::value, 574 void 575 >::type 576 assign(_InputIterator __first, _InputIterator __last); 577 template <class _ForwardIterator> 578 typename enable_if 579 < 580 __is_forward_iterator<_ForwardIterator>::value && 581 is_constructible< 582 value_type, 583 typename iterator_traits<_ForwardIterator>::reference>::value, 584 void 585 >::type 586 assign(_ForwardIterator __first, _ForwardIterator __last); 587 588 void assign(size_type __n, const_reference __u); 589#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 590 _LIBCPP_INLINE_VISIBILITY 591 void assign(initializer_list<value_type> __il) 592 {assign(__il.begin(), __il.end());} 593#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 594 595 _LIBCPP_INLINE_VISIBILITY 596 allocator_type get_allocator() const _NOEXCEPT 597 {return this->__alloc();} 598 599 _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT; 600 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT; 601 _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT; 602 _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT; 603 604 _LIBCPP_INLINE_VISIBILITY 605 reverse_iterator rbegin() _NOEXCEPT 606 {return reverse_iterator(end());} 607 _LIBCPP_INLINE_VISIBILITY 608 const_reverse_iterator rbegin() const _NOEXCEPT 609 {return const_reverse_iterator(end());} 610 _LIBCPP_INLINE_VISIBILITY 611 reverse_iterator rend() _NOEXCEPT 612 {return reverse_iterator(begin());} 613 _LIBCPP_INLINE_VISIBILITY 614 const_reverse_iterator rend() const _NOEXCEPT 615 {return const_reverse_iterator(begin());} 616 617 _LIBCPP_INLINE_VISIBILITY 618 const_iterator cbegin() const _NOEXCEPT 619 {return begin();} 620 _LIBCPP_INLINE_VISIBILITY 621 const_iterator cend() const _NOEXCEPT 622 {return end();} 623 _LIBCPP_INLINE_VISIBILITY 624 const_reverse_iterator crbegin() const _NOEXCEPT 625 {return rbegin();} 626 _LIBCPP_INLINE_VISIBILITY 627 const_reverse_iterator crend() const _NOEXCEPT 628 {return rend();} 629 630 _LIBCPP_INLINE_VISIBILITY 631 size_type size() const _NOEXCEPT 632 {return static_cast<size_type>(this->__end_ - this->__begin_);} 633 _LIBCPP_INLINE_VISIBILITY 634 size_type capacity() const _NOEXCEPT 635 {return __base::capacity();} 636 _LIBCPP_INLINE_VISIBILITY 637 bool empty() const _NOEXCEPT 638 {return this->__begin_ == this->__end_;} 639 size_type max_size() const _NOEXCEPT; 640 void reserve(size_type __n); 641 void shrink_to_fit() _NOEXCEPT; 642 643 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n); 644 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const; 645 reference at(size_type __n); 646 const_reference at(size_type __n) const; 647 648 _LIBCPP_INLINE_VISIBILITY reference front() 649 { 650 _LIBCPP_ASSERT(!empty(), "front() called for empty vector"); 651 return *this->__begin_; 652 } 653 _LIBCPP_INLINE_VISIBILITY const_reference front() const 654 { 655 _LIBCPP_ASSERT(!empty(), "front() called for empty vector"); 656 return *this->__begin_; 657 } 658 _LIBCPP_INLINE_VISIBILITY reference back() 659 { 660 _LIBCPP_ASSERT(!empty(), "back() called for empty vector"); 661 return *(this->__end_ - 1); 662 } 663 _LIBCPP_INLINE_VISIBILITY const_reference back() const 664 { 665 _LIBCPP_ASSERT(!empty(), "back() called for empty vector"); 666 return *(this->__end_ - 1); 667 } 668 669 _LIBCPP_INLINE_VISIBILITY 670 value_type* data() _NOEXCEPT 671 {return _VSTD::__to_raw_pointer(this->__begin_);} 672 _LIBCPP_INLINE_VISIBILITY 673 const value_type* data() const _NOEXCEPT 674 {return _VSTD::__to_raw_pointer(this->__begin_);} 675 676 _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x); 677#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 678 _LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x); 679#ifndef _LIBCPP_HAS_NO_VARIADICS 680 template <class... _Args> 681 _LIBCPP_INLINE_VISIBILITY 682 reference emplace_back(_Args&&... __args); 683#endif // _LIBCPP_HAS_NO_VARIADICS 684#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 685 _LIBCPP_INLINE_VISIBILITY 686 void pop_back(); 687 688 iterator insert(const_iterator __position, const_reference __x); 689#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 690 iterator insert(const_iterator __position, value_type&& __x); 691#ifndef _LIBCPP_HAS_NO_VARIADICS 692 template <class... _Args> 693 iterator emplace(const_iterator __position, _Args&&... __args); 694#endif // _LIBCPP_HAS_NO_VARIADICS 695#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 696 iterator insert(const_iterator __position, size_type __n, const_reference __x); 697 template <class _InputIterator> 698 typename enable_if 699 < 700 __is_input_iterator <_InputIterator>::value && 701 !__is_forward_iterator<_InputIterator>::value && 702 is_constructible< 703 value_type, 704 typename iterator_traits<_InputIterator>::reference>::value, 705 iterator 706 >::type 707 insert(const_iterator __position, _InputIterator __first, _InputIterator __last); 708 template <class _ForwardIterator> 709 typename enable_if 710 < 711 __is_forward_iterator<_ForwardIterator>::value && 712 is_constructible< 713 value_type, 714 typename iterator_traits<_ForwardIterator>::reference>::value, 715 iterator 716 >::type 717 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last); 718#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 719 _LIBCPP_INLINE_VISIBILITY 720 iterator insert(const_iterator __position, initializer_list<value_type> __il) 721 {return insert(__position, __il.begin(), __il.end());} 722#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 723 724 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position); 725 iterator erase(const_iterator __first, const_iterator __last); 726 727 _LIBCPP_INLINE_VISIBILITY 728 void clear() _NOEXCEPT 729 { 730 size_type __old_size = size(); 731 __base::clear(); 732 __annotate_shrink(__old_size); 733 __invalidate_all_iterators(); 734 } 735 736 void resize(size_type __sz); 737 void resize(size_type __sz, const_reference __x); 738 739 void swap(vector&) 740#if _LIBCPP_STD_VER >= 14 741 _NOEXCEPT; 742#else 743 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 744 __is_nothrow_swappable<allocator_type>::value); 745#endif 746 747 bool __invariants() const; 748 749#if _LIBCPP_DEBUG_LEVEL >= 2 750 751 bool __dereferenceable(const const_iterator* __i) const; 752 bool __decrementable(const const_iterator* __i) const; 753 bool __addable(const const_iterator* __i, ptrdiff_t __n) const; 754 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const; 755 756#endif // _LIBCPP_DEBUG_LEVEL >= 2 757 758private: 759 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators(); 760 void allocate(size_type __n); 761 void deallocate() _NOEXCEPT; 762 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const; 763 void __construct_at_end(size_type __n); 764 _LIBCPP_INLINE_VISIBILITY 765 void __construct_at_end(size_type __n, const_reference __x); 766 template <class _ForwardIterator> 767 typename enable_if 768 < 769 __is_forward_iterator<_ForwardIterator>::value, 770 void 771 >::type 772 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n); 773 void __append(size_type __n); 774 void __append(size_type __n, const_reference __x); 775 _LIBCPP_INLINE_VISIBILITY 776 iterator __make_iter(pointer __p) _NOEXCEPT; 777 _LIBCPP_INLINE_VISIBILITY 778 const_iterator __make_iter(const_pointer __p) const _NOEXCEPT; 779 void __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v); 780 pointer __swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p); 781 void __move_range(pointer __from_s, pointer __from_e, pointer __to); 782 void __move_assign(vector& __c, true_type) 783 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value); 784 void __move_assign(vector& __c, false_type) 785 _NOEXCEPT_(__alloc_traits::is_always_equal::value); 786 _LIBCPP_INLINE_VISIBILITY 787 void __destruct_at_end(pointer __new_last) _NOEXCEPT 788 { 789#if _LIBCPP_DEBUG_LEVEL >= 2 790 __c_node* __c = __get_db()->__find_c_and_lock(this); 791 for (__i_node** __p = __c->end_; __p != __c->beg_; ) 792 { 793 --__p; 794 const_iterator* __i = static_cast<const_iterator*>((*__p)->__i_); 795 if (__i->base() > __new_last) 796 { 797 (*__p)->__c_ = nullptr; 798 if (--__c->end_ != __p) 799 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*)); 800 } 801 } 802 __get_db()->unlock(); 803#endif 804 size_type __old_size = size(); 805 __base::__destruct_at_end(__new_last); 806 __annotate_shrink(__old_size); 807 } 808 template <class _Up> 809 void 810#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 811 __push_back_slow_path(_Up&& __x); 812#else 813 __push_back_slow_path(_Up& __x); 814#endif 815#if !defined(_LIBCPP_HAS_NO_VARIADICS) && !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) 816 template <class... _Args> 817 void 818 __emplace_back_slow_path(_Args&&... __args); 819#endif 820 // The following functions are no-ops outside of AddressSanitizer mode. 821 // We call annotatations only for the default Allocator because other allocators 822 // may not meet the AddressSanitizer alignment constraints. 823 // See the documentation for __sanitizer_annotate_contiguous_container for more details. 824 void __annotate_contiguous_container 825 (const void *__beg, const void *__end, const void *__old_mid, const void *__new_mid) const 826 { 827#ifndef _LIBCPP_HAS_NO_ASAN 828 if (__beg && is_same<allocator_type, __default_allocator_type>::value) 829 __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid); 830#endif 831 } 832 833 void __annotate_new(size_type __current_size) const 834 { 835 __annotate_contiguous_container(data(), data() + capacity(), 836 data() + capacity(), data() + __current_size); 837 } 838 void __annotate_delete() const 839 { 840 __annotate_contiguous_container(data(), data() + capacity(), 841 data() + size(), data() + capacity()); 842 } 843 void __annotate_increase(size_type __n) const 844 { 845 __annotate_contiguous_container(data(), data() + capacity(), 846 data() + size(), data() + size() + __n); 847 } 848 void __annotate_shrink(size_type __old_size) const 849 { 850 __annotate_contiguous_container(data(), data() + capacity(), 851 data() + __old_size, data() + size()); 852 } 853#ifndef _LIBCPP_HAS_NO_ASAN 854 // The annotation for size increase should happen before the actual increase, 855 // but if an exception is thrown after that the annotation has to be undone. 856 struct __RAII_IncreaseAnnotator { 857 __RAII_IncreaseAnnotator(const vector &__v, size_type __n = 1) 858 : __commit(false), __v(__v), __old_size(__v.size() + __n) { 859 __v.__annotate_increase(__n); 860 } 861 void __done() { __commit = true; } 862 ~__RAII_IncreaseAnnotator() { 863 if (__commit) return; 864 __v.__annotate_shrink(__old_size); 865 } 866 bool __commit; 867 const vector &__v; 868 size_type __old_size; 869 }; 870#else 871 struct __RAII_IncreaseAnnotator { 872 inline __RAII_IncreaseAnnotator(const vector &, size_type __n = 1) {} 873 inline void __done() {} 874 }; 875#endif 876 877}; 878 879template <class _Tp, class _Allocator> 880void 881vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v) 882{ 883 __annotate_delete(); 884 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, this->__end_, __v.__begin_); 885 _VSTD::swap(this->__begin_, __v.__begin_); 886 _VSTD::swap(this->__end_, __v.__end_); 887 _VSTD::swap(this->__end_cap(), __v.__end_cap()); 888 __v.__first_ = __v.__begin_; 889 __annotate_new(size()); 890 __invalidate_all_iterators(); 891} 892 893template <class _Tp, class _Allocator> 894typename vector<_Tp, _Allocator>::pointer 895vector<_Tp, _Allocator>::__swap_out_circular_buffer(__split_buffer<value_type, allocator_type&>& __v, pointer __p) 896{ 897 __annotate_delete(); 898 pointer __r = __v.__begin_; 899 __alloc_traits::__construct_backward(this->__alloc(), this->__begin_, __p, __v.__begin_); 900 __alloc_traits::__construct_forward(this->__alloc(), __p, this->__end_, __v.__end_); 901 _VSTD::swap(this->__begin_, __v.__begin_); 902 _VSTD::swap(this->__end_, __v.__end_); 903 _VSTD::swap(this->__end_cap(), __v.__end_cap()); 904 __v.__first_ = __v.__begin_; 905 __annotate_new(size()); 906 __invalidate_all_iterators(); 907 return __r; 908} 909 910// Allocate space for __n objects 911// throws length_error if __n > max_size() 912// throws (probably bad_alloc) if memory run out 913// Precondition: __begin_ == __end_ == __end_cap() == 0 914// Precondition: __n > 0 915// Postcondition: capacity() == __n 916// Postcondition: size() == 0 917template <class _Tp, class _Allocator> 918void 919vector<_Tp, _Allocator>::allocate(size_type __n) 920{ 921 if (__n > max_size()) 922 this->__throw_length_error(); 923 this->__begin_ = this->__end_ = __alloc_traits::allocate(this->__alloc(), __n); 924 this->__end_cap() = this->__begin_ + __n; 925 __annotate_new(0); 926} 927 928template <class _Tp, class _Allocator> 929void 930vector<_Tp, _Allocator>::deallocate() _NOEXCEPT 931{ 932 if (this->__begin_ != nullptr) 933 { 934 clear(); 935 __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity()); 936 this->__begin_ = this->__end_ = this->__end_cap() = nullptr; 937 } 938} 939 940template <class _Tp, class _Allocator> 941typename vector<_Tp, _Allocator>::size_type 942vector<_Tp, _Allocator>::max_size() const _NOEXCEPT 943{ 944 return _VSTD::min<size_type>(__alloc_traits::max_size(this->__alloc()), numeric_limits<size_type>::max() / 2); // end() >= begin(), always 945} 946 947// Precondition: __new_size > capacity() 948template <class _Tp, class _Allocator> 949inline _LIBCPP_INLINE_VISIBILITY 950typename vector<_Tp, _Allocator>::size_type 951vector<_Tp, _Allocator>::__recommend(size_type __new_size) const 952{ 953 const size_type __ms = max_size(); 954 if (__new_size > __ms) 955 this->__throw_length_error(); 956 const size_type __cap = capacity(); 957 if (__cap >= __ms / 2) 958 return __ms; 959 return _VSTD::max<size_type>(2*__cap, __new_size); 960} 961 962// Default constructs __n objects starting at __end_ 963// throws if construction throws 964// Precondition: __n > 0 965// Precondition: size() + __n <= capacity() 966// Postcondition: size() == size() + __n 967template <class _Tp, class _Allocator> 968void 969vector<_Tp, _Allocator>::__construct_at_end(size_type __n) 970{ 971 allocator_type& __a = this->__alloc(); 972 do 973 { 974 __RAII_IncreaseAnnotator __annotator(*this); 975 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_)); 976 ++this->__end_; 977 --__n; 978 __annotator.__done(); 979 } while (__n > 0); 980} 981 982// Copy constructs __n objects starting at __end_ from __x 983// throws if construction throws 984// Precondition: __n > 0 985// Precondition: size() + __n <= capacity() 986// Postcondition: size() == old size() + __n 987// Postcondition: [i] == __x for all i in [size() - __n, __n) 988template <class _Tp, class _Allocator> 989inline 990void 991vector<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) 992{ 993 allocator_type& __a = this->__alloc(); 994 do 995 { 996 __RAII_IncreaseAnnotator __annotator(*this); 997 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), __x); 998 ++this->__end_; 999 --__n; 1000 __annotator.__done(); 1001 } while (__n > 0); 1002} 1003 1004template <class _Tp, class _Allocator> 1005template <class _ForwardIterator> 1006typename enable_if 1007< 1008 __is_forward_iterator<_ForwardIterator>::value, 1009 void 1010>::type 1011vector<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last, size_type __n) 1012{ 1013 allocator_type& __a = this->__alloc(); 1014 __RAII_IncreaseAnnotator __annotator(*this, __n); 1015 __alloc_traits::__construct_range_forward(__a, __first, __last, this->__end_); 1016 __annotator.__done(); 1017} 1018 1019// Default constructs __n objects starting at __end_ 1020// throws if construction throws 1021// Postcondition: size() == size() + __n 1022// Exception safety: strong. 1023template <class _Tp, class _Allocator> 1024void 1025vector<_Tp, _Allocator>::__append(size_type __n) 1026{ 1027 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n) 1028 this->__construct_at_end(__n); 1029 else 1030 { 1031 allocator_type& __a = this->__alloc(); 1032 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a); 1033 __v.__construct_at_end(__n); 1034 __swap_out_circular_buffer(__v); 1035 } 1036} 1037 1038// Default constructs __n objects starting at __end_ 1039// throws if construction throws 1040// Postcondition: size() == size() + __n 1041// Exception safety: strong. 1042template <class _Tp, class _Allocator> 1043void 1044vector<_Tp, _Allocator>::__append(size_type __n, const_reference __x) 1045{ 1046 if (static_cast<size_type>(this->__end_cap() - this->__end_) >= __n) 1047 this->__construct_at_end(__n, __x); 1048 else 1049 { 1050 allocator_type& __a = this->__alloc(); 1051 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), size(), __a); 1052 __v.__construct_at_end(__n, __x); 1053 __swap_out_circular_buffer(__v); 1054 } 1055} 1056 1057template <class _Tp, class _Allocator> 1058vector<_Tp, _Allocator>::vector(size_type __n) 1059{ 1060#if _LIBCPP_DEBUG_LEVEL >= 2 1061 __get_db()->__insert_c(this); 1062#endif 1063 if (__n > 0) 1064 { 1065 allocate(__n); 1066 __construct_at_end(__n); 1067 } 1068} 1069 1070#if _LIBCPP_STD_VER > 11 1071template <class _Tp, class _Allocator> 1072vector<_Tp, _Allocator>::vector(size_type __n, const allocator_type& __a) 1073 : __base(__a) 1074{ 1075#if _LIBCPP_DEBUG_LEVEL >= 2 1076 __get_db()->__insert_c(this); 1077#endif 1078 if (__n > 0) 1079 { 1080 allocate(__n); 1081 __construct_at_end(__n); 1082 } 1083} 1084#endif 1085 1086template <class _Tp, class _Allocator> 1087vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x) 1088{ 1089#if _LIBCPP_DEBUG_LEVEL >= 2 1090 __get_db()->__insert_c(this); 1091#endif 1092 if (__n > 0) 1093 { 1094 allocate(__n); 1095 __construct_at_end(__n, __x); 1096 } 1097} 1098 1099template <class _Tp, class _Allocator> 1100vector<_Tp, _Allocator>::vector(size_type __n, const_reference __x, const allocator_type& __a) 1101 : __base(__a) 1102{ 1103#if _LIBCPP_DEBUG_LEVEL >= 2 1104 __get_db()->__insert_c(this); 1105#endif 1106 if (__n > 0) 1107 { 1108 allocate(__n); 1109 __construct_at_end(__n, __x); 1110 } 1111} 1112 1113template <class _Tp, class _Allocator> 1114template <class _InputIterator> 1115vector<_Tp, _Allocator>::vector(_InputIterator __first, 1116 typename enable_if<__is_input_iterator <_InputIterator>::value && 1117 !__is_forward_iterator<_InputIterator>::value && 1118 is_constructible< 1119 value_type, 1120 typename iterator_traits<_InputIterator>::reference>::value, 1121 _InputIterator>::type __last) 1122{ 1123#if _LIBCPP_DEBUG_LEVEL >= 2 1124 __get_db()->__insert_c(this); 1125#endif 1126 for (; __first != __last; ++__first) 1127 push_back(*__first); 1128} 1129 1130template <class _Tp, class _Allocator> 1131template <class _InputIterator> 1132vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a, 1133 typename enable_if<__is_input_iterator <_InputIterator>::value && 1134 !__is_forward_iterator<_InputIterator>::value && 1135 is_constructible< 1136 value_type, 1137 typename iterator_traits<_InputIterator>::reference>::value>::type*) 1138 : __base(__a) 1139{ 1140#if _LIBCPP_DEBUG_LEVEL >= 2 1141 __get_db()->__insert_c(this); 1142#endif 1143 for (; __first != __last; ++__first) 1144 push_back(*__first); 1145} 1146 1147template <class _Tp, class _Allocator> 1148template <class _ForwardIterator> 1149vector<_Tp, _Allocator>::vector(_ForwardIterator __first, 1150 typename enable_if<__is_forward_iterator<_ForwardIterator>::value && 1151 is_constructible< 1152 value_type, 1153 typename iterator_traits<_ForwardIterator>::reference>::value, 1154 _ForwardIterator>::type __last) 1155{ 1156#if _LIBCPP_DEBUG_LEVEL >= 2 1157 __get_db()->__insert_c(this); 1158#endif 1159 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last)); 1160 if (__n > 0) 1161 { 1162 allocate(__n); 1163 __construct_at_end(__first, __last, __n); 1164 } 1165} 1166 1167template <class _Tp, class _Allocator> 1168template <class _ForwardIterator> 1169vector<_Tp, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a, 1170 typename enable_if<__is_forward_iterator<_ForwardIterator>::value && 1171 is_constructible< 1172 value_type, 1173 typename iterator_traits<_ForwardIterator>::reference>::value>::type*) 1174 : __base(__a) 1175{ 1176#if _LIBCPP_DEBUG_LEVEL >= 2 1177 __get_db()->__insert_c(this); 1178#endif 1179 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last)); 1180 if (__n > 0) 1181 { 1182 allocate(__n); 1183 __construct_at_end(__first, __last, __n); 1184 } 1185} 1186 1187template <class _Tp, class _Allocator> 1188vector<_Tp, _Allocator>::vector(const vector& __x) 1189 : __base(__alloc_traits::select_on_container_copy_construction(__x.__alloc())) 1190{ 1191#if _LIBCPP_DEBUG_LEVEL >= 2 1192 __get_db()->__insert_c(this); 1193#endif 1194 size_type __n = __x.size(); 1195 if (__n > 0) 1196 { 1197 allocate(__n); 1198 __construct_at_end(__x.__begin_, __x.__end_, __n); 1199 } 1200} 1201 1202template <class _Tp, class _Allocator> 1203vector<_Tp, _Allocator>::vector(const vector& __x, const allocator_type& __a) 1204 : __base(__a) 1205{ 1206#if _LIBCPP_DEBUG_LEVEL >= 2 1207 __get_db()->__insert_c(this); 1208#endif 1209 size_type __n = __x.size(); 1210 if (__n > 0) 1211 { 1212 allocate(__n); 1213 __construct_at_end(__x.__begin_, __x.__end_, __n); 1214 } 1215} 1216 1217#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1218 1219template <class _Tp, class _Allocator> 1220inline _LIBCPP_INLINE_VISIBILITY 1221vector<_Tp, _Allocator>::vector(vector&& __x) 1222#if _LIBCPP_STD_VER > 14 1223 _NOEXCEPT 1224#else 1225 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) 1226#endif 1227 : __base(_VSTD::move(__x.__alloc())) 1228{ 1229#if _LIBCPP_DEBUG_LEVEL >= 2 1230 __get_db()->__insert_c(this); 1231 __get_db()->swap(this, &__x); 1232#endif 1233 this->__begin_ = __x.__begin_; 1234 this->__end_ = __x.__end_; 1235 this->__end_cap() = __x.__end_cap(); 1236 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr; 1237} 1238 1239template <class _Tp, class _Allocator> 1240inline _LIBCPP_INLINE_VISIBILITY 1241vector<_Tp, _Allocator>::vector(vector&& __x, const allocator_type& __a) 1242 : __base(__a) 1243{ 1244#if _LIBCPP_DEBUG_LEVEL >= 2 1245 __get_db()->__insert_c(this); 1246#endif 1247 if (__a == __x.__alloc()) 1248 { 1249 this->__begin_ = __x.__begin_; 1250 this->__end_ = __x.__end_; 1251 this->__end_cap() = __x.__end_cap(); 1252 __x.__begin_ = __x.__end_ = __x.__end_cap() = nullptr; 1253#if _LIBCPP_DEBUG_LEVEL >= 2 1254 __get_db()->swap(this, &__x); 1255#endif 1256 } 1257 else 1258 { 1259 typedef move_iterator<iterator> _Ip; 1260 assign(_Ip(__x.begin()), _Ip(__x.end())); 1261 } 1262} 1263 1264#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 1265 1266template <class _Tp, class _Allocator> 1267inline _LIBCPP_INLINE_VISIBILITY 1268vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il) 1269{ 1270#if _LIBCPP_DEBUG_LEVEL >= 2 1271 __get_db()->__insert_c(this); 1272#endif 1273 if (__il.size() > 0) 1274 { 1275 allocate(__il.size()); 1276 __construct_at_end(__il.begin(), __il.end(), __il.size()); 1277 } 1278} 1279 1280template <class _Tp, class _Allocator> 1281inline _LIBCPP_INLINE_VISIBILITY 1282vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a) 1283 : __base(__a) 1284{ 1285#if _LIBCPP_DEBUG_LEVEL >= 2 1286 __get_db()->__insert_c(this); 1287#endif 1288 if (__il.size() > 0) 1289 { 1290 allocate(__il.size()); 1291 __construct_at_end(__il.begin(), __il.end(), __il.size()); 1292 } 1293} 1294 1295#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 1296 1297template <class _Tp, class _Allocator> 1298inline _LIBCPP_INLINE_VISIBILITY 1299vector<_Tp, _Allocator>& 1300vector<_Tp, _Allocator>::operator=(vector&& __x) 1301 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)) 1302{ 1303 __move_assign(__x, integral_constant<bool, 1304 __alloc_traits::propagate_on_container_move_assignment::value>()); 1305 return *this; 1306} 1307 1308template <class _Tp, class _Allocator> 1309void 1310vector<_Tp, _Allocator>::__move_assign(vector& __c, false_type) 1311 _NOEXCEPT_(__alloc_traits::is_always_equal::value) 1312{ 1313 if (__base::__alloc() != __c.__alloc()) 1314 { 1315 typedef move_iterator<iterator> _Ip; 1316 assign(_Ip(__c.begin()), _Ip(__c.end())); 1317 } 1318 else 1319 __move_assign(__c, true_type()); 1320} 1321 1322template <class _Tp, class _Allocator> 1323void 1324vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type) 1325 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) 1326{ 1327 deallocate(); 1328 __base::__move_assign_alloc(__c); // this can throw 1329 this->__begin_ = __c.__begin_; 1330 this->__end_ = __c.__end_; 1331 this->__end_cap() = __c.__end_cap(); 1332 __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr; 1333#if _LIBCPP_DEBUG_LEVEL >= 2 1334 __get_db()->swap(this, &__c); 1335#endif 1336} 1337 1338#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1339 1340template <class _Tp, class _Allocator> 1341inline _LIBCPP_INLINE_VISIBILITY 1342vector<_Tp, _Allocator>& 1343vector<_Tp, _Allocator>::operator=(const vector& __x) 1344{ 1345 if (this != &__x) 1346 { 1347 __base::__copy_assign_alloc(__x); 1348 assign(__x.__begin_, __x.__end_); 1349 } 1350 return *this; 1351} 1352 1353template <class _Tp, class _Allocator> 1354template <class _InputIterator> 1355typename enable_if 1356< 1357 __is_input_iterator <_InputIterator>::value && 1358 !__is_forward_iterator<_InputIterator>::value && 1359 is_constructible< 1360 _Tp, 1361 typename iterator_traits<_InputIterator>::reference>::value, 1362 void 1363>::type 1364vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last) 1365{ 1366 clear(); 1367 for (; __first != __last; ++__first) 1368 push_back(*__first); 1369} 1370 1371template <class _Tp, class _Allocator> 1372template <class _ForwardIterator> 1373typename enable_if 1374< 1375 __is_forward_iterator<_ForwardIterator>::value && 1376 is_constructible< 1377 _Tp, 1378 typename iterator_traits<_ForwardIterator>::reference>::value, 1379 void 1380>::type 1381vector<_Tp, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) 1382{ 1383 size_type __new_size = static_cast<size_type>(_VSTD::distance(__first, __last)); 1384 if (__new_size <= capacity()) 1385 { 1386 _ForwardIterator __mid = __last; 1387 bool __growing = false; 1388 if (__new_size > size()) 1389 { 1390 __growing = true; 1391 __mid = __first; 1392 _VSTD::advance(__mid, size()); 1393 } 1394 pointer __m = _VSTD::copy(__first, __mid, this->__begin_); 1395 if (__growing) 1396 __construct_at_end(__mid, __last, __new_size - size()); 1397 else 1398 this->__destruct_at_end(__m); 1399 } 1400 else 1401 { 1402 deallocate(); 1403 allocate(__recommend(__new_size)); 1404 __construct_at_end(__first, __last, __new_size); 1405 } 1406} 1407 1408template <class _Tp, class _Allocator> 1409void 1410vector<_Tp, _Allocator>::assign(size_type __n, const_reference __u) 1411{ 1412 if (__n <= capacity()) 1413 { 1414 size_type __s = size(); 1415 _VSTD::fill_n(this->__begin_, _VSTD::min(__n, __s), __u); 1416 if (__n > __s) 1417 __construct_at_end(__n - __s, __u); 1418 else 1419 this->__destruct_at_end(this->__begin_ + __n); 1420 } 1421 else 1422 { 1423 deallocate(); 1424 allocate(__recommend(static_cast<size_type>(__n))); 1425 __construct_at_end(__n, __u); 1426 } 1427} 1428 1429template <class _Tp, class _Allocator> 1430inline _LIBCPP_INLINE_VISIBILITY 1431typename vector<_Tp, _Allocator>::iterator 1432vector<_Tp, _Allocator>::__make_iter(pointer __p) _NOEXCEPT 1433{ 1434#if _LIBCPP_DEBUG_LEVEL >= 2 1435 return iterator(this, __p); 1436#else 1437 return iterator(__p); 1438#endif 1439} 1440 1441template <class _Tp, class _Allocator> 1442inline _LIBCPP_INLINE_VISIBILITY 1443typename vector<_Tp, _Allocator>::const_iterator 1444vector<_Tp, _Allocator>::__make_iter(const_pointer __p) const _NOEXCEPT 1445{ 1446#if _LIBCPP_DEBUG_LEVEL >= 2 1447 return const_iterator(this, __p); 1448#else 1449 return const_iterator(__p); 1450#endif 1451} 1452 1453template <class _Tp, class _Allocator> 1454inline _LIBCPP_INLINE_VISIBILITY 1455typename vector<_Tp, _Allocator>::iterator 1456vector<_Tp, _Allocator>::begin() _NOEXCEPT 1457{ 1458 return __make_iter(this->__begin_); 1459} 1460 1461template <class _Tp, class _Allocator> 1462inline _LIBCPP_INLINE_VISIBILITY 1463typename vector<_Tp, _Allocator>::const_iterator 1464vector<_Tp, _Allocator>::begin() const _NOEXCEPT 1465{ 1466 return __make_iter(this->__begin_); 1467} 1468 1469template <class _Tp, class _Allocator> 1470inline _LIBCPP_INLINE_VISIBILITY 1471typename vector<_Tp, _Allocator>::iterator 1472vector<_Tp, _Allocator>::end() _NOEXCEPT 1473{ 1474 return __make_iter(this->__end_); 1475} 1476 1477template <class _Tp, class _Allocator> 1478inline _LIBCPP_INLINE_VISIBILITY 1479typename vector<_Tp, _Allocator>::const_iterator 1480vector<_Tp, _Allocator>::end() const _NOEXCEPT 1481{ 1482 return __make_iter(this->__end_); 1483} 1484 1485template <class _Tp, class _Allocator> 1486inline _LIBCPP_INLINE_VISIBILITY 1487typename vector<_Tp, _Allocator>::reference 1488vector<_Tp, _Allocator>::operator[](size_type __n) 1489{ 1490 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds"); 1491 return this->__begin_[__n]; 1492} 1493 1494template <class _Tp, class _Allocator> 1495inline _LIBCPP_INLINE_VISIBILITY 1496typename vector<_Tp, _Allocator>::const_reference 1497vector<_Tp, _Allocator>::operator[](size_type __n) const 1498{ 1499 _LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds"); 1500 return this->__begin_[__n]; 1501} 1502 1503template <class _Tp, class _Allocator> 1504typename vector<_Tp, _Allocator>::reference 1505vector<_Tp, _Allocator>::at(size_type __n) 1506{ 1507 if (__n >= size()) 1508 this->__throw_out_of_range(); 1509 return this->__begin_[__n]; 1510} 1511 1512template <class _Tp, class _Allocator> 1513typename vector<_Tp, _Allocator>::const_reference 1514vector<_Tp, _Allocator>::at(size_type __n) const 1515{ 1516 if (__n >= size()) 1517 this->__throw_out_of_range(); 1518 return this->__begin_[__n]; 1519} 1520 1521template <class _Tp, class _Allocator> 1522void 1523vector<_Tp, _Allocator>::reserve(size_type __n) 1524{ 1525 if (__n > capacity()) 1526 { 1527 allocator_type& __a = this->__alloc(); 1528 __split_buffer<value_type, allocator_type&> __v(__n, size(), __a); 1529 __swap_out_circular_buffer(__v); 1530 } 1531} 1532 1533template <class _Tp, class _Allocator> 1534void 1535vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT 1536{ 1537 if (capacity() > size()) 1538 { 1539#ifndef _LIBCPP_NO_EXCEPTIONS 1540 try 1541 { 1542#endif // _LIBCPP_NO_EXCEPTIONS 1543 allocator_type& __a = this->__alloc(); 1544 __split_buffer<value_type, allocator_type&> __v(size(), size(), __a); 1545 __swap_out_circular_buffer(__v); 1546#ifndef _LIBCPP_NO_EXCEPTIONS 1547 } 1548 catch (...) 1549 { 1550 } 1551#endif // _LIBCPP_NO_EXCEPTIONS 1552 } 1553} 1554 1555template <class _Tp, class _Allocator> 1556template <class _Up> 1557void 1558#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1559vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x) 1560#else 1561vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x) 1562#endif 1563{ 1564 allocator_type& __a = this->__alloc(); 1565 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a); 1566 // __v.push_back(_VSTD::forward<_Up>(__x)); 1567 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Up>(__x)); 1568 __v.__end_++; 1569 __swap_out_circular_buffer(__v); 1570} 1571 1572template <class _Tp, class _Allocator> 1573inline _LIBCPP_INLINE_VISIBILITY 1574void 1575vector<_Tp, _Allocator>::push_back(const_reference __x) 1576{ 1577 if (this->__end_ != this->__end_cap()) 1578 { 1579 __RAII_IncreaseAnnotator __annotator(*this); 1580 __alloc_traits::construct(this->__alloc(), 1581 _VSTD::__to_raw_pointer(this->__end_), __x); 1582 __annotator.__done(); 1583 ++this->__end_; 1584 } 1585 else 1586 __push_back_slow_path(__x); 1587} 1588 1589#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1590 1591template <class _Tp, class _Allocator> 1592inline _LIBCPP_INLINE_VISIBILITY 1593void 1594vector<_Tp, _Allocator>::push_back(value_type&& __x) 1595{ 1596 if (this->__end_ < this->__end_cap()) 1597 { 1598 __RAII_IncreaseAnnotator __annotator(*this); 1599 __alloc_traits::construct(this->__alloc(), 1600 _VSTD::__to_raw_pointer(this->__end_), 1601 _VSTD::move(__x)); 1602 __annotator.__done(); 1603 ++this->__end_; 1604 } 1605 else 1606 __push_back_slow_path(_VSTD::move(__x)); 1607} 1608 1609#ifndef _LIBCPP_HAS_NO_VARIADICS 1610 1611template <class _Tp, class _Allocator> 1612template <class... _Args> 1613void 1614vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args) 1615{ 1616 allocator_type& __a = this->__alloc(); 1617 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a); 1618// __v.emplace_back(_VSTD::forward<_Args>(__args)...); 1619 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(__v.__end_), _VSTD::forward<_Args>(__args)...); 1620 __v.__end_++; 1621 __swap_out_circular_buffer(__v); 1622} 1623 1624template <class _Tp, class _Allocator> 1625template <class... _Args> 1626inline 1627typename vector<_Tp, _Allocator>::reference 1628vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) 1629{ 1630 if (this->__end_ < this->__end_cap()) 1631 { 1632 __RAII_IncreaseAnnotator __annotator(*this); 1633 __alloc_traits::construct(this->__alloc(), 1634 _VSTD::__to_raw_pointer(this->__end_), 1635 _VSTD::forward<_Args>(__args)...); 1636 __annotator.__done(); 1637 ++this->__end_; 1638 } 1639 else 1640 __emplace_back_slow_path(_VSTD::forward<_Args>(__args)...); 1641 return this->back(); 1642} 1643 1644#endif // _LIBCPP_HAS_NO_VARIADICS 1645#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1646 1647template <class _Tp, class _Allocator> 1648inline 1649void 1650vector<_Tp, _Allocator>::pop_back() 1651{ 1652 _LIBCPP_ASSERT(!empty(), "vector::pop_back called for empty vector"); 1653 this->__destruct_at_end(this->__end_ - 1); 1654} 1655 1656template <class _Tp, class _Allocator> 1657inline _LIBCPP_INLINE_VISIBILITY 1658typename vector<_Tp, _Allocator>::iterator 1659vector<_Tp, _Allocator>::erase(const_iterator __position) 1660{ 1661#if _LIBCPP_DEBUG_LEVEL >= 2 1662 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, 1663 "vector::erase(iterator) called with an iterator not" 1664 " referring to this vector"); 1665#endif 1666 _LIBCPP_ASSERT(__position != end(), 1667 "vector::erase(iterator) called with a non-dereferenceable iterator"); 1668 difference_type __ps = __position - cbegin(); 1669 pointer __p = this->__begin_ + __ps; 1670 iterator __r = __make_iter(__p); 1671 this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p)); 1672 return __r; 1673} 1674 1675template <class _Tp, class _Allocator> 1676typename vector<_Tp, _Allocator>::iterator 1677vector<_Tp, _Allocator>::erase(const_iterator __first, const_iterator __last) 1678{ 1679#if _LIBCPP_DEBUG_LEVEL >= 2 1680 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this, 1681 "vector::erase(iterator, iterator) called with an iterator not" 1682 " referring to this vector"); 1683#endif 1684 _LIBCPP_ASSERT(__first <= __last, "vector::erase(first, last) called with invalid range"); 1685 pointer __p = this->__begin_ + (__first - begin()); 1686 iterator __r = __make_iter(__p); 1687 if (__first != __last) 1688 this->__destruct_at_end(_VSTD::move(__p + (__last - __first), this->__end_, __p)); 1689 return __r; 1690} 1691 1692template <class _Tp, class _Allocator> 1693void 1694vector<_Tp, _Allocator>::__move_range(pointer __from_s, pointer __from_e, pointer __to) 1695{ 1696 pointer __old_last = this->__end_; 1697 difference_type __n = __old_last - __to; 1698 for (pointer __i = __from_s + __n; __i < __from_e; ++__i, ++this->__end_) 1699 __alloc_traits::construct(this->__alloc(), 1700 _VSTD::__to_raw_pointer(this->__end_), 1701 _VSTD::move(*__i)); 1702 _VSTD::move_backward(__from_s, __from_s + __n, __old_last); 1703} 1704 1705template <class _Tp, class _Allocator> 1706typename vector<_Tp, _Allocator>::iterator 1707vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x) 1708{ 1709#if _LIBCPP_DEBUG_LEVEL >= 2 1710 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, 1711 "vector::insert(iterator, x) called with an iterator not" 1712 " referring to this vector"); 1713#endif 1714 pointer __p = this->__begin_ + (__position - begin()); 1715 if (this->__end_ < this->__end_cap()) 1716 { 1717 __RAII_IncreaseAnnotator __annotator(*this); 1718 if (__p == this->__end_) 1719 { 1720 __alloc_traits::construct(this->__alloc(), 1721 _VSTD::__to_raw_pointer(this->__end_), __x); 1722 ++this->__end_; 1723 } 1724 else 1725 { 1726 __move_range(__p, this->__end_, __p + 1); 1727 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x); 1728 if (__p <= __xr && __xr < this->__end_) 1729 ++__xr; 1730 *__p = *__xr; 1731 } 1732 __annotator.__done(); 1733 } 1734 else 1735 { 1736 allocator_type& __a = this->__alloc(); 1737 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a); 1738 __v.push_back(__x); 1739 __p = __swap_out_circular_buffer(__v, __p); 1740 } 1741 return __make_iter(__p); 1742} 1743 1744#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 1745 1746template <class _Tp, class _Allocator> 1747typename vector<_Tp, _Allocator>::iterator 1748vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x) 1749{ 1750#if _LIBCPP_DEBUG_LEVEL >= 2 1751 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, 1752 "vector::insert(iterator, x) called with an iterator not" 1753 " referring to this vector"); 1754#endif 1755 pointer __p = this->__begin_ + (__position - begin()); 1756 if (this->__end_ < this->__end_cap()) 1757 { 1758 __RAII_IncreaseAnnotator __annotator(*this); 1759 if (__p == this->__end_) 1760 { 1761 __alloc_traits::construct(this->__alloc(), 1762 _VSTD::__to_raw_pointer(this->__end_), 1763 _VSTD::move(__x)); 1764 ++this->__end_; 1765 } 1766 else 1767 { 1768 __move_range(__p, this->__end_, __p + 1); 1769 *__p = _VSTD::move(__x); 1770 } 1771 __annotator.__done(); 1772 } 1773 else 1774 { 1775 allocator_type& __a = this->__alloc(); 1776 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a); 1777 __v.push_back(_VSTD::move(__x)); 1778 __p = __swap_out_circular_buffer(__v, __p); 1779 } 1780 return __make_iter(__p); 1781} 1782 1783#ifndef _LIBCPP_HAS_NO_VARIADICS 1784 1785template <class _Tp, class _Allocator> 1786template <class... _Args> 1787typename vector<_Tp, _Allocator>::iterator 1788vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args) 1789{ 1790#if _LIBCPP_DEBUG_LEVEL >= 2 1791 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, 1792 "vector::emplace(iterator, x) called with an iterator not" 1793 " referring to this vector"); 1794#endif 1795 pointer __p = this->__begin_ + (__position - begin()); 1796 if (this->__end_ < this->__end_cap()) 1797 { 1798 __RAII_IncreaseAnnotator __annotator(*this); 1799 if (__p == this->__end_) 1800 { 1801 __alloc_traits::construct(this->__alloc(), 1802 _VSTD::__to_raw_pointer(this->__end_), 1803 _VSTD::forward<_Args>(__args)...); 1804 ++this->__end_; 1805 } 1806 else 1807 { 1808 __temp_value<value_type, _Allocator> __tmp(this->__alloc(), _VSTD::forward<_Args>(__args)...); 1809 __move_range(__p, this->__end_, __p + 1); 1810 *__p = _VSTD::move(__tmp.get()); 1811 } 1812 __annotator.__done(); 1813 } 1814 else 1815 { 1816 allocator_type& __a = this->__alloc(); 1817 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), __p - this->__begin_, __a); 1818 __v.emplace_back(_VSTD::forward<_Args>(__args)...); 1819 __p = __swap_out_circular_buffer(__v, __p); 1820 } 1821 return __make_iter(__p); 1822} 1823 1824#endif // _LIBCPP_HAS_NO_VARIADICS 1825#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 1826 1827template <class _Tp, class _Allocator> 1828typename vector<_Tp, _Allocator>::iterator 1829vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x) 1830{ 1831#if _LIBCPP_DEBUG_LEVEL >= 2 1832 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, 1833 "vector::insert(iterator, n, x) called with an iterator not" 1834 " referring to this vector"); 1835#endif 1836 pointer __p = this->__begin_ + (__position - begin()); 1837 if (__n > 0) 1838 { 1839 if (__n <= static_cast<size_type>(this->__end_cap() - this->__end_)) 1840 { 1841 size_type __old_n = __n; 1842 pointer __old_last = this->__end_; 1843 if (__n > static_cast<size_type>(this->__end_ - __p)) 1844 { 1845 size_type __cx = __n - (this->__end_ - __p); 1846 __construct_at_end(__cx, __x); 1847 __n -= __cx; 1848 } 1849 if (__n > 0) 1850 { 1851 __RAII_IncreaseAnnotator __annotator(*this, __n); 1852 __move_range(__p, __old_last, __p + __old_n); 1853 __annotator.__done(); 1854 const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x); 1855 if (__p <= __xr && __xr < this->__end_) 1856 __xr += __old_n; 1857 _VSTD::fill_n(__p, __n, *__xr); 1858 } 1859 } 1860 else 1861 { 1862 allocator_type& __a = this->__alloc(); 1863 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a); 1864 __v.__construct_at_end(__n, __x); 1865 __p = __swap_out_circular_buffer(__v, __p); 1866 } 1867 } 1868 return __make_iter(__p); 1869} 1870 1871template <class _Tp, class _Allocator> 1872template <class _InputIterator> 1873typename enable_if 1874< 1875 __is_input_iterator <_InputIterator>::value && 1876 !__is_forward_iterator<_InputIterator>::value && 1877 is_constructible< 1878 _Tp, 1879 typename iterator_traits<_InputIterator>::reference>::value, 1880 typename vector<_Tp, _Allocator>::iterator 1881>::type 1882vector<_Tp, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last) 1883{ 1884#if _LIBCPP_DEBUG_LEVEL >= 2 1885 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, 1886 "vector::insert(iterator, range) called with an iterator not" 1887 " referring to this vector"); 1888#endif 1889 difference_type __off = __position - begin(); 1890 pointer __p = this->__begin_ + __off; 1891 allocator_type& __a = this->__alloc(); 1892 pointer __old_last = this->__end_; 1893 for (; this->__end_ != this->__end_cap() && __first != __last; ++__first) 1894 { 1895 __RAII_IncreaseAnnotator __annotator(*this); 1896 __alloc_traits::construct(__a, _VSTD::__to_raw_pointer(this->__end_), 1897 *__first); 1898 ++this->__end_; 1899 __annotator.__done(); 1900 } 1901 __split_buffer<value_type, allocator_type&> __v(__a); 1902 if (__first != __last) 1903 { 1904#ifndef _LIBCPP_NO_EXCEPTIONS 1905 try 1906 { 1907#endif // _LIBCPP_NO_EXCEPTIONS 1908 __v.__construct_at_end(__first, __last); 1909 difference_type __old_size = __old_last - this->__begin_; 1910 difference_type __old_p = __p - this->__begin_; 1911 reserve(__recommend(size() + __v.size())); 1912 __p = this->__begin_ + __old_p; 1913 __old_last = this->__begin_ + __old_size; 1914#ifndef _LIBCPP_NO_EXCEPTIONS 1915 } 1916 catch (...) 1917 { 1918 erase(__make_iter(__old_last), end()); 1919 throw; 1920 } 1921#endif // _LIBCPP_NO_EXCEPTIONS 1922 } 1923 __p = _VSTD::rotate(__p, __old_last, this->__end_); 1924 insert(__make_iter(__p), make_move_iterator(__v.begin()), 1925 make_move_iterator(__v.end())); 1926 return begin() + __off; 1927} 1928 1929template <class _Tp, class _Allocator> 1930template <class _ForwardIterator> 1931typename enable_if 1932< 1933 __is_forward_iterator<_ForwardIterator>::value && 1934 is_constructible< 1935 _Tp, 1936 typename iterator_traits<_ForwardIterator>::reference>::value, 1937 typename vector<_Tp, _Allocator>::iterator 1938>::type 1939vector<_Tp, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) 1940{ 1941#if _LIBCPP_DEBUG_LEVEL >= 2 1942 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this, 1943 "vector::insert(iterator, range) called with an iterator not" 1944 " referring to this vector"); 1945#endif 1946 pointer __p = this->__begin_ + (__position - begin()); 1947 difference_type __n = _VSTD::distance(__first, __last); 1948 if (__n > 0) 1949 { 1950 if (__n <= this->__end_cap() - this->__end_) 1951 { 1952 size_type __old_n = __n; 1953 pointer __old_last = this->__end_; 1954 _ForwardIterator __m = __last; 1955 difference_type __dx = this->__end_ - __p; 1956 if (__n > __dx) 1957 { 1958 __m = __first; 1959 difference_type __diff = this->__end_ - __p; 1960 _VSTD::advance(__m, __diff); 1961 __construct_at_end(__m, __last, __n - __diff); 1962 __n = __dx; 1963 } 1964 if (__n > 0) 1965 { 1966 __RAII_IncreaseAnnotator __annotator(*this, __n); 1967 __move_range(__p, __old_last, __p + __old_n); 1968 __annotator.__done(); 1969 _VSTD::copy(__first, __m, __p); 1970 } 1971 } 1972 else 1973 { 1974 allocator_type& __a = this->__alloc(); 1975 __split_buffer<value_type, allocator_type&> __v(__recommend(size() + __n), __p - this->__begin_, __a); 1976 __v.__construct_at_end(__first, __last); 1977 __p = __swap_out_circular_buffer(__v, __p); 1978 } 1979 } 1980 return __make_iter(__p); 1981} 1982 1983template <class _Tp, class _Allocator> 1984void 1985vector<_Tp, _Allocator>::resize(size_type __sz) 1986{ 1987 size_type __cs = size(); 1988 if (__cs < __sz) 1989 this->__append(__sz - __cs); 1990 else if (__cs > __sz) 1991 this->__destruct_at_end(this->__begin_ + __sz); 1992} 1993 1994template <class _Tp, class _Allocator> 1995void 1996vector<_Tp, _Allocator>::resize(size_type __sz, const_reference __x) 1997{ 1998 size_type __cs = size(); 1999 if (__cs < __sz) 2000 this->__append(__sz - __cs, __x); 2001 else if (__cs > __sz) 2002 this->__destruct_at_end(this->__begin_ + __sz); 2003} 2004 2005template <class _Tp, class _Allocator> 2006void 2007vector<_Tp, _Allocator>::swap(vector& __x) 2008#if _LIBCPP_STD_VER >= 14 2009 _NOEXCEPT 2010#else 2011 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 2012 __is_nothrow_swappable<allocator_type>::value) 2013#endif 2014{ 2015 _LIBCPP_ASSERT(__alloc_traits::propagate_on_container_swap::value || 2016 this->__alloc() == __x.__alloc(), 2017 "vector::swap: Either propagate_on_container_swap must be true" 2018 " or the allocators must compare equal"); 2019 _VSTD::swap(this->__begin_, __x.__begin_); 2020 _VSTD::swap(this->__end_, __x.__end_); 2021 _VSTD::swap(this->__end_cap(), __x.__end_cap()); 2022 __swap_allocator(this->__alloc(), __x.__alloc(), 2023 integral_constant<bool,__alloc_traits::propagate_on_container_swap::value>()); 2024#if _LIBCPP_DEBUG_LEVEL >= 2 2025 __get_db()->swap(this, &__x); 2026#endif // _LIBCPP_DEBUG_LEVEL >= 2 2027} 2028 2029template <class _Tp, class _Allocator> 2030bool 2031vector<_Tp, _Allocator>::__invariants() const 2032{ 2033 if (this->__begin_ == nullptr) 2034 { 2035 if (this->__end_ != nullptr || this->__end_cap() != nullptr) 2036 return false; 2037 } 2038 else 2039 { 2040 if (this->__begin_ > this->__end_) 2041 return false; 2042 if (this->__begin_ == this->__end_cap()) 2043 return false; 2044 if (this->__end_ > this->__end_cap()) 2045 return false; 2046 } 2047 return true; 2048} 2049 2050#if _LIBCPP_DEBUG_LEVEL >= 2 2051 2052template <class _Tp, class _Allocator> 2053bool 2054vector<_Tp, _Allocator>::__dereferenceable(const const_iterator* __i) const 2055{ 2056 return this->__begin_ <= __i->base() && __i->base() < this->__end_; 2057} 2058 2059template <class _Tp, class _Allocator> 2060bool 2061vector<_Tp, _Allocator>::__decrementable(const const_iterator* __i) const 2062{ 2063 return this->__begin_ < __i->base() && __i->base() <= this->__end_; 2064} 2065 2066template <class _Tp, class _Allocator> 2067bool 2068vector<_Tp, _Allocator>::__addable(const const_iterator* __i, ptrdiff_t __n) const 2069{ 2070 const_pointer __p = __i->base() + __n; 2071 return this->__begin_ <= __p && __p <= this->__end_; 2072} 2073 2074template <class _Tp, class _Allocator> 2075bool 2076vector<_Tp, _Allocator>::__subscriptable(const const_iterator* __i, ptrdiff_t __n) const 2077{ 2078 const_pointer __p = __i->base() + __n; 2079 return this->__begin_ <= __p && __p < this->__end_; 2080} 2081 2082#endif // _LIBCPP_DEBUG_LEVEL >= 2 2083 2084template <class _Tp, class _Allocator> 2085inline _LIBCPP_INLINE_VISIBILITY 2086void 2087vector<_Tp, _Allocator>::__invalidate_all_iterators() 2088{ 2089#if _LIBCPP_DEBUG_LEVEL >= 2 2090 __get_db()->__invalidate_all(this); 2091#endif // _LIBCPP_DEBUG_LEVEL >= 2 2092} 2093 2094// vector<bool> 2095 2096template <class _Allocator> class vector<bool, _Allocator>; 2097 2098template <class _Allocator> struct hash<vector<bool, _Allocator> >; 2099 2100template <class _Allocator> 2101struct __has_storage_type<vector<bool, _Allocator> > 2102{ 2103 static const bool value = true; 2104}; 2105 2106template <class _Allocator> 2107class _LIBCPP_TYPE_VIS_ONLY vector<bool, _Allocator> 2108 : private __vector_base_common<true> 2109{ 2110public: 2111 typedef vector __self; 2112 typedef bool value_type; 2113 typedef _Allocator allocator_type; 2114 typedef allocator_traits<allocator_type> __alloc_traits; 2115 typedef typename __alloc_traits::size_type size_type; 2116 typedef typename __alloc_traits::difference_type difference_type; 2117 typedef size_type __storage_type; 2118 typedef __bit_iterator<vector, false> pointer; 2119 typedef __bit_iterator<vector, true> const_pointer; 2120 typedef pointer iterator; 2121 typedef const_pointer const_iterator; 2122 typedef _VSTD::reverse_iterator<iterator> reverse_iterator; 2123 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator; 2124 2125private: 2126 typedef typename __rebind_alloc_helper<__alloc_traits, __storage_type>::type __storage_allocator; 2127 typedef allocator_traits<__storage_allocator> __storage_traits; 2128 typedef typename __storage_traits::pointer __storage_pointer; 2129 typedef typename __storage_traits::const_pointer __const_storage_pointer; 2130 2131 __storage_pointer __begin_; 2132 size_type __size_; 2133 __compressed_pair<size_type, __storage_allocator> __cap_alloc_; 2134public: 2135 typedef __bit_reference<vector> reference; 2136 typedef __bit_const_reference<vector> const_reference; 2137private: 2138 _LIBCPP_INLINE_VISIBILITY 2139 size_type& __cap() _NOEXCEPT 2140 {return __cap_alloc_.first();} 2141 _LIBCPP_INLINE_VISIBILITY 2142 const size_type& __cap() const _NOEXCEPT 2143 {return __cap_alloc_.first();} 2144 _LIBCPP_INLINE_VISIBILITY 2145 __storage_allocator& __alloc() _NOEXCEPT 2146 {return __cap_alloc_.second();} 2147 _LIBCPP_INLINE_VISIBILITY 2148 const __storage_allocator& __alloc() const _NOEXCEPT 2149 {return __cap_alloc_.second();} 2150 2151 static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT); 2152 2153 _LIBCPP_INLINE_VISIBILITY 2154 static size_type __internal_cap_to_external(size_type __n) _NOEXCEPT 2155 {return __n * __bits_per_word;} 2156 _LIBCPP_INLINE_VISIBILITY 2157 static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT 2158 {return (__n - 1) / __bits_per_word + 1;} 2159 2160public: 2161 _LIBCPP_INLINE_VISIBILITY 2162 vector() _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); 2163 2164 _LIBCPP_INLINE_VISIBILITY explicit vector(const allocator_type& __a) 2165#if _LIBCPP_STD_VER <= 14 2166 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value); 2167#else 2168 _NOEXCEPT; 2169#endif 2170 ~vector(); 2171 explicit vector(size_type __n); 2172#if _LIBCPP_STD_VER > 11 2173 explicit vector(size_type __n, const allocator_type& __a); 2174#endif 2175 vector(size_type __n, const value_type& __v); 2176 vector(size_type __n, const value_type& __v, const allocator_type& __a); 2177 template <class _InputIterator> 2178 vector(_InputIterator __first, _InputIterator __last, 2179 typename enable_if<__is_input_iterator <_InputIterator>::value && 2180 !__is_forward_iterator<_InputIterator>::value>::type* = 0); 2181 template <class _InputIterator> 2182 vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a, 2183 typename enable_if<__is_input_iterator <_InputIterator>::value && 2184 !__is_forward_iterator<_InputIterator>::value>::type* = 0); 2185 template <class _ForwardIterator> 2186 vector(_ForwardIterator __first, _ForwardIterator __last, 2187 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0); 2188 template <class _ForwardIterator> 2189 vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a, 2190 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type* = 0); 2191 2192 vector(const vector& __v); 2193 vector(const vector& __v, const allocator_type& __a); 2194 vector& operator=(const vector& __v); 2195#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2196 vector(initializer_list<value_type> __il); 2197 vector(initializer_list<value_type> __il, const allocator_type& __a); 2198#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2199 2200#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 2201 _LIBCPP_INLINE_VISIBILITY 2202 vector(vector&& __v) 2203#if _LIBCPP_STD_VER > 14 2204 _NOEXCEPT; 2205#else 2206 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); 2207#endif 2208 vector(vector&& __v, const allocator_type& __a); 2209 _LIBCPP_INLINE_VISIBILITY 2210 vector& operator=(vector&& __v) 2211 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)); 2212#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 2213#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2214 _LIBCPP_INLINE_VISIBILITY 2215 vector& operator=(initializer_list<value_type> __il) 2216 {assign(__il.begin(), __il.end()); return *this;} 2217#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2218 2219 template <class _InputIterator> 2220 typename enable_if 2221 < 2222 __is_input_iterator<_InputIterator>::value && 2223 !__is_forward_iterator<_InputIterator>::value, 2224 void 2225 >::type 2226 assign(_InputIterator __first, _InputIterator __last); 2227 template <class _ForwardIterator> 2228 typename enable_if 2229 < 2230 __is_forward_iterator<_ForwardIterator>::value, 2231 void 2232 >::type 2233 assign(_ForwardIterator __first, _ForwardIterator __last); 2234 2235 void assign(size_type __n, const value_type& __x); 2236#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2237 _LIBCPP_INLINE_VISIBILITY 2238 void assign(initializer_list<value_type> __il) 2239 {assign(__il.begin(), __il.end());} 2240#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2241 2242 _LIBCPP_INLINE_VISIBILITY allocator_type get_allocator() const _NOEXCEPT 2243 {return allocator_type(this->__alloc());} 2244 2245 size_type max_size() const _NOEXCEPT; 2246 _LIBCPP_INLINE_VISIBILITY 2247 size_type capacity() const _NOEXCEPT 2248 {return __internal_cap_to_external(__cap());} 2249 _LIBCPP_INLINE_VISIBILITY 2250 size_type size() const _NOEXCEPT 2251 {return __size_;} 2252 _LIBCPP_INLINE_VISIBILITY 2253 bool empty() const _NOEXCEPT 2254 {return __size_ == 0;} 2255 void reserve(size_type __n); 2256 void shrink_to_fit() _NOEXCEPT; 2257 2258 _LIBCPP_INLINE_VISIBILITY 2259 iterator begin() _NOEXCEPT 2260 {return __make_iter(0);} 2261 _LIBCPP_INLINE_VISIBILITY 2262 const_iterator begin() const _NOEXCEPT 2263 {return __make_iter(0);} 2264 _LIBCPP_INLINE_VISIBILITY 2265 iterator end() _NOEXCEPT 2266 {return __make_iter(__size_);} 2267 _LIBCPP_INLINE_VISIBILITY 2268 const_iterator end() const _NOEXCEPT 2269 {return __make_iter(__size_);} 2270 2271 _LIBCPP_INLINE_VISIBILITY 2272 reverse_iterator rbegin() _NOEXCEPT 2273 {return reverse_iterator(end());} 2274 _LIBCPP_INLINE_VISIBILITY 2275 const_reverse_iterator rbegin() const _NOEXCEPT 2276 {return const_reverse_iterator(end());} 2277 _LIBCPP_INLINE_VISIBILITY 2278 reverse_iterator rend() _NOEXCEPT 2279 {return reverse_iterator(begin());} 2280 _LIBCPP_INLINE_VISIBILITY 2281 const_reverse_iterator rend() const _NOEXCEPT 2282 {return const_reverse_iterator(begin());} 2283 2284 _LIBCPP_INLINE_VISIBILITY 2285 const_iterator cbegin() const _NOEXCEPT 2286 {return __make_iter(0);} 2287 _LIBCPP_INLINE_VISIBILITY 2288 const_iterator cend() const _NOEXCEPT 2289 {return __make_iter(__size_);} 2290 _LIBCPP_INLINE_VISIBILITY 2291 const_reverse_iterator crbegin() const _NOEXCEPT 2292 {return rbegin();} 2293 _LIBCPP_INLINE_VISIBILITY 2294 const_reverse_iterator crend() const _NOEXCEPT 2295 {return rend();} 2296 2297 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __make_ref(__n);} 2298 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __make_ref(__n);} 2299 reference at(size_type __n); 2300 const_reference at(size_type __n) const; 2301 2302 _LIBCPP_INLINE_VISIBILITY reference front() {return __make_ref(0);} 2303 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __make_ref(0);} 2304 _LIBCPP_INLINE_VISIBILITY reference back() {return __make_ref(__size_ - 1);} 2305 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __make_ref(__size_ - 1);} 2306 2307 void push_back(const value_type& __x); 2308#if _LIBCPP_STD_VER > 11 2309 template <class... _Args> 2310 _LIBCPP_INLINE_VISIBILITY reference emplace_back(_Args&&... __args) { 2311 push_back ( value_type ( _VSTD::forward<_Args>(__args)... )); 2312 return this->back(); 2313 } 2314#endif 2315 2316 _LIBCPP_INLINE_VISIBILITY void pop_back() {--__size_;} 2317 2318#if _LIBCPP_STD_VER > 11 2319 template <class... _Args> 2320 _LIBCPP_INLINE_VISIBILITY iterator emplace(const_iterator position, _Args&&... __args) 2321 { return insert ( position, value_type ( _VSTD::forward<_Args>(__args)... )); } 2322#endif 2323 2324 iterator insert(const_iterator __position, const value_type& __x); 2325 iterator insert(const_iterator __position, size_type __n, const value_type& __x); 2326 iterator insert(const_iterator __position, size_type __n, const_reference __x); 2327 template <class _InputIterator> 2328 typename enable_if 2329 < 2330 __is_input_iterator <_InputIterator>::value && 2331 !__is_forward_iterator<_InputIterator>::value, 2332 iterator 2333 >::type 2334 insert(const_iterator __position, _InputIterator __first, _InputIterator __last); 2335 template <class _ForwardIterator> 2336 typename enable_if 2337 < 2338 __is_forward_iterator<_ForwardIterator>::value, 2339 iterator 2340 >::type 2341 insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last); 2342#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2343 _LIBCPP_INLINE_VISIBILITY 2344 iterator insert(const_iterator __position, initializer_list<value_type> __il) 2345 {return insert(__position, __il.begin(), __il.end());} 2346#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2347 2348 _LIBCPP_INLINE_VISIBILITY iterator erase(const_iterator __position); 2349 iterator erase(const_iterator __first, const_iterator __last); 2350 2351 _LIBCPP_INLINE_VISIBILITY 2352 void clear() _NOEXCEPT {__size_ = 0;} 2353 2354 void swap(vector&) 2355#if _LIBCPP_STD_VER >= 14 2356 _NOEXCEPT; 2357#else 2358 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 2359 __is_nothrow_swappable<allocator_type>::value); 2360#endif 2361 static void swap(reference __x, reference __y) _NOEXCEPT { _VSTD::swap(__x, __y); } 2362 2363 void resize(size_type __sz, value_type __x = false); 2364 void flip() _NOEXCEPT; 2365 2366 bool __invariants() const; 2367 2368private: 2369 _LIBCPP_INLINE_VISIBILITY void __invalidate_all_iterators(); 2370 void allocate(size_type __n); 2371 void deallocate() _NOEXCEPT; 2372 _LIBCPP_INLINE_VISIBILITY 2373 static size_type __align_it(size_type __new_size) _NOEXCEPT 2374 {return __new_size + (__bits_per_word-1) & ~((size_type)__bits_per_word-1);}; 2375 _LIBCPP_INLINE_VISIBILITY size_type __recommend(size_type __new_size) const; 2376 _LIBCPP_INLINE_VISIBILITY void __construct_at_end(size_type __n, bool __x); 2377 template <class _ForwardIterator> 2378 typename enable_if 2379 < 2380 __is_forward_iterator<_ForwardIterator>::value, 2381 void 2382 >::type 2383 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last); 2384 void __append(size_type __n, const_reference __x); 2385 _LIBCPP_INLINE_VISIBILITY 2386 reference __make_ref(size_type __pos) _NOEXCEPT 2387 {return reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);} 2388 _LIBCPP_INLINE_VISIBILITY 2389 const_reference __make_ref(size_type __pos) const _NOEXCEPT 2390 {return const_reference(__begin_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);} 2391 _LIBCPP_INLINE_VISIBILITY 2392 iterator __make_iter(size_type __pos) _NOEXCEPT 2393 {return iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));} 2394 _LIBCPP_INLINE_VISIBILITY 2395 const_iterator __make_iter(size_type __pos) const _NOEXCEPT 2396 {return const_iterator(__begin_ + __pos / __bits_per_word, static_cast<unsigned>(__pos % __bits_per_word));} 2397 _LIBCPP_INLINE_VISIBILITY 2398 iterator __const_iterator_cast(const_iterator __p) _NOEXCEPT 2399 {return begin() + (__p - cbegin());} 2400 2401 _LIBCPP_INLINE_VISIBILITY 2402 void __copy_assign_alloc(const vector& __v) 2403 {__copy_assign_alloc(__v, integral_constant<bool, 2404 __storage_traits::propagate_on_container_copy_assignment::value>());} 2405 _LIBCPP_INLINE_VISIBILITY 2406 void __copy_assign_alloc(const vector& __c, true_type) 2407 { 2408 if (__alloc() != __c.__alloc()) 2409 deallocate(); 2410 __alloc() = __c.__alloc(); 2411 } 2412 2413 _LIBCPP_INLINE_VISIBILITY 2414 void __copy_assign_alloc(const vector&, false_type) 2415 {} 2416 2417 void __move_assign(vector& __c, false_type); 2418 void __move_assign(vector& __c, true_type) 2419 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value); 2420 _LIBCPP_INLINE_VISIBILITY 2421 void __move_assign_alloc(vector& __c) 2422 _NOEXCEPT_( 2423 !__storage_traits::propagate_on_container_move_assignment::value || 2424 is_nothrow_move_assignable<allocator_type>::value) 2425 {__move_assign_alloc(__c, integral_constant<bool, 2426 __storage_traits::propagate_on_container_move_assignment::value>());} 2427 _LIBCPP_INLINE_VISIBILITY 2428 void __move_assign_alloc(vector& __c, true_type) 2429 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) 2430 { 2431 __alloc() = _VSTD::move(__c.__alloc()); 2432 } 2433 2434 _LIBCPP_INLINE_VISIBILITY 2435 void __move_assign_alloc(vector&, false_type) 2436 _NOEXCEPT 2437 {} 2438 2439 size_t __hash_code() const _NOEXCEPT; 2440 2441 friend class __bit_reference<vector>; 2442 friend class __bit_const_reference<vector>; 2443 friend class __bit_iterator<vector, false>; 2444 friend class __bit_iterator<vector, true>; 2445 friend struct __bit_array<vector>; 2446 friend struct _LIBCPP_TYPE_VIS_ONLY hash<vector>; 2447}; 2448 2449template <class _Allocator> 2450inline _LIBCPP_INLINE_VISIBILITY 2451void 2452vector<bool, _Allocator>::__invalidate_all_iterators() 2453{ 2454} 2455 2456// Allocate space for __n objects 2457// throws length_error if __n > max_size() 2458// throws (probably bad_alloc) if memory run out 2459// Precondition: __begin_ == __end_ == __cap() == 0 2460// Precondition: __n > 0 2461// Postcondition: capacity() == __n 2462// Postcondition: size() == 0 2463template <class _Allocator> 2464void 2465vector<bool, _Allocator>::allocate(size_type __n) 2466{ 2467 if (__n > max_size()) 2468 this->__throw_length_error(); 2469 __n = __external_cap_to_internal(__n); 2470 this->__begin_ = __storage_traits::allocate(this->__alloc(), __n); 2471 this->__size_ = 0; 2472 this->__cap() = __n; 2473} 2474 2475template <class _Allocator> 2476void 2477vector<bool, _Allocator>::deallocate() _NOEXCEPT 2478{ 2479 if (this->__begin_ != nullptr) 2480 { 2481 __storage_traits::deallocate(this->__alloc(), this->__begin_, __cap()); 2482 __invalidate_all_iterators(); 2483 this->__begin_ = nullptr; 2484 this->__size_ = this->__cap() = 0; 2485 } 2486} 2487 2488template <class _Allocator> 2489typename vector<bool, _Allocator>::size_type 2490vector<bool, _Allocator>::max_size() const _NOEXCEPT 2491{ 2492 size_type __amax = __storage_traits::max_size(__alloc()); 2493 size_type __nmax = numeric_limits<size_type>::max() / 2; // end() >= begin(), always 2494 if (__nmax / __bits_per_word <= __amax) 2495 return __nmax; 2496 return __internal_cap_to_external(__amax); 2497} 2498 2499// Precondition: __new_size > capacity() 2500template <class _Allocator> 2501inline _LIBCPP_INLINE_VISIBILITY 2502typename vector<bool, _Allocator>::size_type 2503vector<bool, _Allocator>::__recommend(size_type __new_size) const 2504{ 2505 const size_type __ms = max_size(); 2506 if (__new_size > __ms) 2507 this->__throw_length_error(); 2508 const size_type __cap = capacity(); 2509 if (__cap >= __ms / 2) 2510 return __ms; 2511 return _VSTD::max(2*__cap, __align_it(__new_size)); 2512} 2513 2514// Default constructs __n objects starting at __end_ 2515// Precondition: __n > 0 2516// Precondition: size() + __n <= capacity() 2517// Postcondition: size() == size() + __n 2518template <class _Allocator> 2519inline _LIBCPP_INLINE_VISIBILITY 2520void 2521vector<bool, _Allocator>::__construct_at_end(size_type __n, bool __x) 2522{ 2523 size_type __old_size = this->__size_; 2524 this->__size_ += __n; 2525 _VSTD::fill_n(__make_iter(__old_size), __n, __x); 2526} 2527 2528template <class _Allocator> 2529template <class _ForwardIterator> 2530typename enable_if 2531< 2532 __is_forward_iterator<_ForwardIterator>::value, 2533 void 2534>::type 2535vector<bool, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) 2536{ 2537 size_type __old_size = this->__size_; 2538 this->__size_ += _VSTD::distance(__first, __last); 2539 _VSTD::copy(__first, __last, __make_iter(__old_size)); 2540} 2541 2542template <class _Allocator> 2543inline _LIBCPP_INLINE_VISIBILITY 2544vector<bool, _Allocator>::vector() 2545 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) 2546 : __begin_(nullptr), 2547 __size_(0), 2548 __cap_alloc_(0) 2549{ 2550} 2551 2552template <class _Allocator> 2553inline _LIBCPP_INLINE_VISIBILITY 2554vector<bool, _Allocator>::vector(const allocator_type& __a) 2555#if _LIBCPP_STD_VER <= 14 2556 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value) 2557#else 2558 _NOEXCEPT 2559#endif 2560 : __begin_(nullptr), 2561 __size_(0), 2562 __cap_alloc_(0, static_cast<__storage_allocator>(__a)) 2563{ 2564} 2565 2566template <class _Allocator> 2567vector<bool, _Allocator>::vector(size_type __n) 2568 : __begin_(nullptr), 2569 __size_(0), 2570 __cap_alloc_(0) 2571{ 2572 if (__n > 0) 2573 { 2574 allocate(__n); 2575 __construct_at_end(__n, false); 2576 } 2577} 2578 2579#if _LIBCPP_STD_VER > 11 2580template <class _Allocator> 2581vector<bool, _Allocator>::vector(size_type __n, const allocator_type& __a) 2582 : __begin_(nullptr), 2583 __size_(0), 2584 __cap_alloc_(0, static_cast<__storage_allocator>(__a)) 2585{ 2586 if (__n > 0) 2587 { 2588 allocate(__n); 2589 __construct_at_end(__n, false); 2590 } 2591} 2592#endif 2593 2594template <class _Allocator> 2595vector<bool, _Allocator>::vector(size_type __n, const value_type& __x) 2596 : __begin_(nullptr), 2597 __size_(0), 2598 __cap_alloc_(0) 2599{ 2600 if (__n > 0) 2601 { 2602 allocate(__n); 2603 __construct_at_end(__n, __x); 2604 } 2605} 2606 2607template <class _Allocator> 2608vector<bool, _Allocator>::vector(size_type __n, const value_type& __x, const allocator_type& __a) 2609 : __begin_(nullptr), 2610 __size_(0), 2611 __cap_alloc_(0, static_cast<__storage_allocator>(__a)) 2612{ 2613 if (__n > 0) 2614 { 2615 allocate(__n); 2616 __construct_at_end(__n, __x); 2617 } 2618} 2619 2620template <class _Allocator> 2621template <class _InputIterator> 2622vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, 2623 typename enable_if<__is_input_iterator <_InputIterator>::value && 2624 !__is_forward_iterator<_InputIterator>::value>::type*) 2625 : __begin_(nullptr), 2626 __size_(0), 2627 __cap_alloc_(0) 2628{ 2629#ifndef _LIBCPP_NO_EXCEPTIONS 2630 try 2631 { 2632#endif // _LIBCPP_NO_EXCEPTIONS 2633 for (; __first != __last; ++__first) 2634 push_back(*__first); 2635#ifndef _LIBCPP_NO_EXCEPTIONS 2636 } 2637 catch (...) 2638 { 2639 if (__begin_ != nullptr) 2640 __storage_traits::deallocate(__alloc(), __begin_, __cap()); 2641 __invalidate_all_iterators(); 2642 throw; 2643 } 2644#endif // _LIBCPP_NO_EXCEPTIONS 2645} 2646 2647template <class _Allocator> 2648template <class _InputIterator> 2649vector<bool, _Allocator>::vector(_InputIterator __first, _InputIterator __last, const allocator_type& __a, 2650 typename enable_if<__is_input_iterator <_InputIterator>::value && 2651 !__is_forward_iterator<_InputIterator>::value>::type*) 2652 : __begin_(nullptr), 2653 __size_(0), 2654 __cap_alloc_(0, static_cast<__storage_allocator>(__a)) 2655{ 2656#ifndef _LIBCPP_NO_EXCEPTIONS 2657 try 2658 { 2659#endif // _LIBCPP_NO_EXCEPTIONS 2660 for (; __first != __last; ++__first) 2661 push_back(*__first); 2662#ifndef _LIBCPP_NO_EXCEPTIONS 2663 } 2664 catch (...) 2665 { 2666 if (__begin_ != nullptr) 2667 __storage_traits::deallocate(__alloc(), __begin_, __cap()); 2668 __invalidate_all_iterators(); 2669 throw; 2670 } 2671#endif // _LIBCPP_NO_EXCEPTIONS 2672} 2673 2674template <class _Allocator> 2675template <class _ForwardIterator> 2676vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, 2677 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*) 2678 : __begin_(nullptr), 2679 __size_(0), 2680 __cap_alloc_(0) 2681{ 2682 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last)); 2683 if (__n > 0) 2684 { 2685 allocate(__n); 2686 __construct_at_end(__first, __last); 2687 } 2688} 2689 2690template <class _Allocator> 2691template <class _ForwardIterator> 2692vector<bool, _Allocator>::vector(_ForwardIterator __first, _ForwardIterator __last, const allocator_type& __a, 2693 typename enable_if<__is_forward_iterator<_ForwardIterator>::value>::type*) 2694 : __begin_(nullptr), 2695 __size_(0), 2696 __cap_alloc_(0, static_cast<__storage_allocator>(__a)) 2697{ 2698 size_type __n = static_cast<size_type>(_VSTD::distance(__first, __last)); 2699 if (__n > 0) 2700 { 2701 allocate(__n); 2702 __construct_at_end(__first, __last); 2703 } 2704} 2705 2706#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2707 2708template <class _Allocator> 2709vector<bool, _Allocator>::vector(initializer_list<value_type> __il) 2710 : __begin_(nullptr), 2711 __size_(0), 2712 __cap_alloc_(0) 2713{ 2714 size_type __n = static_cast<size_type>(__il.size()); 2715 if (__n > 0) 2716 { 2717 allocate(__n); 2718 __construct_at_end(__il.begin(), __il.end()); 2719 } 2720} 2721 2722template <class _Allocator> 2723vector<bool, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a) 2724 : __begin_(nullptr), 2725 __size_(0), 2726 __cap_alloc_(0, static_cast<__storage_allocator>(__a)) 2727{ 2728 size_type __n = static_cast<size_type>(__il.size()); 2729 if (__n > 0) 2730 { 2731 allocate(__n); 2732 __construct_at_end(__il.begin(), __il.end()); 2733 } 2734} 2735 2736#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS 2737 2738template <class _Allocator> 2739vector<bool, _Allocator>::~vector() 2740{ 2741 if (__begin_ != nullptr) 2742 __storage_traits::deallocate(__alloc(), __begin_, __cap()); 2743 __invalidate_all_iterators(); 2744} 2745 2746template <class _Allocator> 2747vector<bool, _Allocator>::vector(const vector& __v) 2748 : __begin_(nullptr), 2749 __size_(0), 2750 __cap_alloc_(0, __storage_traits::select_on_container_copy_construction(__v.__alloc())) 2751{ 2752 if (__v.size() > 0) 2753 { 2754 allocate(__v.size()); 2755 __construct_at_end(__v.begin(), __v.end()); 2756 } 2757} 2758 2759template <class _Allocator> 2760vector<bool, _Allocator>::vector(const vector& __v, const allocator_type& __a) 2761 : __begin_(nullptr), 2762 __size_(0), 2763 __cap_alloc_(0, __a) 2764{ 2765 if (__v.size() > 0) 2766 { 2767 allocate(__v.size()); 2768 __construct_at_end(__v.begin(), __v.end()); 2769 } 2770} 2771 2772template <class _Allocator> 2773vector<bool, _Allocator>& 2774vector<bool, _Allocator>::operator=(const vector& __v) 2775{ 2776 if (this != &__v) 2777 { 2778 __copy_assign_alloc(__v); 2779 if (__v.__size_) 2780 { 2781 if (__v.__size_ > capacity()) 2782 { 2783 deallocate(); 2784 allocate(__v.__size_); 2785 } 2786 _VSTD::copy(__v.__begin_, __v.__begin_ + __external_cap_to_internal(__v.__size_), __begin_); 2787 } 2788 __size_ = __v.__size_; 2789 } 2790 return *this; 2791} 2792 2793#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES 2794 2795template <class _Allocator> 2796inline _LIBCPP_INLINE_VISIBILITY 2797vector<bool, _Allocator>::vector(vector&& __v) 2798#if _LIBCPP_STD_VER > 14 2799 _NOEXCEPT 2800#else 2801 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) 2802#endif 2803 : __begin_(__v.__begin_), 2804 __size_(__v.__size_), 2805 __cap_alloc_(__v.__cap_alloc_) 2806{ 2807 __v.__begin_ = nullptr; 2808 __v.__size_ = 0; 2809 __v.__cap() = 0; 2810} 2811 2812template <class _Allocator> 2813vector<bool, _Allocator>::vector(vector&& __v, const allocator_type& __a) 2814 : __begin_(nullptr), 2815 __size_(0), 2816 __cap_alloc_(0, __a) 2817{ 2818 if (__a == allocator_type(__v.__alloc())) 2819 { 2820 this->__begin_ = __v.__begin_; 2821 this->__size_ = __v.__size_; 2822 this->__cap() = __v.__cap(); 2823 __v.__begin_ = nullptr; 2824 __v.__cap() = __v.__size_ = 0; 2825 } 2826 else if (__v.size() > 0) 2827 { 2828 allocate(__v.size()); 2829 __construct_at_end(__v.begin(), __v.end()); 2830 } 2831} 2832 2833template <class _Allocator> 2834inline _LIBCPP_INLINE_VISIBILITY 2835vector<bool, _Allocator>& 2836vector<bool, _Allocator>::operator=(vector&& __v) 2837 _NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value)) 2838{ 2839 __move_assign(__v, integral_constant<bool, 2840 __storage_traits::propagate_on_container_move_assignment::value>()); 2841 return *this; 2842} 2843 2844template <class _Allocator> 2845void 2846vector<bool, _Allocator>::__move_assign(vector& __c, false_type) 2847{ 2848 if (__alloc() != __c.__alloc()) 2849 assign(__c.begin(), __c.end()); 2850 else 2851 __move_assign(__c, true_type()); 2852} 2853 2854template <class _Allocator> 2855void 2856vector<bool, _Allocator>::__move_assign(vector& __c, true_type) 2857 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) 2858{ 2859 deallocate(); 2860 __move_assign_alloc(__c); 2861 this->__begin_ = __c.__begin_; 2862 this->__size_ = __c.__size_; 2863 this->__cap() = __c.__cap(); 2864 __c.__begin_ = nullptr; 2865 __c.__cap() = __c.__size_ = 0; 2866} 2867 2868#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES 2869 2870template <class _Allocator> 2871void 2872vector<bool, _Allocator>::assign(size_type __n, const value_type& __x) 2873{ 2874 __size_ = 0; 2875 if (__n > 0) 2876 { 2877 size_type __c = capacity(); 2878 if (__n <= __c) 2879 __size_ = __n; 2880 else 2881 { 2882 vector __v(__alloc()); 2883 __v.reserve(__recommend(__n)); 2884 __v.__size_ = __n; 2885 swap(__v); 2886 } 2887 _VSTD::fill_n(begin(), __n, __x); 2888 } 2889} 2890 2891template <class _Allocator> 2892template <class _InputIterator> 2893typename enable_if 2894< 2895 __is_input_iterator<_InputIterator>::value && 2896 !__is_forward_iterator<_InputIterator>::value, 2897 void 2898>::type 2899vector<bool, _Allocator>::assign(_InputIterator __first, _InputIterator __last) 2900{ 2901 clear(); 2902 for (; __first != __last; ++__first) 2903 push_back(*__first); 2904} 2905 2906template <class _Allocator> 2907template <class _ForwardIterator> 2908typename enable_if 2909< 2910 __is_forward_iterator<_ForwardIterator>::value, 2911 void 2912>::type 2913vector<bool, _Allocator>::assign(_ForwardIterator __first, _ForwardIterator __last) 2914{ 2915 clear(); 2916 difference_type __n = _VSTD::distance(__first, __last); 2917 if (__n) 2918 { 2919 if (__n > capacity()) 2920 { 2921 deallocate(); 2922 allocate(__n); 2923 } 2924 __construct_at_end(__first, __last); 2925 } 2926} 2927 2928template <class _Allocator> 2929void 2930vector<bool, _Allocator>::reserve(size_type __n) 2931{ 2932 if (__n > capacity()) 2933 { 2934 vector __v(this->__alloc()); 2935 __v.allocate(__n); 2936 __v.__construct_at_end(this->begin(), this->end()); 2937 swap(__v); 2938 __invalidate_all_iterators(); 2939 } 2940} 2941 2942template <class _Allocator> 2943void 2944vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT 2945{ 2946 if (__external_cap_to_internal(size()) > __cap()) 2947 { 2948#ifndef _LIBCPP_NO_EXCEPTIONS 2949 try 2950 { 2951#endif // _LIBCPP_NO_EXCEPTIONS 2952 vector(*this, allocator_type(__alloc())).swap(*this); 2953#ifndef _LIBCPP_NO_EXCEPTIONS 2954 } 2955 catch (...) 2956 { 2957 } 2958#endif // _LIBCPP_NO_EXCEPTIONS 2959 } 2960} 2961 2962template <class _Allocator> 2963typename vector<bool, _Allocator>::reference 2964vector<bool, _Allocator>::at(size_type __n) 2965{ 2966 if (__n >= size()) 2967 this->__throw_out_of_range(); 2968 return (*this)[__n]; 2969} 2970 2971template <class _Allocator> 2972typename vector<bool, _Allocator>::const_reference 2973vector<bool, _Allocator>::at(size_type __n) const 2974{ 2975 if (__n >= size()) 2976 this->__throw_out_of_range(); 2977 return (*this)[__n]; 2978} 2979 2980template <class _Allocator> 2981void 2982vector<bool, _Allocator>::push_back(const value_type& __x) 2983{ 2984 if (this->__size_ == this->capacity()) 2985 reserve(__recommend(this->__size_ + 1)); 2986 ++this->__size_; 2987 back() = __x; 2988} 2989 2990template <class _Allocator> 2991typename vector<bool, _Allocator>::iterator 2992vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __x) 2993{ 2994 iterator __r; 2995 if (size() < capacity()) 2996 { 2997 const_iterator __old_end = end(); 2998 ++__size_; 2999 _VSTD::copy_backward(__position, __old_end, end()); 3000 __r = __const_iterator_cast(__position); 3001 } 3002 else 3003 { 3004 vector __v(__alloc()); 3005 __v.reserve(__recommend(__size_ + 1)); 3006 __v.__size_ = __size_ + 1; 3007 __r = _VSTD::copy(cbegin(), __position, __v.begin()); 3008 _VSTD::copy_backward(__position, cend(), __v.end()); 3009 swap(__v); 3010 } 3011 *__r = __x; 3012 return __r; 3013} 3014 3015template <class _Allocator> 3016typename vector<bool, _Allocator>::iterator 3017vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const value_type& __x) 3018{ 3019 iterator __r; 3020 size_type __c = capacity(); 3021 if (__n <= __c && size() <= __c - __n) 3022 { 3023 const_iterator __old_end = end(); 3024 __size_ += __n; 3025 _VSTD::copy_backward(__position, __old_end, end()); 3026 __r = __const_iterator_cast(__position); 3027 } 3028 else 3029 { 3030 vector __v(__alloc()); 3031 __v.reserve(__recommend(__size_ + __n)); 3032 __v.__size_ = __size_ + __n; 3033 __r = _VSTD::copy(cbegin(), __position, __v.begin()); 3034 _VSTD::copy_backward(__position, cend(), __v.end()); 3035 swap(__v); 3036 } 3037 _VSTD::fill_n(__r, __n, __x); 3038 return __r; 3039} 3040 3041template <class _Allocator> 3042template <class _InputIterator> 3043typename enable_if 3044< 3045 __is_input_iterator <_InputIterator>::value && 3046 !__is_forward_iterator<_InputIterator>::value, 3047 typename vector<bool, _Allocator>::iterator 3048>::type 3049vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __first, _InputIterator __last) 3050{ 3051 difference_type __off = __position - begin(); 3052 iterator __p = __const_iterator_cast(__position); 3053 iterator __old_end = end(); 3054 for (; size() != capacity() && __first != __last; ++__first) 3055 { 3056 ++this->__size_; 3057 back() = *__first; 3058 } 3059 vector __v(__alloc()); 3060 if (__first != __last) 3061 { 3062#ifndef _LIBCPP_NO_EXCEPTIONS 3063 try 3064 { 3065#endif // _LIBCPP_NO_EXCEPTIONS 3066 __v.assign(__first, __last); 3067 difference_type __old_size = static_cast<difference_type>(__old_end - begin()); 3068 difference_type __old_p = __p - begin(); 3069 reserve(__recommend(size() + __v.size())); 3070 __p = begin() + __old_p; 3071 __old_end = begin() + __old_size; 3072#ifndef _LIBCPP_NO_EXCEPTIONS 3073 } 3074 catch (...) 3075 { 3076 erase(__old_end, end()); 3077 throw; 3078 } 3079#endif // _LIBCPP_NO_EXCEPTIONS 3080 } 3081 __p = _VSTD::rotate(__p, __old_end, end()); 3082 insert(__p, __v.begin(), __v.end()); 3083 return begin() + __off; 3084} 3085 3086template <class _Allocator> 3087template <class _ForwardIterator> 3088typename enable_if 3089< 3090 __is_forward_iterator<_ForwardIterator>::value, 3091 typename vector<bool, _Allocator>::iterator 3092>::type 3093vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last) 3094{ 3095 difference_type __n = _VSTD::distance(__first, __last); 3096 iterator __r; 3097 size_type __c = capacity(); 3098 if (__n <= __c && size() <= __c - __n) 3099 { 3100 const_iterator __old_end = end(); 3101 __size_ += __n; 3102 _VSTD::copy_backward(__position, __old_end, end()); 3103 __r = __const_iterator_cast(__position); 3104 } 3105 else 3106 { 3107 vector __v(__alloc()); 3108 __v.reserve(__recommend(__size_ + __n)); 3109 __v.__size_ = __size_ + __n; 3110 __r = _VSTD::copy(cbegin(), __position, __v.begin()); 3111 _VSTD::copy_backward(__position, cend(), __v.end()); 3112 swap(__v); 3113 } 3114 _VSTD::copy(__first, __last, __r); 3115 return __r; 3116} 3117 3118template <class _Allocator> 3119inline _LIBCPP_INLINE_VISIBILITY 3120typename vector<bool, _Allocator>::iterator 3121vector<bool, _Allocator>::erase(const_iterator __position) 3122{ 3123 iterator __r = __const_iterator_cast(__position); 3124 _VSTD::copy(__position + 1, this->cend(), __r); 3125 --__size_; 3126 return __r; 3127} 3128 3129template <class _Allocator> 3130typename vector<bool, _Allocator>::iterator 3131vector<bool, _Allocator>::erase(const_iterator __first, const_iterator __last) 3132{ 3133 iterator __r = __const_iterator_cast(__first); 3134 difference_type __d = __last - __first; 3135 _VSTD::copy(__last, this->cend(), __r); 3136 __size_ -= __d; 3137 return __r; 3138} 3139 3140template <class _Allocator> 3141void 3142vector<bool, _Allocator>::swap(vector& __x) 3143#if _LIBCPP_STD_VER >= 14 3144 _NOEXCEPT 3145#else 3146 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 3147 __is_nothrow_swappable<allocator_type>::value) 3148#endif 3149{ 3150 _VSTD::swap(this->__begin_, __x.__begin_); 3151 _VSTD::swap(this->__size_, __x.__size_); 3152 _VSTD::swap(this->__cap(), __x.__cap()); 3153 __swap_allocator(this->__alloc(), __x.__alloc(), 3154 integral_constant<bool, __alloc_traits::propagate_on_container_swap::value>()); 3155} 3156 3157template <class _Allocator> 3158void 3159vector<bool, _Allocator>::resize(size_type __sz, value_type __x) 3160{ 3161 size_type __cs = size(); 3162 if (__cs < __sz) 3163 { 3164 iterator __r; 3165 size_type __c = capacity(); 3166 size_type __n = __sz - __cs; 3167 if (__n <= __c && __cs <= __c - __n) 3168 { 3169 __r = end(); 3170 __size_ += __n; 3171 } 3172 else 3173 { 3174 vector __v(__alloc()); 3175 __v.reserve(__recommend(__size_ + __n)); 3176 __v.__size_ = __size_ + __n; 3177 __r = _VSTD::copy(cbegin(), cend(), __v.begin()); 3178 swap(__v); 3179 } 3180 _VSTD::fill_n(__r, __n, __x); 3181 } 3182 else 3183 __size_ = __sz; 3184} 3185 3186template <class _Allocator> 3187void 3188vector<bool, _Allocator>::flip() _NOEXCEPT 3189{ 3190 // do middle whole words 3191 size_type __n = __size_; 3192 __storage_pointer __p = __begin_; 3193 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) 3194 *__p = ~*__p; 3195 // do last partial word 3196 if (__n > 0) 3197 { 3198 __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 3199 __storage_type __b = *__p & __m; 3200 *__p &= ~__m; 3201 *__p |= ~__b & __m; 3202 } 3203} 3204 3205template <class _Allocator> 3206bool 3207vector<bool, _Allocator>::__invariants() const 3208{ 3209 if (this->__begin_ == nullptr) 3210 { 3211 if (this->__size_ != 0 || this->__cap() != 0) 3212 return false; 3213 } 3214 else 3215 { 3216 if (this->__cap() == 0) 3217 return false; 3218 if (this->__size_ > this->capacity()) 3219 return false; 3220 } 3221 return true; 3222} 3223 3224template <class _Allocator> 3225size_t 3226vector<bool, _Allocator>::__hash_code() const _NOEXCEPT 3227{ 3228 size_t __h = 0; 3229 // do middle whole words 3230 size_type __n = __size_; 3231 __storage_pointer __p = __begin_; 3232 for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) 3233 __h ^= *__p; 3234 // do last partial word 3235 if (__n > 0) 3236 { 3237 const __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); 3238 __h ^= *__p & __m; 3239 } 3240 return __h; 3241} 3242 3243template <class _Allocator> 3244struct _LIBCPP_TYPE_VIS_ONLY hash<vector<bool, _Allocator> > 3245 : public unary_function<vector<bool, _Allocator>, size_t> 3246{ 3247 _LIBCPP_INLINE_VISIBILITY 3248 size_t operator()(const vector<bool, _Allocator>& __vec) const _NOEXCEPT 3249 {return __vec.__hash_code();} 3250}; 3251 3252template <class _Tp, class _Allocator> 3253inline _LIBCPP_INLINE_VISIBILITY 3254bool 3255operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) 3256{ 3257 const typename vector<_Tp, _Allocator>::size_type __sz = __x.size(); 3258 return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin()); 3259} 3260 3261template <class _Tp, class _Allocator> 3262inline _LIBCPP_INLINE_VISIBILITY 3263bool 3264operator!=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) 3265{ 3266 return !(__x == __y); 3267} 3268 3269template <class _Tp, class _Allocator> 3270inline _LIBCPP_INLINE_VISIBILITY 3271bool 3272operator< (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) 3273{ 3274 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end()); 3275} 3276 3277template <class _Tp, class _Allocator> 3278inline _LIBCPP_INLINE_VISIBILITY 3279bool 3280operator> (const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) 3281{ 3282 return __y < __x; 3283} 3284 3285template <class _Tp, class _Allocator> 3286inline _LIBCPP_INLINE_VISIBILITY 3287bool 3288operator>=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) 3289{ 3290 return !(__x < __y); 3291} 3292 3293template <class _Tp, class _Allocator> 3294inline _LIBCPP_INLINE_VISIBILITY 3295bool 3296operator<=(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y) 3297{ 3298 return !(__y < __x); 3299} 3300 3301template <class _Tp, class _Allocator> 3302inline _LIBCPP_INLINE_VISIBILITY 3303void 3304swap(vector<_Tp, _Allocator>& __x, vector<_Tp, _Allocator>& __y) 3305 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 3306{ 3307 __x.swap(__y); 3308} 3309 3310_LIBCPP_END_NAMESPACE_STD 3311 3312#endif // _LIBCPP_VECTOR 3313