xref: /freebsd/contrib/llvm-project/libcxx/include/__split_buffer (revision 725a9f47324d42037db93c27ceb40d4956872f3e)
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 <__config>
17#include <__iterator/distance.h>
18#include <__iterator/iterator_traits.h>
19#include <__iterator/move_iterator.h>
20#include <__memory/allocate_at_least.h>
21#include <__memory/allocator.h>
22#include <__memory/allocator_traits.h>
23#include <__memory/compressed_pair.h>
24#include <__memory/pointer_traits.h>
25#include <__memory/swap_allocator.h>
26#include <__type_traits/add_lvalue_reference.h>
27#include <__type_traits/enable_if.h>
28#include <__type_traits/integral_constant.h>
29#include <__type_traits/is_nothrow_default_constructible.h>
30#include <__type_traits/is_nothrow_move_assignable.h>
31#include <__type_traits/is_nothrow_move_constructible.h>
32#include <__type_traits/is_swappable.h>
33#include <__type_traits/is_trivially_destructible.h>
34#include <__type_traits/remove_reference.h>
35#include <__utility/forward.h>
36#include <__utility/move.h>
37#include <cstddef>
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
48// __split_buffer allocates a contiguous chunk of memory and stores objects in the range [__begin_, __end_).
49// It has uninitialized memory in the ranges  [__first_, __begin_) and [__end_, __end_cap_.first()). That allows
50// it to grow both in the front and back without having to move the data.
51
52template <class _Tp, class _Allocator = allocator<_Tp> >
53struct __split_buffer {
54public:
55  using value_type      = _Tp;
56  using allocator_type  = _Allocator;
57  using __alloc_rr      = __libcpp_remove_reference_t<allocator_type>;
58  using __alloc_traits  = allocator_traits<__alloc_rr>;
59  using reference       = value_type&;
60  using const_reference = const value_type&;
61  using size_type       = typename __alloc_traits::size_type;
62  using difference_type = typename __alloc_traits::difference_type;
63  using pointer         = typename __alloc_traits::pointer;
64  using const_pointer   = typename __alloc_traits::const_pointer;
65  using iterator        = pointer;
66  using const_iterator  = const_pointer;
67
68  pointer __first_;
69  pointer __begin_;
70  pointer __end_;
71  __compressed_pair<pointer, allocator_type> __end_cap_;
72
73  using __alloc_ref       = __add_lvalue_reference_t<allocator_type>;
74  using __alloc_const_ref = __add_lvalue_reference_t<allocator_type>;
75
76  __split_buffer(const __split_buffer&)            = delete;
77  __split_buffer& operator=(const __split_buffer&) = delete;
78
79  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
80      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
81      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) {}
82
83  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
84      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
85
86  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
87      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
88
89  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
90  __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
91
92  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c)
93      _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
94
95  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
96
97  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c)
98      _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
99                  is_nothrow_move_assignable<allocator_type>::value) ||
100                 !__alloc_traits::propagate_on_container_move_assignment::value);
101
102  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
103
104  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __end_cap_.second(); }
105  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT {
106    return __end_cap_.second();
107  }
108
109  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_.first(); }
110  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {
111    return __end_cap_.first();
112  }
113
114  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
115  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
116
117  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __end_; }
118  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __end_; }
119
120  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(__begin_); }
121
122  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {
123    return static_cast<size_type>(__end_ - __begin_);
124  }
125
126  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
127
128  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
129    return static_cast<size_type>(__end_cap() - __first_);
130  }
131
132  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
133    return static_cast<size_type>(__begin_ - __first_);
134  }
135
136  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
137    return static_cast<size_type>(__end_cap() - __end_);
138  }
139
140  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
141  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const { return *__begin_; }
142  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() { return *(__end_ - 1); }
143  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const { return *(__end_ - 1); }
144
145  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
146  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
147  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(const_reference __x);
148  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
149  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
150  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
151
152  template <class... _Args>
153  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
154
155  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() { __destruct_at_begin(__begin_ + 1); }
156  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() { __destruct_at_end(__end_ - 1); }
157
158  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
159  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
160
161  template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
162  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(_InputIter __first, _InputIter __last);
163
164  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
165  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
166  __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
167
168  template <class _Iterator, class _Sentinel>
169  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
170  __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
171
172  template <class _Iterator>
173  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
174  __construct_at_end_with_size(_Iterator __first, size_type __n);
175
176  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
177    __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
178  }
179
180  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
181  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
182
183  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
184    __destruct_at_end(__new_last, false_type());
185  }
186
187  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
188  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
189
190  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
191      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<__alloc_rr>::value);
192
193  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
194
195private:
196  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
197      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
198    __alloc() = std::move(__c.__alloc());
199  }
200
201  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
202
203  struct _ConstructTransaction {
204    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(
205        pointer* __p, size_type __n) _NOEXCEPT
206        : __pos_(*__p),
207          __end_(*__p + __n),
208          __dest_(__p) {}
209
210    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { *__dest_ = __pos_; }
211
212    pointer __pos_;
213    const pointer __end_;
214
215  private:
216    pointer* __dest_;
217  };
218};
219
220template <class _Tp, class _Allocator>
221_LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants() const {
222  if (__first_ == nullptr) {
223    if (__begin_ != nullptr)
224      return false;
225    if (__end_ != nullptr)
226      return false;
227    if (__end_cap() != nullptr)
228      return false;
229  } else {
230    if (__begin_ < __first_)
231      return false;
232    if (__end_ < __begin_)
233      return false;
234    if (__end_cap() < __end_)
235      return false;
236  }
237  return true;
238}
239
240//  Default constructs __n objects starting at __end_
241//  throws if construction throws
242//  Precondition:  __n > 0
243//  Precondition:  size() + __n <= capacity()
244//  Postcondition:  size() == size() + __n
245template <class _Tp, class _Allocator>
246_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
247  _ConstructTransaction __tx(&this->__end_, __n);
248  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
249    __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
250  }
251}
252
253//  Copy constructs __n objects starting at __end_ from __x
254//  throws if construction throws
255//  Precondition:  __n > 0
256//  Precondition:  size() + __n <= capacity()
257//  Postcondition:  size() == old size() + __n
258//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
259template <class _Tp, class _Allocator>
260_LIBCPP_CONSTEXPR_SINCE_CXX20 void
261__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
262  _ConstructTransaction __tx(&this->__end_, __n);
263  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
264    __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
265  }
266}
267
268template <class _Tp, class _Allocator>
269template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
270_LIBCPP_CONSTEXPR_SINCE_CXX20 void
271__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) {
272  __construct_at_end_with_sentinel(__first, __last);
273}
274
275template <class _Tp, class _Allocator>
276template <class _Iterator, class _Sentinel>
277_LIBCPP_CONSTEXPR_SINCE_CXX20 void
278__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
279  __alloc_rr& __a = this->__alloc();
280  for (; __first != __last; ++__first) {
281    if (__end_ == __end_cap()) {
282      size_type __old_cap = __end_cap() - __first_;
283      size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
284      __split_buffer __buf(__new_cap, 0, __a);
285      for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
286        __alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
287      swap(__buf);
288    }
289    __alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
290    ++this->__end_;
291  }
292}
293template <class _Tp, class _Allocator>
294template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
295_LIBCPP_CONSTEXPR_SINCE_CXX20 void
296__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) {
297  __construct_at_end_with_size(__first, std::distance(__first, __last));
298}
299
300template <class _Tp, class _Allocator>
301template <class _ForwardIterator>
302_LIBCPP_CONSTEXPR_SINCE_CXX20 void
303__split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
304  _ConstructTransaction __tx(&this->__end_, __n);
305  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
306    __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
307  }
308}
309
310template <class _Tp, class _Allocator>
311_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
312__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
313  while (__begin_ != __new_begin)
314    __alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
315}
316
317template <class _Tp, class _Allocator>
318_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
319__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) {
320  __begin_ = __new_begin;
321}
322
323template <class _Tp, class _Allocator>
324_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
325__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
326  while (__new_last != __end_)
327    __alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
328}
329
330template <class _Tp, class _Allocator>
331_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
332__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT {
333  __end_ = __new_last;
334}
335
336template <class _Tp, class _Allocator>
337_LIBCPP_CONSTEXPR_SINCE_CXX20
338__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
339    : __end_cap_(nullptr, __a) {
340  if (__cap == 0) {
341    __first_ = nullptr;
342  } else {
343    auto __allocation = std::__allocate_at_least(__alloc(), __cap);
344    __first_          = __allocation.ptr;
345    __cap             = __allocation.count;
346  }
347  __begin_ = __end_ = __first_ + __start;
348  __end_cap()       = __first_ + __cap;
349}
350
351template <class _Tp, class _Allocator>
352_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
353  clear();
354  if (__first_)
355    __alloc_traits::deallocate(__alloc(), __first_, capacity());
356}
357
358template <class _Tp, class _Allocator>
359_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
360    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
361    : __first_(std::move(__c.__first_)),
362      __begin_(std::move(__c.__begin_)),
363      __end_(std::move(__c.__end_)),
364      __end_cap_(std::move(__c.__end_cap_)) {
365  __c.__first_    = nullptr;
366  __c.__begin_    = nullptr;
367  __c.__end_      = nullptr;
368  __c.__end_cap() = nullptr;
369}
370
371template <class _Tp, class _Allocator>
372_LIBCPP_CONSTEXPR_SINCE_CXX20
373__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
374    : __end_cap_(nullptr, __a) {
375  if (__a == __c.__alloc()) {
376    __first_        = __c.__first_;
377    __begin_        = __c.__begin_;
378    __end_          = __c.__end_;
379    __end_cap()     = __c.__end_cap();
380    __c.__first_    = nullptr;
381    __c.__begin_    = nullptr;
382    __c.__end_      = nullptr;
383    __c.__end_cap() = nullptr;
384  } else {
385    auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
386    __first_          = __allocation.ptr;
387    __begin_ = __end_ = __first_;
388    __end_cap()       = __first_ + __allocation.count;
389    typedef move_iterator<iterator> _Ip;
390    __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
391  }
392}
393
394template <class _Tp, class _Allocator>
395_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>&
396__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
397    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
398                is_nothrow_move_assignable<allocator_type>::value) ||
399               !__alloc_traits::propagate_on_container_move_assignment::value) {
400  clear();
401  shrink_to_fit();
402  __first_    = __c.__first_;
403  __begin_    = __c.__begin_;
404  __end_      = __c.__end_;
405  __end_cap() = __c.__end_cap();
406  __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
407  __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
408  return *this;
409}
410
411template <class _Tp, class _Allocator>
412_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
413    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<__alloc_rr>::value) {
414  std::swap(__first_, __x.__first_);
415  std::swap(__begin_, __x.__begin_);
416  std::swap(__end_, __x.__end_);
417  std::swap(__end_cap(), __x.__end_cap());
418  std::__swap_allocator(__alloc(), __x.__alloc());
419}
420
421template <class _Tp, class _Allocator>
422_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
423  if (__n < capacity()) {
424    __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
425    __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
426    std::swap(__first_, __t.__first_);
427    std::swap(__begin_, __t.__begin_);
428    std::swap(__end_, __t.__end_);
429    std::swap(__end_cap(), __t.__end_cap());
430  }
431}
432
433template <class _Tp, class _Allocator>
434_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
435  if (capacity() > size()) {
436#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
437    try {
438#endif // _LIBCPP_HAS_NO_EXCEPTIONS
439      __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
440      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
441      __t.__end_ = __t.__begin_ + (__end_ - __begin_);
442      std::swap(__first_, __t.__first_);
443      std::swap(__begin_, __t.__begin_);
444      std::swap(__end_, __t.__end_);
445      std::swap(__end_cap(), __t.__end_cap());
446#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
447    } catch (...) {
448    }
449#endif // _LIBCPP_HAS_NO_EXCEPTIONS
450  }
451}
452
453template <class _Tp, class _Allocator>
454_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(const_reference __x) {
455  if (__begin_ == __first_) {
456    if (__end_ < __end_cap()) {
457      difference_type __d = __end_cap() - __end_;
458      __d                 = (__d + 1) / 2;
459      __begin_            = std::move_backward(__begin_, __end_, __end_ + __d);
460      __end_ += __d;
461    } else {
462      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
463      __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
464      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
465      std::swap(__first_, __t.__first_);
466      std::swap(__begin_, __t.__begin_);
467      std::swap(__end_, __t.__end_);
468      std::swap(__end_cap(), __t.__end_cap());
469    }
470  }
471  __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), __x);
472  --__begin_;
473}
474
475template <class _Tp, class _Allocator>
476_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) {
477  if (__begin_ == __first_) {
478    if (__end_ < __end_cap()) {
479      difference_type __d = __end_cap() - __end_;
480      __d                 = (__d + 1) / 2;
481      __begin_            = std::move_backward(__begin_, __end_, __end_ + __d);
482      __end_ += __d;
483    } else {
484      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
485      __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
486      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
487      std::swap(__first_, __t.__first_);
488      std::swap(__begin_, __t.__begin_);
489      std::swap(__end_, __t.__end_);
490      std::swap(__end_cap(), __t.__end_cap());
491    }
492  }
493  __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::move(__x));
494  --__begin_;
495}
496
497template <class _Tp, class _Allocator>
498_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
499__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) {
500  if (__end_ == __end_cap()) {
501    if (__begin_ > __first_) {
502      difference_type __d = __begin_ - __first_;
503      __d                 = (__d + 1) / 2;
504      __end_              = std::move(__begin_, __end_, __begin_ - __d);
505      __begin_ -= __d;
506    } else {
507      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
508      __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
509      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
510      std::swap(__first_, __t.__first_);
511      std::swap(__begin_, __t.__begin_);
512      std::swap(__end_, __t.__end_);
513      std::swap(__end_cap(), __t.__end_cap());
514    }
515  }
516  __alloc_traits::construct(__alloc(), std::__to_address(__end_), __x);
517  ++__end_;
518}
519
520template <class _Tp, class _Allocator>
521_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) {
522  if (__end_ == __end_cap()) {
523    if (__begin_ > __first_) {
524      difference_type __d = __begin_ - __first_;
525      __d                 = (__d + 1) / 2;
526      __end_              = std::move(__begin_, __end_, __begin_ - __d);
527      __begin_ -= __d;
528    } else {
529      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
530      __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
531      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
532      std::swap(__first_, __t.__first_);
533      std::swap(__begin_, __t.__begin_);
534      std::swap(__end_, __t.__end_);
535      std::swap(__end_cap(), __t.__end_cap());
536    }
537  }
538  __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::move(__x));
539  ++__end_;
540}
541
542template <class _Tp, class _Allocator>
543template <class... _Args>
544_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
545  if (__end_ == __end_cap()) {
546    if (__begin_ > __first_) {
547      difference_type __d = __begin_ - __first_;
548      __d                 = (__d + 1) / 2;
549      __end_              = std::move(__begin_, __end_, __begin_ - __d);
550      __begin_ -= __d;
551    } else {
552      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
553      __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
554      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
555      std::swap(__first_, __t.__first_);
556      std::swap(__begin_, __t.__begin_);
557      std::swap(__end_, __t.__end_);
558      std::swap(__end_cap(), __t.__end_cap());
559    }
560  }
561  __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
562  ++__end_;
563}
564
565template <class _Tp, class _Allocator>
566_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
567swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
568  __x.swap(__y);
569}
570
571_LIBCPP_END_NAMESPACE_STD
572
573_LIBCPP_POP_MACROS
574
575#endif // _LIBCPP___SPLIT_BUFFER
576