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