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