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