xref: /freebsd/contrib/llvm-project/libcxx/include/__split_buffer (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric// -*- C++ -*-
281ad6265SDimitry Andric//===----------------------------------------------------------------------===//
381ad6265SDimitry Andric//
481ad6265SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
581ad6265SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
681ad6265SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
781ad6265SDimitry Andric//
881ad6265SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
1081ad6265SDimitry Andric#ifndef _LIBCPP___SPLIT_BUFFER
1181ad6265SDimitry Andric#define _LIBCPP___SPLIT_BUFFER
1281ad6265SDimitry Andric
1381ad6265SDimitry Andric#include <__algorithm/max.h>
1481ad6265SDimitry Andric#include <__algorithm/move.h>
1581ad6265SDimitry Andric#include <__algorithm/move_backward.h>
160b57cec5SDimitry Andric#include <__config>
1781ad6265SDimitry Andric#include <__iterator/distance.h>
1881ad6265SDimitry Andric#include <__iterator/iterator_traits.h>
1981ad6265SDimitry Andric#include <__iterator/move_iterator.h>
20bdd1243dSDimitry Andric#include <__memory/allocate_at_least.h>
2181ad6265SDimitry Andric#include <__memory/allocator.h>
22bdd1243dSDimitry Andric#include <__memory/allocator_traits.h>
2381ad6265SDimitry Andric#include <__memory/compressed_pair.h>
24bdd1243dSDimitry Andric#include <__memory/pointer_traits.h>
25972a253aSDimitry Andric#include <__memory/swap_allocator.h>
2606c3fb27SDimitry Andric#include <__type_traits/add_lvalue_reference.h>
27*0fca6ea1SDimitry Andric#include <__type_traits/conditional.h>
2806c3fb27SDimitry Andric#include <__type_traits/enable_if.h>
2906c3fb27SDimitry Andric#include <__type_traits/integral_constant.h>
30*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_assignable.h>
31*0fca6ea1SDimitry Andric#include <__type_traits/is_nothrow_constructible.h>
3206c3fb27SDimitry Andric#include <__type_traits/is_swappable.h>
3306c3fb27SDimitry Andric#include <__type_traits/is_trivially_destructible.h>
34*0fca6ea1SDimitry Andric#include <__type_traits/is_trivially_relocatable.h>
3506c3fb27SDimitry Andric#include <__type_traits/remove_reference.h>
36fe6060f1SDimitry Andric#include <__utility/forward.h>
37bdd1243dSDimitry Andric#include <__utility/move.h>
3806c3fb27SDimitry Andric#include <cstddef>
390b57cec5SDimitry Andric
400b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
410b57cec5SDimitry Andric#  pragma GCC system_header
420b57cec5SDimitry Andric#endif
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
450b57cec5SDimitry Andric#include <__undef_macros>
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
480b57cec5SDimitry Andric
49bdd1243dSDimitry Andric// __split_buffer allocates a contiguous chunk of memory and stores objects in the range [__begin_, __end_).
50bdd1243dSDimitry Andric// It has uninitialized memory in the ranges  [__first_, __begin_) and [__end_, __end_cap_.first()). That allows
51bdd1243dSDimitry Andric// it to grow both in the front and back without having to move the data.
52bdd1243dSDimitry Andric
530b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> >
54cb14a3feSDimitry Andricstruct __split_buffer {
550b57cec5SDimitry Andricpublic:
5606c3fb27SDimitry Andric  using value_type      = _Tp;
5706c3fb27SDimitry Andric  using allocator_type  = _Allocator;
5806c3fb27SDimitry Andric  using __alloc_rr      = __libcpp_remove_reference_t<allocator_type>;
5906c3fb27SDimitry Andric  using __alloc_traits  = allocator_traits<__alloc_rr>;
6006c3fb27SDimitry Andric  using reference       = value_type&;
6106c3fb27SDimitry Andric  using const_reference = const value_type&;
6206c3fb27SDimitry Andric  using size_type       = typename __alloc_traits::size_type;
6306c3fb27SDimitry Andric  using difference_type = typename __alloc_traits::difference_type;
6406c3fb27SDimitry Andric  using pointer         = typename __alloc_traits::pointer;
6506c3fb27SDimitry Andric  using const_pointer   = typename __alloc_traits::const_pointer;
6606c3fb27SDimitry Andric  using iterator        = pointer;
6706c3fb27SDimitry Andric  using const_iterator  = const_pointer;
680b57cec5SDimitry Andric
69*0fca6ea1SDimitry Andric  // A __split_buffer contains the following members which may be trivially relocatable:
70*0fca6ea1SDimitry Andric  // - pointer: may be trivially relocatable, so it's checked
71*0fca6ea1SDimitry Andric  // - allocator_type: may be trivially relocatable, so it's checked
72*0fca6ea1SDimitry Andric  // __split_buffer doesn't have any self-references, so it's trivially relocatable if its members are.
73*0fca6ea1SDimitry Andric  using __trivially_relocatable = __conditional_t<
74*0fca6ea1SDimitry Andric      __libcpp_is_trivially_relocatable<pointer>::value && __libcpp_is_trivially_relocatable<allocator_type>::value,
75*0fca6ea1SDimitry Andric      __split_buffer,
76*0fca6ea1SDimitry Andric      void>;
77*0fca6ea1SDimitry Andric
780b57cec5SDimitry Andric  pointer __first_;
790b57cec5SDimitry Andric  pointer __begin_;
800b57cec5SDimitry Andric  pointer __end_;
810b57cec5SDimitry Andric  __compressed_pair<pointer, allocator_type> __end_cap_;
820b57cec5SDimitry Andric
8306c3fb27SDimitry Andric  using __alloc_ref       = __add_lvalue_reference_t<allocator_type>;
8406c3fb27SDimitry Andric  using __alloc_const_ref = __add_lvalue_reference_t<allocator_type>;
850b57cec5SDimitry Andric
8606c3fb27SDimitry Andric  __split_buffer(const __split_buffer&)            = delete;
8706c3fb27SDimitry Andric  __split_buffer& operator=(const __split_buffer&) = delete;
8806c3fb27SDimitry Andric
8906c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
9006c3fb27SDimitry Andric      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
9106c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) {}
9206c3fb27SDimitry Andric
9306c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
9406c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
9506c3fb27SDimitry Andric
9606c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
9706c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
980b57cec5SDimitry Andric
99bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
10006c3fb27SDimitry Andric  __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
1010b57cec5SDimitry Andric
102bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c)
1030b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
10406c3fb27SDimitry Andric
105bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
10606c3fb27SDimitry Andric
107bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c)
1080b57cec5SDimitry Andric      _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
1090b57cec5SDimitry Andric                  is_nothrow_move_assignable<allocator_type>::value) ||
1100b57cec5SDimitry Andric                 !__alloc_traits::propagate_on_container_move_assignment::value);
1110b57cec5SDimitry Andric
11206c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
11306c3fb27SDimitry Andric
11406c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __end_cap_.second(); }
11506c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT {
11606c3fb27SDimitry Andric    return __end_cap_.second();
11706c3fb27SDimitry Andric  }
11806c3fb27SDimitry Andric
11906c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_.first(); }
12006c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {
12106c3fb27SDimitry Andric    return __end_cap_.first();
12206c3fb27SDimitry Andric  }
12306c3fb27SDimitry Andric
124bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
125bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
12606c3fb27SDimitry Andric
127bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __end_; }
128bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __end_; }
1290b57cec5SDimitry Andric
13006c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(__begin_); }
13106c3fb27SDimitry Andric
13206c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {
13306c3fb27SDimitry Andric    return static_cast<size_type>(__end_ - __begin_);
13406c3fb27SDimitry Andric  }
13506c3fb27SDimitry Andric
136bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
13706c3fb27SDimitry Andric
13806c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
13906c3fb27SDimitry Andric    return static_cast<size_type>(__end_cap() - __first_);
14006c3fb27SDimitry Andric  }
14106c3fb27SDimitry Andric
14206c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
14306c3fb27SDimitry Andric    return static_cast<size_type>(__begin_ - __first_);
14406c3fb27SDimitry Andric  }
14506c3fb27SDimitry Andric
14606c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
14706c3fb27SDimitry Andric    return static_cast<size_type>(__end_cap() - __end_);
14806c3fb27SDimitry Andric  }
1490b57cec5SDimitry Andric
150bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
151bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const { return *__begin_; }
152bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() { return *(__end_ - 1); }
153bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const { return *(__end_ - 1); }
1540b57cec5SDimitry Andric
155bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
156bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
157bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(const_reference __x);
158bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
159bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
160bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
16106c3fb27SDimitry Andric
1620b57cec5SDimitry Andric  template <class... _Args>
163bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
1640b57cec5SDimitry Andric
165bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() { __destruct_at_begin(__begin_ + 1); }
166bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() { __destruct_at_end(__end_ - 1); }
1670b57cec5SDimitry Andric
168bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
169bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
17006c3fb27SDimitry Andric
1715f757f3fSDimitry Andric  template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
172cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(_InputIter __first, _InputIter __last);
17306c3fb27SDimitry Andric
1745f757f3fSDimitry Andric  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
175cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
176cb14a3feSDimitry Andric  __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1770b57cec5SDimitry Andric
17806c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
179cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
180cb14a3feSDimitry Andric  __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
1810b57cec5SDimitry Andric
18206c3fb27SDimitry Andric  template <class _Iterator>
183cb14a3feSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
184cb14a3feSDimitry Andric  __construct_at_end_with_size(_Iterator __first, size_type __n);
18506c3fb27SDimitry Andric
18606c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
18706c3fb27SDimitry Andric    __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
18806c3fb27SDimitry Andric  }
18906c3fb27SDimitry Andric
19006c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
19106c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
19206c3fb27SDimitry Andric
19306c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
19406c3fb27SDimitry Andric    __destruct_at_end(__new_last, false_type());
19506c3fb27SDimitry Andric  }
19606c3fb27SDimitry Andric
19706c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
19806c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
1990b57cec5SDimitry Andric
200bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
201*0fca6ea1SDimitry Andric      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__alloc_rr>);
2020b57cec5SDimitry Andric
203bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andricprivate:
20606c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
20706c3fb27SDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
2085f757f3fSDimitry Andric    __alloc() = std::move(__c.__alloc());
2090b57cec5SDimitry Andric  }
2100b57cec5SDimitry Andric
21106c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
212e40139ffSDimitry Andric
213e40139ffSDimitry Andric  struct _ConstructTransaction {
214*0fca6ea1SDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20
215*0fca6ea1SDimitry Andric    _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT
21606c3fb27SDimitry Andric        : __pos_(*__p),
21706c3fb27SDimitry Andric          __end_(*__p + __n),
21806c3fb27SDimitry Andric          __dest_(__p) {}
21906c3fb27SDimitry Andric
22006c3fb27SDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { *__dest_ = __pos_; }
22106c3fb27SDimitry Andric
222e40139ffSDimitry Andric    pointer __pos_;
223e40139ffSDimitry Andric    const pointer __end_;
22406c3fb27SDimitry Andric
225e40139ffSDimitry Andric  private:
226e40139ffSDimitry Andric    pointer* __dest_;
227e40139ffSDimitry Andric  };
2280b57cec5SDimitry Andric};
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
231cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 bool __split_buffer<_Tp, _Allocator>::__invariants() const {
232cb14a3feSDimitry Andric  if (__first_ == nullptr) {
2330b57cec5SDimitry Andric    if (__begin_ != nullptr)
2340b57cec5SDimitry Andric      return false;
2350b57cec5SDimitry Andric    if (__end_ != nullptr)
2360b57cec5SDimitry Andric      return false;
2370b57cec5SDimitry Andric    if (__end_cap() != nullptr)
2380b57cec5SDimitry Andric      return false;
239cb14a3feSDimitry Andric  } else {
2400b57cec5SDimitry Andric    if (__begin_ < __first_)
2410b57cec5SDimitry Andric      return false;
2420b57cec5SDimitry Andric    if (__end_ < __begin_)
2430b57cec5SDimitry Andric      return false;
2440b57cec5SDimitry Andric    if (__end_cap() < __end_)
2450b57cec5SDimitry Andric      return false;
2460b57cec5SDimitry Andric  }
2470b57cec5SDimitry Andric  return true;
2480b57cec5SDimitry Andric}
2490b57cec5SDimitry Andric
2500b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
2510b57cec5SDimitry Andric//  throws if construction throws
2520b57cec5SDimitry Andric//  Precondition:  __n > 0
2530b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2540b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
2550b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
256cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) {
257e40139ffSDimitry Andric  _ConstructTransaction __tx(&this->__end_, __n);
258e40139ffSDimitry Andric  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
2595f757f3fSDimitry Andric    __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
260e40139ffSDimitry Andric  }
2610b57cec5SDimitry Andric}
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andric//  Copy constructs __n objects starting at __end_ from __x
2640b57cec5SDimitry Andric//  throws if construction throws
2650b57cec5SDimitry Andric//  Precondition:  __n > 0
2660b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2670b57cec5SDimitry Andric//  Postcondition:  size() == old size() + __n
2680b57cec5SDimitry Andric//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
2690b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
270cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
271cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) {
272e40139ffSDimitry Andric  _ConstructTransaction __tx(&this->__end_, __n);
273e40139ffSDimitry Andric  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
274cb14a3feSDimitry Andric    __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), __x);
275e40139ffSDimitry Andric  }
2760b57cec5SDimitry Andric}
2770b57cec5SDimitry Andric
2780b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2795f757f3fSDimitry Andrictemplate <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
280cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
281cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) {
28206c3fb27SDimitry Andric  __construct_at_end_with_sentinel(__first, __last);
28306c3fb27SDimitry Andric}
28406c3fb27SDimitry Andric
28506c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
28606c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
287cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
288cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
2890b57cec5SDimitry Andric  __alloc_rr& __a = this->__alloc();
290cb14a3feSDimitry Andric  for (; __first != __last; ++__first) {
291cb14a3feSDimitry Andric    if (__end_ == __end_cap()) {
2920b57cec5SDimitry Andric      size_type __old_cap = __end_cap() - __first_;
2935f757f3fSDimitry Andric      size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
2940b57cec5SDimitry Andric      __split_buffer __buf(__new_cap, 0, __a);
295349cc55cSDimitry Andric      for (pointer __p = __begin_; __p != __end_; ++__p, (void)++__buf.__end_)
296cb14a3feSDimitry Andric        __alloc_traits::construct(__buf.__alloc(), std::__to_address(__buf.__end_), std::move(*__p));
2970b57cec5SDimitry Andric      swap(__buf);
2980b57cec5SDimitry Andric    }
2995f757f3fSDimitry Andric    __alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
3000b57cec5SDimitry Andric    ++this->__end_;
3010b57cec5SDimitry Andric  }
3020b57cec5SDimitry Andric}
30306c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
3045f757f3fSDimitry Andrictemplate <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
305cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
306cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) {
30706c3fb27SDimitry Andric  __construct_at_end_with_size(__first, std::distance(__first, __last));
30806c3fb27SDimitry Andric}
3090b57cec5SDimitry Andric
3100b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3110b57cec5SDimitry Andrictemplate <class _ForwardIterator>
312cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void
313cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
31406c3fb27SDimitry Andric  _ConstructTransaction __tx(&this->__end_, __n);
315349cc55cSDimitry Andric  for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void)++__first) {
316cb14a3feSDimitry Andric    __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_), *__first);
3170b57cec5SDimitry Andric  }
3180b57cec5SDimitry Andric}
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
321cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
322cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) {
3230b57cec5SDimitry Andric  while (__begin_ != __new_begin)
3245f757f3fSDimitry Andric    __alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
3250b57cec5SDimitry Andric}
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
328cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline void
329cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) {
3300b57cec5SDimitry Andric  __begin_ = __new_begin;
3310b57cec5SDimitry Andric}
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
334cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
335cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT {
3360b57cec5SDimitry Andric  while (__new_last != __end_)
3375f757f3fSDimitry Andric    __alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
3380b57cec5SDimitry Andric}
3390b57cec5SDimitry Andric
3400b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
341cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
342cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT {
3430b57cec5SDimitry Andric  __end_ = __new_last;
3440b57cec5SDimitry Andric}
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
347bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3480b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
349cb14a3feSDimitry Andric    : __end_cap_(nullptr, __a) {
35081ad6265SDimitry Andric  if (__cap == 0) {
35181ad6265SDimitry Andric    __first_ = nullptr;
35281ad6265SDimitry Andric  } else {
35381ad6265SDimitry Andric    auto __allocation = std::__allocate_at_least(__alloc(), __cap);
35481ad6265SDimitry Andric    __first_          = __allocation.ptr;
35581ad6265SDimitry Andric    __cap             = __allocation.count;
35681ad6265SDimitry Andric  }
3570b57cec5SDimitry Andric  __begin_ = __end_ = __first_ + __start;
3580b57cec5SDimitry Andric  __end_cap()       = __first_ + __cap;
3590b57cec5SDimitry Andric}
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
362cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::~__split_buffer() {
3630b57cec5SDimitry Andric  clear();
3640b57cec5SDimitry Andric  if (__first_)
3650b57cec5SDimitry Andric    __alloc_traits::deallocate(__alloc(), __first_, capacity());
3660b57cec5SDimitry Andric}
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
369cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
3700b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
3715f757f3fSDimitry Andric    : __first_(std::move(__c.__first_)),
3725f757f3fSDimitry Andric      __begin_(std::move(__c.__begin_)),
3735f757f3fSDimitry Andric      __end_(std::move(__c.__end_)),
374cb14a3feSDimitry Andric      __end_cap_(std::move(__c.__end_cap_)) {
3750b57cec5SDimitry Andric  __c.__first_    = nullptr;
3760b57cec5SDimitry Andric  __c.__begin_    = nullptr;
3770b57cec5SDimitry Andric  __c.__end_      = nullptr;
3780b57cec5SDimitry Andric  __c.__end_cap() = nullptr;
3790b57cec5SDimitry Andric}
3800b57cec5SDimitry Andric
3810b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
382bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3830b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
384cb14a3feSDimitry Andric    : __end_cap_(nullptr, __a) {
385cb14a3feSDimitry Andric  if (__a == __c.__alloc()) {
3860b57cec5SDimitry Andric    __first_        = __c.__first_;
3870b57cec5SDimitry Andric    __begin_        = __c.__begin_;
3880b57cec5SDimitry Andric    __end_          = __c.__end_;
3890b57cec5SDimitry Andric    __end_cap()     = __c.__end_cap();
3900b57cec5SDimitry Andric    __c.__first_    = nullptr;
3910b57cec5SDimitry Andric    __c.__begin_    = nullptr;
3920b57cec5SDimitry Andric    __c.__end_      = nullptr;
3930b57cec5SDimitry Andric    __c.__end_cap() = nullptr;
394cb14a3feSDimitry Andric  } else {
39581ad6265SDimitry Andric    auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
39681ad6265SDimitry Andric    __first_          = __allocation.ptr;
3970b57cec5SDimitry Andric    __begin_ = __end_ = __first_;
39881ad6265SDimitry Andric    __end_cap()       = __first_ + __allocation.count;
3990b57cec5SDimitry Andric    typedef move_iterator<iterator> _Ip;
4000b57cec5SDimitry Andric    __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
4010b57cec5SDimitry Andric  }
4020b57cec5SDimitry Andric}
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
405cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 __split_buffer<_Tp, _Allocator>&
4060b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
4070b57cec5SDimitry Andric    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
4080b57cec5SDimitry Andric                is_nothrow_move_assignable<allocator_type>::value) ||
409cb14a3feSDimitry Andric               !__alloc_traits::propagate_on_container_move_assignment::value) {
4100b57cec5SDimitry Andric  clear();
4110b57cec5SDimitry Andric  shrink_to_fit();
4120b57cec5SDimitry Andric  __first_    = __c.__first_;
4130b57cec5SDimitry Andric  __begin_    = __c.__begin_;
4140b57cec5SDimitry Andric  __end_      = __c.__end_;
4150b57cec5SDimitry Andric  __end_cap() = __c.__end_cap();
416cb14a3feSDimitry Andric  __move_assign_alloc(__c, integral_constant<bool, __alloc_traits::propagate_on_container_move_assignment::value>());
4170b57cec5SDimitry Andric  __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
4180b57cec5SDimitry Andric  return *this;
4190b57cec5SDimitry Andric}
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
422cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
423*0fca6ea1SDimitry Andric    _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__alloc_rr>) {
4245f757f3fSDimitry Andric  std::swap(__first_, __x.__first_);
4255f757f3fSDimitry Andric  std::swap(__begin_, __x.__begin_);
4265f757f3fSDimitry Andric  std::swap(__end_, __x.__end_);
4275f757f3fSDimitry Andric  std::swap(__end_cap(), __x.__end_cap());
4285f757f3fSDimitry Andric  std::__swap_allocator(__alloc(), __x.__alloc());
4290b57cec5SDimitry Andric}
4300b57cec5SDimitry Andric
4310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
432cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::reserve(size_type __n) {
433cb14a3feSDimitry Andric  if (__n < capacity()) {
4340b57cec5SDimitry Andric    __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
435cb14a3feSDimitry Andric    __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
4365f757f3fSDimitry Andric    std::swap(__first_, __t.__first_);
4375f757f3fSDimitry Andric    std::swap(__begin_, __t.__begin_);
4385f757f3fSDimitry Andric    std::swap(__end_, __t.__end_);
4395f757f3fSDimitry Andric    std::swap(__end_cap(), __t.__end_cap());
4400b57cec5SDimitry Andric  }
4410b57cec5SDimitry Andric}
4420b57cec5SDimitry Andric
4430b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
444cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT {
445cb14a3feSDimitry Andric  if (capacity() > size()) {
44606c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
447cb14a3feSDimitry Andric    try {
44806c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
4490b57cec5SDimitry Andric      __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
450cb14a3feSDimitry Andric      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
4510b57cec5SDimitry Andric      __t.__end_ = __t.__begin_ + (__end_ - __begin_);
4525f757f3fSDimitry Andric      std::swap(__first_, __t.__first_);
4535f757f3fSDimitry Andric      std::swap(__begin_, __t.__begin_);
4545f757f3fSDimitry Andric      std::swap(__end_, __t.__end_);
4555f757f3fSDimitry Andric      std::swap(__end_cap(), __t.__end_cap());
45606c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
457cb14a3feSDimitry Andric    } catch (...) {
4580b57cec5SDimitry Andric    }
45906c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
4600b57cec5SDimitry Andric  }
4610b57cec5SDimitry Andric}
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
464cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(const_reference __x) {
465cb14a3feSDimitry Andric  if (__begin_ == __first_) {
466cb14a3feSDimitry Andric    if (__end_ < __end_cap()) {
4670b57cec5SDimitry Andric      difference_type __d = __end_cap() - __end_;
4680b57cec5SDimitry Andric      __d                 = (__d + 1) / 2;
4695f757f3fSDimitry Andric      __begin_            = std::move_backward(__begin_, __end_, __end_ + __d);
4700b57cec5SDimitry Andric      __end_ += __d;
471cb14a3feSDimitry Andric    } else {
472bdd1243dSDimitry Andric      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
4730b57cec5SDimitry Andric      __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
474cb14a3feSDimitry Andric      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
4755f757f3fSDimitry Andric      std::swap(__first_, __t.__first_);
4765f757f3fSDimitry Andric      std::swap(__begin_, __t.__begin_);
4775f757f3fSDimitry Andric      std::swap(__end_, __t.__end_);
4785f757f3fSDimitry Andric      std::swap(__end_cap(), __t.__end_cap());
4790b57cec5SDimitry Andric    }
4800b57cec5SDimitry Andric  }
4815f757f3fSDimitry Andric  __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), __x);
4820b57cec5SDimitry Andric  --__begin_;
4830b57cec5SDimitry Andric}
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
486cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) {
487cb14a3feSDimitry Andric  if (__begin_ == __first_) {
488cb14a3feSDimitry Andric    if (__end_ < __end_cap()) {
4890b57cec5SDimitry Andric      difference_type __d = __end_cap() - __end_;
4900b57cec5SDimitry Andric      __d                 = (__d + 1) / 2;
4915f757f3fSDimitry Andric      __begin_            = std::move_backward(__begin_, __end_, __end_ + __d);
4920b57cec5SDimitry Andric      __end_ += __d;
493cb14a3feSDimitry Andric    } else {
494bdd1243dSDimitry Andric      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
4950b57cec5SDimitry Andric      __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
496cb14a3feSDimitry Andric      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
4975f757f3fSDimitry Andric      std::swap(__first_, __t.__first_);
4985f757f3fSDimitry Andric      std::swap(__begin_, __t.__begin_);
4995f757f3fSDimitry Andric      std::swap(__end_, __t.__end_);
5005f757f3fSDimitry Andric      std::swap(__end_cap(), __t.__end_cap());
5010b57cec5SDimitry Andric    }
5020b57cec5SDimitry Andric  }
503cb14a3feSDimitry Andric  __alloc_traits::construct(__alloc(), std::__to_address(__begin_ - 1), std::move(__x));
5040b57cec5SDimitry Andric  --__begin_;
5050b57cec5SDimitry Andric}
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
508cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
509cb14a3feSDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) {
510cb14a3feSDimitry Andric  if (__end_ == __end_cap()) {
511cb14a3feSDimitry Andric    if (__begin_ > __first_) {
5120b57cec5SDimitry Andric      difference_type __d = __begin_ - __first_;
5130b57cec5SDimitry Andric      __d                 = (__d + 1) / 2;
5145f757f3fSDimitry Andric      __end_              = std::move(__begin_, __end_, __begin_ - __d);
5150b57cec5SDimitry Andric      __begin_ -= __d;
516cb14a3feSDimitry Andric    } else {
517bdd1243dSDimitry Andric      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5180b57cec5SDimitry Andric      __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
519cb14a3feSDimitry Andric      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
5205f757f3fSDimitry Andric      std::swap(__first_, __t.__first_);
5215f757f3fSDimitry Andric      std::swap(__begin_, __t.__begin_);
5225f757f3fSDimitry Andric      std::swap(__end_, __t.__end_);
5235f757f3fSDimitry Andric      std::swap(__end_cap(), __t.__end_cap());
5240b57cec5SDimitry Andric    }
5250b57cec5SDimitry Andric  }
5265f757f3fSDimitry Andric  __alloc_traits::construct(__alloc(), std::__to_address(__end_), __x);
5270b57cec5SDimitry Andric  ++__end_;
5280b57cec5SDimitry Andric}
5290b57cec5SDimitry Andric
5300b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
531cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) {
532cb14a3feSDimitry Andric  if (__end_ == __end_cap()) {
533cb14a3feSDimitry Andric    if (__begin_ > __first_) {
5340b57cec5SDimitry Andric      difference_type __d = __begin_ - __first_;
5350b57cec5SDimitry Andric      __d                 = (__d + 1) / 2;
5365f757f3fSDimitry Andric      __end_              = std::move(__begin_, __end_, __begin_ - __d);
5370b57cec5SDimitry Andric      __begin_ -= __d;
538cb14a3feSDimitry Andric    } else {
539bdd1243dSDimitry Andric      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5400b57cec5SDimitry Andric      __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
541cb14a3feSDimitry Andric      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
5425f757f3fSDimitry Andric      std::swap(__first_, __t.__first_);
5435f757f3fSDimitry Andric      std::swap(__begin_, __t.__begin_);
5445f757f3fSDimitry Andric      std::swap(__end_, __t.__end_);
5455f757f3fSDimitry Andric      std::swap(__end_cap(), __t.__end_cap());
5460b57cec5SDimitry Andric    }
5470b57cec5SDimitry Andric  }
548cb14a3feSDimitry Andric  __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::move(__x));
5490b57cec5SDimitry Andric  ++__end_;
5500b57cec5SDimitry Andric}
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
5530b57cec5SDimitry Andrictemplate <class... _Args>
554cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 void __split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
555cb14a3feSDimitry Andric  if (__end_ == __end_cap()) {
556cb14a3feSDimitry Andric    if (__begin_ > __first_) {
5570b57cec5SDimitry Andric      difference_type __d = __begin_ - __first_;
5580b57cec5SDimitry Andric      __d                 = (__d + 1) / 2;
5595f757f3fSDimitry Andric      __end_              = std::move(__begin_, __end_, __begin_ - __d);
5600b57cec5SDimitry Andric      __begin_ -= __d;
561cb14a3feSDimitry Andric    } else {
562bdd1243dSDimitry Andric      size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5630b57cec5SDimitry Andric      __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
564cb14a3feSDimitry Andric      __t.__construct_at_end(move_iterator<pointer>(__begin_), move_iterator<pointer>(__end_));
5655f757f3fSDimitry Andric      std::swap(__first_, __t.__first_);
5665f757f3fSDimitry Andric      std::swap(__begin_, __t.__begin_);
5675f757f3fSDimitry Andric      std::swap(__end_, __t.__end_);
5685f757f3fSDimitry Andric      std::swap(__end_cap(), __t.__end_cap());
5690b57cec5SDimitry Andric    }
5700b57cec5SDimitry Andric  }
571cb14a3feSDimitry Andric  __alloc_traits::construct(__alloc(), std::__to_address(__end_), std::forward<_Args>(__args)...);
5720b57cec5SDimitry Andric  ++__end_;
5730b57cec5SDimitry Andric}
5740b57cec5SDimitry Andric
5750b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
576cb14a3feSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI void
577cb14a3feSDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
5780b57cec5SDimitry Andric  __x.swap(__y);
5790b57cec5SDimitry Andric}
5800b57cec5SDimitry Andric
5810b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric_LIBCPP_POP_MACROS
5840b57cec5SDimitry Andric
58581ad6265SDimitry Andric#endif // _LIBCPP___SPLIT_BUFFER
586