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