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