• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <__cstddef/size_t.h>
18#include <__iterator/distance.h>
19#include <__iterator/iterator_traits.h>
20#include <__iterator/move_iterator.h>
21#include <__memory/allocate_at_least.h>
22#include <__memory/allocator.h>
23#include <__memory/allocator_traits.h>
24#include <__memory/compressed_pair.h>
25#include <__memory/pointer_traits.h>
26#include <__memory/swap_allocator.h>
27#include <__type_traits/add_lvalue_reference.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 <__type_traits/remove_reference.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
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_, __cap_). That allows
51// it to grow both in the front and back without having to move the data.
52
53template <class _Tp, class _Allocator = allocator<_Tp> >
54struct __split_buffer {
55public:
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;
68
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,
75      __split_buffer,
76      void>;
77
78  pointer __first_;
79  pointer __begin_;
80  pointer __end_;
81  _LIBCPP_COMPRESSED_PAIR(pointer, __cap_, allocator_type, __alloc_);
82
83  __split_buffer(const __split_buffer&)            = delete;
84  __split_buffer& operator=(const __split_buffer&) = delete;
85
86  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
87      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
88      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __cap_(nullptr) {}
89
90  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
91      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __cap_(nullptr), __alloc_(__a) {}
92
93  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
94      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __cap_(nullptr), __alloc_(__a) {}
95
96  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
97  __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
98
99  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c)
100      _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
101
102  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
103
104  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c)
105      _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
106                  is_nothrow_move_assignable<allocator_type>::value) ||
107                 !__alloc_traits::propagate_on_container_move_assignment::value);
108
109  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
110
111  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
112  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
113
114  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __end_; }
115  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __end_; }
116
117  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(__begin_); }
118
119  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {
120    return static_cast<size_type>(__end_ - __begin_);
121  }
122
123  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
124
125  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
126    return static_cast<size_type>(__cap_ - __first_);
127  }
128
129  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
130    return static_cast<size_type>(__begin_ - __first_);
131  }
132
133  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
134    return static_cast<size_type>(__cap_ - __end_);
135  }
136
137  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
138  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const { return *__begin_; }
139  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() { return *(__end_ - 1); }
140  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const { return *(__end_ - 1); }
141
142  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
143
144  template <class... _Args>
145  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_front(_Args&&... __args);
146  template <class... _Args>
147  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
148
149  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() { __destruct_at_begin(__begin_ + 1); }
150  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() { __destruct_at_end(__end_ - 1); }
151
152  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
153  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
154
155  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
156  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
157  __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
158
159  template <class _Iterator, class _Sentinel>
160  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
161  __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
162
163  template <class _Iterator>
164  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
165  __construct_at_end_with_size(_Iterator __first, size_type __n);
166
167  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
168    __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
169  }
170
171  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
172  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
173
174  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
175    __destruct_at_end(__new_last, false_type());
176  }
177
178  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
179  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
180
181  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
182      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__alloc_rr>);
183
184  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
185
186private:
187  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
188      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
189    __alloc_ = std::move(__c.__alloc_);
190  }
191
192  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
193
194  struct _ConstructTransaction {
195    _LIBCPP_CONSTEXPR_SINCE_CXX20
196    _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
197        : __pos_(*__p),
198          __end_(*__p + __n),
199          __dest_(__p) {}
200
201    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { *__dest_ = __pos_; }
202
203    pointer __pos_;
204    const pointer __end_;
205
206  private:
207    pointer* __dest_;
208  };
209};
210
211template <class _Tp, class _Allocator>
212_LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants() const {
213  if (__first_ == nullptr) {
214    if (__begin_ != nullptr)
215      return false;
216    if (__end_ != nullptr)
217      return false;
218    if (__cap_ != nullptr)
219      return false;
220  } else {
221    if (__begin_ < __first_)
222      return false;
223    if (__end_ < __begin_)
224      return false;
225    if (__cap_ < __end_)
226      return false;
227  }
228  return true;
229}
230
231//  Default constructs __n objects starting at __end_
232//  throws if construction throws
233//  Precondition:  __n > 0
234//  Precondition:  size() + __n <= capacity()
235//  Postcondition:  size() == size() + __n
236template <class _Tp, class _Allocator>
237_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
238  _ConstructTransaction __tx(&this->__end_, __n);
239  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
240    __alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_));
241  }
242}
243
244//  Copy constructs __n objects starting at __end_ from __x
245//  throws if construction throws
246//  Precondition:  __n > 0
247//  Precondition:  size() + __n <= capacity()
248//  Postcondition:  size() == old size() + __n
249//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
250template <class _Tp, class _Allocator>
251_LIBCPP_CONSTEXPR_SINCE_CXX20 void
252__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
253  _ConstructTransaction __tx(&this->__end_, __n);
254  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
255    __alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), __x);
256  }
257}
258
259template <class _Tp, class _Allocator>
260template <class _Iterator, class _Sentinel>
261_LIBCPP_CONSTEXPR_SINCE_CXX20 void
262__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
263  __alloc_rr& __a = __alloc_;
264  for (; __first != __last; ++__first) {
265    if (__end_ == __cap_) {
266      size_type __old_cap = __cap_ - __first_;
267      size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
268      __split_buffer __buf(__new_cap, 0, __a);
269      for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
270        __alloc_traits::construct(__buf.__alloc_, std::__to_address(__buf.__end_), std::move(*__p));
271      swap(__buf);
272    }
273    __alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
274    ++this->__end_;
275  }
276}
277template <class _Tp, class _Allocator>
278template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
279_LIBCPP_CONSTEXPR_SINCE_CXX20 void
280__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) {
281  __construct_at_end_with_size(__first, std::distance(__first, __last));
282}
283
284template <class _Tp, class _Allocator>
285template <class _ForwardIterator>
286_LIBCPP_CONSTEXPR_SINCE_CXX20 void
287__split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
288  _ConstructTransaction __tx(&this->__end_, __n);
289  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
290    __alloc_traits::construct(__alloc_, std::__to_address(__tx.__pos_), *__first);
291  }
292}
293
294template <class _Tp, class _Allocator>
295_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
296__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
297  while (__begin_ != __new_begin)
298    __alloc_traits::destroy(__alloc_, std::__to_address(__begin_++));
299}
300
301template <class _Tp, class _Allocator>
302_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
303__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) {
304  __begin_ = __new_begin;
305}
306
307template <class _Tp, class _Allocator>
308_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
309__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
310  while (__new_last != __end_)
311    __alloc_traits::destroy(__alloc_, std::__to_address(--__end_));
312}
313
314template <class _Tp, class _Allocator>
315_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
316__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT {
317  __end_ = __new_last;
318}
319
320template <class _Tp, class _Allocator>
321_LIBCPP_CONSTEXPR_SINCE_CXX20
322__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
323    : __cap_(nullptr), __alloc_(__a) {
324  if (__cap == 0) {
325    __first_ = nullptr;
326  } else {
327    auto __allocation = std::__allocate_at_least(__alloc_, __cap);
328    __first_          = __allocation.ptr;
329    __cap             = __allocation.count;
330  }
331  __begin_ = __end_ = __first_ + __start;
332  __cap_            = __first_ + __cap;
333}
334
335template <class _Tp, class _Allocator>
336_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
337  clear();
338  if (__first_)
339    __alloc_traits::deallocate(__alloc_, __first_, capacity());
340}
341
342template <class _Tp, class _Allocator>
343_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
344    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
345    : __first_(std::move(__c.__first_)),
346      __begin_(std::move(__c.__begin_)),
347      __end_(std::move(__c.__end_)),
348      __cap_(std::move(__c.__cap_)),
349      __alloc_(std::move(__c.__alloc_)) {
350  __c.__first_ = nullptr;
351  __c.__begin_ = nullptr;
352  __c.__end_   = nullptr;
353  __c.__cap_   = nullptr;
354}
355
356template <class _Tp, class _Allocator>
357_LIBCPP_CONSTEXPR_SINCE_CXX20
358__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
359    : __cap_(nullptr), __alloc_(__a) {
360  if (__a == __c.__alloc_) {
361    __first_     = __c.__first_;
362    __begin_     = __c.__begin_;
363    __end_       = __c.__end_;
364    __cap_       = __c.__cap_;
365    __c.__first_ = nullptr;
366    __c.__begin_ = nullptr;
367    __c.__end_   = nullptr;
368    __c.__cap_   = nullptr;
369  } else {
370    auto __allocation = std::__allocate_at_least(__alloc_, __c.size());
371    __first_          = __allocation.ptr;
372    __begin_ = __end_ = __first_;
373    __cap_            = __first_ + __allocation.count;
374    typedef move_iterator<iterator> _Ip;
375    __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
376  }
377}
378
379template <class _Tp, class _Allocator>
380_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>&
381__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
382    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
383                is_nothrow_move_assignable<allocator_type>::value) ||
384               !__alloc_traits::propagate_on_container_move_assignment::value) {
385  clear();
386  shrink_to_fit();
387  __first_ = __c.__first_;
388  __begin_ = __c.__begin_;
389  __end_   = __c.__end_;
390  __cap_   = __c.__cap_;
391  __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
392  __c.__first_ = __c.__begin_ = __c.__end_ = __c.__cap_ = nullptr;
393  return *this;
394}
395
396template <class _Tp, class _Allocator>
397_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
398    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__alloc_rr>) {
399  std::swap(__first_, __x.__first_);
400  std::swap(__begin_, __x.__begin_);
401  std::swap(__end_, __x.__end_);
402  std::swap(__cap_, __x.__cap_);
403  std::__swap_allocator(__alloc_, __x.__alloc_);
404}
405
406template <class _Tp, class _Allocator>
407_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
408  if (capacity() > size()) {
409#if _LIBCPP_HAS_EXCEPTIONS
410    try {
411#endif // _LIBCPP_HAS_EXCEPTIONS
412      __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc_);
413      if (__t.capacity() < capacity()) {
414        __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
415        __t.__end_ = __t.__begin_ + (__end_ - __begin_);
416        std::swap(__first_, __t.__first_);
417        std::swap(__begin_, __t.__begin_);
418        std::swap(__end_, __t.__end_);
419        std::swap(__cap_, __t.__cap_);
420      }
421#if _LIBCPP_HAS_EXCEPTIONS
422    } catch (...) {
423    }
424#endif // _LIBCPP_HAS_EXCEPTIONS
425  }
426}
427
428template <class _Tp, class _Allocator>
429template <class... _Args>
430_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_front(_Args&&... __args) {
431  if (__begin_ == __first_) {
432    if (__end_ < __cap_) {
433      difference_type __d = __cap_ - __end_;
434      __d                 = (__d + 1) / 2;
435      __begin_            = std::move_backward(__begin_, __end_, __end_ + __d);
436      __end_ += __d;
437    } else {
438      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__cap_ - __first_), 1);
439      __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc_);
440      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
441      std::swap(__first_, __t.__first_);
442      std::swap(__begin_, __t.__begin_);
443      std::swap(__end_, __t.__end_);
444      std::swap(__cap_, __t.__cap_);
445    }
446  }
447  __alloc_traits::construct(__alloc_, std::__to_address(__begin_ - 1), std::forward<_Args>(__args)...);
448  --__begin_;
449}
450
451template <class _Tp, class _Allocator>
452template <class... _Args>
453_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
454  if (__end_ == __cap_) {
455    if (__begin_ > __first_) {
456      difference_type __d = __begin_ - __first_;
457      __d                 = (__d + 1) / 2;
458      __end_              = std::move(__begin_, __end_, __begin_ - __d);
459      __begin_ -= __d;
460    } else {
461      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__cap_ - __first_), 1);
462      __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc_);
463      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
464      std::swap(__first_, __t.__first_);
465      std::swap(__begin_, __t.__begin_);
466      std::swap(__end_, __t.__end_);
467      std::swap(__cap_, __t.__cap_);
468    }
469  }
470  __alloc_traits::construct(__alloc_, std::__to_address(__end_), std::forward<_Args>(__args)...);
471  ++__end_;
472}
473
474template <class _Tp, class _Allocator>
475_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
476swap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
477  __x.swap(__y);
478}
479
480_LIBCPP_END_NAMESPACE_STD
481
482_LIBCPP_POP_MACROS
483
484#endif // _LIBCPP___SPLIT_BUFFER
485