xref: /freebsd/contrib/llvm-project/libcxx/include/__split_buffer (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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>
26*06c3fb27SDimitry Andric#include <__type_traits/add_lvalue_reference.h>
27*06c3fb27SDimitry Andric#include <__type_traits/enable_if.h>
28*06c3fb27SDimitry Andric#include <__type_traits/integral_constant.h>
29*06c3fb27SDimitry Andric#include <__type_traits/is_nothrow_default_constructible.h>
30*06c3fb27SDimitry Andric#include <__type_traits/is_nothrow_move_assignable.h>
31*06c3fb27SDimitry Andric#include <__type_traits/is_nothrow_move_constructible.h>
32*06c3fb27SDimitry Andric#include <__type_traits/is_swappable.h>
33*06c3fb27SDimitry Andric#include <__type_traits/is_trivially_destructible.h>
34*06c3fb27SDimitry Andric#include <__type_traits/remove_reference.h>
35fe6060f1SDimitry Andric#include <__utility/forward.h>
36bdd1243dSDimitry Andric#include <__utility/move.h>
37*06c3fb27SDimitry 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:
57*06c3fb27SDimitry Andric  using value_type      = _Tp;
58*06c3fb27SDimitry Andric  using allocator_type  = _Allocator;
59*06c3fb27SDimitry Andric  using __alloc_rr      = __libcpp_remove_reference_t<allocator_type>;
60*06c3fb27SDimitry Andric  using __alloc_traits  = allocator_traits<__alloc_rr>;
61*06c3fb27SDimitry Andric  using reference       = value_type&;
62*06c3fb27SDimitry Andric  using const_reference = const value_type&;
63*06c3fb27SDimitry Andric  using size_type       = typename __alloc_traits::size_type;
64*06c3fb27SDimitry Andric  using difference_type = typename __alloc_traits::difference_type;
65*06c3fb27SDimitry Andric  using pointer         = typename __alloc_traits::pointer;
66*06c3fb27SDimitry Andric  using const_pointer   = typename __alloc_traits::const_pointer;
67*06c3fb27SDimitry Andric  using iterator        = pointer;
68*06c3fb27SDimitry 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
75*06c3fb27SDimitry Andric  using __alloc_ref       = __add_lvalue_reference_t<allocator_type>;
76*06c3fb27SDimitry Andric  using __alloc_const_ref = __add_lvalue_reference_t<allocator_type>;
770b57cec5SDimitry Andric
78*06c3fb27SDimitry Andric  __split_buffer(const __split_buffer&) = delete;
79*06c3fb27SDimitry Andric  __split_buffer& operator=(const __split_buffer&) = delete;
80*06c3fb27SDimitry Andric
81*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer()
82*06c3fb27SDimitry Andric      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
83*06c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) {}
84*06c3fb27SDimitry Andric
85*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(__alloc_rr& __a)
86*06c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
87*06c3fb27SDimitry Andric
88*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit __split_buffer(const __alloc_rr& __a)
89*06c3fb27SDimitry Andric      : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) {}
900b57cec5SDimitry Andric
91bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
92*06c3fb27SDimitry 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);
96*06c3fb27SDimitry Andric
97bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a);
98*06c3fb27SDimitry 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
104*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer();
105*06c3fb27SDimitry Andric
106*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT { return __end_cap_.second(); }
107*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT {
108*06c3fb27SDimitry Andric    return __end_cap_.second();
109*06c3fb27SDimitry Andric  }
110*06c3fb27SDimitry Andric
111*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT { return __end_cap_.first(); }
112*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {
113*06c3fb27SDimitry Andric    return __end_cap_.first();
114*06c3fb27SDimitry Andric  }
115*06c3fb27SDimitry 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_; }
118*06c3fb27SDimitry 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
122*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { __destruct_at_end(__begin_); }
123*06c3fb27SDimitry Andric
124*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {
125*06c3fb27SDimitry Andric    return static_cast<size_type>(__end_ - __begin_);
126*06c3fb27SDimitry Andric  }
127*06c3fb27SDimitry Andric
128bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const { return __end_ == __begin_; }
129*06c3fb27SDimitry Andric
130*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {
131*06c3fb27SDimitry Andric    return static_cast<size_type>(__end_cap() - __first_);
132*06c3fb27SDimitry Andric  }
133*06c3fb27SDimitry Andric
134*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {
135*06c3fb27SDimitry Andric    return static_cast<size_type>(__begin_ - __first_);
136*06c3fb27SDimitry Andric  }
137*06c3fb27SDimitry Andric
138*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {
139*06c3fb27SDimitry Andric    return static_cast<size_type>(__end_cap() - __end_);
140*06c3fb27SDimitry 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);
153*06c3fb27SDimitry 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);
162*06c3fb27SDimitry Andric
1630b57cec5SDimitry Andric  template <class _InputIter>
164*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
165*06c3fb27SDimitry Andric      __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value>
1660b57cec5SDimitry Andric      __construct_at_end(_InputIter __first, _InputIter __last);
167*06c3fb27SDimitry Andric
1680b57cec5SDimitry Andric  template <class _ForwardIterator>
169*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
170*06c3fb27SDimitry Andric      __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value>
1710b57cec5SDimitry Andric      __construct_at_end(_ForwardIterator __first, _ForwardIterator __last);
1720b57cec5SDimitry Andric
173*06c3fb27SDimitry Andric  template <class _Iterator, class _Sentinel>
174bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
175*06c3fb27SDimitry Andric  void __construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last);
1760b57cec5SDimitry Andric
177*06c3fb27SDimitry Andric  template <class _Iterator>
178bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
179*06c3fb27SDimitry Andric  void __construct_at_end_with_size(_Iterator __first, size_type __n);
180*06c3fb27SDimitry Andric
181*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) {
182*06c3fb27SDimitry Andric    __destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());
183*06c3fb27SDimitry Andric  }
184*06c3fb27SDimitry Andric
185*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, false_type);
186*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin, true_type);
187*06c3fb27SDimitry Andric
188*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last) _NOEXCEPT {
189*06c3fb27SDimitry Andric    __destruct_at_end(__new_last, false_type());
190*06c3fb27SDimitry Andric  }
191*06c3fb27SDimitry Andric
192*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT;
193*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT;
1940b57cec5SDimitry Andric
195bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x)
196*06c3fb27SDimitry Andric      _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || __is_nothrow_swappable<__alloc_rr>::value);
1970b57cec5SDimitry Andric
198bdd1243dSDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const;
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andricprivate:
201*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer& __c, true_type)
202*06c3fb27SDimitry Andric      _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) {
2030b57cec5SDimitry Andric    __alloc() = _VSTD::move(__c.__alloc());
2040b57cec5SDimitry Andric  }
2050b57cec5SDimitry Andric
206*06c3fb27SDimitry Andric  _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT {}
207e40139ffSDimitry Andric
208e40139ffSDimitry Andric  struct _ConstructTransaction {
209*06c3fb27SDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(
210*06c3fb27SDimitry Andric        pointer* __p, size_type __n) _NOEXCEPT
211*06c3fb27SDimitry Andric        : __pos_(*__p),
212*06c3fb27SDimitry Andric          __end_(*__p + __n),
213*06c3fb27SDimitry Andric          __dest_(__p) {}
214*06c3fb27SDimitry Andric
215*06c3fb27SDimitry Andric    _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { *__dest_ = __pos_; }
216*06c3fb27SDimitry Andric
217e40139ffSDimitry Andric    pointer __pos_;
218e40139ffSDimitry Andric    const pointer __end_;
219*06c3fb27SDimitry Andric
220e40139ffSDimitry Andric  private:
221e40139ffSDimitry Andric    pointer* __dest_;
222e40139ffSDimitry Andric  };
2230b57cec5SDimitry Andric};
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
226bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
2270b57cec5SDimitry Andricbool
2280b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__invariants() const
2290b57cec5SDimitry Andric{
2300b57cec5SDimitry Andric    if (__first_ == nullptr)
2310b57cec5SDimitry Andric    {
2320b57cec5SDimitry Andric        if (__begin_ != nullptr)
2330b57cec5SDimitry Andric            return false;
2340b57cec5SDimitry Andric        if (__end_ != nullptr)
2350b57cec5SDimitry Andric            return false;
2360b57cec5SDimitry Andric        if (__end_cap() != nullptr)
2370b57cec5SDimitry Andric            return false;
2380b57cec5SDimitry Andric    }
2390b57cec5SDimitry Andric    else
2400b57cec5SDimitry Andric    {
2410b57cec5SDimitry Andric        if (__begin_ < __first_)
2420b57cec5SDimitry Andric            return false;
2430b57cec5SDimitry Andric        if (__end_ < __begin_)
2440b57cec5SDimitry Andric            return false;
2450b57cec5SDimitry Andric        if (__end_cap() < __end_)
2460b57cec5SDimitry Andric            return false;
2470b57cec5SDimitry Andric    }
2480b57cec5SDimitry Andric    return true;
2490b57cec5SDimitry Andric}
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric//  Default constructs __n objects starting at __end_
2520b57cec5SDimitry Andric//  throws if construction throws
2530b57cec5SDimitry Andric//  Precondition:  __n > 0
2540b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2550b57cec5SDimitry Andric//  Postcondition:  size() == size() + __n
2560b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
257bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
2580b57cec5SDimitry Andricvoid
2590b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n)
2600b57cec5SDimitry Andric{
261e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
262e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
263480093f4SDimitry Andric        __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_));
264e40139ffSDimitry Andric    }
2650b57cec5SDimitry Andric}
2660b57cec5SDimitry Andric
2670b57cec5SDimitry Andric//  Copy constructs __n objects starting at __end_ from __x
2680b57cec5SDimitry Andric//  throws if construction throws
2690b57cec5SDimitry Andric//  Precondition:  __n > 0
2700b57cec5SDimitry Andric//  Precondition:  size() + __n <= capacity()
2710b57cec5SDimitry Andric//  Postcondition:  size() == old size() + __n
2720b57cec5SDimitry Andric//  Postcondition:  [i] == __x for all i in [size() - __n, __n)
2730b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
274bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
2750b57cec5SDimitry Andricvoid
2760b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x)
2770b57cec5SDimitry Andric{
278e40139ffSDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
279e40139ffSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) {
280e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
281480093f4SDimitry Andric            _VSTD::__to_address(__tx.__pos_), __x);
282e40139ffSDimitry Andric    }
2830b57cec5SDimitry Andric}
2840b57cec5SDimitry Andric
2850b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
2860b57cec5SDimitry Andrictemplate <class _InputIter>
287*06c3fb27SDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 __enable_if_t<__has_exactly_input_iterator_category<_InputIter>::value>
2880b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last)
2890b57cec5SDimitry Andric{
290*06c3fb27SDimitry Andric  __construct_at_end_with_sentinel(__first, __last);
291*06c3fb27SDimitry Andric}
292*06c3fb27SDimitry Andric
293*06c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
294*06c3fb27SDimitry Andrictemplate <class _Iterator, class _Sentinel>
295*06c3fb27SDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
296*06c3fb27SDimitry Andricvoid __split_buffer<_Tp, _Allocator>::__construct_at_end_with_sentinel(_Iterator __first, _Sentinel __last) {
2970b57cec5SDimitry Andric    __alloc_rr& __a = this->__alloc();
2980b57cec5SDimitry Andric    for (; __first != __last; ++__first)
2990b57cec5SDimitry Andric    {
3000b57cec5SDimitry Andric        if (__end_ == __end_cap())
3010b57cec5SDimitry Andric        {
3020b57cec5SDimitry Andric            size_type __old_cap = __end_cap() - __first_;
3030b57cec5SDimitry Andric            size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8);
3040b57cec5SDimitry Andric            __split_buffer __buf(__new_cap, 0, __a);
305349cc55cSDimitry Andric            for (pointer __p = __begin_; __p != __end_; ++__p, (void) ++__buf.__end_)
3060b57cec5SDimitry Andric                __alloc_traits::construct(__buf.__alloc(),
307480093f4SDimitry Andric                        _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p));
3080b57cec5SDimitry Andric            swap(__buf);
3090b57cec5SDimitry Andric        }
310480093f4SDimitry Andric        __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first);
3110b57cec5SDimitry Andric        ++this->__end_;
3120b57cec5SDimitry Andric    }
3130b57cec5SDimitry Andric}
314*06c3fb27SDimitry Andrictemplate <class _Tp, class _Allocator>
315*06c3fb27SDimitry Andrictemplate <class _ForwardIterator>
316*06c3fb27SDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 __enable_if_t<__has_forward_iterator_category<_ForwardIterator>::value>
317*06c3fb27SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last)
318*06c3fb27SDimitry Andric{
319*06c3fb27SDimitry Andric  __construct_at_end_with_size(__first, std::distance(__first, __last));
320*06c3fb27SDimitry Andric}
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
3230b57cec5SDimitry Andrictemplate <class _ForwardIterator>
324*06c3fb27SDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
325*06c3fb27SDimitry Andricvoid __split_buffer<_Tp, _Allocator>::__construct_at_end_with_size(_ForwardIterator __first, size_type __n) {
326*06c3fb27SDimitry Andric    _ConstructTransaction __tx(&this->__end_, __n);
327349cc55cSDimitry Andric    for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void) ++__first) {
328e40139ffSDimitry Andric        __alloc_traits::construct(this->__alloc(),
329480093f4SDimitry Andric            _VSTD::__to_address(__tx.__pos_), *__first);
3300b57cec5SDimitry Andric    }
3310b57cec5SDimitry Andric}
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
334bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3350b57cec5SDimitry Andricinline
3360b57cec5SDimitry Andricvoid
3370b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type)
3380b57cec5SDimitry Andric{
3390b57cec5SDimitry Andric    while (__begin_ != __new_begin)
340e8d8bef9SDimitry Andric        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++));
3410b57cec5SDimitry Andric}
3420b57cec5SDimitry Andric
3430b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
344bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3450b57cec5SDimitry Andricinline
3460b57cec5SDimitry Andricvoid
3470b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type)
3480b57cec5SDimitry Andric{
3490b57cec5SDimitry Andric    __begin_ = __new_begin;
3500b57cec5SDimitry Andric}
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
353bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
354bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
3550b57cec5SDimitry Andricvoid
3560b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT
3570b57cec5SDimitry Andric{
3580b57cec5SDimitry Andric    while (__new_last != __end_)
359e8d8bef9SDimitry Andric        __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_));
3600b57cec5SDimitry Andric}
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
363bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
364bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
3650b57cec5SDimitry Andricvoid
3660b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT
3670b57cec5SDimitry Andric{
3680b57cec5SDimitry Andric    __end_ = __new_last;
3690b57cec5SDimitry Andric}
3700b57cec5SDimitry Andric
3710b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
372bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3730b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a)
3740b57cec5SDimitry Andric    : __end_cap_(nullptr, __a)
3750b57cec5SDimitry Andric{
37681ad6265SDimitry Andric    if (__cap == 0) {
37781ad6265SDimitry Andric        __first_ = nullptr;
37881ad6265SDimitry Andric    } else {
37981ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __cap);
38081ad6265SDimitry Andric        __first_ = __allocation.ptr;
38181ad6265SDimitry Andric        __cap = __allocation.count;
38281ad6265SDimitry Andric    }
3830b57cec5SDimitry Andric    __begin_ = __end_ = __first_ + __start;
3840b57cec5SDimitry Andric    __end_cap() = __first_ + __cap;
3850b57cec5SDimitry Andric}
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
388bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3890b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer()
3900b57cec5SDimitry Andric{
3910b57cec5SDimitry Andric    clear();
3920b57cec5SDimitry Andric    if (__first_)
3930b57cec5SDimitry Andric        __alloc_traits::deallocate(__alloc(), __first_, capacity());
3940b57cec5SDimitry Andric}
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
397bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
3980b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c)
3990b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
4000b57cec5SDimitry Andric    : __first_(_VSTD::move(__c.__first_)),
4010b57cec5SDimitry Andric      __begin_(_VSTD::move(__c.__begin_)),
4020b57cec5SDimitry Andric      __end_(_VSTD::move(__c.__end_)),
4030b57cec5SDimitry Andric      __end_cap_(_VSTD::move(__c.__end_cap_))
4040b57cec5SDimitry Andric{
4050b57cec5SDimitry Andric    __c.__first_ = nullptr;
4060b57cec5SDimitry Andric    __c.__begin_ = nullptr;
4070b57cec5SDimitry Andric    __c.__end_ = nullptr;
4080b57cec5SDimitry Andric    __c.__end_cap() = nullptr;
4090b57cec5SDimitry Andric}
4100b57cec5SDimitry Andric
4110b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
412bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4130b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a)
414480093f4SDimitry Andric    : __end_cap_(nullptr, __a)
4150b57cec5SDimitry Andric{
4160b57cec5SDimitry Andric    if (__a == __c.__alloc())
4170b57cec5SDimitry Andric    {
4180b57cec5SDimitry Andric        __first_ = __c.__first_;
4190b57cec5SDimitry Andric        __begin_ = __c.__begin_;
4200b57cec5SDimitry Andric        __end_ = __c.__end_;
4210b57cec5SDimitry Andric        __end_cap() = __c.__end_cap();
4220b57cec5SDimitry Andric        __c.__first_ = nullptr;
4230b57cec5SDimitry Andric        __c.__begin_ = nullptr;
4240b57cec5SDimitry Andric        __c.__end_ = nullptr;
4250b57cec5SDimitry Andric        __c.__end_cap() = nullptr;
4260b57cec5SDimitry Andric    }
4270b57cec5SDimitry Andric    else
4280b57cec5SDimitry Andric    {
42981ad6265SDimitry Andric        auto __allocation = std::__allocate_at_least(__alloc(), __c.size());
43081ad6265SDimitry Andric        __first_ = __allocation.ptr;
4310b57cec5SDimitry Andric        __begin_ = __end_ = __first_;
43281ad6265SDimitry Andric        __end_cap() = __first_ + __allocation.count;
4330b57cec5SDimitry Andric        typedef move_iterator<iterator> _Ip;
4340b57cec5SDimitry Andric        __construct_at_end(_Ip(__c.begin()), _Ip(__c.end()));
4350b57cec5SDimitry Andric    }
4360b57cec5SDimitry Andric}
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
439bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4400b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>&
4410b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c)
4420b57cec5SDimitry Andric    _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value &&
4430b57cec5SDimitry Andric                is_nothrow_move_assignable<allocator_type>::value) ||
4440b57cec5SDimitry Andric               !__alloc_traits::propagate_on_container_move_assignment::value)
4450b57cec5SDimitry Andric{
4460b57cec5SDimitry Andric    clear();
4470b57cec5SDimitry Andric    shrink_to_fit();
4480b57cec5SDimitry Andric    __first_ = __c.__first_;
4490b57cec5SDimitry Andric    __begin_ = __c.__begin_;
4500b57cec5SDimitry Andric    __end_ = __c.__end_;
4510b57cec5SDimitry Andric    __end_cap() = __c.__end_cap();
4520b57cec5SDimitry Andric    __move_assign_alloc(__c,
4530b57cec5SDimitry Andric        integral_constant<bool,
4540b57cec5SDimitry Andric                          __alloc_traits::propagate_on_container_move_assignment::value>());
4550b57cec5SDimitry Andric    __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr;
4560b57cec5SDimitry Andric    return *this;
4570b57cec5SDimitry Andric}
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
460bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4610b57cec5SDimitry Andricvoid
4620b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x)
4630b57cec5SDimitry Andric        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value||
4640b57cec5SDimitry Andric                   __is_nothrow_swappable<__alloc_rr>::value)
4650b57cec5SDimitry Andric{
4660b57cec5SDimitry Andric    _VSTD::swap(__first_, __x.__first_);
4670b57cec5SDimitry Andric    _VSTD::swap(__begin_, __x.__begin_);
4680b57cec5SDimitry Andric    _VSTD::swap(__end_, __x.__end_);
4690b57cec5SDimitry Andric    _VSTD::swap(__end_cap(), __x.__end_cap());
470e8d8bef9SDimitry Andric    _VSTD::__swap_allocator(__alloc(), __x.__alloc());
4710b57cec5SDimitry Andric}
4720b57cec5SDimitry Andric
4730b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
474bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4750b57cec5SDimitry Andricvoid
4760b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n)
4770b57cec5SDimitry Andric{
4780b57cec5SDimitry Andric    if (__n < capacity())
4790b57cec5SDimitry Andric    {
4800b57cec5SDimitry Andric        __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc());
4810b57cec5SDimitry Andric        __t.__construct_at_end(move_iterator<pointer>(__begin_),
4820b57cec5SDimitry Andric                               move_iterator<pointer>(__end_));
4830b57cec5SDimitry Andric        _VSTD::swap(__first_, __t.__first_);
4840b57cec5SDimitry Andric        _VSTD::swap(__begin_, __t.__begin_);
4850b57cec5SDimitry Andric        _VSTD::swap(__end_, __t.__end_);
4860b57cec5SDimitry Andric        _VSTD::swap(__end_cap(), __t.__end_cap());
4870b57cec5SDimitry Andric    }
4880b57cec5SDimitry Andric}
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
491bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
4920b57cec5SDimitry Andricvoid
4930b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
4940b57cec5SDimitry Andric{
4950b57cec5SDimitry Andric    if (capacity() > size())
4960b57cec5SDimitry Andric    {
497*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
4980b57cec5SDimitry Andric        try
4990b57cec5SDimitry Andric        {
500*06c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
5010b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc());
5020b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5030b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5040b57cec5SDimitry Andric            __t.__end_ = __t.__begin_ + (__end_ - __begin_);
5050b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5060b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5070b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5080b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
509*06c3fb27SDimitry Andric#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
5100b57cec5SDimitry Andric        }
5110b57cec5SDimitry Andric        catch (...)
5120b57cec5SDimitry Andric        {
5130b57cec5SDimitry Andric        }
514*06c3fb27SDimitry Andric#endif // _LIBCPP_HAS_NO_EXCEPTIONS
5150b57cec5SDimitry Andric    }
5160b57cec5SDimitry Andric}
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
519bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
5200b57cec5SDimitry Andricvoid
5210b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x)
5220b57cec5SDimitry Andric{
5230b57cec5SDimitry Andric    if (__begin_ == __first_)
5240b57cec5SDimitry Andric    {
5250b57cec5SDimitry Andric        if (__end_ < __end_cap())
5260b57cec5SDimitry Andric        {
5270b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
5280b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5290b57cec5SDimitry Andric            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
5300b57cec5SDimitry Andric            __end_ += __d;
5310b57cec5SDimitry Andric        }
5320b57cec5SDimitry Andric        else
5330b57cec5SDimitry Andric        {
534bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5350b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
5360b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5370b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5380b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5390b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5400b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5410b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5420b57cec5SDimitry Andric        }
5430b57cec5SDimitry Andric    }
544480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x);
5450b57cec5SDimitry Andric    --__begin_;
5460b57cec5SDimitry Andric}
5470b57cec5SDimitry Andric
5480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
549bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
5500b57cec5SDimitry Andricvoid
5510b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x)
5520b57cec5SDimitry Andric{
5530b57cec5SDimitry Andric    if (__begin_ == __first_)
5540b57cec5SDimitry Andric    {
5550b57cec5SDimitry Andric        if (__end_ < __end_cap())
5560b57cec5SDimitry Andric        {
5570b57cec5SDimitry Andric            difference_type __d = __end_cap() - __end_;
5580b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5590b57cec5SDimitry Andric            __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d);
5600b57cec5SDimitry Andric            __end_ += __d;
5610b57cec5SDimitry Andric        }
5620b57cec5SDimitry Andric        else
5630b57cec5SDimitry Andric        {
564bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5650b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc());
5660b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5670b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
5680b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
5690b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
5700b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
5710b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
5720b57cec5SDimitry Andric        }
5730b57cec5SDimitry Andric    }
574480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1),
5750b57cec5SDimitry Andric            _VSTD::move(__x));
5760b57cec5SDimitry Andric    --__begin_;
5770b57cec5SDimitry Andric}
5780b57cec5SDimitry Andric
5790b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
580bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
581bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
5820b57cec5SDimitry Andricvoid
5830b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x)
5840b57cec5SDimitry Andric{
5850b57cec5SDimitry Andric    if (__end_ == __end_cap())
5860b57cec5SDimitry Andric    {
5870b57cec5SDimitry Andric        if (__begin_ > __first_)
5880b57cec5SDimitry Andric        {
5890b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
5900b57cec5SDimitry Andric            __d = (__d + 1) / 2;
5910b57cec5SDimitry Andric            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
5920b57cec5SDimitry Andric            __begin_ -= __d;
5930b57cec5SDimitry Andric        }
5940b57cec5SDimitry Andric        else
5950b57cec5SDimitry Andric        {
596bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
5970b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
5980b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
5990b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
6000b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
6010b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
6020b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
6030b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
6040b57cec5SDimitry Andric        }
6050b57cec5SDimitry Andric    }
606480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x);
6070b57cec5SDimitry Andric    ++__end_;
6080b57cec5SDimitry Andric}
6090b57cec5SDimitry Andric
6100b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
611bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
6120b57cec5SDimitry Andricvoid
6130b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x)
6140b57cec5SDimitry Andric{
6150b57cec5SDimitry Andric    if (__end_ == __end_cap())
6160b57cec5SDimitry Andric    {
6170b57cec5SDimitry Andric        if (__begin_ > __first_)
6180b57cec5SDimitry Andric        {
6190b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
6200b57cec5SDimitry Andric            __d = (__d + 1) / 2;
6210b57cec5SDimitry Andric            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
6220b57cec5SDimitry Andric            __begin_ -= __d;
6230b57cec5SDimitry Andric        }
6240b57cec5SDimitry Andric        else
6250b57cec5SDimitry Andric        {
626bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
6270b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
6280b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
6290b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
6300b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
6310b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
6320b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
6330b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
6340b57cec5SDimitry Andric        }
6350b57cec5SDimitry Andric    }
636480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
6370b57cec5SDimitry Andric            _VSTD::move(__x));
6380b57cec5SDimitry Andric    ++__end_;
6390b57cec5SDimitry Andric}
6400b57cec5SDimitry Andric
6410b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
6420b57cec5SDimitry Andrictemplate <class... _Args>
643bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
6440b57cec5SDimitry Andricvoid
6450b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args)
6460b57cec5SDimitry Andric{
6470b57cec5SDimitry Andric    if (__end_ == __end_cap())
6480b57cec5SDimitry Andric    {
6490b57cec5SDimitry Andric        if (__begin_ > __first_)
6500b57cec5SDimitry Andric        {
6510b57cec5SDimitry Andric            difference_type __d = __begin_ - __first_;
6520b57cec5SDimitry Andric            __d = (__d + 1) / 2;
6530b57cec5SDimitry Andric            __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d);
6540b57cec5SDimitry Andric            __begin_ -= __d;
6550b57cec5SDimitry Andric        }
6560b57cec5SDimitry Andric        else
6570b57cec5SDimitry Andric        {
658bdd1243dSDimitry Andric            size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1);
6590b57cec5SDimitry Andric            __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc());
6600b57cec5SDimitry Andric            __t.__construct_at_end(move_iterator<pointer>(__begin_),
6610b57cec5SDimitry Andric                                   move_iterator<pointer>(__end_));
6620b57cec5SDimitry Andric            _VSTD::swap(__first_, __t.__first_);
6630b57cec5SDimitry Andric            _VSTD::swap(__begin_, __t.__begin_);
6640b57cec5SDimitry Andric            _VSTD::swap(__end_, __t.__end_);
6650b57cec5SDimitry Andric            _VSTD::swap(__end_cap(), __t.__end_cap());
6660b57cec5SDimitry Andric        }
6670b57cec5SDimitry Andric    }
668480093f4SDimitry Andric    __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_),
6690b57cec5SDimitry Andric                              _VSTD::forward<_Args>(__args)...);
6700b57cec5SDimitry Andric    ++__end_;
6710b57cec5SDimitry Andric}
6720b57cec5SDimitry Andric
6730b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator>
674bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20
675bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
6760b57cec5SDimitry Andricvoid
6770b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y)
6780b57cec5SDimitry Andric        _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
6790b57cec5SDimitry Andric{
6800b57cec5SDimitry Andric    __x.swap(__y);
6810b57cec5SDimitry Andric}
6820b57cec5SDimitry Andric
6830b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
6840b57cec5SDimitry Andric
6850b57cec5SDimitry Andric_LIBCPP_POP_MACROS
6860b57cec5SDimitry Andric
68781ad6265SDimitry Andric#endif // _LIBCPP___SPLIT_BUFFER
688