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