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> 20*bdd1243dSDimitry Andric#include <__memory/allocate_at_least.h> 2181ad6265SDimitry Andric#include <__memory/allocator.h> 22*bdd1243dSDimitry Andric#include <__memory/allocator_traits.h> 2381ad6265SDimitry Andric#include <__memory/compressed_pair.h> 24*bdd1243dSDimitry Andric#include <__memory/pointer_traits.h> 25972a253aSDimitry Andric#include <__memory/swap_allocator.h> 26fe6060f1SDimitry Andric#include <__utility/forward.h> 27*bdd1243dSDimitry Andric#include <__utility/move.h> 28fe6060f1SDimitry Andric#include <type_traits> 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 310b57cec5SDimitry Andric# pragma GCC system_header 320b57cec5SDimitry Andric#endif 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 350b57cec5SDimitry Andric#include <__undef_macros> 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 390b57cec5SDimitry Andric 40*bdd1243dSDimitry Andric// __split_buffer allocates a contiguous chunk of memory and stores objects in the range [__begin_, __end_). 41*bdd1243dSDimitry Andric// It has uninitialized memory in the ranges [__first_, __begin_) and [__end_, __end_cap_.first()). That allows 42*bdd1243dSDimitry Andric// it to grow both in the front and back without having to move the data. 43*bdd1243dSDimitry Andric 440b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> > 450b57cec5SDimitry Andricstruct __split_buffer 460b57cec5SDimitry Andric{ 470b57cec5SDimitry Andricprivate: 480b57cec5SDimitry Andric __split_buffer(const __split_buffer&); 490b57cec5SDimitry Andric __split_buffer& operator=(const __split_buffer&); 500b57cec5SDimitry Andricpublic: 510b57cec5SDimitry Andric typedef _Tp value_type; 520b57cec5SDimitry Andric typedef _Allocator allocator_type; 53*bdd1243dSDimitry Andric typedef __libcpp_remove_reference_t<allocator_type> __alloc_rr; 540b57cec5SDimitry Andric typedef allocator_traits<__alloc_rr> __alloc_traits; 550b57cec5SDimitry Andric typedef value_type& reference; 560b57cec5SDimitry Andric typedef const value_type& const_reference; 570b57cec5SDimitry Andric typedef typename __alloc_traits::size_type size_type; 580b57cec5SDimitry Andric typedef typename __alloc_traits::difference_type difference_type; 590b57cec5SDimitry Andric typedef typename __alloc_traits::pointer pointer; 600b57cec5SDimitry Andric typedef typename __alloc_traits::const_pointer const_pointer; 610b57cec5SDimitry Andric typedef pointer iterator; 620b57cec5SDimitry Andric typedef const_pointer const_iterator; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric pointer __first_; 650b57cec5SDimitry Andric pointer __begin_; 660b57cec5SDimitry Andric pointer __end_; 670b57cec5SDimitry Andric __compressed_pair<pointer, allocator_type> __end_cap_; 680b57cec5SDimitry Andric 69*bdd1243dSDimitry Andric typedef __add_lvalue_reference_t<allocator_type> __alloc_ref; 70*bdd1243dSDimitry Andric typedef __add_lvalue_reference_t<allocator_type> __alloc_const_ref; 710b57cec5SDimitry Andric 72*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();} 73*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();} 74*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();} 75*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();} 760b57cec5SDimitry Andric 77*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 780b57cec5SDimitry Andric __split_buffer() 790b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); 80*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 810b57cec5SDimitry Andric explicit __split_buffer(__alloc_rr& __a); 82*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 830b57cec5SDimitry Andric explicit __split_buffer(const __alloc_rr& __a); 84*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a); 85*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~__split_buffer(); 860b57cec5SDimitry Andric 87*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c) 880b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); 89*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer(__split_buffer&& __c, const __alloc_rr& __a); 90*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __split_buffer& operator=(__split_buffer&& __c) 910b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 920b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 930b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value); 940b57cec5SDimitry Andric 95*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {return __begin_;} 96*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {return __begin_;} 97*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {return __end_;} 98*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {return __end_;} 990b57cec5SDimitry Andric 100*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1010b57cec5SDimitry Andric void clear() _NOEXCEPT 1020b57cec5SDimitry Andric {__destruct_at_end(__begin_);} 103*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const {return static_cast<size_type>(__end_ - __begin_);} 104*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const {return __end_ == __begin_;} 105*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);} 106*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);} 107*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);} 1080b57cec5SDimitry Andric 109*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference front() {return *__begin_;} 110*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference front() const {return *__begin_;} 111*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reference back() {return *(__end_ - 1);} 112*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reference back() const {return *(__end_ - 1);} 1130b57cec5SDimitry Andric 114*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n); 115*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void shrink_to_fit() _NOEXCEPT; 116*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(const_reference __x); 117*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(const_reference __x); 118*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_front(value_type&& __x); 119*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void push_back(value_type&& __x); 1200b57cec5SDimitry Andric template <class... _Args> 121*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void emplace_back(_Args&&... __args); 1220b57cec5SDimitry Andric 123*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_front() {__destruct_at_begin(__begin_+1);} 124*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void pop_back() {__destruct_at_end(__end_-1);} 1250b57cec5SDimitry Andric 126*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n); 127*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __construct_at_end(size_type __n, const_reference __x); 1280b57cec5SDimitry Andric template <class _InputIter> 129*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __enable_if_t<__is_exactly_cpp17_input_iterator<_InputIter>::value> 1300b57cec5SDimitry Andric __construct_at_end(_InputIter __first, _InputIter __last); 1310b57cec5SDimitry Andric template <class _ForwardIterator> 132*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value> 1330b57cec5SDimitry Andric __construct_at_end(_ForwardIterator __first, _ForwardIterator __last); 1340b57cec5SDimitry Andric 135*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __destruct_at_begin(pointer __new_begin) 1360b57cec5SDimitry Andric {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());} 137*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1380b57cec5SDimitry Andric void __destruct_at_begin(pointer __new_begin, false_type); 139*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1400b57cec5SDimitry Andric void __destruct_at_begin(pointer __new_begin, true_type); 1410b57cec5SDimitry Andric 142*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1430b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last) _NOEXCEPT 1440b57cec5SDimitry Andric {__destruct_at_end(__new_last, false_type());} 145*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1460b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT; 147*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1480b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT; 1490b57cec5SDimitry Andric 150*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void swap(__split_buffer& __x) 1510b57cec5SDimitry Andric _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| 1520b57cec5SDimitry Andric __is_nothrow_swappable<__alloc_rr>::value); 1530b57cec5SDimitry Andric 154*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool __invariants() const; 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andricprivate: 157*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1580b57cec5SDimitry Andric void __move_assign_alloc(__split_buffer& __c, true_type) 1590b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) 1600b57cec5SDimitry Andric { 1610b57cec5SDimitry Andric __alloc() = _VSTD::move(__c.__alloc()); 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 164*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI 1650b57cec5SDimitry Andric void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT 1660b57cec5SDimitry Andric {} 167e40139ffSDimitry Andric 168e40139ffSDimitry Andric struct _ConstructTransaction { 169*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT 170e40139ffSDimitry Andric : __pos_(*__p), __end_(*__p + __n), __dest_(__p) { 171e40139ffSDimitry Andric } 172*bdd1243dSDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI ~_ConstructTransaction() { 173e40139ffSDimitry Andric *__dest_ = __pos_; 174e40139ffSDimitry Andric } 175e40139ffSDimitry Andric pointer __pos_; 176e40139ffSDimitry Andric const pointer __end_; 177e40139ffSDimitry Andric private: 178e40139ffSDimitry Andric pointer *__dest_; 179e40139ffSDimitry Andric }; 1800b57cec5SDimitry Andric}; 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 183*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 1840b57cec5SDimitry Andricbool 1850b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__invariants() const 1860b57cec5SDimitry Andric{ 1870b57cec5SDimitry Andric if (__first_ == nullptr) 1880b57cec5SDimitry Andric { 1890b57cec5SDimitry Andric if (__begin_ != nullptr) 1900b57cec5SDimitry Andric return false; 1910b57cec5SDimitry Andric if (__end_ != nullptr) 1920b57cec5SDimitry Andric return false; 1930b57cec5SDimitry Andric if (__end_cap() != nullptr) 1940b57cec5SDimitry Andric return false; 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric else 1970b57cec5SDimitry Andric { 1980b57cec5SDimitry Andric if (__begin_ < __first_) 1990b57cec5SDimitry Andric return false; 2000b57cec5SDimitry Andric if (__end_ < __begin_) 2010b57cec5SDimitry Andric return false; 2020b57cec5SDimitry Andric if (__end_cap() < __end_) 2030b57cec5SDimitry Andric return false; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric return true; 2060b57cec5SDimitry Andric} 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric// Default constructs __n objects starting at __end_ 2090b57cec5SDimitry Andric// throws if construction throws 2100b57cec5SDimitry Andric// Precondition: __n > 0 2110b57cec5SDimitry Andric// Precondition: size() + __n <= capacity() 2120b57cec5SDimitry Andric// Postcondition: size() == size() + __n 2130b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 214*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 2150b57cec5SDimitry Andricvoid 2160b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) 2170b57cec5SDimitry Andric{ 218e40139ffSDimitry Andric _ConstructTransaction __tx(&this->__end_, __n); 219e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { 220480093f4SDimitry Andric __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_)); 221e40139ffSDimitry Andric } 2220b57cec5SDimitry Andric} 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric// Copy constructs __n objects starting at __end_ from __x 2250b57cec5SDimitry Andric// throws if construction throws 2260b57cec5SDimitry Andric// Precondition: __n > 0 2270b57cec5SDimitry Andric// Precondition: size() + __n <= capacity() 2280b57cec5SDimitry Andric// Postcondition: size() == old size() + __n 2290b57cec5SDimitry Andric// Postcondition: [i] == __x for all i in [size() - __n, __n) 2300b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 231*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 2320b57cec5SDimitry Andricvoid 2330b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) 2340b57cec5SDimitry Andric{ 235e40139ffSDimitry Andric _ConstructTransaction __tx(&this->__end_, __n); 236e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { 237e40139ffSDimitry Andric __alloc_traits::construct(this->__alloc(), 238480093f4SDimitry Andric _VSTD::__to_address(__tx.__pos_), __x); 239e40139ffSDimitry Andric } 2400b57cec5SDimitry Andric} 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2430b57cec5SDimitry Andrictemplate <class _InputIter> 244*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 __enable_if_t<__is_exactly_cpp17_input_iterator<_InputIter>::value> 2450b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) 2460b57cec5SDimitry Andric{ 2470b57cec5SDimitry Andric __alloc_rr& __a = this->__alloc(); 2480b57cec5SDimitry Andric for (; __first != __last; ++__first) 2490b57cec5SDimitry Andric { 2500b57cec5SDimitry Andric if (__end_ == __end_cap()) 2510b57cec5SDimitry Andric { 2520b57cec5SDimitry Andric size_type __old_cap = __end_cap() - __first_; 2530b57cec5SDimitry Andric size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8); 2540b57cec5SDimitry Andric __split_buffer __buf(__new_cap, 0, __a); 255349cc55cSDimitry Andric for (pointer __p = __begin_; __p != __end_; ++__p, (void) ++__buf.__end_) 2560b57cec5SDimitry Andric __alloc_traits::construct(__buf.__alloc(), 257480093f4SDimitry Andric _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p)); 2580b57cec5SDimitry Andric swap(__buf); 2590b57cec5SDimitry Andric } 260480093f4SDimitry Andric __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first); 2610b57cec5SDimitry Andric ++this->__end_; 2620b57cec5SDimitry Andric } 2630b57cec5SDimitry Andric} 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 2660b57cec5SDimitry Andrictemplate <class _ForwardIterator> 267*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 __enable_if_t<__is_cpp17_forward_iterator<_ForwardIterator>::value> 2680b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) 2690b57cec5SDimitry Andric{ 270e8d8bef9SDimitry Andric _ConstructTransaction __tx(&this->__end_, _VSTD::distance(__first, __last)); 271349cc55cSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, (void) ++__first) { 272e40139ffSDimitry Andric __alloc_traits::construct(this->__alloc(), 273480093f4SDimitry Andric _VSTD::__to_address(__tx.__pos_), *__first); 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric} 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 278*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 2790b57cec5SDimitry Andricinline 2800b57cec5SDimitry Andricvoid 2810b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) 2820b57cec5SDimitry Andric{ 2830b57cec5SDimitry Andric while (__begin_ != __new_begin) 284e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++)); 2850b57cec5SDimitry Andric} 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 288*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 2890b57cec5SDimitry Andricinline 2900b57cec5SDimitry Andricvoid 2910b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) 2920b57cec5SDimitry Andric{ 2930b57cec5SDimitry Andric __begin_ = __new_begin; 2940b57cec5SDimitry Andric} 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 297*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 298*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI 2990b57cec5SDimitry Andricvoid 3000b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT 3010b57cec5SDimitry Andric{ 3020b57cec5SDimitry Andric while (__new_last != __end_) 303e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_)); 3040b57cec5SDimitry Andric} 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 307*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 308*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI 3090b57cec5SDimitry Andricvoid 3100b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT 3110b57cec5SDimitry Andric{ 3120b57cec5SDimitry Andric __end_ = __new_last; 3130b57cec5SDimitry Andric} 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 316*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 3170b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a) 3180b57cec5SDimitry Andric : __end_cap_(nullptr, __a) 3190b57cec5SDimitry Andric{ 32081ad6265SDimitry Andric if (__cap == 0) { 32181ad6265SDimitry Andric __first_ = nullptr; 32281ad6265SDimitry Andric } else { 32381ad6265SDimitry Andric auto __allocation = std::__allocate_at_least(__alloc(), __cap); 32481ad6265SDimitry Andric __first_ = __allocation.ptr; 32581ad6265SDimitry Andric __cap = __allocation.count; 32681ad6265SDimitry Andric } 3270b57cec5SDimitry Andric __begin_ = __end_ = __first_ + __start; 3280b57cec5SDimitry Andric __end_cap() = __first_ + __cap; 3290b57cec5SDimitry Andric} 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 332*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 3330b57cec5SDimitry Andricinline 3340b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer() 3350b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) 336480093f4SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) 3370b57cec5SDimitry Andric{ 3380b57cec5SDimitry Andric} 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 341*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 3420b57cec5SDimitry Andricinline 3430b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a) 3440b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 3450b57cec5SDimitry Andric{ 3460b57cec5SDimitry Andric} 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 349*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 3500b57cec5SDimitry Andricinline 3510b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a) 3520b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 3530b57cec5SDimitry Andric{ 3540b57cec5SDimitry Andric} 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 357*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 3580b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer() 3590b57cec5SDimitry Andric{ 3600b57cec5SDimitry Andric clear(); 3610b57cec5SDimitry Andric if (__first_) 3620b57cec5SDimitry Andric __alloc_traits::deallocate(__alloc(), __first_, capacity()); 3630b57cec5SDimitry Andric} 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 366*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 3670b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c) 3680b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) 3690b57cec5SDimitry Andric : __first_(_VSTD::move(__c.__first_)), 3700b57cec5SDimitry Andric __begin_(_VSTD::move(__c.__begin_)), 3710b57cec5SDimitry Andric __end_(_VSTD::move(__c.__end_)), 3720b57cec5SDimitry Andric __end_cap_(_VSTD::move(__c.__end_cap_)) 3730b57cec5SDimitry Andric{ 3740b57cec5SDimitry Andric __c.__first_ = nullptr; 3750b57cec5SDimitry Andric __c.__begin_ = nullptr; 3760b57cec5SDimitry Andric __c.__end_ = nullptr; 3770b57cec5SDimitry Andric __c.__end_cap() = nullptr; 3780b57cec5SDimitry Andric} 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 381*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 3820b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a) 383480093f4SDimitry Andric : __end_cap_(nullptr, __a) 3840b57cec5SDimitry Andric{ 3850b57cec5SDimitry Andric if (__a == __c.__alloc()) 3860b57cec5SDimitry Andric { 3870b57cec5SDimitry Andric __first_ = __c.__first_; 3880b57cec5SDimitry Andric __begin_ = __c.__begin_; 3890b57cec5SDimitry Andric __end_ = __c.__end_; 3900b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 3910b57cec5SDimitry Andric __c.__first_ = nullptr; 3920b57cec5SDimitry Andric __c.__begin_ = nullptr; 3930b57cec5SDimitry Andric __c.__end_ = nullptr; 3940b57cec5SDimitry Andric __c.__end_cap() = nullptr; 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric else 3970b57cec5SDimitry Andric { 39881ad6265SDimitry Andric auto __allocation = std::__allocate_at_least(__alloc(), __c.size()); 39981ad6265SDimitry Andric __first_ = __allocation.ptr; 4000b57cec5SDimitry Andric __begin_ = __end_ = __first_; 40181ad6265SDimitry Andric __end_cap() = __first_ + __allocation.count; 4020b57cec5SDimitry Andric typedef move_iterator<iterator> _Ip; 4030b57cec5SDimitry Andric __construct_at_end(_Ip(__c.begin()), _Ip(__c.end())); 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric} 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 408*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 4090b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>& 4100b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c) 4110b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 4120b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 4130b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value) 4140b57cec5SDimitry Andric{ 4150b57cec5SDimitry Andric clear(); 4160b57cec5SDimitry Andric shrink_to_fit(); 4170b57cec5SDimitry Andric __first_ = __c.__first_; 4180b57cec5SDimitry Andric __begin_ = __c.__begin_; 4190b57cec5SDimitry Andric __end_ = __c.__end_; 4200b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 4210b57cec5SDimitry Andric __move_assign_alloc(__c, 4220b57cec5SDimitry Andric integral_constant<bool, 4230b57cec5SDimitry Andric __alloc_traits::propagate_on_container_move_assignment::value>()); 4240b57cec5SDimitry Andric __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr; 4250b57cec5SDimitry Andric return *this; 4260b57cec5SDimitry Andric} 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 429*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 4300b57cec5SDimitry Andricvoid 4310b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x) 4320b57cec5SDimitry Andric _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| 4330b57cec5SDimitry Andric __is_nothrow_swappable<__alloc_rr>::value) 4340b57cec5SDimitry Andric{ 4350b57cec5SDimitry Andric _VSTD::swap(__first_, __x.__first_); 4360b57cec5SDimitry Andric _VSTD::swap(__begin_, __x.__begin_); 4370b57cec5SDimitry Andric _VSTD::swap(__end_, __x.__end_); 4380b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __x.__end_cap()); 439e8d8bef9SDimitry Andric _VSTD::__swap_allocator(__alloc(), __x.__alloc()); 4400b57cec5SDimitry Andric} 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 443*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 4440b57cec5SDimitry Andricvoid 4450b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n) 4460b57cec5SDimitry Andric{ 4470b57cec5SDimitry Andric if (__n < capacity()) 4480b57cec5SDimitry Andric { 4490b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc()); 4500b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4510b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4520b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4530b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4540b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4550b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric} 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 460*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 4610b57cec5SDimitry Andricvoid 4620b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT 4630b57cec5SDimitry Andric{ 4640b57cec5SDimitry Andric if (capacity() > size()) 4650b57cec5SDimitry Andric { 4660b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 4670b57cec5SDimitry Andric try 4680b57cec5SDimitry Andric { 4690b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 4700b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc()); 4710b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 4720b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 4730b57cec5SDimitry Andric __t.__end_ = __t.__begin_ + (__end_ - __begin_); 4740b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 4750b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 4760b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 4770b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 4780b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric catch (...) 4810b57cec5SDimitry Andric { 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric} 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 488*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 4890b57cec5SDimitry Andricvoid 4900b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x) 4910b57cec5SDimitry Andric{ 4920b57cec5SDimitry Andric if (__begin_ == __first_) 4930b57cec5SDimitry Andric { 4940b57cec5SDimitry Andric if (__end_ < __end_cap()) 4950b57cec5SDimitry Andric { 4960b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 4970b57cec5SDimitry Andric __d = (__d + 1) / 2; 4980b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 4990b57cec5SDimitry Andric __end_ += __d; 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric else 5020b57cec5SDimitry Andric { 503*bdd1243dSDimitry Andric size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5040b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 5050b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5060b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5070b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5080b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5090b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5100b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric } 513480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x); 5140b57cec5SDimitry Andric --__begin_; 5150b57cec5SDimitry Andric} 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 518*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 5190b57cec5SDimitry Andricvoid 5200b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) 5210b57cec5SDimitry Andric{ 5220b57cec5SDimitry Andric if (__begin_ == __first_) 5230b57cec5SDimitry Andric { 5240b57cec5SDimitry Andric if (__end_ < __end_cap()) 5250b57cec5SDimitry Andric { 5260b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 5270b57cec5SDimitry Andric __d = (__d + 1) / 2; 5280b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 5290b57cec5SDimitry Andric __end_ += __d; 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric else 5320b57cec5SDimitry Andric { 533*bdd1243dSDimitry Andric size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5340b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 5350b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5360b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5370b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5380b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5390b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5400b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric } 543480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), 5440b57cec5SDimitry Andric _VSTD::move(__x)); 5450b57cec5SDimitry Andric --__begin_; 5460b57cec5SDimitry Andric} 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 549*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 550*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI 5510b57cec5SDimitry Andricvoid 5520b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) 5530b57cec5SDimitry Andric{ 5540b57cec5SDimitry Andric if (__end_ == __end_cap()) 5550b57cec5SDimitry Andric { 5560b57cec5SDimitry Andric if (__begin_ > __first_) 5570b57cec5SDimitry Andric { 5580b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5590b57cec5SDimitry Andric __d = (__d + 1) / 2; 5600b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5610b57cec5SDimitry Andric __begin_ -= __d; 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric else 5640b57cec5SDimitry Andric { 565*bdd1243dSDimitry Andric size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5660b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 5670b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5680b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5690b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 5700b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 5710b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 5720b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 5730b57cec5SDimitry Andric } 5740b57cec5SDimitry Andric } 575480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x); 5760b57cec5SDimitry Andric ++__end_; 5770b57cec5SDimitry Andric} 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 580*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 5810b57cec5SDimitry Andricvoid 5820b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) 5830b57cec5SDimitry Andric{ 5840b57cec5SDimitry Andric if (__end_ == __end_cap()) 5850b57cec5SDimitry Andric { 5860b57cec5SDimitry Andric if (__begin_ > __first_) 5870b57cec5SDimitry Andric { 5880b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 5890b57cec5SDimitry Andric __d = (__d + 1) / 2; 5900b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 5910b57cec5SDimitry Andric __begin_ -= __d; 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric else 5940b57cec5SDimitry Andric { 595*bdd1243dSDimitry Andric size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 5960b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 5970b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 5980b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 5990b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 6000b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 6010b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 6020b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric } 605480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 6060b57cec5SDimitry Andric _VSTD::move(__x)); 6070b57cec5SDimitry Andric ++__end_; 6080b57cec5SDimitry Andric} 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 6110b57cec5SDimitry Andrictemplate <class... _Args> 612*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 6130b57cec5SDimitry Andricvoid 6140b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) 6150b57cec5SDimitry Andric{ 6160b57cec5SDimitry Andric if (__end_ == __end_cap()) 6170b57cec5SDimitry Andric { 6180b57cec5SDimitry Andric if (__begin_ > __first_) 6190b57cec5SDimitry Andric { 6200b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 6210b57cec5SDimitry Andric __d = (__d + 1) / 2; 6220b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 6230b57cec5SDimitry Andric __begin_ -= __d; 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric else 6260b57cec5SDimitry Andric { 627*bdd1243dSDimitry Andric size_type __c = std::max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 6280b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 6290b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 6300b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 6310b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 6320b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 6330b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 6340b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric } 637480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 6380b57cec5SDimitry Andric _VSTD::forward<_Args>(__args)...); 6390b57cec5SDimitry Andric ++__end_; 6400b57cec5SDimitry Andric} 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 643*bdd1243dSDimitry Andric_LIBCPP_CONSTEXPR_SINCE_CXX20 644*bdd1243dSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI 6450b57cec5SDimitry Andricvoid 6460b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) 6470b57cec5SDimitry Andric _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 6480b57cec5SDimitry Andric{ 6490b57cec5SDimitry Andric __x.swap(__y); 6500b57cec5SDimitry Andric} 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric_LIBCPP_POP_MACROS 6550b57cec5SDimitry Andric 65681ad6265SDimitry Andric#endif // _LIBCPP___SPLIT_BUFFER 657