2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___SPLIT_BUFFER
11 #define _LIBCPP___SPLIT_BUFFER
13 #include <__cxx03/__algorithm/max.h>
14 #include <__cxx03/__algorithm/move.h>
15 #include <__cxx03/__algorithm/move_backward.h>
16 #include <__cxx03/__config>
17 #include <__cxx03/__iterator/distance.h>
18 #include <__cxx03/__iterator/iterator_traits.h>
19 #include <__cxx03/__iterator/move_iterator.h>
20 #include <__cxx03/__memory/allocate_at_least.h>
21 #include <__cxx03/__memory/allocator.h>
22 #include <__cxx03/__memory/allocator_traits.h>
23 #include <__cxx03/__memory/compressed_pair.h>
24 #include <__cxx03/__memory/pointer_traits.h>
25 #include <__cxx03/__memory/swap_allocator.h>
26 #include <__cxx03/__type_traits/add_lvalue_reference.h>
27 #include <__cxx03/__type_traits/conditional.h>
28 #include <__cxx03/__type_traits/enable_if.h>
29 #include <__cxx03/__type_traits/integral_constant.h>
30 #include <__cxx03/__type_traits/is_nothrow_assignable.h>
31 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
32 #include <__cxx03/__type_traits/is_swappable.h>
33 #include <__cxx03/__type_traits/is_trivially_destructible.h>
34 #include <__cxx03/__type_traits/is_trivially_relocatable.h>
35 #include <__cxx03/__type_traits/remove_reference.h>
36 #include <__cxx03/__utility/forward.h>
37 #include <__cxx03/__utility/move.h>
38 #include <__cxx03/cstddef>
40 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
41 # pragma GCC system_header
45 #include <__cxx03/__undef_macros>
47 _LIBCPP_BEGIN_NAMESPACE_STD
49 // __split_buffer allocates a contiguous chunk of memory and stores objects in the range [__begin_, __end_).
50 // It has uninitialized memory in the ranges [__first_, __begin_) and [__end_, __end_cap_.first()). That allows
51 // it to grow both in the front and back without having to move the data.
53 template <class _Tp, class _Allocator = allocator<_Tp> >
54 struct __split_buffer {
56 using value_type = _Tp;
57 using allocator_type = _Allocator;
58 using __alloc_rr = __libcpp_remove_reference_t<allocator_type>;
59 using __alloc_traits = allocator_traits<__alloc_rr>;
60 using reference = value_type&;
61 using const_reference = const value_type&;
62 using size_type = typename __alloc_traits::size_type;
63 using difference_type = typename __alloc_traits::difference_type;
64 using pointer = typename __alloc_traits::pointer;
65 using const_pointer = typename __alloc_traits::const_pointer;
66 using iterator = pointer;
67 using const_iterator = const_pointer;
69 // A __split_buffer contains the following members which may be trivially relocatable:
70 // - pointer: may be trivially relocatable, so it's checked
71 // - allocator_type: may be trivially relocatable, so it's checked
72 // __split_buffer doesn't have any self-references, so it's trivially relocatable if its members are.
73 using __trivially_relocatable = __conditional_t<
74 __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
81 __compressed_pair<pointer, allocator_type> __end_cap_;
83 using __alloc_ref = __add_lvalue_reference_t<allocator_type>;
84 using __alloc_const_ref = __add_lvalue_reference_t<allocator_type>;
86 __split_buffer(const __split_buffer&) = delete;
87 __split_buffer& operator=(const __split_buffer&) = delete;
89 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
90 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
91 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) {}
93 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
94 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
96 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
97 : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
99 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
100 __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
102 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c)
103 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
105 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
107 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c)
108 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
109 is_nothrow_move_assignable<allocator_type>::value) ||
110 !__alloc_traits::propagate_on_container_move_assignment::value);
112 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
114 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __end_cap_.second(); }
115 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT {
116 return __end_cap_.second();
119 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_.first(); }
120 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {
121 return __end_cap_.first();
124 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
125 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
127 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __end_; }
128 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __end_; }
130 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(__begin_); }
132 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {
133 return static_cast<size_type>(__end_ - __begin_);
136 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
138 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
139 return static_cast<size_type>(__end_cap() - __first_);
142 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
143 return static_cast<size_type>(__begin_ - __first_);
146 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
147 return static_cast<size_type>(__end_cap() - __end_);
150 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
151 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const { return *__begin_; }
152 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() { return *(__end_ - 1); }
153 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const { return *(__end_ - 1); }
155 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
156 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
157 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(const_reference __x);
158 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
159 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
160 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
162 template <class... _Args>
163 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
165 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() { __destruct_at_begin(__begin_ + 1); }
166 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() { __destruct_at_end(__end_ - 1); }
168 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
169 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
171 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
172 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(_InputIter __first, _InputIter __last);
174 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
175 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
176 __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
178 template <class _Iterator, class _Sentinel>
179 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
180 __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
182 template <class _Iterator>
183 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
184 __construct_at_end_with_size(_Iterator __first, size_type __n);
186 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
187 __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
190 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
191 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
193 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
194 __destruct_at_end(__new_last, false_type());
197 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
198 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
200 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
201 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__alloc_rr>);
203 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
206 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
207 _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
208 __alloc() = std::move(__c.__alloc());
211 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
213 struct _ConstructTransaction {
214 _LIBCPP_CONSTEXPR_SINCE_CXX20
215 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
220 _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { *__dest_ = __pos_; }
223 const pointer __end_;
230 template <class _Tp, class _Allocator>
231 _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants() const {
232 if (__first_ == nullptr) {
233 if (__begin_ != nullptr)
235 if (__end_ != nullptr)
237 if (__end_cap() != nullptr)
240 if (__begin_ < __first_)
242 if (__end_ < __begin_)
244 if (__end_cap() < __end_)
250 // Default constructs __n objects starting at __end_
251 // throws if construction throws
252 // Precondition: __n > 0
253 // Precondition: size() + __n <= capacity()
254 // Postcondition: size() == size() + __n
255 template <class _Tp, class _Allocator>
256 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
257 _ConstructTransaction __tx(&this->__end_, __n);
258 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
259 __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
263 // Copy constructs __n objects starting at __end_ from __x
264 // throws if construction throws
265 // Precondition: __n > 0
266 // Precondition: size() + __n <= capacity()
267 // Postcondition: size() == old size() + __n
268 // Postcondition: [i] == __x for all i in [size() - __n, __n)
269 template <class _Tp, class _Allocator>
270 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
271 __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
272 _ConstructTransaction __tx(&this->__end_, __n);
273 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
274 __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
278 template <class _Tp, class _Allocator>
279 template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
280 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
281 __split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) {
282 __construct_at_end_with_sentinel(__first, __last);
285 template <class _Tp, class _Allocator>
286 template <class _Iterator, class _Sentinel>
287 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
288 __split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
289 __alloc_rr& __a = this->__alloc();
290 for (; __first != __last; ++__first) {
291 if (__end_ == __end_cap()) {
292 size_type __old_cap = __end_cap() - __first_;
293 size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
294 __split_buffer __buf(__new_cap, 0, __a);
295 for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
296 __alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
299 __alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
303 template <class _Tp, class _Allocator>
304 template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
305 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
306 __split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) {
307 __construct_at_end_with_size(__first, std::distance(__first, __last));
310 template <class _Tp, class _Allocator>
311 template <class _ForwardIterator>
312 _LIBCPP_CONSTEXPR_SINCE_CXX20 void
313 __split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
314 _ConstructTransaction __tx(&this->__end_, __n);
315 for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
316 __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
320 template <class _Tp, class _Allocator>
321 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
322 __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
323 while (__begin_ != __new_begin)
324 __alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
327 template <class _Tp, class _Allocator>
328 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
329 __split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) {
330 __begin_ = __new_begin;
333 template <class _Tp, class _Allocator>
334 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
335 __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
336 while (__new_last != __end_)
337 __alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
340 template <class _Tp, class _Allocator>
341 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
342 __split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT {
346 template <class _Tp, class _Allocator>
347 _LIBCPP_CONSTEXPR_SINCE_CXX20
348 __split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
349 : __end_cap_(nullptr, __a) {
353 auto __allocation = std::__allocate_at_least(__alloc(), __cap);
354 __first_ = __allocation.ptr;
355 __cap = __allocation.count;
357 __begin_ = __end_ = __first_ + __start;
358 __end_cap() = __first_ + __cap;
361 template <class _Tp, class _Allocator>
362 _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
365 __alloc_traits::deallocate(__alloc(), __first_, capacity());
368 template <class _Tp, class _Allocator>
369 _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
370 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
371 : __first_(std::move(__c.__first_)),
372 __begin_(std::move(__c.__begin_)),
373 __end_(std::move(__c.__end_)),
374 __end_cap_(std::move(__c.__end_cap_)) {
375 __c.__first_ = nullptr;
376 __c.__begin_ = nullptr;
377 __c.__end_ = nullptr;
378 __c.__end_cap() = nullptr;
381 template <class _Tp, class _Allocator>
382 _LIBCPP_CONSTEXPR_SINCE_CXX20
383 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
384 : __end_cap_(nullptr, __a) {
385 if (__a == __c.__alloc()) {
386 __first_ = __c.__first_;
387 __begin_ = __c.__begin_;
389 __end_cap() = __c.__end_cap();
390 __c.__first_ = nullptr;
391 __c.__begin_ = nullptr;
392 __c.__end_ = nullptr;
393 __c.__end_cap() = nullptr;
395 auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
396 __first_ = __allocation.ptr;
397 __begin_ = __end_ = __first_;
398 __end_cap() = __first_ + __allocation.count;
399 typedef move_iterator<iterator> _Ip;
400 __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
404 template <class _Tp, class _Allocator>
405 _LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>&
406 __split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
407 _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
408 is_nothrow_move_assignable<allocator_type>::value) ||
409 !__alloc_traits::propagate_on_container_move_assignment::value) {
412 __first_ = __c.__first_;
413 __begin_ = __c.__begin_;
415 __end_cap() = __c.__end_cap();
416 __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
417 __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
421 template <class _Tp, class _Allocator>
422 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
423 _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__alloc_rr>) {
424 std::swap(__first_, __x.__first_);
425 std::swap(__begin_, __x.__begin_);
426 std::swap(__end_, __x.__end_);
427 std::swap(__end_cap(), __x.__end_cap());
428 std::__swap_allocator(__alloc(), __x.__alloc());
431 template <class _Tp, class _Allocator>
432 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
433 if (__n < capacity()) {
434 __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
435 __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
436 std::swap(__first_, __t.__first_);
437 std::swap(__begin_, __t.__begin_);
438 std::swap(__end_, __t.__end_);
439 std::swap(__end_cap(), __t.__end_cap());
443 template <class _Tp, class _Allocator>
444 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
445 if (capacity() > size()) {
446 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
448 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
449 __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
450 __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
451 __t.__end_ = __t.__begin_ + (__end_ - __begin_);
452 std::swap(__first_, __t.__first_);
453 std::swap(__begin_, __t.__begin_);
454 std::swap(__end_, __t.__end_);
455 std::swap(__end_cap(), __t.__end_cap());
456 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
459 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
463 template <class _Tp, class _Allocator>
464 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(const_reference __x) {
465 if (__begin_ == __first_) {
466 if (__end_ < __end_cap()) {
467 difference_type __d = __end_cap() - __end_;
469 __begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
472 size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
473 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
474 __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
475 std::swap(__first_, __t.__first_);
476 std::swap(__begin_, __t.__begin_);
477 std::swap(__end_, __t.__end_);
478 std::swap(__end_cap(), __t.__end_cap());
481 __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), __x);
485 template <class _Tp, class _Allocator>
486 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) {
487 if (__begin_ == __first_) {
488 if (__end_ < __end_cap()) {
489 difference_type __d = __end_cap() - __end_;
491 __begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
494 size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
495 __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
496 __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
497 std::swap(__first_, __t.__first_);
498 std::swap(__begin_, __t.__begin_);
499 std::swap(__end_, __t.__end_);
500 std::swap(__end_cap(), __t.__end_cap());
503 __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::move(__x));
507 template <class _Tp, class _Allocator>
508 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
509 __split_buffer<_Tp, _Allocator>::push_back(const_reference __x) {
510 if (__end_ == __end_cap()) {
511 if (__begin_ > __first_) {
512 difference_type __d = __begin_ - __first_;
514 __end_ = std::move(__begin_, __end_, __begin_ - __d);
517 size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
518 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
519 __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
520 std::swap(__first_, __t.__first_);
521 std::swap(__begin_, __t.__begin_);
522 std::swap(__end_, __t.__end_);
523 std::swap(__end_cap(), __t.__end_cap());
526 __alloc_traits::construct(__alloc(), std::__to_address(__end_), __x);
530 template <class _Tp, class _Allocator>
531 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) {
532 if (__end_ == __end_cap()) {
533 if (__begin_ > __first_) {
534 difference_type __d = __begin_ - __first_;
536 __end_ = std::move(__begin_, __end_, __begin_ - __d);
539 size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
540 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
541 __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
542 std::swap(__first_, __t.__first_);
543 std::swap(__begin_, __t.__begin_);
544 std::swap(__end_, __t.__end_);
545 std::swap(__end_cap(), __t.__end_cap());
548 __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::move(__x));
552 template <class _Tp, class _Allocator>
553 template <class... _Args>
554 _LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
555 if (__end_ == __end_cap()) {
556 if (__begin_ > __first_) {
557 difference_type __d = __begin_ - __first_;
559 __end_ = std::move(__begin_, __end_, __begin_ - __d);
562 size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
563 __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
564 __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
565 std::swap(__first_, __t.__first_);
566 std::swap(__begin_, __t.__begin_);
567 std::swap(__end_, __t.__end_);
568 std::swap(__end_cap(), __t.__end_cap());
571 __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
575 template <class _Tp, class _Allocator>
576 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
577 swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
581 _LIBCPP_END_NAMESPACE_STD
585 #endif // _LIBCPP___SPLIT_BUFFER