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