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