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