xref: /freebsd/contrib/llvm-project/libcxx/include/__split_buffer (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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>
2706c3fb27SDimitry Andric#include <__type_traits/enable_if.h>
2806c3fb27SDimitry Andric#include <__type_traits/integral_constant.h>
2906c3fb27SDimitry Andric#include <__type_traits/is_nothrow_default_constructible.h>
3006c3fb27SDimitry Andric#include <__type_traits/is_nothrow_move_assignable.h>
3106c3fb27SDimitry Andric#include <__type_traits/is_nothrow_move_constructible.h>
3206c3fb27SDimitry Andric#include <__type_traits/is_swappable.h>
3306c3fb27SDimitry Andric#include <__type_traits/is_trivially_destructible.h>
3406c3fb27SDimitry Andric#include <__type_traits/remove_reference.h>
35fe6060f1SDimitry Andric#include <__utility/forward.h>
36bdd1243dSDimitry Andric#include <__utility/move.h>
3706c3fb27SDimitry Andric#include <cstddef>
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
400b57cec5SDimitry Andric#  pragma GCC system_header
410b57cec5SDimitry Andric#endif
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
440b57cec5SDimitry Andric#include <__undef_macros>
450b57cec5SDimitry Andric
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> >
540b57cec5SDimitry Andricstruct __split_buffer
550b57cec5SDimitry Andric{
560b57cec5SDimitry Andricpublic:
5706c3fb27SDimitry Andric  using value_type      = _Tp;
5806c3fb27SDimitry Andric  using allocator_type  = _Allocator;
5906c3fb27SDimitry Andric  using __alloc_rr      = __libcpp_remove_reference_t<allocator_type>;
6006c3fb27SDimitry Andric  using __alloc_traits  = allocator_traits<__alloc_rr>;
6106c3fb27SDimitry Andric  using reference       = value_type&;
6206c3fb27SDimitry Andric  using const_reference = const value_type&;
6306c3fb27SDimitry Andric  using size_type       = typename __alloc_traits::size_type;
6406c3fb27SDimitry Andric  using difference_type = typename __alloc_traits::difference_type;
6506c3fb27SDimitry Andric  using pointer         = typename __alloc_traits::pointer;
6606c3fb27SDimitry Andric  using const_pointer   = typename __alloc_traits::const_pointer;
6706c3fb27SDimitry Andric  using iterator        = pointer;
6806c3fb27SDimitry Andric  using const_iterator  = const_pointer;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric  pointer __first_;
710b57cec5SDimitry Andric  pointer __begin_;
720b57cec5SDimitry Andric  pointer __end_;
730b57cec5SDimitry Andric  __compressed_pair<pointer, allocator_type> __end_cap_;
740b57cec5SDimitry Andric
7506c3fb27SDimitry Andric  using __alloc_ref       = __add_lvalue_reference_t<allocator_type>;
7606c3fb27SDimitry Andric  using __alloc_const_ref = __add_lvalue_reference_t<allocator_type>;
770b57cec5SDimitry Andric
7806c3fb27SDimitry Andric  __split_buffer(const __split_buffer&) = delete;
7906c3fb27SDimitry Andric  __split_buffer& operator=(const __split_buffer&) = delete;
8006c3fb27SDimitry Andric
8106c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
8206c3fb27SDimitry Andric      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
8306c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) {}
8406c3fb27SDimitry Andric
8506c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
8606c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
8706c3fb27SDimitry Andric
8806c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
8906c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
900b57cec5SDimitry Andric
91bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
9206c3fb27SDimitry Andric  __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a);
930b57cec5SDimitry Andric
94bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c)
950b57cec5SDimitry Andric      _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
9606c3fb27SDimitry Andric
97bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
9806c3fb27SDimitry Andric
99bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c)
1000b57cec5SDimitry Andric      _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
1010b57cec5SDimitry Andric                  is_nothrow_move_assignable<allocator_type>::value) ||
1020b57cec5SDimitry Andric                 !__alloc_traits::propagate_on_container_move_assignment::value);
1030b57cec5SDimitry Andric
10406c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
10506c3fb27SDimitry Andric
10606c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __end_cap_.second(); }
10706c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT {
10806c3fb27SDimitry Andric    return __end_cap_.second();
10906c3fb27SDimitry Andric  }
11006c3fb27SDimitry Andric
11106c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_.first(); }
11206c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {
11306c3fb27SDimitry Andric    return __end_cap_.first();
11406c3fb27SDimitry Andric  }
11506c3fb27SDimitry Andric
116bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return __begin_; }
117bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return __begin_; }
11806c3fb27SDimitry Andric
119bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return __end_; }
120bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return __end_; }
1210b57cec5SDimitry Andric
12206c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(__begin_); }
12306c3fb27SDimitry Andric
12406c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {
12506c3fb27SDimitry Andric    return static_cast<size_type>(__end_ - __begin_);
12606c3fb27SDimitry Andric  }
12706c3fb27SDimitry Andric
128bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
12906c3fb27SDimitry Andric
13006c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
13106c3fb27SDimitry Andric    return static_cast<size_type>(__end_cap() - __first_);
13206c3fb27SDimitry Andric  }
13306c3fb27SDimitry Andric
13406c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
13506c3fb27SDimitry Andric    return static_cast<size_type>(__begin_ - __first_);
13606c3fb27SDimitry Andric  }
13706c3fb27SDimitry Andric
13806c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
13906c3fb27SDimitry Andric    return static_cast<size_type>(__end_cap() - __end_);
14006c3fb27SDimitry Andric  }
1410b57cec5SDimitry Andric
142bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() { return *__begin_; }
143bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const { return *__begin_; }
144bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() { return *(__end_ - 1); }
145bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const { return *(__end_ - 1); }
1460b57cec5SDimitry Andric
147bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
148bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT;
149bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(const_reference __x);
150bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x);
151bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x);
152bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x);
15306c3fb27SDimitry Andric
1540b57cec5SDimitry Andric  template <class... _Args>
155bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args);
1560b57cec5SDimitry Andric
157bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() { __destruct_at_begin(__begin_ + 1); }
158bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() { __destruct_at_end(__end_ - 1); }
1590b57cec5SDimitry Andric
160bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n);
161bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x);
16206c3fb27SDimitry Andric
163*5f757f3fSDimitry Andric  template <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> = 0>
16406c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
165*5f757f3fSDimitry Andric  void __construct_at_end(_InputIter __first, _InputIter __last);
16606c3fb27SDimitry Andric
167*5f757f3fSDimitry Andric  template <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> = 0>
16806c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
169*5f757f3fSDimitry Andric  void __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1700b57cec5SDimitry Andric
17106c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
172bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
17306c3fb27SDimitry Andric  void __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
1740b57cec5SDimitry Andric
17506c3fb27SDimitry Andric  template <class _Iterator>
176bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
17706c3fb27SDimitry Andric  void __construct_at_end_with_size(_Iterator __first, size_type __n);
17806c3fb27SDimitry Andric
17906c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
18006c3fb27SDimitry Andric    __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
18106c3fb27SDimitry Andric  }
18206c3fb27SDimitry Andric
18306c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
18406c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
18506c3fb27SDimitry Andric
18606c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
18706c3fb27SDimitry Andric    __destruct_at_end(__new_last, false_type());
18806c3fb27SDimitry Andric  }
18906c3fb27SDimitry Andric
19006c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
19106c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
1920b57cec5SDimitry Andric
193bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
19406c3fb27SDimitry Andric      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<__alloc_rr>::value);
1950b57cec5SDimitry Andric
196bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andricprivate:
19906c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
20006c3fb27SDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
201*5f757f3fSDimitry Andric    __alloc() = std::move(__c.__alloc());
2020b57cec5SDimitry Andric  }
2030b57cec5SDimitry Andric
20406c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
205e40139ffSDimitry Andric
206e40139ffSDimitry Andric  struct _ConstructTransaction {
20706c3fb27SDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(
20806c3fb27SDimitry Andric        pointer* __p, size_type __n) _NOEXCEPT
20906c3fb27SDimitry Andric        : __pos_(*__p),
21006c3fb27SDimitry Andric          __end_(*__p + __n),
21106c3fb27SDimitry Andric          __dest_(__p) {}
21206c3fb27SDimitry Andric
21306c3fb27SDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { *__dest_ = __pos_; }
21406c3fb27SDimitry Andric
215e40139ffSDimitry Andric    pointer __pos_;
216e40139ffSDimitry Andric    const pointer __end_;
21706c3fb27SDimitry Andric
218e40139ffSDimitry Andric  private:
219e40139ffSDimitry Andric    pointer* __dest_;
220e40139ffSDimitry Andric  };
2210b57cec5SDimitry Andric};
2220b57cec5SDimitry Andric
2230b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
224bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
2250b57cec5SDimitry Andricbool
2260b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__invariants() const
2270b57cec5SDimitry Andric{
2280b57cec5SDimitry Andric    if (__first_ == nullptr)
2290b57cec5SDimitry Andric    {
2300b57cec5SDimitry Andric        if (__begin_ != nullptr)
2310b57cec5SDimitry Andric            return false;
2320b57cec5SDimitry Andric        if (__end_ != nullptr)
2330b57cec5SDimitry Andric            return false;
2340b57cec5SDimitry Andric        if (__end_cap() != nullptr)
2350b57cec5SDimitry Andric            return false;
2360b57cec5SDimitry Andric    }
2370b57cec5SDimitry Andric    else
2380b57cec5SDimitry Andric    {
2390b57cec5SDimitry Andric        if (__begin_ < __first_)
2400b57cec5SDimitry Andric            return false;
2410b57cec5SDimitry Andric        if (__end_ < __begin_)
2420b57cec5SDimitry Andric            return false;
2430b57cec5SDimitry Andric        if (__end_cap() < __end_)
2440b57cec5SDimitry Andric            return false;
2450b57cec5SDimitry Andric    }
2460b57cec5SDimitry Andric    return true;
2470b57cec5SDimitry Andric}
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
2500b57cec5SDimitry Andric//  throws if construction throws
2510b57cec5SDimitry Andric//  Precondition:  __n > 0
2520b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2530b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
2540b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
255bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
2560b57cec5SDimitry Andricvoid
2570b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
2580b57cec5SDimitry Andric{
259e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
260e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
261*5f757f3fSDimitry Andric        __alloc_traits::construct(this->__alloc(), std::__to_address(__tx.__pos_));
262e40139ffSDimitry Andric    }
2630b57cec5SDimitry Andric}
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric//  Copy constructs __n objects starting at __end_ from __x
2660b57cec5SDimitry Andric//  throws if construction throws
2670b57cec5SDimitry Andric//  Precondition:  __n > 0
2680b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2690b57cec5SDimitry Andric//  Postcondition:  size() == old size() + __n
2700b57cec5SDimitry Andric//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
2710b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
272bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
2730b57cec5SDimitry Andricvoid
2740b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
2750b57cec5SDimitry Andric{
276e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
277e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
278e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
279*5f757f3fSDimitry Andric            std::__to_address(__tx.__pos_), __x);
280e40139ffSDimitry Andric    }
2810b57cec5SDimitry Andric}
2820b57cec5SDimitry Andric
2830b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
284*5f757f3fSDimitry Andrictemplate <class _InputIter, __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value, int> >
285*5f757f3fSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
286*5f757f3fSDimitry Andricvoid __split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
2870b57cec5SDimitry Andric{
28806c3fb27SDimitry Andric  __construct_at_end_with_sentinel(__first, __last);
28906c3fb27SDimitry Andric}
29006c3fb27SDimitry Andric
29106c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
29206c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
29306c3fb27SDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
29406c3fb27SDimitry Andricvoid __split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
2950b57cec5SDimitry Andric    __alloc_rr& __a = this->__alloc();
2960b57cec5SDimitry Andric    for (; __first != __last; ++__first)
2970b57cec5SDimitry Andric    {
2980b57cec5SDimitry Andric        if (__end_ == __end_cap())
2990b57cec5SDimitry Andric        {
3000b57cec5SDimitry Andric            size_type __old_cap = __end_cap() - __first_;
301*5f757f3fSDimitry Andric            size_type __new_cap = std::max<size_type>(2 * __old_cap, 8);
3020b57cec5SDimitry Andric            __split_buffer __buf(__new_cap, 0, __a);
303349cc55cSDimitry Andric            for (pointer __p = __begin_; __p != __end_; ++__p, (void) ++__buf.__end_)
3040b57cec5SDimitry Andric                __alloc_traits::construct(__buf.__alloc(),
305*5f757f3fSDimitry Andric                        std::__to_address(__buf.__end_), std::move(*__p));
3060b57cec5SDimitry Andric            swap(__buf);
3070b57cec5SDimitry Andric        }
308*5f757f3fSDimitry Andric        __alloc_traits::construct(__a, std::__to_address(this->__end_), *__first);
3090b57cec5SDimitry Andric        ++this->__end_;
3100b57cec5SDimitry Andric    }
3110b57cec5SDimitry Andric}
31206c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
313*5f757f3fSDimitry Andrictemplate <class _ForwardIterator, __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value, int> >
314*5f757f3fSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
315*5f757f3fSDimitry Andricvoid __split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
31606c3fb27SDimitry Andric{
31706c3fb27SDimitry Andric  __construct_at_end_with_size(__first, std::distance(__first, __last));
31806c3fb27SDimitry Andric}
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3210b57cec5SDimitry Andrictemplate <class _ForwardIterator>
32206c3fb27SDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
32306c3fb27SDimitry Andricvoid __split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
32406c3fb27SDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
325349cc55cSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void) ++__first) {
326e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
327*5f757f3fSDimitry Andric            std::__to_address(__tx.__pos_), *__first);
3280b57cec5SDimitry Andric    }
3290b57cec5SDimitry Andric}
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
332bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3330b57cec5SDimitry Andricinline
3340b57cec5SDimitry Andricvoid
3350b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
3360b57cec5SDimitry Andric{
3370b57cec5SDimitry Andric    while (__begin_ != __new_begin)
338*5f757f3fSDimitry Andric        __alloc_traits::destroy(__alloc(), std::__to_address(__begin_++));
3390b57cec5SDimitry Andric}
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
342bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3430b57cec5SDimitry Andricinline
3440b57cec5SDimitry Andricvoid
3450b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
3460b57cec5SDimitry Andric{
3470b57cec5SDimitry Andric    __begin_ = __new_begin;
3480b57cec5SDimitry Andric}
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
351bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
352bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
3530b57cec5SDimitry Andricvoid
3540b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
3550b57cec5SDimitry Andric{
3560b57cec5SDimitry Andric    while (__new_last != __end_)
357*5f757f3fSDimitry Andric        __alloc_traits::destroy(__alloc(), std::__to_address(--__end_));
3580b57cec5SDimitry Andric}
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
361bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
362bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
3630b57cec5SDimitry Andricvoid
3640b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
3650b57cec5SDimitry Andric{
3660b57cec5SDimitry Andric    __end_ = __new_last;
3670b57cec5SDimitry Andric}
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
370bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3710b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
3720b57cec5SDimitry Andric    : __end_cap_(nullptr, __a)
3730b57cec5SDimitry Andric{
37481ad6265SDimitry Andric    if (__cap == 0) {
37581ad6265SDimitry Andric        __first_ = nullptr;
37681ad6265SDimitry Andric    } else {
37781ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __cap);
37881ad6265SDimitry Andric        __first_ = __allocation.ptr;
37981ad6265SDimitry Andric        __cap = __allocation.count;
38081ad6265SDimitry Andric    }
3810b57cec5SDimitry Andric    __begin_ = __end_ = __first_ + __start;
3820b57cec5SDimitry Andric    __end_cap() = __first_ + __cap;
3830b57cec5SDimitry Andric}
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
386bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3870b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer()
3880b57cec5SDimitry Andric{
3890b57cec5SDimitry Andric    clear();
3900b57cec5SDimitry Andric    if (__first_)
3910b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __first_, capacity());
3920b57cec5SDimitry Andric}
3930b57cec5SDimitry Andric
3940b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
395bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3960b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
3970b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
398*5f757f3fSDimitry Andric    : __first_(std::move(__c.__first_)),
399*5f757f3fSDimitry Andric      __begin_(std::move(__c.__begin_)),
400*5f757f3fSDimitry Andric      __end_(std::move(__c.__end_)),
401*5f757f3fSDimitry Andric      __end_cap_(std::move(__c.__end_cap_))
4020b57cec5SDimitry Andric{
4030b57cec5SDimitry Andric    __c.__first_ = nullptr;
4040b57cec5SDimitry Andric    __c.__begin_ = nullptr;
4050b57cec5SDimitry Andric    __c.__end_ = nullptr;
4060b57cec5SDimitry Andric    __c.__end_cap() = nullptr;
4070b57cec5SDimitry Andric}
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
410bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4110b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
412480093f4SDimitry Andric    : __end_cap_(nullptr, __a)
4130b57cec5SDimitry Andric{
4140b57cec5SDimitry Andric    if (__a == __c.__alloc())
4150b57cec5SDimitry Andric    {
4160b57cec5SDimitry Andric        __first_ = __c.__first_;
4170b57cec5SDimitry Andric        __begin_ = __c.__begin_;
4180b57cec5SDimitry Andric        __end_ = __c.__end_;
4190b57cec5SDimitry Andric        __end_cap() = __c.__end_cap();
4200b57cec5SDimitry Andric        __c.__first_ = nullptr;
4210b57cec5SDimitry Andric        __c.__begin_ = nullptr;
4220b57cec5SDimitry Andric        __c.__end_ = nullptr;
4230b57cec5SDimitry Andric        __c.__end_cap() = nullptr;
4240b57cec5SDimitry Andric    }
4250b57cec5SDimitry Andric    else
4260b57cec5SDimitry Andric    {
42781ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
42881ad6265SDimitry Andric        __first_ = __allocation.ptr;
4290b57cec5SDimitry Andric        __begin_ = __end_ = __first_;
43081ad6265SDimitry Andric        __end_cap() = __first_ + __allocation.count;
4310b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
4320b57cec5SDimitry Andric        __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
4330b57cec5SDimitry Andric    }
4340b57cec5SDimitry Andric}
4350b57cec5SDimitry Andric
4360b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
437bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4380b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>&
4390b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
4400b57cec5SDimitry Andric    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
4410b57cec5SDimitry Andric                is_nothrow_move_assignable<allocator_type>::value) ||
4420b57cec5SDimitry Andric               !__alloc_traits::propagate_on_container_move_assignment::value)
4430b57cec5SDimitry Andric{
4440b57cec5SDimitry Andric    clear();
4450b57cec5SDimitry Andric    shrink_to_fit();
4460b57cec5SDimitry Andric    __first_ = __c.__first_;
4470b57cec5SDimitry Andric    __begin_ = __c.__begin_;
4480b57cec5SDimitry Andric    __end_ = __c.__end_;
4490b57cec5SDimitry Andric    __end_cap() = __c.__end_cap();
4500b57cec5SDimitry Andric    __move_assign_alloc(__c,
4510b57cec5SDimitry Andric        integral_constant<bool,
4520b57cec5SDimitry Andric                          __alloc_traits::propagate_on_container_move_assignment::value>());
4530b57cec5SDimitry Andric    __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
4540b57cec5SDimitry Andric    return *this;
4550b57cec5SDimitry Andric}
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
458bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4590b57cec5SDimitry Andricvoid
4600b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
4610b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
4620b57cec5SDimitry Andric                   __is_nothrow_swappable<__alloc_rr>::value)
4630b57cec5SDimitry Andric{
464*5f757f3fSDimitry Andric    std::swap(__first_, __x.__first_);
465*5f757f3fSDimitry Andric    std::swap(__begin_, __x.__begin_);
466*5f757f3fSDimitry Andric    std::swap(__end_, __x.__end_);
467*5f757f3fSDimitry Andric    std::swap(__end_cap(), __x.__end_cap());
468*5f757f3fSDimitry Andric    std::__swap_allocator(__alloc(), __x.__alloc());
4690b57cec5SDimitry Andric}
4700b57cec5SDimitry Andric
4710b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
472bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4730b57cec5SDimitry Andricvoid
4740b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
4750b57cec5SDimitry Andric{
4760b57cec5SDimitry Andric    if (__n < capacity())
4770b57cec5SDimitry Andric    {
4780b57cec5SDimitry Andric        __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
4790b57cec5SDimitry Andric        __t.__construct_at_end(move_iterator<pointer>(__begin_),
4800b57cec5SDimitry Andric                               move_iterator<pointer>(__end_));
481*5f757f3fSDimitry Andric        std::swap(__first_, __t.__first_);
482*5f757f3fSDimitry Andric        std::swap(__begin_, __t.__begin_);
483*5f757f3fSDimitry Andric        std::swap(__end_, __t.__end_);
484*5f757f3fSDimitry Andric        std::swap(__end_cap(), __t.__end_cap());
4850b57cec5SDimitry Andric    }
4860b57cec5SDimitry Andric}
4870b57cec5SDimitry Andric
4880b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
489bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4900b57cec5SDimitry Andricvoid
4910b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
4920b57cec5SDimitry Andric{
4930b57cec5SDimitry Andric    if (capacity() > size())
4940b57cec5SDimitry Andric    {
49506c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
4960b57cec5SDimitry Andric        try
4970b57cec5SDimitry Andric        {
49806c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
4990b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
5000b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5010b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5020b57cec5SDimitry Andric            __t.__end_ = __t.__begin_ + (__end_ - __begin_);
503*5f757f3fSDimitry Andric            std::swap(__first_, __t.__first_);
504*5f757f3fSDimitry Andric            std::swap(__begin_, __t.__begin_);
505*5f757f3fSDimitry Andric            std::swap(__end_, __t.__end_);
506*5f757f3fSDimitry Andric            std::swap(__end_cap(), __t.__end_cap());
50706c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
5080b57cec5SDimitry Andric        }
5090b57cec5SDimitry Andric        catch (...)
5100b57cec5SDimitry Andric        {
5110b57cec5SDimitry Andric        }
51206c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
5130b57cec5SDimitry Andric    }
5140b57cec5SDimitry Andric}
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
517bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
5180b57cec5SDimitry Andricvoid
5190b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
5200b57cec5SDimitry Andric{
5210b57cec5SDimitry Andric    if (__begin_ == __first_)
5220b57cec5SDimitry Andric    {
5230b57cec5SDimitry Andric        if (__end_ < __end_cap())
5240b57cec5SDimitry Andric        {
5250b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
5260b57cec5SDimitry Andric            __d = (__d + 1) / 2;
527*5f757f3fSDimitry Andric            __begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
5280b57cec5SDimitry Andric            __end_ += __d;
5290b57cec5SDimitry Andric        }
5300b57cec5SDimitry Andric        else
5310b57cec5SDimitry Andric        {
532bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5330b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
5340b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5350b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
536*5f757f3fSDimitry Andric            std::swap(__first_, __t.__first_);
537*5f757f3fSDimitry Andric            std::swap(__begin_, __t.__begin_);
538*5f757f3fSDimitry Andric            std::swap(__end_, __t.__end_);
539*5f757f3fSDimitry Andric            std::swap(__end_cap(), __t.__end_cap());
5400b57cec5SDimitry Andric        }
5410b57cec5SDimitry Andric    }
542*5f757f3fSDimitry Andric    __alloc_traits::construct(__alloc(), std::__to_address(__begin_-1), __x);
5430b57cec5SDimitry Andric    --__begin_;
5440b57cec5SDimitry Andric}
5450b57cec5SDimitry Andric
5460b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
547bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
5480b57cec5SDimitry Andricvoid
5490b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
5500b57cec5SDimitry Andric{
5510b57cec5SDimitry Andric    if (__begin_ == __first_)
5520b57cec5SDimitry Andric    {
5530b57cec5SDimitry Andric        if (__end_ < __end_cap())
5540b57cec5SDimitry Andric        {
5550b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
5560b57cec5SDimitry Andric            __d = (__d + 1) / 2;
557*5f757f3fSDimitry Andric            __begin_ = std::move_backward(__begin_, __end_, __end_ + __d);
5580b57cec5SDimitry Andric            __end_ += __d;
5590b57cec5SDimitry Andric        }
5600b57cec5SDimitry Andric        else
5610b57cec5SDimitry Andric        {
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 + 3) / 4, __alloc());
5640b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5650b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
566*5f757f3fSDimitry Andric            std::swap(__first_, __t.__first_);
567*5f757f3fSDimitry Andric            std::swap(__begin_, __t.__begin_);
568*5f757f3fSDimitry Andric            std::swap(__end_, __t.__end_);
569*5f757f3fSDimitry Andric            std::swap(__end_cap(), __t.__end_cap());
5700b57cec5SDimitry Andric        }
5710b57cec5SDimitry Andric    }
572*5f757f3fSDimitry Andric    __alloc_traits::construct(__alloc(), std::__to_address(__begin_-1),
573*5f757f3fSDimitry Andric            std::move(__x));
5740b57cec5SDimitry Andric    --__begin_;
5750b57cec5SDimitry Andric}
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
578bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
579bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
5800b57cec5SDimitry Andricvoid
5810b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
5820b57cec5SDimitry Andric{
5830b57cec5SDimitry Andric    if (__end_ == __end_cap())
5840b57cec5SDimitry Andric    {
5850b57cec5SDimitry Andric        if (__begin_ > __first_)
5860b57cec5SDimitry Andric        {
5870b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
5880b57cec5SDimitry Andric            __d = (__d + 1) / 2;
589*5f757f3fSDimitry Andric            __end_ = std::move(__begin_, __end_, __begin_ - __d);
5900b57cec5SDimitry Andric            __begin_ -= __d;
5910b57cec5SDimitry Andric        }
5920b57cec5SDimitry Andric        else
5930b57cec5SDimitry Andric        {
594bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5950b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5960b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5970b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
598*5f757f3fSDimitry Andric            std::swap(__first_, __t.__first_);
599*5f757f3fSDimitry Andric            std::swap(__begin_, __t.__begin_);
600*5f757f3fSDimitry Andric            std::swap(__end_, __t.__end_);
601*5f757f3fSDimitry Andric            std::swap(__end_cap(), __t.__end_cap());
6020b57cec5SDimitry Andric        }
6030b57cec5SDimitry Andric    }
604*5f757f3fSDimitry Andric    __alloc_traits::construct(__alloc(), std::__to_address(__end_), __x);
6050b57cec5SDimitry Andric    ++__end_;
6060b57cec5SDimitry Andric}
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
609bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
6100b57cec5SDimitry Andricvoid
6110b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
6120b57cec5SDimitry Andric{
6130b57cec5SDimitry Andric    if (__end_ == __end_cap())
6140b57cec5SDimitry Andric    {
6150b57cec5SDimitry Andric        if (__begin_ > __first_)
6160b57cec5SDimitry Andric        {
6170b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
6180b57cec5SDimitry Andric            __d = (__d + 1) / 2;
619*5f757f3fSDimitry Andric            __end_ = std::move(__begin_, __end_, __begin_ - __d);
6200b57cec5SDimitry Andric            __begin_ -= __d;
6210b57cec5SDimitry Andric        }
6220b57cec5SDimitry Andric        else
6230b57cec5SDimitry Andric        {
624bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
6250b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
6260b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
6270b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
628*5f757f3fSDimitry Andric            std::swap(__first_, __t.__first_);
629*5f757f3fSDimitry Andric            std::swap(__begin_, __t.__begin_);
630*5f757f3fSDimitry Andric            std::swap(__end_, __t.__end_);
631*5f757f3fSDimitry Andric            std::swap(__end_cap(), __t.__end_cap());
6320b57cec5SDimitry Andric        }
6330b57cec5SDimitry Andric    }
634*5f757f3fSDimitry Andric    __alloc_traits::construct(__alloc(), std::__to_address(__end_),
635*5f757f3fSDimitry Andric            std::move(__x));
6360b57cec5SDimitry Andric    ++__end_;
6370b57cec5SDimitry Andric}
6380b57cec5SDimitry Andric
6390b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
6400b57cec5SDimitry Andrictemplate <class... _Args>
641bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
6420b57cec5SDimitry Andricvoid
6430b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
6440b57cec5SDimitry Andric{
6450b57cec5SDimitry Andric    if (__end_ == __end_cap())
6460b57cec5SDimitry Andric    {
6470b57cec5SDimitry Andric        if (__begin_ > __first_)
6480b57cec5SDimitry Andric        {
6490b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
6500b57cec5SDimitry Andric            __d = (__d + 1) / 2;
651*5f757f3fSDimitry Andric            __end_ = std::move(__begin_, __end_, __begin_ - __d);
6520b57cec5SDimitry Andric            __begin_ -= __d;
6530b57cec5SDimitry Andric        }
6540b57cec5SDimitry Andric        else
6550b57cec5SDimitry Andric        {
656bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
6570b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
6580b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
6590b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
660*5f757f3fSDimitry Andric            std::swap(__first_, __t.__first_);
661*5f757f3fSDimitry Andric            std::swap(__begin_, __t.__begin_);
662*5f757f3fSDimitry Andric            std::swap(__end_, __t.__end_);
663*5f757f3fSDimitry Andric            std::swap(__end_cap(), __t.__end_cap());
6640b57cec5SDimitry Andric        }
6650b57cec5SDimitry Andric    }
666*5f757f3fSDimitry Andric    __alloc_traits::construct(__alloc(), std::__to_address(__end_),
667*5f757f3fSDimitry Andric                              std::forward<_Args>(__args)...);
6680b57cec5SDimitry Andric    ++__end_;
6690b57cec5SDimitry Andric}
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
672bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
673bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
6740b57cec5SDimitry Andricvoid
6750b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
6760b57cec5SDimitry Andric        _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
6770b57cec5SDimitry Andric{
6780b57cec5SDimitry Andric    __x.swap(__y);
6790b57cec5SDimitry Andric}
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andric_LIBCPP_POP_MACROS
6840b57cec5SDimitry Andric
68581ad6265SDimitry Andric#endif // _LIBCPP___SPLIT_BUFFER
686