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___SPLIT_BUFFER
11#define _LIBCPP___SPLIT_BUFFER
12
13#include <__algorithm/max.h>
14#include <__algorithm/move.h>
15#include <__algorithm/move_backward.h>
16#include <__assert>
17#include <__config>
18#include <__iterator/distance.h>
19#include <__iterator/iterator_traits.h>
20#include <__iterator/move_iterator.h>
21#include <__memory/addressof.h>
22#include <__memory/allocate_at_least.h>
23#include <__memory/allocator.h>
24#include <__memory/allocator_traits.h>
25#include <__memory/compressed_pair.h>
26#include <__memory/pointer_traits.h>
27#include <__memory/swap_allocator.h>
28#include <__memory/uninitialized_algorithms.h>
29#include <__type_traits/conditional.h>
30#include <__type_traits/enable_if.h>
31#include <__type_traits/integral_constant.h>
32#include <__type_traits/is_nothrow_assignable.h>
33#include <__type_traits/is_nothrow_constructible.h>
34#include <__type_traits/is_relocatable.h>
35#include <__type_traits/is_swappable.h>
36#include <__type_traits/is_trivially_destructible.h>
37#include <__utility/forward.h>
38#include <__utility/move.h>
39
40#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
41# pragma GCC system_header
42#endif
43
44_LIBCPP_PUSH_MACROS
45#include <__undef_macros>
46
47_LIBCPP_BEGIN_NAMESPACE_STD
48
49template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
50class __split_buffer;
51
52template <class _SplitBuffer, class _Tp, class _Allocator>
53class __split_buffer_pointer_layout {
54protected:
55 using value_type = _Tp;
56 using allocator_type = _Allocator;
57 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;
58 using reference = value_type&;
59 using const_reference = const value_type&;
60 using size_type = typename __alloc_traits::size_type;
61 using difference_type = typename __alloc_traits::difference_type;
62 using pointer = typename __alloc_traits::pointer;
63 using const_pointer = typename __alloc_traits::const_pointer;
64 using iterator = pointer;
65 using const_iterator = const_pointer;
66 using __sentinel_type _LIBCPP_NODEBUG = pointer;
67
68public:
69 // Can't be defaulted due to _LIBCPP_COMPRESSED_PAIR not being an aggregate in C++03 and C++11.
70 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer_pointer_layout() : __back_cap_(nullptr) {}
71
72 _LIBCPP_CONSTEXPR_SINCE_CXX20
73 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer_pointer_layout(const allocator_type& __alloc)
74 : __back_cap_(nullptr), __alloc_(__alloc) {}
75
76 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __front_cap() _NOEXCEPT { return __front_cap_; }
77
78 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_pointer __front_cap() const _NOEXCEPT {
79 return __front_cap_;
80 }
81
82 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer begin() _NOEXCEPT { return __begin_; }
83
84 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_pointer begin() const _NOEXCEPT { return __begin_; }
85
86 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer end() _NOEXCEPT { return __end_; }
87
88 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer end() const _NOEXCEPT { return __end_; }
89
90 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT {
91 return static_cast<size_type>(__end_ - __begin_);
92 }
93
94 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __begin_ == __end_; }
95
96 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const _NOEXCEPT {
97 return static_cast<size_type>(__back_cap_ - __front_cap_);
98 }
99
100 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type& __get_allocator() _NOEXCEPT { return __alloc_; }
101
102 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type const& __get_allocator() const _NOEXCEPT {
103 return __alloc_;
104 }
105
106 // Returns the sentinel object directly. Should be used in conjunction with automatic type deduction,
107 // not explicit types.
108 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __sentinel_type __raw_sentinel() const _NOEXCEPT {
109 return __end_;
110 }
111 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __sentinel_type __raw_capacity() const _NOEXCEPT {
112 return __back_cap_;
113 }
114
115 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_data(pointer __new_first) _NOEXCEPT {
116 __front_cap_ = __new_first;
117 }
118
119 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
120 __set_valid_range(pointer __new_begin, pointer __new_end) _NOEXCEPT {
121 __begin_ = __new_begin;
122 __end_ = __new_end;
123 }
124
125 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
126 __set_valid_range(pointer __new_begin, size_type __new_size) _NOEXCEPT {
127 __begin_ = __new_begin;
128 __end_ = __begin_ + __new_size;
129 }
130
131 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_sentinel(pointer __new_end) _NOEXCEPT {
132 _LIBCPP_ASSERT_INTERNAL(__front_cap_ <= __new_end, "__new_end cannot precede __front_cap_");
133 __end_ = __new_end;
134 }
135
136 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_sentinel(size_type __new_size) _NOEXCEPT {
137 __end_ = __begin_ + __new_size;
138 }
139
140 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_capacity(size_type __new_capacity) _NOEXCEPT {
141 __back_cap_ = __front_cap_ + __new_capacity;
142 }
143
144 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_capacity(pointer __new_capacity) _NOEXCEPT {
145 __back_cap_ = __new_capacity;
146 }
147
148 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const _NOEXCEPT {
149 return static_cast<size_type>(__begin_ - __front_cap_);
150 }
151
152 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const _NOEXCEPT {
153 return static_cast<size_type>(__back_cap_ - __end_);
154 }
155
156 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT { return *(__end_ - 1); }
157
158 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT { return *(__end_ - 1); }
159
160 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
161 __swap_without_allocator(__split_buffer_pointer_layout& __other) _NOEXCEPT {
162 std::swap(__front_cap_, __other.__front_cap_);
163 std::swap(__begin_, __other.__begin_);
164 std::swap(__back_cap_, __other.__back_cap_);
165 std::swap(__end_, __other.__end_);
166 }
167
168 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer_pointer_layout& __other) _NOEXCEPT {
169 std::swap(__front_cap_, __other.__front_cap_);
170 std::swap(__begin_, __other.__begin_);
171 std::swap(__back_cap_, __other.__back_cap_);
172 std::swap(__end_, __other.__end_);
173 std::__swap_allocator(__alloc_, __other.__alloc_);
174 }
175
176 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __reset() _NOEXCEPT {
177 __front_cap_ = nullptr;
178 __begin_ = nullptr;
179 __end_ = nullptr;
180 __back_cap_ = nullptr;
181 }
182
183 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
184 __copy_without_alloc(__split_buffer_pointer_layout const& __other)
185 _NOEXCEPT_(is_nothrow_copy_assignable<pointer>::value) {
186 __front_cap_ = __other.__front_cap_;
187 __begin_ = __other.__begin_;
188 __end_ = __other.__end_;
189 __back_cap_ = __other.__back_cap_;
190 }
191
192 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
193 __swap_layouts(pointer& __begin, pointer& __end, pointer& __back_capacity) {
194 using std::swap;
195 swap(__begin_, __begin);
196 swap(__end_, __end);
197 swap(__back_cap_, __back_capacity);
198 }
199
200 /// Relocates the objects in the range `[__first, __last)` to the front of the buffer, then swaps
201 /// `__first`, `__last`, and `__capacity` with `__begin_`, `__end_`, and `__back_cap_`,
202 /// respectively.
203 ///
204 /// Precondition: `__front_spare() == __last - __first`.
205 /// Exceptions: This function has a strong exception guarantee.
206 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
207 __relocate(pointer& __first, pointer& __last, pointer& __capacity) {
208 auto __size = __last - __first;
209 auto __new_begin = __begin_ - __size;
210 std::__uninitialized_allocator_relocate(
211 __alloc_, std::__to_address(__first), std::__to_address(__last), std::__to_address(__new_begin));
212 __begin_ = __new_begin;
213 __last = __first;
214
215 __swap_layouts(begin&: __first, end&: __last, back_capacity&: __capacity);
216 __front_cap_ = __begin_;
217 }
218
219 /// Relocates the objects in the range `[__first, __pivot)` to the front of the buffer, the
220 /// objects in `[__pivot, __last)` into the back of the buffer, then swaps `__first`, `__last`,
221 /// and `__capacity` with `__begin_`, `__end_`, and `__back_cap_`, respectively.
222 ///
223 /// Preconditions:
224 /// * `__front_spare() == __pivot - __first`
225 /// * `__back_spare() == __last - __pivot`
226 /// Exceptions: This function has a strong exception guarantee if `__first == __pivot` or
227 /// `__last == __pivot`.
228 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer
229 __relocate_with_pivot(pointer __pivot, pointer& __first, pointer& __last, pointer& __capacity) {
230 pointer __result = __begin_;
231
232 // Relocate [__p, __last) first to avoid having a hole in [__first, __last)
233 // in case something in [__first, __p) throws.
234 std::__uninitialized_allocator_relocate(
235 __alloc_, std::__to_address(__pivot), std::__to_address(__last), std::__to_address(__end_));
236 auto __relocated_so_far = __last - __pivot;
237 __end_ += __relocated_so_far;
238 __last = __pivot; // The objects in [__p, __last) have been destroyed by relocating them.
239
240 auto __new_begin = __begin_ - (__pivot - __first);
241 std::__uninitialized_allocator_relocate(
242 __alloc_, std::__to_address(__first), std::__to_address(__pivot), std::__to_address(__new_begin));
243 __begin_ = __new_begin;
244
245 __last = __first; // All the objects have been destroyed by relocating them.
246 __swap_layouts(begin&: __first, end&: __last, back_capacity&: __capacity);
247 __front_cap_ = __begin_;
248 return __result;
249 }
250
251private:
252 pointer __front_cap_ = nullptr;
253 pointer __begin_ = nullptr;
254 pointer __end_ = nullptr;
255 _LIBCPP_COMPRESSED_PAIR(pointer, __back_cap_, allocator_type, __alloc_);
256
257 template <class, class, class>
258 friend class __split_buffer_pointer_layout;
259};
260
261template <class _SplitBuffer, class _Tp, class _Allocator>
262class __split_buffer_size_layout {
263protected:
264 using value_type = _Tp;
265 using allocator_type = _Allocator;
266 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;
267 using reference = value_type&;
268 using const_reference = const value_type&;
269 using size_type = typename __alloc_traits::size_type;
270 using difference_type = typename __alloc_traits::difference_type;
271 using pointer = typename __alloc_traits::pointer;
272 using const_pointer = typename __alloc_traits::const_pointer;
273 using iterator = pointer;
274 using const_iterator = const_pointer;
275 using __sentinel_type _LIBCPP_NODEBUG = size_type;
276
277public:
278 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer_size_layout() = default;
279
280 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer_size_layout(const allocator_type& __alloc)
281 : __alloc_(__alloc) {}
282
283 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer __front_cap() _NOEXCEPT { return __front_cap_; }
284
285 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_pointer __front_cap() const _NOEXCEPT {
286 return __front_cap_;
287 }
288
289 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer begin() _NOEXCEPT { return __begin_; }
290
291 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_pointer begin() const _NOEXCEPT { return __begin_; }
292
293 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer end() _NOEXCEPT { return __begin_ + __size_; }
294
295 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer end() const _NOEXCEPT { return __begin_ + __size_; }
296
297 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size_; }
298
299 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return __size_ == 0; }
300
301 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const _NOEXCEPT { return __cap_; }
302
303 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type& __get_allocator() _NOEXCEPT { return __alloc_; }
304
305 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI allocator_type const& __get_allocator() const _NOEXCEPT {
306 return __alloc_;
307 }
308
309 // Returns the sentinel object directly. Should be used in conjunction with automatic type deduction,
310 // not explicit types.
311 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __sentinel_type __raw_sentinel() const _NOEXCEPT {
312 return __size_;
313 }
314 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __sentinel_type __raw_capacity() const _NOEXCEPT {
315 return __cap_;
316 }
317
318 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_data(pointer __new_first) _NOEXCEPT {
319 __front_cap_ = __new_first;
320 }
321
322 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
323 __set_valid_range(pointer __new_begin, pointer __new_end) _NOEXCEPT {
324 __begin_ = __new_begin;
325 __set_sentinel(__new_end);
326 }
327
328 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
329 __set_valid_range(pointer __new_begin, size_type __new_size) _NOEXCEPT {
330 __begin_ = __new_begin;
331 __set_sentinel(__new_size);
332 }
333
334 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_sentinel(pointer __new_end) _NOEXCEPT {
335 _LIBCPP_ASSERT_INTERNAL(__front_cap_ <= __new_end, "__new_end cannot precede __front_cap_");
336 __size_ = __new_end - __begin_;
337 }
338
339 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_sentinel(size_type __new_size) _NOEXCEPT {
340 __size_ = __new_size;
341 }
342
343 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_capacity(size_type __new_capacity) _NOEXCEPT {
344 __cap_ = __new_capacity;
345 }
346
347 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __set_capacity(pointer __new_capacity) _NOEXCEPT {
348 __cap_ = __new_capacity - __begin_;
349 }
350
351 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const _NOEXCEPT {
352 return static_cast<size_type>(__begin_ - __front_cap_);
353 }
354
355 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const _NOEXCEPT {
356 // `__cap_ - __end_` tells us the total number of spares when in size-mode. We need to remove
357 // the __front_spare from the count.
358 return __cap_ - __size_ - __front_spare();
359 }
360
361 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT { return __begin_[__size_ - 1]; }
362
363 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
364 return __begin_[__size_ - 1];
365 }
366
367 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
368 __swap_without_allocator(__split_buffer_size_layout& __other) _NOEXCEPT {
369 std::swap(__front_cap_, __other.__front_cap_);
370 std::swap(__begin_, __other.__begin_);
371 std::swap(__cap_, __other.__cap_);
372 std::swap(__size_, __other.__size_);
373 }
374
375 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer_size_layout& __other) _NOEXCEPT {
376 std::swap(__front_cap_, __other.__front_cap_);
377 std::swap(__begin_, __other.__begin_);
378 std::swap(__cap_, __other.__cap_);
379 std::swap(__size_, __other.__size_);
380 std::__swap_allocator(__alloc_, __other.__alloc_);
381 }
382
383 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __reset() _NOEXCEPT {
384 __front_cap_ = nullptr;
385 __begin_ = nullptr;
386 __size_ = 0;
387 __cap_ = 0;
388 }
389
390 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
391 __copy_without_alloc(__split_buffer_size_layout const& __other)
392 _NOEXCEPT_(is_nothrow_copy_assignable<pointer>::value) {
393 __front_cap_ = __other.__front_cap_;
394 __begin_ = __other.__begin_;
395 __cap_ = __other.__cap_;
396 __size_ = __other.__size_;
397 }
398
399 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
400 __swap_layouts(pointer& __begin, size_type& __size, size_type& __capacity) {
401 using std::swap;
402 swap(__begin_, __begin);
403 swap(__size_, __size);
404 swap(__cap_, __capacity);
405 }
406
407 /// Relocates the objects in the range `[__first, __first + __n)` to the front of the buffer, then
408 /// swaps `__first`, `__n`, and `__capacity` with `__begin_`, `__size_`, and `__capacity_`,
409 /// respectively.
410 ///
411 /// Precondition: `__front_spare() == __n`.
412 /// Exceptions: This function has a strong exception guarantee.
413 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
414 __relocate(pointer& __first, size_type& __n, size_type& __capacity) {
415 auto __new_begin = __begin_ - __n;
416 auto __last = __first + __n;
417 std::__uninitialized_allocator_relocate(
418 __alloc_, std::__to_address(__first), std::__to_address(__last), std::__to_address(__new_begin));
419 __set_valid_range(__new_begin, __size_ + __n);
420
421 __n = 0;
422
423 __swap_layouts(begin&: __first, size&: __n, __capacity);
424 __front_cap_ = __begin_;
425 }
426
427 /// Relocates the objects in the range `[__first, __pivot)` to the front of the buffer, the
428 /// objects in `[__pivot, __first + __n)` to the back of the buffer, then swaps `__first`, `__n`,
429 /// and `__capacity` with `__begin_`, `__size_`, and `__capacity_`, respectively.
430 ///
431 /// Preconditions:
432 /// * `__front_spare() == __pivot - __first`
433 /// * `__back_spare() == __last - __pivot`
434 /// Exceptions: This function has a strong exception guarantee if `__first == __pivot` or
435 /// `__first + __n == __pivot`.
436 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer
437 __relocate_with_pivot(pointer __pivot, pointer& __first, size_type& __n, size_type& __capacity) {
438 pointer __result = __begin_;
439 pointer __last = __first + __n;
440
441 // We relocate elements in reverse order to ensure that a throwing relocation operation leaves the
442 // buffer in a valid state.
443 std::__uninitialized_allocator_relocate(
444 __alloc_, std::__to_address(__pivot), std::__to_address(__last), std::__to_address(end()));
445
446 auto const __relocated_so_far = __last - __pivot;
447 __size_ += __relocated_so_far;
448 __n -= __relocated_so_far; // The objects in [__pivot, __last) have been destroyed by relocating them.
449
450 auto __new_begin = __begin_ - __n;
451 std::__uninitialized_allocator_relocate(
452 __alloc_, std::__to_address(__first), std::__to_address(__pivot), std::__to_address(__new_begin));
453 __begin_ = __new_begin;
454 __size_ += __n;
455 __n = 0; // All the objects have been destroyed by relocating them.
456
457 __swap_layouts(begin&: __first, size&: __n, __capacity);
458 __front_cap_ = __begin_;
459 return __result;
460 }
461
462private:
463 pointer __front_cap_ = nullptr;
464 pointer __begin_ = nullptr;
465 size_type __size_ = 0;
466 size_type __cap_ = 0;
467 _LIBCPP_NO_UNIQUE_ADDRESS allocator_type __alloc_;
468
469 template <class, class, class>
470 friend class __split_buffer_size_layout;
471};
472
473// `__split_buffer` is a contiguous array data structure. It may hold spare capacity at both ends of
474// the sequence. This allows for a `__split_buffer` to grow from both the front and the back without
475// relocating its contents until it runs out of room. This characteristic sets it apart from
476// `std::vector`, which only holds spare capacity at its end. As such, `__split_buffer` is useful
477// for implementing both `std::vector` and `std::deque`.
478//
479// The sequence is stored as a contiguous chunk of memory delimited by the following "pointers" (`o` denotes
480// uninitialized memory and `x` denotes a valid object):
481//
482// |oooooooooooooooooooxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxoooooooooooooooooooooooo|
483// ^ ^ ^ ^
484// __front_cap_ __begin_ __end_ __back_cap_
485//
486// The range [__front_cap_, __begin_) contains uninitialized memory. It is referred to as the "front spare capacity".
487// The range [__begin_, __end_) contains valid objects. It is referred to as the "valid range".
488// The range [__end_, __back_cap_) contains uninitialized memory. It is referred to as the "back spare capacity".
489//
490// The layout of `__split_buffer` is determined by the `_Layout` template template parameter. This
491// `_Layout` allows the above pointers to be stored as different representations, such as integer
492// offsets. A layout class template must provide the following interface:
493//
494// template<class _Tp, class _Allocator, class _Layout>
495// class __layout {
496// protected:
497// using value_type = _Tp;
498// using allocator_type = _Allocator;
499// using __alloc_traits = allocator_traits<allocator_type>;
500// using reference = value_type&;
501// using const_reference = const value_type&;
502// using size_type = typename __alloc_traits::size_type;
503// using difference_type = typename __alloc_traits::difference_type;
504// using pointer = typename __alloc_traits::pointer;
505// using const_pointer = typename __alloc_traits::const_pointer;
506// using iterator = pointer;
507// using const_iterator = const_pointer;
508// using __sentinel_type = /* type that represents the layout's sentinel */;
509//
510// public:
511// __layout() = default;
512// explicit __layout(const allocator_type&);
513//
514// pointer __front_cap();
515// const_pointer __front_cap() const;
516//
517// pointer begin();
518// const_pointer begin() const;
519//
520// pointer end();
521// pointer end() const;
522//
523// size_type size() const;
524// bool empty() const;
525// size_type capacity() const;
526//
527// allocator_type& __get_allocator();
528// allocator_type const& __get_allocator() const;
529//
530// __sentinel_type __raw_sentinel() const;
531// __sentinel_type __raw_capacity() const;
532//
533// void __set_data(pointer);
534// void __set_valid_range(pointer __begin, pointer __end);
535// void __set_valid_range(pointer __begin, size_type __size);
536// void __set_sentinel(pointer __end);
537// void __set_sentinel(size_type __size);
538//
539// void __set_capacity(size_type __capacity);
540// void __set_capacity(pointer __capacity);
541//
542// size_type __front_spare() const;
543// size_type __back_spare() const;
544//
545// reference back();
546// const_reference back() const;
547//
548// template<class _OtherLayout>
549// void __swap_without_allocator(_OtherLayout&);
550// void swap(__layout&);
551//
552// void __reset();
553// void __copy_without_alloc(__layout const&);
554// };
555//
556template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
557class __split_buffer : _Layout<__split_buffer<_Tp, _Allocator, _Layout>, _Tp, _Allocator> {
558 using __base_type _LIBCPP_NODEBUG = _Layout<__split_buffer<_Tp, _Allocator, _Layout>, _Tp, _Allocator>;
559
560public:
561 using __base_type::__back_spare;
562 using __base_type::__copy_without_alloc;
563 using __base_type::__front_cap;
564 using __base_type::__front_spare;
565 using __base_type::__get_allocator;
566 using __base_type::__raw_capacity;
567 using __base_type::__raw_sentinel;
568 using __base_type::__relocate;
569 using __base_type::__relocate_with_pivot;
570 using __base_type::__reset;
571 using __base_type::__set_capacity;
572 using __base_type::__set_data;
573 using __base_type::__set_sentinel;
574 using __base_type::__set_valid_range;
575 using __base_type::__swap_layouts;
576
577 using typename __base_type::__alloc_traits;
578 using typename __base_type::allocator_type;
579 using typename __base_type::const_iterator;
580 using typename __base_type::const_pointer;
581 using typename __base_type::const_reference;
582 using typename __base_type::difference_type;
583 using typename __base_type::iterator;
584 using typename __base_type::pointer;
585 using typename __base_type::reference;
586 using typename __base_type::size_type;
587 using typename __base_type::value_type;
588
589 // A __split_buffer contains the following members which may be trivially relocatable:
590 // - pointer: may be trivially relocatable, so it's checked
591 // - allocator_type: may be trivially relocatable, so it's checked
592 // __split_buffer doesn't have any self-references, so it's trivially relocatable if its members are.
593 using __trivially_relocatable _LIBCPP_NODEBUG =
594 __conditional_t<__is_trivially_relocatable_v<pointer> && __is_trivially_relocatable_v<allocator_type>,
595 __split_buffer,
596 void>;
597
598 __split_buffer(const __split_buffer&) = delete;
599 __split_buffer& operator=(const __split_buffer&) = delete;
600
601 _LIBCPP_HIDE_FROM_ABI __split_buffer() = default;
602
603 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(allocator_type& __a) : __base_type(__a) {}
604
605 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const allocator_type& __a)
606 : __base_type(__a) {}
607
608 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
609 __split_buffer(size_type __cap, size_type __start, allocator_type& __a);
610
611 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c)
612 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
613
614 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const allocator_type& __a);
615
616 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c)
617 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
618 is_nothrow_move_assignable<allocator_type>::value) ||
619 !__alloc_traits::propagate_on_container_move_assignment::value);
620
621 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
622
623 using __base_type::back;
624 using __base_type::begin;
625 using __base_type::capacity;
626 using __base_type::empty;
627 using __base_type::end;
628 using __base_type::size;
629
630 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(begin()); }
631 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *begin(); }
632 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const { return *begin(); }
633
634 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
635
636 template <class... _Args>
637 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
638 template <class... _Args>
639 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
640
641 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() { __destruct_at_begin(begin() + 1); }
642 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() { __destruct_at_end(end() - 1); }
643
644 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
645 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
646
647 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
648 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
649 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
650
651 template <class _Iterator, class _Sentinel>
652 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
653 __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
654
655 template <class _Iterator>
656 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
657 __construct_at_end_with_size(_Iterator __first, size_type __n);
658
659 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
660 __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
661 }
662
663 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
664 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
665
666 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
667 __destruct_at_end(__new_last, false_type());
668 }
669
670 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
671 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
672
673 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
674 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>);
675
676 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const {
677 if (__front_cap() == nullptr) {
678 if (begin() != nullptr)
679 return false;
680
681 if (!empty())
682 return false;
683
684 if (capacity() != 0)
685 return false;
686
687 return true;
688 } else {
689 if (begin() < __front_cap())
690 return false;
691
692 if (capacity() < size())
693 return false;
694
695 if (end() < begin())
696 return false;
697
698 return true;
699 }
700 }
701
702 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
703 __swap_without_allocator(__split_buffer<value_type, allocator_type, _Layout>& __other) _NOEXCEPT {
704 __base_type::__swap_without_allocator(static_cast<__base_type&>(__other));
705 }
706
707private:
708 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
709 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
710 __get_allocator() = std::move(__c.__get_allocator());
711 }
712
713 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
714
715 struct _ConstructTransaction {
716 _LIBCPP_CONSTEXPR_SINCE_CXX20
717 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(__split_buffer* __parent, pointer __p, size_type __n) _NOEXCEPT
718 : __pos_(__p),
719 __end_(__p + __n),
720 __parent_(__parent) {}
721
722 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { __parent_->__set_sentinel(__pos_); }
723
724 pointer __pos_;
725 const pointer __end_;
726
727 private:
728 __split_buffer* __parent_;
729 };
730
731 template <class _T2, class _A2, template <class, class, class> class _L2>
732 friend class __split_buffer;
733};
734
735// Default constructs __n objects starting at `end()`
736// throws if construction throws
737// Precondition: __n > 0
738// Precondition: size() + __n <= capacity()
739// Postcondition: size() == size() + __n
740template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
741_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator, _Layout>::__construct_at_end(size_type __n) {
742 _ConstructTransaction __tx(this, end(), __n);
743 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
744 __alloc_traits::construct(__get_allocator(), std::__to_address(__tx.__pos_));
745 }
746}
747
748// Copy constructs __n objects starting at `end()` from __x
749// throws if construction throws
750// Precondition: __n > 0
751// Precondition: size() + __n <= capacity()
752// Postcondition: size() == old size() + __n
753// Postcondition: [i] == __x for all i in [size() - __n, __n)
754template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
755_LIBCPP_CONSTEXPR_SINCE_CXX20 void
756__split_buffer<_Tp, _Allocator, _Layout>::__construct_at_end(size_type __n, const_reference __x) {
757 _ConstructTransaction __tx(this, end(), __n);
758 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
759 __alloc_traits::construct(__get_allocator(), std::__to_address(__tx.__pos_), __x);
760 }
761}
762
763template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
764template <class _Iterator, class _Sentinel>
765_LIBCPP_CONSTEXPR_SINCE_CXX20 void
766__split_buffer<_Tp, _Allocator, _Layout>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
767 allocator_type& __a = __get_allocator();
768 for (; __first != __last; ++__first) {
769 if (__back_spare() == 0) {
770 size_type __old_cap = capacity();
771 size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
772 __split_buffer __buf(__new_cap, 0, __a);
773 pointer __buf_end = __buf.end();
774 pointer __end = end();
775 for (pointer __p = begin(); __p != __end; ++__p) {
776 __alloc_traits::construct(__buf.__get_allocator(), std::__to_address(__buf_end), std::move(*__p));
777 __buf.__set_sentinel(++__buf_end);
778 }
779 swap(x&: __buf);
780 }
781
782 __alloc_traits::construct(__a, std::__to_address(end()), *__first);
783 __set_sentinel(size() + 1);
784 }
785}
786
787template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
788template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
789_LIBCPP_CONSTEXPR_SINCE_CXX20 void
790__split_buffer<_Tp, _Allocator, _Layout>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) {
791 __construct_at_end_with_size(__first, std::distance(__first, __last));
792}
793
794template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
795template <class _ForwardIterator>
796_LIBCPP_CONSTEXPR_SINCE_CXX20 void
797__split_buffer<_Tp, _Allocator, _Layout>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
798 _ConstructTransaction __tx(this, end(), __n);
799 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
800 __alloc_traits::construct(__get_allocator(), std::__to_address(__tx.__pos_), *__first);
801 }
802}
803
804template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
805_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
806__split_buffer<_Tp, _Allocator, _Layout>::__destruct_at_begin(pointer __new_begin, false_type) {
807 pointer __begin = begin();
808 // Updating begin at every iteration is unnecessary because destruction can't throw.
809 while (__begin != __new_begin)
810 __alloc_traits::destroy(__get_allocator(), std::__to_address(__begin++));
811 __set_valid_range(__begin, end());
812}
813
814template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
815_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
816__split_buffer<_Tp, _Allocator, _Layout>::__destruct_at_begin(pointer __new_begin, true_type) {
817 __set_valid_range(__new_begin, end());
818}
819
820template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
821_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
822__split_buffer<_Tp, _Allocator, _Layout>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
823 pointer __end = end();
824 // Updating begin at every iteration is unnecessary because destruction can't throw.
825 while (__new_last != __end)
826 __alloc_traits::destroy(__get_allocator(), std::__to_address(--__end));
827 __set_sentinel(__end);
828}
829
830template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
831_LIBCPP_CONSTEXPR_SINCE_CXX20
832__split_buffer<_Tp, _Allocator, _Layout>::__split_buffer(size_type __cap, size_type __start, allocator_type& __a)
833 : __base_type(__a) {
834 _LIBCPP_ASSERT_INTERNAL(__cap >= __start, "can't have a start point outside the capacity");
835 if (__cap > 0) {
836 auto __allocation = std::__allocate_at_least(__get_allocator(), __cap);
837 __set_data(__allocation.ptr);
838 __cap = __allocation.count;
839 }
840
841 pointer __begin = __front_cap() + __start;
842 __set_valid_range(__begin, __begin);
843 __set_capacity(__cap);
844}
845
846template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
847_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator, _Layout>::~__split_buffer() {
848 clear();
849 if (__front_cap())
850 __alloc_traits::deallocate(__get_allocator(), __front_cap(), capacity());
851}
852
853template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
854_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator, _Layout>::__split_buffer(__split_buffer&& __c)
855 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
856 : __base_type(std::move(__c)) {
857 __c.__reset();
858}
859
860template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
861_LIBCPP_CONSTEXPR_SINCE_CXX20
862__split_buffer<_Tp, _Allocator, _Layout>::__split_buffer(__split_buffer&& __c, const allocator_type& __a)
863 : __base_type(__a) {
864 if (__a == __c.__get_allocator()) {
865 __set_data(__c.__front_cap());
866 __set_valid_range(__c.begin(), __c.end());
867 __set_capacity(__c.capacity());
868 __c.__reset();
869 } else {
870 auto __allocation = std::__allocate_at_least(__get_allocator(), __c.size());
871 __set_data(__allocation.ptr);
872 __set_valid_range(__front_cap(), __front_cap());
873 __set_capacity(__allocation.count);
874 typedef move_iterator<iterator> _Ip;
875 __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
876 }
877}
878
879template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
880_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator, _Layout>&
881__split_buffer<_Tp, _Allocator, _Layout>::operator=(__split_buffer&& __c)
882 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
883 is_nothrow_move_assignable<allocator_type>::value) ||
884 !__alloc_traits::propagate_on_container_move_assignment::value) {
885 clear();
886 shrink_to_fit();
887 __copy_without_alloc(__c);
888 __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
889 __c.__reset();
890 return *this;
891}
892
893template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
894_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator, _Layout>::swap(__split_buffer& __x)
895 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<allocator_type>) {
896 __base_type::swap(__x);
897}
898
899template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
900_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator, _Layout>::shrink_to_fit() _NOEXCEPT {
901 if (capacity() > size()) {
902#if _LIBCPP_HAS_EXCEPTIONS
903 try {
904#endif // _LIBCPP_HAS_EXCEPTIONS
905 __split_buffer<value_type, allocator_type, _Layout> __t(size(), 0, __get_allocator());
906 if (__t.capacity() < capacity()) {
907 __t.__construct_at_end(move_iterator<pointer>(begin()), move_iterator<pointer>(end()));
908 __t.__set_sentinel(size());
909 __swap_without_allocator(other&: __t);
910 }
911#if _LIBCPP_HAS_EXCEPTIONS
912 } catch (...) {
913 }
914#endif // _LIBCPP_HAS_EXCEPTIONS
915 }
916}
917
918template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
919template <class... _Args>
920_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator, _Layout>::emplace_front(_Args&&... __args) {
921 if (__front_spare() == 0) {
922 pointer __end = end();
923 if (__back_spare() > 0) {
924 // The elements are pressed up against the front of the buffer: we need to move them back a
925 // little bit to make `emplace_front` have amortised O(1) complexity.
926 difference_type __d = __back_spare();
927 __d = (__d + 1) / 2;
928 auto __new_end = __end + __d;
929 __set_valid_range(std::move_backward(begin(), __end, __new_end), __new_end);
930 } else {
931 size_type __c = std::max<size_type>(2 * capacity(), 1);
932 __split_buffer<value_type, allocator_type, _Layout> __t(__c, (__c + 3) / 4, __get_allocator());
933 __t.__construct_at_end(move_iterator<pointer>(begin()), move_iterator<pointer>(__end));
934 __base_type::__swap_without_allocator(__t);
935 }
936 }
937
938 __alloc_traits::construct(__get_allocator(), std::__to_address(begin() - 1), std::forward<_Args>(__args)...);
939 __set_valid_range(begin() - 1, size() + 1);
940}
941
942template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
943template <class... _Args>
944_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator, _Layout>::emplace_back(_Args&&... __args) {
945 pointer __end = end();
946 if (__back_spare() == 0) {
947 if (__front_spare() > 0) {
948 difference_type __d = __front_spare();
949 __d = (__d + 1) / 2;
950 __end = std::move(begin(), __end, begin() - __d);
951 __set_valid_range(begin() - __d, __end);
952 } else {
953 size_type __c = std::max<size_type>(2 * capacity(), 1);
954 __split_buffer<value_type, allocator_type, _Layout> __t(__c, __c / 4, __get_allocator());
955 __t.__construct_at_end(move_iterator<pointer>(begin()), move_iterator<pointer>(__end));
956 __base_type::__swap_without_allocator(__t);
957 }
958 }
959
960 __alloc_traits::construct(__get_allocator(), std::__to_address(__end), std::forward<_Args>(__args)...);
961 __set_sentinel(++__end);
962}
963
964template <class _Tp, class _Allocator, template <class, class, class> class _Layout>
965_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
966swap(__split_buffer<_Tp, _Allocator, _Layout>& __x, __split_buffer<_Tp, _Allocator, _Layout>& __y)
967 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
968 __x.swap(__y);
969}
970
971_LIBCPP_END_NAMESPACE_STD
972
973_LIBCPP_POP_MACROS
974
975#endif // _LIBCPP___SPLIT_BUFFER
976