1*0b57cec5SDimitry Andric// -*- C++ -*- 2*0b57cec5SDimitry Andric#ifndef _LIBCPP_SPLIT_BUFFER 3*0b57cec5SDimitry Andric#define _LIBCPP_SPLIT_BUFFER 4*0b57cec5SDimitry Andric 5*0b57cec5SDimitry Andric#include <__config> 6*0b57cec5SDimitry Andric#include <type_traits> 7*0b57cec5SDimitry Andric#include <algorithm> 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 10*0b57cec5SDimitry Andric#pragma GCC system_header 11*0b57cec5SDimitry Andric#endif 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS 14*0b57cec5SDimitry Andric#include <__undef_macros> 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric 17*0b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 18*0b57cec5SDimitry Andric 19*0b57cec5SDimitry Andrictemplate <bool> 20*0b57cec5SDimitry Andricclass __split_buffer_common 21*0b57cec5SDimitry Andric{ 22*0b57cec5SDimitry Andricprotected: 23*0b57cec5SDimitry Andric void __throw_length_error() const; 24*0b57cec5SDimitry Andric void __throw_out_of_range() const; 25*0b57cec5SDimitry Andric}; 26*0b57cec5SDimitry Andric 27*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator = allocator<_Tp> > 28*0b57cec5SDimitry Andricstruct __split_buffer 29*0b57cec5SDimitry Andric : private __split_buffer_common<true> 30*0b57cec5SDimitry Andric{ 31*0b57cec5SDimitry Andricprivate: 32*0b57cec5SDimitry Andric __split_buffer(const __split_buffer&); 33*0b57cec5SDimitry Andric __split_buffer& operator=(const __split_buffer&); 34*0b57cec5SDimitry Andricpublic: 35*0b57cec5SDimitry Andric typedef _Tp value_type; 36*0b57cec5SDimitry Andric typedef _Allocator allocator_type; 37*0b57cec5SDimitry Andric typedef typename remove_reference<allocator_type>::type __alloc_rr; 38*0b57cec5SDimitry Andric typedef allocator_traits<__alloc_rr> __alloc_traits; 39*0b57cec5SDimitry Andric typedef value_type& reference; 40*0b57cec5SDimitry Andric typedef const value_type& const_reference; 41*0b57cec5SDimitry Andric typedef typename __alloc_traits::size_type size_type; 42*0b57cec5SDimitry Andric typedef typename __alloc_traits::difference_type difference_type; 43*0b57cec5SDimitry Andric typedef typename __alloc_traits::pointer pointer; 44*0b57cec5SDimitry Andric typedef typename __alloc_traits::const_pointer const_pointer; 45*0b57cec5SDimitry Andric typedef pointer iterator; 46*0b57cec5SDimitry Andric typedef const_pointer const_iterator; 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric pointer __first_; 49*0b57cec5SDimitry Andric pointer __begin_; 50*0b57cec5SDimitry Andric pointer __end_; 51*0b57cec5SDimitry Andric __compressed_pair<pointer, allocator_type> __end_cap_; 52*0b57cec5SDimitry Andric 53*0b57cec5SDimitry Andric typedef typename add_lvalue_reference<allocator_type>::type __alloc_ref; 54*0b57cec5SDimitry Andric typedef typename add_lvalue_reference<allocator_type>::type __alloc_const_ref; 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY __alloc_rr& __alloc() _NOEXCEPT {return __end_cap_.second();} 57*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const __alloc_rr& __alloc() const _NOEXCEPT {return __end_cap_.second();} 58*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY pointer& __end_cap() _NOEXCEPT {return __end_cap_.first();} 59*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const pointer& __end_cap() const _NOEXCEPT {return __end_cap_.first();} 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 62*0b57cec5SDimitry Andric __split_buffer() 63*0b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value); 64*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 65*0b57cec5SDimitry Andric explicit __split_buffer(__alloc_rr& __a); 66*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 67*0b57cec5SDimitry Andric explicit __split_buffer(const __alloc_rr& __a); 68*0b57cec5SDimitry Andric __split_buffer(size_type __cap, size_type __start, __alloc_rr& __a); 69*0b57cec5SDimitry Andric ~__split_buffer(); 70*0b57cec5SDimitry Andric 71*0b57cec5SDimitry Andric __split_buffer(__split_buffer&& __c) 72*0b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value); 73*0b57cec5SDimitry Andric __split_buffer(__split_buffer&& __c, const __alloc_rr& __a); 74*0b57cec5SDimitry Andric __split_buffer& operator=(__split_buffer&& __c) 75*0b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 76*0b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 77*0b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value); 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY iterator begin() _NOEXCEPT {return __begin_;} 80*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_iterator begin() const _NOEXCEPT {return __begin_;} 81*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY iterator end() _NOEXCEPT {return __end_;} 82*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_iterator end() const _NOEXCEPT {return __end_;} 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 85*0b57cec5SDimitry Andric void clear() _NOEXCEPT 86*0b57cec5SDimitry Andric {__destruct_at_end(__begin_);} 87*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type size() const {return static_cast<size_type>(__end_ - __begin_);} 88*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY bool empty() const {return __end_ == __begin_;} 89*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type capacity() const {return static_cast<size_type>(__end_cap() - __first_);} 90*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type __front_spare() const {return static_cast<size_type>(__begin_ - __first_);} 91*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY size_type __back_spare() const {return static_cast<size_type>(__end_cap() - __end_);} 92*0b57cec5SDimitry Andric 93*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY reference front() {return *__begin_;} 94*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_reference front() const {return *__begin_;} 95*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY reference back() {return *(__end_ - 1);} 96*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY const_reference back() const {return *(__end_ - 1);} 97*0b57cec5SDimitry Andric 98*0b57cec5SDimitry Andric void reserve(size_type __n); 99*0b57cec5SDimitry Andric void shrink_to_fit() _NOEXCEPT; 100*0b57cec5SDimitry Andric void push_front(const_reference __x); 101*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x); 102*0b57cec5SDimitry Andric void push_front(value_type&& __x); 103*0b57cec5SDimitry Andric void push_back(value_type&& __x); 104*0b57cec5SDimitry Andric template <class... _Args> 105*0b57cec5SDimitry Andric void emplace_back(_Args&&... __args); 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void pop_front() {__destruct_at_begin(__begin_+1);} 108*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void pop_back() {__destruct_at_end(__end_-1);} 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric void __construct_at_end(size_type __n); 111*0b57cec5SDimitry Andric void __construct_at_end(size_type __n, const_reference __x); 112*0b57cec5SDimitry Andric template <class _InputIter> 113*0b57cec5SDimitry Andric typename enable_if 114*0b57cec5SDimitry Andric < 115480093f4SDimitry Andric __is_cpp17_input_iterator<_InputIter>::value && 116480093f4SDimitry Andric !__is_cpp17_forward_iterator<_InputIter>::value, 117*0b57cec5SDimitry Andric void 118*0b57cec5SDimitry Andric >::type 119*0b57cec5SDimitry Andric __construct_at_end(_InputIter __first, _InputIter __last); 120*0b57cec5SDimitry Andric template <class _ForwardIterator> 121*0b57cec5SDimitry Andric typename enable_if 122*0b57cec5SDimitry Andric < 123480093f4SDimitry Andric __is_cpp17_forward_iterator<_ForwardIterator>::value, 124*0b57cec5SDimitry Andric void 125*0b57cec5SDimitry Andric >::type 126*0b57cec5SDimitry Andric __construct_at_end(_ForwardIterator __first, _ForwardIterator __last); 127*0b57cec5SDimitry Andric 128*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY void __destruct_at_begin(pointer __new_begin) 129*0b57cec5SDimitry Andric {__destruct_at_begin(__new_begin, is_trivially_destructible<value_type>());} 130*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 131*0b57cec5SDimitry Andric void __destruct_at_begin(pointer __new_begin, false_type); 132*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 133*0b57cec5SDimitry Andric void __destruct_at_begin(pointer __new_begin, true_type); 134*0b57cec5SDimitry Andric 135*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 136*0b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last) _NOEXCEPT 137*0b57cec5SDimitry Andric {__destruct_at_end(__new_last, false_type());} 138*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 139*0b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last, false_type) _NOEXCEPT; 140*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 141*0b57cec5SDimitry Andric void __destruct_at_end(pointer __new_last, true_type) _NOEXCEPT; 142*0b57cec5SDimitry Andric 143*0b57cec5SDimitry Andric void swap(__split_buffer& __x) 144*0b57cec5SDimitry Andric _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| 145*0b57cec5SDimitry Andric __is_nothrow_swappable<__alloc_rr>::value); 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric bool __invariants() const; 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andricprivate: 150*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 151*0b57cec5SDimitry Andric void __move_assign_alloc(__split_buffer& __c, true_type) 152*0b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_assignable<allocator_type>::value) 153*0b57cec5SDimitry Andric { 154*0b57cec5SDimitry Andric __alloc() = _VSTD::move(__c.__alloc()); 155*0b57cec5SDimitry Andric } 156*0b57cec5SDimitry Andric 157*0b57cec5SDimitry Andric _LIBCPP_INLINE_VISIBILITY 158*0b57cec5SDimitry Andric void __move_assign_alloc(__split_buffer&, false_type) _NOEXCEPT 159*0b57cec5SDimitry Andric {} 160e40139ffSDimitry Andric 161e40139ffSDimitry Andric struct _ConstructTransaction { 162e40139ffSDimitry Andric explicit _ConstructTransaction(pointer* __p, size_type __n) _NOEXCEPT 163e40139ffSDimitry Andric : __pos_(*__p), __end_(*__p + __n), __dest_(__p) { 164e40139ffSDimitry Andric } 165e40139ffSDimitry Andric ~_ConstructTransaction() { 166e40139ffSDimitry Andric *__dest_ = __pos_; 167e40139ffSDimitry Andric } 168e40139ffSDimitry Andric pointer __pos_; 169e40139ffSDimitry Andric const pointer __end_; 170e40139ffSDimitry Andric private: 171e40139ffSDimitry Andric pointer *__dest_; 172e40139ffSDimitry Andric }; 173*0b57cec5SDimitry Andric}; 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 176*0b57cec5SDimitry Andricbool 177*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__invariants() const 178*0b57cec5SDimitry Andric{ 179*0b57cec5SDimitry Andric if (__first_ == nullptr) 180*0b57cec5SDimitry Andric { 181*0b57cec5SDimitry Andric if (__begin_ != nullptr) 182*0b57cec5SDimitry Andric return false; 183*0b57cec5SDimitry Andric if (__end_ != nullptr) 184*0b57cec5SDimitry Andric return false; 185*0b57cec5SDimitry Andric if (__end_cap() != nullptr) 186*0b57cec5SDimitry Andric return false; 187*0b57cec5SDimitry Andric } 188*0b57cec5SDimitry Andric else 189*0b57cec5SDimitry Andric { 190*0b57cec5SDimitry Andric if (__begin_ < __first_) 191*0b57cec5SDimitry Andric return false; 192*0b57cec5SDimitry Andric if (__end_ < __begin_) 193*0b57cec5SDimitry Andric return false; 194*0b57cec5SDimitry Andric if (__end_cap() < __end_) 195*0b57cec5SDimitry Andric return false; 196*0b57cec5SDimitry Andric } 197*0b57cec5SDimitry Andric return true; 198*0b57cec5SDimitry Andric} 199*0b57cec5SDimitry Andric 200*0b57cec5SDimitry Andric// Default constructs __n objects starting at __end_ 201*0b57cec5SDimitry Andric// throws if construction throws 202*0b57cec5SDimitry Andric// Precondition: __n > 0 203*0b57cec5SDimitry Andric// Precondition: size() + __n <= capacity() 204*0b57cec5SDimitry Andric// Postcondition: size() == size() + __n 205*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 206*0b57cec5SDimitry Andricvoid 207*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n) 208*0b57cec5SDimitry Andric{ 209e40139ffSDimitry Andric _ConstructTransaction __tx(&this->__end_, __n); 210e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { 211480093f4SDimitry Andric __alloc_traits::construct(this->__alloc(), _VSTD::__to_address(__tx.__pos_)); 212e40139ffSDimitry Andric } 213*0b57cec5SDimitry Andric} 214*0b57cec5SDimitry Andric 215*0b57cec5SDimitry Andric// Copy constructs __n objects starting at __end_ from __x 216*0b57cec5SDimitry Andric// throws if construction throws 217*0b57cec5SDimitry Andric// Precondition: __n > 0 218*0b57cec5SDimitry Andric// Precondition: size() + __n <= capacity() 219*0b57cec5SDimitry Andric// Postcondition: size() == old size() + __n 220*0b57cec5SDimitry Andric// Postcondition: [i] == __x for all i in [size() - __n, __n) 221*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 222*0b57cec5SDimitry Andricvoid 223*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(size_type __n, const_reference __x) 224*0b57cec5SDimitry Andric{ 225e40139ffSDimitry Andric _ConstructTransaction __tx(&this->__end_, __n); 226e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_) { 227e40139ffSDimitry Andric __alloc_traits::construct(this->__alloc(), 228480093f4SDimitry Andric _VSTD::__to_address(__tx.__pos_), __x); 229e40139ffSDimitry Andric } 230*0b57cec5SDimitry Andric} 231*0b57cec5SDimitry Andric 232*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 233*0b57cec5SDimitry Andrictemplate <class _InputIter> 234*0b57cec5SDimitry Andrictypename enable_if 235*0b57cec5SDimitry Andric< 236480093f4SDimitry Andric __is_cpp17_input_iterator<_InputIter>::value && 237480093f4SDimitry Andric !__is_cpp17_forward_iterator<_InputIter>::value, 238*0b57cec5SDimitry Andric void 239*0b57cec5SDimitry Andric>::type 240*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_InputIter __first, _InputIter __last) 241*0b57cec5SDimitry Andric{ 242*0b57cec5SDimitry Andric __alloc_rr& __a = this->__alloc(); 243*0b57cec5SDimitry Andric for (; __first != __last; ++__first) 244*0b57cec5SDimitry Andric { 245*0b57cec5SDimitry Andric if (__end_ == __end_cap()) 246*0b57cec5SDimitry Andric { 247*0b57cec5SDimitry Andric size_type __old_cap = __end_cap() - __first_; 248*0b57cec5SDimitry Andric size_type __new_cap = _VSTD::max<size_type>(2 * __old_cap, 8); 249*0b57cec5SDimitry Andric __split_buffer __buf(__new_cap, 0, __a); 250*0b57cec5SDimitry Andric for (pointer __p = __begin_; __p != __end_; ++__p, ++__buf.__end_) 251*0b57cec5SDimitry Andric __alloc_traits::construct(__buf.__alloc(), 252480093f4SDimitry Andric _VSTD::__to_address(__buf.__end_), _VSTD::move(*__p)); 253*0b57cec5SDimitry Andric swap(__buf); 254*0b57cec5SDimitry Andric } 255480093f4SDimitry Andric __alloc_traits::construct(__a, _VSTD::__to_address(this->__end_), *__first); 256*0b57cec5SDimitry Andric ++this->__end_; 257*0b57cec5SDimitry Andric } 258*0b57cec5SDimitry Andric} 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 261*0b57cec5SDimitry Andrictemplate <class _ForwardIterator> 262*0b57cec5SDimitry Andrictypename enable_if 263*0b57cec5SDimitry Andric< 264480093f4SDimitry Andric __is_cpp17_forward_iterator<_ForwardIterator>::value, 265*0b57cec5SDimitry Andric void 266*0b57cec5SDimitry Andric>::type 267*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__construct_at_end(_ForwardIterator __first, _ForwardIterator __last) 268*0b57cec5SDimitry Andric{ 269e8d8bef9SDimitry Andric _ConstructTransaction __tx(&this->__end_, _VSTD::distance(__first, __last)); 270e40139ffSDimitry Andric for (; __tx.__pos_ != __tx.__end_; ++__tx.__pos_, ++__first) { 271e40139ffSDimitry Andric __alloc_traits::construct(this->__alloc(), 272480093f4SDimitry Andric _VSTD::__to_address(__tx.__pos_), *__first); 273*0b57cec5SDimitry Andric } 274*0b57cec5SDimitry Andric} 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 277*0b57cec5SDimitry Andricinline 278*0b57cec5SDimitry Andricvoid 279*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, false_type) 280*0b57cec5SDimitry Andric{ 281*0b57cec5SDimitry Andric while (__begin_ != __new_begin) 282e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(__begin_++)); 283*0b57cec5SDimitry Andric} 284*0b57cec5SDimitry Andric 285*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 286*0b57cec5SDimitry Andricinline 287*0b57cec5SDimitry Andricvoid 288*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_begin(pointer __new_begin, true_type) 289*0b57cec5SDimitry Andric{ 290*0b57cec5SDimitry Andric __begin_ = __new_begin; 291*0b57cec5SDimitry Andric} 292*0b57cec5SDimitry Andric 293*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 294*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 295*0b57cec5SDimitry Andricvoid 296*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, false_type) _NOEXCEPT 297*0b57cec5SDimitry Andric{ 298*0b57cec5SDimitry Andric while (__new_last != __end_) 299e8d8bef9SDimitry Andric __alloc_traits::destroy(__alloc(), _VSTD::__to_address(--__end_)); 300*0b57cec5SDimitry Andric} 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 303*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 304*0b57cec5SDimitry Andricvoid 305*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__destruct_at_end(pointer __new_last, true_type) _NOEXCEPT 306*0b57cec5SDimitry Andric{ 307*0b57cec5SDimitry Andric __end_ = __new_last; 308*0b57cec5SDimitry Andric} 309*0b57cec5SDimitry Andric 310*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 311*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(size_type __cap, size_type __start, __alloc_rr& __a) 312*0b57cec5SDimitry Andric : __end_cap_(nullptr, __a) 313*0b57cec5SDimitry Andric{ 314*0b57cec5SDimitry Andric __first_ = __cap != 0 ? __alloc_traits::allocate(__alloc(), __cap) : nullptr; 315*0b57cec5SDimitry Andric __begin_ = __end_ = __first_ + __start; 316*0b57cec5SDimitry Andric __end_cap() = __first_ + __cap; 317*0b57cec5SDimitry Andric} 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 320*0b57cec5SDimitry Andricinline 321*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer() 322*0b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) 323480093f4SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __default_init_tag()) 324*0b57cec5SDimitry Andric{ 325*0b57cec5SDimitry Andric} 326*0b57cec5SDimitry Andric 327*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 328*0b57cec5SDimitry Andricinline 329*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__alloc_rr& __a) 330*0b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 331*0b57cec5SDimitry Andric{ 332*0b57cec5SDimitry Andric} 333*0b57cec5SDimitry Andric 334*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 335*0b57cec5SDimitry Andricinline 336*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(const __alloc_rr& __a) 337*0b57cec5SDimitry Andric : __first_(nullptr), __begin_(nullptr), __end_(nullptr), __end_cap_(nullptr, __a) 338*0b57cec5SDimitry Andric{ 339*0b57cec5SDimitry Andric} 340*0b57cec5SDimitry Andric 341*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 342*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::~__split_buffer() 343*0b57cec5SDimitry Andric{ 344*0b57cec5SDimitry Andric clear(); 345*0b57cec5SDimitry Andric if (__first_) 346*0b57cec5SDimitry Andric __alloc_traits::deallocate(__alloc(), __first_, capacity()); 347*0b57cec5SDimitry Andric} 348*0b57cec5SDimitry Andric 349*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 350*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c) 351*0b57cec5SDimitry Andric _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value) 352*0b57cec5SDimitry Andric : __first_(_VSTD::move(__c.__first_)), 353*0b57cec5SDimitry Andric __begin_(_VSTD::move(__c.__begin_)), 354*0b57cec5SDimitry Andric __end_(_VSTD::move(__c.__end_)), 355*0b57cec5SDimitry Andric __end_cap_(_VSTD::move(__c.__end_cap_)) 356*0b57cec5SDimitry Andric{ 357*0b57cec5SDimitry Andric __c.__first_ = nullptr; 358*0b57cec5SDimitry Andric __c.__begin_ = nullptr; 359*0b57cec5SDimitry Andric __c.__end_ = nullptr; 360*0b57cec5SDimitry Andric __c.__end_cap() = nullptr; 361*0b57cec5SDimitry Andric} 362*0b57cec5SDimitry Andric 363*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 364*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::__split_buffer(__split_buffer&& __c, const __alloc_rr& __a) 365480093f4SDimitry Andric : __end_cap_(nullptr, __a) 366*0b57cec5SDimitry Andric{ 367*0b57cec5SDimitry Andric if (__a == __c.__alloc()) 368*0b57cec5SDimitry Andric { 369*0b57cec5SDimitry Andric __first_ = __c.__first_; 370*0b57cec5SDimitry Andric __begin_ = __c.__begin_; 371*0b57cec5SDimitry Andric __end_ = __c.__end_; 372*0b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 373*0b57cec5SDimitry Andric __c.__first_ = nullptr; 374*0b57cec5SDimitry Andric __c.__begin_ = nullptr; 375*0b57cec5SDimitry Andric __c.__end_ = nullptr; 376*0b57cec5SDimitry Andric __c.__end_cap() = nullptr; 377*0b57cec5SDimitry Andric } 378*0b57cec5SDimitry Andric else 379*0b57cec5SDimitry Andric { 380*0b57cec5SDimitry Andric size_type __cap = __c.size(); 381*0b57cec5SDimitry Andric __first_ = __alloc_traits::allocate(__alloc(), __cap); 382*0b57cec5SDimitry Andric __begin_ = __end_ = __first_; 383*0b57cec5SDimitry Andric __end_cap() = __first_ + __cap; 384*0b57cec5SDimitry Andric typedef move_iterator<iterator> _Ip; 385*0b57cec5SDimitry Andric __construct_at_end(_Ip(__c.begin()), _Ip(__c.end())); 386*0b57cec5SDimitry Andric } 387*0b57cec5SDimitry Andric} 388*0b57cec5SDimitry Andric 389*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 390*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>& 391*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::operator=(__split_buffer&& __c) 392*0b57cec5SDimitry Andric _NOEXCEPT_((__alloc_traits::propagate_on_container_move_assignment::value && 393*0b57cec5SDimitry Andric is_nothrow_move_assignable<allocator_type>::value) || 394*0b57cec5SDimitry Andric !__alloc_traits::propagate_on_container_move_assignment::value) 395*0b57cec5SDimitry Andric{ 396*0b57cec5SDimitry Andric clear(); 397*0b57cec5SDimitry Andric shrink_to_fit(); 398*0b57cec5SDimitry Andric __first_ = __c.__first_; 399*0b57cec5SDimitry Andric __begin_ = __c.__begin_; 400*0b57cec5SDimitry Andric __end_ = __c.__end_; 401*0b57cec5SDimitry Andric __end_cap() = __c.__end_cap(); 402*0b57cec5SDimitry Andric __move_assign_alloc(__c, 403*0b57cec5SDimitry Andric integral_constant<bool, 404*0b57cec5SDimitry Andric __alloc_traits::propagate_on_container_move_assignment::value>()); 405*0b57cec5SDimitry Andric __c.__first_ = __c.__begin_ = __c.__end_ = __c.__end_cap() = nullptr; 406*0b57cec5SDimitry Andric return *this; 407*0b57cec5SDimitry Andric} 408*0b57cec5SDimitry Andric 409*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 410*0b57cec5SDimitry Andricvoid 411*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::swap(__split_buffer& __x) 412*0b57cec5SDimitry Andric _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value|| 413*0b57cec5SDimitry Andric __is_nothrow_swappable<__alloc_rr>::value) 414*0b57cec5SDimitry Andric{ 415*0b57cec5SDimitry Andric _VSTD::swap(__first_, __x.__first_); 416*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __x.__begin_); 417*0b57cec5SDimitry Andric _VSTD::swap(__end_, __x.__end_); 418*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __x.__end_cap()); 419e8d8bef9SDimitry Andric _VSTD::__swap_allocator(__alloc(), __x.__alloc()); 420*0b57cec5SDimitry Andric} 421*0b57cec5SDimitry Andric 422*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 423*0b57cec5SDimitry Andricvoid 424*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::reserve(size_type __n) 425*0b57cec5SDimitry Andric{ 426*0b57cec5SDimitry Andric if (__n < capacity()) 427*0b57cec5SDimitry Andric { 428*0b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__n, 0, __alloc()); 429*0b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 430*0b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 431*0b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 432*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 433*0b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 434*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 435*0b57cec5SDimitry Andric } 436*0b57cec5SDimitry Andric} 437*0b57cec5SDimitry Andric 438*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 439*0b57cec5SDimitry Andricvoid 440*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT 441*0b57cec5SDimitry Andric{ 442*0b57cec5SDimitry Andric if (capacity() > size()) 443*0b57cec5SDimitry Andric { 444*0b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 445*0b57cec5SDimitry Andric try 446*0b57cec5SDimitry Andric { 447*0b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 448*0b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(size(), 0, __alloc()); 449*0b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 450*0b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 451*0b57cec5SDimitry Andric __t.__end_ = __t.__begin_ + (__end_ - __begin_); 452*0b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 453*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 454*0b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 455*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 456*0b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS 457*0b57cec5SDimitry Andric } 458*0b57cec5SDimitry Andric catch (...) 459*0b57cec5SDimitry Andric { 460*0b57cec5SDimitry Andric } 461*0b57cec5SDimitry Andric#endif // _LIBCPP_NO_EXCEPTIONS 462*0b57cec5SDimitry Andric } 463*0b57cec5SDimitry Andric} 464*0b57cec5SDimitry Andric 465*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 466*0b57cec5SDimitry Andricvoid 467*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(const_reference __x) 468*0b57cec5SDimitry Andric{ 469*0b57cec5SDimitry Andric if (__begin_ == __first_) 470*0b57cec5SDimitry Andric { 471*0b57cec5SDimitry Andric if (__end_ < __end_cap()) 472*0b57cec5SDimitry Andric { 473*0b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 474*0b57cec5SDimitry Andric __d = (__d + 1) / 2; 475*0b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 476*0b57cec5SDimitry Andric __end_ += __d; 477*0b57cec5SDimitry Andric } 478*0b57cec5SDimitry Andric else 479*0b57cec5SDimitry Andric { 480*0b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 481*0b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 482*0b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 483*0b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 484*0b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 485*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 486*0b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 487*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 488*0b57cec5SDimitry Andric } 489*0b57cec5SDimitry Andric } 490480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), __x); 491*0b57cec5SDimitry Andric --__begin_; 492*0b57cec5SDimitry Andric} 493*0b57cec5SDimitry Andric 494*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 495*0b57cec5SDimitry Andricvoid 496*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_front(value_type&& __x) 497*0b57cec5SDimitry Andric{ 498*0b57cec5SDimitry Andric if (__begin_ == __first_) 499*0b57cec5SDimitry Andric { 500*0b57cec5SDimitry Andric if (__end_ < __end_cap()) 501*0b57cec5SDimitry Andric { 502*0b57cec5SDimitry Andric difference_type __d = __end_cap() - __end_; 503*0b57cec5SDimitry Andric __d = (__d + 1) / 2; 504*0b57cec5SDimitry Andric __begin_ = _VSTD::move_backward(__begin_, __end_, __end_ + __d); 505*0b57cec5SDimitry Andric __end_ += __d; 506*0b57cec5SDimitry Andric } 507*0b57cec5SDimitry Andric else 508*0b57cec5SDimitry Andric { 509*0b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 510*0b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, (__c + 3) / 4, __alloc()); 511*0b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 512*0b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 513*0b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 514*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 515*0b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 516*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 517*0b57cec5SDimitry Andric } 518*0b57cec5SDimitry Andric } 519480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__begin_-1), 520*0b57cec5SDimitry Andric _VSTD::move(__x)); 521*0b57cec5SDimitry Andric --__begin_; 522*0b57cec5SDimitry Andric} 523*0b57cec5SDimitry Andric 524*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 525*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 526*0b57cec5SDimitry Andricvoid 527*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(const_reference __x) 528*0b57cec5SDimitry Andric{ 529*0b57cec5SDimitry Andric if (__end_ == __end_cap()) 530*0b57cec5SDimitry Andric { 531*0b57cec5SDimitry Andric if (__begin_ > __first_) 532*0b57cec5SDimitry Andric { 533*0b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 534*0b57cec5SDimitry Andric __d = (__d + 1) / 2; 535*0b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 536*0b57cec5SDimitry Andric __begin_ -= __d; 537*0b57cec5SDimitry Andric } 538*0b57cec5SDimitry Andric else 539*0b57cec5SDimitry Andric { 540*0b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 541*0b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 542*0b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 543*0b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 544*0b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 545*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 546*0b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 547*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 548*0b57cec5SDimitry Andric } 549*0b57cec5SDimitry Andric } 550480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), __x); 551*0b57cec5SDimitry Andric ++__end_; 552*0b57cec5SDimitry Andric} 553*0b57cec5SDimitry Andric 554*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 555*0b57cec5SDimitry Andricvoid 556*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::push_back(value_type&& __x) 557*0b57cec5SDimitry Andric{ 558*0b57cec5SDimitry Andric if (__end_ == __end_cap()) 559*0b57cec5SDimitry Andric { 560*0b57cec5SDimitry Andric if (__begin_ > __first_) 561*0b57cec5SDimitry Andric { 562*0b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 563*0b57cec5SDimitry Andric __d = (__d + 1) / 2; 564*0b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 565*0b57cec5SDimitry Andric __begin_ -= __d; 566*0b57cec5SDimitry Andric } 567*0b57cec5SDimitry Andric else 568*0b57cec5SDimitry Andric { 569*0b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 570*0b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 571*0b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 572*0b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 573*0b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 574*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 575*0b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 576*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 577*0b57cec5SDimitry Andric } 578*0b57cec5SDimitry Andric } 579480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 580*0b57cec5SDimitry Andric _VSTD::move(__x)); 581*0b57cec5SDimitry Andric ++__end_; 582*0b57cec5SDimitry Andric} 583*0b57cec5SDimitry Andric 584*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 585*0b57cec5SDimitry Andrictemplate <class... _Args> 586*0b57cec5SDimitry Andricvoid 587*0b57cec5SDimitry Andric__split_buffer<_Tp, _Allocator>::emplace_back(_Args&&... __args) 588*0b57cec5SDimitry Andric{ 589*0b57cec5SDimitry Andric if (__end_ == __end_cap()) 590*0b57cec5SDimitry Andric { 591*0b57cec5SDimitry Andric if (__begin_ > __first_) 592*0b57cec5SDimitry Andric { 593*0b57cec5SDimitry Andric difference_type __d = __begin_ - __first_; 594*0b57cec5SDimitry Andric __d = (__d + 1) / 2; 595*0b57cec5SDimitry Andric __end_ = _VSTD::move(__begin_, __end_, __begin_ - __d); 596*0b57cec5SDimitry Andric __begin_ -= __d; 597*0b57cec5SDimitry Andric } 598*0b57cec5SDimitry Andric else 599*0b57cec5SDimitry Andric { 600*0b57cec5SDimitry Andric size_type __c = max<size_type>(2 * static_cast<size_t>(__end_cap() - __first_), 1); 601*0b57cec5SDimitry Andric __split_buffer<value_type, __alloc_rr&> __t(__c, __c / 4, __alloc()); 602*0b57cec5SDimitry Andric __t.__construct_at_end(move_iterator<pointer>(__begin_), 603*0b57cec5SDimitry Andric move_iterator<pointer>(__end_)); 604*0b57cec5SDimitry Andric _VSTD::swap(__first_, __t.__first_); 605*0b57cec5SDimitry Andric _VSTD::swap(__begin_, __t.__begin_); 606*0b57cec5SDimitry Andric _VSTD::swap(__end_, __t.__end_); 607*0b57cec5SDimitry Andric _VSTD::swap(__end_cap(), __t.__end_cap()); 608*0b57cec5SDimitry Andric } 609*0b57cec5SDimitry Andric } 610480093f4SDimitry Andric __alloc_traits::construct(__alloc(), _VSTD::__to_address(__end_), 611*0b57cec5SDimitry Andric _VSTD::forward<_Args>(__args)...); 612*0b57cec5SDimitry Andric ++__end_; 613*0b57cec5SDimitry Andric} 614*0b57cec5SDimitry Andric 615*0b57cec5SDimitry Andrictemplate <class _Tp, class _Allocator> 616*0b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY 617*0b57cec5SDimitry Andricvoid 618*0b57cec5SDimitry Andricswap(__split_buffer<_Tp, _Allocator>& __x, __split_buffer<_Tp, _Allocator>& __y) 619*0b57cec5SDimitry Andric _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) 620*0b57cec5SDimitry Andric{ 621*0b57cec5SDimitry Andric __x.swap(__y); 622*0b57cec5SDimitry Andric} 623*0b57cec5SDimitry Andric 624*0b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD 625*0b57cec5SDimitry Andric 626*0b57cec5SDimitry Andric_LIBCPP_POP_MACROS 627*0b57cec5SDimitry Andric 628*0b57cec5SDimitry Andric#endif // _LIBCPP_SPLIT_BUFFER 629