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