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